Question regarding mmap() with MAP_FIXED flag

From: Jongman Heo
Date: Mon Jun 13 2011 - 17:17:18 EST


Dear all,

Please let me ask a dumb question.

I use following mmap call, to get the same virtual address with given
physical address.
And when reading the mmap-ed memory, it seems that the virtual address
correctly points to the physical address.

ptr = mmap((void*)0x400000, getpagesize(), PROT_READ, MAP_PRIVATE |
MAP_FIXED , mem_fd, 0x400000);

where mem_fd is the file descriptor of /dev/mem.

And using memmap boot arguments, 4M RAM location is not used by Linux.
The area is written by bootloader. (.. memmap=exactmap memmap=64k@0
memmap=3M@1M memmap=1019M@5M)


It works, but I'm afraid that the possiblity of mmap() failure with
MAP_FIXED flag.
man page of mmap states following:

MAP_FIXED Do not select a different address than the one
specified. If the memory region speci-
fied by start and len overlaps pages of any
existing mapping(s), then the overlapped
part of the existing mapping(s) will be discarded.
If the specified address cannot be
used, mmap() will fail. If MAP_FIXED is specified,
start must be a multiple of the page
size. Use of this option is discouraged.


Q1. What does the existing mapping mean?
Another mmap mapping by any other user space process? Or it means that
the virtual address (0x400000 in my case) is already acquired/used by
others (kernel, process, or whatever)?

Q2. When can't the specified address be used?

Q3. Is there any solution such that mmap with MAP_FIXED being not
failed but always succeed?


Thanks in advance,
Jongman Heo.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/