[RFC PATCH 47/49] tick: Move CPUHP_AP_TICK_DYING post stop_machine()

From: Frederic Weisbecker

Date: Mon Oct 19 2026 - 09:19:41 EST


Between CPUHP_TEARDOWN_CPU and CPUHP_AP_OFFLINE, stop_machine executes
all the teardown callbacks while stopping all CPUs in the system. This
doesn't mix-up 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 teardown callback here can run concurrently with other CPUs:

* The cancelled local tick doesn't interact with other CPUs.
* The local clockevents are removed under the clockevents_lock
* The CPU is removed from broadcasting under the tick_broadcast_lock

The only needed change is to send an IPI to the new timekeeper because
outside stop machine, other CPUs may be in dynticks mode.

Then the teardown callback can be moved to post stop_machine()
accordingly.

Signed-off-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
---
kernel/cpu.c | 4 ++--
kernel/time/tick-common.c | 14 ++++++++------
2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/kernel/cpu.c b/kernel/cpu.c
index 1da41620a583..59ffcd4534c8 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_HRTIMERS_DYING);
+ enum cpuhp_state target = max((int)st->target, CPUHP_AP_TICK_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_HRTIMERS_DYING);
+ WARN_ON(st->state != CPUHP_AP_TICK_DYING);
/*
* Invoke the former CPU_DYING callbacks. DYING must not fail!
*/
diff --git a/kernel/time/tick-common.c b/kernel/time/tick-common.c
index 6a9198a4279b..39b8cb3e3484 100644
--- a/kernel/time/tick-common.c
+++ b/kernel/time/tick-common.c
@@ -16,6 +16,7 @@
#include <linux/percpu.h>
#include <linux/profile.h>
#include <linux/sched.h>
+#include <linux/sched/isolation.h>
#include <linux/module.h>
#include <trace/events/power.h>

@@ -394,13 +395,14 @@ void tick_assert_timekeeping_handover(void)
int tick_cpu_dying(unsigned int dying_cpu)
{
/*
- * If the current CPU is the timekeeper, it's the only one that can
- * safely hand over its duty. Also all online CPUs are in stop
- * machine, guaranteed not to be idle, therefore there is no
- * concurrency and it's safe to pick any online successor.
+ * If the current CPU is the timekeeper, hand over to any online
+ * CPU and kick it out of nohz if needed
*/
- if (tick_do_timer_cpu == dying_cpu)
- tick_do_timer_cpu = cpumask_first(cpu_online_mask);
+ if (tick_do_timer_cpu == dying_cpu) {
+ int timekeeper = cpumask_any_and(housekeeping_cpumask(HK_TYPE_KERNEL_NOISE), cpu_online_mask);
+ WRITE_ONCE(tick_do_timer_cpu, timekeeper);
+ irq_work_kick_cpu(timekeeper);
+ }

/* Make sure the CPU won't try to retake the timekeeping duty */
tick_sched_timer_dying(dying_cpu);
--
2.53.0