Fundamentals 7 min read

Understanding Linux Memory Management: Address Mapping, Buddy System, and Page Faults

This article explains how Linux maps logical to physical addresses, describes the buddy and slab memory allocation mechanisms, outlines their limitations and improvements such as slub and vmalloc, and details the page‑fault handling process including registers involved and swap behavior.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Understanding Linux Memory Management: Address Mapping, Buddy System, and Page Faults

Address Mapping

In Linux, the address translation process follows the sequence logical address → linear address → physical address. The physical address is the value transmitted on the address bus, while linear and logical addresses represent conversion rules. The mapping is performed by the MMU using registers such as CR0 and CR3.

The logical address in Linux is equivalent to the linear address, simplifying the translation process.

Memory Management Methods

During boot, the kernel probes memory size and uses a simple bitmap allocator called bootmem . Because bootmem is inefficient for larger allocations, the buddy system is employed. It maintains free blocks of sizes that are powers of two; allocating a 3‑page block, for example, takes a 4‑page block and splits the remainder.

The buddy relationship can be computed with the following inline function:

static inline unsigned long __find_buddy_index(unsigned long page_idx, unsigned int order) { return page_idx ^ (1 << order); }

This shows that adjacent blocks are not always buddies; for example, blocks 0‑1 and 2‑3 are buddies, but 1‑2 are not.

To reduce fragmentation for small objects, Linux uses the slab allocator, which obtains larger chunks from the buddy system and subdivides them. However, slab suffers from complex queue management, high overhead, long partial queues, and difficult NUMA support.

Improvements include slub , which redesigns page structures and provides per‑CPU caches, and slob , a lightweight slab alternative for embedded systems.

For large contiguous allocations, the vmalloc mechanism stitches together non‑contiguous pages, effectively using “scrap” memory to create a larger virtual block.

Page Fault Handling

When a process accesses a virtual address that has no physical page mapped, the CPU raises a page‑fault exception. The kernel receives the following information:

cr2 – the linear address that caused the fault.

err_code – a code indicating the fault reason.

regs – the register state at the time of the fault.

The handling flow involves locating a free page (or swapping one in), updating page tables, and resuming the process. If the needed page has been swapped out, the swap subsystem is invoked; typical commands to inspect swap usage include cat /proc/buddyinfo and related utilities.

If memory is mapped with mmap, reads or writes to that region can also trigger page faults.

Overall, Linux’s memory management combines address translation, buddy and slab allocators, and sophisticated page‑fault handling to provide both performance and flexibility for a wide range of workloads.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Memory ManagementKernelPage Faultbuddy systemSlab Allocator
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.