[PATCH 44/47] hrtimer: Move CPUHP_AP_HRTIMERS_DYING post stop_machine()

From: Frederic Weisbecker

Date: Wed Nov 25 2026 - 17:26:32 EST


Between CPUHP_TEARDOWN_CPU and CPUHP_AP_OFFLINE, stop_machine executes
all the teardown callbacks while temporarily stopping/pausing all CPUs
in the system. This doesn't mix well with running RT or isolated tasks.

In order to transition smoothly from global stop_machine to finer
grained synchronization between teardown callbacks and the other CPUs,
a new stop machine post function has been introduced to be executed
after the main one with IRQs still disabled while other CPUs can exit
stop_machine().

The migration of timers can happen while other CPUs are running because
everything is serialized with the base locks. Also those locks only
nest on the outgoing CPU during hotplug so no locking inversion should
happen.

However deadlocks of the following kind can trigger between the base
locks and the IPI's csd, as reported by LKP:

foo() / CPU0 hrtimer_cpu_dying() / CPU1 hrtimer_interrupt() / CPU 2
---- ------------------- ------------------

raw_spin_lock_nested(&new_base(CPU2)->lock)
raw_spin_lock_irqsave(&cpu_base->lock, flags);
smp_call_function_single(CPU2, whatever, 0);
csd_lock(CPU2)
csd_data(CPU2) |= CSD_FLAG_LOCK
smp_call_function_single(CPU2, retrigger_next_event, 0);
csd_lock(CPU2)
csd_lock_wait(CPU2)
// DEADLOCK

While it can only happen while CSD debugging is on, reusing the base
csd makes more sense anyway as it is well synchronized against the base
lock and should avoid that kind of deadlock.

And then the teardown callback can be moved to post stop_machine()
safely.

Signed-off-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
Tested-by: Michael Kelley <mhklinux@xxxxxxxxxxx>
---
kernel/cpu.c | 4 ++--
kernel/time/hrtimer.c | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/kernel/cpu.c b/kernel/cpu.c
index 304a88fc7a62..4d34c4ed9836 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -1275,7 +1275,7 @@ void clear_tasks_mm_cpumask(int cpu)
static int take_cpu_down(void *_param)
{
struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
- enum cpuhp_state target = max((int)st->target, CPUHP_AP_SMPCFD_DYING);
+ enum cpuhp_state target = max((int)st->target, CPUHP_AP_HRTIMERS_DYING);
int err, cpu = smp_processor_id();

/* Ensure this CPU doesn't handle any more interrupts. */
@@ -1303,7 +1303,7 @@ static int take_cpu_down_post(void *_param)
enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
int cpu = smp_processor_id();

- WARN_ON(st->state != CPUHP_AP_SMPCFD_DYING);
+ WARN_ON(st->state != CPUHP_AP_HRTIMERS_DYING);
/*
* Invoke the former CPU_DYING callbacks. DYING must not fail!
*/
diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index a9dc2d53aa92..c638225379f4 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -2632,7 +2632,7 @@ int hrtimers_cpu_dying(unsigned int dying_cpu)
migrate_hrtimer_list(&old_base->clock_base[i], &new_base->clock_base[i]);

/* Tell the other CPU to retrigger the next event */
- smp_call_function_single(ncpu, retrigger_next_event, NULL, 0);
+ smp_call_function_single_async(ncpu, &new_base->csd);

raw_spin_unlock(&new_base->lock);
old_base->online = false;
--
2.56.1