2 ms·
> mmap will go and create the page table mappings (entries) for that address space (~262144 of them with 4k pages) It does not, actually[1]. Page tables are a
by ajross 7y ago
> mmap will go and create the page table mappings (entries) for that address space (~262144 of them with 4k pages)
It does not, actually[1]. Page tables are a hardware datastructure with significant overhead. All the kernel does at mmap() time is create the a virtualized record of the mapping. It's not until the process takes a page fault to a mapped address that the hardware page tables are allocated and filled out. In practice this is a win because (especially with use cases like malloc) most processes tend to map large chunks of which they'll only use a subset in the short term.
[1] By default. There is a MAP_POPULATE flag you can pass that will allocate the page tables and allocate/prefault all the mapped pages before the system call returns.