From: Kuba Piecuch <jpiecuch@google.com>
To: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
Andrea Righi <arighi@nvidia.com>,
Changwoo Min <changwoo@igalia.com>
Cc: Kuba Piecuch <jpiecuch@google.com>,
sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: sub: Don't run ops.sub_caps_updated() in an SCX op context
Date: Wed, 25 Nov 2026 15:34:19 +0000 [thread overview]
Message-ID: <20261125153353.1364467-1-jpiecuch@google.com> (raw)
scx_bpf_sub_grant() and scx_bpf_sub_revoke() are in scx_kfunc_ids_any, so
a parent scheduler can call them from any op. Before returning, they
deliver ops.sub_caps_updated() to the affected children synchronously:
caps_updated_deliver()
SCX_CALL_OP(sch, sub_caps_updated, NULL, cu->cmask_arena_out, caps);
__SCX_CALL_OP() leaves scx_locked_rq_state untouched when locked_rq is
NULL. Unlike other SCX ops, which either always run unlocked or always
hold a known rq lock, ops.sub_caps_updated() inherits whatever context its
parent happened to call grant or revoke from:
1) Inconsistent locking context across calls. When the parent calls
grant/revoke from ops.sub_attach() or a BPF timer, scx_locked_rq() is
NULL; when it calls them from an rq-locked op, scx_locked_rq() still
points at the parent's rq. For example, scx_qmap nested three deep runs
a grandchild's ops.sub_caps_updated() under the middle scheduler's rq
lock when ops.sub_ecaps_updated() picks up a pending PART_REDISTRIBUTE
in execute_partition() -> apply_partition().
ops.sub_caps_updated() has no scx_kf_allow_flags[] entry (only the
universal any/idle/cid kfunc sets are allowed), yet kfuncs in
scx_kfunc_ids_any consult scx_locked_rq() and the child has no safe way
to probe which state it is in: scx_bpf_locked_rq() aborts the child
when unlocked, SCX_DSQ_LOCAL in scx_bpf_dsq_reenq() and
scx_bpf_dsq_nr_queued() resolves to scx_locked_rq() ?: this_rq(), and
scx_bpf_cidperf_set() (for example on cids where SCX_CAP_PERF is
already effective) works across all cids when unlocked but aborts the
child on any cid outside the parent's rq:
CPU 2, parent P, child sub-scheduler C
enqueue_task_scx(rq2, p) // rq2 locked
SCX_CALL_OP_TASK(P, enqueue, rq2, p) // scx_locked_rq() = rq2
P.enqueue(p)
scx_bpf_sub_grant(C, SCX_CAP_PERF, cids)
caps_updated_deliver()
SCX_CALL_OP(C, sub_caps_updated, NULL) // still rq2
C.sub_caps_updated()
scx_bpf_cidperf_set(cid of CPU 1, ...)
scx_cpuperf_set()
scx_error("Invalid target CPU 1") // C aborted
sched_ext: BPF sub-scheduler "sub_caps_ctx" disabled (runtime error)
sched_ext: sub_caps_ctx: Invalid target CPU 1
bpf__sched_ext_ops_cid_sub_caps_updated+0x57/0xb7
bpf__sched_ext_ops_cid_enqueue+0x50/0xb0
2) Parent lock hold time across subtree cascades. ops.sub_caps_updated()
is meant to track held shard caps and propagate grant/revoke to
children. A single grant or revoke can synchronously fan out across a
subtree of descendant callbacks (up to SCX_SUB_MAX_DEPTH levels, with
multiple children per level) while the parent's rq lock, p->pi_lock
(from ops.select_cpu()) or scx_dump_lock (from ops.dump*()) remains
held, delaying other CPUs contending on that lock.
3) Dump state leakage. When the call comes from ops.dump*(),
scx_dump_data.cpu remains set, so a child calling scx_bpf_dump() passes
the check in scx_bpf_dump_bstr() and writes into the parent's dump
instead of failing.
Sending as an RFC because there is no obvious best way to resolve this:
- Restricting scx_bpf_sub_grant() and scx_bpf_sub_revoke() to unlocked ops
would avoid deferred delivery, but would disallow repartitioning from
rq-locked callbacks such as scx_qmap's ops.sub_ecaps_updated().
- Keeping synchronous delivery as-is, handling scx_dump_data separately
(or rejecting grant/revoke during dump), and documenting that
ops.sub_caps_updated() inherits the caller's locking context would avoid
irq_work while keeping kfuncs deadlock-free, at the cost of an
inconsistent locked/unlocked context and holding parent locks across
subtree cascades.
- Deferring delivery to irq_work when called from an SCX op context (this
series) keeps grant/revoke callable from any op while giving
ops.sub_caps_updated() a consistent unlocked context and releasing
parent locks and dump state before subtree callbacks run, at the cost of
extra irq_work, hotplug, PM and teardown machinery.
Patch 1 adds caps_updated_deliver_or_defer() for grant/revoke. When the
caller has an rq locked, has kf_tasks[] set, is in ops.select_cpu() or is
dumping (the new scx_in_dump()), the to_deliver list is spliced onto a
global pending list and a per-cpu hard irq_work delivers it. Each cpu uses
its own hard irq_work so concurrent deliveries across cpus cannot clear
IRQ_WORK_BUSY early, and so a dying cpu on PREEMPT_RT doesn't strand work
after its irq_work thread is parked; if this cpu is already offline (such
as ops.update_idle() when hotplug parks the migration thread), an online
cpu's irq_work is kicked instead. While PM is in progress, kicking is
suppressed so irq_work_queue() never runs in the syscore phase, and
scx_sub_pm_end() kicks any queued deliveries once the PM cycle ends.
Otherwise delivery stays synchronous, as before. Queued scx_caps_updated
nodes stay in flight, so later changes keep coalescing into them.
scx_free_pshards() drops a dying scheduler's queued entries and flushes
every cpu's irq_work before freeing the pshards.
Patch 2 adds the sub_caps_ctx selftest. A cid-form root grants/revokes
SCX_CAP_PERF to a child from ops.enqueue() and SCX_CAP_PREEMPT from
ops.select_cid(). The child calls scx_bpf_cidperf_set() on every cid from
ops.sub_caps_updated(). Without patch 1, sub_caps_ctx fails with:
Scheduler exited unexpectedly (kind=1024 code=0 msg='Invalid target CPU 0')
The patches were written with the help of an AI coding assistant.
Kuba Piecuch (2):
sched_ext: sub: Defer ops.sub_caps_updated() out of SCX op contexts
selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked
kernel/sched/ext/ext.c | 6 +
kernel/sched/ext/internal.h | 8 +
kernel/sched/ext/sub.c | 129 +++++++++++++-
tools/testing/selftests/sched_ext/Makefile | 1 +
.../selftests/sched_ext/sub_caps_ctx.bpf.c | 136 +++++++++++++++
.../selftests/sched_ext/sub_caps_ctx.c | 165 ++++++++++++++++++
6 files changed, 441 insertions(+), 4 deletions(-)
create mode 100644 tools/testing/selftests/sched_ext/sub_caps_ctx.bpf.c
create mode 100644 tools/testing/selftests/sched_ext/sub_caps_ctx.c
base-commit: 97e1c7d467d07734f8f5c8c64730e1f7c415bd46
--
2.56.1.839.g22a8290db9-goog
next reply other threads:[~2026-11-25 15:34 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 15:34 Kuba Piecuch [this message]
2026-11-25 15:34 ` [RFC PATCH 1/2] sched_ext: sub: Defer ops.sub_caps_updated() out of SCX op contexts Kuba Piecuch
2026-11-25 15:34 ` [RFC PATCH 2/2] selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked Kuba Piecuch
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125153353.1364467-1-jpiecuch@google.com \
--to=jpiecuch@google.com \
--cc=arighi@nvidia.com \
--cc=changwoo@igalia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=sched-ext@lists.linux.dev \
--cc=tj@kernel.org \
--cc=void@manifault.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®