Using mmap(2) with a hint address

Artem Belevich art at freebsd.org
Tue Dec 20 17:49:35 UTC 2011


Hi,

On Tue, Dec 20, 2011 at 7:03 AM, Andriy Gapon <avg at freebsd.org> wrote:
> on 20/12/2011 16:31 Ganael LAPLANCHE said the following:
>> On Tue, 20 Dec 2011 15:02:01 +0100 (CET), Ganael LAPLANCHE wrote
>>
>>> But there is still something I don't understand : on the Linux
>>> machine where I ran my test program, the current RLIMIT_DATA
>>> is set to 0xffffffff/0xffffffff and I can manage to mmap at
>>> address 0x20000000. If I set the same limit on FreeBSD, I
>>> won't get the mapping at 0x20000000. So, there *is* a
>>> difference of behaviour between the two systems, but I don't
>>> understand why.
>>
>> Well, in fact, two things remain not very clear for me :
>>
>> - Why are mmap()s performed *after* data segment ?
>>   => It seems they can go within, on GNU/Linux and NetBSD.
>>
>> - Why do we have such a default value for datasize (8.2, amd64) :
>>
>> $ limits
>> Resource limits (current):
>>   cputime              infinity secs
>>   filesize             infinity kB
>>   datasize             33554432 kB
>>
>> this is HUGE !
>
> Just a guess - this might be some sort of optimization to keep virtual address
> range of dynamic allocations untouched by unrelated mmap calls.  Not sure if
> that's so and how useful could that be.

Something like that. In the past heap allocator used to get memory
from system via sbrk(). It still may do so, if you set
MALLOC_OPTIONS=D. The problem is that sbrk() can't advance past an
area used by something else (i.e. mmaped region) so kernel makes an
effort to leave a lot of unused address space which sbrk() may claim
later on. These days malloc() by default uses mmap, so if you don't
force it to use sbrk() you can probably lower MAXDSIZE and let kernel
use most of address space for hinted mmaps.

That said, unless you use MAP_FIXED, malloc is not guaranteed to pay
attention to hints and the app must be able to deal with that. FreeBSD
kernel behavior is just one possible scenario that may affect mmap
behavior. Behavior may also change between architectures, or due to
preceeding mmaps (think of dynamic linker mapping in shared
libraries). If an application relies on hints having effect without
MAP_FIXED, it's the app that needs fixing, IMHO.

--Artem

> svn log / svn annotate of the file may reveal more details.
>
> --
> Andriy Gapon
> _______________________________________________
> freebsd-stable at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-stable
> To unsubscribe, send any mail to "freebsd-stable-unsubscribe at freebsd.org"


More information about the freebsd-stable mailing list