内核中cpu_idle()的作用是什么?为何作为start_kernel的最终调用?
cpu_idle caps off the start_kernel flow, and what it actually does Great question—this gets to the core of how the Linux kernel transitions from a boot-time initialization sequence to a fully operational system. Let’s break it down:
First, why rest_init is start_kernel’s final call
start_kernel is the kernel’s main initialization hub: it sets up memory management, the process scheduler, interrupt handling, device drivers, and every other critical subsystem. By the time it finishes, all the infrastructure needed to run processes is in place—but the system isn’t yet running processes.
That’s where rest_init comes in. It handles the last critical handoff to runtime:
- It spawns the first user-space process (
init, PID 1), which launches the entire userspace stack (systemd, init scripts, etc.). - It creates
kthreadd(PID 2), the kernel’s thread manager that oversees all other kernel thread creation and management. - It triggers the first execution of the process scheduler with
schedule(), allowing the kernel to start switching between the new processes.
After these steps, the original thread that ran start_kernel has no more initialization work left. It’s repurposed as the CPU idle thread—and that’s why cpu_idle is the final call in rest_init.
What cpu_idle actually does
cpu_idle is the infinite loop that powers the idle thread, running whenever the CPU has no other tasks to execute. Its primary jobs are:
- Power efficiency: It puts the CPU into architecture-specific low-power states (like Intel’s C-states or ARM’s
WFI/WFEinstructions). These states shut down unused CPU components to reduce power draw, only waking up when an interrupt (timer, device event, etc.) fires. - Wait-for-work loop: When no processes are ready to run,
cpu_idletells the CPU to wait for an interrupt. Once triggered, the kernel checks for runnable processes—if found, the scheduler switches to that process; if not, it loops back tocpu_idle. - Lightweight housekeeping: On some architectures, it may handle minor idle-time tasks (like cache flushes or statistic updates), but this is secondary to power saving.
In plain terms: once the kernel is fully initialized, there’s no more setup work for the original boot thread. Instead, it becomes the "fallback" thread that runs when nothing else is happening, keeping the CPU in a low-power state until work arrives.
内容的提问来源于stack exchange,提问作者T.Meyer

