Re: [PATCH 1/2] cpu/hotplug: introduce 'num_dying_cpus' to get dying CPUs count

From: Yury Norov
Date: Mon Apr 03 2023 - 22:24:39 EST


On Tue, Apr 04, 2023 at 09:42:05AM +0800, Ye Bin wrote:
> From: Ye Bin <yebin10@xxxxxxxxxx>
>
> Introduce '__num_dying_cpus' variable to cache the number of dying CPUs
> in the core and just return the cached variable.
>
> Signed-off-by: Ye Bin <yebin10@xxxxxxxxxx>
> ---
> include/linux/cpumask.h | 20 ++++++++++++++++----
> kernel/cpu.c | 2 ++
> 2 files changed, 18 insertions(+), 4 deletions(-)
>
> diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
> index 2a61ddcf8321..8127fd598f51 100644
> --- a/include/linux/cpumask.h
> +++ b/include/linux/cpumask.h
> @@ -135,6 +135,8 @@ extern struct cpumask __cpu_dying_mask;
>
> extern atomic_t __num_online_cpus;
>
> +extern atomic_t __num_dying_cpus;
> +
> extern cpumask_t cpus_booted_once_mask;
>
> static __always_inline void cpu_max_bits_warn(unsigned int cpu, unsigned int bits)
> @@ -1018,10 +1020,14 @@ set_cpu_active(unsigned int cpu, bool active)
> static __always_inline void
> set_cpu_dying(unsigned int cpu, bool dying)
> {
> - if (dying)
> - cpumask_set_cpu(cpu, &__cpu_dying_mask);
> - else
> - cpumask_clear_cpu(cpu, &__cpu_dying_mask);
> + if (dying) {
> + if (!cpumask_test_and_set_cpu(cpu, &__cpu_dying_mask))
> + atomic_inc(&__num_dying_cpus);
> + }
> + else {
> + if (cpumask_test_and_clear_cpu(cpu, &__cpu_dying_mask))
> + atomic_dec(&__num_dying_cpus);
> + }
> }

Corresponding set_cpu_online() is implemented in C-file probably for a
reason. Are you sure that similar function for dying mask should
reside in a header? If so, can you share your reasoning?

Regardless, now that you added the identical function to
set_cpu_online, I think it's worth to make it a general approach:

void set_cpu_counted(unsigned int cpu, bool set,
struct cpumask *mask, atomic_t *cnt);

void __always_inline set_cpu_online(unsigned int cpu, bool online)
{
set_cpu_counted(cpu, online, &__cpu_online_mask, &__num_online_cpus);
}

void __always_inline set_cpu_dying(unsigned int cpu, bool dying)
{
set_cpu_counted(cpu, dying, &__cpu_dying_mask, &__num_dying_cpus);
}