Subject: Re: mmap and anonymous memory.
To: Jeff Roberson <nomad@nop.aliensystems.com>
From: Charles M. Hannum <root@ihack.net>
List: tech-kern
Date: 12/09/1999 17:04:56
Jeff Roberson <nomad@nop.aliensystems.com> writes:

> If I mmap an area of anonymous memory and set the protection bits to
> write only will it be associated with any physical memory?  I assume it
> isn't filled until there is a page fault, which shouldn't fill it anyway
> because it's write only.  Are these assumptions correct?

There are multiple issues here...

1) Pages are not allocated until at least one of them is `touched',
   meaning read or written.

2) On systems (such as x86) which do not support separate read and
   write permission bits in the MMU, write permission does imply read
   permission.  So mapping with PROT_WRITE only is equivalent to using
   PROT_READ|PROT_WRITE.

1) Just because it was mapped with PROT_WRITE, that doesn't mean it
   can never be read.  The permissions could be changed later with
   mprotect(2).  So, even on systems not affected by point #2, the
   page still has to be allocated and the write completed.

> I'd like to put
> a cap on a second stack so that it doesn't overflow onto other memory on
> the heap, but I don't want the cap to waste memory.

You can just leave an unmapped page there -- either explicitly with
munmap(2), or by not mapping it in the first place.