How Linux Organizes Memory: From Physical Pages to Virtual Address Mapping
This article explains how Linux manages memory by organizing physical pages, using zones and nodes, allocating memory with the buddy system and SLUB, structuring user and kernel virtual address spaces, and translating virtual addresses to physical ones via page tables, TLBs, and dynamic mapping.
1. Physical Memory
Linux divides physical memory into three hierarchical levels: pages, zones, and nodes.
Physical Memory Organization
A page is the smallest unit, typically 4 KB.
A zone groups pages into multiple queues for management. Linux defines three zones:
ZONE_DMA – used for DMA‑capable I/O devices (kernel‑only).
ZONE_NORMAL – stores kernel data (kernel‑only).
ZONE_HIGHMEM – holds user‑process data.
A node corresponds to a CPU and contains one of each zone. When a CPU’s memory is exhausted, memory from another CPU’s node can be allocated.
Physical Memory Allocation
Linux uses two allocation strategies:
Big memory allocation employs the buddy system , which groups pages into blocks of sizes 1, 2, 4, 8 … up to 1024 pages. When a request for a block of size (2i‑1, 2i] arrives, the allocator finds the smallest suitable block, splits larger blocks if necessary, and updates the free lists.
Small memory allocation uses the SLUB allocator. SLUB reserves a few pages as caches, maintains linked lists of free objects, and allocates objects directly from these lists without zero‑clearing them each time.
2. How to Organize Virtual Addresses
Virtual addresses form a virtual address space that maps to physical memory. The space is divided into user space and kernel space.
In a 32‑bit system, the virtual address space is split 1:3 between kernel and user; in a 64‑bit system, each receives 128 TB.
User‑space layout includes sections for text (code), data, BSS (global variables), heap, stack, and the mmap region.
Kernel‑space layout contains:
Direct mapping area (≈896 MB) that maps a fixed offset of physical memory in ZONE_DMA and ZONE_NORMAL.
Dynamic mapping area, which can map any physical page in ZONE_HIGHMEM and can be re‑mapped when pages are freed.
Dynamic mapping types:
Dynamic memory mapping – pages can be remapped after use.
Permanent mapping – a virtual address is bound to a single physical page until explicitly unmapped.
Fixed mapping – only specific kernel functions may use the mapping.
The difference between direct and dynamic mapping is that direct mapping uses a fixed offset calculation, while dynamic mapping relies on page‑table entries.
3. Mapping Virtual Addresses to Physical Memory
Virtual addresses are translated to physical addresses via page tables. Both virtual and physical memory are divided into 4 KB pages, establishing a one‑to‑one correspondence.
In a 32‑bit system, a virtual address is split into three fields:
10 bits – index into the first‑level page‑table directory.
10 bits – index into the second‑level page‑table entry.
12 bits – offset within the 4 KB page.
This three‑level scheme reduces the need for contiguous memory for page tables. A 64‑bit system uses a five‑level page table hierarchy.
TLB
The Translation Lookaside Buffer (TLB) is a CPU cache that stores recent virtual‑to‑physical address translations. On a memory access, the TLB is consulted first; a miss triggers a page‑table walk.
Virtual Memory
Virtual memory extends RAM by using a swap area on disk. Pages not currently needed are moved to swap, allowing programs to use more memory than physically available, albeit with slower access due to disk I/O.
Conclusion
Both kernel and user spaces have separate virtual address ranges that map to physical memory via page tables. User processes can only map and access user‑space memory, while the kernel accesses kernel‑space memory. System calls transition execution from user to kernel space, requiring data copies between the two spaces.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
