lib/percpu_counter, cpu/hotplug: Cure the cpu_dying_mask woes
Subsystems (6)
Dennis Zhou
maintainerv2 cover lib/percpu_counter, cpu/hotplug: Cure the cpu_dying_mask woes
Hi everyone,
This is a respin of Thomas' series [1] against v6.7-rc4. Largely it's the same minus a slight change in percpu_counter.c for batch percpu_counters and updating __percpu_counter_limited_add().
I don't think we reached an alternative resolution here so I can queue this up and give it some soak time in for-next.
[1] https://lore.kernel.org/lkml/[email protected]/
Thanks,
Dennis
Dennis Zhou (2):
lib/percpu_counter: Fix CPU hotplug handling
cpu/hotplug: Get rid of cpu_dying_mask
Thomas Gleixner (1):
cpu/hotplug: Remove export of cpu_active_mask and cpu_dying_mask
include/linux/cpuhotplug.h | 2 +- include/linux/cpumask.h | 21 ------------ kernel/cpu.c | 45 +++++++++++++++++++++----- kernel/sched/core.c | 4 +-- kernel/smpboot.h | 2 ++ lib/percpu_counter.c | 65 ++++++++++++++++---------------------- 6 files changed, 70 insertions(+), 69 deletions(-)
Signature · 1 line
2.39.1
Dennis Zhou
maintainerPATCH v2 1/3 lib/percpu_counter: Fix CPU hotplug handling
Commit 8b57b11cca88 ("pcpcntrs: fix dying cpu summation race") tried to address a race condition between percpu_counter_sum() and a concurrent CPU hotplug operation.
The race window is between the point where an un-plugged CPU removed itself from the online_cpu_mask and the hotplug state callback which folds the per CPU counters of the now dead CPU into the global count.
percpu_counter_sum() used for_each_online_cpu() to accumulate the per CPU local counts, so during the race window it missed to account for the not yet folded back local count of the offlined CPU.
The attempt to address this used the admittedly undocumented and pointlessly public cpu_dying_mask by changing the loop iterator to take both the cpu_online_mask and the cpu_dying_mask into account.
That works to some extent, but it is incorrect.
The cpu_dying_mask bits are sticky even after cpu_up()/cpu_down() completes. That means that all offlined CPUs are always taken into account. In the case of disabling SMT at boottime or runtime this results in evaluating _all_ offlined SMT siblings counters forever. Depending on system size, that's a massive amount of cache-lines to be touched forever.
It might be argued, that the cpu_dying_mask bit could be cleared when cpu_down() completes, but that's not possible under all circumstances.
Especially with partial hotplug the bit must be sticky in order to keep the initial user, i.e. the scheduler correct. Partial hotplug which allows explicit state transitions also can create a situation where the race window gets recreated:
cpu_down(target = CPUHP_PERCPU_CNT_DEAD + 1)
brings a CPU down to one state before the per CPU counter folding callback. As this did not reach CPUHP_OFFLINE state the bit would stay set. Now the next partial operation:
cpu_up(target = CPUHP_PERCPU_CNT_DEAD + 2)
has to clear the bit and the race window is open again.
There are two ways to solve this:
1) Maintain a local CPU mask in the per CPU counter code which gets the bit set when a CPU comes online and removed in the the CPUHP_PERCPU_CNT_DEAD state after folding.
This adds more code and complexity.
2) Move the folding hotplug state into the DYING callback section, which runs on the outgoing CPU immediatedly after it cleared its online bit.
There is no concurrency vs. percpu_counter_sum() on another CPU because all still online CPUs are waiting in stop_machine() for the outgoing CPU to complete its shutdown. The raw spinlock held around the CPU mask iteration prevents that an online CPU reaches the stop machine thread while iterating, which implicitely prevents the outgoing CPU from clearing its online bit.
This is way simpler than #1 and makes the hotplug calls symmetric for the price of a slightly longer wait time in stop_machine(), which is not the end of the world as CPU un-plug is already slow. The overall time for a cpu_down() operation stays exactly the same.
Implement #2 and plug the race completely.
percpu_counter_sum() is still inherently racy against a concurrent percpu_counter_add_batch() fastpath unless externally serialized. That's completely independent of CPU hotplug though.
[Dennis: Ported to v6.7-rc4. Updated percpu_counter.c for batch
percpu_counter creation and _percpu_counter_limited_add().]
--- include/linux/cpuhotplug.h | 2 +- lib/percpu_counter.c | 65 ++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 38 deletions(-)
include/linux/cpuhotplug.h +1 -1
@@ -90,7 +90,6 @@ enum cpuhp_state {CPUHP_FS_BUFF_DEAD ,CPUHP_PRINTK_DEAD ,CPUHP_MM_MEMCQ_DEAD , -CPUHP_PERCPU_CNT_DEAD ,CPUHP_RADIX_DEAD ,CPUHP_PAGE_ALLOC , CPUHP_NET_DEV_DEAD,
@@ -198,6 +197,7 @@ enum cpuhp_state {CPUHP_AP_HRTIMERS_DYING ,CPUHP_AP_X86_TBOOT_DYING ,CPUHP_AP_ARM_CACHE_B15_RAC_DYING , +CPUHP_AP_PERCPU_COUNTER_STARTING ,CPUHP_AP_ONLINE , CPUHP_TEARDOWN_CPU,
lib/percpu_counter.c +28 -37
135 lines not on this page · raw message
Signature · 1 line
2.39.1
Dennis Zhou
maintainerPATCH v2 2/3 cpu/hotplug: Remove export of cpu_active_mask and cpu_dying_mask
In reply to Dennis ZhouFrom: Thomas Gleixner <[email protected]>
No module users and no module should ever care.
--- kernel/cpu.c | 2 -- 1 file changed, 2 deletions(-)
kernel/cpu.c +0 -2
@@ -3126,10 +3126,8 @@ struct cpumask __cpu_present_mask __read_mostly;EXPORT_SYMBOL (__cpu_present_mask);struct cpumask __cpu_active_mask__read_mostly ; -EXPORT_SYMBOL (__cpu_active_mask);struct cpumask __cpu_dying_mask__read_mostly ; -EXPORT_SYMBOL (__cpu_dying_mask);atomic_t __num_online_cpus__read_mostly ;EXPORT_SYMBOL (__num_online_cpus);
Signature · 1 line
2.39.1
Dennis Zhou
maintainerPATCH v2 3/3 cpu/hotplug: Get rid of cpu_dying_mask
In reply to Dennis ZhouThe cpu_dying_mask is not only undocumented but also to some extent a misnomer. It's purpose is to capture the last direction of a cpu_up() or cpu_down() operation taking eventual rollback operations into account. The name and the lack of documentation lured already someone to use it in the wrong way.
The initial user is the scheduler code which needs to keep the decision correct whether to schedule tasks on a CPU, which is between the CPUHP_ONLINE and the CPUHP_ACTIVE state and has the balance_push() hook installed.
cpu_dying mask is not really useful for general consumption. The cpu_dying_mask bits are sticky even after cpu_up() or cpu_down() completes.
It might be argued, that the cpu_dying_mask bit could be cleared when cpu_down() completes, but that's not possible under all circumstances.
Especially not with partial hotplug operations. In that case the bit must be sticky in order to keep the initial user, i.e. the scheduler correct.
Replace the cpumask completely by:
- recording the direction internally in the CPU hotplug core state
- exposing that state via a documented function to the scheduler
After that cpu_dying_mask is not longer in use and removed before the next user trips over it.
--- include/linux/cpumask.h | 21 -------------------- kernel/cpu.c | 43 +++++++++++++++++++++++++++++++++++------ kernel/sched/core.c | 4 ++-- kernel/smpboot.h | 2 ++ 4 files changed, 41 insertions(+), 29 deletions(-)
include/linux/cpumask.h +0 -21
@@ -126,12 +126,10 @@ extern struct cpumask __cpu_possible_mask;extern struct cpumask __cpu_online_mask;extern struct cpumask __cpu_present_mask;extern struct cpumask __cpu_active_mask; -extern struct cpumask __cpu_dying_mask;#define cpu_possible_mask ((const struct cpumask *)&__cpu_possible_mask)#define cpu_online_mask ((const struct cpumask *)&__cpu_online_mask)#define cpu_present_mask ((const struct cpumask *)&__cpu_present_mask)#define cpu_active_mask ((const struct cpumask *)&__cpu_active_mask) -#define cpu_dying_mask ((const struct cpumask *)&__cpu_dying_mask)extern atomic_t __num_online_cpus;
@@ -1035,15 +1033,6 @@ set_cpu_active(unsigned int cpu, bool active)cpumask_clear_cpu (cpu, &__cpu_active_mask); } -static 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); -} -/** * to_cpumask - convert a NR_CPUS bitmap to a struct cpumask * * @bitmap: the bitmap
@@ -1119,11 +1108,6 @@ static inline bool cpu_active(unsigned int cpu) return cpumask_test_cpu(cpu, cpu_active_mask); } -static inline bool cpu_dying (unsigned int cpu) -{ -return cpumask_test_cpu (cpu, cpu_dying_mask); -} -#else #define num_online_cpus () 1U
@@ -1151,11 +1135,6 @@ static inline bool cpu_active(unsigned int cpu) return cpu == 0; } -static inline bool cpu_dying (unsigned int cpu) -{ -return false; -} -#endif /* NR_CPUS > 1 */ #define cpu_is_offline (cpu) unlikely(!cpu_online(cpu))
kernel/cpu.c +37 -6
79 lines not on this page · raw message
kernel/sched/core.c +2 -2
@@ -2468,7 +2468,7 @@ static inline bool is_cpu_allowed(struct task_struct *p, int cpu)return cpu_online (cpu);/* Regular kernel threads don't get to stay during offline. */ -if (cpu_dying (cpu)) +if (cpuhp_cpu_goes_down (cpu))return false;/* But are allowed during online. */
@@ -9434,7 +9434,7 @@ static void balance_push(struct rq *rq)* Only active while going offline and when invoked on the outgoing * CPU. */ -if (!cpu_dying (rq->cpu) || rq !=this_rq ()) +if (!cpuhp_cpu_goes_down (rq->cpu) || rq !=this_rq ())return ;/*
kernel/smpboot.h +2 -0
@@ -20,4 +20,6 @@ int smpboot_unpark_threads(unsigned int cpu);void __init cpuhp_threads_init (void ); +bool cpuhp_cpu_goes_down (unsigned int cpu); +#endif
Signature · 1 line
2.39.1