🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Intermediate
Memory Management
Linux 6.x
Free
Introduction – Why VAS Matters in Linux Kernel Development
Every process that runs on a Linux system has its own dedicated Virtual Address Space (VAS). This is one of the most fundamental concepts you must master in a free Linux kernel development course, because it directly governs how the kernel allocates memory, isolates processes, and handles everything from heap allocation to shared library loading.
In this tutorial — part of EmbeddedPathashala’s free Linux kernel development course — we will go deep into the process VAS: what it is made of, how to inspect it using the proc filesystem, and how to read and interpret each segment or mapping that makes up the full picture. This knowledge is essential for Linux device driver developers, embedded systems engineers, and anyone working on low-level kernel programming.
By the time you finish this module, you will have a solid, practical grip on how the Linux kernel represents a process’s memory — knowledge that is directly useful when debugging crashes, understanding kernel oops messages, writing Linux device drivers, and working through interview questions for embedded Linux roles.
Prerequisites for This Module
Before working through this part of the free Linux kernel development course, you should be comfortable with:
- Basic C programming and pointers
- What a process is and how it differs from a thread
- The concept of virtual memory (physical vs virtual addresses)
- How to navigate a Linux terminal
What You Will Learn
- What the Process Virtual Address Space (VAS) is and how the kernel structures it
- The role of the proc filesystem (procfs) in exposing live kernel data
- How to read and interpret
/proc/PID/mapsoutput field by field - The difference between file-backed mappings and anonymous mappings
- How permissions, private vs shared flags, and offsets work in memory segments
- How
mmap()is internally responsible for all segment creation - The relationship between user VAS and kernel VAS in modern 64-bit Linux
- Practical command-line tools to inspect any running process’s memory layout
What Is a Process Virtual Address Space?
Every time the Linux kernel creates a process, it assigns that process a completely private virtual address space — a range of addresses the process can use to store code, data, stack, and heap, as well as mappings to shared libraries and kernel interfaces. The kernel never lets two processes accidentally share the same virtual addresses (unless they explicitly request it with shared memory mechanisms).
The VAS is made up of individual segments or mappings. These include:
- Text segment – the executable code of the program
- Data segment – initialized global and static variables
- BSS segment – uninitialized global and static variables
- Heap – dynamically allocated memory (grows upward)
- Stack – function call frames and local variables (grows downward)
- Library mappings – shared libraries (.so files) mapped into VAS
- Special kernel mappings – vDSO, vvar, vsyscall
| Address Range (Conceptual) | Region | Description |
|---|---|---|
| 0xFFFF… (top) | Kernel Space | Kernel code, data, drivers (not directly accessible from user mode) |
| Near top of user space | vsyscall / vDSO / vvar | Special kernel-provided mappings for fast syscalls |
| High user addresses | Stack | Grows downward; holds local vars & call frames |
| Middle addresses | Shared Libraries (.so) | libc, ld-linux, and other shared libraries |
| After BSS / data | Heap | Grows upward; dynamic allocations via malloc / kmalloc |
| Low addresses | Text / Data / BSS | Executable code and global/static variables |
| 0x0000 (bottom) | NULL / Unmapped | Accessing here causes a segfault (intentionally unmapped) |
The proc Filesystem – Your Window Into the Running Kernel
One of the most powerful tools available in any free Linux kernel development course is the proc filesystem (procfs), mounted at /proc. It is a pseudo filesystem — nothing is stored on disk. Instead, when you read a file under /proc, the kernel dynamically generates the content in real time directly from kernel data structures.
The proc filesystem has two main purposes in Linux:
- Expose runtime kernel internals – process state, memory maps, file descriptors, CPU info, and much more via read-only pseudo-files.
- Allow kernel tuning – via files under
/proc/sys/(also calledsysctlknobs), system administrators can tune kernel behavior without rebooting.
sysfs filesystem (/sys) is conceptually related but distinct. It represents the Linux device model — exposing a tree of all devices, bus types, and drivers. For memory map inspection, we focus on procfs.
Reading Process Status via procfs
Every process on a running Linux system gets its own directory under /proc, named after its PID (Process Identifier). For example, a process with PID 1234 has its info under /proc/1234/. You can also use the special keyword self to refer to the calling process itself.
To see a wealth of information about any process’s task structure (including memory stats, signal disposition, scheduler info, and more):
cat /proc/1234/status
Replace 1234 with any actual PID. Using self:
cat /proc/self/status
Examining the User VAS with /proc/PID/maps
The most important file for understanding a process’s memory layout in this free Linux kernel development course is /proc/PID/maps. It gives you a complete, sorted view of every mapping that makes up the user-mode VAS of a given process.
Running the Command
To inspect your own shell process:
cat /proc/self/maps
To inspect any running process by PID (requires root for processes you don’t own):
cat /proc/1234/maps
Understanding the Output Format
Each line in the output represents one segment (mapping) in the process’s user VAS. The output is sorted in ascending order by virtual address. Here is the field layout:
| Field | Example Value | Meaning |
|---|---|---|
| start_uva – end_uva | 555d83b65000-555d83b6d000 | Start and end user virtual addresses of this mapping |
| mode,mapping | r-xp | Permissions (rwx) + private(p) or shared(s) |
| start-off | 00000000 | Offset from beginning of the file (0 for anonymous mappings) |
| mj:mn | 08:01 | Major:minor device number of the backing device (00:00 if anonymous) |
| inode# | 524313 | Inode number of the backing file (0 if anonymous) |
| image-name | /bin/cat | Path to the backing file (or [heap], [stack], [vdso], etc.) |
Decoding the Permission Field
The permissions field is a 4-character string. Here is what each character position means:
r-xp = Read + No Write + Execute + Private mapping → This is a text (code) segment
p (private, copy-on-write) or s (shared). Private mappings mean writes are not visible to other processes. This is set by the flags parameter when mmap() creates the segment.
Types of Mappings in the User VAS
In Linux kernel internals — a critical topic for any free Linux device drivers course — every segment or region in the process VAS is created by the mmap() system call. There are two fundamental types of mappings:
1. File-Backed Mappings
A file-backed mapping loads the contents of a file into the process’s virtual address space. This is how the kernel loads your executable binary, how shared libraries (like libc) get mapped in, and how memory-mapped I/O works in Linux device drivers.
Identifying characteristics in /proc/PID/maps:
- The image-name field shows a real file path (e.g.,
/lib/x86_64-linux-gnu/libc.so.6) - The device field shows a real
mj:mnpair (e.g.,08:01) - The inode number is non-zero
- The start offset is the byte offset into the file from which this mapping starts
2. Anonymous Mappings
Anonymous mappings are not backed by any file. The heap and stack are both anonymous mappings. When you call malloc() in userspace, the C library internally uses mmap() with the MAP_ANONYMOUS flag to allocate memory.
Identifying characteristics in /proc/PID/maps:
- The image-name field is either blank or shows a label like
[heap]or[stack] - The device field is
00:00 - The inode is
0 - The start offset is always
0
| Property | File-Backed Mapping | Anonymous Mapping |
|---|---|---|
| Backing source | A file on disk | No file; kernel provides zero-filled pages |
| Examples | Text segment, shared libraries, memory-mapped files | Heap, stack, large malloc() regions |
| Device field | 08:01 (real device) | 00:00 |
| Inode | Non-zero | 0 |
| Name in maps | /path/to/file.so | [heap], [stack], or blank |
| mmap() flag | MAP_SHARED or MAP_PRIVATE | MAP_ANONYMOUS | MAP_PRIVATE |
The Role of mmap() – How Every Segment Is Born
Here is one of the most important insights in this free Linux kernel development course: every single mapping in a process’s VAS — whether it is the text segment, the heap, the stack, or a shared library — is ultimately created by the mmap() system call inside the Linux kernel.
When you launch a program, the kernel’s ELF loader calls mmap() to map the text and data sections. When malloc() needs a large chunk of memory, the C library calls mmap() with MAP_ANONYMOUS. When you open a shared library, the dynamic linker calls mmap(). When you write a Linux device driver that supports memory-mapped I/O, user space applications will call mmap() on your device file.
/* Example: Manually creating an anonymous mapping in userspace */
#include <sys/mman.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
size_t size = 4096; /* one page */
/* MAP_ANONYMOUS: not backed by any file
MAP_PRIVATE: changes not visible to other processes */
char *buf = mmap(NULL, size,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
if (buf == MAP_FAILED) {
perror("mmap failed");
return 1;
}
strcpy(buf, "Hello from a kernel-managed anonymous mapping!");
printf("%s\n", buf);
munmap(buf, size); /* release the mapping */
return 0;
}
You can verify the mapping appeared in the process VAS by checking /proc/self/maps from within the program while the mapping is active.
Special Kernel-Provided Mappings: vDSO, vvar, vsyscall
Near the top of the user VAS, you will always find several special mappings that the kernel itself injects into every process. These are important to understand in any free Linux kernel development course:
| Mapping Name | What It Is | Purpose |
|---|---|---|
| [vdso] | Virtual Dynamic Shared Object | A small shared library the kernel maps in; allows some syscalls (like gettimeofday()) to run entirely in user space without a full context switch |
| [vvar] | Virtual Variables | Read-only kernel data page; the vDSO reads from here (e.g., current time) without needing a syscall |
| [vsyscall] | Legacy Virtual Syscall | Older mechanism for fast syscalls; fixed virtual address, now largely replaced by vDSO on modern kernels |
clock_gettime(), clock_getres(), gettimeofday(), and time() — all without a full syscall round-trip.
Useful Tools to Inspect Process Memory Maps
Beyond raw cat /proc/PID/maps, several tools make VAS inspection easier for Linux kernel and device driver developers:
pmap – Human-Readable Memory Map
pmap is a userspace tool that reads /proc/PID/maps and presents the data in a cleaner format. It also shows the total size of each mapping in KB:
pmap -x 1234
The -x flag shows the extended format including RSS (Resident Set Size — actually loaded pages) and dirty pages.
smaps – Detailed Per-Mapping Statistics
The /proc/PID/smaps file is an extended version of /proc/PID/maps. For each mapping, it provides additional metrics:
cat /proc/self/smaps | less
Key fields in smaps output:
- Size – total virtual size of the mapping in KB
- Rss – how many KB of this mapping are actually in RAM
- Pss – Proportional Set Size (shared pages divided by the number of sharers)
- Shared_Clean / Shared_Dirty – shared pages that have or have not been modified
- Private_Clean / Private_Dirty – private pages; dirty pages will need to be swapped
Finding a Specific Process’s Memory Map
# Find a process by name, then inspect its VAS
pidof firefox
# returns e.g. 5678
cat /proc/5678/maps
# Or use pmap for a summary
pmap -x 5678 | tail -5
Common Mistakes When Working With Process VAS
- Confusing virtual and physical addresses: The addresses in
/proc/PID/mapsare virtual addresses, not physical RAM locations. Two processes can have the same virtual address that maps to completely different physical pages. - Assuming all virtual memory is in RAM: The kernel uses demand paging. A mapping can exist in the VAS without any physical page backing it until it is actually accessed (page fault triggers physical allocation).
- Forgetting ASLR: Address Space Layout Randomization (ASLR) means the addresses you see in
/proc/PID/mapschange on every execution. Never hardcode virtual addresses in production code. - Misreading the offset field: The offset field is only meaningful for file-backed mappings. For anonymous mappings (heap, stack), the offset is always 0 and carries no useful information.
What Changed in Linux Kernel 6.x for Process VAS
This free Linux kernel development course keeps content current with the latest kernel releases. Here are the key changes relevant to process VAS in kernel 6.x:
- Maple Tree replaces Red-Black Tree for VMAs: Linux 6.1 replaced the red-black tree used to manage Virtual Memory Areas (VMAs) with a new data structure called the Maple Tree. This dramatically improves performance when a process has hundreds or thousands of mappings — common in large server applications or container environments.
- mmap_lock improvements: The per-mm read-write lock (
mmap_lock) has been a scalability bottleneck. Linux 6.x includes work to reduce contention on this lock, improving performance in multi-threaded applications that frequently allocate or map memory. - UFFD (Userfaultfd) improvements: The userfaultfd mechanism — which allows user space to handle page faults for specific regions — received multiple improvements in 6.x, making it more practical for live migration and snapshot use cases.
- /proc/PID/smaps_rollup: This procfs file (present since 4.14, refined in 6.x) gives a single aggregated summary of all smaps data for a process — useful when you need a quick total RSS without parsing thousands of individual mapping entries.
Security Considerations in Process VAS
Understanding the VAS is directly relevant to Linux security hardening — an important area in any complete free Linux device drivers course or kernel development course:
- NX bit (No-Execute): Modern CPUs enforce that pages marked without the execute bit (shown as
---in the x position in maps output) cannot be executed. This prevents code injection attacks where malicious data is executed as code. - ASLR: Randomizing the base addresses of stack, heap, and library mappings on each execution makes it harder for attackers to craft reliable exploits. Check
/proc/sys/kernel/randomize_va_spaceto see your current ASLR level (0=off, 1=partial, 2=full). - Stack canaries: The kernel’s stack protector feature places a canary value at the end of each stack frame; overwriting it triggers a kernel oops before the corrupted address is used.
- Kernel Page-Table Isolation (KPTI): Since Meltdown, the kernel maps as little of itself into the user VAS as possible, reducing the attack surface for speculative execution exploits.
Key Takeaways – Process VAS in Linux Kernel
- Every process on Linux has its own private Virtual Address Space (VAS) composed of multiple mappings or segments.
- The proc filesystem (
/proc) is the primary interface for inspecting live kernel data about any running process, without needing special tools. /proc/PID/mapslists every segment in a process’s user VAS, sorted by virtual address, with full details on permissions, device, inode, and file backing.- Mappings are either file-backed (loaded from disk — executables, libraries) or anonymous (heap, stack — zero-filled by kernel).
- The
mmap()system call is the fundamental mechanism responsible for creating every single mapping in a process VAS. - Special kernel-injected mappings (
[vdso],[vvar],[vsyscall]) allow certain syscalls to run at near-native speed without a full kernel context switch. - Linux 6.x replaced the VMA red-black tree with a Maple Tree for better scalability with large numbers of mappings.
- Security features like ASLR, NX pages, and KPTI all operate directly on the VAS layout.
Frequently Asked Questions
VmRSS field in /proc/PID/status over time — if it keeps growing without bound, the process is likely leaking. You can also watch VmSize (total virtual size) and check /proc/PID/smaps_rollup for the aggregated Private_Dirty figure, which represents pages the process has actually modified and owns exclusively.Continue Your Free Linux Kernel Development Course
EmbeddedPathashala offers a completely free, structured Linux kernel development course covering kernel internals, Linux device drivers, embedded systems, and Bluetooth/BLE — all at zero cost.
Visit EmbeddedPathashala.com