Notes
All notes

TLB and Virtual Memory Hardware (COA Core #5)

Every load and store uses a virtual address. The MMU must translate it to a physical address before the cache can answer. A page-table walk costs several memory accesses, so the CPU caches recent translations in the TLB. A TLB miss and a page fault are different events, and exams test that distinction constantly.

What the TLB is

The translation lookaside buffer is a small, fast, associative cache of page table entries. A hit gives the physical frame number in one cycle; a miss requires a page-table walk.

Property Typical value
Entries 32–2048 (L1 TLB: 32–64, L2 TLB: 512–2048)
Associativity fully associative or 4–8 way
Replacement LRU or random
Hit latency 1 cycle (parallel with L1 lookup)
Miss cost 10–100 cycles (page walk, often cached)

TLB reach is the address range covered without a miss:

\[ \text{reach} = \text{entries} \times \text{page size} \]

64 entries × 4 KB = 256 KB. With 2 MB huge pages, the same 64 entries cover 128 MB. That is the main reason databases and JVMs ask for huge pages.

QQuestionWhy does a TLB with 64 entries not cover 64 pages of every process at

once?

ASolution

The TLB is shared by whatever is running. A context switch either flushes it or tags entries by address space; either way, a process cannot rely on all entries being its own.

TLB hits and misses

A load proceeds in parallel:

  1. The TLB is looked up with the virtual page number.
  2. On a hit, the physical frame is available; the L1 cache is indexed and tagged.
  3. On a miss, the MMU walks the page table (or the kernel does, depending on the architecture), fills the TLB, and retries.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
  b/.style={draw, thick, rounded corners=3pt, minimum width=2.0cm, minimum height=0.6cm, font=\scriptsize, align=center},
  a/.style={->, thick}]
  \node[b, fill=blue!12] (va) at (0,0) {virtual address\\VPN + offset};
  \node[b, fill=green!18] (tlb) at (3.0,0) {TLB lookup};
  \node[b, fill=orange!15] (walk) at (3.0,-1.2) {page-table walk\\4 levels};
  \node[b, fill=orange!25] (pa) at (6.2,0) {physical address\\PFN + offset};
  \node[b, fill=green!15] (l1) at (6.2,-1.2) {L1 data cache};
  \draw[a] (va) -- (tlb);
  \draw[a] (tlb) -- node[above, font=\tiny] {hit} (pa);
  \draw[a] (tlb) -- node[right, font=\tiny] {miss} (walk);
  \draw[a] (walk) -| (pa);
  \draw[a] (pa) -- (l1);
\end{tikzpicture}

The TLB sits in front of the cache, not beside it. On a TLB miss, the cache cannot even be indexed correctly (for a physically indexed cache), so the walk must finish first.

QQuestionWhy can a TLB miss be more expensive than an L1 miss?
ASolution

A page walk reads up to four page-table entries, each of which may itself miss in the cache and go to memory. An L1 miss only fetches one cache line.

Associativity and replacement

TLBs are small, so they are built for parallel search:

Conflict misses in a TLB are real: a program touching many pages that map to the same set will thrash even when the total page count fits.

QQuestionWhy do TLB conflict misses hurt more than cache conflict misses?
ASolution

A cache miss costs one memory access; a TLB miss costs a page walk of several accesses, and may stall the entire pipeline. Page-granularity locality is also coarser, so a thrashing TLB affects every access in the working set.

Virtual-to-physical translation

A virtual address splits into a virtual page number and a page offset. Translation replaces the VPN with a physical frame number; the offset is unchanged.

\[ \text{VA} = \text{VPN} \,\|\, \text{offset}, \qquad \text{PA} = \text{PFN} \,\|\, \text{offset} \]

With 4 KB pages, the offset is 12 bits. The TLB stores (VPN → PFN) plus permission bits (read/write/execute, user/supervisor, dirty).

Huge pages (2 MB or 1 GB on x86-64) reduce the number of translations and the walk depth, at the cost of internal fragmentation and coarser protection.

\begin{tikzpicture}[font=\rmfamily, >=stealth,
  c/.style={draw, thick, minimum height=0.5cm, font=\scriptsize\ttfamily}]
  \node[c, fill=blue!12, minimum width=2.6cm] (vpn) at (0,0) {VPN (36)};
  \node[c, fill=green!15, minimum width=1.3cm] (off) at (1.95,0) {offset (12)};
  \node[c, fill=orange!18, minimum width=1.8cm] (pfn) at (0,-0.9) {PFN (e.g. 24)};
  \node[c, fill=green!15, minimum width=1.3cm] (off2) at (1.95,-0.9) {offset (12)};
  \draw[->, thick] (vpn) -- node[right, font=\tiny] {TLB / page table} (pfn);
  \node[font=\scriptsize, anchor=west] at (3.0,-0.3) {virtual};
  \node[font=\scriptsize, anchor=west] at (3.0,-1.2) {physical};
\end{tikzpicture}
QQuestionDoes a 2 MB page change the number of offset bits?
ASolution

Yes: \(2^{21}\) bytes means 21 offset bits. The VPN shrinks by 9 bits, so fewer translations are needed for the same address range.

Page-table walks

x86-64 with 4-level paging splits the 36-bit VPN into four 9-bit indices. A walk reads PML4, PDPT, PD, and PT entries — four memory accesses, each potentially a cache miss.

\begin{tikzpicture}[font=\rmfamily, >=stealth,
  b/.style={draw, thick, minimum width=1.5cm, minimum height=0.55cm, font=\scriptsize, align=center},
  a/.style={->, thick}]
  \node[b, fill=blue!12] (cr3) at (0,0) {CR3};
  \node[b, fill=blue!10] (pml4) at (2.0,0) {PML4};
  \node[b, fill=orange!15] (pdpt) at (4.0,0) {PDPT};
  \node[b, fill=orange!20] (pd) at (6.0,0) {PD};
  \node[b, fill=green!18] (pt) at (8.0,0) {PT};
  \node[b, fill=green!25] (frame) at (10.0,0) {frame};
  \foreach \x/\y in {cr3/pml4,pml4/pdpt,pdpt/pd,pd/pt,pt/frame} \draw[a] (\x) -- (\y);
  \node[font=\scriptsize, anchor=north] at (5.0,-0.7) {each arrow is one memory read};
\end{tikzpicture}

The hardware caches intermediate levels in the page-walk cache and in the TLB itself. A walk that hits those caches is much cheaper than four DRAM accesses.

QQuestionWhy does a 5-level page table make walks more expensive?
ASolution

One more level is one more dependent memory read, even if the TLB hit rate compensates most of the time.

TLB versus CPU cache

TLB CPU cache
Caches translations (VPN → PFN) data and instructions
Key virtual page number physical (usually) or virtual address
Size tens–thousands of entries KB–MB
Miss cost page walk (10–100 cycles) next level (cycles–100 ns)
Managed by MMU, kernel fills entries hardware

They interact. On a TLB miss, the cache cannot be searched until the physical address exists. Some CPUs use virtually indexed, physically tagged (VIPT) L1 caches so the index can be computed in parallel with the TLB lookup; the tag check waits for the physical address.

QQuestionWhat problem does a virtually indexed cache introduce?
ASolution

Two virtual addresses can map to the same physical page (aliasing), so the same data may be cached twice under different virtual tags. The OS and hardware must handle that, which is why most caches are physically indexed.

TLB and page faults

These are different:

Event Meaning Who handles it Cost
TLB hit translation cached hardware ~1 cycle
TLB miss translation not cached, page is present hardware walk / kernel 10–100 cycles
Page fault PTE not present, or permission violation kernel µs–ms

A TLB miss may find the PTE present and refill the TLB. A page fault means the mapping itself is invalid: the page is on disk, not yet allocated, or the access is illegal. The kernel handles the fault, possibly loads the page, updates the PTE, and returns; the instruction retries.

Every page fault invalidates or updates TLB entries, which is why fault-heavy workloads also show TLB pressure.

QQuestionCan a page fault occur without a TLB miss?
ASolution

Yes. If the TLB still holds a stale translation for a page that was unmapped (and the OS has not invalidated it), the access can fault even though the TLB “hit”. Correct OSes invalidate the TLB when they change a mapping.

Context switches and address spaces

Each process has its own page tables, so translations are only valid for one address space. On a context switch the kernel can:

Tagging avoids the flush but consumes TLB capacity: the effective reach per process shrinks.

Changing a mapping on one core requires invalidating the translation on other cores that may have cached it. That is a TLB shootdown, implemented with inter-processor interrupts. It is expensive, which is why kernels batch and defer unmappings.

QQuestionWhy is a TLB shootdown needed after munmap?
ASolution

Other cores may still hold the old translation. Without invalidation, they could access freed physical memory.

Deep: simulate an LRU TLB by hand

Four-entry fully associative TLB, LRU replacement. Page references (VPNs): 1 2 3 4 1 5 1 2 3 4.

Ref TLB after Result
1 1 miss
2 1 2 miss
3 1 2 3 miss
4 1 2 3 4 miss
1 2 3 4 1 hit
5 3 4 1 5 miss, evict 2
1 3 4 5 1 hit
2 4 5 1 2 miss, evict 3
3 5 1 2 3 miss, evict 4
4 1 2 3 4 miss, evict 5

Hits: 2. Misses: 8. Hit rate: 20%.

The same trace with 4 KB pages covers \(4 \times 4\text{KB} = 16\text{KB}\) of address space. If the program touches 8 pages cyclically, LRU with 4 entries thrashes; with 2 MB pages those 8 pages fit in 2 entries.

For a set-associative TLB, add the set index: with 8 entries and 2 ways, VPN bits select the set, and each set does its own LRU. The procedure is identical to the cache simulation in Ch. 4.

QQuestionRepeat the trace with FIFO instead of LRU. How many hits?
ASolution

FIFO evicts the oldest insertion: after 1 2 3 4, ref 1 hits; 5 evicts 1; 1 misses (evict 2); 2 misses (evict 3); 3 misses (evict 4); 4 misses (evict 5). That is 1 hit, 9 misses — worse than LRU on this trace.

EMAT with a TLB

\[ \text{EMAT} = h\,(t_{\text{TLB}} + t_{\text{mem}}) + (1-h)\,(t_{\text{TLB}} + k\,t_{\text{mem}} + t_{\text{mem}}) \]

where \(h\) is the TLB hit rate and \(k\) is the walk depth.

Example: \(h = 0.98\), \(t_{\text{TLB}} = 1\) ns, \(t_{\text{mem}} = 100\) ns, \(k = 4\):

\[ \text{EMAT} = 0.98(101) + 0.02(1 + 400 + 100) = 98.98 + 10.02 = 109 \text{ ns} \]

A 2% miss rate adds about 8% to the access time. At 90% hit rate it adds 40%.

QQuestionWhy does doubling TLB entries not always halve the miss rate?
ASolution

Misses are driven by the working set’s page count, conflict patterns, and context switches. If the working set is far larger than the TLB, doubling capacity may barely change the hit rate.

Bugs and edge cases

QQuestionA program’s first iteration is much slower than the rest, and the

slowdown disappears with huge pages. What was the bottleneck?

ASolution

TLB misses and page walks on a working set that fits in memory but not in TLB reach. Huge pages multiply the reach.

Next: Cache Coherence and Multiprocessors (COA Core #6) — MESI, false sharing, and why two threads on different variables fight.