Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cpumask: Cache num_possible_cpus()

Reevaluating num_possible_cpus() over and over does not make sense. That
becomes a constant after init as cpu_possible_mask is marked ro_after_init.

Cache the value during initialization and provide that for consumption.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Yury Norov <yury.norov@gmail.com>
Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Reviewed-by: Shrikanth Hegde <sshegde@linux.ibm.com>
Link: https://patch.msgid.link/20251119172549.578653738@linutronix.de

+27 -2
+8 -2
include/linux/cpumask.h
··· 126 126 #define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask) 127 127 128 128 extern atomic_t __num_online_cpus; 129 + extern unsigned int __num_possible_cpus; 129 130 130 131 extern cpumask_t cpus_booted_once_mask; 131 132 ··· 1153 1152 #define __assign_cpu(cpu, mask, val) \ 1154 1153 __assign_bit(cpumask_check(cpu), cpumask_bits(mask), (val)) 1155 1154 1156 - #define set_cpu_possible(cpu, possible) assign_cpu((cpu), &__cpu_possible_mask, (possible)) 1157 1155 #define set_cpu_enabled(cpu, enabled) assign_cpu((cpu), &__cpu_enabled_mask, (enabled)) 1158 1156 #define set_cpu_present(cpu, present) assign_cpu((cpu), &__cpu_present_mask, (present)) 1159 1157 #define set_cpu_active(cpu, active) assign_cpu((cpu), &__cpu_active_mask, (active)) 1160 1158 #define set_cpu_dying(cpu, dying) assign_cpu((cpu), &__cpu_dying_mask, (dying)) 1161 1159 1162 1160 void set_cpu_online(unsigned int cpu, bool online); 1161 + void set_cpu_possible(unsigned int cpu, bool possible); 1163 1162 1164 1163 /** 1165 1164 * to_cpumask - convert a NR_CPUS bitmap to a struct cpumask * ··· 1212 1211 { 1213 1212 return raw_atomic_read(&__num_online_cpus); 1214 1213 } 1215 - #define num_possible_cpus() cpumask_weight(cpu_possible_mask) 1214 + 1215 + static __always_inline unsigned int num_possible_cpus(void) 1216 + { 1217 + return __num_possible_cpus; 1218 + } 1219 + 1216 1220 #define num_enabled_cpus() cpumask_weight(cpu_enabled_mask) 1217 1221 #define num_present_cpus() cpumask_weight(cpu_present_mask) 1218 1222 #define num_active_cpus() cpumask_weight(cpu_active_mask)
+19
kernel/cpu.c
··· 3085 3085 #ifdef CONFIG_INIT_ALL_POSSIBLE 3086 3086 struct cpumask __cpu_possible_mask __ro_after_init 3087 3087 = {CPU_BITS_ALL}; 3088 + unsigned int __num_possible_cpus __ro_after_init = NR_CPUS; 3088 3089 #else 3089 3090 struct cpumask __cpu_possible_mask __ro_after_init; 3091 + unsigned int __num_possible_cpus __ro_after_init; 3090 3092 #endif 3091 3093 EXPORT_SYMBOL(__cpu_possible_mask); 3094 + EXPORT_SYMBOL(__num_possible_cpus); 3092 3095 3093 3096 struct cpumask __cpu_online_mask __read_mostly; 3094 3097 EXPORT_SYMBOL(__cpu_online_mask); ··· 3119 3116 void init_cpu_possible(const struct cpumask *src) 3120 3117 { 3121 3118 cpumask_copy(&__cpu_possible_mask, src); 3119 + __num_possible_cpus = cpumask_weight(&__cpu_possible_mask); 3122 3120 } 3123 3121 3124 3122 void set_cpu_online(unsigned int cpu, bool online) ··· 3140 3136 } else { 3141 3137 if (cpumask_test_and_clear_cpu(cpu, &__cpu_online_mask)) 3142 3138 atomic_dec(&__num_online_cpus); 3139 + } 3140 + } 3141 + 3142 + /* 3143 + * This should be marked __init, but there is a boatload of call sites 3144 + * which need to be fixed up to do so. Sigh... 3145 + */ 3146 + void set_cpu_possible(unsigned int cpu, bool possible) 3147 + { 3148 + if (possible) { 3149 + if (!cpumask_test_and_set_cpu(cpu, &__cpu_possible_mask)) 3150 + __num_possible_cpus++; 3151 + } else { 3152 + if (cpumask_test_and_clear_cpu(cpu, &__cpu_possible_mask)) 3153 + __num_possible_cpus--; 3143 3154 } 3144 3155 } 3145 3156