Skip to contentSkip to conversation

riscv: Populate boot CPU topology on UP kernels

PATCH In review Chen Pei 4 participants 6 messages Oct 12 – Oct 21 RISC-V

lore
Share & tools
lore

Chen Pei

author
#0 lore raw

PATCH 1/1 riscv: Populate boot CPU topology on UP kernels

RISC-V selects GENERIC_ARCH_TOPOLOGY unconditionally, so the topology cpumask accessors resolve into cpu_topology[]. The RISC-V calls to init_cpu_topology() and store_cpu_topology() are in smpboot.c, which is only built with CONFIG_SMP. On UP kernels, cpu_topology[] remains zero-initialized and its sibling masks are empty.

The sysfs topology interface is available on UP kernels too. As a result, cpu0 exports empty thread_siblings_list and core_siblings_list attributes rather than masks containing itself, violating the expected topology semantics of this stable ABI.

Initialize and store the boot CPU topology in a UP-only early initcall. This runs after DT and ACPI CPU discovery and after the memory allocator is available, before the topology is exposed through sysfs.

SMP kernels continue to initialize topology in smp_prepare_cpus(). NUMA needs no additional handling because it depends on SMP.

Fixes fbd92809997a ("riscv: topology: fix default topology reporting")

---
Tested on QEMU virt (RV64):
- UP, default DT: cpu0 thread_siblings_list, core_cpus_list, core_siblings_list and package_cpus_list all contain CPU0.
- UP, DT with 32 KiB L1 instruction/data caches: the same topology checks pass, and both caches appear in sysfs with shared_cpu_list=0.
- SMP, 8 CPUs: each CPU's thread_siblings_list and core_cpus_list contain itself; core_siblings_list and package_cpus_list are 0-7.

 arch/riscv/kernel/setup.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/riscv/kernel/setup.c +12 -0
index d79f99b86033..234d9af414b7 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/arch_topology.h>
 #include <linux/cpu.h>
 #include <linux/init.h>
 #include <linux/mm.h>
@@ -370,6 +371,17 @@ void __init setup_arch(char **cmdline_p)
 	riscv_clear_hypervisor_csr();
 }
 
+#ifndef CONFIG_SMP
+static int __init riscv_init_cpu_topology(void)
+{
+	init_cpu_topology();
+	store_cpu_topology(0);
+
+	return 0;
+}
+early_initcall(riscv_init_cpu_topology);
+#endif
+
 bool arch_cpu_is_hotpluggable(int cpu)
 {
 	return cpu_has_hotplug(cpu);
Signature · 1 line
2.50.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 50 lines · Chen Pei

RISC-V selects GENERIC_ARCH_TOPOLOGY unconditionally, so the topology cpumask accessors resolve into cpu_topology[]. The RISC-V calls to init_cpu_topology() and store_cpu_topology() are in smpboot.c, which is only built with CONFIG_SMP. On UP kernels, cpu_topology[] remains zero-initialized and its sibling masks are empty.

The sysfs topology interface is available on UP kernels too. As a result, cpu0 exports empty thread_siblings_list and core_siblings_list attributes rather than masks containing itself, violating the expected topology semantics of this stable ABI.

Initialize and store the boot CPU topology in a UP-only early initcall. This runs after DT and ACPI CPU discovery and after the memory allocator is available, before the topology is exposed through sysfs.

SMP kernels continue to initialize topology in smp_prepare_cpus(). NUMA needs no additional handling because it depends on SMP.

Fixes fbd92809997a ("riscv: topology: fix default topology reporting")

---
Tested on QEMU virt (RV64):
- UP, default DT: cpu0 thread_siblings_list, core_cpus_list, core_siblings_list and package_cpus_list all contain CPU0.
- UP, DT with 32 KiB L1 instruction/data caches: the same topology checks pass, and both caches appear in sysfs with shared_cpu_list=0.
- SMP, 8 CPUs: each CPU's thread_siblings_list and core_cpus_list contain itself; core_siblings_list and package_cpus_list are 0-7.

 arch/riscv/kernel/setup.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/riscv/kernel/setup.c +2 -0
index d79f99b86033..234d9af414b7 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/arch_topology.h>
 #include <linux/cpu.h>
 #include <linux/init.h>
 #include <linux/mm.h>
@@ -370,6 +371,17 @@ void __init setup_arch(char **cmdline_p)
        riscv_clear_hypervisor_csr();
 }
 
+#ifndef CONFIG_SMP

arch/riscv/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 384

Quoted 10 lines
arch/riscv/kernel/setup.c +10 -0
+static int __init riscv_init_cpu_topology(void)
+{
+       init_cpu_topology();
+       store_cpu_topology(0);
+
+       return 0;
+}
+early_initcall(riscv_init_cpu_topology);
+#endif
+

Good catch. Although CONFIG_SMP is enabled by default (default y), we should still set the cpumask to BIT(0), so the cpumask stays valid even when SMP is disabled.

Signature · 2 lines
Best Regards
 Guo Ren
#2 lore raw
In reply to Chen Pei
Quoted 5 lines · Chen Pei

RISC-V selects GENERIC_ARCH_TOPOLOGY unconditionally, so the topology cpumask accessors resolve into cpu_topology[]. The RISC-V calls to init_cpu_topology() and store_cpu_topology() are in smpboot.c, which is only built with CONFIG_SMP. On UP kernels, cpu_topology[] remains zero-initialized and its sibling masks are empty.

I'm curious about arm64 since they've got the same as us as far as I know. Is the same issue there or are they solving it in a different way?

Cheers,
Conor.

Quoted 61 lines

The sysfs topology interface is available on UP kernels too. As a result, cpu0 exports empty thread_siblings_list and core_siblings_list attributes rather than masks containing itself, violating the expected topology semantics of this stable ABI.

Initialize and store the boot CPU topology in a UP-only early initcall. This runs after DT and ACPI CPU discovery and after the memory allocator is available, before the topology is exposed through sysfs.

SMP kernels continue to initialize topology in smp_prepare_cpus(). NUMA needs no additional handling because it depends on SMP.

Fixes fbd92809997a ("riscv: topology: fix default topology reporting")

---
Tested on QEMU virt (RV64):
- UP, default DT: cpu0 thread_siblings_list, core_cpus_list, core_siblings_list and package_cpus_list all contain CPU0.
- UP, DT with 32 KiB L1 instruction/data caches: the same topology checks pass, and both caches appear in sysfs with shared_cpu_list=0.
- SMP, 8 CPUs: each CPU's thread_siblings_list and core_cpus_list contain itself; core_siblings_list and package_cpus_list are 0-7.

 arch/riscv/kernel/setup.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
arch/riscv/kernel/setup.c +12 -0
index d79f99b86033..234d9af414b7 100644
@@ -9,6 +9,7 @@
  */
 
 #include <linux/acpi.h>
+#include <linux/arch_topology.h>
 #include <linux/cpu.h>
 #include <linux/init.h>
 #include <linux/mm.h>
@@ -370,6 +371,17 @@ void __init setup_arch(char **cmdline_p)
 	riscv_clear_hypervisor_csr();
 }
 
+#ifndef CONFIG_SMP
+static int __init riscv_init_cpu_topology(void)
+{
+	init_cpu_topology();
+	store_cpu_topology(0);
+
+	return 0;
+}
+early_initcall(riscv_init_cpu_topology);
+#endif
+
 bool arch_cpu_is_hotpluggable(int cpu)
 {
 	return cpu_has_hotplug(cpu);
Signature · 2 lines
-- 
2.50.1
#3 lore raw
Quoted 10 lines · Conor Dooley
Quoted 5 lines · Chen Pei

RISC-V selects GENERIC_ARCH_TOPOLOGY unconditionally, so the topology cpumask accessors resolve into cpu_topology[]. The RISC-V calls to init_cpu_topology() and store_cpu_topology() are in smpboot.c, which is only built with CONFIG_SMP. On UP kernels, cpu_topology[] remains zero-initialized and its sibling masks are empty.

I'm curious about arm64 since they've got the same as us as far as I know. Is the same issue there or are they solving it in a different way?

Commit 4b3dc9679cf7 ("arm64: force CONFIG_SMP=y and remove redundant #ifdefs") removed UP support and made SMP default enabled.

Signature · 2 lines
Regards,
Sudeep
author
#4 lore raw
Quoted 2 lines · Sudeep Holla

Commit 4b3dc9679cf7 ("arm64: force CONFIG_SMP=y and remove redundant #ifdefs") removed UP support and made SMP default enabled.

Thanks. RISC-V still allows UP: CONFIG_SMP is a plain bool with no default, yet arch/riscv/Kconfig selects GENERIC_ARCH_TOPOLOGY unconditionally while the only callers of init_cpu_topology() and store_cpu_topology() sit in smpboot.c, which is built only for CONFIG_SMP. That mismatch is what this patch closes.

Gating that select on SMP instead does not work. UP would then fall back to the cpumask_of(cpu) defaults in include/linux/topology.h and report the same sysfs, but vmlinux no longer links: arch_freq_scale, capacity_freq_ref, topology_scale_freq_tick and topology_set_freq_scale all become undefined references.

arch/riscv/include/asm/topology.h binds the arch_scale_freq_* and arch_set_freq_scale hooks to symbols that only drivers/base/arch_topology.c provides, and those defines suppress the generic fallbacks in kernel/sched/sched.h and include/linux/sched/topology.h. Making the gating work needs extra conditionals at several Kconfig and Makefile gates, and gives up freq and capacity invariance on UP, which is a much larger change than the twelve lines here.

Conor, and RISC-V maintainers: would you rather force CONFIG_SMP=y on RISC-V as well and drop UP altogether, or keep UP buildable and take this fix? I would keep the current approach unless you prefer the former.

Best regards,
Pei

Reviewed-by
#5 lore raw
Quoted 3 lines · Chen Pei
Quoted 2 lines · Sudeep Holla

Commit 4b3dc9679cf7 ("arm64: force CONFIG_SMP=y and remove redundant #ifdefs") removed UP support and made SMP default enabled.

Ah yes, I forgot about this..

Quoted 20 lines

Thanks. RISC-V still allows UP: CONFIG_SMP is a plain bool with no default, yet arch/riscv/Kconfig selects GENERIC_ARCH_TOPOLOGY unconditionally while the only callers of init_cpu_topology() and store_cpu_topology() sit in smpboot.c, which is built only for CONFIG_SMP. That mismatch is what this patch closes.

Gating that select on SMP instead does not work. UP would then fall back to the cpumask_of(cpu) defaults in include/linux/topology.h and report the same sysfs, but vmlinux no longer links: arch_freq_scale, capacity_freq_ref, topology_scale_freq_tick and topology_set_freq_scale all become undefined references.

arch/riscv/include/asm/topology.h binds the arch_scale_freq_* and arch_set_freq_scale hooks to symbols that only drivers/base/arch_topology.c provides, and those defines suppress the generic fallbacks in kernel/sched/sched.h and include/linux/sched/topology.h. Making the gating work needs extra conditionals at several Kconfig and Makefile gates, and gives up freq and capacity invariance on UP, which is a much larger change than the twelve lines here.

Quoted 4 lines

Conor, and RISC-V maintainers: would you rather force CONFIG_SMP=y on RISC-V as well and drop UP altogether, or keep UP buildable and take this fix? I would keep the current approach unless you prefer the former.

I think we probably should drop CONFIG_SMP=N, but this patch would be needed as a fix regardless since making SMP mandatory would be for 7.5 or later whereas fixing this is 7.4-rc content and should probably be backported to stable.

FWIW, the patch looks reasonable to me, I was just wondering why arm64 didn't need it.

Thanks,
Conor.