lesson depth
Mastery
not started · 0%

Virtual Memory & Paging Mechanics

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

Freshness: current15 min readComputer Science and Programming

Key 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(20 lines)
1// C example: Memory mapping a file directly to virtual address space
2#include <stdio.h>
3#include <fcntl.h>
4#include <sys/mman.h>
5#include <unistd.h>
6
7int main() {
8 int fd = open("data.bin", O_RDONLY);
9 size_t length = 4096;
10
11 // Map file into virtual address space
12 char *map = mmap(NULL, length, PROT_READ, MAP_SHARED, fd, 0);
13 if (map != MAP_FAILED) {
14 printf("First byte: %c\n", map[0]);
15 munmap(map, length);
16 }
17 close(fd);
18 return 0;
19}

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.

Prerequisites & Related Concepts (2)

Private notes

0 words
Next