Skip to content

Commit a5cbbea

Browse files
lkpdngregkh
authored andcommitted
hrtimers: Handle CPU state correctly on hotplug
commit 2f8dea1 upstream. Consider a scenario where a CPU transitions from CPUHP_ONLINE to halfway through a CPU hotunplug down to CPUHP_HRTIMERS_PREPARE, and then back to CPUHP_ONLINE: Since hrtimers_prepare_cpu() does not run, cpu_base.hres_active remains set to 1 throughout. However, during a CPU unplug operation, the tick and the clockevents are shut down at CPUHP_AP_TICK_DYING. On return to the online state, for instance CFS incorrectly assumes that the hrtick is already active, and the chance of the clockevent device to transition to oneshot mode is also lost forever for the CPU, unless it goes back to a lower state than CPUHP_HRTIMERS_PREPARE once. This round-trip reveals another issue; cpu_base.online is not set to 1 after the transition, which appears as a WARN_ON_ONCE in enqueue_hrtimer(). Aside of that, the bulk of the per CPU state is not reset either, which means there are dangling pointers in the worst case. Address this by adding a corresponding startup() callback, which resets the stale per CPU state and sets the online flag. [ tglx: Make the new callback unconditionally available, remove the online modification in the prepare() callback and clear the remaining state in the starting callback instead of the prepare callback ] Fixes: 5c0930c ("hrtimers: Push pending hrtimers away from outgoing CPU earlier") Signed-off-by: Koichiro Den <koichiro.den@canonical.com> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: stable@vger.kernel.org Link: https://lore.kernel.org/all/20241220134421.3809834-1-koichiro.den@canonical.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 6c84ff2 commit a5cbbea

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

include/linux/hrtimer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ extern void __init hrtimers_init(void);
532532
extern void sysrq_timer_list_show(void);
533533

534534
int hrtimers_prepare_cpu(unsigned int cpu);
535+
int hrtimers_cpu_starting(unsigned int cpu);
535536
#ifdef CONFIG_HOTPLUG_CPU
536537
int hrtimers_cpu_dying(unsigned int cpu);
537538
#else

kernel/cpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ static struct cpuhp_step cpuhp_hp_states[] = {
22062206
},
22072207
[CPUHP_AP_HRTIMERS_DYING] = {
22082208
.name = "hrtimers:dying",
2209-
.startup.single = NULL,
2209+
.startup.single = hrtimers_cpu_starting,
22102210
.teardown.single = hrtimers_cpu_dying,
22112211
},
22122212

kernel/time/hrtimer.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,15 @@ int hrtimers_prepare_cpu(unsigned int cpu)
21802180
}
21812181

21822182
cpu_base->cpu = cpu;
2183+
hrtimer_cpu_base_init_expiry_lock(cpu_base);
2184+
return 0;
2185+
}
2186+
2187+
int hrtimers_cpu_starting(unsigned int cpu)
2188+
{
2189+
struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
2190+
2191+
/* Clear out any left over state from a CPU down operation */
21832192
cpu_base->active_bases = 0;
21842193
cpu_base->hres_active = 0;
21852194
cpu_base->hang_detected = 0;
@@ -2188,7 +2197,6 @@ int hrtimers_prepare_cpu(unsigned int cpu)
21882197
cpu_base->expires_next = KTIME_MAX;
21892198
cpu_base->softirq_expires_next = KTIME_MAX;
21902199
cpu_base->online = 1;
2191-
hrtimer_cpu_base_init_expiry_lock(cpu_base);
21922200
return 0;
21932201
}
21942202

@@ -2266,6 +2274,7 @@ int hrtimers_cpu_dying(unsigned int dying_cpu)
22662274
void __init hrtimers_init(void)
22672275
{
22682276
hrtimers_prepare_cpu(smp_processor_id());
2277+
hrtimers_cpu_starting(smp_processor_id());
22692278
open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
22702279
}
22712280

0 commit comments

Comments
 (0)