TLB Flush and Address-Space IDs
A TLB holds copies of table entries. When the tables change, or another program takes over, a copy can become a lie. This page shows two ways to keep the TLB honest.
You Will Be Able To
- Explain how a TLB entry goes out of date, by a changed table or by a switch of program.
- Explain what a flush does, and what it costs.
- Explain how a number in each entry lets two programs share a TLB with no flush.
- Lesson
- S3.24
- Time
- 24 min
- Runs on
- Your browser
- Check
- 6 questions
Two programs take turns on one processor. Each has its own tables, and they are correct. Still program 1 reads a word that program 0 wrote. No table says that it may.
Before You Start
You need S3.8: TLB hit, miss and refill, and the rule that the TLB holds copies.
You also need S3.7: each program has its own page table.
| Question | |
|---|---|
| 1 | How can an entry of the TLB be wrong when the tables are right? |
| 2 | What does a flush do? |
| 3 | What does a flush at every switch of program cost? |
| 4 | What is an address-space ID? |
| 5 | When must the TLB be flushed even with such IDs? |
The Idea: A Copy Must Be Told
The last rule of the TLB page was that the TLB holds copies. A copy is right only as long as the original stays the same. There are two ways for the original to change.
The first: the operating system changes an entry of a table. It takes a frame away from a page, or it takes away the right to write. The TLB still holds the old entry, and goes on using it.
The second: another program gets the processor. The operating system points the walker to the root table of that program. Every page number now means something else. The TLB still holds the meanings of the old program.
An entry of the TLB that no longer matches the tables is a stale entry. The cure for both cases is to remove it. Removing entries from the TLB is called a flush. A flush may empty the whole TLB, or remove the entries of one page.
A flush of the whole TLB at every switch is safe and costly. After each switch, every page misses again and is walked again. Programs switch many times a second.
There is a cheaper way for the second case. Give each program a small number, and store it in every entry of the TLB. A hit then needs the right page and the right number. Entries of several programs can sit side by side, and a switch needs no flush. The number is called an address-space ID, or ASID.
New Words
| Word | Meaning |
|---|---|
| Address space | All the virtual addresses of one program, with the table that gives them a meaning. |
| Switch of program | The moment when the operating system takes the processor from one program and gives it to another. Also called a context switch. |
| Stale entry | An entry of the TLB that no longer matches the page tables. |
| Flush of a TLB | Removing entries from the TLB: all of them, or those of one page. |
| Address-space ID | A small number that names one address space. It is stored in each entry of the TLB. ASID for short. |
| Current ID | The address-space ID of the program that runs now. The operating system sets it at a switch. |
One Picture: The Stale Entry
- Memory
- Control / FSM
- Processor
- Bus
- A refill makes a copy
Program 0 used page 00404. The TLB holds a copy of its translation: frame 06.
- Another program takes over
Program 1 has a table of its own. There, page 00404 lies in frame 0c.
- The copy still answers
The TLB compares page numbers only. It finds 00404 and says frame 06. Program 1 now reads the data of program 0.
- Two cures
Empty the TLB at every switch. Or put the number of the program into each entry, and compare it too.
Read the diagram as text
- TLB entry: page 00404 is in frame 06
- Table of program 0: page 00404 is in frame 06
- Table of program 1: page 00404 is in frame 0c
- Program 1 runs: loads from page 00404
- Frame 06: data of program 0
- A number in the entry: whose translation is this
- Table of program 0 → TLB entry: copied by a refill
- Program 1 runs → TLB entry: page 00404?
- TLB entry → Frame 06: a hit: frame 06
- Program 1 runs → Table of program 1: should have asked here
- A number in the entry → TLB entry: compared on every lookup
The TLB cannot see that the tables behind it changed. Something must tell it.
The Smallest Example, Worked by Hand
Two programs each use page 00404. In program 0 it lies in frame 06, and in program 1 in frame 0c. The TLB starts empty. Program 0 loads from the page, then program 1, then program 0 again.
| Who | TLB before | Result | TLB after |
|---|---|---|---|
| program 0 | empty | miss, walk: frame 06 | 00404 to 06 |
| program 1 | empty (flushed) | miss, walk: frame 0c | 00404 to 0c |
| program 0 | empty (flushed) | miss, walk: frame 06 | 00404 to 06 |
Three loads, three walks. Every answer is right.
| Who | TLB before | Result | TLB after |
|---|---|---|---|
| program 0 | empty | miss, walk: frame 06 | ID 0: 00404 to 06 |
| program 1 | ID 0: 00404 to 06 | the page matches, the ID does not: miss, walk: frame 0c | both entries |
| program 0 | both entries | hit in the entry with ID 0: frame 06 | both entries |
Three loads, two walks. The entry of program 0 was still there when it came back.
The second table saves one walk in three loads. With longer turns the gain is larger: nothing that a program has put into the TLB is thrown away at a switch.
The Rules
Rule 1: The TLB cannot see a change of the tables.
Whoever changes a table must also remove the copies.
Rule 2: After an entry of a table changes, the TLB entries of that page are flushed.
The next access walks, and gets the new entry.
Rule 3: A switch of program changes the meaning of every page number.
Without IDs, the whole TLB is flushed at the switch.
Rule 4: With IDs, a hit needs the page number and the current ID.
A switch then only changes the current ID.
Rule 5: An ID must name one address space at a time.
Before an ID is given to another program, its old entries are flushed.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.7. See "Implementing Protection with Virtual Memory". On a switch of process the TLB is cleared, or its entries carry a process identifier.
In RISC-V the number of the address space sits in the register that holds the root table. One instruction flushes the TLB. We state this from common knowledge. It is not checked against the RISC-V privileged specification, which is not in our library.
Rule 5 follows from rule 4. IDs are few, and a system with more programs than IDs must use an ID again.
The two programs and the TLB of four entries are our own teaching example.
Now Run It
The lab at the end of this page is the TLB of four entries from the earlier page, in test code. It now has two programs with a table each. Both map page 00404, to different frames.
It runs the three loads of the hand example twice. First with a flush at each switch, then with an ID in each entry. Then it counts a longer run under both policies.
program 0 load 00404010 | TLB miss, walk, entry 0 filled | physical 06010
-- switch to program 1, and flush the TLB
program 1 load 00404010 | TLB miss, walk, entry 1 filled | physical 0c010
-- switch to program 0, and flush the TLB
program 0 load 00404010 | TLB miss, walk, entry 2 filled | physical 06010
accesses=3 hits=0 walks=3 table_reads=6
program 0 load 00404010 | TLB miss, walk, entry 0 filled | physical 06010
-- switch to program 1
program 1 load 00404010 | TLB miss, walk, entry 1 filled | physical 0c010
-- switch to program 0
program 0 load 00404010 | TLB hit in entry 0 | physical 06010
accesses=3 hits=1 walks=2 table_reads=4
flush at a switch: accesses=20 hits=0 walks=20 table_reads=40
numbered entries : accesses=20 hits=16 walks=4 table_reads=8Rule 3 at work: the first six rows. Each switch empties the TLB, so the third load misses, although program 0 had the page moments ago. Three walks.
Rule 4 at work: the next six rows. Program 1 misses on page 00404, although the TLB holds that page number: the ID is wrong. When program 0 returns, its entry is still there. Two walks.
The last two rows are ten turns. Each program fetches and loads once in each turn. With a flush, all 20 accesses miss, and the tables are read 40 times. With IDs, only the first use of each page misses: 4 walks.
Now rule 2. The operating system takes the W right away from a page that the TLB knows.
program 0 store 00404018 | TLB miss, walk, entry 0 filled | physical 06018
-- the table changes: page 00404 loses W
program 0 store 00404018 | TLB hit in entry 0 | physical 06018
-- the entry of page 00404 is flushed
program 0 store 00404018 | TLB miss, walk, entry 1 filled | page fault: W is 0, and this is a storeThe third row is a store that the table forbids. The TLB allows it, from its old copy. Only after the flush of that page does the walk find W at 0.
This lab leaves out how IDs are handed out, and the time that a flush takes.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Flush by software | A TLB that watches the tables | The TLB stays small and fast. Software must never forget a flush. |
| A flush of one page | Always the whole TLB | The other entries survive a small change. The TLB needs a compare for the flush. |
| An ID in each entry | A flush at every switch | No walks after a switch for pages that are still there. A few more bits to store and compare. |
| A small ID | An ID as wide as a program number | Few bits in every entry. IDs must be used again, with a flush. |
The Trap: A Switch with No Flush and No ID
This is the system from the top of the page. Its operating system changes the root table at a switch, and does nothing else. The TLB has no IDs.
program 0 load 00404010 | TLB miss, walk, entry 0 filled | physical 06010
-- switch to program 1
program 1 load 00404010 | TLB hit in entry 0 | physical 06010
program 1 store 00404010 | TLB hit in entry 0 | physical 06010The third row is the failure. Program 1 loads from its page 00404. The TLB hits, in the entry that program 0 left behind, and gives frame 06. That is the data of program 0.
The fourth row is worse. Program 1 stores into that frame. Program 0 will find its data changed, and no table ever allowed it.
Both tables were correct the whole time. The wall between the programs was broken by a copy. Rule 3 is the check: at every switch, either flush or change the current ID.
At Scale
This section looks ahead. Real systems use both tools, and add a few more.
Pages of everyone
The pages of the operating system look the same in every address space. Their entries can be marked to match any ID. Not checked.
Several cores
Each core has a TLB. When a table changes, every core that may hold a copy must flush. That takes a message between cores.
Guests and devices
A chip that runs several operating systems tags entries with a second number: I/O Memory Management and Virtualisation.
The cache has the same question
A cache that is looked up with virtual addresses can go stale in the same way. That is the next lesson.
Try It
Run the lab. Check the first twelve rows against the two hand tables.
Try restart(0, 0) before ten_slices, with quiet at 0. Which rows show a wrong frame?
Give each program a third page in ten_slices. With IDs, do the hits survive? The TLB has four entries.
program 0 load 00404010 | TLB miss, walk, entry 0 filled | physical 06010 -- switch to program 1, and flush the TLB program 1 load 00404010 | TLB miss, walk, entry 1 filled | physical 0c010 -- switch to program 0, and flush the TLB program 0 load 00404010 | TLB miss, walk, entry 2 filled | physical 06010 accesses=3 hits=0 walks=3 table_reads=6 program 0 load 00404010 | TLB miss, walk, entry 0 filled | physical 06010 -- switch to program 1 program 1 load 00404010 | TLB miss, walk, entry 1 filled | physical 0c010 -- switch to program 0 program 0 load 00404010 | TLB hit in entry 0 | physical 06010 accesses=3 hits=1 walks=2 table_reads=4 flush at a switch: accesses=20 hits=0 walks=20 table_reads=40 numbered entries : accesses=20 hits=16 walks=4 table_reads=8Check Yourself
Common Mistakes
Wrong Change a table entry and leave the TLB alone.
The TLB keeps using its old copy, with the old frame and the old rights.
Fix Flush the entries of that page right after the change.
Wrong Switch programs by changing the root table only.
The TLB still holds translations of the old program.
Fix Flush the TLB at the switch, or change the current ID.
Wrong Give a used ID to a new program without a flush.
Entries of the old owner would hit for the new one.
Fix Flush the entries of an ID before it gets a new owner.
Remember
- The TLB holds copies and cannot see the tables change.
- A changed entry needs a flush of its page. A switch of program needs a flush, or IDs.
- With an address-space ID in each entry, a hit needs the page number and the current ID.
- A flush is safe and costs walks. IDs save the walks and cost a few bits.