* [PATCH 1/2] sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated()
2026-11-30 15:53 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated() Kuba Piecuch
@ 2026-11-30 15:53 ` Kuba Piecuch
2026-11-30 15:53 ` [PATCH 2/2] selftests/sched_ext: Add sub_caps_kfunc test Kuba Piecuch
2026-11-30 16:59 ` [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated() Tejun Heo
2 siblings, 0 replies; 4+ messages in thread
From: Kuba Piecuch @ 2026-11-30 15:53 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() 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@vger.kernel.org # v7.3+
Link: https://lore.kernel.org/all/aw2KiAUML23tN-L3@slm.duckdns.org/
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/2] selftests/sched_ext: Add sub_caps_kfunc test
2026-11-30 15:53 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated() Kuba Piecuch
2026-11-30 15:53 ` [PATCH 1/2] " Kuba Piecuch
@ 2026-11-30 15:53 ` Kuba Piecuch
2026-11-30 16:59 ` [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated() Tejun Heo
2 siblings, 0 replies; 4+ messages in thread
From: Kuba Piecuch @ 2026-11-30 15:53 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, sched-ext, linux-kernel
Add the sub_caps_kfunc selftest to verify BPF verifier restrictions on
ops.sub_caps_updated().
A positive cid-form scheduler verifies that ops.sub_caps_updated() can
still call sub-scheduler cap and context-free kfuncs (scx_bpf_sub_caps(),
scx_bpf_sub_grant(), scx_bpf_sub_revoke(), scx_bpf_sub_kill(),
scx_bpf_nr_cids(), scx_bpf_kick_cid(), scx_bpf_dsq_peek(),
scx_bpf_task_set_slice(), and scx_bpf_destroy_dsq()) and that
ops.sub_ecaps_updated() can still call scx_bpf_cidperf_set().
Seven negative test cases verify that the verifier rejects
scx_bpf_cidperf_set(), scx_bpf_locked_rq(), scx_bpf_dsq_nr_queued(),
scx_bpf_dsq_reenq(), scx_bpf_reenqueue_local___v2(),
bpf_for_each(scx_dsq, ...), and scx_bpf_dump() from ops.sub_caps_updated()
with -EACCES.
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
tools/testing/selftests/sched_ext/Makefile | 1 +
.../selftests/sched_ext/sub_caps_kfunc.bpf.c | 66 +++++++++++
.../selftests/sched_ext/sub_caps_kfunc.c | 107 +++++++++++++++++
.../sched_ext/sub_caps_kfunc_fail.bpf.c | 109 ++++++++++++++++++
4 files changed, 283 insertions(+)
create mode 100644 tools/testing/selftests/sched_ext/sub_caps_kfunc.bpf.c
create mode 100644 tools/testing/selftests/sched_ext/sub_caps_kfunc.c
create mode 100644 tools/testing/selftests/sched_ext/sub_caps_kfunc_fail.bpf.c
diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index fac8b26c4d70..47dafa4c08c4 100644
--- a/tools/testing/selftests/sched_ext/Makefile
+++ b/tools/testing/selftests/sched_ext/Makefile
@@ -197,6 +197,7 @@ auto-test-targets := \
select_cpu_dispatch_dbl_dsp \
select_cpu_vtime \
rt_stall \
+ sub_caps_kfunc \
sub_core_caps \
test_example \
total_bw \
diff --git a/tools/testing/selftests/sched_ext/sub_caps_kfunc.bpf.c b/tools/testing/selftests/sched_ext/sub_caps_kfunc.bpf.c
new file mode 100644
index 000000000000..05c590c63738
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_kfunc.bpf.c
@@ -0,0 +1,66 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A cid-form scheduler that calls allowed kfuncs from ops.sub_caps_updated()
+ * and calls scx_bpf_cidperf_set() from ops.sub_ecaps_updated().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ 0
+#define MAX_CIDS 1024
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 1 << 4);
+} arena SEC(".maps");
+
+struct sub_caps_kfunc_cmask {
+ union {
+ struct scx_cmask mask;
+ u64 words[CMASK_NR_WORDS(MAX_CIDS) + 2];
+ };
+};
+
+struct sub_caps_kfunc_cmask __arena_global caps_mask;
+
+void BPF_STRUCT_OPS(sub_caps_kfunc_sub_caps_updated, const struct scx_cmask *cmask, u64 caps)
+{
+ struct task_struct *p;
+
+ /* @cmask is an arena pointer, associate the arena */
+ asm volatile("" :: "r"(&arena));
+
+ if (scx_bpf_nr_cids() > MAX_CIDS) {
+ scx_bpf_error("too many cids");
+ return;
+ }
+
+ scx_bpf_sub_caps(0, SCX_CAP_ENQ, &caps_mask.mask);
+ scx_bpf_sub_grant(0, SCX_CAP_ENQ, &caps_mask.mask, NULL);
+ scx_bpf_sub_revoke(0, SCX_CAP_ENQ, &caps_mask.mask);
+ scx_bpf_sub_kill(0, "sub_caps_kfunc");
+ scx_bpf_kick_cid(0, 0);
+
+ bpf_rcu_read_lock();
+ p = scx_bpf_dsq_peek(SHARED_DSQ);
+ if (p)
+ scx_bpf_task_set_slice(p, SCX_SLICE_DFL);
+ bpf_rcu_read_unlock();
+
+ scx_bpf_destroy_dsq(SHARED_DSQ);
+}
+
+void BPF_STRUCT_OPS(sub_caps_kfunc_sub_ecaps_updated, s32 cid, u64 before, u64 after)
+{
+ scx_bpf_cidperf_set(cid, SCX_CPUPERF_ONE);
+}
+
+SCX_OPS_CID_DEFINE(sub_caps_kfunc_ops,
+ .sub_caps_updated = (void *)sub_caps_kfunc_sub_caps_updated,
+ .sub_ecaps_updated = (void *)sub_caps_kfunc_sub_ecaps_updated,
+ .name = "sub_caps_kfunc");
diff --git a/tools/testing/selftests/sched_ext/sub_caps_kfunc.c b/tools/testing/selftests/sched_ext/sub_caps_kfunc.c
new file mode 100644
index 000000000000..c689538d8594
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_kfunc.c
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects rq-lock-sensitive and dump kfuncs from
+ * ops.sub_caps_updated(), while still allowing sub-scheduler cap propagation
+ * and context-free kfuncs there (and scx_bpf_cidperf_set() from
+ * ops.sub_ecaps_updated()).
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+#include <bpf/bpf.h>
+#include <errno.h>
+#include <scx/common.h>
+#include "scx_test.h"
+
+/* opaque to userspace, must match the size in sub_caps_kfunc.bpf.c */
+#define MAX_CIDS 1024
+struct sub_caps_kfunc_cmask {
+ __u64 words[(MAX_CIDS + 63) / 64 + 1 + 2];
+};
+
+#include "sub_caps_kfunc.bpf.skel.h"
+#include "sub_caps_kfunc_fail.bpf.skel.h"
+
+struct fail_case {
+ const char *name;
+ size_t map_off;
+ size_t prog_off;
+};
+
+#define FAIL_CASE(id) { \
+ .name = #id, \
+ .map_off = offsetof(struct sub_caps_kfunc_fail, maps.id##_ops), \
+ .prog_off = offsetof(struct sub_caps_kfunc_fail, progs.id##_cb), \
+}
+
+static const struct fail_case fail_cases[] = {
+ FAIL_CASE(fail_cidperf),
+ FAIL_CASE(fail_locked_rq),
+ FAIL_CASE(fail_dsq_nr_queued),
+ FAIL_CASE(fail_dsq_reenq),
+ FAIL_CASE(fail_reenq_local),
+ FAIL_CASE(fail_dsq_iter),
+ FAIL_CASE(fail_dump),
+ {},
+};
+
+static struct bpf_map *case_map(struct sub_caps_kfunc_fail *skel, size_t off)
+{
+ return *(struct bpf_map **)((char *)skel + off);
+}
+
+static struct bpf_program *case_prog(struct sub_caps_kfunc_fail *skel, size_t off)
+{
+ return *(struct bpf_program **)((char *)skel + off);
+}
+
+static enum scx_test_status run(void *ctx)
+{
+ struct sub_caps_kfunc *skel;
+ int err, i, j;
+
+ if (!__COMPAT_struct_has_field("sched_ext_ops_cid", "sub_caps_updated") ||
+ !__COMPAT_has_ksym("scx_bpf_sub_grant"))
+ return SCX_TEST_SKIP;
+
+ skel = sub_caps_kfunc__open();
+ SCX_FAIL_IF(!skel, "Failed to open sub_caps_kfunc");
+ SCX_ENUM_INIT(skel);
+ err = sub_caps_kfunc__load(skel);
+ sub_caps_kfunc__destroy(skel);
+ SCX_FAIL_IF(err, "Failed to load sub_caps_kfunc (%d)", err);
+
+ for (i = 0; fail_cases[i].name; i++) {
+ struct sub_caps_kfunc_fail *fail_skel;
+
+ fail_skel = sub_caps_kfunc_fail__open();
+ SCX_FAIL_IF(!fail_skel, "Failed to open %s", fail_cases[i].name);
+ SCX_ENUM_INIT(fail_skel);
+
+ for (j = 0; fail_cases[j].name; j++) {
+ if (j == i)
+ continue;
+ bpf_map__set_autocreate(case_map(fail_skel, fail_cases[j].map_off),
+ false);
+ bpf_program__set_autoload(case_prog(fail_skel, fail_cases[j].prog_off),
+ false);
+ }
+
+ err = sub_caps_kfunc_fail__load(fail_skel);
+ sub_caps_kfunc_fail__destroy(fail_skel);
+ SCX_FAIL_IF(!err, "%s loaded when it should have been rejected",
+ fail_cases[i].name);
+ SCX_FAIL_IF(err != -EACCES,
+ "Expected -EACCES loading %s, got %d",
+ fail_cases[i].name, err);
+ }
+
+ return SCX_TEST_PASS;
+}
+
+struct scx_test sub_caps_kfunc = {
+ .name = "sub_caps_kfunc",
+ .description = "Verify verifier kfunc restrictions on ops.sub_caps_updated()",
+ .run = run,
+};
+
+REGISTER_SCX_TEST(&sub_caps_kfunc)
diff --git a/tools/testing/selftests/sched_ext/sub_caps_kfunc_fail.bpf.c b/tools/testing/selftests/sched_ext/sub_caps_kfunc_fail.bpf.c
new file mode 100644
index 000000000000..c33e030c62df
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/sub_caps_kfunc_fail.bpf.c
@@ -0,0 +1,109 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects rq-lock-sensitive and dump kfuncs from
+ * ops.sub_caps_updated().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+#include <scx/compat.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARENA);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, 1 << 4);
+} arena SEC(".maps");
+
+static __always_inline void assoc_arena(void)
+{
+ /*
+ * ops.sub_caps_updated() takes an arena pointer (@cmask), so
+ * check_attach_btf_id() rejects the program with -EINVAL before
+ * scx_kfunc_context_filter() runs unless an arena map is associated.
+ * Reference the arena so verification reaches the kfunc context check
+ * and fails with -EACCES.
+ */
+ asm volatile("" :: "r"(&arena));
+}
+
+void BPF_STRUCT_OPS(fail_cidperf_cb, const struct scx_cmask *cmask, u64 caps)
+{
+ assoc_arena();
+ scx_bpf_cidperf_set(0, SCX_CPUPERF_ONE);
+}
+
+SCX_OPS_CID_DEFINE(fail_cidperf_ops,
+ .sub_caps_updated = (void *)fail_cidperf_cb,
+ .name = "sub_caps_fail_cidperf");
+
+void BPF_STRUCT_OPS(fail_locked_rq_cb, const struct scx_cmask *cmask, u64 caps)
+{
+ assoc_arena();
+ if (scx_bpf_locked_rq())
+ bpf_printk("locked rq\n");
+}
+
+SCX_OPS_CID_DEFINE(fail_locked_rq_ops,
+ .sub_caps_updated = (void *)fail_locked_rq_cb,
+ .name = "sub_caps_fail_locked_rq");
+
+void BPF_STRUCT_OPS(fail_dsq_nr_queued_cb, const struct scx_cmask *cmask, u64 caps)
+{
+ assoc_arena();
+ if (scx_bpf_dsq_nr_queued(SCX_DSQ_LOCAL) > 0)
+ bpf_printk("queued\n");
+}
+
+SCX_OPS_CID_DEFINE(fail_dsq_nr_queued_ops,
+ .sub_caps_updated = (void *)fail_dsq_nr_queued_cb,
+ .name = "sub_caps_fail_dsq_nr_queued");
+
+void BPF_STRUCT_OPS(fail_dsq_reenq_cb, const struct scx_cmask *cmask, u64 caps)
+{
+ assoc_arena();
+ scx_bpf_dsq_reenq(SCX_DSQ_LOCAL, 0);
+}
+
+SCX_OPS_CID_DEFINE(fail_dsq_reenq_ops,
+ .sub_caps_updated = (void *)fail_dsq_reenq_cb,
+ .name = "sub_caps_fail_dsq_reenq");
+
+void BPF_STRUCT_OPS(fail_reenq_local_cb, const struct scx_cmask *cmask, u64 caps)
+{
+ assoc_arena();
+ scx_bpf_reenqueue_local___v2___compat();
+}
+
+SCX_OPS_CID_DEFINE(fail_reenq_local_ops,
+ .sub_caps_updated = (void *)fail_reenq_local_cb,
+ .name = "sub_caps_fail_reenq_local");
+
+void BPF_STRUCT_OPS(fail_dsq_iter_cb, const struct scx_cmask *cmask, u64 caps)
+{
+ struct task_struct *p;
+
+ assoc_arena();
+ bpf_rcu_read_lock();
+ bpf_for_each(scx_dsq, p, 0, 0) {
+ if (p)
+ break;
+ }
+ bpf_rcu_read_unlock();
+}
+
+SCX_OPS_CID_DEFINE(fail_dsq_iter_ops,
+ .sub_caps_updated = (void *)fail_dsq_iter_cb,
+ .name = "sub_caps_fail_dsq_iter");
+
+void BPF_STRUCT_OPS(fail_dump_cb, const struct scx_cmask *cmask, u64 caps)
+{
+ assoc_arena();
+ scx_bpf_dump("caps=0x%llx\n", caps);
+}
+
+SCX_OPS_CID_DEFINE(fail_dump_ops,
+ .sub_caps_updated = (void *)fail_dump_cb,
+ .name = "sub_caps_fail_dump");
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply [flat|nested] 4+ messages in thread