How Does Linux Manage Virtual Address Space? – Free Linux Device Driver Training

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →
Linux Process Virtual Address Space (VAS) Explained | Free Linux Kernel Course – EmbeddedPathashala

Linux Process Virtual Address Space (VAS)
Free Linux Kernel Development Course – EmbeddedPathashala
⏱ ~25 min read
💻 Kernel 6.x
📚 Intermediate

When you run a program on Linux, the operating system creates an isolated, private memory world for it — called the Virtual Address Space (VAS). Understanding the Linux process virtual address space is one of the most important skills for anyone learning Linux kernel programming or Linux device drivers development. In this free Linux kernel course tutorial, we will break down exactly how the VAS is organized, what goes into both user space and kernel space, and how processes and threads relate to each other in memory.

Whether you are preparing for an embedded systems role or pursuing a free Linux development course, mastering VAS concepts gives you the mental model needed to understand memory bugs, write kernel modules, and read kernel source code confidently.

Topics Covered:
Process VAS User Space Segments Kernel Segment task_struct Thread Stacks Kernel Mode Stack Linux 6.x Free Linux Kernel Course

What You Will Learn

  • How the Linux Virtual Address Space is split between user space and kernel space
  • What each segment inside a process VAS represents (text, data, heap, stack, library mappings)
  • How threads share a process VAS but each get their own stack
  • What the kernel segment contains beyond just code — subsystems, task structures, kernel stacks
  • The full system-wide picture: all processes, all threads, the kernel — at once
  • What task_struct is and why it is the root of everything in the Linux kernel
  • How to start reading the actual kernel source for VAS-related structures

Prerequisites

Before reading this tutorial, make sure you are comfortable with:

  • Basic C programming (pointers, structs)
  • What a Linux process is and how to run programs in the terminal
  • The concept of virtual memory vs physical memory
  • Ideally, you have written at least one basic Linux kernel module (LKM)

What is a Virtual Address Space in Linux?

A Virtual Address Space (VAS) is the complete range of memory addresses that the operating system makes available to a single process. On a 64-bit Linux system, this is an enormous range — theoretically 264 bytes. But in practice, only a smaller portion is actually mapped to real physical RAM at any moment. The rest is either unmapped or handled through demand paging.

The key insight is: every process gets its own VAS. Process A cannot directly read or write into Process B’s memory. This is the foundation of process isolation and security. The Linux kernel uses the CPU’s Memory Management Unit (MMU) to enforce these boundaries.

The VAS of every Linux process is divided into two major halves:

RegionWho Uses ItTypical Size (64-bit)Protection
User SpaceThe running application codeLower 128 TB (addresses 0 to ~0x7FFF…)User mode accessible
Kernel SpaceLinux kernel itselfUpper portion of address spaceKernel mode only
Note: On ARM64 and x86-64 with Linux 6.x, the exact split depends on the configured virtual address bits (VA_BITS). A common configuration gives 128 TB to user space and maps kernel space separately. The important principle is: user space addresses are low, kernel space addresses are high.

Visualizing the VAS Split

Linux 64-bit Virtual Address Space Split
High Address
KERNEL SPACE
Kernel code, data, subsystems, task structures, kernel stacks
— Kernel/User Space Boundary (MMU enforced) —
Stack (grows downward ↓)
main thread + each app thread gets its own stack
Library Mappings (mmap region)
libc, libpthread, shared .so files
Heap (grows upward ↑)
malloc/free lives here
BSS (Uninitialized Data)
Global/static vars with no initial value
Data (Initialized Data)
Global/static vars with initial values
Text Segment [r-x]
Your compiled program code — read-only, executable
Low Address (0x0)

User Space Segments of the Process VAS

Every running program in Linux has the following segments arranged inside the user space portion of its VAS. Let’s understand each one:

Text Segment

This is where your compiled machine code lives. When you compile a C program and run it, the binary instructions end up here. The text segment is marked read-only and executable — Linux prevents your program from modifying its own code at runtime. This protects against a class of security bugs where an attacker tries to inject and run arbitrary code.

Tip: You can verify segment permissions using cat /proc/<pid>/maps — look for the r-xp permission flags on the region mapped to your executable file. This is the text segment.

Initialized Data Segment

Global variables and static variables that your program initializes with a specific value are stored here. For example, int counter = 10; declared at file scope goes into the initialized data segment. This segment is readable and writable but not executable.

BSS Segment (Uninitialized Data)

Global and static variables declared without an initial value land in the BSS segment. The name BSS stands for “Block Started by Symbol” — a historical term. The kernel automatically zero-initializes these at program startup, so your uninitialized global int total; starts as 0. An interesting property of BSS: it doesn’t actually consume space in the binary file on disk. The kernel knows how much space is needed and allocates it at load time.

Heap

Dynamic memory — the memory you allocate at runtime using malloc(), calloc(), or realloc() — comes from the heap. The heap grows upward toward higher addresses. The kernel manages heap expansion using the brk() system call internally (though modern glibc often uses mmap() for larger allocations).

Common Mistake: Beginners often think malloc() directly talks to the kernel each time. In reality, the C library maintains a pool of memory from the kernel and carves it up for your requests. Only when the pool is exhausted does glibc call the kernel via brk() or mmap().

Library Mappings (mmap region)

When your program uses shared libraries like libc.so or libpthread.so, the kernel maps them into your process VAS in this region. This is why multiple processes running the same program can share the physical pages of library code in RAM — each process has its own virtual mapping, but they point to the same physical memory. This saves a huge amount of RAM on a typical Linux system running hundreds of processes.

Stack

The stack holds local variables, function call frames, and return addresses. It grows downward — from high addresses toward lower addresses — as functions call other functions. Each thread gets its own stack. The main thread’s stack is automatically set up by the kernel when the program starts. When your program creates additional threads (using pthreads), each new thread gets a separate stack, typically allocated in the library mappings area.

Multiple Processes: Each Gets Its Own VAS

When three processes — P1, P2, and P3 — are running simultaneously on your Linux system, each one has a completely independent VAS. They cannot see each other’s memory. The kernel is what connects them all to the same physical hardware underneath.

Multiple Process VASes Running in Parallel
Process P1
Stack [rw]
main thread
Library Mappings
Heap
BSS / Data
Text [r-x]
Process P2 (3 threads)
Stack: main
Stack: thrd2
Stack: thrd3
Library Mappings
Heap (shared)
BSS / Data (shared)
Text [r-x] (shared)
Process Pn (2 threads)
Stack: main
Stack: thrd2
Library Mappings
Heap
BSS / Data
Text [r-x]

Each process has a completely independent user space VAS. Threads within P2 share all segments except their individual stacks.

Key Insight about Threads: Threads within the same process share all segments — text, data, BSS, heap, and library mappings — but each thread gets its own private stack. This is why threads can share global variables (they’re in the data/BSS segment) but have independent local variable state (those live on the per-thread stack).

The Kernel Space: What Lives in the Kernel Segment?

The upper portion of every process’s virtual address space is occupied by the kernel. This is called the kernel segment or kernel VAS. It is mapped into every process’s address space, but access to it is controlled by the CPU’s privilege level. Your user-space application cannot simply read or write into kernel memory — attempting to do so causes a fault.

When a process makes a system call (like read() or write()), the CPU switches from user mode to kernel mode, and the kernel executes on behalf of that process using the kernel segment mappings.

What the Kernel Segment Contains

ComponentWhat It IsExamples
Kernel Code & DataThe compiled kernel itself — all subsystems, scheduler, memory managerscheduler, VFS, networking stack
Task StructuresPer-thread metadata — one struct per thread in the entire systemstruct task_struct
Kernel StacksEach thread has a small dedicated kernel-mode stackUsed during system calls and interrupts
Kernel ThreadsThreads created by the kernel itself for background workkworker, ksoftirqd, kthreadd
Memory DescriptorsStructures tracking page tables and memory mappings per processstruct mm_struct
Device DriversKernel modules that control hardwareNetwork drivers, character device drivers
Arch-Specific CodeCPU-level code for x86-64, ARM64, RISC-V, etc.Interrupt entry points, system call handlers
Inside the Linux Kernel Segment
KERNEL SEGMENT (mapped into every process VAS)
Core Kernel
Scheduler, MM subsystem, VFS, Networking, IPC, Signaling, Timers, Namespaces, Cgroups, Module support
Per-Thread Data
task_struct (P1·main)
task_struct (P2·main)
task_struct (P2·thrd2)
… one per thread
Kernel Stacks
One small stack per thread (~8 or 16 KB) — used during system calls and interrupt handling
Kernel Threads
kthreadd, kworker/*, ksoftirqd/*, migration/*, rcu_sched
Device Drivers (non-core) — Network NIC, Character, Storage, Block I/O, Sound, Virtualization
Arch-Specific
CPU init, MMU setup, IRQ entry, Syscall handlers
Hardware
CPU(s), MMU, RAM — physical layer below all virtual addresses

The Per-Thread Kernel Mode Stack

This surprises many beginners: every thread — not just every process, but every thread in the system — has its own dedicated kernel mode stack. This is separate from the user-space stack that lives in the process VAS.

Why? Because when a thread makes a system call, the CPU switches to kernel mode and the kernel code needs a stack to operate on. It cannot use the user-space stack (that would be a security risk — the kernel would be executing on user-controlled memory). So the kernel maintains a small private stack for each thread, used only during kernel execution on behalf of that thread.

Stack TypeLocationSizeUsed When
User Space StackUser portion of VAS (top, grows down)Configurable, default ~8 MBNormal application code execution
Kernel Mode StackKernel segment, per-thread8 KB (32-bit) or 16 KB (64-bit, Linux 6.x)System calls, interrupts, exceptions
Important for Kernel Developers: The kernel mode stack is tiny (typically 16 KB on 64-bit Linux 6.x). Never allocate large arrays or deeply recurse inside kernel code — you will cause a stack overflow, which usually results in kernel panic or silent memory corruption. Use dynamic allocation (kmalloc) for large buffers in kernel modules.

Inspecting the VAS of a Running Process

Linux exposes every process’s memory map through the /proc filesystem. You can inspect this from any terminal without needing special tools.

Using /proc/PID/maps

The following command shows you the full VAS of a running process. Replace 1234 with the actual PID:

cat /proc/1234/maps

You will see output like this (simplified):

55a3e4000000-55a3e4001000 r--p 00000000 08:01 1234567   /usr/bin/myapp
55a3e4001000-55a3e4002000 r-xp 00001000 08:01 1234567   /usr/bin/myapp   ← text segment
55a3e4002000-55a3e4003000 r--p 00002000 08:01 1234567   /usr/bin/myapp
55a3e4003000-55a3e4004000 rw-p 00003000 08:01 1234567   /usr/bin/myapp   ← data segment
55a3e5c00000-55a3e5e00000 rw-p 00000000 00:00 0         [heap]
7f4abc000000-7f4abc200000 r--p 00000000 08:01 9876543   /usr/lib/libc.so.6
7f4abc200000-7f4abc380000 r-xp 00200000 08:01 9876543   /usr/lib/libc.so.6  ← libc text
7ffda0000000-7ffda0021000 rw-p 00000000 00:00 0         [stack]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]

Notice how each line shows: virtual address range, permissions (r=read, w=write, x=execute, p=private), offset, device, inode, and the mapping name. The [heap], [stack], and library paths confirm exactly what we described earlier.

Using /proc/PID/smaps for Detailed Memory Info

cat /proc/1234/smaps | less

This gives you per-mapping details like RSS (Resident Set Size — physically loaded pages), anonymous vs file-backed pages, and much more. This is extremely useful when debugging memory usage in embedded Linux systems.

Using pmap Tool

pmap -x 1234

The pmap command gives a cleaner summary of the process memory map, including total resident memory. Very useful for quick process memory analysis.

The System-Wide View: All Processes Together

When you have tens or hundreds of processes running, each with their own VAS, plus the shared kernel segment — how does it all fit together in the system?

Think of it this way: the kernel maintains a global picture of all threads and processes. Each thread has a task_struct in the kernel. Each process has a memory descriptor (mm_struct) describing its user-space VAS. The kernel segment itself is shared across all processes — when any process makes a system call, it runs in the same kernel space.

System-Wide View: Processes + Kernel Together
P1
Stack (main)
Libs / Heap
Data / Text
P2 (3 threads)
Stack×3
Libs / Heap
Data / Text
Pn (2 threads)
Stack×2
Libs / Heap
Data / Text
↕ User / Kernel Boundary ↕
KERNEL SEGMENT (shared, mapped into all processes)
task_struct
P1·main, P2·main, P2·t2, P2·t3, Pn·main, Pn·t2, kworker…
Kernel Stacks
16 KB per thread — one for each task_struct above
Core Subsystems
Scheduler, MM, VFS, Net, IPC, Timers
Device Drivers (NIC, Character, Storage) + Arch-Specific Code (CPU, MMU, IRQ)

Best Practices and Common Mistakes

✅ Best Practices
  • Always use /proc/PID/maps and pmap to inspect real VAS layouts — don’t just theorize
  • When writing kernel modules, avoid allocating large data structures on the kernel stack — use kmalloc() instead
  • Understand that stack overflows in user space often produce SIGSEGV due to the stack guard page; in the kernel, they can cause silent corruption or panic
  • Use ulimit -s to check or modify the user-space stack size limit on your system
  • When reading kernel source, remember that current is a macro that returns the task_struct pointer for the currently executing thread
❌ Common Mistakes
  • Thinking that threads have separate heap — they don’t; all threads in a process share one heap
  • Confusing user-space stack with kernel-mode stack — they are completely different memory regions
  • Assuming the kernel segment is not mapped into user-process VAS — it is mapped, but access is CPU-privilege-controlled
  • Ignoring the BSS segment when calculating binary size — BSS is zero in the file but consumes memory at runtime
  • Forgetting that shared libraries are mapped once per-process but may share physical pages via CoW (Copy-on-Write)

Performance Considerations

Understanding the VAS helps you write faster Linux programs and better kernel code. Library code mapped into the mmap region may be shared across many processes — this means the kernel can keep one copy in the CPU’s TLB cache, reducing translation overhead. Large heap allocations may trigger mmap() rather than brk(), which can affect memory fragmentation. On embedded Linux systems with limited RAM, minimizing resident set size (RSS) is critical — understanding which VAS regions are actually loaded into RAM versus just mapped is essential.

Security Considerations

The VAS design is central to Linux’s security model. Key security features related to VAS include:

  • ASLR (Address Space Layout Randomization): The kernel randomizes the base addresses of stack, heap, and library mappings at each program launch, making buffer overflow exploits much harder. Check your system: cat /proc/sys/kernel/randomize_va_space (2 = full ASLR, recommended)
  • NX bit / W^X: Memory pages are either writable or executable, never both. This prevents injected shellcode from running. Visible in /proc/PID/maps permissions.
  • Kernel KASLR: The kernel itself is loaded at a randomized address, protecting against kernel-space exploits that rely on fixed addresses.

Summary and Key Takeaways

  • Every Linux process has a private Virtual Address Space split into user space and kernel space
  • User space contains: text, initialized data, BSS, heap, library mappings, and stack segments
  • Threads within a process share all segments except the stack — each thread has its own stack
  • The kernel segment is mapped into every process’s VAS but is accessible only in kernel mode
  • The kernel segment holds kernel code, all task structures (task_struct), kernel-mode stacks, kernel threads, device drivers, and arch-specific code
  • Every thread — user or kernel — has a small dedicated kernel-mode stack (~16 KB on 64-bit Linux 6.x)
  • You can inspect any process’s VAS using /proc/PID/maps, /proc/PID/smaps, or pmap
  • The next step is understanding task_struct — covered in the next lecture

Authoritative References

Frequently Asked Questions

Q1: Does every process really get the full 128 TB of user space on a 64-bit system?
Technically yes, the virtual address range is available, but only the portions that are actually mapped (via mmap, stack, heap, etc.) consume any resources. The vast majority of the virtual address space is simply unmapped and costs nothing.
Q2: Why do all processes share the kernel segment? Is that not a security risk?
The kernel segment is mapped into every process’s address space for performance — when a system call happens, the kernel can start executing immediately without switching page tables (on architectures that support this). The CPU’s privilege level (ring 0 vs ring 3) enforces that user-mode code cannot access kernel memory. Accessing kernel addresses from user mode causes an immediate fault.
Q3: What is the difference between VSZ and RSS in top/ps output?
VSZ (Virtual Size) is the total size of the process’s VAS — every byte that is mapped, whether physically in RAM or not. RSS (Resident Set Size) is how much of that is actually loaded into physical RAM right now. VSZ is always larger than or equal to RSS.
Q4: If threads share the heap, can two threads corrupt each other’s malloc allocations?
Yes — this is a real problem called heap corruption or data races. Modern glibc malloc is thread-safe for the allocation operations themselves (it uses per-thread arenas to reduce locking), but once a pointer is handed to your code, two threads accessing the same object without synchronization will cause data races. Always use mutexes or atomic operations when sharing heap-allocated data between threads.
Q5: How big is the kernel mode stack in Linux 6.x?
On 64-bit systems running Linux 6.x, the kernel mode stack is typically 16 KB (two pages). On 32-bit systems it is 8 KB. This is configured by THREAD_SIZE in the kernel source. Some configs enable IRQ stacks or exception stacks separately to reduce pressure on the main kernel stack.
Q6: What is KASLR and should I disable it for kernel development?
KASLR (Kernel Address Space Layout Randomization) randomizes the kernel load address at boot time. For production systems, keep it enabled. For kernel debugging and development, you can disable it by passing nokaslr on the kernel command line — this makes stack traces and addresses in crash dumps match the symbol table without needing offsets.
Q7: Is this course really free? Where can I learn more Linux kernel programming?
Yes, EmbeddedPathashala is a completely free Linux kernel development course. You can access all tutorials at embeddedpathashala.com. We cover Linux kernel modules, device drivers, memory management, process management, and much more — completely free for students and embedded engineers.
Q8: What comes next after understanding the Process VAS?
The logical next step is understanding the task_struct — the kernel’s per-thread metadata structure. Every user thread and kernel thread is represented by one task_struct inside the kernel. Understanding it unlocks how the scheduler works, how signals are delivered, how CPU affinity is tracked, and much more.
{ “@context”: “https://schema.org”, “@type”: “FAQPage”, “mainEntity”: [ {“@type”:”Question”,”name”:”Does every process get the full 128 TB of user space on a 64-bit system?”,”acceptedAnswer”:{“@type”:”Answer”,”text”:”Technically yes, the virtual address range is available, but only mapped portions consume resources.”}}, {“@type”:”Question”,”name”:”Why do all processes share the kernel segment?”,”acceptedAnswer”:{“@type”:”Answer”,”text”:”It is mapped for performance so system calls don’t require full page table switches. CPU privilege levels enforce that user-mode code cannot access it.”}}, {“@type”:”Question”,”name”:”How big is the kernel mode stack in Linux 6.x?”,”acceptedAnswer”:{“@type”:”Answer”,”text”:”16 KB on 64-bit systems, 8 KB on 32-bit systems, controlled by THREAD_SIZE in kernel source.”}}, {“@type”:”Question”,”name”:”Is this Linux kernel course really free?”,”acceptedAnswer”:{“@type”:”Answer”,”text”:”Yes, EmbeddedPathashala provides a completely free Linux kernel development and device drivers course at embeddedpathashala.com.”}} ] }

Continue Your Free Linux Kernel Journey

EmbeddedPathashala — Free Linux Kernel, Device Drivers & Embedded Systems Courses

Visit EmbeddedPathashala
💬 Need help or career guidance? Ask us!Contact Us →

Leave a Reply

Your email address will not be published. Required fields are marked *

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →