Skip to contentSkip to conversation

sched_ext: Reset cpuperf_target when a sub loses SCX_CAP_PERF or dies

PATCH In review Tao Cui 3 participants 4 messages Oct 6 – Oct 8

Subsystems (2)
lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH v2 1/1 sched_ext: Reset cpuperf_target when a sub loses SCX_CAP_PERF or dies

From: Tao Cui <[email protected]>

When a sub-scheduler holding SCX_CAP_PERF sets a low cpuperf target and then goes away - through cap revoke, kill, detach or cgroup removal - the target stays behind. scx_bpf_sub_revoke() only clears the pshard caps[] bitmaps: the cap gate in scx_cpuperf_set() blocks new writes but leaves the old value in place. scx_sub_disable() rehomes the tasks and unlinks the scheduler without touching rq->scx.cpuperf_target either.

Nothing ever resets the value afterwards. The reader gate scx_cpuperf_target() only tests the global scx_enabled(), which still holds while the root scheduler is running, so schedutil keeps consuming the stale target as its utilization base on every DVFS update. In switched-all mode, where sugov_get_util() does not add CFS utilization on top, the affected CPU stays pinned at a low frequency until sched_ext is disabled and re-enabled as a whole. A root scheduler like scx_simple never calls the cpuperf kfuncs, so the pin is unbounded.

Fix it by restoring the neutral base that the root scheduler establishes at enable time, at every point where a writer loses the ability to write:

- scx_bpf_sub_revoke() queues a reset for the CPUs backing the actually-revoked cids. The revoke's write gate only closes when the target CPU folds the ecaps sync in dispatch_one, so the queueing alone leaves a window where the still-running sub-scheduler can re-pollute the freshly reset value; the fold now also restores the neutral base in the same rq-locked section that closes the gate, which is ordered against both the racing writes and any surviving co-holder's writes.
- scx_sub_disable() sweeps the scheduler's pshard SCX_CAP_PERF cmasks after it is unlinked and drained and queues a reset for every CPU it held the cap on. The sweep runs with scx_enable_mutex and cpus_read_lock() held: the mutex keeps a concurrent root disable from retiring the cid tables the sweep dereferences - drain_descendants() only waits for descendants to unlink, so scx_root_disable() would otherwise be free to run to scx_cid_retire_tables() while the sweep is in flight - and cpus_read_lock() keeps the target CPUs from going offline across the cpu_online() tests. The tables are __rcu-annotated and the sweeper is preemptible, so the sweep also takes the RCU read side.
- scx_bpf_cidperf_set() rejects writes from a dying scheduler: between the sweep and ops.exit() - and in the timers/tracers window after it - the exit path may still run, and a write there would land after the final reset with nothing left to clean it up.
- scx_online_ecaps() restores the neutral base when a CPU comes back, so a CPU that was offline when its writer went away is cleaned up at re-online instead of resuming the pin.

The reset needs the target rq's lock. scx_bpf_sub_revoke() runs with IRQs disabled under pshard locks and may be entered from BPF with another rq already locked, so the reset is carried out from a per-CPU irq_work on the target CPU instead of taking cross-CPU rq locks. The two resets cover different cases: the fold-time reset closes the window at the moment the gate closes, and the IPI resets the value right away for CPUs whose fold is postponed indefinitely by a higher-class hog or that never dispatch again - a sustained remote writer against such a CPU can keep re-polluting the value after the one-shot IPI; it is corrected whenever the gate eventually closes, and for a CPU that never dispatches again the exposure lasts as long as the writer does. The revoke path can't pin hotplug; there, a CPU marked offline between the cpu_online() test and the queueing trips irq_work_queue_on()'s WARN_ON_ONCE once, and the queued work is still delivered - by the dying-cpu flush, the re-online flush, or the per-CPU irq_work kthread on RT - before or after the online reset; either order ends at the neutral base. Over-resetting is deliberate: a scheduler still sharing the cap rewrites its own target on its next update, which on an idle CPU may take a while; a writer that never writes again leaves the CPU at the neutral
base - the safe state to sit on.

Tested on a VM with a lockdep-enabled kernel. A sub-scheduler setting target 1 and then being killed leaves rq->scx.cpuperf_target at 1 under full load on the baseline; with this patch, the target returns to SCX_CPUPERF_ONE immediately on kill. With the CPU taken offline before the kill, the baseline resumes pinning the CPU at the stale target once it comes back, while this patch restores the neutral base at re-online. Revoking SCX_CAP_PERF from a live sub-scheduler resets the target when the revoke's gate closes and the cap gate keeps the CPU at the neutral base afterwards. A hotplug cycle with the cap still held restores the base at re-online until the sub-scheduler rewrites it, and ten rounds of sub-scheduler load/unload with interleaved hotplug run clean under both lockdep and KASAN.

Fixes 3a21e34eb258 ("sched_ext: Gate scx_bpf_cidperf_set() behind a new SCX_CAP_PERF")

---
v1 -> v2:
- queue the disable-path sweep under scx_enable_mutex and cpus_read_lock() so that it can't race scx_cid_retire_tables() on a concurrent root disable and its cpu_online() tests can't lose to hotplug (Sashiko)
- take the RCU read side around the sweep: the tables are __rcu-annotated and the sweeper is preemptible, so lockdep (rightly) complained about the bare dereference
- restore the neutral base in scx_online_ecaps() so that a CPU which was offline when its writer went away doesn't come back up with the stale target, refreshing the rq clock first so that cpufreq_update_util()'s rq_clock assertion holds on the hotplug path (Sashiko)
- fold the target back to the neutral base in the rq-locked section that closes the write gate on cap loss, closing the window where a still-running sub-scheduler could re-pollute the value reset by the revoke-path IPI
- reject cpuperf writes from a dying scheduler so that ops.exit() and late stragglers can't land a write after the final sweep
- fix up the comments that the above falsifies and reword the over-reset/bounded claims; retarget Fixes at the commit that introduced the SCX_CAP_PERF gate

---
 kernel/sched/ext/ext.c |  17 ++++-
 kernel/sched/ext/sub.c | 160 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 174 insertions(+), 3 deletions(-)
kernel/sched/ext/ext.c +14 -3
index ae62c08b097a..5490a0e3cf9e 100644
@@ -10884,7 +10884,9 @@ static s32 scx_cpuperf_set(struct scx_sched *sch, s32 cpu, u32 perf)
 	/*
 	 * ecaps updates are folded under the rq lock, making this test
 	 * authoritative: a write can never land after a revoke has taken
-	 * effect on @cpu.
+	 * effect on @cpu. The fold in scx_process_sync_ecaps() also restores
+	 * the neutral target when SCX_CAP_PERF is lost, so a revoke can't
+	 * leave a stale value behind the just-closed gate.
 	 */
 	if (likely(!scx_missing_caps(sch, cpu, SCX_CAP_PERF))) {
 		rq->scx.cpuperf_target = perf;
@@ -10940,8 +10942,8 @@ __bpf_kfunc void scx_bpf_cpuperf_set(s32 cpu, u32 perf, const struct bpf_prog_au
  *
  * cid-addressed equivalent of scx_bpf_cpuperf_set(). A sub-sched needs
  * SCX_CAP_PERF on @cid. Returns 0 if the target was applied, -%EACCES if
- * the write was denied for missing caps, other -errnos if @cid didn't
- * resolve.
+ * the write was denied for missing caps or because the scheduler is dying,
+ * other -errnos if @cid didn't resolve.
  */
 __bpf_kfunc s32 scx_bpf_cidperf_set(s32 cid, u32 perf,
 				    const struct bpf_prog_aux *aux)
@@ -10958,6 +10960,15 @@ __bpf_kfunc s32 scx_bpf_cidperf_set(s32 cid, u32 perf,
 	if (!scx_kf_allowed_ctx(sch))
 		return -EDEADLK;
 
+	/*
+	 * Reject writes from a dying scheduler. Its targets have been or
+	 * will be swept back to the neutral base, but ops.exit() and any
+	 * late stragglers still run after that sweep - a write there would
+	 * land after the final reset with nothing left to clean it up.
+	 */
+	if (unlikely(atomic_read(&sch->exit_kind) != SCX_EXIT_NONE))
+		return -EACCES;
+
 	cpu = scx_cid_to_cpu(sch, cid);
 	if (cpu < 0)
 		return cpu;
kernel/sched/ext/sub.c +160 -0

202 lines not on this page · raw message

Signature · 1 line
2.53.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] bpf: missing dying scheduler check in scx_bpf_cpuperf_set() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: missing dying scheduler check in scx_bpf_cpuperf_set()
--

commit 44646e483943d4ebcaaab6b95abd7f0ef7f1bd41
Author: Tao Cui <[email protected]>

sched_ext: Reset cpuperf_target when a sub loses SCX_CAP_PERF or dies

This commit resets the cpuperf_target of a sub-scheduler when it loses SCX_CAP_PERF or dies. It adds a dying scheduler check to scx_bpf_cidperf_set() to prevent late writes from polluting the state.

Quoted 4 lines
kernel/sched/ext/ext.c +0 -0
index 0917f05263a37..8b661e9bdf6bb 100644

[ ... ]

kernel/sched/ext/ext.c __bpf_kfunc s32 scx_bpf_cidperf_set(s32 cid, u32 perf, · line 10971

Quoted 13 lines
kernel/sched/ext/ext.c +9 -0
@@ -10958,6 +10960,15 @@ __bpf_kfunc s32 scx_bpf_cidperf_set(s32 cid, u32 perf,
 	if (!scx_kf_allowed_ctx(sch))
 		return -EDEADLK;
 
+	/*
+	 * Reject writes from a dying scheduler. Its targets have been or
+	 * will be swept back to the neutral base, but ops.exit() and any
+	 * late stragglers still run after that sweep - a write there would
+	 * land after the final reset with nothing left to clean it up.
+	 */
+	if (unlikely(atomic_read(&sch->exit_kind) != SCX_EXIT_NONE))
+		return -EACCES;
+

[Severity: High]
This correctly prevents a dying scheduler from repolluting targets via the CID-addressed variant, but should the same check be applied to the CPU-addressed variant, scx_bpf_cpuperf_set(), defined just above this function?

Looking at scx_bpf_cpuperf_set() in kernel/sched/ext/ext.c:

    if (!scx_kf_allowed_ctx(sch))
            return;

    scx_cpuperf_set(sch, cpu, perf);

If a dying sub-scheduler executes BPF timers or tracers after the final cpuperf sweep in scx_sub_disable() but before sch->dead is set, could it use scx_bpf_cpuperf_set() to bypass this safety gate and repollute rq->scx.cpuperf_target?

Since the root scheduler remains running, would schedutil permanently consume this stale low-frequency target and pin the CPU?

Quoted 3 lines
kernel/sched/ext/ext.c +0 -0
 	cpu = scx_cid_to_cpu(sch, cid);
 	if (cpu < 0)
 		return cpu;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer
#2 lore raw
In reply to Tao Cui

Hello, Tao.

Quoted 3 lines · Tao Cui

Fix it by restoring the neutral base that the root scheduler establishes at enable time, at every point where a writer loses the ability to write:

I don't think the kernel should be picking a value here. The cpuperf target is last-writer-wins state with no owner tracking, and the only value the kernel ever imposes is the initialization at enable. If a parent grants SCX_CAP_PERF to a child, it should expect the child to have changed the targets, and restoring them after a revoke or in ops.sub_detach() is the parent's responsibility. The root holds every cap and a sub-sched parent holds PERF wherever it could grant it, so the rewrite always has the cap.

There is a gap on the kernel side which makes that unreliable right now. ops.sub_detach() runs before the child's ops.exit() and before the child loses kfunc access, so a child timer or its ops.exit() can still write after the parent has cleaned up. I'll post a patch which drops the dying child's caps before ops.sub_detach() so that the child is inert by the time the parent is notified.

Thanks.

--
tejun

author
#3 lore raw

Hello Tejun,

Quoted 15 lines · Tejun Heo

Hello, Tao.

Quoted 3 lines · Tao Cui

Fix it by restoring the neutral base that the root scheduler establishes at enable time, at every point where a writer loses the ability to write:

I don't think the kernel should be picking a value here. The cpuperf target is last-writer-wins state with no owner tracking, and the only value the kernel ever imposes is the initialization at enable. If a parent grants SCX_CAP_PERF to a child, it should expect the child to have changed the targets, and restoring them after a revoke or in ops.sub_detach() is the parent's responsibility. The root holds every cap and a sub-sched parent holds PERF wherever it could grant it, so the rewrite always has the cap.

Quoted 7 lines

There is a gap on the kernel side which makes that unreliable right now. ops.sub_detach() runs before the child's ops.exit() and before the child loses kfunc access, so a child timer or its ops.exit() can still write after the parent has cleaned up. I'll post a patch which drops the dying child's caps before ops.sub_detach() so that the child is inert by the time the parent is notified.

Fair points. I conflated "the kernel can see a writer go away" with "the kernel should fix the value". Treating the cpuperf target as last-writer-wins with enable-time initialization as the only value the kernel imposes is a cleaner contract, and whoever granted the cap is the right owner for restoring targets after a revoke. I'm dropping my patch.

Thanks.
--
Tao

Quoted 4 lines

Thanks.

--
tejun