[PATCHSET sched_ext/for-7.4-fixes] sched_ext: Don't allow unlocked kfuncs from ops.dump() and ops.init_cids()
From: Kuba Piecuch
Date: Mon Nov 23 2026 - 08:56:47 EST
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
--
2.56.1.750.gf70dc682cf-goog