Context Switching Internals
Process Management & Scheduling · OS Core Internals track
5 min read
In this module
What Is a Context Switch?
When the kernel decides to stop running one process and start running another, it has to save the entire execution state of the outgoing process and restore the state of the incoming one. That operation is a context switch, and it sits on one of the kernel's hottest performance paths.
Every time you switch between applications, every time a process blocks on I/O and another takes its turn, a context switch happens underneath.
What Gets Saved
The "context" of a running process is everything needed to pick it up later as if nothing had happened. On x86-64, that means:
- General-purpose registers - all 16 of them: RAX, RBX, RCX, RDX, RSI, RDI, RBP, RSP, R8 through R15
- RIP - the instruction pointer, so the process resumes at the correct instruction
- RFLAGS - the processor status flags (carry, zero, overflow, etc.)
- CR3 - the page table base register, which defines the process's virtual address space
- FPU/SSE state - floating-point and SIMD register contents
A context switch saves the complete CPU state of the outgoing process (registers, instruction pointer, flags, page table pointer, floating-point state) and then restores the incoming process's state. The CPU picks up exactly where it left off, with no clue it was ever interrupted.
The Context Switch Path
When the scheduler selects a new process to run, the kernel follows a well-defined sequence. This is the Linux x86-64 path, slightly simplified:
Two details matter here. The user-mode registers aren't copied into task_struct at switch time: they were pushed onto the process's kernel stack when it entered the kernel, and they stay there. task_struct (in thread.sp) mostly records where that kernel stack pointer was left. And in Linux the address space switch (switch_mm) happens before the register and stack switch (switch_to); a switch between threads of the same process skips the CR3 load entirely.
The CR3 load is the heavyweight step. It swaps the entire virtual memory mapping, so the new process sees its own address space rather than the old one's.
The Cost
Context switches aren't free. A single switch typically costs between 1 and 5 microseconds, depending on hardware and how much state has to be saved. Sounds tiny, until you remember a busy server might rack up tens of thousands of context switches per second.
The direct cost (saving and restoring registers) is only part of the story. The indirect costs often hurt more:
- Cache pollution - the new process's working set isn't in the CPU cache, triggering a flurry of cache misses
- TLB flush - switching CR3 invalidates TLB entries for the old address space, so the new process starts with an empty TLB and every memory access kicks off a page table walk
- Branch predictor - the CPU's branch prediction tables are trained for the old process's code patterns
These indirect costs can dwarf the direct register save/restore, particularly for processes with hefty working sets.
Thread Switch vs Process Switch
Not all context switches are created equal. Switching between threads within the same process is cheaper than switching between processes because threads share the address space.
When switching threads:
- CR3 stays the same (same address space)
- No TLB flush is needed
- The cache and branch predictor often remain useful, since threads tend to share code
When switching processes:
- CR3 must change
- TLB entries for the old address space are invalidated
- Cache contents from the old process are largely worthless
This cost gap is one reason multi-threaded designs win out for concurrent work inside a single application.
TLB Flush and Performance
The TLB (Translation Lookaside Buffer) caches virtual-to-physical address translations. On a process switch, the old process's TLB entries are useless to the new process because the same virtual addresses now point at different physical pages.
Historically, the kernel flushed the entire TLB on every process switch. Modern processors support Process Context Identifiers (PCIDs), which tag TLB entries with a process ID. The TLB can keep entries from multiple processes simultaneously, dodging a full flush and slashing the indirect cost of a context switch.
Meltdown mitigations raised the stakes. With kernel page-table isolation (KPTI), each process has two sets of page tables, one for user mode and one for the kernel, and the CPU switches CR3 on every kernel entry and exit, not just on a context switch. PCIDs are what keep that affordable; on CPUs without them, syscall-heavy and switch-heavy workloads got noticeably slower.
# Monitor context switches on Linuxvmstat 1# The 'cs' column shows context switches per second
# Per-process context switchesgrep ctxt /proc/<pid>/status# voluntary_ctxt_switches: - process blocked# nonvoluntary_ctxt_switches: - preempted by schedulerWhy It Matters
Context switch cost is a major consideration in system design. Excessive context switching, caused by too many threads, overly short scheduling quanta, or lock contention, drags performance down hard. Profiling tools that report high context switch rates are pointing at contention problems. For exploit developers, the details above are practical. Kernel exploits that hijack control flow have to return to user mode cleanly, which means restoring the saved user registers on the kernel stack and, with KPTI, switching CR3 back to the user page tables (the "KPTI trampoline" step in many public exploits). Get that wrong and the exploit crashes the machine instead of giving you a shell.