Linux Internals

The TLB Explained: How Linux Translates Virtual Addresses Fast

By NileVPS Team · Published · 5 min read

اقرأ المقال بالعربي

Every pointer your program dereferences is a virtual address. The CPU cannot fetch data with it directly; it first has to turn that virtual address into a physical location in RAM. If the processor did this the slow way on every memory access, even simple code would crawl. The component that keeps it fast is the Translation Lookaside Buffer, or TLB. Understanding it explains why huge pages help databases, why some workloads slow down inside virtual machines, and what "TLB shootdowns" are.

Virtual memory in one paragraph

Linux gives each process its own virtual address space. Memory is managed in pages, 4 KB by default on x86-64. The kernel keeps a page table per process that maps virtual pages to physical frames, along with permission bits (readable, writable, executable, user or kernel). On x86-64 the page table is a tree with four levels, or five on CPUs and kernels that enable 5-level paging. Resolving one address means reading one entry at each level, which is called a page table walk.

What the TLB does

A page table walk can cost several dependent memory reads. The TLB is a small, very fast cache inside the CPU core that stores recent translations: "virtual page X maps to physical frame Y, with these permissions". When the CPU needs a translation:

  1. It checks the TLB. On a hit, the physical address is available almost immediately.
  2. On a miss, the hardware page walker reads the page tables (helped by the normal data caches and dedicated paging-structure caches).
  3. The result is stored in the TLB for next time.
  4. If the walk finds no valid mapping, the CPU raises a page fault and the kernel takes over.

Modern cores have separate first-level TLBs for instructions and data, plus a larger second-level TLB, totalling hundreds to a few thousand entries depending on the CPU.

TLB reach: why page size matters

TLB reach is the amount of memory the TLB can map at once: number of entries multiplied by page size.

Page size1,500 entries coverTypical use
4 KBabout 6 MBdefault for all processes
2 MBabout 3 GBtransparent or explicit huge pages
1 GBabout 1.5 TBexplicit huge pages for very large heaps

A database with a 16 GB buffer pool, a JVM with a large heap, or a Redis instance holding millions of keys touches far more than 6 MB of memory in random order. With 4 KB pages, many accesses miss the TLB and trigger a walk. Larger pages reduce misses and also shrink the page tables themselves. We cover the two Linux mechanisms for this in HugeTLB vs Transparent Huge Pages.

Context switches, PCID and flushes

Because each process has its own page tables, a translation cached for one process is wrong for another. Older CPUs had to flush the TLB on every switch between processes. Modern x86 CPUs support PCID (Process Context Identifiers), which tags entries with an address-space ID so entries from several processes can coexist. Linux uses PCID when available, and it became especially important after the Meltdown mitigation (page table isolation) separated user and kernel page tables. You can check for support with:

grep -o -w pcid /proc/cpuinfo | head -1

TLB shootdowns

When the kernel changes a mapping, for example on munmap(), mprotect(), page migration or memory compaction, every CPU that might hold the old translation must invalidate it. The kernel sends inter-processor interrupts to those CPUs, and they flush the affected entries. This is a TLB shootdown. In a process with many threads spread across many cores, frequent unmapping, such as a memory allocator that constantly returns memory to the OS, can generate a steady stream of these interrupts. You can see the counter per CPU:

grep TLB /proc/interrupts

A rapidly rising counter is a hint to look at allocator settings or how often the application unmaps memory.

TLB misses inside a VPS

In a virtual machine, translation has two stages: the guest's page tables map guest-virtual to guest-physical addresses, and the hypervisor's tables (Intel EPT or AMD NPT) map guest-physical to host-physical. A TLB miss inside a guest may therefore need a much longer two-dimensional walk. Hardware caches most of this efficiently, but memory-heavy workloads with random access feel it more, and huge pages help even more in a VM than on bare metal.

Measuring TLB behavior

On many virtual machines hardware performance counters are not exposed, so perf may report events as not supported. Where they are available, try:

sudo perf stat -e dTLB-loads,dTLB-load-misses,iTLB-load-misses -- ./your_app

A practical checklist for a memory-heavy server:

  • Check the transparent huge page mode: cat /sys/kernel/mm/transparent_hugepage/enabled.
  • See how much memory is backed by huge pages: grep AnonHugePages /proc/meminfo.
  • Follow your database vendor's guidance on huge pages; some recommend explicit huge pages, others recommend disabling THP.
  • Watch /proc/interrupts for shootdown storms.
  • Benchmark before and after any change with your real workload.

If you want a sandbox to experiment with these settings safely, you can deploy a Linux VPS and take a snapshot first so any change can be rolled back.

FAQ

Can I change the size of the TLB?

No. The TLB is fixed hardware inside the CPU. What you can change is how much memory each entry covers, by using huge pages, and how often entries are invalidated, by reducing unnecessary mapping changes.

Does a TLB miss always mean a page fault?

No. A TLB miss only means the translation was not cached, so the CPU walks the page tables. A page fault happens only when that walk finds no valid mapping or the access violates permissions, and then the kernel must handle it.

Will huge pages always make my application faster?

Not always. They help workloads that touch large amounts of memory randomly. For small processes there is little gain, and transparent huge pages can add latency spikes from compaction, which is why some databases recommend turning them off.

Related guides