Skip to content

Commit 2f8dea1

Browse files
lkpdnKAGA-KOKO
authored andcommitted
hrtimers: Handle CPU state correctly on hotplug
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 <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/all/[email protected]
1 parent 922efd2 commit 2f8dea1

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
@@ -386,6 +386,7 @@ extern void __init hrtimers_init(void);
386386
extern void sysrq_timer_list_show(void);
387387

388388
int hrtimers_prepare_cpu(unsigned int cpu);
389+
int hrtimers_cpu_starting(unsigned int cpu);
389390
#ifdef CONFIG_HOTPLUG_CPU
390391
int hrtimers_cpu_dying(unsigned int cpu);
391392
#else

kernel/cpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,7 @@ static struct cpuhp_step cpuhp_hp_states[] = {
21792179
},
21802180
[CPUHP_AP_HRTIMERS_DYING] = {
21812181
.name = "hrtimers:dying",
2182-
.startup.single = NULL,
2182+
.startup.single = hrtimers_cpu_starting,
21832183
.teardown.single = hrtimers_cpu_dying,
21842184
},
21852185
[CPUHP_AP_TICK_DYING] = {

kernel/time/hrtimer.c

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

22042204
cpu_base->cpu = cpu;
2205+
hrtimer_cpu_base_init_expiry_lock(cpu_base);
2206+
return 0;
2207+
}
2208+
2209+
int hrtimers_cpu_starting(unsigned int cpu)
2210+
{
2211+
struct hrtimer_cpu_base *cpu_base = this_cpu_ptr(&hrtimer_bases);
2212+
2213+
/* Clear out any left over state from a CPU down operation */
22052214
cpu_base->active_bases = 0;
22062215
cpu_base->hres_active = 0;
22072216
cpu_base->hang_detected = 0;
@@ -2210,7 +2219,6 @@ int hrtimers_prepare_cpu(unsigned int cpu)
22102219
cpu_base->expires_next = KTIME_MAX;
22112220
cpu_base->softirq_expires_next = KTIME_MAX;
22122221
cpu_base->online = 1;
2213-
hrtimer_cpu_base_init_expiry_lock(cpu_base);
22142222
return 0;
22152223
}
22162224

@@ -2286,5 +2294,6 @@ int hrtimers_cpu_dying(unsigned int dying_cpu)
22862294
void __init hrtimers_init(void)
22872295
{
22882296
hrtimers_prepare_cpu(smp_processor_id());
2297+
hrtimers_cpu_starting(smp_processor_id());
22892298
open_softirq(HRTIMER_SOFTIRQ, hrtimer_run_softirq);
22902299
}

0 commit comments

Comments
 (0)