All of lore.kernel.org
 help / color / mirror / Atom feed
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 sched_ext/for-7.4-fixes] sched_ext: Clarify ops.sub_caps_updated() context and intended use
Date: Mon, 30 Nov 2026 12:55:09 +0000	[thread overview]
Message-ID: <20261130125422.3895514-1-jpiecuch@google.com> (raw)

The doc comment on ops.sub_caps_updated() states that the callback is
"delivered asynchronously after the change is recorded", which can be
misread as running in a separate unlocked context.

In reality, scx_bpf_sub_grant() and scx_bpf_sub_revoke() deliver
ops.sub_caps_updated() synchronously before returning, after releasing the
shard locks. When called from an SCX op, ops.sub_caps_updated() inherits
whatever locking context the parent holds (such as an rq lock), while
grants during ops.sub_attach() are seeded unlocked via
scx_sub_seed_caps(). As discussed on the list, synchronous delivery is
intentional to keep competing updates ordered; ops.sub_caps_updated() is
only for tracking held caps and propagating grants/revokes to children,
whereas acting on caps (such as calling scx_bpf_cidperf_set()) belongs in
ops.sub_ecaps_updated(), which runs with the cid's rq lock held once the
change is in effect.

Clarify the execution context and intended scope in the doc comments on
@sub_caps_updated, struct scx_caps_updated, scx_bpf_sub_grant(), and
scx_bpf_sub_revoke().

Link: https://lore.kernel.org/all/8e4043f995e96c0d4642287bc5009000@kernel.org/
Signed-off-by: Kuba Piecuch <jpiecuch@google.com>
---
This patch was written with the help of an AI coding assistant. I have
reviewed the patch and verified the tests run by the tool.

 kernel/sched/ext/internal.h | 25 ++++++++++++++++---------
 kernel/sched/ext/sub.c      |  7 +++++++
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/kernel/sched/ext/internal.h b/kernel/sched/ext/internal.h
index c77f62cd7bc9..ce8cf284b484 100644
--- a/kernel/sched/ext/internal.h
+++ b/kernel/sched/ext/internal.h
@@ -902,13 +902,19 @@ struct sched_ext_ops {
 	 * all changes since the last delivery. Direction (set vs cleared) isn't
 	 * encoded. Query current state with scx_bpf_sub_caps().
 	 *
-	 * Delivered asynchronously after the change is recorded, and may run
-	 * before it takes effect on any given cpu. Use it to track which caps
-	 * the sub-sched holds and propagate to its own children, not to decide
-	 * if a task can run on a cpu now. sub_ecaps_updated() reports that per
-	 * cpu, once it is in effect.
-	 *
-	 * May call scx_bpf_sub_grant() / scx_bpf_sub_revoke() on children.
+	 * Delivered synchronously from the parent's scx_bpf_sub_grant() /
+	 * scx_bpf_sub_revoke() after the shard lock is released, or unlocked
+	 * from scx_sub_seed_caps() when seeding caps granted during
+	 * ops.sub_attach(). It therefore inherits whatever locking context
+	 * the parent holds (e.g. an rq lock, or unlocked from an unlocked
+	 * caller) and may run before the change takes effect on any given cpu.
+	 *
+	 * Use this only to track held caps and propagate grants and revokes to
+	 * children via scx_bpf_sub_grant() / scx_bpf_sub_revoke(). Do not act
+	 * on caps here (e.g. deciding if a task can run on a cpu or calling
+	 * rq-lock-sensitive kfuncs such as scx_bpf_cidperf_set()); anything
+	 * that acts on caps belongs in ops.sub_ecaps_updated(), which runs
+	 * with the cid's rq lock held once the change is in effect on the cpu.
 	 */
 	void (*sub_caps_updated)(const struct scx_cmask *cmask, u64 caps);
 
@@ -1664,8 +1670,9 @@ enum scx_cap_flags {
  * delivery is in flight per shard. Further changes fold into the same buffer
  * and ship as the next callback, so a shard's callbacks fire in order.
  *
- * (3) leads to deferred delivery. Events accumulate during grant/revoke and are
- * delivered after the shard lock is released.
+ * (3) leads to delivering outside the shard lock. Events accumulate under the
+ * shard lock and are delivered synchronously after the shard lock is released,
+ * before grant/revoke returns.
  */
 struct scx_caps_updated {
 	raw_spinlock_t		lock;
diff --git a/kernel/sched/ext/sub.c b/kernel/sched/ext/sub.c
index 7c069a289766..cc5f6dfc7e37 100644
--- a/kernel/sched/ext/sub.c
+++ b/kernel/sched/ext/sub.c
@@ -2862,6 +2862,11 @@ static s32 sub_cap_preamble(u64 cgroup_id, u64 caps, const struct bpf_prog_aux *
  * All-or-nothing keeps the caller-visible result binary per cid, so the denied
  * mask is one mask to interpret rather than a per-cap matrix.
  *
+ * Any resulting ops.sub_caps_updated() callbacks are delivered synchronously
+ * after the shard locks are released (inheriting the caller's locking context),
+ * except when called from ops.sub_attach(), where scx_sub_seed_caps() delivers
+ * them unlocked once the child's ops are ready.
+ *
  * Return 0 on full success, -EPERM if any cid was refused, -ENODEV if the child
  * is gone or being disabled, or a negative errno on other failures.
  */
@@ -2995,6 +3000,8 @@ __bpf_kfunc s32 scx_bpf_sub_grant(u64 cgroup_id, u64 caps,
  * Clear @caps bits on @cmask__arena from the child named by @cgroup_id and all
  * its descendants. The origin parent's pshard lock is held across the subtree
  * walk so a concurrent grant from the origin parent observes the revoked state.
+ * Any resulting ops.sub_caps_updated() callbacks are delivered synchronously
+ * after the shard locks are released, inheriting the caller's locking context.
  */
 __bpf_kfunc void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps,
 				    const struct scx_cmask *cmask__arena,

base-commit: d43078a237230fd8fb6bf082ff285c93d0ccd763
-- 
2.56.1.839.g22a8290db9-goog


             reply	other threads:[~2026-11-30 12:55 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 12:55 Kuba Piecuch [this message]
2026-11-30 15:26 ` [PATCH sched_ext/for-7.4-fixes] sched_ext: Clarify ops.sub_caps_updated() context and intended use Tejun Heo
2026-11-30 17:21   ` Kuba Piecuch

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=20261130125422.3895514-1-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.