Skip to contentSkip to conversation

x86/smpboot: tidy sched-topology and drop useless SMT level

PATCH Superseded Li Chen 2 participants 8 messages Jul 6, 2025 – Jul 11, 2025

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Li Chen

author
#0 lore raw

v4 cover x86/smpboot: tidy sched-topology and drop useless SMT level

From: Li Chen <[email protected]>

This series cleans up sched-domain topology handling and eliminates hundreds of pointless attach/destroy cycles for large machines when SMT is not available.

Patch 1, 2, and 3 do some cleanup and refactor.

Patch 4 is a follow-up that simply skip SMT domain when cpu_smt_num_threads <= 1, so the SMT level never gets created.

Tested on Qemu.

changelog:
v2: fix wording issue as suggested by Thomas [1]
v3: remove pointless memset and adjust PKG index accordingly, as
suggested by Thomas [2], and refine some other wording issues.
v4: v4: Split refactor patche into three parts (as suggested by Peter [3])
and refined patch 4 logic (as done by K. [4]).

[1]: https://lore.kernel.org/all/87msa2r018.ffs@tglx/
[2]: https://lore.kernel.org/all/875xglntx1.ffs@tglx/
[3]: https://lkml.org/lkml/2025/6/25/584
[4]: https://lore.kernel.org/lkml/[email protected]/

Li Chen (4):
smpboot: introduce SDTL() helper to tidy sched topology setup x86/smpboot: remove redundant CONFIG_SCHED_SMT x86/smpboot: moves x86_topology to static initialize and truncate x86/smpboot: avoid SMT domain attach/destroy if SMT is not enabled

 arch/powerpc/kernel/smp.c      | 34 ++++++++-------------
 arch/s390/kernel/topology.c    | 10 +++----
 arch/x86/kernel/smpboot.c      | 55 ++++++++++++++++------------------
 include/linux/sched/topology.h |  6 ++--
 kernel/sched/topology.c        | 16 ++++------
 5 files changed, 53 insertions(+), 68 deletions(-)
Signature · 1 line
2.49.0
author
#1 lore raw

PATCH v4 1/4 smpboot: introduce SDTL() helper to tidy sched topology setup

From: Li Chen <[email protected]>

Define a small SDTL(maskfn, flagsfn, name) macro and use it to build the sched_domain_topology_level. Purely a cleanup; behaviour is unchanged.

---
 arch/powerpc/kernel/smp.c      | 34 +++++++++++++---------------------
 arch/s390/kernel/topology.c    | 10 +++++-----
 arch/x86/kernel/smpboot.c      | 21 ++++++---------------
 include/linux/sched/topology.h |  6 +++---
 kernel/sched/topology.c        | 16 ++++++----------
 5 files changed, 33 insertions(+), 54 deletions(-)
arch/powerpc/kernel/smp.c +13 -21
index 5ac7084eebc0b..b05995e272134 100644
@@ -1700,28 +1700,20 @@ static void __init build_sched_topology(void)
 #ifdef CONFIG_SCHED_SMT
 	if (has_big_cores) {
 		pr_info("Big cores detected but using small core scheduling\n");
-		powerpc_topology[i++] = (struct sched_domain_topology_level){
-			smallcore_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT)
-		};
-	} else {
-		powerpc_topology[i++] = (struct sched_domain_topology_level){
-			cpu_smt_mask, powerpc_smt_flags, SD_INIT_NAME(SMT)
-		};
-	}
+		powerpc_topology[i++] =
+			SDTL(smallcore_smt_mask, powerpc_smt_flags, SMT);
+	} else
+		powerpc_topology[i++] = SDTL(cpu_smt_mask, powerpc_smt_flags, SMT);
 #endif
-	if (shared_caches) {
-		powerpc_topology[i++] = (struct sched_domain_topology_level){
-			shared_cache_mask, powerpc_shared_cache_flags, SD_INIT_NAME(CACHE)
-		};
-	}
-	if (has_coregroup_support()) {
-		powerpc_topology[i++] = (struct sched_domain_topology_level){
-			cpu_mc_mask, powerpc_shared_proc_flags, SD_INIT_NAME(MC)
-		};
-	}
-	powerpc_topology[i++] = (struct sched_domain_topology_level){
-		cpu_cpu_mask, powerpc_shared_proc_flags, SD_INIT_NAME(PKG)
-	};
+	if (shared_caches)
+		powerpc_topology[i++] =
+			SDTL(shared_cache_mask, powerpc_shared_cache_flags, CACHE);
+
+	if (has_coregroup_support())
+		powerpc_topology[i++] =
+			SDTL(cpu_mc_mask, powerpc_shared_proc_flags, MC);
+
+	powerpc_topology[i++] = SDTL(cpu_cpu_mask, powerpc_shared_proc_flags, PKG);
 
 	/* There must be one trailing NULL entry left.  */
 	BUG_ON(i >= ARRAY_SIZE(powerpc_topology) - 1);
arch/s390/kernel/topology.c +5 -5
index 3df048e190b11..ccf5dcc56f86d 100644
@@ -531,11 +531,11 @@ static const struct cpumask *cpu_drawer_mask(int cpu)
 }
 
 static struct sched_domain_topology_level s390_topology[] = {
-	{ cpu_thread_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
-	{ cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
-	{ cpu_book_mask, SD_INIT_NAME(BOOK) },
-	{ cpu_drawer_mask, SD_INIT_NAME(DRAWER) },
-	{ cpu_cpu_mask, SD_INIT_NAME(PKG) },
+	SDTL(cpu_thread_mask, cpu_smt_flags, SMT),
+	SDTL(cpu_coregroup_mask, cpu_core_flags, MC),
+	SDTL(cpu_book_mask, NULL, BOOK),
+	SDTL(cpu_drawer_mask, NULL, DRAWER),
+	SDTL(cpu_cpu_mask, NULL, PKG),
 	{ NULL, },
 };
 
arch/x86/kernel/smpboot.c +6 -15
index 58ede3fa6a75b..55f09946c791b 100644
@@ -485,35 +485,26 @@ static void __init build_sched_topology(void)
 	int i = 0;
 
 #ifdef CONFIG_SCHED_SMT
-	x86_topology[i++] = (struct sched_domain_topology_level){
-		cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT)
-	};
+	x86_topology[i++] = SDTL(cpu_smt_mask, cpu_smt_flags, SMT);
 #endif
 #ifdef CONFIG_SCHED_CLUSTER
-	x86_topology[i++] = (struct sched_domain_topology_level){
-		cpu_clustergroup_mask, x86_cluster_flags, SD_INIT_NAME(CLS)
-	};
+	x86_topology[i++] = SDTL(cpu_clustergroup_mask, x86_cluster_flags, CLS);
 #endif
 #ifdef CONFIG_SCHED_MC
-	x86_topology[i++] = (struct sched_domain_topology_level){
-		cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC)
-	};
+	x86_topology[i++] = SDTL(cpu_coregroup_mask, x86_core_flags, MC);
 #endif
 	/*
 	 * When there is NUMA topology inside the package skip the PKG domain
 	 * since the NUMA domains will auto-magically create the right spanning
 	 * domains based on the SLIT.
 	 */
-	if (!x86_has_numa_in_package) {
-		x86_topology[i++] = (struct sched_domain_topology_level){
-			cpu_cpu_mask, x86_sched_itmt_flags, SD_INIT_NAME(PKG)
-		};
-	}
+	if (!x86_has_numa_in_package)
+		x86_topology[i++] = SDTL(cpu_cpu_mask, x86_sched_itmt_flags, PKG);
 
 	/*
 	 * There must be one trailing NULL entry left.
 	 */
-	BUG_ON(i >= ARRAY_SIZE(x86_topology)-1);
+	BUG_ON(i >= ARRAY_SIZE(x86_topology) - 1);
 
 	set_sched_topology(x86_topology);
 }
include/linux/sched/topology.h +3 -3
index 198bb5cc1774b..0b53e372c445c 100644
@@ -197,9 +197,9 @@ struct sched_domain_topology_level {
 extern void __init set_sched_topology(struct sched_domain_topology_level *tl);
 extern void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio);
 
-
-# define SD_INIT_NAME(type)		.name = #type
-
+#define SDTL(maskfn, flagsfn, dname) \
+	((struct sched_domain_topology_level) \
+	    { .mask = maskfn, .sd_flags = flagsfn, .name = #dname, .numa_level = 0 })
 #else /* CONFIG_SMP */
 
 struct sched_domain_attr;
kernel/sched/topology.c +6 -10
index b958fe48e0205..e6ec65ae4b75d 100644
@@ -1739,17 +1739,17 @@ sd_init(struct sched_domain_topology_level *tl,
  */
 static struct sched_domain_topology_level default_topology[] = {
 #ifdef CONFIG_SCHED_SMT
-	{ cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
+	SDTL(cpu_smt_mask, cpu_smt_flags, SMT),
 #endif
 
 #ifdef CONFIG_SCHED_CLUSTER
-	{ cpu_clustergroup_mask, cpu_cluster_flags, SD_INIT_NAME(CLS) },
+	SDTL(cpu_clustergroup_mask, cpu_cluster_flags, CLS),
 #endif
 
 #ifdef CONFIG_SCHED_MC
-	{ cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
+	SDTL(cpu_coregroup_mask, cpu_core_flags, MC),
 #endif
-	{ cpu_cpu_mask, SD_INIT_NAME(PKG) },
+	SDTL(cpu_cpu_mask, NULL, PKG),
 	{ NULL, },
 };
 
@@ -2010,11 +2010,7 @@ void sched_init_numa(int offline_node)
 	/*
 	 * Add the NUMA identity distance, aka single NODE.
 	 */
-	tl[i++] = (struct sched_domain_topology_level){
-		.mask = sd_numa_mask,
-		.numa_level = 0,
-		SD_INIT_NAME(NODE)
-	};
+	tl[i++] = SDTL(sd_numa_mask, NULL, NODE);
 
 	/*
 	 * .. and append 'j' levels of NUMA goodness.
@@ -2025,7 +2021,7 @@ void sched_init_numa(int offline_node)
 			.sd_flags = cpu_numa_flags,
 			.flags = SDTL_OVERLAP,
 			.numa_level = j,
-			SD_INIT_NAME(NUMA)
+			.name = "NUMA",
 		};
 	}
 
Signature · 1 line
2.49.0
reviewer Tested-by on v4 1/4
#2 lore raw

Re: [PATCH v4 1/4] smpboot: introduce SDTL() helper to tidy sched topology setup

Hello Li,

Apart from few comments inline below, feel free to include:

for the entire series.

Quoted 5 lines · Li Chen
include/linux/sched/topology.h +0 -0
index 198bb5cc1774b..0b53e372c445c 100644

include/linux/sched/topology.h struct sched_domain_topology_level { · line 202

Quoted 9 lines
include/linux/sched/topology.h +3 -3
@@ -197,9 +197,9 @@ struct sched_domain_topology_level {
 extern void __init set_sched_topology(struct sched_domain_topology_level *tl);
 extern void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio);
 
-
-# define SD_INIT_NAME(type)		.name = #type
-
+#define SDTL(maskfn, flagsfn, dname) \
+	((struct sched_domain_topology_level) \
+	    { .mask = maskfn, .sd_flags = flagsfn, .name = #dname, .numa_level = 0 })

I prefer the following alignment:

#define SDTL(maskfn, flagsfn, dname) ((struct sched_domain_topology_level) \
{ .mask = maskfn, .sd_flags = flagsfn, .name = #dname })

instead of having 3 lines. "numa_level" is 0 by default so I don't think we need to explicitly specify it again.

Also perhaps the macro can be named "SDTL_INIT()" to keep consistent with the naming convention.

include/linux/sched/topology.h struct sched_domain_topology_level { · line 203

Quoted 1 line
include/linux/sched/topology.h +0 -0
 #else /* CONFIG_SMP */

A bunch of the CONFIG_SMP related ifdeffry is being removed for the next cycle. You can perhaps rebase the series on top of the tip tree (git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git)

Quoted 2 lines
include/linux/sched/topology.h +0 -0
 struct sched_domain_attr;

[..snip..]

kernel/sched/topology.c void sched_init_numa(int offline_node) · line 2024

Quoted 10 lines
kernel/sched/topology.c +1 -1
index b958fe48e0205..e6ec65ae4b75d 100644
@@ -2025,7 +2021,7 @@ void sched_init_numa(int offline_node)
 			.sd_flags = cpu_numa_flags,
 			.flags = SDTL_OVERLAP,
 			.numa_level = j,
-			SD_INIT_NAME(NUMA)
+			.name = "NUMA",

This can use SDTL() macro too. Just explicitly set "tl[i].numa_level" to "j" after.

Quoted 3 lines
kernel/sched/topology.c +0 -0
 		};
 	}
 
Signature · 2 lines
Thanks and Regards,
Prateek
author
#3 lore raw

Re: [PATCH v4 1/4] smpboot: introduce SDTL() helper to tidy sched topology setup

Hi K,

Thanks for your reviews and test! I have addressed all issues in v5:
https://www.spinics.net/lists/kernel/msg5761848.html

---- On Mon, 07 Jul 2025 13:33:53 +0800 K Prateek Nayak <[email protected]> wrote ---

Quoted 60 lines

Hello Li,

Apart from few comments inline below, feel free to include:

for the entire series.

Quoted 5 lines · Li Chen
include/linux/sched/topology.h +0 -0
index 198bb5cc1774b..0b53e372c445c 100644

include/linux/sched/topology.h struct sched_domain_topology_level { · line 202

Quoted 9 lines
include/linux/sched/topology.h +3 -3
@@ -197,9 +197,9 @@ struct sched_domain_topology_level {
 extern void __init set_sched_topology(struct sched_domain_topology_level *tl);
 extern void sched_update_asym_prefer_cpu(int cpu, int old_prio, int new_prio);
 
-
-# define SD_INIT_NAME(type)        .name = #type
-
+#define SDTL(maskfn, flagsfn, dname) \
+    ((struct sched_domain_topology_level) \
+        { .mask = maskfn, .sd_flags = flagsfn, .name = #dname, .numa_level = 0 })

I prefer the following alignment:

#define SDTL(maskfn, flagsfn, dname) ((struct sched_domain_topology_level) \
{ .mask = maskfn, .sd_flags = flagsfn, .name = #dname })

instead of having 3 lines. "numa_level" is 0 by default so I don't think we need to explicitly specify it again.

Also perhaps the macro can be named "SDTL_INIT()" to keep consistent with the naming convention.

include/linux/sched/topology.h struct sched_domain_topology_level { · line 203

Quoted 1 line
include/linux/sched/topology.h +0 -0
 #else /* CONFIG_SMP */

A bunch of the CONFIG_SMP related ifdeffry is being removed for the next cycle. You can perhaps rebase the series on top of the tip tree (git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git)

Quoted 2 lines
include/linux/sched/topology.h +0 -0
 struct sched_domain_attr;

[..snip..]

kernel/sched/topology.c void sched_init_numa(int offline_node) · line 2024

Quoted 10 lines
kernel/sched/topology.c +1 -1
index b958fe48e0205..e6ec65ae4b75d 100644
@@ -2025,7 +2021,7 @@ void sched_init_numa(int offline_node)
             .sd_flags = cpu_numa_flags,
             .flags = SDTL_OVERLAP,
             .numa_level = j,
-            SD_INIT_NAME(NUMA)
+            .name = "NUMA",

This can use SDTL() macro too. Just explicitly set "tl[i].numa_level" to "j" after.

Quoted 9 lines
Quoted 3 lines
kernel/sched/topology.c +0 -0
         };
     }
 
Signature · 2 lines
Thanks and Regards,
Prateek

Regards,

Li​

author
#4 lore raw

PATCH v4 2/4 x86/smpboot: remove redundant CONFIG_SCHED_SMT

In reply to Li Chen

From: Li Chen <[email protected]>

CONFIG_SCHED_SMT is default y if SMP is enabled,
so let's simply drop CONFIG_SCHED_SMT.

---
 arch/x86/kernel/smpboot.c | 2 --
 1 file changed, 2 deletions(-)
arch/x86/kernel/smpboot.c +0 -2
index 55f09946c791b..6e36306632792 100644
@@ -484,9 +484,7 @@ static void __init build_sched_topology(void)
 {
 	int i = 0;
 
-#ifdef CONFIG_SCHED_SMT
 	x86_topology[i++] = SDTL(cpu_smt_mask, cpu_smt_flags, SMT);
-#endif
 #ifdef CONFIG_SCHED_CLUSTER
 	x86_topology[i++] = SDTL(cpu_clustergroup_mask, x86_cluster_flags, CLS);
 #endif
Signature · 1 line
2.49.0
author
#5 lore raw

PATCH v4 3/4 x86/smpboot: moves x86_topology to static initialize and truncate

In reply to Li Chen

From: Li Chen <[email protected]>

The #ifdeffery and the initializers in build_sched_topology() are just disgusting.

Statically initialize the domain levels in the topology array and let build_sched_topology() invalidate the package domain level when NUMA in package is available.

---
 arch/x86/kernel/smpboot.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)
arch/x86/kernel/smpboot.c +16 -18
index 6e36306632792..cd70e5322462a 100644
@@ -478,32 +478,30 @@ static int x86_cluster_flags(void)
  */
 static bool x86_has_numa_in_package;
 
-static struct sched_domain_topology_level x86_topology[6];
-
-static void __init build_sched_topology(void)
-{
-	int i = 0;
-
-	x86_topology[i++] = SDTL(cpu_smt_mask, cpu_smt_flags, SMT);
+static struct sched_domain_topology_level x86_topology[] = {
+	SDTL(cpu_smt_mask, cpu_smt_flags, SMT),
 #ifdef CONFIG_SCHED_CLUSTER
-	x86_topology[i++] = SDTL(cpu_clustergroup_mask, x86_cluster_flags, CLS);
+	SDTL(cpu_clustergroup_mask, x86_cluster_flags, CLS),
 #endif
 #ifdef CONFIG_SCHED_MC
-	x86_topology[i++] = SDTL(cpu_coregroup_mask, x86_core_flags, MC);
+	SDTL(cpu_coregroup_mask, x86_core_flags, MC),
 #endif
-	/*
-	 * When there is NUMA topology inside the package skip the PKG domain
-	 * since the NUMA domains will auto-magically create the right spanning
-	 * domains based on the SLIT.
-	 */
-	if (!x86_has_numa_in_package)
-		x86_topology[i++] = SDTL(cpu_cpu_mask, x86_sched_itmt_flags, PKG);
+	SDTL(cpu_cpu_mask, x86_sched_itmt_flags, PKG),
+	{ NULL },
+};
 
+static void __init build_sched_topology(void)
+{
 	/*
-	 * There must be one trailing NULL entry left.
+	 * When there is NUMA topology inside the package invalidate the
+	 * PKG domain since the NUMA domains will auto-magically create the
+	 * right spanning domains based on the SLIT.
 	 */
-	BUG_ON(i >= ARRAY_SIZE(x86_topology) - 1);
+	if (x86_has_numa_in_package) {
+		unsigned int pkgdom = ARRAY_SIZE(x86_topology) - 2;
 
+		memset(&x86_topology[pkgdom], 0, sizeof(x86_topology[pkgdom]));
+	}
 	set_sched_topology(x86_topology);
 }
 
Signature · 1 line
2.49.0
author
#6 lore raw

PATCH v4 4/4 x86/smpboot: avoid SMT domain attach/destroy if SMT is not enabled

In reply to Li Chen

From: Li Chen <[email protected]>

Currently, the SMT domain is added into sched_domain_topology by default.

If cpu_attach_domain() finds that the CPU SMT domain’s cpumask_weight is just 1, it will destroy it.

On a large machine, such as one with 512 cores, this results in 512 redundant domain attach/destroy operations.

Avoid these unnecessary operations by simply checking cpu_smt_num_threads and skip SMT domain if the SMT domain is not enabled.

---
 arch/x86/kernel/smpboot.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kernel/smpboot.c +11 -1
index cd70e5322462a..8c1960a455bfb 100644
@@ -492,6 +492,8 @@ static struct sched_domain_topology_level x86_topology[] = {
 
 static void __init build_sched_topology(void)
 {
+	struct sched_domain_topology_level *topology = x86_topology;
+
 	/*
 	 * When there is NUMA topology inside the package invalidate the
 	 * PKG domain since the NUMA domains will auto-magically create the
@@ -502,7 +504,15 @@ static void __init build_sched_topology(void)
 
 		memset(&x86_topology[pkgdom], 0, sizeof(x86_topology[pkgdom]));
 	}
-	set_sched_topology(x86_topology);
+
+    /*
+     * Drop the SMT domains if there is only one thread per-core
+     * since it'll get degenerated by the scheduler anyways.
+     */
+	if (cpu_smt_num_threads <= 1)
+		++topology;
+
+	set_sched_topology(topology);
 }
 
 void set_cpu_sibling_map(int cpu)
Signature · 1 line
2.49.0
#7 lore raw

Re: [PATCH v4 4/4] x86/smpboot: avoid SMT domain attach/destroy if SMT is not enabled

Hello Li,

Quoted 7 lines · Li Chen

From: Li Chen <[email protected]>

Currently, the SMT domain is added into sched_domain_topology by default.

If cpu_attach_domain() finds that the CPU SMT domain’s cpumask_weight is just 1, it will destroy it.

Quoted 8 lines

On a large machine, such as one with 512 cores, this results in 512 redundant domain attach/destroy operations.

Avoid these unnecessary operations by simply checking cpu_smt_num_threads and skip SMT domain if the SMT domain is not enabled.

"Suggested-by:" should be fine :)

Quoted 20 lines
---
 arch/x86/kernel/smpboot.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kernel/smpboot.c +2 -0
index cd70e5322462a..8c1960a455bfb 100644
@@ -492,6 +492,8 @@ static struct sched_domain_topology_level x86_topology[] = {
 
 static void __init build_sched_topology(void)
 {
+	struct sched_domain_topology_level *topology = x86_topology;
+
 	/*
 	 * When there is NUMA topology inside the package invalidate the
 	 * PKG domain since the NUMA domains will auto-magically create the

arch/x86/kernel/smpboot.c static void __init build_sched_topology(void) · line 511

Quoted 9 lines
arch/x86/kernel/smpboot.c +5 -1
@@ -502,7 +504,15 @@ static void __init build_sched_topology(void)
 
 		memset(&x86_topology[pkgdom], 0, sizeof(x86_topology[pkgdom]));
 	}
-	set_sched_topology(x86_topology);
+
+    /*
+     * Drop the SMT domains if there is only one thread per-core
+     * since it'll get degenerated by the scheduler anyways.
+     */

The comment above has spaces instead of tab before it.

Quoted 6 lines
arch/x86/kernel/smpboot.c +3 -0
+	if (cpu_smt_num_threads <= 1)
+		++topology;> +
+	set_sched_topology(topology);
 }
 
 void set_cpu_sibling_map(int cpu)
Signature · 2 lines
Thanks and Regards,
Prateek