2026-07-25
Some kernel updates can't be done incrementally. Patching a function that every CPU might be executing right now, swapping a data structure that has no RCU-safe pointer, or updating CPU microcode — all require a moment where nothing else is running. That moment is what stop_machine() creates.
The mechanism. Each CPU has a dedicated high-priority kernel thread called migration/N (visible in ps). These threads sit at SCHED_FIFO priority 99 — higher than any user task, higher than nearly any kernel thread. When you call stop_machine(fn, data, cpumask), the kernel wakes up the migration thread on each targeted CPU. Because of their priority, they preempt whatever is running — kernel or user, RT or normal — almost immediately.
Each thread then spins on a shared atomic counter waiting for all its peers to arrive. Only when every CPU has checked in does one designated CPU execute fn(data). The others sit in the spin loop with interrupts disabled. When fn returns, they all release and resume normal scheduling.
Real-world uses:
stop_machine() makes that trivial.stop_machine() ensures no CPU is mid-decode when the bytes flip.The cost. Rule of thumb: stop_machine() takes roughly 10–100 microseconds per CPU in the best case, but scales poorly. On a 128-core server, the barrier synchronization alone can push a single invocation past 1 millisecond. Every CPU is idle during that time — that's 128 core-milliseconds of throughput vaporized per call. Facebook and Google engineers have documented cases where frequent stop_machine() calls (e.g. from repeatedly toggling static keys or ftrace) caused visible tail-latency spikes in production services.
Escaping it. Modern kernels work hard to avoid stop_machine(). Static keys use text_poke_bp (patching via an INT3 trap so other CPUs stumble harmlessly through the transition). RCU replaces the "wait for everyone" pattern with "wait for everyone to voluntarily reach a quiescent state." Both trade complexity for the ability to update global state without stopping the world.
stop_machine() is Linux's global mutex on the entire machine — cheap to invoke, brutally expensive to run, and every modern kernel subsystem is engineered to avoid needing it.
