[PATCH 4/4] sched_ext: Extract online CID construction from scx_cid_init

From: Zhang Qiao

Date: Thu Nov 05 2026 - 01:16:56 EST


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@xxxxxxxxxx>
---
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