* [PATCH 1/2] sched_ext: Don't allow unlocked kfuncs from ops.dump() and ops.init_cids()
2026-11-23 13:55 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Don't allow unlocked kfuncs from ops.dump() and ops.init_cids() Kuba Piecuch
@ 2026-11-23 13:55 ` Kuba Piecuch
2026-11-23 13:55 ` [PATCH 2/2] selftests/sched_ext: Add unlocked_kfunc test Kuba Piecuch
1 sibling, 0 replies; 3+ messages in thread
From: Kuba Piecuch @ 2026-11-23 13:55 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, sched-ext, linux-kernel
scx_kf_allow_flags[] grants SCX_KF_ALLOW_UNLOCKED to ops.dump(). This
allows ops.dump() to call scx_bpf_dsq_move*(), scx_bpf_dsq_reenq_task(),
and scx_bpf_select_cpu_*(). When no rq is locked, these kfuncs acquire
rq, DSQ, and pi locks and may invoke nested SCX ops such as
ops.dequeue().
scx_dump_state() runs ops.dump() with IRQs disabled under scx_dump_lock.
Callers include the hardirq disable irq_work and SysRq-D. With
SCX_KF_ALLOW_UNLOCKED, a debug dump can migrate tasks and run nested
scheduler callbacks instead of only inspecting state. ops.dump_cpu() and
ops.dump_task() already deny these kfuncs. Remove SCX_KF_ALLOW_UNLOCKED
from ops.dump().
ops.init_cids() runs before the cid tables are published and before the
arena pool and pshards are set up. Its only job is to finalize the cid
layout. SCX_KF_ALLOW_UNLOCKED also gave it scx_bpf_create_dsq(),
scx_bpf_dsq_move*(), and scx_bpf_dsq_reenq_task(). Restrict
ops.init_cids() to SCX_KF_ALLOW_INIT_CIDS.
No in-tree scheduler calls these kfuncs from either op.
Fixes: 4c30f5ce4f7a ("sched_ext: Implement scx_bpf_dispatch[_vtime]_from_dsq()")
Fixes: 874fdc0e86e4 ("sched_ext: Add ops.init_cids() to finalize the cid layout before init")
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
kernel/sched/ext/ext.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index e8c30a21e03e..6274ea04fc6f 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -12293,7 +12293,6 @@ static const u32 scx_kf_allow_flags[] = {
[SCX_OP_IDX(dispatch)] = SCX_KF_ALLOW_ENQUEUE | SCX_KF_ALLOW_DISPATCH,
[SCX_OP_IDX(cpu_release)] = SCX_KF_ALLOW_CPU_RELEASE,
[SCX_OP_IDX(init_task)] = SCX_KF_ALLOW_UNLOCKED,
- [SCX_OP_IDX(dump)] = SCX_KF_ALLOW_UNLOCKED,
#ifdef CONFIG_EXT_GROUP_SCHED
[SCX_OP_IDX(cgroup_init)] = SCX_KF_ALLOW_UNLOCKED,
[SCX_OP_IDX(cgroup_exit)] = SCX_KF_ALLOW_UNLOCKED,
@@ -12309,7 +12308,12 @@ static const u32 scx_kf_allow_flags[] = {
[SCX_OP_IDX(sub_child_ecaps_updated)] = SCX_KF_ALLOW_ENQUEUE | SCX_KF_ALLOW_DISPATCH,
[SCX_OP_IDX(cpu_online)] = SCX_KF_ALLOW_UNLOCKED,
[SCX_OP_IDX(cpu_offline)] = SCX_KF_ALLOW_UNLOCKED,
- [SCX_OP_IDX(init_cids)] = SCX_KF_ALLOW_UNLOCKED | SCX_KF_ALLOW_INIT_CIDS,
+ /*
+ * ops.init_cids() runs before the cid tables are published and before
+ * the arena pool and pshards are allocated. Limit it to finalizing the
+ * cid layout; everything else belongs in ops.init().
+ */
+ [SCX_OP_IDX(init_cids)] = SCX_KF_ALLOW_INIT_CIDS,
[SCX_OP_IDX(init)] = SCX_KF_ALLOW_UNLOCKED,
[SCX_OP_IDX(exit)] = SCX_KF_ALLOW_UNLOCKED,
};
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 3+ messages in thread* [PATCH 2/2] selftests/sched_ext: Add unlocked_kfunc test
2026-11-23 13:55 [PATCHSET sched_ext/for-7.4-fixes] sched_ext: Don't allow unlocked kfuncs from ops.dump() and ops.init_cids() Kuba Piecuch
2026-11-23 13:55 ` [PATCH 1/2] " Kuba Piecuch
@ 2026-11-23 13:55 ` Kuba Piecuch
1 sibling, 0 replies; 3+ messages in thread
From: Kuba Piecuch @ 2026-11-23 13:55 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min
Cc: Kuba Piecuch, sched-ext, linux-kernel
Add the unlocked_kfunc test to verify verifier-time restrictions on the
SCX_KF_ALLOW_UNLOCKED kfuncs.
The valid cpu-form and cid-form schedulers verify that ops.init() and
ops.exit() can call scx_bpf_create_dsq(), scx_bpf_dsq_move(),
scx_bpf_dsq_reenq_task(), and scx_bpf_select_cpu_dfl(). They also verify
that ops.dump() can call scx_bpf_dump() and that ops.init_cids() can
call scx_bpf_cid_override().
The two negative schedulers verify that the verifier rejects
scx_bpf_dsq_move() from ops.dump() and scx_bpf_create_dsq() from
ops.init_cids() with -EACCES.
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
tools/testing/selftests/sched_ext/Makefile | 1 +
.../selftests/sched_ext/unlocked_kfunc.bpf.c | 69 ++++++++++++++++++
.../selftests/sched_ext/unlocked_kfunc.c | 73 +++++++++++++++++++
.../sched_ext/unlocked_kfunc_cid.bpf.c | 67 +++++++++++++++++
.../sched_ext/unlocked_kfunc_fail_dump.bpf.c | 26 +++++++
.../unlocked_kfunc_fail_init_cids.bpf.c | 22 ++++++
6 files changed, 258 insertions(+)
create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc.bpf.c
create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc.c
create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc_cid.bpf.c
create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc_fail_dump.bpf.c
create mode 100644 tools/testing/selftests/sched_ext/unlocked_kfunc_fail_init_cids.bpf.c
diff --git a/tools/testing/selftests/sched_ext/Makefile b/tools/testing/selftests/sched_ext/Makefile
index c5d3a2eaea7d..9a776a8a36ff 100644
--- a/tools/testing/selftests/sched_ext/Makefile
+++ b/tools/testing/selftests/sched_ext/Makefile
@@ -199,6 +199,7 @@ auto-test-targets := \
rt_stall \
test_example \
total_bw \
+ unlocked_kfunc \
cyclic_kick_wait \
testcase-targets := $(addsuffix .o,$(addprefix $(SCXOBJ_DIR)/,$(auto-test-targets)))
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc.bpf.c b/tools/testing/selftests/sched_ext/unlocked_kfunc.bpf.c
new file mode 100644
index 000000000000..194993a0ef07
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc.bpf.c
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A cpu-form scheduler that calls SCX_KF_ALLOW_UNLOCKED kfuncs from ops.init()
+ * and ops.exit(). ops.dump() calls scx_bpf_dump().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ 0
+
+char _license[] SEC("license") = "GPL";
+
+static void drain_dsq(void)
+{
+ struct task_struct *p;
+
+ bpf_rcu_read_lock();
+ bpf_for_each(scx_dsq, p, SHARED_DSQ, 0) {
+ if (p->pid & 1)
+ scx_bpf_dsq_reenq_task(BPF_FOR_EACH_ITER, p);
+ else
+ scx_bpf_dsq_move(BPF_FOR_EACH_ITER, p, SCX_DSQ_GLOBAL, 0);
+ }
+ bpf_rcu_read_unlock();
+}
+
+static void select_cpu_for_current(void)
+{
+ struct task_struct *p = bpf_get_current_task_btf();
+ bool is_idle;
+
+ bpf_rcu_read_lock();
+ scx_bpf_select_cpu_dfl(p, bpf_get_smp_processor_id(), 0, &is_idle);
+ bpf_rcu_read_unlock();
+}
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(unlocked_kfunc_init)
+{
+ s32 ret;
+
+ ret = scx_bpf_create_dsq(SHARED_DSQ, -1);
+ if (ret)
+ return ret;
+
+ drain_dsq();
+ select_cpu_for_current();
+ return 0;
+}
+
+void BPF_STRUCT_OPS(unlocked_kfunc_exit, struct scx_exit_info *ei)
+{
+ drain_dsq();
+ select_cpu_for_current();
+}
+
+void BPF_STRUCT_OPS(unlocked_kfunc_dump, struct scx_dump_ctx *dctx)
+{
+ scx_bpf_dump("nr_cpu_ids=%u\n", scx_bpf_nr_cpu_ids());
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops unlocked_kfunc_ops = {
+ .init = (void *)unlocked_kfunc_init,
+ .exit = (void *)unlocked_kfunc_exit,
+ .dump = (void *)unlocked_kfunc_dump,
+ .name = "unlocked_kfunc",
+};
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc.c b/tools/testing/selftests/sched_ext/unlocked_kfunc.c
new file mode 100644
index 000000000000..0052c608365d
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects SCX_KF_ALLOW_UNLOCKED kfuncs from
+ * ops.dump() and ops.init_cids(). Unlocked ops such as ops.init() and
+ * ops.exit() must still be allowed to call them.
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+#include <bpf/bpf.h>
+#include <errno.h>
+#include <scx/common.h>
+#include "scx_test.h"
+#include "unlocked_kfunc.bpf.skel.h"
+#include "unlocked_kfunc_cid.bpf.skel.h"
+#include "unlocked_kfunc_fail_dump.bpf.skel.h"
+#include "unlocked_kfunc_fail_init_cids.bpf.skel.h"
+
+static enum scx_test_status run(void *ctx)
+{
+ struct unlocked_kfunc *skel;
+ struct unlocked_kfunc_cid *cid_skel;
+ struct unlocked_kfunc_fail_dump *fail_dump;
+ struct unlocked_kfunc_fail_init_cids *fail_init_cids;
+ int err;
+
+ if (!__COMPAT_struct_has_field("sched_ext_ops_cid", "init_cids") ||
+ !__COMPAT_has_ksym("scx_bpf_dsq_reenq_task"))
+ return SCX_TEST_SKIP;
+
+ skel = unlocked_kfunc__open();
+ SCX_FAIL_IF(!skel, "Failed to open unlocked_kfunc");
+ SCX_ENUM_INIT(skel);
+ err = unlocked_kfunc__load(skel);
+ unlocked_kfunc__destroy(skel);
+ SCX_FAIL_IF(err, "Failed to load unlocked_kfunc (%d)", err);
+
+ cid_skel = unlocked_kfunc_cid__open();
+ SCX_FAIL_IF(!cid_skel, "Failed to open unlocked_kfunc_cid");
+ SCX_ENUM_INIT(cid_skel);
+ err = unlocked_kfunc_cid__load(cid_skel);
+ unlocked_kfunc_cid__destroy(cid_skel);
+ SCX_FAIL_IF(err, "Failed to load unlocked_kfunc_cid (%d)", err);
+
+ fail_dump = unlocked_kfunc_fail_dump__open();
+ SCX_FAIL_IF(!fail_dump, "Failed to open unlocked_kfunc_fail_dump");
+ SCX_ENUM_INIT(fail_dump);
+ err = unlocked_kfunc_fail_dump__load(fail_dump);
+ unlocked_kfunc_fail_dump__destroy(fail_dump);
+ SCX_FAIL_IF(!err,
+ "unlocked_kfunc_fail_dump loaded when it should have been rejected");
+ SCX_FAIL_IF(err != -EACCES,
+ "Expected -EACCES loading unlocked_kfunc_fail_dump, got %d", err);
+
+ fail_init_cids = unlocked_kfunc_fail_init_cids__open();
+ SCX_FAIL_IF(!fail_init_cids, "Failed to open unlocked_kfunc_fail_init_cids");
+ SCX_ENUM_INIT(fail_init_cids);
+ err = unlocked_kfunc_fail_init_cids__load(fail_init_cids);
+ unlocked_kfunc_fail_init_cids__destroy(fail_init_cids);
+ SCX_FAIL_IF(!err,
+ "unlocked_kfunc_fail_init_cids loaded when it should have been rejected");
+ SCX_FAIL_IF(err != -EACCES,
+ "Expected -EACCES loading unlocked_kfunc_fail_init_cids, got %d", err);
+
+ return SCX_TEST_PASS;
+}
+
+struct scx_test unlocked_kfunc = {
+ .name = "unlocked_kfunc",
+ .description = "Verify verifier kfunc restrictions on ops.dump() and ops.init_cids()",
+ .run = run,
+};
+
+REGISTER_SCX_TEST(&unlocked_kfunc)
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc_cid.bpf.c b/tools/testing/selftests/sched_ext/unlocked_kfunc_cid.bpf.c
new file mode 100644
index 000000000000..ffe2f03c42e3
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc_cid.bpf.c
@@ -0,0 +1,67 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * A cid-form scheduler that calls scx_bpf_cid_override() from ops.init_cids()
+ * and SCX_KF_ALLOW_UNLOCKED kfuncs from ops.init(). ops.dump() calls
+ * scx_bpf_dump().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ 0
+#define MAX_CPUS 512
+
+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");
+
+s32 __arena_global cpu_to_cid[MAX_CPUS];
+s32 __arena_global shard_start[MAX_CPUS];
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(unlocked_kfunc_cid_init_cids)
+{
+ u32 nr_cpu_ids = scx_bpf_nr_cpu_ids();
+
+ if (nr_cpu_ids > MAX_CPUS)
+ return -E2BIG;
+
+ scx_bpf_cid_override(cpu_to_cid, nr_cpu_ids, shard_start, 1);
+ return 0;
+}
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(unlocked_kfunc_cid_init)
+{
+ struct task_struct *p;
+ s32 ret;
+
+ ret = scx_bpf_create_dsq(SHARED_DSQ, -1);
+ if (ret)
+ return ret;
+
+ bpf_rcu_read_lock();
+ bpf_for_each(scx_dsq, p, SHARED_DSQ, 0) {
+ if (p->pid & 1)
+ scx_bpf_dsq_reenq_task(BPF_FOR_EACH_ITER, p);
+ else
+ scx_bpf_dsq_move(BPF_FOR_EACH_ITER, p, SCX_DSQ_GLOBAL, 0);
+ }
+ bpf_rcu_read_unlock();
+
+ return 0;
+}
+
+void BPF_STRUCT_OPS(unlocked_kfunc_cid_dump, struct scx_dump_ctx *dctx)
+{
+ scx_bpf_dump("nr_cids=%u\n", scx_bpf_nr_cids());
+}
+
+SCX_OPS_CID_DEFINE(unlocked_kfunc_cid_ops,
+ .init_cids = (void *)unlocked_kfunc_cid_init_cids,
+ .init = (void *)unlocked_kfunc_cid_init,
+ .dump = (void *)unlocked_kfunc_cid_dump,
+ .name = "unlocked_kfunc_cid");
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_dump.bpf.c b/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_dump.bpf.c
new file mode 100644
index 000000000000..70218f48e93a
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_dump.bpf.c
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects scx_bpf_dsq_move() from ops.dump().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ 0
+
+char _license[] SEC("license") = "GPL";
+
+void BPF_STRUCT_OPS(unlocked_kfunc_fail_dump_cb, struct scx_dump_ctx *dctx)
+{
+ struct task_struct *p;
+
+ bpf_for_each(scx_dsq, p, SHARED_DSQ, 0)
+ scx_bpf_dsq_move(BPF_FOR_EACH_ITER, p, SCX_DSQ_GLOBAL, 0);
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops unlocked_kfunc_fail_dump_ops = {
+ .dump = (void *)unlocked_kfunc_fail_dump_cb,
+ .name = "unlocked_kfunc_fail_dump",
+};
diff --git a/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_init_cids.bpf.c b/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_init_cids.bpf.c
new file mode 100644
index 000000000000..2f2d277aa54f
--- /dev/null
+++ b/tools/testing/selftests/sched_ext/unlocked_kfunc_fail_init_cids.bpf.c
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Verify that the BPF verifier rejects scx_bpf_create_dsq() from
+ * ops.init_cids().
+ *
+ * Copyright (c) 2026 Google LLC.
+ */
+
+#include <scx/common.bpf.h>
+
+#define SHARED_DSQ 0
+
+char _license[] SEC("license") = "GPL";
+
+s32 BPF_STRUCT_OPS_SLEEPABLE(unlocked_kfunc_fail_init_cids_cb)
+{
+ return scx_bpf_create_dsq(SHARED_DSQ, -1);
+}
+
+SCX_OPS_CID_DEFINE(unlocked_kfunc_fail_init_cids_ops,
+ .init_cids = (void *)unlocked_kfunc_fail_init_cids_cb,
+ .name = "unlocked_kfunc_fail_init_cids");
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply [flat|nested] 3+ messages in thread