sched_ext: Don't allow unlocked kfuncs from ops.dump() and ops.init_cids()
Subsystems (3)
PATCH 1/1 sched_ext: Don't allow unlocked kfuncs from ops.dump() and ops.init_cids()
scx_kf_allow_flags[] grants SCX_KF_ALLOW_UNLOCKED to ops.dump() and ops.init_cids(). That group contains scx_bpf_create_dsq(), scx_bpf_dsq_move*(), scx_bpf_dsq_reenq_task(), and scx_bpf_select_cpu_*().
1) ops.dump() had SCX_KF_UNLOCKED since commit 07814a9439a3 ("sched_ext: Print debug dump after an error exit") because its caller holds no rq lock. Commit 4c30f5ce4f7a ("sched_ext: Implement scx_bpf_dispatch[_vtime]_from_dsq()") then made DSQ moves callable from SCX_KF_UNLOCKED contexts, and commit d1d3c1c6ae36 ("sched_ext:
Add verifier-time kfunc context filter") carried the grant into scx_kf_allow_flags[].
However, scx_dump_state() runs ops.dump() with IRQs disabled under scx_dump_lock, including from scx_disable_irq_workfn() (hardirq) and SysRq-D. On a live SysRq-D dump, an ops.dump() that calls scx_bpf_dsq_move() migrates tasks and runs nested SCX ops from inside the dump:
sysrq_handle_sched_ext_dump() /* may run in hardirq */
scx_dump_state()
raw_spin_lock_irqsave(&scx_dump_lock)
ops_dump_init() /* arms scx_dump_data */
ops.dump()
scx_bpf_dsq_move()
kf_switch_rq_lock(task_rq(p))
raw_spin_lock(&dsq->lock)
move_task_between_dsqs()
move_remote_task_to_local_dsq()
deactivate_task(src_rq)
activate_task(dst_rq)
...
ops.dequeue() /* nested SCX op */ Any scx_bpf_dump() call from the nested op also splices output into the active dump because scx_dump_data is armed on this CPU. scx_bpf_dsq_reenq_task() takes the same rq and DSQ locks, and scx_bpf_select_cpu_*() take p->pi_lock and run idle CPU selection. ops.dump_cpu() and ops.dump_task() already deny all context-sensitive kfuncs.
2) Commit 874fdc0e86e4 ("sched_ext: Add ops.init_cids() to finalize the cid layout before init") granted SCX_KF_ALLOW_UNLOCKED alongside SCX_KF_ALLOW_INIT_CIDS. ops.init_cids() runs before scx_cid_publish_tables(), scx_arena_pool_init(), and scx_alloc_pshards(). Its only job is to finalize the cid layout via scx_bpf_cid_override(), so it has no reason to create DSQs or call the DSQ move and re-enqueue kfuncs.
Patch 1 removes SCX_KF_ALLOW_UNLOCKED from ops.dump() and restricts ops.init_cids() to SCX_KF_ALLOW_INIT_CIDS. All remaining SCX_KF_ALLOW_UNLOCKED ops run in process context with IRQs enabled and hold no rq, DSQ, or pi locks.
Patch 2 adds the unlocked_kfunc selftest. Without patch 1, the test fails with:
ERR: unlocked_kfunc.c:49
unlocked_kfunc_fail_dump loaded when it should have been rejected not ok 1 unlocked_kfunc #
The patches were written with the help of an AI coding assistant.
Kuba Piecuch (2):
sched_ext: Don't allow unlocked kfuncs from ops.dump() and
ops.init_cids()
selftests/sched_ext: Add unlocked_kfunc test
kernel/sched/ext/ext.c | 8 +- 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 ++++++ 7 files changed, 264 insertions(+), 2 deletions(-) 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
base-commit: 2f7928516314f37d7cb334675656b4412edc8be3
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 1/2 sched_ext: Don't allow unlocked kfuncs from ops.dump() and ops.init_cids()
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.
--- kernel/sched/ext/ext.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/sched/ext/ext.c +6 -2
@@ -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, };
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH 2/2 selftests/sched_ext: Add unlocked_kfunc test
In reply to Kuba PiecuchAdd 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.
--- 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
tools/testing/selftests/sched_ext/Makefile +1 -0
@@ -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)))
tools/testing/selftests/sched_ext/unlocked_kfunc.bpf.c new file +69 -0
69 lines not on this page · raw message
tools/testing/selftests/sched_ext/unlocked_kfunc.c new file +73 -0
73 lines not on this page · raw message
tools/testing/selftests/sched_ext/unlocked_kfunc_cid.bpf.c new file +67 -0
67 lines not on this page · raw message
tools/testing/selftests/sched_ext/unlocked_kfunc_fail_dump.bpf.c new file +26 -0
@@ -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" , +};
tools/testing/selftests/sched_ext/unlocked_kfunc_fail_init_cids.bpf.c new file +22 -0
@@ -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" );
Signature · 1 line
2.56.1.750.gf70dc682cf-goog