Skip to contentSkip to conversation

cpu: memoise number of possible cpus

PATCH In review Alexey Dobriyan 3 participants 4 messages Apr 18, 2024 – Apr 19, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu: memoise number of possible cpus

cpu_possible_mask is fixed after boot, so it makes sense
to calculate number of possible cpus to
a) make num_possible_cpus() faster (distros ship with _large_ NR_CPUS),
b) unscrew codegen elsewhere replacing function call
with simple memory load.

---
 include/linux/cpumask.h | 3 ++-
 init/main.c             | 3 +++
 kernel/cpu.c            | 3 +++
 3 files changed, 8 insertions(+), 1 deletion(-)
include/linux/cpumask.h +2 -1
index 04536a29f10f..a98843ca6131 100644
@@ -1106,7 +1106,8 @@ static __always_inline unsigned int num_online_cpus(void)
 {
 	return raw_atomic_read(&__num_online_cpus);
 }
-#define num_possible_cpus()	cpumask_weight(cpu_possible_mask)
+extern unsigned int num_possible_cpus;
+#define num_possible_cpus()	num_possible_cpus
 #define num_present_cpus()	cpumask_weight(cpu_present_mask)
 #define num_active_cpus()	cpumask_weight(cpu_active_mask)
 
init/main.c +3 -0
index 881f6230ee59..fe0291b44d78 100644
@@ -904,6 +904,9 @@ void start_kernel(void)
 	setup_boot_config();
 	setup_command_line(command_line);
 	setup_nr_cpu_ids();
+#if NR_CPUS > 1
+	num_possible_cpus = cpumask_weight(cpu_possible_mask);
+#endif
 	setup_per_cpu_areas();
 	smp_prepare_boot_cpu();	/* arch-specific boot-cpu hooks */
 	boot_cpu_hotplug_init();
kernel/cpu.c +3 -0
index 07ad53b7f119..4a75f95fec82 100644
@@ -3106,6 +3106,9 @@ EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
 const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
 EXPORT_SYMBOL(cpu_all_bits);
 
+unsigned int num_possible_cpus __ro_after_init = 1;
+EXPORT_SYMBOL(num_possible_cpus);
+
 #ifdef CONFIG_INIT_ALL_POSSIBLE
 struct cpumask __cpu_possible_mask __ro_after_init
 	= {CPU_BITS_ALL};
Signature · 1 line
2.43.2
#1 lore raw
Quoted 5 lines · On Thu, 18 Apr 2024 07:19:27 +0300 Alexey Dobriyan <[email protected]> wrote:

cpu_possible_mask is fixed after boot, so it makes sense
to calculate number of possible cpus to
a) make num_possible_cpus() faster (distros ship with _large_ NR_CPUS),
b) unscrew codegen elsewhere replacing function call
with simple memory load.

There are a lot of calls to set_cpu_possible(). Perhaps calculating num_possible_cpus within there would reduce risk of things getting out of sync, either now or in the future.

reset_cpu_possible_mask() appears to have no callers. Kill?

#2 lore raw
Quoted 1 line · On Thu, 18 Apr 2024 13:26:00 -0700 Andrew Morton <[email protected]>…

reset_cpu_possible_mask() appears to have no callers. Kill?

oh, you already did.

#3 lore raw
In reply to Alexey Dobriyan

On Thu, Apr 18 2024 at 07:19, Alexey Dobriyan wrote:

memoise?

Quoted 2 lines

cpu_possible_mask is fixed after boot, so it makes sense to calculate number of possible cpus to

The kernel calculates the number of possible CPUs already today, no?

Quoted 3 lines

a) make num_possible_cpus() faster (distros ship with _large_ NR_CPUS),
b) unscrew codegen elsewhere replacing function call
with simple memory load.

Can we please have complete sentences which use precise technical wording to describe the changes?

init/main.c void start_kernel(void) · line 909

Quoted 11 lines
init/main.c +3 -0
index 881f6230ee59..fe0291b44d78 100644
@@ -904,6 +904,9 @@ void start_kernel(void)
 	setup_boot_config();
 	setup_command_line(command_line);
 	setup_nr_cpu_ids();
+#if NR_CPUS > 1
+	num_possible_cpus = cpumask_weight(cpu_possible_mask);
+#endif

setup_nr_cpu_ids() does exactly the same thing despite using a different algorithm. So why not do the obvious and have:

#define num_possible_cpus() nr_cpu_ids

and make nr_cpu_ids __ro_after_init?

Which made me look at CONFIG_FORCE_NR_CPUS. That's simply broken because:

static inline void set_nr_cpu_ids(unsigned int nr) {

#if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
       WARN_ON(nr != nr_cpu_ids);
#else
        nr_cpu_ids = nr;

#endif
}

So if num_possible_cpus() != nr_cpu_ids then everything after that becomes lottery. If that hard-coded NR_CPUS is actually worth it then this WARN_ON() is just wrong. The only sensible solution to that is to make it a BUG_ON().

Thanks,

tglx