← Back to all chapters

Chapter 14Down the Rabbit Hole: How Linux Decides Who Runs Next

6 min read
rabbit-holelinuxoperating-systemsscheduling

If you run Ubuntu 24.04, Debian 13, a recent Fedora or Arch, or an Android kernel on 6.6 or later, every ordinary thread is scheduled by an algorithm published in 1995. Linux 6.6 (October 2023) replaced the core of CFS with EEVDF, Earliest Eligible Virtual Deadline First, from Stoica and Abdel-Wahab.

What pulled me in was a line in Jonathan Corbet's LWN coverage: the switch let Peter Zijlstra delete around 450 lines of heuristics. A scheduler change that made the code smaller while giving users a new knob. What idea does that?

What CFS couldn't say#

CFS (2007) gave every task a vruntime: CPU time used, scaled by weight (nice 0 is weight 1024, each nice step about 1.25x). Run whoever has the smallest. Everyone converges on their fair share.

Fair, but it can't express latency. A task wanting 100-microsecond turns looks the same as one wanting 10 ms. The only lever was how a waking task got placed near the minimum vruntime, steered by tunables with mixed units: sched_latency_ns, sched_min_granularity_ns, sched_wakeup_granularity_ns, sleeper bonuses. Fix a server regression and you'd break a desktop.

The picture: one barista, a line of customers. EEVDF gives two honest rules:

  • Fairness: keep a ledger of who is owed time. Only serve people owed time (or exactly square).
  • Urgency: each customer says how long a turn they want. Shorter turns get an earlier finish-by time. Among those allowed, serve the earliest.

Two dials. Weight sets how much CPU. Slice sets when.

Lag, eligibility, deadline#

Lag is the service a task would have got from an ideal fluid scheduler (every task gets its weighted share at every instant) minus what it actually got. Positive: the CPU owes you. Negative: you've overdrawn. In virtual time:

V       = Σ w_j · v_j / Σ w_j        weighted mean of everyone's vruntime
vlag_i  = V − v_i                    virtual lag
lag_i   = w_i · (V − v_i)            real lag
 
eligible:  lag_i ≥ 0   ⇔   v_i ≤ V
deadline:  vd_i = v_i + slice_i · 1024 / w_i

You're eligible if you're at or behind the average. Your deadline is where you are plus your slice in virtual time; heavier tasks burn virtual time slower, so the same slice is a shorter distance. Because V is a weighted mean, all lags on the runqueue sum to zero. The ledger balances.

Watching it run#

The dive's small exact-arithmetic simulator mirrors kernel/sched/fair.c. Three equal-weight CPU-bound tasks: A and B ask for 3 ms slices, C asks for 1 ms. A star marks eligible.

1/5t=0
V = 0.00
A v=0.00 vd=3.00 *
B v=0.00 vd=3.00 *
C v=0.00 vd=1.00 *   <- earliest
run: C

Everyone is square. C's deadline (1) beats A's and B's (3).

First 4 ms of the trace. Values are the simulator's.
0123virtual timeAvd=3Bvd=3Cvd=2V = 0.33C is ahead of V: ineligible, despite the earliest deadlineA and B are eligible and tie on deadline: A runs
Snapshot at t=1 from the trace above. A dot is a task’s vruntime v, the bar runs to its virtual deadline vd; only tasks at or left of V may run.

Change the weights instead (2048 vs 1024, equal slices) and you get a clean 2:1 split, since the heavy task's vruntime advances at half speed. Remove the eligibility check and C monopolises the CPU: a stream of tiny-slice tasks starves everyone. That's why the "E" is in the name.

Finding the task in O(log n)#

The kernel keeps runnable tasks in a red-black tree keyed by deadline, each node augmented with the minimum vruntime in its subtree. Usually the leftmost task is eligible and you're done. Otherwise walk down: if the left subtree has any eligible task, go left (earlier deadlines); else check this node, then go right. Subtrees with nobody eligible are pruned.

V = 3.5tree key: deadline dd=5 v=3min v=3d=3 v=6min v=5d=8 v=1min v=1d=2 v=5min v=5pickedfirst eligible nodeleftmost: earliest deadline,but v=5 > V, not eligiblenever visitedskip: min v=5 > V
How the pick walks the tree. Node values are illustrative, not from a real run.
pick_eevdf() from fair.c
/* Pick the leftmost entity if it's eligible */
if (se && entity_eligible(cfs_rq, se)) { best = se; goto found; }
/* Heap search for the EEVD entity */
while (node) {
	struct rb_node *left = node->rb_left;
	if (left && vruntime_eligible(cfs_rq, __node_2_se(left)->min_vruntime)) {
		node = left; continue;
	}
	se = __node_2_se(node);
	if (entity_eligible(cfs_rq, se)) { best = se; break; }
	node = node->rb_right;
}

The eligibility test never computes V. It cross-multiplies, comparing Σ w_j (v_j − v₀) against (v_i − v₀) · Σ w_j, because the fair.c comment says the divided form is inaccurate. Sums are relative to a moving zero point and products use 128-bit arithmetic. Hold that thought.

The hard part is sleep#

My bank-ledger model breaks in one place: the ledger is relative. V is the mean of whoever is queued, so your balance changes when others walk in or out. Sleeping, waking and reweighting get subtle here, and that's where most real bugs live.

A sleeping task's virtual lag is saved, and place_entity() restores it on wake. But re-adding the task shifts V, because it joins the average:

V'    = (W·V + w_i·(V − vl_i)) / (W + w_i)  =  V − w_i·vl_i / (W + w_i)
vl'_i = V' − v_i                            =  vl_i · W / (W + w_i)

A creditor shrinks their own credit just by arriving. So the kernel inflates the saved lag first by (W + w_i) / W; in code, lag *= load + weight; lag /= load.

Then gaming. If sleep forgot lag, a task could overspend, nap, and return square. Linux 6.12 added DELAY_DEQUEUE: a task sleeping with negative lag stays on the tree, marked delayed and ineligible, burning off its debt. It leaves only when picked (lag non-negative by then) or woken. None of this is in the 1995 paper. The kernel docs still say: "There are ongoing discussions on how to manage lag, especially for sleeping tasks."

When the arithmetic bites#

CVE-2024-35985 is what happens when one path forgets the clamp. reweight_eevdf(), which rescales lag on a weight change, computed virtual lag without it. Weights are 20-bit fixed point; a signed 64-bit multiply overflowed, entity_eligible() returned false for every task, pick_eevdf() returned NULL, and the kernel dereferenced it. "Rare, but fatal when it does happen." Fixed in 6.9-rc6 and backported.

Theory and engineering meet at one point: the theorem says lag is bounded, and the code must keep that true in finite integers on every path, including a cgroup's weight changing while members are queued.

The 6.6 merge had rough edges too: wakeups with slightly earlier deadlines preempted constantly and context switches exploded. RUN_TO_PARITY curbs it by protecting the current task until its lag hits zero or it uses its minimum slice.

Two misreadings to avoid:

  • A short slice does not get you more CPU, only smaller, earlier pieces of the same share (plus more context switches).
  • EEVDF is not real-time. Deadlines are virtual, relative to a fair-share reference. For wall-clock guarantees use SCHED_DEADLINE.

The knob, and what's still open#

Since Linux 6.12 a thread can ask for its own slice without touching its weight:

struct sched_attr a = {
    .size = sizeof(a),
    .sched_policy = SCHED_OTHER,
    .sched_nice = 0,
    .sched_runtime = 500 * 1000,   /* ns; kernel clamps to [100us, 100ms] */
};
syscall(SYS_sched_setattr, 0, &a, 0);

The default slice (checked in fair.c) is 0.7 ms times 1 + ilog2(min(ncpu, 8)): 0.7, 1.4, 2.1 or 2.8 ms for 1, 2, 4 or 8+ CPUs.

  • 1 CPU0.7 ms
  • 2 CPUs1.4 ms
  • 4 CPUs2.1 ms
  • 8+ CPUs2.8 ms
Default slice by CPU count (0.7 ms times 1 + ilog2(min(ncpu, 8))).

The paper's cleanest guarantee: for request size c and quantum q, lag stays strictly between −c and max(c_max, q), and every request completes within q of its virtual deadline. Nobody gets more than about one slice ahead or behind the ideal.

But that's per runqueue. Fairness across CPUs is the load balancer's job. It predates EEVDF, equalises load rather than lag, and runs on tens to 100+ ms timescales, far longer than a slice. Nobody has shown per-CPU lag bounds compose into a global bound under a periodic balancer.

The interface is the other open question. Slice is a proxy; what services want is "my p99 should be under 5 ms", which you can't say without changing weight or going real-time. Meanwhile the action moved up to sched_ext (merged in 6.12), where Valve's and Igalia's scx_lavd reimplement a latency-aware virtual deadline in BPF, and research like SchedCP has LLM agents writing sched_ext policies. The BPF verifier proves memory safety, not fairness or freedom from starvation. EEVDF's appeal was one theory-backed rule in place of many guesses. I'm curious whether the next generation keeps that.

Sources#

© 2026 Bharat Kaurav. Built with Next.js & Tailwind.

>_ bk@netherwatcheresc to close

Welcome, reader. Type help to see what this shell can do.