nos_
ENRU
Вся документация

Как это устроено

Планировщик

docs/scheduler.mdОткрыть на GitHub

Эта страница пока есть только на английском: документация собирается прямо из каталога docs/ репозитория и поэтому всегда совпадает с кодом. На русском у нас есть руководства.

Preemptive, over per-CPU run queues, by EEVDF -- earliest eligible virtual deadline first, Linux's fair class since 6.6 -- with a weight from each task's nice, a slice of its own, and no user space. Everything in this kernel that is not an interrupt handler runs as a Task: the shell, the softirq workers, the USB poll loop, the DHCP client, a guest's vCPUs, and each CPU's idle loop. The code is kernel/sched.cpp, kernel/eevdf.cpp, kernel/task.cpp and kernel/preempt.cpp, with the context switch itself in arch/x86_64/asm.asm and arch/arm64/asm.S.

The model

One TaskQueue per CPU, owned by that CPU's Cpu object, under one spinlock. It is home to every task that runs on that CPU, and keeps them in the places a decision needs, so that a decision never passes over a task that cannot run:

  • The run list -- the tasks that can run, with what EEVDF knows of each (below), and the one on the CPU as the queue's current. Only those: a task that blocks is off it until it is woken.
  • The sleep list -- the tasks asleep until a deadline (Sleep(), Event::wait_for()), soonest first: what the CPU's timer is armed for, and what each decision looks at the head of.
  • The idle task's place -- apart from the run list, taken only when nothing else can run. It used to sit in the list as an equal, which meant a reschedule -- and every softirq raised from an interrupt handler forces one -- could pick it over the task that had just been given work. It then halted the CPU, and the work waited for whatever interrupt came next.
  • The wake stack -- the tasks woken since the last decision, pushed by their wakers from any CPU and any context without a lock (below).
  • And a list of every task the queue is home to, asleep or not, which only its bookkeeping walks.

schedule() takes the wake stack and places what was on it on the run list by the lag each brings, does the same for the sleepers whose time has come, and switches to the eligible task whose deadline is soonest; the task it displaces, if it can still run, goes back on the list, where its virtual runtime and deadline say. With nothing on the run list the CPU keeps a task that can still run when an interrupt asked (Preempt()) or it is polling (yield_to_runnable()), and otherwise goes to the idle task -- for a voluntary schedule(), and for a task that has just blocked.

Before, the queue was one list of every task, blocked and asleep ones included, and each decision walked it from the head, past every task that could not run, the idle task and any task whose preemption count was up -- and once more after the timer fired, for the soonest sleeper. A decision walks only the run list now, which holds nothing that cannot run, and the head of the sleep list; the turns were a round robin until EEVDF took them.

Tasks move between queues only when the load balancer says so, and only while they can run.

Who runs next: EEVDF

Each task is owed a share of its CPU in proportion to its weight, and virtual time is what it has had of it (kernel/eevdf.h):

  • Weight, from the task's nice, -20 to 19: 1024 at nice 0, each step about 1.25 times the next -- Linux's sched_prio_to_weight, so that one nice apart is about ten per cent of the CPU between two tasks that both want all of it. A nanosecond on the CPU advances a task's virtual runtime by 1024/weight virtual nanoseconds, a heavier task's slower.
  • V, the weighted average of the virtual runtime of every task on the queue -- the run list's and the current one's: where a task would be had it had exactly its share. Its lag, V less its virtual runtime, is what it is owed, and it is eligible while that is not negative.
  • Slice, how long it asks to run at a time -- 3 ms unless set, 100 us to 100 ms -- and its virtual deadline, where its virtual runtime will be when it has had the slice. The weight decides how much of the CPU a task gets, and the slice how soon after it is eligible it gets it: a short slice is an early deadline, latency without a larger share.

The next to run is the eligible task whose deadline is soonest. On an interrupt -- the tick, a slice's end, the IPI a wakeup sends (Preempt()) -- the task on the CPU is one of the candidates and keeps the CPU unless another eligible one's deadline is sooner: its slice done, or a wakeup owed more. A task that blocks or yields lets the rest go first, and one that yields has its deadline put at least a slice past V -- bounded, so that a task yielding without end does not push it without end. While another task waits on the list, the CPU's timer is armed for the end of the running task's slice too (below), so the next decision comes when its deadline does and not at the next tick; alone on its CPU, a task's slice would end deciding nothing, and the timer is not armed for it.

Lag is kept across a sleep and a move to another CPU. A task leaves its queue with its lag, clamped to two slices or a tick, the longer, and joins the next with it: its virtual runtime that far behind V. Joining moves V by the weight it adds, which would take part of the lag away, so the lag is made larger by (W + w) / W first, and once in the task has the lag it left with (Linux's place_entity has the derivation). Neither sleeping nor moving is a way to be owed more or to owe less: a task that waited for the CPU before it slept is owed that when it wakes, and runs at once; one that had more than its share owes it. A new task joins with no lag and half a slice, to run soon but not a whole slice ahead of everybody.

Everything is integers. The kernel keeps no floating point, so a weight's inverse is fixed point ((1024 << 32) / weight), and V and every eligibility are reckoned with 128-bit multiplications -- never a division the compiler would make a library call of -- as Linux's vruntime_eligible does: relative to the smallest virtual runtime on the queue, a task is eligible when its distance from it times the queue's load is at most the weighted sum of everyone's, so that virtual times may wrap. And every decision is reckoned from scratch: V and every eligibility computed over the run list as it is, nothing kept between decisions that could drift. A decision is O(n) in the tasks that can run on its CPU, which are few.

The queue owns the task on the CPU (EevdfQueue::current). A task on the queue is on the run list or current, and counted in V either way: it joins the list -- new, woken, moved here -- or the CPU, as the idle task does when it starts to run something; goes from one to the other; and leaves from either. A decision picks the next while the last is still current, then the last goes to sleep or back on the list, then the next is taken, and take refuses -- a BUG_ON -- while anything is still current. That order was a comment once, and was broken: the next was taken off the list before the last left the CPU, so the lag a task went to sleep with was reckoned from a V without the next in it. Beside one spinner that V was the sleeper's own virtual runtime, so it went to sleep owed nothing, every time, and at every wakeup waited out what was left of the spinner's slice -- 1.3 ms at the median. Now no order of the calls can leave a task out of V.

The idle task shares the CPU only while it runs something other than its idling (below): it joins the count as it stops idling, placed as a task waking, and leaves it, its lag kept, when it idles again.

sched queue [cpu] prints what EEVDF has of each task a CPU's queue can run -- nice, slice, lag, how far past V its deadline is, whether it is eligible -- and sched nice <pid> <n> and sched slice <pid> <us> set a task's; in the kernel, set_task_nice and set_task_slice.

What holds it:

  • cpp-fuzz.py's eevdf target (testing) drives the queue -- kernel/eevdf.cpp, compiled for the host -- in schedule()'s order, with tasks of random weights and slices that run, sleep, wake, yield and move away and back, and holds every step to a model computed from scratch in 128 bits: the one picked eligible, and no eligible one's deadline sooner; a task that joins has the lag it left with, one that leaves the lag V gives it, one that ran is charged what its weight says; every task on the queue on its list or current, never both or neither; and over a long run of tasks that never sleep, each has had the CPU in proportion to its weight. A leave that reckoned V without the leaver in it failed its first input.
  • The boot's test_eevdf, on a CPU other than the boot's: a task woken beside one task that never blocks, and beside two, must run within half a millisecond and within a millisecond at the median, and a spinner of nice 0 beside one of nice 5 must have between 2.4 and 3.9 times its CPU, for 3.06. The signals come at random points of the spinners' slices, which the first bound takes: at a fixed rhythm they settled just before a slice ended, and the broken order above waited 153 us at the median, under the bound, where at random points it waits 1.3 ms. Under HVF a wakeup waits 4-8 us at the median, under TCG 18-26 us, and the share is 2.9-3.1; with the round robin a wakeup behind two spinners waited 8 ms, and the two shared the CPU half and half.

The context switch

TaskQueue::switch_to marks the outgoing task STATE_WAITING, charges it the time it ran, marks the incoming one STATE_RUNNING, records next->Prev = curr, and calls:

switch_context(next->rsp_, &curr->rsp_, &TaskQueue::switch_complete, next)

switch_context pushes the callee-saved state (x86: PushAll — all GPRs and the flags; arm64: x19–x30 and DAIF), stores the old stack pointer through currSp, loads the new one, and calls the completion callback on the new stack before popping the incoming task's registers. On arm64 it starts with a dsb ish: the task it switches away from may next run on another CPU, and a TLBI it issued here is complete only once this CPU passes a DSB (TLB shootdown).

That callback is the whole trick. The queue lock, the outgoing task's lock and the incoming task's lock were all taken by the previous task, on the previous stack; they are released by switch_complete running as the incoming task. It then does one of two things with the task it just displaced:

  • Still runnable: ask select_next_task_queue() whether it belongs on a lighter CPU, and drop its preempt-disable count. Moved, it comes off this queue first, has its count dropped while it is on none, and only then goes onto the other: no decision ever finds a task on a run list whose count is up. It used to go onto the other first, and every walk had to pass over a task whose count was up; now that would be a bug, and is a BUG_ON. Asleep, it stays: its wakeups and its deadline are this queue's.
  • Exited: put it on the CPU's exited_list_ instead of releasing it. The final put() frees a 64 KiB stack, which triggers a blocking cross-CPU TLB shootdown, and this code runs with interrupts off — two CPUs freeing exited stacks here concurrently would deadlock on each other's shootdown IPI. reap_exited() does it later from the idle task and the softirq task, both of which run with interrupts enabled.

A brand-new task has no saved frame, so hal::build_task_frame fabricates one that looks exactly like a frame switch_context would have pushed, its return address a thunk's -- task_entry_thunk on x86, which calls the entry function out of rbx, and x30 via a thunk on arm64. The first switch into a task is therefore indistinguishable from any other, the stack's alignment included: on x86 the frame is laid out so that switch_context's callback on the new stack (switch_complete) and then the entry function are each entered as the ABI has it, 8 below a 16-byte boundary. It used to be made for the ret alone, straight into the entry function, which left every task's first switch_complete 8 off.

Preemption

Two independent gates:

  • Global. preempt_on() is called once by the BSP, late in boot, right before the Preempt is now on marker. Until then schedule() returns immediately, which is what lets the whole bring-up run without a scheduler. APs spin in preempt_on_wait() until they see it.
  • Per task. Task::preempt_disable_counter_. schedule() increments it and bails out if it was already non-zero, so a task inside preempt_disable() keeps the CPU; and no other task with a non-zero count is on a run list when a decision is made (switch_complete, above), since it could not be switched to safely either. A reschedule that bails out is not forgotten: it leaves Task::preempt_pending_ set, and the preempt_enable() that brings the count back to zero makes it (see below). preempt_irq_save()/preempt_irq_restore() pair the two with interrupt disabling, stashing "preemption was on" in bit 63 of the saved flags so the restore balances correctly even if the global gate moves in between.
  • Spinlocks. RawSpinLock::lock() — and so every SpinLock — disables preemption until unlock(), as Linux's spin_lock does: a holder is never switched away with the lock taken, so no taker spins out a time slice waiting for it. The lock remembers whose count it raised: the scheduler takes its own locks in one task and releases them in the next, across switch_context, and switch_complete checks that schedule()'s is the only count the switched-out task still has. preempt_can_block() asks the whole question — interrupts on and preemption enabled — for code that has to know whether it may wait. RawRwSpinLock does the same on both sides; a read lock has many holders at once, so read_lock() hands back whose count it raised and read_unlock() takes it, the way write_lock_irq_save() hands back its flags.
  • How a spinlock is waited for. One cmpxchg takes a free lock and is the whole of acquire. A taker that finds it held waits by reading it (RawSpinLock::contend) and tries the cmpxchg again only once it reads free: a read shares the lock's cache line with every other waiter, where a cmpxchg takes the line for itself, from the holder too, whose every write to it -- the stamp, the release -- then waits its turn behind the waiters'. The release is a store with release order (Atomic::set_release: a plain MOV on x86-64, STLR on arm64) and the watchdog's stamp a relaxed one; they were lock xchgs, each a full barrier. On Newton's two sockets of 48 CPUs, every one of them taking the same lock at each allocation and free (heapbench's 64 KiB pairs, on a build that did), a pair took 330 µs with the waiters on cmpxchg and 94 µs with them reading; on one CPU a 64-byte pair, two locks taken and let go of, went from 460 ns to 379 ns. A pause after a lost cmpxchg, doubling up to 16, 64 or 256 pauses, measured no different there and is not done.

Involuntary preemption comes from the per-CPU timer tick — the local APIC timer on x86-64, the generic timer on arm64, both at 100 Hz — whose handler ends in Preempt(). The timer is one-shot, armed for the next tick or sooner for a task asleep until a deadline (The local timer); an interrupt for a deadline only reschedules. So does the IPI handler, which is what makes an IPI a reschedule request. Voluntary preemption is a direct schedule() call.

The two differ in one case: when nothing but the idle task could take the CPU. schedule() hands it over — its caller asked to let others run, and the idle task halting the CPU until the next interrupt is what makes Sleep() cheap. Preempt() leaves the CPU with the task the interrupt landed on, if that task can still run. The tick and the IPI handler used to end in schedule(), and that made stalls: the idle task only halts, and a task displaced while runnable — not blocked — gets no IPI from whoever has work for it, so it waited for the next tick. On the AX41 the netblk worker, polling for requests with poll= and never blocking, was switched out that way by the tick some 25 times a second, a tick each: the idle task took a quarter of its CPU while it had work, and the server's p99 was 10 ms. An IPI landing on a running task could do the same to it.

A reschedule that finds preemption off

The tick or an IPI can land on a task with preemption off -- inside a spinlock, most often -- where the reschedule cannot be made. It used to be dropped: the task went on, and whatever the reschedule was for waited for the next tick or IPI. For the tick's own turn-taking that costs nothing; for the IPI a wakeup sends, the wakeup is lost. SoftIrq::raise_mask in an interrupt handler unblocks its CPU's softirq task and sends the CPU an IPI to switch to it. Landing on the idle task inside the lock reap_exited() takes -- a few instructions, with interrupts on -- that IPI's reschedule was dropped, and the idle task went on to halt with the softirq task runnable. Nothing else woke it: a raise that finds its bit already up sends no IPI, a device interrupt never reschedules on its own, and the igb driver keeps its vector masked until the receive poll re-arms it. The CPU slept on its receive queue until its next tick. On the AX41 under netblk's load that was the receive path going quiet for up to 10 ms some 80 times a second -- 13% of round trips over 2 ms, nearly every silence ending on the same 10 ms grid, one CPU's tick -- while netblk's own service time stayed under half a millisecond (see netblk).

A refused reschedule now sets Task::preempt_pending_, and preempt_enable() or preempt_enable_task() -- every unlock goes through one of them -- makes it the moment the count is back to zero with interrupts on: on that idle task, right after the unlock and before the hlt. With interrupts off it waits, since that is an interrupt handler, whose own Preempt() or the next tick will do, or a section its caller keeps closed, until its next preempt_enable(). Only the running task's reschedule is made that way (switch_complete releases locks for the task it switched away from), and none once a panic has begun. schedule(), when it keeps the CPU, also drops its own count before it turns interrupts back on, so an IPI waiting on the way out finds the task preemptible.

One consequence for code that sends and then waits for an answer: the answer can be in before the send returns. The receive softirq may run on the way out of the transmit path, at the transmit lock's unlock -- rare while it took a tick landing just there, likely now that the reschedule an interrupt asked for meanwhile is made there. The DHCP client cleared its answer flag after sending, and over QEMU's igb, which transmits within the doorbell's write, lost every answer that way; it clears it before the send now (DhcpClient::ArmResponse). Arm first, then send.

top prints how many reschedules were deferred in its window and since boot, and how many of those landed on an idle task; the count since boot starts at one per CPU, made on purpose on each idle task by the boot self-test (test_preempt_deferred). Under netblk's load in QEMU (virtio-net, 12 vCPUs), 20 seconds of it put more than a thousand on idle tasks, and on the AX41 some 200 a second land there -- before, each one a CPU asleep on work until its next tick or IPI. Made instead of dropped, they took the AX41's receive path from 397 silences in 5 seconds to none (see netblk for the rest).

Sleep(ns) blocks. The task sets its deadline (Task::sleep_until_) and Block()s, interrupts off as below, and nobody wakes it: the schedule() that switches it out puts it on its queue's sleep list, soonest first, and every scheduling point the CPU has looks at that list's head and puts the sleepers whose time has come back on the run list (TaskQueue::wake_due). The CPU's timer makes one at the deadline (The local timer), so a sleep ends there. It used to end at the first tick after its deadline -- a sleep of a millisecond took ten -- which is where it ended when Sleep was a loop around schedule() that checked the clock each time it got the CPU back. That loop kept the task runnable: alone on its CPU it let the idle task halt it until the tick, but two sleepers on one CPU handed it to each other without end and the idle task never ran. Two idle hypervisor guests on one CPU of the AX41, each vCPU asleep until its guest's next timer edge, kept that CPU busy between them, 46% each; a usb poll task and an ssh session's top did the same. The self-test test_sleep_blocks puts two sleepers on one CPU at every boot and fails it if they run more than a tenth of what they sleep -- a poller runs close to half. Where a task cannot block -- before preemption is on, off a task's stack, in a CPU's idle task, with preemption disabled -- Sleep still polls.

Blocking and waking

Block() and unblock() take no lock; Block() is called by the task itself, unblock() from any CPU including hard IRQ context. Each is a compare-and-swap on one word, Task::run_state_, whose values say where the task is with its queue:

State Means Made by
Queued can run: on the run list, or on its CPU the task's creation; a waker or the deadline from Blocking or Sleeping; its CPU from Waking
Blocking has called Block(), still on its CPU the task itself, from Queued
Sleeping off its CPU, on no list but the sleep list if it has a deadline its CPU's schedule(), from Blocking, as it switches it out
Waking an unblock() found it Sleeping and is handing it back that unblock(), from Sleeping

unblock() that finds a task Blocking makes it Queued, and the schedule() that was about to take it off finds it so and keeps it runnable. One that finds it Sleeping makes it Waking -- having won it from any other waker and from its deadline, since each of those is a compare-and-swap from Sleeping too and exactly one succeeds -- and pushes it onto its queue's wake stack: a Treiber stack that is pushed onto from any context and only ever taken whole, by its CPU's next schedule() (TaskQueue::push_wake, drain_wakes), so it has no pop and no ABA. That schedule() is the waker's to bring about, as it always was: Event::signal and SoftIrq::raise_mask send the CPU an IPI. A task asleep is never moved to another queue, so the queue an unblock() reads is the one the task sleeps on.

The ordering is what makes it safe, and both sides do the mirror image of each other:

  • The sleeper raises its state first, then re-checks whether there is work, then calls schedule().
  • The waker publishes the work first (sets the pending bit, or clears the "running" bit), then looks at the state -- with a locked operation whatever it finds, a failed compare-and-swap included.

A wakeup landing anywhere in that window is either seen by the re-check or finds the task Blocking or Sleeping and puts it back. Every step is sequentially consistent (lock cmpxchg on x86, a sequentially consistent compare-and-swap on arm64); a plain read of the state on the waker's side would let it be made before the work it published was seen, which holds on x86 and fails on arm64 only.

The sleeper's side runs with interrupts off, from the state going up until it is down again, its own schedule() included. The tick and every IPI end in a reschedule of their own, and one landing while it is up would switch the task out as a sleeping one -- asleep on work it never saw, or had just seen and not yet put the state down for, while a waker that found the work already published may not look again. schedule() saves and restores interrupts per task, so a task that blocks with them off comes back with them off, and turns them back on itself.

A waker that names its task through what the task waits on -- an Event's waiter -- holds a pointer the task may outlive the use of: the wait ends, by its deadline or another signal, the task returns and exits, and its Task is freed under a waker that had read the pointer and not yet unblock()ed it. So Event::signal touches the task only inside a window -- counted, interrupts off, the waiter read again inside it -- and every way out of a wait (Event::let_go) empties the waiter's slot first and then waits out whatever signal is still in the window: a few instructions, or a panic naming the event after ten seconds. Once wait() returns, no signal of that event touches its task again -- neither a task freed nor one asleep on something else by then. A freed Task has its magic written dead, and the window holds the task it finds to the live one. The boot self-test test_event_let_go frees 200 short-lived waiters as they return while tasks on the other CPUs signal their event without a pause, each signal holding the task it found for 20 us inside the window (Event::signal_holding): the boot fails unless some of those waits met a signal still in the window and waited it out.

A wakeup that is lost is a task asleep for good, which no panic reports. The boot self-test test_wake_storm makes them in every window the state has: a ring of tasks, two a CPU, each pinned to its own, passing one token round -- every pass a wakeup on another CPU, half of the waits with a deadline and half without, a sleep now and then so that a deadline races the signal that comes for it, and a task yielding on every other CPU so that the run lists are not empty. It fails the boot when the token stops for two seconds, naming the task it was sent to and where that task is -- "woken, and not yet back on its cpu's run list" for a wakeup its CPU never took. 3000 passes take some 70 ms under HVF on four vCPUs. A control run that dropped one push in 1500 stopped the token at pass 1633 and failed the boot.

The idle task only halts (Cpu::idle), so a task that yields on a CPU with nothing else to run gives that CPU to it: the CPU sleeps until the next interrupt it takes itself -- the tick, when the device's interrupt is routed elsewhere -- and a waker that finds the task merely runnable sends no IPI. schedule() is a way to let others run, not a way to wait: wait by blocking, or poll with yield_to_runnable(), which gives the CPU to another runnable task and never to the idle task, returning at once when there is none. A plain spin is no better than the yield: it keeps whatever else is runnable on the CPU off it until the tick -- a softirq task the tick preempted mid-handler included, with its type's work held up on every CPU meanwhile. Sleep() is not for waiting on work either: it ends at its deadline, which the work may come long before; block on the work, with Event::wait_for when there is a deadline as well.

SoftIrq::run is the worked example, and it also shows the one case where "the task is already running" is true and useless: between Block() and the schedule() that follows it, a task is Blocking while still being the current task, so a raise landing there must kick anyway. Its tick_kick is the belt and braces — one atomic read per tick turns a lost wakeup from a wedged CPU into at most a tick of latency. (Before its window had interrupts off, that tick was paid a few times in every thousand network round trips.)

Event (kernel/event.h) packages the pattern for any task with something to wait for: one waiter, signal() from anywhere, a hard IRQ handler included, and an IPI to the waiter's CPU when it finds the waiter blocked. wait_for(ns) is the same wait with Sleep()'s deadline beside it -- the task blocked, woken by a signal() at once or by the scheduler when its time has come -- and says which: how a halted guest's vCPU waits for its timer's next edge or a frame, whichever comes first. The netblk module's worker sleeps on one. A CPU's idle task must never block, on an event or otherwise: it is the scheduler's last resort, and a task exiting on a CPU whose idle task is blocked has nothing to switch to.

The local timer

Each CPU's timer -- the local APIC's on x86-64, the generic timer's CNTV on arm64 -- is one-shot (Cpu::LocalTimer*, hal/timer.h). It is armed for the earliest of three things: the CPU's next tick, on a 100 Hz grid that the tick keeps however the interrupts in between fall; the soonest deadline of a task asleep on the CPU until one -- Sleep(), Event::wait_for(); and, while another task waits on the run list, the end of the running task's slice. An interrupt at the tick does the tick's work, as the periodic timer's did; one before it only reschedules, and the decision that follows puts the sleeper whose time has come back on the run list, or gives the CPU to the task whose deadline has come.

Who arms it for what:

  • Every schedule() brings its CPU's timer forward to the head of the queue's sleep list -- the soonest deadline, a task going to sleep in that very schedule() included -- with interrupts off and the queue's lock held: a write of the local APIC's initial count, or of CNTV's compare value, when the deadline is sooner than what the timer has, and nothing otherwise.
  • And to the end of the slice of whoever runs from there on, if another task waits on the run list (TaskQueue::arm_slice): its deadline, in nanoseconds on the CPU at its weight, and no sooner than 50 us from now. Alone on its CPU a task's slice would end deciding nothing, and a guest's vCPU on a core of its own would leave its guest every 3 ms for it; a task that joins later comes with a decision of its own -- a wakeup's, a deadline's, the tick's -- which arms it then.
  • The interrupt re-arms it for the tick only, and leaves a flag; the next schedule() on the CPU -- the Preempt() at the end of the same handler, as a rule -- brings it forward again, and the flag is what has the queue counted for sched and looked at for a task left waiting too long. That is under the queue lock schedule() holds anyway, so the interrupt takes no lock of its own -- one taken there would spin, interrupts off, on a CPU whose queue another holds, and a stuck one would stop that CPU's tick and with it the watchdog that reports it.
  • A task asleep is not moved to another CPU at its switch-out (switch_complete): only its own CPU's timer is armed for it, and its wakeups go to its queue. It moves, if it should, once it runs.
  • A panic re-arms the tick and nothing else: the queue's lock may be the panicking CPU's.

So every task asleep on a CPU has that CPU's timer armed at or before its deadline, but in the moment between an interrupt and the decision after it -- which is what Preempt() makes, or the preempt_enable() that makes a reschedule the interrupt found deferred. An interrupt for a deadline that a signal() already answered finds nothing to run and costs a reschedule.

test_sleep_prompt, at every boot on every CPU, has a task of its own sleep a millisecond at a time while the CPU's idle task halts between interrupts -- not polling the clock, which would wake the sleeper on time whatever the timer did -- and fails the boot if the median sleep is over 5 ms. Measured: 1.007-1.07 ms under HVF, 1.1-2.4 ms under TCG; with the arming taken out, 10.0 ms on both architectures. The median, because a TCG host that stalls a vCPU now and then puts a sleep or two at 15-50 ms. On the EX44 a guest's usleep 1000, its vCPU halted between, took 10 ms before.

Everything that polls with a short sleep now polls at the rate it asked for: the USB poll at 4 ms instead of the tick's 10, netconsole's idle poll at 2, an ARP wait at 1 -- shorter waits, and more wakeups. TCP's blocking calls -- connect, accept, send and recv, which nothing signals -- polled at 1 ms too, and on the EX44 an idle nos with sshd up and two sessions open woke 2,220 times a second, 1.2% busy over its 20 CPUs; they back off now, 1 ms doubling to 10 (Poll in layers/net/src/tcp.rs), and the same nos woke 741 times a second, 0.4% busy, most of it the USB poll.

Load balancing

Task::select_next_task_queue() returns the queue this task should be on: the one its affinity mask allows with the fewest tasks that can run there -- the one on the CPU included, the idle task not (get_runnable_count) -- and of two as busy, the one home to fewer tasks; or nullptr for "stay put". It runs at task creation and on every context switch for the task being displaced, if that task can still run, and it reads the lock-free mirror of the running-CPU set rather than taking the CPU table lock, because it is on the switch path.

It used to count every task a queue was home to, asleep or not, so a CPU home to a dozen sleepers looked busier than one with two tasks spinning, and tasks were sent where they had to wait. Tasks asleep cost a CPU nothing; what could run there is what a task placed there competes with. They still break a tie, so that new tasks -- most of which sleep -- spread out rather than all go to the first CPU. A task that blocks is no longer weighed at its switch-out: it stays on the queue its wakeups go to, and is weighed the next time it is displaced while it can run.

Two rules keep it from thrashing:

  • The queue the task is already on competes like any other. It used to be skipped, which meant every task moved to another CPU on every context switch — not balancing, just motion, and it threw away whatever the task had warm in that core's caches.
  • A move needs a difference of two (MIGRATE_THRESHOLD). The task can run and is still counted where it is, so moving it takes one off that side and adds one to the other; a difference of one would be reversed on the next switch and the task would trade places forever.

On a machine of several NUMA nodes (NUMA) the lightest queue of the task's own node -- its queue's CPU's, or for a first placing its creator's -- is weighed beside the lightest of all: a move within the node takes the difference of two, one to another node a difference of three (NUMA_MIGRATE_EXTRA), since the task leaves its memory behind there; a task placed for the first time goes to its node's lightest queue unless another node has a lighter one. On a machine of one node the two are the same queue.

A task left waiting

A task on a run list longer than TaskQueue::STALL_REPORT_NS -- five seconds -- is reported, once a wait: sched: <name> (pid N) could run, and has waited M ms for cpu C, with K that can run there. The run list is in no order of waiting, so each time the CPU's timer has the queue looked at, the look walks it for the task that has waited longest. The report is noted under the queue's lock and said once the CPU holds none of the scheduler's locks: the trace ends in the console and, with disklog=on, in a disk write. EEVDF starves no task -- one that waits falls behind V, is eligible, and its deadline comes -- but a weight can make a wait long by design: beside a spinner of nice -20, one of nice 19 has 15/88776 of the CPU, a 3 ms slice every 18 seconds, and this says so. So would a scheduler gone wrong.

Affinity is a ulong bitmask, which is one of the reasons MAX_CPUS is 64. Idle tasks and softirq tasks are pinned to their own CPU; everything else runs anywhere. top prints the number of migrations since boot, which is the only way to know whether "rare" is actually true.

Changing a task's affinity

set_task_affinity(task, mask) -- the shell's sched affinity <pid> <cpus> -- gives a task new CPUs while it runs, and a task on a CPU that is no longer one of them leaves it, whatever state it is in:

  • Running there, or waiting there to run. Its CPU is interrupted, and the decision every IPI ends in (Cpu::ipi) finds the task outside its mask: it does not keep the CPU -- another task takes it, the idle task if nothing else can (TaskQueue::schedule, evicted) -- and switch_complete moves it to the lightest CPU of its mask, whatever the loads (select_next_task_queue skips MIGRATE_THRESHOLD for it), and interrupts that CPU, which may be halted. A task waiting on the run list runs there once more when it is picked, and the decision that evicts it is then asked for at once (the local timer at now) rather than at its slice's end or the tick's.
  • Asleep there. It moves at its wakeup: drain_wakes and wake_due take a woken task outside its mask off the queue instead of onto its run list (leave_if_outbound), and once the queue's lock is down -- schedule's keep path, switch_complete -- it is inserted on the lightest queue of its mask (send_outbound), whose CPU is interrupted. Without it a task that only ever runs a moment between two sleeps mostly blocked again before the decision that evicts a task that runs, and moved only when one happened to find it running: after 1.4 s in one control run, after 30 more wakeups there in another, where it moves at its next wakeup, a millisecond. One asleep for good stays where it is until it wakes.

A task whose mask has no CPU that runs is not evicted (out_of_affinity): it would be taken off at every decision and picked again at the next. set_task_affinity refuses such a mask, and a task bound to its CPU (Task::pin_to_cpu): the idle tasks, the softirq tasks, and a measurement's -- trace bench's and sched probe's tasks, whose numbers are that CPU's. A guest's vCPUs, a disk's or a network's task, the shell: any other task can be moved -- hvarch takes a vCPU's state along (VT-x vmclears its VMCS on the CPU it left, AMD-V gives it an ASID on the new one, and its kick finds the CPU it entered on last). What hv placed with it does not follow: the performance hold on the CPU it was placed on, and hv cores's account of the cores.

The boot's test_affinity moves a spinning task and a sleeping one off the CPU they run on, and holds them away from it, every boot of both arches.

Tasks and stacks

A task's stack is a 64 KiB block that carries its own identity: a Task* and two magic words at its top, above the stack. Task::get_current_task() rounds the stack pointer down to a 64 KiB boundary and reads the pointer from the block found -- no per-CPU variable, no register convention. It works because the page allocator's 16-page bucket hands out blocks aligned to their own size.

The block's lowest page is no part of the stack, and is not mapped while the task has it (Task::guard_stack: the page unmapped, on every CPU; mapped again before the block goes back to the heap). A stack that runs out runs into a fault, there and then. On x86 the fault has no stack to be delivered on and becomes a double fault, on its IST stack, whose report says the stack ran out; on arm64, where an exception is taken on the stack it interrupted, the entry sees a data abort within a page of the stack pointer and takes it on the CPU's overflow stack (vec_stack_fault). Either report walks the frames that ran the stack out. Until then the page was mapped, and a tripwire only: a stack pointer found in it by the next get_current_task() was a BUG_ON, and a stack that ran on without one ran over its own header and into whatever the heap had put below. Not caught: a frame larger than the page, which steps over it. The price is a TLB shootdown at each task's start. There is deliberately no klog() in get_current_task — logging takes a lock, which calls preempt_disable, which calls get_current_task. Every lock asks that way (preempt_current_task) until a panic has begun; from then on a stack that is no task's -- x86's double fault's, a static one -- has nothing to count, since the panic switches to no task again, and the BUG_ON there would be a panic inside the panic that only spins, the report stopped at its first lock.

Stacks are filled with a pattern before first use, so stacks can report each task's high-water mark: what is still intact is what the task never reached. That catches a spike that lasted microseconds during boot and costs nothing while the machine runs.

Tasks are refcounted (get/put), registered in a 512-bucket TaskTable hashed by pointer, and given a pid by an ObjectTable. A task ends by returning from its function: exec_callback calls Exit(), which stamps the exit time, removes the task from the table, sets STATE_EXITED and calls schedule() — which never returns.

Waiting for one to end

Task::wait() is the join every stop path makes -- a Rust TaskHandle dropped or waited on among them: hv stop waiting for a guest's task, the guest's disk waiting for its own, rmmod, the host's shutdown. It never gives up, because a task that has not exited is still using whatever its waiter is about to free. It does not wait in silence either: ten seconds in, and again each time the wait has doubled (five minutes apart at most), it reports to the kernel log who waits, for which task, for how long, and where that task is, its frames named --

join: shell (pid 12) has waited 10000 ms for hv/vm3/vda (pid 57) to exit, and it is blocked
join:   [0] 0xFFFF80000105AF99 kernel::TaskQueue::schedule+0x449
join:   [1] 0xFFFF80000105B594 kernel::schedule_current+0x154
...

-- which is what the TLB shootdown's naming of the CPUs that never answered is for a CPU, for a task. Where a task is comes from Task::locate, which bt uses too:

  • Off its CPU -- blocked, asleep, or runnable and waiting for its turn -- it is read off its own stack: the frame pointer its switch saved, the chain walked inside the stack's bounds (hal::task_saved_frame_pointer checks the saved frame lies inside them too, since the saved stack pointer is read with nothing holding the task still). The read is a seqlock's: kept only if the task's switch count and state show it did not run while it was read, and looked at again if it did. A lost race names wrong frames and is thrown away; it cannot fault, because the stack stays mapped while the asker holds its reference.
  • On a CPU, it is asked about: an IPI to every other CPU, whose handler checks whether the task it landed on is the one wanted and, if so, records where it was -- the interrupted instruction and the frames above it.
  • On a CPU that does not answer -- interrupts off, which is usually where such a task is -- the CPU is named after a second instead: "on a cpu that would not answer: cpus 0x20 took no interrupt in 1000 ms". What the IPI writes into is one slot per CPU, made the first time that CPU is asked and kept for good, never the asker's stack: a CPU that takes the IPI late would otherwise write into a stack long since reused. A slot whose IPI is still unanswered is not sent again, and the CPU is named again; one answered late is reused.

The boot self-test test_task_locate holds each case to frames that name the function the task is in -- a CPU kept deaf for a tenth of a second included -- and has a join reported.

The idle task

Each CPU's idle task is created by Cpu::run(), pinned to that CPU and marked with FLAG_IDLE_BIT. Its body is the CPU's bring-up function (ap_startup / bp_startup), which ends in a loop over Cpu::idle(): reap any tasks that exited on this CPU, then hlt (x86) or wfi (arm64). The BSP's idle task additionally watches for a shutdown or reboot request from the shell.

It is the scheduler's last resort only while it idles: inside Cpu::idle(), about to halt or halted (Task::set_idling), an interrupt that finds something else to run switches to it and leaves the idle task in its place, to run again only when nothing else can. Running anything else -- the boot and its self-tests, which are what the bring-up function runs before its loop, or the shutdown -- it is a task like any other, counted in V and displaced back to the run list to take its turns: it joins the count as it stops idling, placed as a task waking, and leaves it, its lag kept, as it idles again. Each time round its loop it puts the flag up first and then looks at its queue: with a task there it can run, it takes the flag down again and yields (yield_to_runnable) instead of halting. Out of its idling -- woken, round the loop, at the reap -- it is counted as any task, and an interrupt's decision leaves the CPU to the task it lands on unless another's deadline comes sooner: a task woken then, its deadline a slice off like the idle task's, waited on the list while the idle task halted again, until the idle task's slice ended on the timer 3 ms on. On the EX44 a guest's vCPU woken just after its CPU had taken another interrupt -- its disk's completion, a ring from another vCPU -- met it: its guest's timer interrupts were more than a millisecond late thousands of times a run, and a task woken on those CPUs waited 2-4 ms at the 99th percentile with nothing else on the queue. The look comes after the flag, so no window is left between them: a decision before the flag has put its task on the run list, where the look counts it; one after finds the flag and switches. It used to be the last resort whatever it ran, and a task the boot started on the boot CPU that never blocked kept the boot itself off that CPU for as long as it ran: the ring selftest's producer, pinned to the boot CPU and polling for a consumer the boot had not started yet, took the CPU at the next tick, and the boot sat for the thirty seconds the producer gave the consumer -- one boot in a few hundred under HVF, and the selftest's failure said two values of a hundred thousand came out. The old walk had hidden it by an accident of list order: it stopped at the idle task, and a task inserted behind it was not reached. test_idle_takes_turns holds the rule at every boot: beside a task pinned to the boot CPU that only ever yields, a millisecond's sleep in the boot must come back in about a millisecond -- with the old rule it came back in the two seconds that task waited.

Because the idle task is the scheduler's last resort while it idles, a CPU with something always runnable may not reach reap_exited for a while — which is why the softirq task calls it too. The same starvation used to swallow poweroff and reboot outright: Sleep() yielded rather than blocked then, so a CPU carrying the shell, DHCP and USB poll tasks never ran its idle task again, and the request sat unseen (on a two-CPU QEMU, every time). Cmd::request_shutdown()/request_reboot() now clear the BSP idle task's FLAG_IDLE_BIT, making it an ordinary task for what remains of its life: it takes its turn, sees the flag, unmounts the filesystems while the soft IRQs still run, and halts.

What it is measured by

Each queue counts what its CPU's scheduler does, as it does it (SchedStats in kernel/sched.h), and sched [ms] prints a window of it:

  • A wakeup's wait: from the moment a task could run again until it ran. For a task unblocked by someone, that moment is the unblock() that took it from Sleeping -- stamped there (Task::wake_time_), from whatever CPU and context the waker is in, and only by the call that won it. For one asleep until a deadline, it is the deadline (TaskQueue::wake_due stamps it). A stamp from before the task last left its CPU is not this wait's -- the wakeup before -- and is not counted.
  • A delay: a task displaced while it could still run -- by the tick, or an IPI, a slice's end -- waits until it runs again. Kept apart from wakeups: it is the turn-taking's own, where a wakeup's wait is latency someone is waiting on.
  • A decision: what TaskQueue::schedule cost from the moment it turned interrupts off until it switched or returned -- the queue lock's wait included, the switch not -- in cycles, and how many tasks it touched: one for itself, and one for each task it put on the run list, woken or due. Its walks of the run list, for V and for the pick, are as long as the queue at the timer, below.
  • The queue at the timer: each time the CPU's timer has the queue looked at, how many tasks there could run, the one on the CPU included and the idle task not -- and whether two or more could, which is where a woken task has to wait its turn behind another.

Waits go into log2 buckets of microseconds, so a percentile is a bucket's bound ("<256us"), and the longest since boot is kept exactly. The counters are the owning CPU's to write, under its queue lock, with no locked instruction; sched reads them from another CPU a word at a time, so a reading is a moment's and not a consistent whole. The idle task's turns are nobody's wait and are not counted.

Two things a reading says about the machine rather than the scheduler. Under a hypervisor a wakeup that has to bring a halted vCPU back -- an IPI to a CPU in its idle task's hlt or wfi -- waits for the host to run that vCPU again, which is part of the number. And the boot's own test_locate_on_cpu keeps the highest CPU deaf for a tenth of a second on purpose, so that CPU's longest wait since boot is about that.

A woken task behind spinners

sched probe makes the case a scheduler is chosen by and measures it: spinners pinned to one CPU that never yield, a waiter pinned beside them asleep on an Event, and the shell's task on another CPU signalling it a sample at a time. With the round robin there was, under HVF on four vCPUs:

Spinners Waited, p50 Their shares of the CPU, per mille
0 <32us --
1 <8us 995
2 <8.1ms 248, 743
4 <32.7ms 82, 247, 331, 331

A woken task waited behind every task that was ahead of it in the list, a tick each -- the IPI that came for it switched the CPU to whichever runnable task was first, which was not the one woken -- and those early switches made the turns unequal too. With EEVDF, the same machine:

Spinners Waited, p50 p99 Their shares of the CPU, per mille
0 <32us <128us --
1 <32us <128us 998
2 <32us <256us 498, 497
4 <16us <512us 249, 250, 249, 249
8 <16us <1.0ms 124 each

The waiter wakes owed what it waited for the last time, and its deadline is the soonest, so the IPI that comes for it gives it the CPU; and the spinners have their weights' shares, equal ones here, to the per mille. The probe's woken with lag line says what the waiter was owed as it woke, the virtual microseconds its placement brought (SchedEntity::placed_lag).

On the EX44's silicon (cpu=40, a P-core), where the round robin had given HVF's numbers: under 2 us at the median beside one, two and four spinners, p99 under 64 us, the shares 998; 498, 499; 250, 249, 249, 249 -- and the boot's test_eevdf there, on an E-core, 1 us beside one spinner and two, and 3.0 for the share. What it costs a machine at rest is about what the round robin did: a decision 1.2-1.6 us on each CPU, where the round robin's took 1.5, and blkload's random reads 26768 IOPS at qd=1, 180610 at qd=8. Under HVF, the three measured side by side at rest, a decision costs 1.0 us, the round robin's 0.74 and the run queue's before EEVDF 0.66: V and every eligibility are reckoned from scratch, two to four times a decision. The longest wait of each run there, 5-7 ms, comes with no spinner at all, as it did with the round robin: it is not the queue's.

Looking at it from the shell

Command Shows
ps Every task: pid, state, flags, accumulated runtime, context switches, name
top [ms] Per-task CPU use over a window, as a percentage per CPU (a busy thread reads 100%, a 20-CPU box tops out at 2000%), plus the migration count and the deferred reschedules
sched [ms] Per CPU over a window: wakeups and how long they waited for the CPU, displaced tasks' delays, decisions and their cost, the tasks each touched, and how many tasks could run at the timer (above)
sched probe [spinners=N] [ms=M] [cpu=N] How long a woken task waits beside tasks that never yield, the lag it woke with, and each task's share (above)
sched queue [cpu] What EEVDF has of each task a CPU can run, the one on it first: nice, slice, lag and deadline from V, eligible or not (above)
sched nice <pid> <n>, sched slice <pid> <us> A task's nice, -20 to 19, and its slice, 100 us to 100 ms
trace on sched (idle, debug) The scheduler's own events, recorded into each CPU's ring as they happen and read back merged by time: every switch with what the one before went to be and the decision's reason, every wakeup and who made it, blocks with how long the task ran, migrations, IPIs, a task made and one exiting, the idles -- see Tracepoints
stacks High-water mark of every task stack and every static boot stack
bt <pid> Where a task is and its stack trace (Task::locate, above): read off its stack when it is off its CPU, caught by an IPI on the CPU running it, or the CPUs that would not answer one named
profile Where the time actually goes — see Profiler