Concept lesson

Virtual Memory & Paging Mechanics

Page tables, TLB cache hits, page faults (major/minor), and mmap() memory mapping.

lesson
Freshness: current15 min read
Mastery
not started · 0%

Learning outcomes

  • Analyze virtual memory allocation and page table translation
  • Optimize file I/O performance using mmap() memory mapping

Mental model

Linux abstracts physical RAM by assigning every process an isolated Virtual Address Space. The MMU (Memory Management Unit) translates virtual addresses to physical RAM pages (typically 4KB) using multi-level Page Tables and CPU TLB caches.

Virtual Memory Address Access
Check CPU TLB Cache
TLB HIT -> Access Physical RAM Page
TLB MISS -> MMU Page Table Walk
Page Fault Handler (Minor vs Major Disk Read)
Conceptual teaching model synthesized from:Linux Kernel Kernel.org Official Architecture & Systems Documentation

Theory

  • Page Table: Hierarchical table mapping virtual page numbers (VPN) to physical frame numbers (PFN).
  • TLB (Translation Lookaside Buffer): On-CPU hardware cache storing recent virtual-to-physical address translations.
  • Minor Page Fault: Requested page is in physical RAM but not mapped in the process page table (e.g. COW allocation). Zero disk I/O.
  • Major Page Fault: Requested page is not in physical RAM and must be read from disk (swap space or file system). High I/O latency.
// C example: Memory mapping a file directly to virtual address space
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>

int main() {
    int fd = open("data.bin", O_RDONLY);
    size_t length = 4096;
    
    // Map file into virtual address space
    char *map = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
    if (map != MAP_FAILED) {
        printf("First byte: %c\n", map[0]);
        munmap(map, length);
    }
    close(fd);
    return 0;
}

Alternatives and trade-offs

  • Standard read() / write(): Copies data between kernel page cache and user space buffer.
  • mmap() File Mapping: Zero-copy I/O; maps disk files directly into process address space, eliminating user/kernel buffer copying.

Failure modes and misconceptions

  1. TLB Cache Thrashing: Allocating gigabytes of RAM across millions of 4KB pages overloads CPU TLB cache capacity, causing performance drops. Remedy: Enable Transparent Huge Pages (THP 2MB/1GB pages) for large database engines.
  2. Ignoring Major Page Fault Spikes: High major page fault rates indicate memory starvation forcing constant disk swap reads, degrading application throughput.
Reflect before revealing the guide

Decision scenario

Use mmap() for high-throughput database storage engines to map data files directly into virtual memory, eliminating user-space buffer copy overhead.

Learning outcomes

  • Trace virtual-to-physical address translation via MMU page tables and TLB caches.
  • Distinguish zero-disk-I/O minor page faults from disk-bound major page faults.
  • Optimize file I/O using mmap() zero-copy memory mapping.

Trade-offs

Virtual memory provides strict process memory isolation and lazy page allocation, but page faults and TLB misses introduce CPU performance overhead.

Evidence assessment

Theory and decision mastery

not-started · 0%
theory0%
decision0%
activityNot mapped
projectNot mapped
1. What is the core architectural principle governing linux virtual memory paging?
2. What primary operational trade-off must be managed when configuring linux virtual memory paging?
3. Which failure mode is most commonly observed when linux virtual memory paging is misconfigured?

Decision scenario

You are designing a high-concurrency production system requiring reliable execution of linux virtual memory paging under heavy traffic load.

Which architectural decision ensures maximum resilience, scalability, and system stability?

Primary sources