Re: [PATCH] cpu/hotplug: Cache number of online CPUs

From: Mathieu Desnoyers
Date: Thu Jul 04 2019 - 16:59:51 EST


----- On Jul 4, 2019, at 4:42 PM, Thomas Gleixner tglx@xxxxxxxxxxxxx wrote:

> Revaluating the bitmap wheight of the online cpus bitmap in every
> invocation of num_online_cpus() over and over is a pretty useless
> exercise. Especially when num_online_cpus() is used in code pathes like the
> IPI delivery of x86 or the membarrier code.
>
> Cache the number of online CPUs in the core and just return the cached
> variable.
>
> Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> ---
> include/linux/cpumask.h | 16 +++++++---------
> kernel/cpu.c | 16 ++++++++++++++++
> 2 files changed, 23 insertions(+), 9 deletions(-)
>
> --- a/include/linux/cpumask.h
> +++ b/include/linux/cpumask.h
> @@ -95,8 +95,13 @@ extern struct cpumask __cpu_active_mask;
> #define cpu_present_mask ((const struct cpumask *)&__cpu_present_mask)
> #define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask)
>
> +extern unsigned int __num_online_cpus;

[...]

> +
> +void set_cpu_online(unsigned int cpu, bool online)
> +{
> + lockdep_assert_cpus_held();

I don't think it is required that the cpu_hotplug lock is held
when reading __num_online_cpus, right ?

I would have expected the increment/decrement below to be performed
with a WRITE_ONCE(), and use a READ_ONCE() when reading the current
value.

Thanks,

Mathieu

> +
> + if (online) {
> + if (!cpumask_test_and_set_cpu(cpu, &__cpu_online_mask))
> + __num_online_cpus++;
> + } else {
> + if (cpumask_test_and_clear_cpu(cpu, &__cpu_online_mask))
> + __num_online_cpus--;
> + }
> +}
> +
> /*
> * Activate the first processor.
> */

--
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com