How Linux Kernel Turns malloc Calls into Physical Memory: A Simple Guide
This article explains Linux kernel memory management by describing how user‑level malloc allocates virtual memory, how page tables map virtual to physical addresses, and how the hardware‑triggered page‑fault mechanism ultimately provides the needed physical pages.
Memory Allocation Overview
The article starts with the familiar malloc interface that user programs call to obtain memory. In Linux, malloc returns a virtual address; the corresponding physical memory is not allocated at this point.
Virtual Memory vs Physical Memory
Virtual memory is a logical concept from the process perspective and does not exist as a physical entity.
Physical memory resides on the actual RAM modules.
Each virtual address has a potential mapping to a physical address.
When virtual memory is allocated, the physical memory may remain unassigned until it is accessed.
Page Tables
Mapping between virtual and physical memory is maintained by page tables, which can be thought of as a hash‑like table stored in memory that records the correspondence of virtual addresses to physical frames.
Who Uses Page Tables?
The page table is not accessed directly by applications. Instead, the CPU’s Memory Management Unit (MMU) reads the page table to translate virtual addresses to physical ones. The operating system kernel creates and updates the page tables, while the MMU handles the actual translation.
Multiple Page Tables per Process
Each process has its own independent virtual address space, so each process requires its own page table. Consequently, the number of page tables in the system equals the number of active processes.
Page Table Creation and Maintenance
Page tables are created when a new process is spawned. The kernel is responsible for building and maintaining these tables throughout the process’s lifetime.
Page Fault Handling
If the MMU looks up a virtual address and finds no corresponding page‑table entry, it triggers a page‑fault exception. The hardware raises this synchronous exception, and the kernel’s page‑fault handler allocates a physical page, updates the page table, and then resumes the original instruction.
Step‑by‑Step Memory Allocation Process
User‑space program calls malloc and receives a virtual address.
The program later accesses that address (e.g., via memset).
The MMU attempts to translate the virtual address to a physical one.
The MMU reads the page table and discovers the entry is missing.
A page‑fault exception is automatically generated by the hardware.
The CPU jumps to the kernel’s registered page‑fault handler.
The handler allocates a physical page and inserts a new entry into the process’s page table.
Control returns to user space, and the original memory operation proceeds.
Further Topics
Kernel memory allocators such as slab and vmalloc The buddy system for managing physical pages
Process address‑space management
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
