arthrop0d

Brief articles on varied topics.

TLB Shootdowns and Cross-Core Address Invalidation

operating systemsperformance

A TLB shootdown is the operating system's coordination protocol for removing stale virtual-to-physical address translations from other CPU cores. It is needed when a page-table change would make a translation cached by another core incorrect or unsafe.

The problem: translation caches are private

Programs use virtual addresses. Hardware translates each one to a physical address, the location in RAM, by consulting page tables maintained by the operating system. A page-table entry also records permissions such as readable, writable, executable, or accessible from user code.

Walking page tables for every memory access would be far too slow, so each CPU core keeps a small cache of recent translations called a translation lookaside buffer, or TLB. A TLB entry can contain both the physical address and the permissions for a virtual page.

For example, suppose a process's virtual page 0x4000 currently maps to physical page A:

virtual page 0x4000  ->  physical page A, readable and writable

The process runs on core 0, and core 0 stores this result in its TLB. The scheduler may then run another thread from the same process on core 1, which builds its own TLB entry for the same mapping.

Now the operating system unmaps 0x4000, changes it to read-only, or remaps it to physical page B. It updates the page-table entry in memory, but that update does not automatically erase the old entry from core 0's or core 1's private TLB.

This distinction matters: normal cache-coherence mechanisms make the page-table memory itself visible to other cores, but a TLB is a separate translation cache. A core can continue using its cached answer without reading the changed page table.

Changing a page table does not, by itself, make every core notice. The operating system must explicitly arrange the invalidation.

Without that arrangement, a core could keep writing through a mapping that was supposed to be read-only, access a page after it was unmapped, or access a physical page that has already been given to a different process. The last case can become a correctness bug or a security vulnerability.

How a shootdown works

The exact instructions differ between processor architectures, but the protocol usually follows this sequence:

  1. Change the page tables. The kernel updates the relevant page-table entries while holding whatever locks are needed to prevent conflicting changes.
  2. Make the update visible. The kernel uses the architecture's required ordering rules so that invalidation cannot be observed before the new page-table contents are properly written.
  3. Find the affected cores. The kernel tracks which CPUs are currently executing the address space. A CPU that is not using that address space may not need an immediate invalidation.
  4. Request remote invalidation. The kernel sends an interrupt or another inter-processor request to those CPUs. This request is often called an IPI, or inter-processor interrupt.
  5. Invalidate locally on each target CPU. The receiving CPU executes an architecture-specific operation that removes one virtual-page translation, a range of translations, or a whole address-space context from its TLB.
  6. Wait for completion. Each target reports that it has completed the invalidation. The initiating CPU does not treat the operation as finished until the required acknowledgements arrive.

The initiating CPU also invalidates its own TLB. An invalidation may target only the changed page, but a larger range or the entire address-space translation cache can be cheaper when many entries changed. Some processors provide identifiers for address spaces—called ASIDs or, on x86, commonly used with PCIDs—so entries belonging to different processes can coexist and be invalidated selectively.

On x86, kernel code can use instructions such as INVLPG for a page or INVPCID for selected address-space entries. Other architectures provide their own TLB-invalidation instructions, sometimes with hardware-supported broadcast behavior. Even when the hardware broadcasts an invalidation, the operating system still has to establish the right ordering and know when it is safe to continue.

Why waiting is necessary

Consider an unmap operation that removes a page and then immediately returns the page's physical memory to an allocator. If another core still has the old TLB entry, it could access that physical page after the allocator has assigned it to unrelated data.

Therefore, the kernel generally follows this rule:

Do not reuse or free memory whose old mapping might still be usable until every relevant CPU has passed the invalidation point.

The acknowledgement is that invalidation point. It does not merely mean that the interrupt was received; the remote CPU must have executed the required operation. Memory-ordering rules also prevent the CPU from acknowledging too early or continuing to use the old translation after acknowledging.

Similar care is required when changing permissions. If a writable mapping becomes read-only, a stale TLB entry could allow a write that should fault. If a user mapping is removed from a process, a stale entry could let user code reach memory that the new page tables no longer expose.

Why shootdowns cost performance

An IPI interrupts another core, disrupts its current work, and forces it to execute kernel code. The initiating CPU may then wait for several cores, so a page-table change can become a cross-core synchronization event rather than a local memory update.

This cost appears in systems that frequently create and destroy address spaces, change memory permissions, unmap large ranges, or modify mappings shared by many threads. It can show up in performance profiles or kernel logs as TLB flushes, remote TLB invalidations, or TLB shootdowns.

Operating systems reduce the cost with techniques such as batching many page-table changes into one invalidation, invalidating only affected ranges, and delaying work for CPUs that are not currently using the address space. They may also use generations or address-space identifiers so a CPU can detect that its cached entries are old when it next switches contexts.

The central idea remains simple: page tables describe the current mapping, while each core may be running from an older cached description. A TLB shootdown is the operating system's protocol for closing that gap before stale translations can cause incorrect access.

← All articles