🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →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.
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_structis 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:
| Region | Who Uses It | Typical Size (64-bit) | Protection |
|---|---|---|---|
| User Space | The running application code | Lower 128 TB (addresses 0 to ~0x7FFF…) | User mode accessible |
| Kernel Space | Linux kernel itself | Upper portion of address space | Kernel mode only |
Visualizing the VAS Split
Kernel code, data, subsystems, task structures, kernel stacks
main thread + each app thread gets its own stack
libc, libpthread, shared .so files
malloc/free lives here
Global/static vars with no initial value
Global/static vars with initial values
Your compiled program code — read-only, executable
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.
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).
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.
main thread
Stack: thrd2
Stack: thrd3
Stack: thrd2
Each process has a completely independent user space VAS. Threads within P2 share all segments except their individual stacks.
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
| Component | What It Is | Examples |
|---|---|---|
| Kernel Code & Data | The compiled kernel itself — all subsystems, scheduler, memory manager | scheduler, VFS, networking stack |
| Task Structures | Per-thread metadata — one struct per thread in the entire system | struct task_struct |
| Kernel Stacks | Each thread has a small dedicated kernel-mode stack | Used during system calls and interrupts |
| Kernel Threads | Threads created by the kernel itself for background work | kworker, ksoftirqd, kthreadd |
| Memory Descriptors | Structures tracking page tables and memory mappings per process | struct mm_struct |
| Device Drivers | Kernel modules that control hardware | Network drivers, character device drivers |
| Arch-Specific Code | CPU-level code for x86-64, ARM64, RISC-V, etc. | Interrupt entry points, system call handlers |
task_struct (P2·main)
task_struct (P2·thrd2)
… one per thread
CPU init, MMU setup, IRQ entry, Syscall handlers
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 Type | Location | Size | Used When |
|---|---|---|---|
| User Space Stack | User portion of VAS (top, grows down) | Configurable, default ~8 MB | Normal application code execution |
| Kernel Mode Stack | Kernel segment, per-thread | 8 KB (32-bit) or 16 KB (64-bit, Linux 6.x) | System calls, interrupts, exceptions |
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.
Stack (main)
Libs / Heap
Data / Text
Stack×3
Libs / Heap
Data / Text
Stack×2
Libs / Heap
Data / Text
P1·main, P2·main, P2·t2, P2·t3, Pn·main, Pn·t2, kworker…
16 KB per thread — one for each task_struct above
Scheduler, MM, VFS, Net, IPC, Timers
Best Practices and Common Mistakes
- Always use
/proc/PID/mapsandpmapto 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
SIGSEGVdue to the stack guard page; in the kernel, they can cause silent corruption or panic - Use
ulimit -sto check or modify the user-space stack size limit on your system - When reading kernel source, remember that
currentis a macro that returns thetask_structpointer for the currently executing thread
- 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/mapspermissions. - 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, orpmap - The next step is understanding
task_struct— covered in the next lecture
Authoritative References
- Linux Kernel Memory Management Documentation (kernel.org)
- proc(5) man page — /proc filesystem reference (man7.org)
- x86-64 Linux Memory Map (kernel.org)
Frequently Asked Questions
THREAD_SIZE in the kernel source. Some configs enable IRQ stacks or exception stacks separately to reduce pressure on the main kernel stack.nokaslr on the kernel command line — this makes stack traces and addresses in crash dumps match the symbol table without needing offsets.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.Continue Your Free Linux Kernel Journey
EmbeddedPathashala — Free Linux Kernel, Device Drivers & Embedded Systems Courses
Visit EmbeddedPathashala