This project explores how virtual memory is implemented in Linux by building page table walkers in both user space and kernel space. It was originally developed for an Operating Systems course, but is useful as a standalone reference for anyone learning about page tables, /proc/self/pagemap, kmalloc, and vmalloc.
The repo contains:
- A user-space page table walker (
memalloc) that uses/proc/self/pagemapto translate user-level virtual addresses to physical addresses. - A kernel module (
ko5204) that allocates kernel memory usingvmallocandkmalloc, walks the kernel page tables, and logs the virtual→physical mappings.
.
├── user_space/ # User-space page table walker
│ ├── memalloc.c
│ ├── pagemap.c
│ ├── pagemap.h
│ └── Makefile
└── kernel_space/ # Kernel-space page table walker
├── ko5204.c
└── Makefile
memalloc is a user-space program that:
- Allocates memory in two different ways.
- Walks the process's own page table via
/proc/self/pagemap. - Prints one line per page in the form:
VA=0x<virtual_address> -> PA=0x<physical_address>This allows the user to see how Linux maps virtual pages to physical frames for a single process.
The user can choose the allocation strategy with -b and the total size with -m:
-
-b 4K-> 4 KB mode- Allocates many individual 4 KB blocks (one per page) using a double-pointer array.
- Each block is touched to ensure it is backed by a physical page.
- Good for observing fragmented virtual allocations and scattered physical frames.
-
-b 1G-> big block mode- Allocates a single large contiguous virtual range.
- The total size is controlled by
-m(does not have to be 1GiB; the name is historical) - Each page in that large block is touched and then translated.
Note
-b selects the allocation mode (many small blocks vs one big block).
-m selects the total amount of memory to allocate.
From user_space/ directory, run:
makeTo create the executable:
memalloc# Many 4KB blocks, 64MB total
./memalloc -b 4K -m 64M -o out_4k.txt
# One large block, 64MB total
./memalloc -b 1G -m 64M -o out_big.txt
# Full 1GB single block (if the machine can handle it)
./memalloc -b 1G -m 1G -o out_1g.txtko5204 is a Linux Kernel module that:
- Creates a
/proc/pagetableinterface. - Responds to simple commands written from user space:
vmallockmalloc
- Allocated 1MiB of kernel memory (256 x 4KiB pages), walks the mapping page-by-page and logs:
Vmalloc: VA=0xffff89af05cf8000 -> PA=0x0000000123456000
Kmalloc: VA=0xffff89af05cf9000 -> PA=0x0000000123457000These logs show how the kernel maps its own virtual addresses to physical frames for different allocators.
*vmalloc
* Returns a virtually contiguous region.
* Backed by non-contiguous physical pages.
* We use vmalloc_to_page() + page_to_phys() to translate VA -> PA.
* Physical addresses jump around between pages.
*kmalloc
* Returns **physically contiguous memory (from direct-mapped region).
* We use virt_to_phys() to translate VA -> PA.
* Both virtually and physical addresses usually increase smoothly page-by-page.
* Large allocations can fail if there isn't enough contiguous physical memory.
From kernel_space/ directory, run:
sudo apt-get update
sudo apt-get install -y build-essential linux-headers-$(uname -r)
makeTo create the executable:
ko5204.ko# Insert the module
sudo insmod ko5204.ko
# Confirm it's loaded
lsmod | grep ko5204
# Trigger vmalloc-based walk
echo "vmalloc" | sudo tee /proc/pagetable
# Trigger kmalloc-based walk
echo "kmalloc" | sudo tee /proc/pagetableCheck the kernel logs:
sudo dmesg | tail -n 100
# or on Ubuntu:
sudo tail -n 100 /var/log/kern.logUnload the module when done:
sudo rmmod ko5204This project is released under the MIT License.
Felipe Campoverde