x86/smpboot: tidy sched-topology and drop useless SMT level
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
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
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
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
-0struct 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@@ -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
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:
Tested-by K Prateek Nayak <[email protected]>for the entire series.
Quoted 5 lines · Li Chen
include/linux/sched/topology.h +0
-0include/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)((structsched_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_SMPrelated 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
-0struct 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@@ -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
PATCH v4 2/4 x86/smpboot: remove redundant CONFIG_SCHED_SMT
In reply to Li ChenFrom: 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
@@ -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
PATCH v4 3/4 x86/smpboot: moves x86_topology to static initialize and truncate
In reply to Li ChenFrom: 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
@@ -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
PATCH v4 4/4 x86/smpboot: avoid SMT domain attach/destroy if SMT is not enabled
In reply to Li ChenFrom: 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
@@ -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
K Prateek Nayak
reviewerRe: [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_topologyby default.If
cpu_attach_domain()finds that the CPU SMT domain’scpumask_weightis 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_threadsand skip SMT domain if the SMT domain is not enabled.Signed-off-by K Prateek Nayak <[email protected]>
"Suggested-by:" should be fine :)
Quoted 20 lines
Tested-by K Prateek Nayak <[email protected]>Signed-off-by Li Chen <[email protected]>--- arch/x86/kernel/smpboot.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)arch/x86/kernel/smpboot.c +2
-0@@ -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