[PATCH 1/2] sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated()

From: Kuba Piecuch

Date: Mon Nov 30 2026 - 10:53:36 EST


scx_bpf_sub_grant() and scx_bpf_sub_revoke() deliver ops.sub_caps_updated()
synchronously with locked_rq = NULL, while scx_sub_seed_caps() invokes it
unlocked. ops.sub_caps_updated() therefore either runs unlocked or inherits
the parent's rq lock or dump state, rather than providing a known locking
context.

ops.sub_caps_updated() currently permits every kfunc in scx_kfunc_ids_any.
As discussed on the list, ops.sub_caps_updated() is only for tracking held
caps and propagating grants and revokes to children, whereas acting on caps
belongs in ops.sub_ecaps_updated().

Add scx_kfunc_ids_no_sub_caps and reject kfuncs that depend on being called
from an rq-holding or dump context from ops.sub_caps_updated() at BPF load
time in scx_kfunc_context_filter().

Fixes: 5f2a9a4c2e6c ("sched_ext: Add coalescing sub_caps_updated() notifier for sub-schedulers")
Cc: stable@xxxxxxxxxxxxxxx # v7.3+
Link: https://lore.kernel.org/all/aw2KiAUML23tN-L3@xxxxxxxxxxxxxxx/
Signed-off-by: Kuba Piecuch <jpiecuch@xxxxxxxxxx>
---
kernel/sched/ext/ext.c | 49 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index a4a93ed97f7b..b8e5251edd2b 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -12280,6 +12280,42 @@ BTF_ID_FLAGS(func, bpf_iter_scx_dsq_next, KF_ITER_NEXT | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_iter_scx_dsq_destroy, KF_ITER_DESTROY)
BTF_KFUNCS_END(scx_kfunc_ids_no_tracing)

+/*
+ * Kfuncs forbidden from ops.sub_caps_updated().
+ *
+ * ops.sub_caps_updated() runs synchronously from the parent's
+ * scx_bpf_sub_grant() / scx_bpf_sub_revoke() (inheriting whatever rq lock or
+ * dump state the parent holds) or unlocked from scx_sub_seed_caps(). It is
+ * meant only for tracking held caps and propagating grants/revokes to
+ * children; acting on caps belongs in ops.sub_ecaps_updated().
+ *
+ * - scx_bpf_cidperf_set() consults scx_locked_rq() and aborts when it points
+ * to another CPU's rq, or acquires the target rq lock when unlocked.
+ * (scx_bpf_cpuperf_set() is already rejected because ops.sub_caps_updated()
+ * only attaches on cid-form schedulers, where scx_kfunc_ids_cpu_only forbids
+ * it.)
+ * - scx_bpf_locked_rq() aborts when unlocked and exposes the parent's rq when
+ * locked.
+ * - scx_bpf_dsq_nr_queued(), scx_bpf_dsq_reenq(),
+ * scx_bpf_reenqueue_local___v2(), and bpf_iter_scx_dsq_destroy() consult
+ * scx_locked_rq(). Excluding bpf_iter_scx_dsq_destroy() also rejects
+ * bpf_for_each(scx_dsq, ...) loops.
+ * - scx_bpf_dump_bstr() checks scx_dump_data.cpu, aborting outside dump
+ * contexts and writing into the parent's dump when called from ops.dump*().
+ *
+ * Members must stay in scx_kfunc_ids_any and carry matching KF_* flags (see
+ * the flag-intersection comment below).
+ */
+BTF_KFUNCS_START(scx_kfunc_ids_no_sub_caps)
+BTF_ID_FLAGS(func, scx_bpf_dsq_nr_queued, KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, scx_bpf_dsq_reenq, KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, scx_bpf_reenqueue_local___v2, KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, bpf_iter_scx_dsq_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, scx_bpf_dump_bstr, KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, scx_bpf_cidperf_set, KF_IMPLICIT_ARGS)
+BTF_ID_FLAGS(func, scx_bpf_locked_rq, KF_IMPLICIT_ARGS | KF_RET_NULL)
+BTF_KFUNCS_END(scx_kfunc_ids_no_sub_caps)
+
/*
* cpu-form kfuncs that are forbidden from cid-form schedulers
* (bpf_sched_ext_ops_cid). Programs targeting the cid struct_ops type must
@@ -12393,6 +12429,7 @@ int scx_kfunc_context_filter(const struct bpf_prog *prog, u32 kfunc_id)
bool in_cpu_only = btf_id_set8_contains(&scx_kfunc_ids_cpu_only, kfunc_id);
bool in_cid = btf_id_set8_contains(&scx_kfunc_ids_cid, kfunc_id);
bool in_no_tracing = btf_id_set8_contains(&scx_kfunc_ids_no_tracing, kfunc_id);
+ bool in_no_sub_caps = btf_id_set8_contains(&scx_kfunc_ids_no_sub_caps, kfunc_id);
u32 moff, flags;

/* Not an SCX kfunc - allow. */
@@ -12439,11 +12476,21 @@ int scx_kfunc_context_filter(const struct bpf_prog *prog, u32 kfunc_id)
if (prog->aux->st_ops == &bpf_sched_ext_ops_cid && in_cpu_only)
return -EACCES;

+ moff = prog->aux->attach_st_ops_member_off;
+
+ /*
+ * ops.sub_caps_updated() inherits the caller's locking and dump state
+ * or runs unlocked. Reject kfuncs that depend on scx_locked_rq() or
+ * scx_dump_data.
+ */
+ if (moff == offsetof(struct sched_ext_ops, sub_caps_updated) &&
+ in_no_sub_caps)
+ return -EACCES;
+
/* SCX struct_ops: check the per-op allow list. */
if (in_any || in_idle || in_cid)
return 0;

- moff = prog->aux->attach_st_ops_member_off;
flags = scx_kf_allow_flags[SCX_MOFF_IDX(moff)];

if ((flags & SCX_KF_ALLOW_UNLOCKED) && in_unlocked)
--
2.56.1.839.g22a8290db9-goog