This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com
UPDATE: In the video I talk about the ASID table in the kernel. I incorrectly say that the table is 4095 entries long. On X86, the ASID table in the kernel is of size 6. Rest of the details as explained are correct.
The Translation Lookaside Buffer (TLB) is a CPU cache that stores virtual-to-physical page translation mappings. TLB misses are among the most significant performance bottlenecks in modern systems, leading developers to carefully optimize memory layouts to maximize TLB hit rates.
But what happens during process context switches? A common concern is that the Linux kernel might flush the TLB, potentially wiping away all the performance benefits of those optimizations. This video dives deep into the Linux kernel's TLB management during context switches, revealing the true impact on system performance and debunking common misconceptions.
We'll examine actual kernel data structures and mechanisms to understand when and why TLB flushes occur, and explore how modern hardware features help mitigate their impact.
Quick tip: As a non-native English speaker, I speak at a measured pace. Feel free to watch at 2x speed for a more natural viewing experience!
The Linux Context Switching Internals Book
As you might be aware that I am also releasing a series of articles on Linux Context Switching Internals in the form of a short ebook which is currently at an early-access discount at the below link.
As a thank you, I am offering the book at a 50% discount to monthly subscribers, and 100% discount to annual subscribers. Subscribe now and I will send you a discount link:
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
SPEAKER_00Hey everyone, this is Abhinav from Confessions of a Code Addict. In this video, we are going to be talking about TLB flashes and context switching. So if you are a regular reader of my blog, I'm sure you must be aware of the importance of TLB hits. So in order to build a high performance system, it is important that we minimize the number of TLB misses. But it doesn't matter the amount of effort that we put into
minimizing the number
SPEAKER_00of TLB misses, but if the kernel does a TLB flush during a context switch, then all of that effort goes wasted because once that process, that TLB state is flushed, then we have to spend hundreds of cycles restoring all of that state before the process comes back to its previous levels. So, it is important that we understand when and how the TLB is flushed during your context switch. Many people believe that the TLB is always flushed during your
context switch but that is not really the case on modern hardware and on a modern kernel such as Linux. so things are more complicated than that so in this video we are going to be decoding when and how TLB flash happens between a context switch when it doesn't really happen what are the scenarios so let's dive in so first of all let's start with some background so TLB is stands for translation look-aside buffer it is a
SPEAKER_00small piece of hardware cache within each CPU core It contains or caches the mappings of virtual to physical pages for each process. As you might be aware, the address space of a process is arranged in the form of virtual pages. Each page is of fixed size and that page is mapped to a physical page frame of the same size. The physical page frame is continuously allocated.
And in order to resolve a virtual address in the address space of a given process,
SPEAKER_00the CPU has to or the hardware has to do a page table walk. So the page table is a data structure which is maintained by the kernel per process. Every process has its own page table. And when a specific process is running on the CPU, then the CPU is only aware of that process as page table. So the way the page table walk happens is it starts from the root of the page table which is called the page directory and then the hardware has to
go through all the levels of the page table until it reaches the final level from where it finds the
SPEAKER_00actual mapping of the physical page frame. Because these page tables are multilevel, so the hardware has to do multiple lookups in the main memory until it resolves the address. So overall, this whole process of the page table walk is quite expensive. In the worst case, it can take hundreds of cycles. So in order to hide all of this latency, the hardware utilizes the TLB cache. So it caches all these translations in the TLB.
SPEAKER_00But what happens during your context switch? So when the context switch happens, the kernel switches the address spaces of the processes as well. And as part of that the page table is switched. So now after a context switch, the CPU is aware of the new processes page table. So when the CPU or the hardware tries to resolve the addresses of the new process, it will be referring to these space table mappings, which means that whatever is cached in the TLB currently
belongs to the previous
SPEAKER_00process and it is not valid for the new process. So in the absence of any other mechanism, the CPU does not really know, cannot distinguish whatever is cached in the TLB, whether it belongs to this process or not. As a result, it is important that the kernel does a TLB flush during a context switch so that this process does not access the previous process's translate memory. But to improve performance, it is important that we do not unnecessarily do a TLB flush
during context switches. so in the recent years there has been some hardware support which has come up in some of the modern processors which has allowed the kernel to not do a TLB flash
SPEAKER_00during a context switch so this hardware feature is called
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we learned about copy-on-write (CoW) and understood its mechanics by digging inside the mmap pathway in the...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the last video, we discussed demand paging. Now, let’s move towards an even more interesting topic: copy-on-write (CoW). Just...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com In the previous video in our virtual memory series, we discussed demand paging. But that was mostly theoretical, explaining how...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com Welcome back to the video series on virtual memory based on my article on the same topic. It is also available in ebook format on...
This is a free preview of a paid episode. To hear more, visit blog.codingconfessions.com This is a short aside in our virtual memory series. We have talked extensively about the size of a virtual address and the virtual...