[PATCH] sched/topology: Fix sd_share_id of CPUs without a cluster domain
From: Keisuke Nishimura
Date: Fri Dec 04 2026 - 10:13:16 EST
When two CPUs have the same sd_share_id, the CPUs should share their
resources. If a CPU has no cluster domain, the id falls back to the
first CPU in the LLC. This id is only used by cpus_share_resources(),
whose callers check sched_cluster_active first. The current
implementation has two issues.
First, if a CPU has no cluster domain but there are clusters in the
same LLC, the id may be the same as that of one of those clusters.
Consider the situation (CPU 7 is isolated/offline):
CPU 0 1 2 3 4 5 6 7
L2 (hardware) [------][------][------][------]
cluster domain [------][------][------]
LLC domain [--------------------------]
sd_share_id (current) 0 0 2 2 4 4 0
sd_share_id (patch) 0 0 2 2 4 4 6
Second, if the L2 cache of a CPU is shared only by SMT siblings, the CPU
uses the first CPU in the LLC as sd_share_id, because the cluster domain
degenerates into the SMT domain. Then, cpus_share_resources() returns
true for two CPUs that do not share an L2.
Consider the situation (e.g., Alder Lake):
CPU 0 1 2 3 4 5 6 7
L2 (hardware) [------][------][--------------]
SMT domain [------][------]
cluster domain [--------------]
LLC domain [------------------------------]
sd_share_id (current) 0 0 0 0 4 4 4 4
sd_share_id (patch) 0 0 2 2 4 4 4 4
Fix the two issues by using:
- the first CPU in the cluster, if available,
- the first CPU in the SMT, if available,
- its own CPU ID.
The ids on non-cluster machines change too, but should not change the
behavior. This is because cpus_share_resources() is not called there.
This issue was found through random testing on QEMU. I used coding
agents to help write this patch.
Fixes: b95303e0aeaf ("sched: Add cpus_share_resources API")
Signed-off-by: Keisuke Nishimura <keisuke.nishimura@xxxxxxxx>
---
For the second issue, this change may affect performance because the
result of select_idle_sibling() changes. However, I don't have access to
an Alder Lake machine, so I have not tested the performance on it.
---
kernel/sched/core.c | 4 ++--
kernel/sched/topology.c | 9 +++++----
2 files changed, 7 insertions(+), 6 deletions(-)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index ea89471d4743..8eaa79ff6bda 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -4098,8 +4098,8 @@ bool cpus_share_cache(int this_cpu, int that_cpu)
}
/*
- * Whether CPUs are share cache resources, which means LLC on non-cluster
- * machines and LLC tag or L2 on machines with clusters.
+ * Whether CPUs are in the same cluster or, without a cluster domain, the
+ * same SMT core.
*/
bool cpus_share_resources(int this_cpu, int that_cpu)
{
diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index 528897a58aff..d53eefd1d489 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -707,7 +707,6 @@ static void update_top_cache_domain(int cpu)
sd = highest_flag_domain(cpu, SD_SHARE_LLC);
if (sd) {
- id = cpumask_first(sched_domain_span(sd));
size = cpumask_weight(sched_domain_span(sd));
/* If sd_llc exists, sd_llc_shared should exist too. */
@@ -720,13 +719,15 @@ static void update_top_cache_domain(int cpu)
rcu_assign_pointer(per_cpu(sd_llc_shared, cpu), sds);
sd = lowest_flag_domain(cpu, SD_CLUSTER);
+ if (!sd)
+ sd = lowest_flag_domain(cpu, SD_SHARE_CPUCAPACITY);
+
if (sd)
id = cpumask_first(sched_domain_span(sd));
/*
- * This assignment should be placed after the sd_llc_id as
- * we want this id equals to cluster id on cluster machines
- * but equals to LLC id on non-Cluster machines.
+ * The first CPU of the cluster domain, or of the SMT domain
+ * without one, or the CPU itself.
*/
per_cpu(sd_share_id, cpu) = id;
--
2.34.1