[sched]kernel v7.2 · kernel/sched/● stable

Process Scheduler

CFS red-black tree, SCHED_DEADLINE EDF, run queues, load balancing, and preemption — the kernel scheduler dissected.

Subsystem Overview

The Linux scheduler is a modular, multi-class system. Each task belongs to a scheduling class — SCHED_DEADLINE, SCHED_FIFO/RR (real-time), or SCHED_NORMAL/BATCH/IDLE (fair) — checked in priority order on every scheduling decision. The Completely Fair Scheduler (CFS) handles the vast majority of tasks using a virtual runtime (vruntime) metric to approximate ideal CPU sharing.

CFS maintains a per-CPU red-black tree of runnable tasks keyed by vruntime. The leftmost node is always the next task to run. When a task runs, its vruntime advances proportionally to its weight (derived from nice value); lower-priority tasks accumulate vruntime faster, ensuring they are eventually preempted by higher-priority ones that have fallen behind.

Linux 7.2 extended SCHED_DEADLINE with group scheduling support, allowing deadline tasks to be organised into cgroups with aggregate bandwidth enforcement. The scheduler tick was also refactored to reduce timer overhead on tickless (NOHZ) kernels running latency-sensitive workloads.

// scheduling policies — sched_setscheduler() values

SCHED_NORMAL#0CFS default
SCHED_FIFO#1RT · no slice
SCHED_RR#2RT · timeslice
SCHED_BATCH#3CFS · no preempt
SCHED_DEADLINE#6EDF / CBS

Diagrams

// interactive · click nodes to explore

next4.2ms10422.1ms8876.8ms23011.0ms412leftmost3.4ms11985.1ms18768.9ms3044REDBLACKclick a node to inspect · values in ms vruntime

CFS Red-Black Tree

Per-CPU runqueue rb-tree keyed by vruntime. Leftmost node is always the next task to schedule.

HIGHESTLOWESTEDF / CBSSCHED_DEADLINEEarliest Deadline First — highest priority. Guarante…fallthroughReal-TimeSCHED_FIFO / RRFixed-priority real-time. FIFO runs until blocked; R…fallthroughCFSSCHED_NORMAL / BATCHCompletely Fair Scheduler. vruntime-based fairness v…fallthroughIdleSCHED_IDLERuns only when no other task is runnable. Used for b…click a class to highlight · checked in order top → bottom

Scheduling Class Chain

Priority-ordered class vtable: DEADLINE → RT → CFS → IDLE. Each class is checked in turn.

NUMA Domaincross-node migration · high costMC Domain — Node 0intra-socket · L3 sharedMC Domain — Node 1intra-socket · L3 sharedCPU 0HT pairCPU 1HT pairCPU 2HT pairCPU 3HT pairCPU 4HT pairCPU 5HT pairCPU 6HT pairCPU 7HT pairmigrateload_balance() · kernel/sched/fair.cclick a domain to highlight · dashed arrow = cross-node migration

Load Balance Domains

NUMA → MC → SMT domain hierarchy. load_balance() migrates tasks from busiest to idlest group.

Key Concepts

vruntime

Virtual runtime — the CFS fairness metric. Tracks how much CPU time a task has consumed, weighted by its priority. A task with nice -20 accumulates vruntime ~10× slower than a nice +19 task. The scheduler always picks the task with the smallest vruntime from the red-black tree.

sched_entity / sched_class

sched_entity embeds the vruntime, load weight, and rb_node for CFS. sched_class is a vtable of function pointers (enqueue_task, dequeue_task, pick_next_task, etc.) that each scheduling policy implements. Classes are checked in priority order: deadline → rt → fair → idle.

Run Queue (rq)

Per-CPU data structure (struct rq) holding the CFS rb-tree, RT priority array, deadline rb-tree, current task pointer, load statistics, and the scheduler clock. All scheduling decisions are made with the rq lock held.

SCHED_DEADLINE

Earliest Deadline First (EDF) real-time class. Each task declares a runtime budget and a period; the kernel guarantees the budget is available within each period. Uses CBS (Constant Bandwidth Server) to prevent overrun. In 7.2, extended to support cgroup-level bandwidth aggregation.

Load Balancing

Periodic and idle-triggered migration of tasks between CPUs to equalise load. The scheduler builds a domain hierarchy (SMT → MC → NUMA) and migrates tasks from the busiest group to the idlest. Controlled by sched_domain flags and load_balance() in kernel/sched/fair.c.

Preemption & Scheduler Tick

The scheduler tick (triggered by the timer interrupt at CONFIG_HZ) updates vruntime, checks if the current task should be preempted (TIF_NEED_RESCHED), and drives load balancing. NOHZ_FULL suppresses the tick on CPUs running a single task, reducing OS jitter for HPC and RT workloads.

Source References

// kernel/sched/ · annotated entry points

kernel/sched/core.cschedule(), __schedule(), context_switch(), try_to_wake_up() — core scheduling logicelixir ↗
kernel/sched/fair.cCFS: enqueue_task_fair(), dequeue_task_fair(), pick_next_task_fair(), load_balance()elixir ↗
kernel/sched/deadline.cSCHED_DEADLINE: enqueue_task_dl(), CBS bandwidth enforcement, group scheduling (7.2)elixir ↗
kernel/sched/rt.cSCHED_FIFO / SCHED_RR: priority arrays, RT throttling, push/pull migrationelixir ↗
kernel/sched/sched.hCore data structures: struct rq, struct sched_entity, struct sched_class, struct cfs_rqelixir ↗
kernel/sched/topology.csched_domain hierarchy construction: SMT, MC, NUMA topology detection and flagselixir ↗
include/linux/sched.hstruct task_struct: sched_class pointer, sched_entity, prio, static_prio, policy fieldselixir ↗

Linux 7.2 Changes

// recent kernel/sched/ commits

sched/deadline: add cgroup-level bandwidth aggregation for SCHED_DEADLINE task groups
sched/nohz: reduce tick overhead on NOHZ_FULL CPUs by deferring load-avg decay to idle entry
sched/fair: improve NUMA balancing heuristics for CXL-attached memory tiers