Kernel Switch Cost: TLB & CPU State Overhead

Kernel context switches incur overhead from saving/restoring CPU registers, cache invalidation, and Translation Lookaside Buffer (TLB) flushes, contributing to microsecond latencies.

The top 3

  1. TLB Flushes: Hundreds of Cycles Lost per Context Switch: During a kernel context switch between processes, the Translation Lookaside Buffer (TLB) often requires flushing, which can cause each subsequent TLB miss to incur a penalty of hundreds of CPU cycles. While modern CPUs use Address Space IDs (ASID) or Process Context IDs (PCID) to mitigate full flushes, the cost of invalidating and reloading these vital memory translation caches remains a significant overhead.
  2. CPU Register Save: AVX-512 State Swaps Cost Kilobytes: Saving and restoring the CPU's architectural state, particularly large register sets like AVX-512, represents a direct and significant cost during a kernel context switch. This process involves copying over 2 KiB of data for AVX-512 states to and from memory (Process Control Block), consuming thousands of CPU cycles for each switch.
  3. Cache Pollution: The Costlier Indirect Context Switch Penalty: Beyond direct register saves and TLB flushes, the most expensive aspect of a kernel context switch is often the indirect cost of cache pollution. When a new process takes over, it frequently evicts the previous process's data from CPU caches (L1/L2), leading to a 'cold cache' effect that can cost thousands of cycles as the resumed process must refetch data from slower memory.

Sources

Open the full topic