* [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: sub: Don't run ops.sub_caps_updated() in an SCX op context
@ 2026-11-25 15:34 Kuba Piecuch
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
0 siblings, 2 replies; 3+ messages in thread
From: Kuba Piecuch @ 2026-11-25 15:34 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, sched-ext, linux-kernel
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* [RFC PATCH 1/2] sched_ext: sub: Defer ops.sub_caps_updated() out of SCX op contexts
2026-11-25 15:34 [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: sub: Don't run ops.sub_caps_updated() in an SCX op context Kuba Piecuch
@ 2026-11-25 15:34 ` Kuba Piecuch
2026-11-25 15:34 ` [RFC PATCH 2/2] selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked Kuba Piecuch
1 sibling, 0 replies; 3+ messages in thread
From: Kuba Piecuch @ 2026-11-25 15:34 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, sched-ext, linux-kernel
scx_bpf_sub_grant() and scx_bpf_sub_revoke() are in scx_kfunc_ids_any and
deliver ops.sub_caps_updated() synchronously with a NULL locked_rq.
__SCX_CALL_OP() leaves scx_locked_rq_state untouched when locked_rq is
NULL, so ops.sub_caps_updated() inherits whatever context its parent
called grant or revoke from:
- From ops.sub_attach() or a BPF timer, scx_locked_rq() is NULL. From an
rq-locked op (such as ops.enqueue(), or scx_qmap repartitioning a
grandchild from ops.sub_ecaps_updated()), scx_locked_rq() points at the
parent's rq. Unlike other callbacks, the child cannot safely tell which
state it is in: scx_bpf_locked_rq() aborts the child when unlocked,
scx_bpf_cidperf_set() aborts it with "Invalid target CPU" on any cid
outside the parent's rq, and SCX_DSQ_LOCAL in scx_bpf_dsq_reenq() and
scx_bpf_dsq_nr_queued() resolves to scx_locked_rq() ?: this_rq().
- Because ops.sub_caps_updated() may propagate grant/revoke to its own
children, synchronous delivery can fan out across a subtree of
descendant callbacks while holding the parent's rq lock, p->pi_lock
(from ops.select_cpu()) or scx_dump_lock (from ops.dump*()), extending
lock hold time.
- From ops.dump*(), scx_dump_data.cpu remains set, so a child calling
scx_bpf_dump() writes into the parent's dump instead of failing.
Give ops.sub_caps_updated() a consistent unlocked context by deferring
delivery when grant or revoke runs in an SCX op context (an rq lock held,
kf_tasks[] set, ops.select_cpu() or ops.dump*()). Queue the deliveries on
a global list and kick a per-cpu hard irq_work, or an online cpu's when
this cpu is going down. Suppress the kick while PM is in progress, where
irq_work_queue() can malfunction in the syscore phase, and kick any queued
deliveries from scx_sub_pm_end(). Queued nodes stay in flight so later
updates keep coalescing into them. Before freeing a scheduler's pshards,
scx_free_pshards() drops its queued entries and flushes every cpu's
irq_work.
Fixes: 5f2a9a4c2e6c ("sched_ext: Add coalescing sub_caps_updated() notifier for sub-schedulers")
Cc: stable@vger.kernel.org # v7.3+
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
kernel/sched/ext/ext.c | 6 ++
kernel/sched/ext/internal.h | 8 +++
kernel/sched/ext/sub.c | 129 ++++++++++++++++++++++++++++++++++--
3 files changed, 139 insertions(+), 4 deletions(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index efa1d01d03af..570d6c923904 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -7964,6 +7964,12 @@ static void ops_dump_exit(void)
scx_dump_data.cpu = -1;
}
+/* whether this cpu is in scx_dump_state(), where scx_bpf_dump() is allowed */
+bool scx_in_dump(void)
+{
+ return READ_ONCE(scx_dump_data.cpu) == raw_smp_processor_id();
+}
+
static void scx_dump_task(struct scx_sched *sch, struct seq_buf *s, struct scx_dump_ctx *dctx,
struct rq *rq, struct task_struct *p, char marker)
{
diff --git a/kernel/sched/ext/internal.h b/kernel/sched/ext/internal.h
index 58652370da5a..69846ac85a44 100644
--- a/kernel/sched/ext/internal.h
+++ b/kernel/sched/ext/internal.h
@@ -909,6 +909,8 @@ struct sched_ext_ops {
* cpu, once it is in effect.
*
* May call scx_bpf_sub_grant() / scx_bpf_sub_revoke() on children.
+ * Even if the parent called grant / revoke under an rq lock or from
+ * ops.select_cpu() or ops.dump*(), this callback runs unlocked.
*/
void (*sub_caps_updated)(const struct scx_cmask *cmask, u64 caps);
@@ -1666,6 +1668,11 @@ enum scx_cap_flags {
*
* (3) leads to deferred delivery. Events accumulate during grant/revoke and are
* delivered after the shard lock is released.
+ *
+ * When grant or revoke runs in an SCX op context (an rq lock held,
+ * ops.select_cpu() or ops.dump*()), caps_updated_deliver_or_defer() defers the
+ * delivery to an irq_work so that ops.sub_caps_updated() always runs unlocked
+ * instead of inheriting the caller's context.
*/
struct scx_caps_updated {
raw_spinlock_t lock;
@@ -2314,6 +2321,7 @@ bool scx_consume_dispatch_q(struct scx_sched *sch, struct rq *rq,
struct scx_dispatch_q *dsq, u64 enq_flags);
bool scx_consume_global_dsq(struct scx_sched *sch, struct rq *rq);
bool scx_rq_online(struct rq *rq);
+bool scx_in_dump(void);
void scx_flush_dispatch_buf(struct scx_sched *sch, struct rq *rq);
s32 scx_init_dsq(struct scx_dispatch_q *dsq, u64 dsq_id, struct scx_sched *sch);
__printf(2, 3) void scx_dump_line(struct seq_buf *s, const char *fmt, ...);
diff --git a/kernel/sched/ext/sub.c b/kernel/sched/ext/sub.c
index 7c069a289766..d535b9258b10 100644
--- a/kernel/sched/ext/sub.c
+++ b/kernel/sched/ext/sub.c
@@ -66,7 +66,10 @@ static DECLARE_WAIT_QUEUE_HEAD(ecaps_offline_waitq);
static struct cpumask scx_frozen_cpus;
static DECLARE_WAIT_QUEUE_HEAD(scx_frozen_cpus_waitq);
-/* between the PREPARE and POST notifications of a PM cycle */
+/*
+ * True between the PREPARE and POST notifications of a PM cycle. Updated under
+ * caps_updated_deferred_lock.
+ */
static bool scx_pm_in_progress;
/**
@@ -307,6 +310,47 @@ void set_cgroup_sched(struct cgroup *cgrp, struct scx_sched *sch)
rcu_assign_pointer(pos->scx_sched, sch);
}
+/*
+ * Queue of ops.sub_caps_updated() deliveries deferred out of SCX op contexts.
+ * Queued entries stay linked on scx_caps_updated->node_in_flight so that
+ * caps_updated_record() keeps coalescing into them.
+ *
+ * Each cpu has its own hard irq_work:
+ * - A single irq_work queued on multiple cpus can run irq_work_single()
+ * concurrently and clear IRQ_WORK_BUSY early, breaking irq_work_sync().
+ * - A dying cpu still runs SCX ops after hotplug parks its irq_work thread,
+ * which would strand a regular irq_work on PREEMPT_RT.
+ */
+static void caps_updated_deferred_workfn(struct irq_work *irq_work);
+static LIST_HEAD(caps_updated_deferred);
+static DEFINE_RAW_SPINLOCK(caps_updated_deferred_lock);
+static DEFINE_PER_CPU(struct irq_work, caps_updated_deferred_irq_work) =
+ IRQ_WORK_INIT_HARD(caps_updated_deferred_workfn);
+
+/*
+ * Drop @sch's deferred caps_updated deliveries before freeing its pshards.
+ * scx_sched_free_rcu_work() calls this after two RCU grace periods post
+ * unlink, so scx_bpf_sub_grant/revoke() cannot queue new entries for @sch.
+ */
+static void caps_updated_cancel_deferred(struct scx_sched *sch)
+{
+ struct scx_caps_updated *cu, *tmp;
+ s32 cpu;
+
+ scoped_guard (raw_spinlock_irqsave, &caps_updated_deferred_lock) {
+ list_for_each_entry_safe(cu, tmp, &caps_updated_deferred, node_in_flight) {
+ if (container_of(cu, struct scx_pshard, caps_updated)->sch != sch)
+ continue;
+ guard(raw_spinlock)(&cu->lock);
+ list_del_init(&cu->node_in_flight);
+ }
+ }
+
+ /* wait for any workfn that already spliced @sch's entries */
+ for_each_possible_cpu(cpu)
+ irq_work_sync(per_cpu_ptr(&caps_updated_deferred_irq_work, cpu));
+}
+
static void free_pshard(struct scx_pshard *pshard)
{
struct scx_caps_updated *cu;
@@ -327,6 +371,7 @@ void scx_free_pshards(struct scx_sched *sch)
if (!sch->pshard)
return;
+ caps_updated_cancel_deferred(sch);
for (si = 0; si < sch->nr_pshards; si++)
free_pshard(sch->pshard[si]);
kfree(sch->pshard);
@@ -1034,6 +1079,77 @@ static void caps_updated_deliver(struct list_head *to_deliver)
}
}
+static void caps_updated_deferred_workfn(struct irq_work *irq_work)
+{
+ LIST_HEAD(to_deliver);
+
+ /* hard irq_work, same irq-disabled context as a synchronous delivery */
+ lockdep_assert_irqs_disabled();
+
+ scoped_guard (raw_spinlock, &caps_updated_deferred_lock)
+ list_splice_init(&caps_updated_deferred, &to_deliver);
+
+ caps_updated_deliver(&to_deliver);
+}
+
+static void caps_updated_kick_deferred(void)
+{
+ s32 cpu = smp_processor_id();
+
+ lockdep_assert_held(&caps_updated_deferred_lock);
+ lockdep_assert_irqs_disabled();
+
+ /*
+ * During the PM syscore phase, irq_work_queue() can malfunction.
+ * scx_sub_pm_end() kicks the queue when the PM cycle finishes.
+ */
+ if (unlikely(scx_pm_in_progress))
+ return;
+
+ if (likely(cpu_online(cpu))) {
+ irq_work_queue(this_cpu_ptr(&caps_updated_deferred_irq_work));
+ return;
+ }
+
+ /*
+ * When hotplug parks a dying cpu's migration thread, ops.update_idle()
+ * runs after smpcfd_dying_cpu() already drained local irq_work. Route
+ * to an online cpu; cpus_write_lock() keeps cpu_online_mask stable.
+ */
+ cpu = cpumask_first(cpu_online_mask);
+ if (likely(cpu < nr_cpu_ids))
+ irq_work_queue_on(per_cpu_ptr(&caps_updated_deferred_irq_work, cpu),
+ cpu);
+}
+
+/*
+ * If the caller runs in an SCX op context (an rq lock held, ops.select_cpu()
+ * or ops.dump*()), defer @to_deliver to an irq_work. Otherwise deliver it now.
+ *
+ * ops.sub_caps_updated() is called with a NULL locked_rq, which leaves
+ * scx_locked_rq() untouched. Delivering synchronously here would inherit the
+ * parent's scx_locked_rq() or dump state and hold the parent's rq lock, pi_lock
+ * or scx_dump_lock across any cascading grant/revoke down the subtree.
+ */
+static void caps_updated_deliver_or_defer(struct list_head *to_deliver)
+{
+ lockdep_assert_irqs_disabled();
+
+ if (likely(!scx_locked_rq() && !current->scx.kf_tasks[0] &&
+ !this_rq()->scx.in_select_cpu && !scx_in_dump())) {
+ caps_updated_deliver(to_deliver);
+ return;
+ }
+
+ if (list_empty(to_deliver))
+ return;
+
+ scoped_guard (raw_spinlock, &caps_updated_deferred_lock) {
+ list_splice_tail_init(to_deliver, &caps_updated_deferred);
+ caps_updated_kick_deferred();
+ }
+}
+
/*
* Deliver caps owed to @sch that couldn't be delivered earlier (e.g. a grant
* taken during its sub_attach(), before has_op was set). Called once has_op is
@@ -1325,6 +1441,7 @@ void __scx_process_sync_ecaps(struct rq *rq, struct task_struct *prev)
void scx_sub_pm_begin(void)
{
+ guard(raw_spinlock_irqsave)(&caps_updated_deferred_lock);
WRITE_ONCE(scx_pm_in_progress, true);
}
@@ -1376,7 +1493,11 @@ void scx_sub_pm_end(void)
cpumask_weight(&scx_frozen_cpus));
}
- WRITE_ONCE(scx_pm_in_progress, false);
+ scoped_guard (raw_spinlock_irqsave, &caps_updated_deferred_lock) {
+ WRITE_ONCE(scx_pm_in_progress, false);
+ if (!list_empty(&caps_updated_deferred))
+ caps_updated_kick_deferred();
+ }
cpumask_clear(&scx_frozen_cpus);
wake_up_all(&scx_frozen_cpus_waitq);
}
@@ -2981,7 +3102,7 @@ __bpf_kfunc s32 scx_bpf_sub_grant(u64 cgroup_id, u64 caps,
ret = any_denied ? -EPERM : 0;
out:
- caps_updated_deliver(&to_deliver);
+ caps_updated_deliver_or_defer(&to_deliver);
return ret;
}
@@ -3073,7 +3194,7 @@ __bpf_kfunc void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps,
}
}
- caps_updated_deliver(&to_deliver);
+ caps_updated_deliver_or_defer(&to_deliver);
}
/**
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
* [RFC PATCH 2/2] selftests/sched_ext: Test that ops.sub_caps_updated() runs unlocked
2026-11-25 15:34 [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: sub: Don't run ops.sub_caps_updated() in an SCX op context Kuba Piecuch
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 ` Kuba Piecuch
1 sibling, 0 replies; 3+ messages in thread
From: Kuba Piecuch @ 2026-11-25 15:34 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, sched-ext, linux-kernel
Add sub_caps_ctx. A cid-form root grants and revokes caps to a child
sub-scheduler from ops.enqueue() (SCX_CAP_PERF) and ops.select_cid()
(SCX_CAP_PREEMPT). The child calls scx_bpf_cidperf_set() on every cid from
ops.sub_caps_updated(), which aborts the child if the callback inherits
the parent's rq lock. The test is skipped on a single cpu, where the only
cid is the parent's rq.
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
tools/testing/selftests/sched_ext/Makefile | 1 +
.../selftests/sched_ext/sub_caps_ctx.bpf.c | 136 +++++++++++++++
.../selftests/sched_ext/sub_caps_ctx.c | 165 ++++++++++++++++++
3 files changed, 302 insertions(+)
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
diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index 9a776a8a36ff..caedba9dcde9 100644
--- a/tools/testing/selftests/sched_ext/Makefile
+++ b/tools/testing/selftests/sched_ext/Makefile
@@ -196,6 +196,7 @@ auto-test-targets := \
select_cpu_dispatch_bad_dsq \
select_cpu_dispatch_dbl_dsp \
select_cpu_vtime \
+ sub_caps_ctx \
rt_stall \
test_example \
total_bw \
diff --git a/tools/testing/selftests/sched_ext/sub_caps_ctx.bpf.c b/tools/testing/selftests/sched_ext/sub_caps_ctx.bpf.c
new file mode 100644
index 000000000000..e6613fc23d15
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_ctx.bpf.c
@@ -0,0 +1,136 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A cid-form root scheduler which grants and revokes caps to a child
+ * sub-scheduler from its rq-locked ops.enqueue() and from ops.select_cid(), and
+ * a child which calls scx_bpf_cidperf_set() on every cid from
+ * ops.sub_caps_updated().
+ *
+ * ops.sub_caps_updated() must not inherit the parent's rq lock context. If it
+ * did, scx_bpf_cidperf_set() on any cid other than the parent's locked rq's
+ * would fail with "Invalid target CPU" and abort the child.
+ */
+#include <scx/common.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+#define MAX_CIDS 1024
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 1 << 8);
+#if defined(__TARGET_ARCH_arm64) || defined(__aarch64__)
+ __ulong(map_extra, 0x1ull << 32);
+#else
+ __ulong(map_extra, 0x1ull << 44);
+#endif
+} arena SEC(".maps");
+
+struct sub_caps_ctx_cmask {
+ union {
+ struct scx_cmask mask;
+ u64 words[CMASK_NR_WORDS(MAX_CIDS) + 2];
+ };
+};
+
+struct sub_caps_ctx_cmask __arena_global all_cids;
+
+u64 child_cgid;
+u64 nr_enqueued, nr_selected, nr_grants, nr_revokes, nr_caps_updated, nr_perf_set;
+
+UEI_DEFINE(uei);
+
+/*
+ * Toggle @cap on all cids of the child every 8th call. ops.select_cid() and
+ * ops.enqueue() toggle different caps so that neither turns the other's grant
+ * or revoke into a no-op.
+ */
+static void toggle_child_cap(u64 cap, u64 seq)
+{
+ u64 cgid = child_cgid;
+
+ if (!cgid || (seq & 7) || nr_grants + nr_revokes >= 4096)
+ return;
+
+ if ((seq >> 3) & 1) {
+ scx_bpf_sub_revoke(cgid, cap, &all_cids.mask);
+ __sync_fetch_and_add(&nr_revokes, 1);
+ } else {
+ scx_bpf_sub_grant(cgid, cap, &all_cids.mask, NULL);
+ __sync_fetch_and_add(&nr_grants, 1);
+ }
+}
+
+/* grant / revoke from the ops.select_cid() context */
+s32 BPF_STRUCT_OPS(sub_caps_ctx_select_cid, struct task_struct *p, s32 prev_cid, u64 wake_flags)
+{
+ toggle_child_cap(SCX_CAP_PREEMPT, __sync_fetch_and_add(&nr_selected, 1));
+ return prev_cid;
+}
+
+/* grant / revoke while holding the enqueue rq lock */
+void BPF_STRUCT_OPS(sub_caps_ctx_enqueue, struct task_struct *p, u64 enq_flags)
+{
+ scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
+ toggle_child_cap(SCX_CAP_PERF, __sync_fetch_and_add(&nr_enqueued, 1));
+}
+
+void BPF_STRUCT_OPS(sub_caps_ctx_sub_caps_updated, const struct scx_cmask *cmask, u64 caps)
+{
+ u32 nr_cids = scx_bpf_nr_cids();
+ s32 cid;
+
+ /* @cmask is an arena pointer, associate the arena */
+ asm volatile("" :: "r"(&arena));
+
+ __sync_fetch_and_add(&nr_caps_updated, 1);
+
+ /* any cid must be accepted when no rq lock is held */
+ bpf_for(cid, 0, nr_cids) {
+ if (!scx_bpf_cidperf_set(cid, SCX_CPUPERF_ONE))
+ __sync_fetch_and_add(&nr_perf_set, 1);
+ }
+}
+
+s32 BPF_STRUCT_OPS(sub_caps_ctx_sub_attach, struct scx_sub_attach_args *args)
+{
+ child_cgid = args->ops->sub_cgroup_id;
+ return 0;
+}
+
+void BPF_STRUCT_OPS(sub_caps_ctx_sub_detach, struct scx_sub_detach_args *args)
+{
+ child_cgid = 0;
+}
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(sub_caps_ctx_init)
+{
+ u32 nr_cids = scx_bpf_nr_cids();
+ s32 cid;
+
+ if (nr_cids > MAX_CIDS) {
+ scx_bpf_error("nr_cids=%u exceeds MAX_CIDS=%d", nr_cids, MAX_CIDS);
+ return -EINVAL;
+ }
+
+ cmask_init(&all_cids.mask, 0, nr_cids);
+ bpf_for(cid, 0, nr_cids)
+ cmask_set(cid, &all_cids.mask);
+
+ return 0;
+}
+
+void BPF_STRUCT_OPS(sub_caps_ctx_exit, struct scx_exit_info *ei)
+{
+ UEI_RECORD(uei, ei);
+}
+
+SCX_OPS_CID_DEFINE(sub_caps_ctx_ops,
+ .select_cid = (void *)sub_caps_ctx_select_cid,
+ .enqueue = (void *)sub_caps_ctx_enqueue,
+ .sub_caps_updated = (void *)sub_caps_ctx_sub_caps_updated,
+ .sub_attach = (void *)sub_caps_ctx_sub_attach,
+ .sub_detach = (void *)sub_caps_ctx_sub_detach,
+ .init = (void *)sub_caps_ctx_init,
+ .exit = (void *)sub_caps_ctx_exit,
+ .name = "sub_caps_ctx");
diff --git a/tools/testing/selftests/sched_ext/sub_caps_ctx.c b/tools/testing/selftests/sched_ext/sub_caps_ctx.c
new file mode 100644
index 000000000000..5881a6312cb6
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_ctx.c
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Grant and revoke caps to a sub-scheduler from the parent's rq-locked
+ * ops.enqueue() and verify that the child's ops.sub_caps_updated() runs in an
+ * unlocked context.
+ */
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/wait.h>
+#include <bpf/bpf.h>
+#include <scx/common.h>
+
+/* opaque to userspace, must match the size in sub_caps_ctx.bpf.c */
+#define MAX_CIDS 1024
+struct sub_caps_ctx_cmask {
+ __u64 words[(MAX_CIDS + 63) / 64 + 1 + 2];
+};
+
+#include "sub_caps_ctx.bpf.skel.h"
+#include "scx_test.h"
+
+#define CGRP_ROOT "/sys/fs/cgroup"
+#define CGRP_PATH CGRP_ROOT "/sub_caps_ctx"
+#define NR_WORKERS 8
+
+struct ctx {
+ struct sub_caps_ctx *parent;
+ struct sub_caps_ctx *child;
+ struct bpf_link *parent_link;
+ struct bpf_link *child_link;
+};
+
+static struct ctx test_ctx;
+
+static void write_file(const char *path, const char *buf)
+{
+ FILE *f = fopen(path, "w");
+
+ if (!f)
+ return;
+ fputs(buf, f);
+ fclose(f);
+}
+
+static enum scx_test_status setup(void **ctx)
+{
+ struct sub_caps_ctx *skel;
+ struct stat st;
+
+ if (!__COMPAT_struct_has_field("sched_ext_ops_cid", "sub_caps_updated"))
+ return SCX_TEST_SKIP;
+
+ /* a single cid is always the parent's rq, which scx_bpf_cidperf_set() accepts */
+ if (sysconf(_SC_NPROCESSORS_ONLN) < 2)
+ return SCX_TEST_SKIP;
+
+ write_file(CGRP_ROOT "/cgroup.subtree_control", "+cpu");
+ if (mkdir(CGRP_PATH, 0755) && errno != EEXIST) {
+ fprintf(stderr, "Failed to create %s (%s)\n", CGRP_PATH, strerror(errno));
+ return SCX_TEST_SKIP;
+ }
+ SCX_FAIL_IF(stat(CGRP_PATH, &st), "Failed to stat %s", CGRP_PATH);
+
+ skel = SCX_OPS_CID_OPEN(sub_caps_ctx_ops, sub_caps_ctx);
+ SCX_OPS_LOAD(skel, sub_caps_ctx_ops, sub_caps_ctx, uei);
+ test_ctx.parent = skel;
+
+ skel = SCX_OPS_CID_OPEN(sub_caps_ctx_ops, sub_caps_ctx);
+ skel->struct_ops.sub_caps_ctx_ops->sub_cgroup_id = st.st_ino;
+ SCX_OPS_LOAD(skel, sub_caps_ctx_ops, sub_caps_ctx, uei);
+ test_ctx.child = skel;
+
+ *ctx = &test_ctx;
+ return SCX_TEST_PASS;
+}
+
+/* sleep and wake repeatedly to drive the parent's ops.enqueue() */
+static void worker(void)
+{
+ struct timespec ts = { .tv_nsec = 100 * 1000 };
+ int i;
+
+ for (i = 0; i < 2000; i++)
+ nanosleep(&ts, NULL);
+ exit(0);
+}
+
+static enum scx_test_status run(void *arg)
+{
+ struct ctx *ctx = arg;
+ pid_t pids[NR_WORKERS];
+ int i, status;
+
+ ctx->parent_link = bpf_map__attach_struct_ops(ctx->parent->maps.sub_caps_ctx_ops);
+ SCX_FAIL_IF(!ctx->parent_link, "Failed to attach parent");
+
+ ctx->child_link = bpf_map__attach_struct_ops(ctx->child->maps.sub_caps_ctx_ops);
+ SCX_FAIL_IF(!ctx->child_link, "Failed to attach child");
+ SCX_GT(ctx->parent->bss->child_cgid, 0);
+
+ for (i = 0; i < NR_WORKERS; i++) {
+ pids[i] = fork();
+ SCX_FAIL_IF(pids[i] < 0, "Failed to fork");
+ if (!pids[i])
+ worker();
+ }
+ for (i = 0; i < NR_WORKERS; i++)
+ SCX_FAIL_IF(waitpid(pids[i], &status, 0) != pids[i] || status,
+ "Worker %d failed", i);
+
+ /* let deferred notifications drain */
+ usleep(100 * 1000);
+
+ printf("enqueued=%lu selected=%lu grants=%lu revokes=%lu caps_updated=%lu perf_set=%lu\n",
+ (unsigned long)ctx->parent->bss->nr_enqueued,
+ (unsigned long)ctx->parent->bss->nr_selected,
+ (unsigned long)ctx->parent->bss->nr_grants,
+ (unsigned long)ctx->parent->bss->nr_revokes,
+ (unsigned long)ctx->child->bss->nr_caps_updated,
+ (unsigned long)ctx->child->bss->nr_perf_set);
+
+ SCX_ASSERT_ALIVE(ctx->child, uei);
+ SCX_ASSERT_ALIVE(ctx->parent, uei);
+ SCX_GT(ctx->parent->bss->nr_grants, 0);
+ SCX_GT(ctx->parent->bss->nr_revokes, 0);
+ SCX_GT(ctx->child->bss->nr_caps_updated, 0);
+
+ bpf_link__destroy(ctx->child_link);
+ ctx->child_link = NULL;
+ bpf_link__destroy(ctx->parent_link);
+ ctx->parent_link = NULL;
+
+ SCX_EQ(ctx->child->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG));
+ SCX_EQ(ctx->parent->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG));
+
+ return SCX_TEST_PASS;
+}
+
+static void cleanup(void *arg)
+{
+ struct ctx *ctx = arg;
+
+ if (ctx->child_link)
+ bpf_link__destroy(ctx->child_link);
+ if (ctx->parent_link)
+ bpf_link__destroy(ctx->parent_link);
+ sub_caps_ctx__destroy(ctx->child);
+ sub_caps_ctx__destroy(ctx->parent);
+ rmdir(CGRP_PATH);
+}
+
+struct scx_test sub_caps_ctx = {
+ .name = "sub_caps_ctx",
+ .description = "Check ops.sub_caps_updated() runs unlocked after a locked grant",
+ .setup = setup,
+ .run = run,
+ .cleanup = cleanup,
+};
+
+REGISTER_SCX_TEST(&sub_caps_ctx)
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-25 15:34 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 15:34 [RFC PATCHSET sched_ext/for-7.4-fixes] sched_ext: sub: Don't run ops.sub_caps_updated() in an SCX op context Kuba Piecuch
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
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®