Re: don't let mmap allocate down to zero

From: linux-os
Date: Wed Jan 26 2005 - 18:27:03 EST


On Wed, 26 Jan 2005, Olivier Galibert wrote:

On Wed, Jan 26, 2005 at 11:38:15AM -0500, linux-os wrote:
On Wed, 26 Jan 2005, Rik van Riel wrote:

With some programs the 2.6 kernel can end up allocating memory
at address zero, for a non-MAP_FIXED mmap call! This causes
problems with some programs and is generally rude to do. This
simple patch fixes the problem in my tests.

Does this mean that we can't mmap the screen regen buffer at
0x000b8000 anymore?

No. Missed the "non-MAP_FIXED" part? You can always map at 0, you
just have to ask for it.


Okay.


What 'C' standard do you refer to?

Malloc uses mmap to get more memory. Malloc returning 0 means no
memory, not "the memory happens to be at 0". Not that easy to fix in
the glibc if you want to keep the "segfault on null pointer accesses"
debugging help too.


malloc is a runtime library. It has its own documented rules.

Given that the man page itself says that unless you're using MAP_FIXED
start is only a hint and you should use 0 if you don't care things can
get real annoying real fast. Imagine if you want to mmap a <4K file
and mmap then returns 0, i.e. NULL, as the mapping address as you
asked. It's illegal from the point of view of susv3[1] and it's real
annoying in a C/C++ program.

mmap() can (will) return 0 if you use 0 as the hint and use MAP_FIXED
at 0. That's the reason why one does NOT check for NULL with mmap() but
for MAP_FAILED (which on this system is (void *)-1.


OG.

[1]
When MAP_FIXED is not set, the implementation uses addr in an
implementation-defined manner to arrive at pa. The pa so chosen
shall be an area of the address space that the implementation deems
suitable for a mapping of len bytes to the file. All implementations
interpret an addr value of 0 as granting the implementation complete
freedom in selecting pa, subject to constraints described below. A
non-zero value of addr is taken to be a suggestion of a process
address near which the mapping should be placed. When the
implementation selects a value for pa, it never places a mapping at
address 0, nor does it replace any extant mapping.




Cheers,
Dick Johnson
Penguin : Linux version 2.6.10 on an i686 machine (5537.79 BogoMips).
Notice : All mail here is now cached for review by Dictator Bush.
98.36% of all statistics are fiction.
-
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/