Understanding Linux mlock and munlock: Memory Locking and Reclamation Mechanisms on arm64
On arm64 Linux kernels (e.g., 5.10.27), the mlock system call marks VMAs with VM_LOCKED, forces page faults, sets the PG_mlocked flag and moves pages into the unevictable LRU so they cannot be reclaimed, while munlock clears these flags and returns pages to regular LRU lists, guaranteeing resident memory for latency‑sensitive applications.
Linux provides the mlock system call so that user‑space programs can lock physical pages associated with a virtual address range, preventing those pages from being reclaimed when memory pressure occurs. This is essential for latency‑sensitive workloads that cannot tolerate page faults.
The analysis is performed on the arm64 architecture running Linux kernel version 5.10.27, and it follows the relevant kernel source code to explain how mlock locks pages and how the kernel prevents the locked pages from being swapped out.
Active page‑fault handling : When mlock is invoked, the kernel adds the VM_LOCKED flag to vma->vm_flags . Because the address range may span multiple VMAs, the code may split or merge VMAs, but each resulting VMA receives the VM_LOCKED flag. Then __mm_populate is called to populate the page tables, and faultin_page (via handle_mm_fault ) actively triggers a page fault for each virtual page that does not yet have a physical backing.
Memory reclamation – scanning the active LRU : During the scan of the active LRU list, the kernel uses reverse mapping to locate every VMA that maps a given physical page. If a VMA has VM_LOCKED set, the reverse‑mapping walk exits early and the page is excluded from reference‑counting work, ensuring the page remains resident.
Memory reclamation – scanning the inactive LRU : When the inactive LRU is scanned, the reverse‑mapping walk again encounters VMAs with VM_LOCKED . The kernel immediately returns from the walk, and page_check_references reports PAGEREF_RECLAIM . The caller then proceeds to shrink_page_list for further processing.
Page unmapping before reclamation : shrink_page_list invokes try_to_unmap to detach all page‑table entries that map the target page. For each VMA that maps the page, if VM_LOCKED is set, mlock_vma_page is called.
mlock_vma_page : This routine sets the PG_mlocked flag in the page descriptor, updates the zone’s NR_MLOCK counter, removes the page from its original LRU list, and inserts it into the “unevictable” (un‑reclaimable) LRU list. Consequently, the page is no longer considered by the normal reclamation scans.
munlock processing : The munlock system call clears the VM_LOCKED flag in the VMA and clears PG_mlocked in the page descriptor. The page is then moved back from the unevictable LRU to the appropriate regular LRU. If a page is shared by multiple VMAs, try_to_munlock walks the reverse‑mapping list; if any VMA still has VM_LOCKED , the page remains locked, otherwise it is re‑inserted into a reclaimable LRU.
Summary : For applications with strict timing requirements, mlock guarantees that the required physical pages are pre‑faulted and placed in an unevictable LRU, while munlock safely releases those pages. This two‑step mechanism ensures that locked pages stay resident in memory and are excluded from the kernel’s normal page‑reclamation paths.
OPPO Kernel Craftsman
Sharing Linux kernel-related cutting-edge technology, technical articles, technical news, and curated tutorials
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.