Lightweight Guard Pages
This release introduces lightweight guard pages which provide a way to mark regions of virtual memory so that they trigger segmentation faults (SIGSEGV) when accessed. This feature is important for thread stacks and userland memory allocators. The mechanism is designed to remove any memory overhead, by using guard markers rather than creating or splitting Virtual Memory Areas (VMAs).
Before lightweight guard pages, similar functionality was achieved by using mmap(..,
PROT_NONE), which incurred memory overhead. As processes and threads scale up using
this method, overhead increases. Additionally, memory that's mapped in this way remains
unavailable for allocation to user processes. By using lightweight guard pages, the overhead
is avoided and significant memory gains are achieved.
The update uses new madvise() commands:
- MADV_GUARD_INSTALL installs guard markers and removes existing mappings in the range. Installation applies to anonymous-memory-only and installation isn't allowed for special, huge, or locked (mock'ed) VMAs.
- MADV_GUARD_REMOVE removes only the guard markers, keeping any normal mappings untouched.
Guarded ranges persist over MADV_DONTNEED or MADV_FREE (guaranteed protection until removed), but are cleared with process teardown or explicit unmapping.