mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zhang Qiao <zhangqiao22@huawei.com>
To: <sched-ext@lists.linux.dev>
Cc: <linux-kernel@vger.kernel.org>, <tj@kernel.org>,
	<void@manifault.com>, <arighi@nvidia.com>, <changwoo@igalia.com>,
	<tanghui22@huawei.com>, <zhangqiao22@huawei.com>
Subject: [PATCH 4/4] sched_ext: Extract online CID construction from scx_cid_init
Date: Thu, 5 Nov 2026 14:50:13 +0800	[thread overview]
Message-ID: <20261105065013.784398-5-zhangqiao22@huawei.com> (raw)
In-Reply-To: <20261105065013.784398-1-zhangqiao22@huawei.com>

Move the node walk into scx_cid_build_online() so scx_cid_init() is
left with allocation, diagnostics and cleanup only. Document the
builder's mask ownership and add one-line summaries for the traversal
helpers.

The mapping, shard policy, publication timing and error paths are
unchanged. Partial-SMT core counting and the single-core shard
hard-cap corner case are left for separate fixes.

Validation: compiled kernel/sched/build_policy.o with SCHED_CLASS_EXT
enabled and CPUMASK_OFFSTACK both disabled and enabled. A temporary C
harness compared all construction stages against the original across
1800 topology/shard-size combinations and checked seven mask
allocation failure paths under UBSan. Each patch passes strict
checkpatch.

Signed-off-by: Zhang Qiao <zhangqiao22@huawei.com>
---
 kernel/sched/ext/cid.c | 90 ++++++++++++++++++++++++------------------
 1 file changed, 51 insertions(+), 39 deletions(-)

diff --git a/kernel/sched/ext/cid.c b/kernel/sched/ext/cid.c
index 54eacdc23d32..688008823355 100644
--- a/kernel/sched/ext/cid.c
+++ b/kernel/sched/ext/cid.c
@@ -165,6 +165,7 @@ void scx_cid_retire_tables(void)
 	call_rcu(&tbls->rcu, scx_cid_tables_free_rcufn);
 }
 
+/* Build-only state. The masks are borrowed from scx_cid_init(). */
 struct scx_cid_builder {
 	struct scx_cid_tables *tbls;
 	struct cpumask *to_walk;
@@ -180,6 +181,7 @@ struct scx_cid_builder {
 	u32 shard_size, max_cids;
 };
 
+/* Consume one LLC from node_scratch and append its cids and shards. */
 static s32 scx_cid_build_llc(struct scx_cid_builder *b, s32 nid,
 			     s32 node_cid, s32 node_idx)
 {
@@ -192,7 +194,7 @@ static s32 scx_cid_build_llc(struct scx_cid_builder *b, s32 nid,
 	s32 shard_cid, shard_idx;
 	s32 cluster_cid = -1, cluster_idx = -1;
 
-	/* b->llc_scratch = b->node_scratch & this llc */
+	/* Intersect the remaining node CPUs with this LLC. */
 	cpumask_and(b->llc_scratch, b->node_scratch, llc_mask);
 	if (WARN_ON_ONCE(!cpumask_test_cpu(ncpu, b->llc_scratch)))
 		return -EINVAL;
@@ -229,7 +231,7 @@ static s32 scx_cid_build_llc(struct scx_cid_builder *b, s32 nid,
 		core_cid = b->next_cid;
 		core_idx = b->next_core_idx++;
 
-		/* b->core_scratch = b->cluster_scratch & this core */
+		/* Intersect the remaining cluster CPUs with this core. */
 		cpumask_and(b->core_scratch, b->cluster_scratch, sib);
 		if (WARN_ON_ONCE(!cpumask_test_cpu(lcpu, b->core_scratch)))
 			return -EINVAL;
@@ -284,6 +286,45 @@ static s32 scx_cid_build_llc(struct scx_cid_builder *b, s32 nid,
 	return 0;
 }
 
+/* Assign online CPUs by node, consuming to_walk as CPUs get their cids. */
+static s32 scx_cid_build_online(struct scx_cid_builder *b)
+{
+	cpumask_copy(b->to_walk, cpu_online_mask);
+
+	while (!cpumask_empty(b->to_walk)) {
+		s32 next_cpu = cpumask_first(b->to_walk);
+		s32 nid = cpu_to_node(next_cpu);
+		s32 node_cid = b->next_cid;
+		s32 node_idx;
+
+		/*
+		 * No NUMA info: skip and let scx_cid_build_notopo() assign a
+		 * no-topo cid. cpumask_of_node(-1) is undefined.
+		 */
+		if (nid < 0) {
+			cpumask_clear_cpu(next_cpu, b->to_walk);
+			continue;
+		}
+
+		node_idx = b->next_node_idx++;
+
+		/* Intersect the remaining online CPUs with this node. */
+		cpumask_and(b->node_scratch, b->to_walk, cpumask_of_node(nid));
+		if (WARN_ON_ONCE(!cpumask_test_cpu(next_cpu, b->node_scratch)))
+			return -EINVAL;
+
+		while (!cpumask_empty(b->node_scratch)) {
+			s32 ret;
+
+			ret = scx_cid_build_llc(b, nid, node_cid, node_idx);
+			if (ret)
+				return ret;
+		}
+	}
+
+	return 0;
+}
+
 static void scx_cid_build_notopo(struct scx_cid_builder *b)
 {
 	u32 notopo_in_shard;
@@ -292,10 +333,10 @@ static void scx_cid_build_notopo(struct scx_cid_builder *b)
 
 	/*
 	 * No-topo section: any possible cpu without a cid - normally just the
-	 * not-online ones. Pack into shards of up to min(@b->shard_size,
+	 * not-online ones. Pack into shards of up to min(b->shard_size,
 	 * SCX_CID_SHARD_MAX_CPUS) cids so that every cid has a valid shard
-	 * assignment and the hard cap holds even with a large @b->shard_size.
-	 * Collect any currently-online cpus that land here in @b->online_no_topo
+	 * assignment and the hard cap holds even with a large b->shard_size.
+	 * Collect any currently-online cpus that land here in b->online_no_topo
 	 * so we can warn about them at the end.
 	 */
 	notopo_in_shard = min_t(u32, b->shard_size, SCX_CID_SHARD_MAX_CPUS);
@@ -348,7 +389,7 @@ static void scx_cid_finalize_tables(struct scx_cid_builder *b)
 
 /**
  * scx_cid_init - build the cid mapping
- * @sch: the scx_sched being initialized; used as the scx_error() target
+ * @sch: scheduler whose ops specify the target shard size
  *
  * Build a fresh table set. It becomes visible through scx_cid_publish_tables()
  * and is retired by scx_cid_retire_tables() at disable.
@@ -370,7 +411,7 @@ s32 scx_cid_init(struct scx_sched *sch)
 	cpumask_var_t online_no_topo __free(free_cpumask_var) = CPUMASK_VAR_NULL;
 	struct scx_cid_tables *tbls;
 	struct scx_cid_builder b = {};
-	s32 cpu, si;
+	s32 cpu, si, ret;
 
 	/* CMASK_MAX_WORDS in cid.bpf.h covers NR_CPUS up to 8192 */
 	BUILD_BUG_ON(NR_CPUS > 8192);
@@ -412,38 +453,9 @@ s32 scx_cid_init(struct scx_sched *sch)
 	for (cpu = 0; cpu < nr_cpu_ids; cpu++)
 		tbls->cpu_to_cid[cpu] = -1;
 
-	cpumask_copy(b.to_walk, cpu_online_mask);
-
-	while (!cpumask_empty(b.to_walk)) {
-		s32 next_cpu = cpumask_first(b.to_walk);
-		s32 nid = cpu_to_node(next_cpu);
-		s32 node_cid = b.next_cid;
-		s32 node_idx;
-
-		/*
-		 * No NUMA info: skip and let the tail loop assign a no-topo
-		 * cid. cpumask_of_node(-1) is undefined.
-		 */
-		if (nid < 0) {
-			cpumask_clear_cpu(next_cpu, b.to_walk);
-			continue;
-		}
-
-		node_idx = b.next_node_idx++;
-
-		/* b.node_scratch = b.to_walk & this node */
-		cpumask_and(b.node_scratch, b.to_walk, cpumask_of_node(nid));
-		if (WARN_ON_ONCE(!cpumask_test_cpu(next_cpu, b.node_scratch)))
-			return -EINVAL;
-
-		while (!cpumask_empty(b.node_scratch)) {
-			s32 ret;
-
-			ret = scx_cid_build_llc(&b, nid, node_cid, node_idx);
-			if (ret)
-				return ret;
-		}
-	}
+	ret = scx_cid_build_online(&b);
+	if (ret)
+		return ret;
 
 	scx_cid_build_notopo(&b);
 
-- 
2.43.0


  parent reply	other threads:[~2026-11-05  6:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05  6:50 [PATCH 0/4] sched_ext: Decompose scx_cid_init() Zhang Qiao
2026-11-05  6:50 ` [PATCH 1/4] sched_ext: Extract no-topology CID construction and table finalization Zhang Qiao
2026-11-05  6:50 ` [PATCH 2/4] sched_ext: Collect CID construction state in a private builder Zhang Qiao
2026-11-05  6:50 ` [PATCH 3/4] sched_ext: Extract construction of a single LLC CID range Zhang Qiao
2026-11-05  6:50 ` Zhang Qiao [this message]
2026-11-05 17:10 ` [PATCH 0/4] sched_ext: Decompose scx_cid_init() 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=20261105065013.784398-5-zhangqiao22@huawei.com \
    --to=zhangqiao22@huawei.com \
    --cc=arighi@nvidia.com \
    --cc=changwoo@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=tanghui22@huawei.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®