From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796060156; b=UYnaXx52aBa4nTjdu/CblgpYpZ65ONevBGgv3Xn/JQHnR4d1yId59TQ6lgINwtrszbMD DPq1ca/2OBwjXU2NDDVmXLtJnNtf2jGnQ8o0ovm6U8q1sQ8Qcw7N0pz6jpNfBnHAg2AR7 QaGTuNznVwAALIuuQvn9kMPH0Qfgu8y3/Pa1Am15rzPcexNCEEhiN2rQC/eh7qiyKBuPq jgmTv9uCvK4t3ldvTn44hvnErnzKC31bfE7A9fyEidwquLhFpL8iGJwNUmKMm7Rz4R5Zq ITQ1WgkWIqaEqDWBakCOvcMGkf6BDCb7AnpPtloECBGO8r7cEF4Bo/4bLKAJFXOO3vw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796060156; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:Mime-Version: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=gy69I8XruNBF75S4IX37QfuSW38hPAHM24F6E/5o+a4=; b=rK2NRYFJDkoRrziexb6oyDwOepEvoObC0Un+gWbaKFKiGqLCf/pznV5Wn8f7DtlpZnWI +v2TxDN1Wsy0y3yf+OIq8J8fKiAvZF2gx2OA+uWygFEoBIEdPcxzc8H4uA6DDcFnBS5sy m2jZvW3B3StqwyQ7xyCECRI+lwS1fdFYpvjJj73VZkGSKLtHa8v0Pi0rxCLd+G3QV/kPk oZsmu8uRy2b9S55oSLb4FPovS4eGH3EA6fENH/tA0yhMGn8T7jtGeUqpbF+lkxpxPtw05 G15UcUveTX5JyGlWwziQjxjos7Yfp7tcqhoBfimvOSka22lN9abUUnP9zW+ddZpwCZA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--jpiecuch.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=OoptgTTv; arc=none smtp.remote-ip=209.85.128.72 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7D0A606A32A for ; Mon, 30 Nov 2026 17:35:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E7D0A606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jpiecuch.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=OoptgTTv Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4bbb2a62535so22711675e9.2 for ; Mon, 30 Nov 2026 09:35:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796060154; x=1796664954; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=gy69I8XruNBF75S4IX37QfuSW38hPAHM24F6E/5o+a4=; b=OoptgTTvpp+uj2MKhpXM/o9+W8VF0vRcUswir+XrMPWciKsxroztN80VXoB54HIkMj T41P0NL/Irz5IW+1wAt/Ro2/P3AmwzSmNUFoHgdp6b8YcWiaO8tuJ89VCM9HcnhjTXOe 7e1UZTXu4bFfMyaGLdnJteeLr82a1OysoM+MarDIFczMzDJcJRvmMqO4MscG7cMRZmMm cis5v/H61UbF4L6KIefXm0Onmo8cSKTUKtaJCJyq3mPGvoAIq7DNRow8HA4i7cT9v+c6 oou0bn6JfmJD+wmuiNr5nFV+QhEs8TpvKmzG+eGI8i7GIzpdJZ9JTXVkSwXoWQAWAeyl 8OnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796060154; x=1796664954; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=gy69I8XruNBF75S4IX37QfuSW38hPAHM24F6E/5o+a4=; b=i3pgVa4bZbhGa0kQTL6fJTEupq8SQh1GsbKzLSkWfQj0Vs+vI37o7COJggu/7vuISq DWEfI03XN7rP8/fflIw2O/VJjQwd6n5Y7WXjTbpWQc22BidVRxPC4kRR+tCNw4mP95df RdZipWad9J1Kfq007y20UjoVQaH5BXGoNySF1kh5WTLA1O35yz5mx0kA/08S1/p2BeQ3 XEs2dzTUk2oLR4SiAKzHeIfdaL1USBns3ZBW5th9zP8zyLkAcNqrFI4IHd95sVSWIUhS Sbz3LaO3EIVo4lVm60wmJQnpojcK2FEhCzpt9EuXCbPRCPRoTQde18r0Hj9rmhehpj0V /d7g== X-Forwarded-Encrypted: i=1; AL+hErEVa8UnuWIkuYel0XBoPwRb7n/XDwOPG95CbTEh6z/nnKo8+vI1dW7RhOsIp9s654g9gNXAtqgJc/oPUG0=@vger.kernel.org X-Gm-Message-State: APuU4Z+1Wmax+5xOzGFJ0gCZDzsjqiQKsr/SWJFBg1wTLFm42/YDjOkI FC/p2pFmasg/Z1y8eN3R2i4r4pgb8JEfE2mU2fBy52mocK5QrpcDUGdWuFUiNH+6rSxh5038VVx W2AIixH1+ZjxE6g== X-Received: from wmg12.prod.google.com ([2002:a05:600c:22cc:b0:4a1:8e4a:36d3]) (user=jpiecuch job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d03:b0:4bb:6118:4f57 with SMTP id 5b1f17b1804b1-4bb888130cfmr778957955e9.18.1796060153719; Mon, 30 Nov 2026 09:35:53 -0800 (PST) Date: Mon, 30 Nov 2026 17:35:53 +0000 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261130173531.340393-1-jpiecuch@google.com> Subject: [PATCH v2 sched_ext/for-7.4-fixes] sched_ext: Clarify ops.sub_caps_updated() context and intended use From: Kuba Piecuch To: Tejun Heo , David Vernet , Andrea Righi , Changwoo Min Cc: Kuba Piecuch , sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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 or ancestor holds (such as an rq lock). If a delivery for the shard is already in flight, a racing update folds into its buffer and is delivered by the in-flight caller from its own context; updates made before the child's ops are ready are delivered unlocked by 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 --- 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. Changes in v2: - Document concurrent in-flight coalescing delivering from the in-flight caller's context, ancestor callers on scx_bpf_sub_revoke(), and scx_sub_seed_caps() handling any pre-ready grant or revoke (Tejun Heo). v1: https://lore.kernel.org/all/20261130125422.3895514-1-jpiecuch@google.com/ kernel/sched/ext/internal.h | 31 ++++++++++++++++++++++--------- kernel/sched/ext/sub.c | 12 ++++++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/kernel/sched/ext/internal.h b/kernel/sched/ext/internal.h index c77f62cd7bc9..0024b6712484 100644 --- a/kernel/sched/ext/internal.h +++ b/kernel/sched/ext/internal.h @@ -902,13 +902,22 @@ 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() or an + * ancestor's scx_bpf_sub_revoke() after the shard lock is released. It + * inherits whatever locking context that caller holds (e.g. an rq + * lock, or unlocked from an unlocked caller), and may run before the + * change takes effect on any given cpu. If a delivery for the shard is + * already in flight, concurrent updates fold into its buffer and the + * in-flight caller delivers them from its own context, possibly after + * the racing kfunc returns. Any update before the child's ops are ready + * is delivered unlocked by scx_sub_seed_caps(). + * + * 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()). Acting 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 +1673,12 @@ 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 it is released, before + * grant/revoke returns. If a delivery for the shard is already in flight, a + * racing grant/revoke only folds into the buffer and returns. The in-flight + * caller then delivers it from its own context, possibly after the racing call + * 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..a581ce7aa9cc 100644 --- a/kernel/sched/ext/sub.c +++ b/kernel/sched/ext/sub.c @@ -2862,6 +2862,12 @@ 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. * + * Resulting ops.sub_caps_updated() callbacks run synchronously after the shard + * locks are released, inheriting the caller's locking context. If a delivery + * for the shard is already in flight, the update folds into it and is + * delivered by that caller instead. Updates made before the child's ops are + * ready are delivered unlocked by scx_sub_seed_caps(). + * * 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 +3001,12 @@ __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. + * + * Resulting ops.sub_caps_updated() callbacks run synchronously after the shard + * locks are released, inheriting the caller's locking context. If a delivery + * for a descendant's shard is already in flight, the update folds into it and + * is delivered by that caller instead. Updates made before a descendant's ops + * are ready are delivered unlocked by scx_sub_seed_caps(). */ __bpf_kfunc void scx_bpf_sub_revoke(u64 cgroup_id, u64 caps, const struct scx_cmask *cmask__arena, base-commit: 2f6b5acb51e3a49a32f5b8cc066a871031837d4b -- 2.56.1.839.g22a8290db9-goog