---
title: Virtual memory and TLBs
heading: Page tables, multi-level walks, TLB reach, huge pages, virtually indexed caches and page faults
eyebrow: S4 · Lesson 24 · Virtual memory and TLBs
xp: 50
---

::: hook
Every load and store a program issues uses a virtual address, which the hardware translates to a physical one through page tables the operating system maintains. That translation gives each process its own address space, protects them from each other and lets memory spill to disk, but it would double or quintuple the cost of every access if done naively, since the page tables themselves live in memory. A small cache of recent translations, the TLB, makes the cost almost disappear, as long as the program's working set fits within the TLB's reach. When it doesn't, huge pages and page-walk caches come to the rescue.
:::

::: play
- **TLB reach**, the memory a TLB can map at once (entries × page size), against page size, on log scales. Programs whose working set exceeds the reach take a page walk on many accesses.
- With 4 KB pages, a 64-entry TLB reaches only 256 KB, smaller than many L2 caches. With 2 MB huge pages the same TLB reaches 128 MB.
- Each page-table level resolves $\log_2(\text{page}/8)$ bits with 8-byte entries, so bigger pages also mean shorter page walks.
:::
:::

```sim plot
view: graph
x: P = 4..4096 KB log
y: R = 0.1..100000 MB log
slider: E = 16..4096 = 64 log
plot: E*P/1024 | samples: 200 | label: TLB reach, MB
marker: x = 4 | label: 4 KB
marker: x = 2048 | label: 2 MB
readout: reach with 4 KB pages, MB = E*4/1024
readout: reach with 2 MB pages, MB = E*2
```

::: predict
## A 48-bit virtual address space uses 4 KB pages and 8-byte page table entries. How large would a single flat page table be?

- [ ] 4 KB
- [ ] 512 KB
- [ ] 512 MB
- [x] 512 GB

::: reveal
512 GB: $2^{48}/2^{12} = 2^{36}$ pages, each needing an 8-byte entry. That's why page tables are trees: a 4 KB table page holds 512 entries and resolves 9 bits, so four levels cover the 36 bits of page number, and only the parts of the tree a process uses need to exist. The cost is a walk of up to four memory accesses on a TLB miss.
:::
:::

::: nameit
## Translation

A virtual address splits into a page number and an offset of $\log_2(\text{page size})$ bits. The page table maps page numbers to physical frames, with permission bits (read, write, execute, user) checked on every access.

- A flat table needs $2^{V - \text{offset}}$ entries.
- A **multi-level** (radix) table resolves $\log_2(\text{page}/\text{PTE size})$ bits per level, so it has $\left\lceil\frac{V - \text{offset}}{\log_2(\text{page}/\text{PTE})}\right\rceil$ levels.

## The TLB

A small, usually fully associative cache of translations. **Reach** = entries × page size. A miss triggers a **page walk** (in hardware on x86, ARM and RISC-V), up to one memory access per level, helped by page-walk caches. With TLB hit rate h, TLB time $t_T$, memory time $t_M$ and L levels:

$$t = t_T + t_M + (1 - h)Lt_M$$

**Huge pages** (2 MB, 1 GB) raise reach and remove levels from the walk.

## Caches and translation

A **virtually indexed, physically tagged** (VIPT) L1 looks up its set with untranslated offset bits while the TLB translates in parallel. That works only if the index and line offset fit in the page offset: each way can be at most one page, so the cache is at most page size × ways. That's why L1 caches are 32 to 48 KB with 8 to 12 ways and 4 KB pages.

## Page faults

A missing page costs milliseconds to fetch from storage. With fault rate p, fault time F and memory time $t_M$, the average access is $(1 - p)t_M + pF$, so p must be tiny.
:::

::: example
## For 48-bit virtual addresses, 4 KB pages and 8-byte entries: how many levels, and what does a 64-entry TLB reach with 4 KB and with 2 MB pages?

::: solution
1. **Bits per level.**
   $$\log_2(4096/8) = 9\ \text{bits}$$
2. **Levels.** The page number has 48 − 12 = 36 bits:
   $$36/9 = 4\ \text{levels}$$
3. **Reach with 4 KB pages.**
   $$64 \times 4\ \text{KB} = 256\ \text{KB}$$
4. **Reach with 2 MB pages.**
   $$64 \times 2\ \text{MB} = 128\ \text{MB}$$
   and the walk has one level fewer (21-bit offset, 27 bits left, 3 levels).

**Answer:** 4 levels; the TLB reaches 256 KB with 4 KB pages and 128 MB with 2 MB pages.
:::
:::

::: example
## Memory takes 100 ns and a page fault 10 ms. What fault rate keeps the slowdown under 10%? And can a 64 KB, 8-way L1 be VIPT with 4 KB pages?

::: solution
1. **Fault rate.** The slowdown stays under 10% if $pF < 0.1t_M$:
   $$p < \frac{10\ \text{ns}}{10\ \text{ms}} = 10^{-6}$$
   under one fault per million accesses.
2. **Size of a way.**
   $$64\ \text{KB}/8 = 8\ \text{KB per way}$$
   more than a 4 KB page, so one index bit would come from the translated part.
3. **What it would take.** It would need 16 ways, or tricks to handle aliases.

**Answer:** the fault rate must stay under $10^{-6}$; the cache can't be plain VIPT with 8 ways (it would need 16 ways, or alias handling).
:::
:::

::: example
## A TLB lookup takes 1 ns, memory 100 ns, and a miss needs a 4-level walk. What is the average access time at 99% and 99.9% TLB hit rates?

::: solution
1. **Average access time.**
   $$t = t_T + t_M + (1 - h)Lt_M$$
2. **At 99%.**
   $$\begin{aligned} t &= 1 + 100 + 0.01 \times 4 \times 100 \\ &= 105\ \text{ns} \end{aligned}$$
3. **At 99.9%.**
   $$\begin{aligned} t &= 1 + 100 + 0.001 \times 4 \times 100 \\ &= 101.4\ \text{ns} \end{aligned}$$
4. **Why huge pages matter.** A 1% miss rate costs 4%, which is why huge pages, by cutting both the miss rate and the walk length, matter so much for big-memory programs.

**Answer:** 105 ns at 99% and 101.4 ns at 99.9%.
:::
:::

::: check
given: E = 32 | 64 | 1536; P = 4 | 64 | 2048
answer: E*P
unit: KB
tolerance: 0.5%

## A TLB has {E} entries for {P} KB pages. What's its reach?

::: right
Right. Entries × page size = {answer} KB.
:::

::: misses
- [E] Each entry maps a whole page.
- [any] Not yet. Entries × page size.
:::
:::

::: check
given: V = 39 | 48 | 57; p = 12 | 14 | 16; e = 8
answer: ceil((V - p)/(p - log(e)/log(2)) - 0.0001)
tolerance: 0

## A {V}-bit virtual address space uses pages of $2^{\given{p}}$ bytes and {e}-byte page table entries, with each table one page in size. How many levels does the page table need?

::: right
Right. Each level resolves $p - \log_2 8$ bits: {answer} levels.
:::

::: misses
- [any] Not yet. Each table page holds page/8 entries, so a level resolves $p - 3$ bits; divide the page-number bits by that and round up.
:::
:::

::: check
given: V = 32 | 39 | 48; p = 12 | 16; e = 4 | 8
answer: 2^(V - p)*e/2^30
unit: GB
tolerance: 0.5%

## How big would a flat page table be for a {V}-bit address space with $2^{\given{p}}$-byte pages and {e}-byte entries?

::: right
Right. $2^{V - p}$ entries × {e} bytes = {answer} GB.
:::

::: misses
- [2^V*e/2^30] Only the page number needs an entry.
- [any] Not yet. One entry per virtual page.
:::
:::

::: check
given: T = 1 | 2; M = 80 | 120; h = 0.98 | 0.995; L = 3 | 4
answer: T + M + (1 - h)*L*M
unit: ns
tolerance: 0.5%

## A TLB lookup takes {T} ns and memory {M} ns. The TLB hits {h} of the time; a miss walks {L} levels, each a memory access. What's the average time for a memory access?

::: right
Right. $t_T + t_M + (1 - h)Lt_M$ = {answer} ns.
:::

::: misses
- [T + M + (1 - h)*M] Each of the L levels is a separate access.
- [any] Not yet. Translation, the access itself, and the walks.
:::
:::

::: check
given: P = 4 | 16; w = 4 | 8 | 12
answer: P*w
unit: KB
tolerance: 0

## With {P} KB pages, how large can a {w}-way VIPT cache be without aliasing?

::: right
Right. One page per way: {answer} KB.
:::

::: misses
- [P] Each way can be a page; there are w of them.
- [any] Not yet. The index and offset must fit in the page offset.
:::
:::

::: check
given: M = 60 | 100; F = 5 | 10 | 20; s = 0.05 | 0.2
answer: s*M*1e-9/(F*1e-3)
tolerance: 0.5%

## Memory takes {M} ns and a page fault {F} ms. What's the largest fault rate per access that keeps the slowdown within {s}?

::: right
Right. $p < s\,t_M/F$ = {answer}.
:::

::: misses
- [M*1e-9/(F*1e-3)] Scale by the allowed slowdown.
- [any] Not yet. Set $pF$ equal to the allowed extra time.
:::
:::

::: check
kind: parts
given: V = 39 | 48 | 57; P = 4 | 16 | 64; E = 64 | 1536; h = 0.98 | 0.995

## A machine has {V}-bit virtual addresses, {P} KB pages, 8-byte page-table entries and an {E}-entry TLB with a {h} hit rate; memory takes 100 ns and the TLB 1 ns.

::: part
answer: ceil((V - log(P*1024)/log(2))/(log(P*1024/8)/log(2)) - 1e-9)
tolerance: 0

### How many levels does its page table need?

::: misses
- [any] $\lceil(V - \text{offset})/\log_2(\text{page}/8)\rceil$.
:::
:::

::: part
answer: E*P/1024
unit: MB
tolerance: 0.5%

### What is the TLB's reach?

::: misses
- [any] Entries × page size.
:::
:::

::: part
answer: 1 + 100 + (1 - h)*ceil((V - log(P*1024)/log(2))/(log(P*1024/8)/log(2)) - 1e-9)*100
unit: ns
tolerance: 0.5%

### What is the average access time?

::: misses
- [any] $t_T + t_M + (1 - h)Lt_M$.
:::
:::
:::

::: check
kind: derivation
vars: p = 1e-9..1e-4, F = 1e-3..1e-1, M = 5e-8..2e-7, s = 0.01..1

## Memory takes M seconds and a page fault F; faults happen with probability p per access.

::: step
answer: (1 - p)*M + p*F

### What is the average access time?
:::

::: step
answer: s*M/(F - M)

### What fault rate gives a slowdown of s (a fraction above M)?
:::

::: right
Right. With F a hundred thousand times M, even one fault in a million accesses doubles the average.
:::
:::

::: explain
## Why do huge pages speed up programs with large working sets?

::: model
Every translation the TLB holds covers one page, so the amount of memory a program can touch without a TLB miss is the number of entries times the page size. With 4 KB pages, a typical TLB covers a few megabytes at best, while databases and scientific codes touch gigabytes, so they take a page walk on a large share of their accesses, each walk costing several memory reads. A 2 MB page lets one entry cover 512 times as much memory, raising the reach to hundreds of megabytes, and it also removes a level from the page-table tree, so the walks that remain are shorter. The cost is coarser allocation, since memory comes in larger units that can be partly wasted.
:::
:::

::: practice

::: check
given: E = 48 | 2048; P = 4 | 2048
answer: E*P
unit: KB
tolerance: 0.5%

## Reach: {E} entries, {P} KB pages?

::: misses
- [any] Entries × page size.
:::
:::

::: check
given: V = 48 | 57; p = 12 | 21; e = 8
answer: ceil((V - p)/(p - log(e)/log(2)) - 0.0001)
tolerance: 0

## Page-table levels: {V}-bit VA, $2^{\given{p}}$-byte pages?

::: misses
- [any] $\lceil (V - p)/(p - 3) \rceil$.
:::
:::

::: check
given: T = 1 | 1.5; M = 90 | 110; h = 0.97 | 0.99; L = 4 | 5
answer: T + M + (1 - h)*L*M
unit: ns
tolerance: 0.5%

## Average access: TLB {T} ns, memory {M} ns, hit {h}, {L} levels?

::: misses
- [any] $t_T + t_M + (1 - h)Lt_M$.
:::
:::
:::
