From: Kuba Piecuch <jpiecuch@google.com>
To: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
Andrea Righi <arighi@nvidia.com>,
Changwoo Min <changwoo@igalia.com>
Cc: Kuba Piecuch <jpiecuch@google.com>,
sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] sched_ext: Reject rq-lock-sensitive kfuncs from ops.sub_caps_updated()
Date: Mon, 30 Nov 2026 15:53:15 +0000 [thread overview]
Message-ID: <20261130155148.2948874-2-jpiecuch@google.com> (raw)
In-Reply-To: <20261130155148.2948874-1-jpiecuch@google.com>
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
next prev parent reply other threads:[~2026-11-30 15:53 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261130155148.2948874-2-jpiecuch@google.com \
--to=jpiecuch@google.com \
--cc=arighi@nvidia.com \
--cc=changwoo@igalia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=sched-ext@lists.linux.dev \
--cc=tj@kernel.org \
--cc=void@manifault.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.