Skip to contentSkip to conversation

sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated()

PATCH In review Kuba Piecuch 2 participants 4 messages Nov 30

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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

scx_bpf_sub_grant() and scx_bpf_sub_revoke() deliver ops.sub_caps_updated() synchronously with locked_rq = NULL, so __SCX_CALL_OP() leaves scx_locked_rq_state and scx_dump_data untouched, while scx_sub_seed_caps() runs it unlocked. Depending on how the parent called grant or revoke, ops.sub_caps_updated() either runs unlocked or inherits the parent's rq lock or dump state:

- scx_bpf_cidperf_set() aborts the sub-scheduler with "Invalid target CPU" on any cid outside the parent's locked rq, or takes the target rq lock when unlocked.
- scx_bpf_locked_rq() aborts when unlocked and returns the parent's rq when locked.
- scx_bpf_dsq_nr_queued() (for SCX_DSQ_LOCAL), scx_bpf_dsq_reenq(), scx_bpf_reenqueue_local___v2(), and bpf_iter_scx_dsq_destroy() consult scx_locked_rq().
- scx_bpf_dump_bstr() aborts outside dump contexts and writes into the parent's dump buffer when called from ops.dump*().

As agreed on the list, ops.sub_caps_updated() is only for tracking held caps and propagating grants and revokes to children (while cap-acting operations like scx_bpf_cidperf_set() belong in ops.sub_ecaps_updated()), so rejecting these kfuncs at BPF load time prevents the misuse up front.

Patch 1 adds scx_kfunc_ids_no_sub_caps (overlapping scx_kfunc_ids_any with matching KF_* flags) and rejects those kfuncs from ops.sub_caps_updated() in scx_kfunc_context_filter(). scx_bpf_cpuperf_set() is already rejected because ops.sub_caps_updated() is only attachable on sched_ext_ops_cid, where scx_kfunc_ids_cpu_only forbids it.

Patch 2 adds the sub_caps_kfunc selftest. Without patch 1, sub_caps_kfunc fails because the verifier accepts the restricted kfuncs in ops.sub_caps_updated():

ERR: sub_caps_kfunc.c:91
fail_cidperf loaded when it should have been rejected not ok 1 sub_caps_kfunc #

The patches were written with the help of an AI coding assistant. I have reviewed the patches and verified the tests run by the tool.

Kuba Piecuch (2):
sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated() selftests/sched_ext: Add sub_caps_kfunc test

 kernel/sched/ext/ext.c                        |  49 +++++++-
 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 ++++++++++++++++++
 5 files changed, 331 insertions(+), 1 deletion(-)
 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

base-commit: d43078a237230fd8fb6bf082ff285c93d0ccd763

Signature · 1 line
2.56.1.839.g22a8290db9-goog
#1 lore raw

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

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")
---
 kernel/sched/ext/ext.c | 49 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)
kernel/sched/ext/ext.c +48 -1
index a4a93ed97f7b..b8e5251edd2b 100644
@@ -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)
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#2 lore raw

PATCH 2/2 selftests/sched_ext: Add sub_caps_kfunc test

In reply to Kuba Piecuch

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.

---
 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
tools/testing/selftests/sched_ext/Makefile +1 -0
index fac8b26c4d70..47dafa4c08c4 100644
@@ -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			\
tools/testing/selftests/sched_ext/sub_caps_kfunc.bpf.c new file +66 -0

66 lines not on this page · raw message

tools/testing/selftests/sched_ext/sub_caps_kfunc.c new file +107 -0

107 lines not on this page · raw message

tools/testing/selftests/sched_ext/sub_caps_kfunc_fail.bpf.c new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.56.1.839.g22a8290db9-goog
reviewer
#3 lore raw
In reply to Kuba Piecuch
Quoted 3 lines

Kuba Piecuch (2):
sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated() selftests/sched_ext: Add sub_caps_kfunc test

Applied 1-2 to sched_ext/for-7.4-fixes.

Thanks.

--
tejun