softirq: fix idle accounting for an interrupted idle task
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest2 patches 6 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/2 | sched_ext: Add scx_tickbug, a scheduler that holds a task on an idle CPUtools/ tools/ tools/ +1 more | +725 | 3 replies | |
| 2/2 | softirq: Treat an interrupted idle task as idle in tick_irq_exit()include/ kernel/ kernel/ | +10 | 3 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH 0/2] softirq: fix idle accounting for an interrupted idle task
tick_irq_exit() decides whether an interrupt that just finished ended inside the idle loop, and it asked sched_core_idle_cpu(), which is idle_cpu(): false whenever the run queue is not empty. But a CPU can be in the idle loop with tasks queued on it - forced idle under core scheduling, and any task a sched_ext BPF scheduler holds back while leaving it runnable - and every interrupt taken in that state skipped tick_nohz_irq_exit(). The fix asks is_idle_task(current) instead, which is the question the function means to ask.
Patch 1 adds the scheduler that holds the state open long enough to measure, patch 2 is the one-line change.
Measurements:
One thread spinning 10 ms in every 100 ms in a cgroup, pinned to the CPU the scheduler holds it on, 20 s windows, three repetitions per cell, arms interleaved, on one 16-CPU KVM guest booted with "no-kvmclock tsc=unstable" - which is what it takes to get an unstable sched_clock in a guest, because mark_tsc_unstable() clears sched_clock stability only when using_native_sched_clock(). Two kernels from this tree differing only in patch 2, booted in turn.
The number is the cgroup's cpu.stat usage_usec over the thread's own CLOCK_MONOTONIC measurement of its busy time, which is an upper bound on the CPU it can possibly have used. 1.00 is correct accounting; above 1.00 the kernel charged the task time it did not spend.
hold (ms) without patch 2 with patch 2
7 1.32 0.964
13 1.21 0.963
23 2.00 0.963
37 1.69 0.964
53 1.33 0.964control, fair class, no BPF scheduler loaded:
1.005 1.006
Nothing in the fixed kernel's figures varies with the hold, and none of the unfixed kernel's is within 20% of correct. The three repetitions of each cell span at most 0.04.
The unfixed kernel also understates how long it held the task, which is the same bug seen from the other side: the scheduler measures the hold with scx_bpf_now(), the sched_clock that is not being resynchronized. For the 23 ms cell it reported a 5.1 ms mean hold where the fixed kernel reported 15.2 ms for the identical configuration. So the fixed kernel accounts correctly across holds three times longer than the ones the unfixed kernel gets wrong.
What is not shown, and what was not done:
No loss in /proc/stat. The sum of that CPU's fields 2 to 9 over the window stayed within 0.6% of the window on both kernels, so in this configuration the kcpustat_irq_exit() half of what tick_nohz_irq_exit() does has no visible effect, unlike the forced idle case that commit 548796e2e70b ("sched/core: introduce sched_core_idle_cpu()") widened the gate for.
The mechanism is bounded rather than measured. A kprobe on tick_nohz_irq_exit() filtered to the idle task counted 998 calls per 20 s window on the held CPU without patch 2 against 1321 with it, n=3 each, but those ranges overlap and the two kernels run the load on different timings.
The forced-idle half of the problem is not measured here at all. Patch 2 changes the same gate for core scheduling, where that commit first widened it, but every number above comes from the sched_ext path.
The figures were taken with an earlier revision of patch 1, before an automated review of the tool found six defects in it. Four of them cannot move these numbers: the per-task hold timestamp (only one task was ever held, so the global it replaces was never overwritten), the ops.dequeue() bookkeeping (the load never changes affinity or priority while parked), the release timer's pinning check (a mispinned timer fails the run loudly, and no run failed) and the userspace CPU validation. Two could: @target_cpu now stays in the idle loop while it holds something instead of taking shared work, and a %SCX_ENQ_LAST enqueue of the tagged task is now held rather than put back. Both make the held state wider, so both move the unfixed kernel further from 1.00, not closer. The load exhausted a 20 ms slice only if a 10 ms burst ran long, so the second one was rarely reached. The test machines are gone and this was not re-measured.
Open question:
The Fixes: tag. The sched_ext path this was found on dates from commit f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class"), which is what patch 2 carries. But the gate was idle_cpu() before that, and what put sched_core_idle_cpu() there was commit 548796e2e70b ("sched/core: introduce sched_core_idle_cpu()"). Whichever of the two you prefer, or neither, is yours to pick.
Tooling:
Both patches were written by Claude Opus 5 (Claude Code), including the test scheduler, the measurement harness and these changelogs. The measurements were run and the results checked by hand. The one-line change in patch 2 and the reasoning for it are in Documentation terms tool-generated content; review it as such.
Radu Toader (2):
sched_ext: Add scx_tickbug, a scheduler that holds a task on an idle
CPU
softirq: Treat an interrupted idle task as idle in tick_irq_exit()
include/linux/sched.h | 2 - kernel/sched/syscalls.c | 12 - kernel/softirq.c | 12 +- tools/sched_ext/Makefile | 2 +- tools/sched_ext/README.md | 13 + tools/sched_ext/scx_tickbug.bpf.c | 534 ++++++++++++++++++++++++++++++ tools/sched_ext/scx_tickbug.c | 177 ++++++++++ 7 files changed, 735 insertions(+), 17 deletions(-) create mode 100644 tools/sched_ext/scx_tickbug.bpf.c create mode 100644 tools/sched_ext/scx_tickbug.c
base-commit: f66469d9905b39d2c260a15d7d6db450f4db67c1
Signature · 1 line
2.43.0