🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →
Linux Kernel Virtual Address Space Explained — Free Linux Kernel Development Course
What You Will Learn
- How the Linux kernel splits the total virtual address space between user processes and itself
- What the kernel VAS (Virtual Address Space) looks like on 32-bit and 64-bit systems
- The purpose of each major kernel memory region — lowmem, vmalloc, module region, fixmap, and more
- How key macros like
PAGE_OFFSET,VMALLOC_START,MODULES_VADDRdefine these boundaries - Why this knowledge is essential for writing Linux device drivers and kernel modules
Every Linux programmer hears the terms “user space” and “kernel space” constantly. But when you start writing Linux device drivers or kernel modules, you need to understand these are not just vague ideas — they map to very specific, concrete ranges of virtual addresses in memory. Understanding the Linux Kernel Virtual Address Space layout is one of the first real steps in mastering kernel internals.
This lecture is part of EmbeddedPathashala’s free Linux kernel development course and free Linux device drivers course. We explain everything from scratch, simply and clearly.
The Big Picture: Splitting Virtual Address Space
When a processor runs in protected mode, every process sees its own virtual address space (VAS). On a 32-bit system this space is 4 GB total. On a 64-bit system the theoretical space is enormous — 264 bytes — though in practice only a subset of that is used.
Linux divides this total virtual address space into two halves:
- User VAS — belongs to the running process (application code, heap, stack, shared libraries)
- Kernel VAS — reserved exclusively for the Linux kernel (kernel code, data, drivers, module code, vmalloc space)
The boundary between these two halves is controlled by a compile-time constant called PAGE_OFFSET. Every virtual address below PAGE_OFFSET is user space. Everything at or above PAGE_OFFSET is kernel space.
On a classic 32-bit ARM or x86 system with a 3:1 split, the kernel gets the top 1 GB of virtual address space (from 0xC0000000 to 0xFFFFFFFF on x86, or 0x80000000 to 0xFFFFFFFF on ARM with a 2:2 split). The user process gets everything below that.
Major Regions Inside the Kernel Virtual Address Space
The kernel VAS is not a flat blob of memory. It is carefully partitioned into distinct regions, each serving a specific purpose. Let’s walk through each one from the lowest kernel address upward.
1. Lowmem — The Direct-Mapped Physical RAM Region
The most important region is called lowmem (low memory). This region starts right at PAGE_OFFSET and extends upward. Physical RAM is mapped directly and linearly here — virtual address PAGE_OFFSET + X always maps to physical address X. This makes address translation trivially fast because no page table lookup is needed for kernel data accesses.
Kernel code itself — the compiled kernel image, the BSS, kernel data structures, the page tables — all live in this region. When you allocate memory with kmalloc() in a kernel module, the memory comes from this lowmem region.
2. Module Region
When you insert a kernel module (a .ko file) using insmod or modprobe, the kernel needs to load that module’s code and data into memory. It does this inside a dedicated virtual address range called the module region.
On a 32-bit ARM system you can see this region starts right at PAGE_OFFSET and extends 16 MB upward. On 64-bit x86_64, the module region is placed just before the kernel text itself at the top of the kernel VAS, occupying a 1 GB range near the top of the canonical kernel address space.
The macros that define this region are:
MODULES_VADDR— start virtual address of the module regionMODULES_END— end virtual address of the module region
| vmalloc region (above module region) | Dynamic kernel mappings, ioremap |
| MODULE REGION MODULES_VADDR → MODULES_END |
All loaded .ko kernel module code lives here |
| lowmem (below module region) | Kernel image, kmalloc allocations |
The module region exists separately because the kernel needs to be able to reach module code with short jumps (relative branches). On many architectures, branch instructions have a limited reach in terms of address offset. By placing module code close to the kernel image in virtual address space, the linker can use efficient short-branch instructions instead of slow indirect jumps through a function pointer table.
3. vmalloc / ioremap Region
Above the lowmem region (and on 32-bit systems, above the module region too) sits the vmalloc region. This region provides virtually contiguous but physically non-contiguous memory allocations. When you call vmalloc() in a driver or kernel module, memory is allocated from this region.
Why do we need this? Physical RAM gets fragmented over time. You might need 10 MB of kernel memory but there are no 10 MB contiguous physical chunks left — only scattered 4 KB pages. vmalloc() maps those scattered pages into a single contiguous virtual range in the vmalloc region, so your code sees one big buffer.
The vmalloc region is also used by the kernel’s ioremap() function, which maps hardware device registers (physical addresses in MMIO space) into the kernel’s virtual address space so that drivers can read and write hardware registers using normal pointer dereferences.
| Allocator | Region Used | Physical Contiguous? | Typical Use Case |
|---|---|---|---|
kmalloc() |
lowmem | Yes | Small, fast kernel allocations |
vmalloc() |
vmalloc region | No | Large allocations, physically fragmented |
ioremap() |
vmalloc region | N/A (MMIO) | Mapping device registers into kernel space |
get_free_pages() |
lowmem | Yes | Whole page allocations |
4. Fixmap Region
Near the very top of the kernel VAS is a small area called the fixmap (fixed mappings). These are virtual addresses that are hard-coded at compile time and always map to specific physical resources — such as the Local APIC (interrupt controller) on x86, or on ARM the high-vectors page which holds the exception handler entry points.
The key property of fixmap entries is that their virtual addresses are fixed and known at build time, but the physical address they map to is determined at boot. This allows the kernel to refer to critical hardware by a stable virtual address even before the full memory management subsystem is initialized.
Kernel VAS on 64-bit Systems (Linux 6.x)
On 64-bit x86_64 (which is what you are most likely using today), the virtual address space is far larger than on 32-bit. Modern x86_64 CPUs implement 48-bit virtual addressing (with 5-level paging extending this to 57 bits in Linux 6.x on supported hardware). The usable kernel virtual address space is roughly 128 TB on 4-level paging.
The split is no longer 3:1 or 2:2 as on 32-bit. Instead, canonical virtual addresses have two distinct halves separated by a “hole” of non-canonical addresses:
- Lower canonical range (0x0000000000000000 to 0x00007FFFFFFFFFFF) — user space
- Non-canonical hole — any access here causes an immediate CPU fault
- Upper canonical range (0xFFFF800000000000 and above) — kernel space
| 0xFFFFFFFFFFFFFFFF | Fixmap, VSYSCALL page |
| ~0xFFFFFFFFC0000000 | Module region (~1 GB) |
| vmalloc / ioremap (~32 TB) | |
| 0xFFFF888000000000 | Direct map of all physical RAM (lowmem, ~64 TB) |
| 0xFFFF800000000000 | Guard / unused kernel start |
| Non-canonical hole | ❌ Access = CPU fault (not mapped) |
| 0x00007FFFFFFFFFFF | User Space top (128 TB) |
| 0x0000000000000000 | User Space bottom |
On 64-bit, the kernel has so much virtual address space that the old 32-bit compromises are gone. The entire physical RAM can be directly mapped, vmalloc gets tens of terabytes of range, and the module region is generously sized to hold any reasonable number of kernel modules.
Key Kernel Macros That Define the Layout
The kernel VAS boundaries are not hardcoded as random numbers in the code — they are defined through architecture-specific macros. Understanding these macros is essential for writing correct kernel code, especially device drivers that work across multiple platforms.
| Macro | What It Marks | Typical 32-bit ARM | x86_64 |
|---|---|---|---|
PAGE_OFFSET |
Start of kernel VAS / end of user VAS | 0x80000000 | 0xFFFF888000000000 |
TASK_SIZE |
Max size of user VAS | 0x7F000000 | 0x00007FFFFFFFFFFF |
VMALLOC_START |
Start of vmalloc region | 0xBB800000 | Computed at boot |
VMALLOC_END |
End of vmalloc region | 0xFF800000 | Computed at boot |
MODULES_VADDR |
Start of module region | 0x7F000000 | ~0xFFFFFFFFC0000000 |
MODULES_END |
End of module region | 0x80000000 | ~0xFFFFFFFFE0000000 |
high_memory |
Top of direct-mapped physical RAM | Varies with RAM | Covers all RAM |
You can read these values at runtime from your kernel module using printk(). Here is a simple example that prints the key boundaries from within a kernel module:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/pgtable.h>
static int __init vas_info_init(void)
{
pr_info("PAGE_OFFSET = 0x%lx\n", PAGE_OFFSET);
pr_info("VMALLOC_START = 0x%lx\n", VMALLOC_START);
pr_info("VMALLOC_END = 0x%lx\n", VMALLOC_END);
pr_info("MODULES_VADDR = 0x%lx\n", MODULES_VADDR);
pr_info("MODULES_END = 0x%lx\n", MODULES_END);
pr_info("high_memory = %px\n", high_memory);
return 0;
}
static void __exit vas_info_exit(void)
{
pr_info("vas_info module removed\n");
}
module_init(vas_info_init);
module_exit(vas_info_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Print Kernel VAS boundaries");
Build this with the standard kernel module Makefile and run insmod vas_info.ko, then check dmesg to see the actual values on your system. The numbers will vary depending on your architecture, kernel configuration, and whether KASLR (Kernel Address Space Layout Randomization) is enabled.
KASLR — Kernel Address Space Layout Randomization
Since Linux 3.14 on x86_64 (and progressively added to other architectures), the kernel randomizes where it places itself and other major regions in the VAS at each boot. This security feature is called KASLR. The purpose is to make it harder for attackers to exploit kernel vulnerabilities that rely on knowing the exact address of kernel functions or data structures.
Practically, this means:
- The base address of the kernel text segment is randomized at boot
- The vmalloc region base is randomized
- The direct map base is randomized (on recent kernels)
- Values like
PAGE_OFFSETmay differ between boots
Inspecting the Kernel Memory Layout at Runtime
Linux provides several ways to inspect the kernel VAS without writing any code:
Using /proc/iomem
The file /proc/iomem shows how physical memory regions are mapped — including system RAM, device MMIO regions, PCI memory ranges, and more.
cat /proc/iomem
Using /proc/kallsyms
/proc/kallsyms lists every exported kernel symbol along with its current virtual address. You can grep for boundary symbols to see where key regions start:
sudo grep -E "(_text|_etext|_sdata|_edata|_end|__start_rodata)" /proc/kallsyms
Using /proc/meminfo
/proc/meminfo gives a human-readable summary of overall memory usage — total RAM, free memory, vmalloc usage, and more.
cat /proc/meminfo | grep -E "VmallocTotal|VmallocUsed|Vmalloc"
Why This Matters for Linux Device Driver Development
When you write a Linux device driver — which is a practical skill this free Linux device drivers course focuses on heavily — you constantly work with kernel virtual addresses. Here are the specific scenarios where understanding the kernel VAS is essential:
- Memory allocation: Choosing between
kmalloc()andvmalloc()depends on whether you need physically contiguous memory. If your hardware requires a DMA transfer from a physically contiguous buffer, you must usekmalloc()ordma_alloc_coherent(), notvmalloc(). - MMIO access: You must call
ioremap()to get a kernel virtual address for your device’s hardware registers. This virtual address comes from the vmalloc region. After use, calliounmap()to release it. - DMA mapping: DMA operations deal with physical addresses. The kernel provides APIs to convert between virtual and physical addresses and to ensure cache coherency.
- Debugging kernel oops: When your driver causes a kernel crash, the oops message shows the faulting virtual address. Knowing the VAS layout helps you immediately see whether the fault was in module space, vmalloc space, or user space — pointing you to the bug’s location.
Common Mistakes and Troubleshooting
copy_from_user() / copy_to_user().vmalloc() can sleep (it may need to allocate page tables). Never call it from an interrupt handler or any atomic context. Use kmalloc(GFP_ATOMIC) in atomic contexts instead.ioremap() call must have a matching iounmap() in your driver’s cleanup path. Failing to unmap leaks vmalloc space.Best Practices for Kernel Memory Usage
- Use
kmalloc()for small, short-lived allocations (up to a few KB) where physical contiguity matters - Use
vmalloc()for large allocations where physical contiguity is not required - Use
devm_kmalloc()anddevm_ioremap()in device drivers — thedevm_variants automatically free resources when the device is removed - Always check the return value of every memory allocation — never assume success
- Keep kernel allocations as short-lived as possible and free them in the correct cleanup path
- Prefer slab allocators (
kmem_cache_create) for frequently allocated fixed-size objects
Key Takeaways
- The Linux kernel splits virtual address space into user VAS and kernel VAS, separated by
PAGE_OFFSET - The kernel VAS contains several distinct regions: lowmem (direct-mapped RAM), module region, vmalloc/ioremap region, and fixmap
- On 32-bit systems the split is constrained (3:1 or 2:2); on 64-bit x86_64 both halves are enormous
- Key macros —
PAGE_OFFSET,VMALLOC_START,MODULES_VADDR— define the layout and are architecture-specific - KASLR randomizes the layout at each boot for security; never hardcode kernel addresses
- Knowing the VAS layout is directly practical for allocating memory correctly and debugging kernel crashes in driver development
Frequently Asked Questions
User space is the virtual address range where application code runs. Kernel space is the virtual address range reserved for the Linux kernel itself. They are separated by PAGE_OFFSET. The CPU enforces that user-space code cannot directly access kernel space — any such attempt triggers a protection fault.
PAGE_OFFSET is a compile-time macro that defines the virtual address at which the kernel VAS begins. On 32-bit ARM it is commonly 0x80000000 (2 GB mark). On x86_64 it points to the start of the direct physical memory map in the upper canonical address range.
The vmalloc region provides virtually contiguous memory mappings that are not necessarily physically contiguous. It is used by vmalloc() for large kernel allocations and by ioremap() to map device hardware registers into the kernel’s virtual address space.
Kernel modules (loaded .ko files) need to call functions in the main kernel image using branch instructions. Many CPU architectures have branch instructions with a limited address reach. By keeping the module region close to the kernel image in virtual address space, all branches stay within reach without needing slow indirect jumps.
KASLR (Kernel Address Space Layout Randomization) randomizes the placement of the kernel image and major VAS regions at each boot. It is a security feature. For driver developers it means you must never assume a kernel function or variable is at a fixed virtual address — always use the provided APIs and macros.
You can check /proc/kallsyms for symbol addresses, /proc/iomem for physical memory mappings, and /proc/meminfo for vmalloc usage. You can also write a simple kernel module that uses pr_info() to print the values of macros like PAGE_OFFSET, VMALLOC_START, and MODULES_VADDR.
Yes, but you must use the GFP_ATOMIC flag: kmalloc(size, GFP_ATOMIC). This tells the allocator not to sleep. Never use vmalloc() in interrupt context as it can sleep.
It will likely cause a kernel oops (a recoverable kernel error) or a kernel panic (an unrecoverable crash). User virtual addresses are only valid in the context of the user process. In kernel context, always use copy_from_user() or get_user() to safely read user-space memory.
No. The layout is architecture-specific. ARM 32-bit, ARM64, and x86_64 all have different PAGE_OFFSET values, different module region placements, and different vmalloc sizes. The concepts are universal but the actual addresses differ. This is why the macros exist — so driver code remains portable across architectures.
This is covered in the Memory Management module of EmbeddedPathashala’s free Linux kernel development course. The course covers the complete Linux kernel programming journey from LKM basics through memory management, interrupt handling, and device driver frameworks — all completely free.
Continue Learning — It’s Completely Free
EmbeddedPathashala offers a full free Linux kernel development course, free Linux device drivers course, and free embedded systems course. No cost, no registration required.
🏠 Go to Course Index 📖 Next: Memory Allocation APIs