Skip to content

Commit 35a5c37

Browse files
committed
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
1 parent 79c11fb commit 35a5c37

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

include/linux/cpumask.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ extern struct cpumask __cpu_dying_mask;
126126
#define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask)
127127

128128
extern atomic_t __num_online_cpus;
129+
extern unsigned int __num_possible_cpus;
129130

130131
extern cpumask_t cpus_booted_once_mask;
131132

@@ -1152,13 +1153,13 @@ void init_cpu_possible(const struct cpumask *src);
11521153
#define __assign_cpu(cpu, mask, val) \
11531154
__assign_bit(cpumask_check(cpu), cpumask_bits(mask), (val))
11541155

1155-
#define set_cpu_possible(cpu, possible) assign_cpu((cpu), &__cpu_possible_mask, (possible))
11561156
#define set_cpu_enabled(cpu, enabled) assign_cpu((cpu), &__cpu_enabled_mask, (enabled))
11571157
#define set_cpu_present(cpu, present) assign_cpu((cpu), &__cpu_present_mask, (present))
11581158
#define set_cpu_active(cpu, active) assign_cpu((cpu), &__cpu_active_mask, (active))
11591159
#define set_cpu_dying(cpu, dying) assign_cpu((cpu), &__cpu_dying_mask, (dying))
11601160

11611161
void set_cpu_online(unsigned int cpu, bool online);
1162+
void set_cpu_possible(unsigned int cpu, bool possible);
11621163

11631164
/**
11641165
* to_cpumask - convert a NR_CPUS bitmap to a struct cpumask *
@@ -1211,7 +1212,12 @@ static __always_inline unsigned int num_online_cpus(void)
12111212
{
12121213
return raw_atomic_read(&__num_online_cpus);
12131214
}
1214-
#define num_possible_cpus() cpumask_weight(cpu_possible_mask)
1215+
1216+
static __always_inline unsigned int num_possible_cpus(void)
1217+
{
1218+
return __num_possible_cpus;
1219+
}
1220+
12151221
#define num_enabled_cpus() cpumask_weight(cpu_enabled_mask)
12161222
#define num_present_cpus() cpumask_weight(cpu_present_mask)
12171223
#define num_active_cpus() cpumask_weight(cpu_active_mask)

kernel/cpu.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3085,10 +3085,13 @@ EXPORT_SYMBOL(cpu_all_bits);
30853085
#ifdef CONFIG_INIT_ALL_POSSIBLE
30863086
struct cpumask __cpu_possible_mask __ro_after_init
30873087
= {CPU_BITS_ALL};
3088+
unsigned int __num_possible_cpus __ro_after_init = NR_CPUS;
30883089
#else
30893090
struct cpumask __cpu_possible_mask __ro_after_init;
3091+
unsigned int __num_possible_cpus __ro_after_init;
30903092
#endif
30913093
EXPORT_SYMBOL(__cpu_possible_mask);
3094+
EXPORT_SYMBOL(__num_possible_cpus);
30923095

30933096
struct cpumask __cpu_online_mask __read_mostly;
30943097
EXPORT_SYMBOL(__cpu_online_mask);
@@ -3116,6 +3119,7 @@ void init_cpu_present(const struct cpumask *src)
31163119
void init_cpu_possible(const struct cpumask *src)
31173120
{
31183121
cpumask_copy(&__cpu_possible_mask, src);
3122+
__num_possible_cpus = cpumask_weight(&__cpu_possible_mask);
31193123
}
31203124

31213125
void set_cpu_online(unsigned int cpu, bool online)
@@ -3139,6 +3143,21 @@ void set_cpu_online(unsigned int cpu, bool online)
31393143
}
31403144
}
31413145

3146+
/*
3147+
* This should be marked __init, but there is a boatload of call sites
3148+
* which need to be fixed up to do so. Sigh...
3149+
*/
3150+
void set_cpu_possible(unsigned int cpu, bool possible)
3151+
{
3152+
if (possible) {
3153+
if (!cpumask_test_and_set_cpu(cpu, &__cpu_possible_mask))
3154+
__num_possible_cpus++;
3155+
} else {
3156+
if (cpumask_test_and_clear_cpu(cpu, &__cpu_possible_mask))
3157+
__num_possible_cpus--;
3158+
}
3159+
}
3160+
31423161
/*
31433162
* Activate the first processor.
31443163
*/

0 commit comments

Comments
 (0)