S4 · Lesson 24 · Virtual memory and TLBs
Page tables, multi-level walks, TLB reach, huge pages, virtually indexed caches and page faults
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.
- 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 bits with 8-byte entries, so bigger pages also mean shorter page walks.
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?
512 GB: 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.
Name it
Translation
A virtual address splits into a page number and an offset of 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 entries.
- A multi-level (radix) table resolves bits per level, so it has 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 , memory time and L levels:
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 , the average access is , so p must be tiny.
Worked 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?
Show the solution
- Bits per level.
- Levels. The page number has 48 − 12 = 36 bits:
- Reach with 4 KB pages.
- Reach with 2 MB pages. 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.
Worked 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?
Show the solution
- Fault rate. The slowdown stays under 10% if : under one fault per million accesses.
- Size of a way. more than a 4 KB page, so one index bit would come from the translated part.
- What it would take. It would need 16 ways, or tricks to handle aliases.
Answer the fault rate must stay under ; the cache can't be plain VIPT with 8 ways (it would need 16 ways, or alias handling).
Worked 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?
Show the solution
- Average access time.
- At 99%.
- At 99.9%.
- 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
A TLB has 64 entries for 4 KB pages. What's its reach?
Check
A 48-bit virtual address space uses pages of bytes and 8-byte page table entries, with each table one page in size. How many levels does the page table need?
Check
How big would a flat page table be for a 39-bit address space with -byte pages and 8-byte entries?
Check
A TLB lookup takes 1 ns and memory 80 ns. The TLB hits 0.995 of the time; a miss walks 4 levels, each a memory access. What's the average time for a memory access?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
With 4 KB pages, how large can a 4-way VIPT cache be without aliasing?
Check
Memory takes 60 ns and a page fault 5 ms. What's the largest fault rate per access that keeps the slowdown within 0.2?
Check
A machine has 48-bit virtual addresses, 4 KB pages, 8-byte page-table entries and a 1536-entry TLB with a 0.995 hit rate; memory takes 100 ns and the TLB 1 ns.
(a)
How many levels does its page table need?
(b)
What is the TLB's reach?
(c)
What is the average access time?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
Check
Memory takes M seconds and a page fault F; faults happen with probability p per access.
Step 1
What is the average access time?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What fault rate gives a slowdown of s (a fraction above M)?
Explain why
Why do huge pages speed up programs with large working sets?
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
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.