9
votes

The mmap documentation says following about the flag MAP_NORESERVE.

Do not reserve swap space for this mapping. When swap space is reserved, one has the guarantee that it is possible to modify the mapping. When swap space is not reserved one might get SIGSEGV upon a write if no physical memory is available.

What I want actually is to only reserve virtual memory addresses and not have actual physical memory allocated. Can this be done with mmap with MAP_NORESERVE? If I want to use any physical memory, I would mmap it again with MAP_FIXED in the address range within the one alloted through the mmap with MAP_NORESERVE.

To summarize, I want the kernel to not reserve any physical page for memory allocated with mmap with MAP_NORSERVE flag. Does it really work like this or do the kernel allocates physical pages if it has enough physical memory?

3
MAP_RESERVE is not about physical memory (RAM) but about backing-storage (swap-space). Physical memory is never reserved (except a few special applications such as kernel memory or shmem)wildplasser
What really is swap-space? And how MAP_RESERVE is advantageous in this regard, that is, not allocating swap-space is useful?MetallicPriest

3 Answers

6
votes

Mmap() is one of the ways to manage the association between {address, Physical memory, disk-blocks} All three members of this association are resources. The association is kept inside Page Table Entries (PTE's)

What mmap() actually does, is:

  • [maybe] allocate an address-range inside the user process. This range must consist of consecutive addresses (should not overlap with existing ranges)
  • create PTEs for the requested range and make them point to the pages within the address-range
  • make the PTE's point to the file being mmap()ed
  • [maybe] allocate and prefetch (some) pages
  • [maybe] reserve some backing storage.

Many (3 out of 5) of the above steps are optional and depend on the actual arguments and flags supplied in the mmap() call. (the fd may be -1: creating an anonymous mapping, the start-adress may be NULL: mmap should allocate a (previously) unused range of memory)

After a call to mmap(), the pagefault-handler inside the kernel should be able to find out what to do. (attach physical ram to a page; flush and detach; allocate and COW, ...)

not reserving swapspace means that the caller trusts that there will be enough swap space at any time in the future. Swap space is shared by all the processes, so there can never be a guarantee that there is enough of it. Preallocating it (more or less) gives a guaranty that the calling process will always have enough of it. (when not: the mmap() should have failed)

3
votes

On linux mmap sets up virtual memory mappings only, whether you use MAP_NORESERVE or not, no physical memory is assigned until you touch the memory.

MAP_FIXED is unrelated to this, it sets up the virtual memory mappings at the virtual(not physical) address you specify, or fails if there's no room for the mapping at that address.

3
votes

Just use a normal mmap(). Any modern enough OS (ie, past, what, 1995?) which implements mmap also implements demand paging, and pages will only be reserved if you actually write to them.