Master Linux Memory Management: Virtual, Physical, and Kernel Spaces Explained
This article provides a comprehensive guide to Linux memory management on x86 32‑bit systems, covering virtual and physical address spaces, kernel zones, user‑space regions, key data structures, and practical commands for inspecting memory layout.
Virtual Address
Linux provides each 32‑bit process with an isolated 4 GB virtual address space. Physical pages are allocated on demand when a process accesses a virtual address, and the MMU translates the virtual address to a physical address.
Benefits
Prevents user code from accessing physical memory directly, protecting the kernel.
Allows a process to use an address space larger than the available RAM.
The 4 GB space is divided into user space (0x00000000‑0xBFFFFFFF) and kernel space (0xC0000000‑0xFFFFFFFF).
Physical Address Translation
When a page fault occurs, the kernel loads the required page and uses the MMU’s segment‑page translation to map the virtual address to a physical address.
Physical memory is organized into three zones:
ZONE_DMA : 0 MB – 16 MB, used by legacy ISA devices.
ZONE_NORMAL : 16 MB – 896 MB, directly mapped into the kernel address space.
ZONE_HIGHMEM : > 896 MB, not directly mapped; accessed via temporary or permanent mappings.
User Space Layout
User space (0x00000000‑0xBFFFFFFF) is split into five regions with distinct access attributes:
Code segment : executable instructions, read‑only.
Data segment : initialized global/static variables.
BSS segment : uninitialized globals, zero‑filled at load time.
Heap : memory allocated by malloc / free, grows upward.
Stack : local variables and call frames, grows downward.
On i386 the stack grows toward lower addresses while the heap expands toward higher addresses.
Inspect a binary’s memory layout with the size command:
[lemon ~]# size /usr/local/sbin/sshd
text data bss dec hex filename
1924532 12412 4268962363840 2411c0 /usr/local/sbin/sshdKernel Space Layout (32‑bit x86)
Kernel virtual addresses range from 0xC0000000 to 0xFFFFFFFF (1 GB).
Direct Mapping Region
The first 896 MB of kernel space is a direct‑mapping region where linear addresses map one‑to‑one with physical addresses using the offset PAGE_OFFSET = 0xC0000000. The conversion is linear = PAGE_OFFSET + physical. The kernel helper virt_to_phys() performs this translation.
High‑Memory Linear Address Space
The remaining 128 MB (0xF8000000‑0xFFFFFFFF) is reserved for high‑memory mappings, allowing the kernel to address physical memory beyond the direct‑mapping limit.
Dynamic Kernel Mapping (vmalloc)
The vmalloc API allocates virtually contiguous kernel memory whose underlying physical pages may be scattered. Allocations reside between vmalloc_start and vmalloc_end and are described by a vm_struct object.
Persistent Kernel Mapping
High‑memory pages can be accessed via alloc_page(_GFP_HIGHMEM) and then mapped with kmap().
Fixed Mapping Region
A small 4 KB window at the top of the kernel address space holds fixed mappings for special purposes (e.g., ACPI_BASE).
Memory Management Data Structures
User‑Space
Each user‑space region is represented by a vm_area_struct. A process may have multiple vm_area_struct instances linked in a doubly‑linked list and indexed in a red‑black tree for fast lookup.
Kernel‑Space Dynamic Allocation
Dynamic kernel mappings allocated by vmalloc are described by a vm_struct. Each vm_struct occupies a 4 KB guard area and is linked between vmalloc_start and vmalloc_end. Physical pages are attached on demand.
References
《Linux内核设计与实现(原书第3版)》
Linux内存管理 https://cloud.tencent.com/developer/article/1515762
linux 内存管理初探 https://cloud.tencent.com/developer/article/1005671
linux内存管理源码分析 - 页框分配器 https://www.cnblogs.com/tolimit/p/4551428.html
Linux内核--内核地址空间分布和进程地址空间 https://my.oschina.net/wuqingyi/blog/854382
Linux内存管理 http://gityuan.com/2015/10/30/kernel-memory/
Linux Used内存到底哪里去了? http://blog.yufeng.info/archives/2456
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.
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.)
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.
