Understanding Linux Memory Management: From Address Mapping to Page Faults
This article explains how Linux manages memory by covering address translation, the buddy and slab allocation systems, the role of vmalloc and kmap, and the detailed handling of page faults, providing programmers with essential knowledge of kernel and user‑space memory operations.
Most servers run on Linux, so programmers need a basic understanding of how the system manages memory. The key topics are address mapping, memory management methods, and page‑fault handling.
Address Mapping
In Linux, a logical address is translated to a linear address and then to a physical address. The MMU performs this translation using registers such as CR0 and CR3. Linux simplifies the process by making the logical address equal to the linear address.
Memory Management Methods
During boot, Linux detects memory size and uses a simple bitmap allocator called bootmem . For better efficiency, the buddy system allocates memory in powers‑of‑two blocks, allowing fast allocation and deallocation.
static inline unsigned long __find_buddy_index(unsigned long page_idx, unsigned int order) { return page_idx ^ (1 << order); // update highest bit, 0↔1 swap }
The buddy system works at the page (4 KB) granularity, but allocating whole pages for small objects is wasteful. Linux therefore uses the slab allocator, which obtains larger chunks from the buddy system and subdivides them for small objects.
On large‑scale SMP and NUMA systems, slab shows drawbacks such as complex queue management and high overhead. The slub allocator redesigns the page structure to reduce overhead, while slob provides a lightweight slab‑like layer for embedded systems.
Large Memory Allocation
When the buddy system cannot provide a contiguous block large enough (e.g., allocating 10 × 4 KB pages may require a 16‑page free list), vmalloc stitches together non‑contiguous pages to create a larger virtual area.
High‑memory pages can be accessed via kmap , which provides a linear address for a page.
Process Address Space and COW
A process consists of code, libraries, global data, heap, and stack, each mapped into its own virtual address space. When malloc is called, only virtual addresses are reserved; physical memory is allocated on demand using the copy‑on‑write (COW) technique.
Page‑Fault Handling
If a process accesses a virtual address that has no physical page mapped, the CPU raises a page‑fault exception. The kernel receives the fault information:
CR2 register – the faulting linear address.
err_code – reason for the exception.
regs – register state at the time of the fault.
The handling flow involves locating or allocating a free page, updating page tables, and resuming the process.
If the needed page has been swapped out, the kernel swaps it back in using commands such as swap. Memory mapped via mmap also triggers page faults on read/write accesses.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
