🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Linux Kernel Memory Management: Understanding the VM Split and Process Virtual Address Space
If you are learning Linux kernel development or working on Linux device drivers, understanding how memory is organized inside the kernel is not optional — it is essential. This tutorial is part of our free Linux kernel development course on EmbeddedPathashala and explains memory management internals from the ground up in a practical, beginner-friendly way.
Memory management in Linux is the backbone of everything: how processes run, how the kernel allocates buffers, how drivers interact with hardware, and how the system stays stable under load. Getting this foundation right will make you a far better kernel and driver developer.
- What virtual memory is and why the Linux kernel uses it
- How the Virtual Memory (VM) split divides address space between user and kernel
- What a Process Virtual Address Space (VAS) looks like on 32-bit and 64-bit systems
- The segments inside a process VAS: text, data, BSS, heap, stack, and library mappings
- How to inspect a process’s memory map using
/proc/<pid>/maps - How shared libraries get mapped into process memory at runtime
- What sparse regions and memory holes are in the VAS
- How this knowledge helps you write better kernel modules and device drivers
- Basic C programming knowledge
- Familiarity with Linux command line
- Understanding of what a process is (optional but helpful)
- A Linux system or VM running Ubuntu 22.04 / Debian 12 or newer (kernel 6.x recommended)
Why Does Linux Use Virtual Memory?
Before diving into the VM split, let us understand why virtual memory exists at all. On a modern computer, many processes run simultaneously. If each process accessed physical RAM directly, one buggy process could corrupt another process’s data or even overwrite the kernel itself. This would be catastrophic.
Virtual memory solves this by giving each process its own private, isolated address space. The process thinks it owns the entire memory range from address 0 up to some large maximum. In reality, the CPU’s Memory Management Unit (MMU) translates these virtual addresses to actual physical RAM locations transparently. The kernel controls this translation using page tables.
This model gives Linux three critical benefits:
- Isolation: Processes cannot access each other’s memory without explicit sharing (like shared memory IPC).
- Protection: The kernel can mark regions as read-only, no-execute, or kernel-only, enforced by the hardware.
- Flexibility: More memory can be addressed than physically exists (via swap and demand paging).
In the context of Linux kernel development and Linux device drivers, you will constantly work with virtual addresses — understanding their layout is the first critical step.
Understanding the VM Split in Linux Kernel Memory Management
The most important concept in Linux virtual memory is the VM split. Every process on a Linux system has its own Virtual Address Space (VAS), but this address space is divided into two halves:
- User space: The lower portion. This is where process code, data, stack, heap, and shared libraries live.
- Kernel space: The upper portion. This is where the Linux kernel itself lives — its code, data structures, device driver buffers, and memory-mapped hardware registers.
This division is what the term VM split refers to. The exact boundary between user space and kernel space depends on your architecture and kernel configuration.
Kernel code, page tables, driver buffers,
hardware register mappings
On 32-bit systems the classic split is 3 GB user / 1 GB kernel. On 64-bit systems, user and kernel spaces are each vastly larger.
VM Split on 32-bit vs 64-bit Systems
The size of the VAS changes dramatically depending on whether you are on a 32-bit or 64-bit system. The table below compares the two:
| Property | 32-bit Linux (x86) | 64-bit Linux (x86_64) |
|---|---|---|
| Total VAS size | 4 GB (2³² bytes) | 128 TB usable (2⁴⁷ bytes per region) |
| User space | 0 to 3 GB (classic 3G/1G split) | 0x0000000000000000 to 0x00007FFFFFFFFFFF |
| Kernel space | 3 GB to 4 GB (1 GB) | 0xFFFF800000000000 and above |
| PAGE_OFFSET (kernel start) | 0xC0000000 | 0xFFFF888000000000 (typical) |
| Non-canonical gap | None | Large hole between user & kernel halves |
| Practical RAM limit | ~896 MB directly mapped (with HIGHMEM for more) | Hundreds of TB (architecture dependent) |
On 64-bit Linux (which is the standard today), there is a large non-canonical address hole between user space and kernel space. Addresses in this hole are invalid — any access causes an immediate CPU exception. This is by design and serves as an extra protection layer.
Anatomy of the Process Virtual Address Space (VAS)
Within the user-space portion, a process’s VAS is organized into well-defined segments (the kernel internally calls these VMAs — Virtual Memory Areas). Each segment serves a specific purpose. Let us go through each one.
local vars, function frames
↓ grows down
libc, libm, etc
(mmap region)
malloc / kmalloc
↑ grows up
uninitialized globals
initialized globals
executable instructions
Holds local variables, function parameters, and return addresses. Each function call pushes a new stack frame. Grows downward toward lower addresses. The kernel sets up an initial stack size limit (typically 8 MB, configurable via ulimit -s).
At program startup, the dynamic linker maps shared libraries (like libc.so) into this region using mmap(). On 64-bit Linux 6.x systems, ASLR randomizes where these libraries land each time the program runs.
Used for dynamic memory allocation via malloc()/free() in user space. The kernel expands the heap using the brk() system call. Grows upward toward higher addresses.
These are segments from the ELF binary. .text is code (read + execute). .data holds initialized global/static variables. .BSS holds uninitialized globals (zero-filled by the OS at startup, taking no disk space).
Inspecting the Process VAS Using /proc — Linux Kernel Memory Management in Action
One of the most powerful tools for understanding Linux kernel memory management is the /proc virtual filesystem. Every running process has a directory at /proc/<pid>/, and within it, two files give you a detailed view of its memory layout:
| File | What It Shows | Useful For |
|---|---|---|
/proc/<pid>/maps |
All VMAs: address ranges, permissions, offset, mapped file name | Quick VAS overview |
/proc/<pid>/smaps |
Detailed per-VMA stats: RSS, PSS, swap usage, locked pages | Memory profiling |
/proc/<pid>/status |
Summary including VmRSS, VmSize, VmPeak | Quick memory summary |
/proc/self/maps |
Same as above but refers to the current process (self-inspection) | Inside a process or kernel module |
How to Read /proc/<pid>/maps
Run a simple program, then inspect its memory map from another terminal. Here is how:
# Open a terminal and run a long-lived program (e.g., sleep)
$ sleep 1000 &
[1] 47382
# Now inspect its VAS
$ cat /proc/47382/maps
A typical output looks like this (64-bit Ubuntu 22.04, kernel 6.x):
55a3b2400000-55a3b2401000 r--p 00000000 08:01 123456 /usr/bin/sleep
55a3b2401000-55a3b2402000 r-xp 00001000 08:01 123456 /usr/bin/sleep
55a3b2402000-55a3b2403000 r--p 00002000 08:01 123456 /usr/bin/sleep
55a3b2403000-55a3b2404000 r--p 00002000 08:01 123456 /usr/bin/sleep
55a3b2404000-55a3b2405000 rw-p 00003000 08:01 123456 /usr/bin/sleep
7f8a12000000-7f8a12200000 r--p 00000000 08:01 789012 /lib/x86_64-linux-gnu/libc.so.6
7f8a12200000-7f8a12395000 r-xp 00200000 08:01 789012 /lib/x86_64-linux-gnu/libc.so.6
7f8a12395000-7f8a123e8000 r--p 00395000 08:01 789012 /lib/x86_64-linux-gnu/libc.so.6
7f8a123e9000-7f8a123ed000 r--p 003e8000 08:01 789012 /lib/x86_64-linux-gnu/libc.so.6
7f8a123ed000-7f8a123ef000 rw-p 003ec000 08:01 789012 /lib/x86_64-linux-gnu/libc.so.6
7ffdc0500000-7ffdc0521000 rw-p 00000000 00:00 0 [stack]
7ffdc05f3000-7ffdc05f7000 r--p 00000000 00:00 0 [vvar]
7ffdc05f7000-7ffdc05f9000 r-xp 00000000 00:00 0 [vdso]
Let us decode the columns:
| Column | Meaning | Example |
|---|---|---|
| Address range | Start-End virtual address of this VMA | 55a3b2400000-55a3b2401000 |
| Permissions | r=read, w=write, x=execute, p=private, s=shared | r-xp |
| Offset | Offset within the file (for file mappings) | 00001000 |
| Device | Major:minor of block device | 08:01 |
| Inode | Inode number of the mapped file | 123456 |
| Pathname | File path, or [stack], [heap], [vdso], [vvar] for special regions | /lib/x86_64-linux-gnu/libc.so.6 |
When writing a Linux device driver, you will often need to map device memory (from a hardware register range) into a process’s VAS using mmap() in your driver’s file_operations. The VMA that gets created is exactly one entry you would see in /proc/<pid>/maps — with the device file path and the permissions you set.
Sparse Regions and Memory Holes in the VAS
A crucial thing to understand is that a process’s VAS is not fully populated. The vast majority of the address range is empty — these are called sparse regions or holes. Only the segments described above (text, data, heap, libraries, stack) are actually backed by physical memory or a swap file.
This is why, on a 64-bit system with a 128 TB user VAS, your process only uses a few megabytes or gigabytes at most. The rest is simply unallocated virtual address space. Accessing an address in a sparse region causes a segmentation fault (SIGSEGV) — the MMU raises a page fault, the kernel finds no VMA for that address, and terminates the process.
Gray areas are unallocated. Accessing them causes a SIGSEGV. ASLR randomizes the base addresses of each region.
How Shared Libraries Are Mapped Into the Process VAS
This is one of the most practically important concepts in Linux kernel memory management. When your program uses printf(), the code for that function does not exist inside your compiled binary. It lives inside libc.so.6. So how does the process access it?
The answer lies in the dynamic linker/loader — a small ELF binary (usually ld-linux-x86-64.so.2 on modern 64-bit systems) that is embedded as a special section in your executable. Here is the sequence of events when a Linux process starts:
Checking Library Dependencies
You can view which shared libraries a binary depends on using the ldd command:
# Compile a simple C program
$ cat hello.c
#include <stdio.h>
int main() { printf("Hello, Embedded World!\n"); return 0; }
$ gcc -o hello hello.c
# Check shared library dependencies
$ ldd hello
Expected output (addresses vary with ASLR):
linux-vdso.so.1 (0x00007ffe8d3f2000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4a82600000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4a82800000)
Notice linux-vdso.so.1 — this is a special virtual library that the kernel maps directly into every process’s VAS. It provides fast implementations of certain system calls (like gettimeofday()) that can be executed without actually entering kernel mode, significantly improving performance.
VMAs Inside the Kernel — What the Linux Kernel Sees
From the kernel’s perspective, each colored segment in the process VAS diagrams above corresponds to a struct vm_area_struct (VMA). The kernel maintains a linked list (and since Linux 6.1, a maple tree for performance) of these VMAs for each process. Each VMA records:
- vm_start, vm_end: The virtual address range of this memory region
- vm_flags: Protection flags (VM_READ, VM_WRITE, VM_EXEC, VM_SHARED, etc.)
- vm_file: Pointer to the file being mapped (NULL for anonymous mappings like heap/stack)
- vm_pgoff: Offset into the file (for file-backed mappings)
- vm_ops: Operations table — includes a fault handler called when a page is first accessed
When you implement the mmap() file operation in a character device driver, the kernel creates a new VMA in the calling process’s address space. You can customize the VMA’s vm_ops to handle page faults, allow or deny certain operations, and control exactly which physical pages (or device registers) back that region. This is a fundamental technique for zero-copy DMA and hardware register access from userspace.
Common Mistakes When Learning Linux Kernel Memory Management
In kernel code, kmalloc() returns a virtual address in kernel space. It is not a physical address. To get the physical address you must use virt_to_phys(). Passing a virtual address to DMA hardware directly will cause data corruption or a system crash.
You cannot simply dereference a user-space pointer in a kernel module. You must use copy_from_user() or copy_to_user() to safely cross the user/kernel boundary. Directly accessing a user pointer can cause kernel oops or be a security vulnerability.
The code pages of a shared library (the read+execute section) are shared across all processes using it. There is only one physical copy in RAM. What differs per-process is the VMA entry pointing to it and the data pages (which get copy-on-write’d if written). This is how Linux achieves memory efficiency.
The stack has a hard limit (check with ulimit -s; default 8 MB on most distros). Exceeding it causes a stack overflow (SIGSEGV). The heap is limited by available virtual address space and swap, but excessive heap use still leads to malloc() failures returning NULL.
Best Practices for Linux Kernel Memory Management
- Always check return values from memory allocation functions (
kmalloc,vmalloc,alloc_pages). Never assume allocation succeeds. - Free every allocation in the corresponding error path and cleanup path of your module.
- Use
copy_from_user()/copy_to_user()religiously when handling user-space pointers in driver ioctl or read/write handlers. - Understand whether you need physically contiguous memory (use
kmalloc) or just virtually contiguous (usevmalloc) — DMA usually requires physical contiguity. - Use
/proc/<pid>/mapsandsmapsduring development and debugging to verify your driver’s memory mappings are correct. - Enable kernel memory debugging tools during development:
KASAN(Kernel Address Sanitizer),SLUB debug,CONFIG_DEBUG_VM.
Frequently Asked Questions — Linux Kernel Memory Management
Virtual memory is what processes see — each process has its own private address space and believes it owns the entire range. Physical memory is the actual RAM chips in your computer. The kernel’s page tables, managed by the MMU, translate virtual addresses to physical addresses transparently. Multiple virtual addresses from different processes can map to the same physical page (for shared libraries) or to different physical pages (for private data).
PAGE_OFFSET is the virtual address at which kernel space begins (i.e., the kernel/user boundary). On 32-bit x86 Linux it is 0xC0000000 (3 GB). On 64-bit x86_64 it is 0xFFFF888000000000 (approximately). Physical RAM is directly mapped starting from PAGE_OFFSET in the kernel’s VAS — so physical address X is accessible at kernel virtual address PAGE_OFFSET + X (within the direct mapping range).
User-space pointers contain virtual addresses that are valid only in the context of that specific process. When kernel code runs, it operates in kernel context. Directly dereferencing a user pointer from kernel code is dangerous because: (1) the address may not be mapped in the current context, (2) the user may have passed an invalid or malicious address, (3) on some architectures with SMAP (Supervisor Mode Access Prevention), hardware will block the access. Always use copy_from_user() / copy_to_user().
vDSO (virtual Dynamic Shared Object) is a small shared library that the kernel automatically maps into every process. It provides user-space implementations of certain frequently-called system calls like gettimeofday(), clock_gettime(), and getcpu(). These can execute in user mode without a full system call trap into the kernel, making them much faster. You will always see it as [vdso] in /proc/pid/maps.
VSZ (Virtual Size) is the total virtual address space claimed by the process — including all VMAs even if the pages have never been accessed. RSS (Resident Set Size) is the amount of physical RAM actually occupied by the process at this moment. VSZ is almost always much larger than RSS because of sparse regions, lazily-faulted pages, and swap.
No. All threads within the same process share the same VAS (same address space, same page tables). What differs per thread is the stack — each thread gets its own private stack segment (created via mmap() with MAP_ANONYMOUS | MAP_STACK flags). You can verify this by looking at /proc/<pid>/task/<tid>/maps for individual threads.
ASLR (Address Space Layout Randomization) randomizes the base addresses of the stack, heap, and shared library segments each time a program runs. It is a security feature that makes exploitation of memory vulnerabilities much harder. For kernel module development you typically do not need to disable ASLR — it only applies to user-space. However, if you are debugging a specific crash and need deterministic addresses, you can temporarily disable it with echo 0 > /proc/sys/kernel/randomize_va_space.
Prior to Linux 6.1, the kernel stored a process’s VMAs in both a doubly-linked list and a red-black tree (for fast range lookups). In Linux 6.1, the VMA data structure was refactored to use a new data structure called the maple tree — an RCU-friendly B-tree optimized for range operations. This improves scalability for processes with very large numbers of VMAs. As a kernel developer, you interact with VMAs through the find_vma(), mmap_read_lock(), and related APIs regardless of the underlying structure.
You now understand the Linux VM split, the structure of a process Virtual Address Space, how shared libraries are mapped, what VMAs are, and how to inspect memory maps using /proc. This foundation is essential for every Linux kernel developer and device driver writer.
In Part 2, we explore the kernel segment in depth, learn about KASLR, and understand how the Linux kernel organizes physical memory using nodes, zones, and pages.
