Subject: Re: 'growable' mmap regions
To: Antti Kantee <pooka@cs.hut.fi>
From: Jaromir Dolecek <jdolecek@NetBSD.org>
List: tech-kern
Date: 08/29/2004 16:23:55
Antti Kantee wrote:
> On Sat Aug 28 2004 at 00:59:16 +0200, Jaromir Dolecek wrote:
> > Linux has concept of 'growable' mmap region. This is used for thread
> > stack memory - memory is allocated using mmap(2) with special flag
> > and passed as stack for a new thread via clone(2).  Then the memory
> > block is automatically extended when the page fault happens below
> > the currently-mapped memory block, up to the per-thread stack size
> > limit.
> > 
> > I'm thinking of a good way to emulate this. Do we want to be able
> > to have stack-like memory segments like this? Currently the page
> > fault code handling growing of stack hardcodes check against USRSTACK, so
> > it's limited only to the (single) stack allocated on process execution.
> 
> What I did in the FreeBSD comapt code for MAP_STACK was to not bother
> with it, for reasons you state below.  I just adjusted the flags to map
> normal MAP_ANON memory, and it's worked well so far.

NetBSD has just ignored the flag too. However, this causes problem
when (cloned) thread runs out of the initial stack. Thread SIGSEGVs
on NetBSD - but on Linux the stack auto-extend feature kicks in,
the stack is increased as required (up to stack size limit, of
course) and it works.

Solution here is to allocate memory sized at the current stack size
limit, and adjusting the requested and returned address of mmap()
call accordingly. This is what I implemented in NetBSD
linux_misc.c rev 1.128.

Looking at it from application standpoint, the 'grow' scheme is
quite a lot harder to use in practice  - for example, all mmap()s
must effectively use MAP_FIXED on Linux if there exists any mapping
with MAP_GROWSDOWN, in order to provide and avoid the address space
intended for eventual stack growth. Since it doesn't make any
difference in memory use and is quite error-prone, I think it was
a mistake to introduce the feature in Linux.

Jaromir
-- 
Jaromir Dolecek <jdolecek@NetBSD.org>            http://www.NetBSD.cz/
-=- We should be mindful of the potential goal, but as the Buddhist -=-
-=- masters say, ``You may notice during meditation that you        -=-
-=- sometimes levitate or glow.   Do not let this distract you.''   -=-