Memory Management
Relevant source files
The following files were used as context for generating this wiki page:
Memory management in the Linux kernel handles the allocation, deallocation, and mapping of physical and virtual memory. It abstracts hardware details to provide a consistent interface for kernel subsystems and user-space programs. Key responsibilities include implementing virtual memory, demand paging, and managing the page cache to optimize disk I/O.
Sources: Documentation/admin-guide/mm/index.rst, Documentation/admin-guide/mm/concepts.rst:8-30
Virtual Memory and Page Tables
The kernel uses virtual memory to abstract physical address ranges. Each memory access uses a virtual address that the CPU translates to a physical address via a hierarchical page table structure. Linux supports a five-level page table hierarchy to manage large address spaces efficiently.
Sources: Documentation/admin-guide/mm/concepts.rst:32-60, Documentation/mm/page_tables.rst:11-20
Page Table Hierarchy
The hierarchy consists of the following levels, from top to bottom:
- PGD (Page Global Directory): The top-level table referenced in
struct mm_struct. - P4D (Page Level 4 Directory): Supports 5-level paging.
- PUD (Page Upper Directory): Used in 4-level paging.
- PMD (Page Middle Directory): The hierarchy level above the PTE.
- PTE (Page Table Entry): The lowest level that maps a virtual page to a physical page frame number (PFN).
Sources: Documentation/mm/page_tables.rst:54-105
The diagram shows the standard five-level translation path from a global directory to an individual physical page frame.
Sources: Documentation/mm/page_tables.rst:75-87
Page Fault Handling
When the MMU cannot find a translation in the Translation Lookaside Buffer (TLB) or the page tables, it triggers a page fault. The kernel processes this fault to allocate pages lazily or swap data from disk.
Sources: Documentation/mm/page_tables.rst:129-160
This sequence illustrates the interaction between hardware (CPU/MMU) and software (Kernel) during an address translation failure.
Sources: Documentation/mm/page_tables.rst:162-185
Physical Memory Organization
Physical memory is grouped into Nodes and Zones to accommodate hardware restrictions and Non-Uniform Memory Access (NUMA) architectures.
Sources: Documentation/admin-guide/mm/concepts.rst:95-117
Memory Zones
Hardware constraints often limit access to specific physical address ranges. The kernel categorizes memory into zones:
| Zone Name | Description |
|---|---|
ZONE_DMA | Memory usable by devices that cannot address the full range of memory. |
ZONE_NORMAL | Standard addressed pages used for most kernel and user operations. |
ZONE_HIGHMEM | Memory not permanently mapped into the kernel address space (on 32-bit systems). |
| Sources: Documentation/admin-guide/mm/concepts.rst:106-117 |
NUMA Nodes
Multi-processor systems organize memory into banks called nodes. Each node has its own memory management subsystem, including free/used page lists and statistics. This reduces latency by prioritizing local memory access for each CPU.
Sources: Documentation/admin-guide/mm/concepts.rst:119-126
Allocation Mechanisms
The kernel provides different allocators tailored to specific size and continuity requirements.
Sources: Documentation/core-api/mm-api.rst:18-50
SLAB/SLUB Allocator
The Slab allocator manages caches of frequently used objects to reduce fragmentation and allocation overhead. It provides kmalloc() for small, physically contiguous allocations.
Sources: Documentation/core-api/mm-api.rst:24-30, mm/slub.c
Vmalloc
vmalloc() provides virtually contiguous memory mappings for large buffers. Unlike kmalloc(), the underlying physical pages do not need to be contiguous.
Sources: Documentation/core-api/mm-api.rst:33-35, mm/vmalloc.c
Page Allocator
The buddy system manages physical page frames. It satisfies requests for power-of-two blocks of contiguous pages.
Sources: mm/page_alloc.c, Documentation/admin-guide/mm/concepts.rst:187-200
Memory Reclaim and OOM
When free memory drops below the low watermark, the kswapd daemon asynchronously reclaims pages. If memory reaches the min watermark, the kernel triggers direct reclaim, stalling allocations until space is freed.
Sources: Documentation/admin-guide/mm/concepts.rst:174-200
If the system cannot reclaim enough memory to continue operation, it invokes the Out-of-Memory (OOM) Killer. This mechanism selects and terminates a process to release memory and prevent a system-wide crash.
Sources: Documentation/admin-guide/mm/concepts.rst:216-222, Documentation/mm/page_tables.rst:187-195
Monitoring Memory Status
The kernel exposes memory statistics through the /proc/meminfo interface. This file provides real-time data on various memory categories.
Sources: fs/proc/meminfo.c:40-100
| Field | Source Counter | Description |
|---|---|---|
MemTotal | i.totalram | Total usable physical RAM. |
MemFree | i.freeram | Sum of low and high memory free pages. |
Buffers | i.bufferram | Temporary storage for raw disk blocks. |
Cached | NR_FILE_PAGES | Physical memory used for caching files. |
SwapTotal | i.totalswap | Total amount of swap space available. |
| Sources: fs/proc/meminfo.c:50-80 |
The memory management subsystem coordinates these complex structures to ensure stability and performance across diverse hardware architectures, from microcontrollers to large NUMA servers.
Sources: Documentation/admin-guide/mm/concepts.rst:8-15