Skip to contentSkip to conversation

cpu: Make atomic callbacks run on UP with disabled interrupts

PATCH Superseded Sebastian Andrzej Siewior 2 participants 7 messages Nov 3, 2025 CPU hotplug

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cpu: Make atomic callbacks run on UP with disabled interrupts

On SMP callbacks in the "starting" range are invoked while the CPU is brought up and interrupts are still disabled. Callbacks which are added later ar invoked via the hotplug-thread on the target CPU and interrupts are explicitly disabled.
In the UP case callbacks which are added later are invoked "directly" without the thread. This is okay since there is just one CPU but with enabled interrupts debug code, such as smp_processor_id(), will issue warnings.

Disable interrupts before invoking the calback on UP if the state is atomic and interrupts are expected to be disabled.
The "save" part is required because this is also invoked early in the boot process while interrupts are disabled and must not be enabled.

Fixes 06ddd17521bf1 ("sched/smp: Always define is_percpu_thread() and scheduler_ipi()")
---
 kernel/cpu.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
kernel/cpu.c +17 -9
index db9f6c539b28c..a6902646b4933 100644
@@ -249,6 +249,14 @@ static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
 	return ret;
 }
 
+/*
+ * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
+ */
+static bool cpuhp_is_atomic_state(enum cpuhp_state state)
+{
+	return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
+}
+
 #ifdef CONFIG_SMP
 static bool cpuhp_is_ap_state(enum cpuhp_state state)
 {
@@ -271,14 +279,6 @@ static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
 	complete(done);
 }
 
-/*
- * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
- */
-static bool cpuhp_is_atomic_state(enum cpuhp_state state)
-{
-	return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
-}
-
 /* Synchronization state management */
 enum cpuhp_sync_state {
 	SYNC_STATE_DEAD,
@@ -2364,7 +2364,15 @@ static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
 	else
 		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
 #else
-	ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
+	if (1) {
+		unsigned long flags = 0;
+
+		if (cpuhp_is_atomic_state(state))
+			local_irq_save(flags);
+		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
+		if (cpuhp_is_atomic_state(state))
+			local_irq_restore(flags);
+	}
 #endif
 	BUG_ON(ret && !bringup);
 	return ret;
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 58 lines · Sebastian Andrzej Siewior

On SMP callbacks in the "starting" range are invoked while the CPU is brought up and interrupts are still disabled. Callbacks which are added later ar invoked via the hotplug-thread on the target CPU and interrupts are explicitly disabled.
In the UP case callbacks which are added later are invoked "directly" without the thread. This is okay since there is just one CPU but with enabled interrupts debug code, such as smp_processor_id(), will issue warnings.

Disable interrupts before invoking the calback on UP if the state is atomic and interrupts are expected to be disabled.
The "save" part is required because this is also invoked early in the boot process while interrupts are disabled and must not be enabled.

Fixes 06ddd17521bf1 ("sched/smp: Always define is_percpu_thread() and scheduler_ipi()")
---
 kernel/cpu.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
kernel/cpu.c +8 -8
index db9f6c539b28c..a6902646b4933 100644
@@ -249,6 +249,14 @@ static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
 	return ret;
 }
 
+/*
+ * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
+ */
+static bool cpuhp_is_atomic_state(enum cpuhp_state state)
+{
+	return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
+}
+
 #ifdef CONFIG_SMP
 static bool cpuhp_is_ap_state(enum cpuhp_state state)
 {
@@ -271,14 +279,6 @@ static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
 	complete(done);
 }
 
-/*
- * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
- */
-static bool cpuhp_is_atomic_state(enum cpuhp_state state)
-{
-	return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
-}
-
 /* Synchronization state management */
 enum cpuhp_sync_state {
 	SYNC_STATE_DEAD,
@@ -2364,7 +2364,15 @@ static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
 	else
 		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
 #else

kernel/cpu.c static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup, · line 2375

Quoted 10 lines
kernel/cpu.c +9 -1
-	ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
+	if (1) {
+		unsigned long flags = 0;
+
+		if (cpuhp_is_atomic_state(state))
+			local_irq_save(flags);
+		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
+		if (cpuhp_is_atomic_state(state))
+			local_irq_restore(flags);
+	}

How about:

	if (cpuhp_is_atomic_state(state)) {
		guard(irqsave)();
		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
		/*
		 * STARTING/DYING must not fail!
		 */
		WARN_ON_ONCE(ret);
	} else {
		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
	}

which is a little more like cpuhp_thread_fun()

Quoted 6 lines
kernel/cpu.c +0 -0
 #endif
 	BUG_ON(ret && !bringup);
 	return ret;
Signature · 1 line
2.51.0
#2 lore raw
Quoted 13 lines · Peter Zijlstra

How about:

	if (cpuhp_is_atomic_state(state)) {
		guard(irqsave)();
		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
		/*
		 * STARTING/DYING must not fail!
		 */
		WARN_ON_ONCE(ret);
	} else {
		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
	}
Quoted 1 line

which is a little more like cpuhp_thread_fun()

very nice indeed. What about WARN_ON_ONCE(ret && bringup) given the BUG_ON(ret && !bringup) below?

Quoted 1 line

Sebastian

maintainer
#3 lore raw
Quoted 14 lines · Sebastian Andrzej Siewior
Quoted 13 lines · Peter Zijlstra

How about:

	if (cpuhp_is_atomic_state(state)) {
		guard(irqsave)();
		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
		/*
		 * STARTING/DYING must not fail!
		 */
		WARN_ON_ONCE(ret);
	} else {
		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
	}
Quoted 4 lines
Quoted 1 line

which is a little more like cpuhp_thread_fun()

very nice indeed. What about WARN_ON_ONCE(ret && bringup) given the BUG_ON(ret && !bringup) below?

That would be confusing to read, the condition here is any failure. And yes, this way you'll get a WARN and then a BUG, but meh :-)

#4 lore raw

PATCH v2 1/1 cpu: Make atomic callbacks run on UP with disabled interrupts

In reply to Peter Zijlstra

On SMP callbacks in the "starting" range are invoked while the CPU is brought up and interrupts are still disabled. Callbacks which are added later ar invoked via the hotplug-thread on the target CPU and interrupts are explicitly disabled.
In the UP case callbacks which are added later are invoked "directly" without the thread. This is okay since there is just one CPU but with enabled interrupts debug code, such as smp_processor_id(), will issue warnings.

Disable interrupts before invoking the calback on UP if the state is atomic and interrupts are expected to be disabled.
The "save" part is required because this is also invoked early in the boot process while interrupts are disabled and must not be enabled. The warnings aligns the function with cpuhp_thread_fun().

Fixes 06ddd17521bf1 ("sched/smp: Always define is_percpu_thread() and scheduler_ipi()")

---
v1…v2:
- guard() syntax as suggested by PeterZ plus the warning from cpuhp_thread_fun(). This will lead to a warning + bug in the bringup case which also happens with the thread on SMP.

 kernel/cpu.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)
kernel/cpu.c +18 -9
index db9f6c539b28c..8ab69a891d7ae 100644
@@ -249,6 +249,14 @@ static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
 	return ret;
 }
 
+/*
+ * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
+ */
+static bool cpuhp_is_atomic_state(enum cpuhp_state state)
+{
+	return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
+}
+
 #ifdef CONFIG_SMP
 static bool cpuhp_is_ap_state(enum cpuhp_state state)
 {
@@ -271,14 +279,6 @@ static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
 	complete(done);
 }
 
-/*
- * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
- */
-static bool cpuhp_is_atomic_state(enum cpuhp_state state)
-{
-	return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
-}
-
 /* Synchronization state management */
 enum cpuhp_sync_state {
 	SYNC_STATE_DEAD,
@@ -2364,7 +2364,16 @@ static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
 	else
 		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
 #else
-	ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
+	if (cpuhp_is_atomic_state(state)) {
+		guard(irqsave)();
+		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
+		/*
+		 * STARTING/DYING must not fail!
+		 */
+		WARN_ON_ONCE(ret);
+	} else {
+		ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
+	}
 #endif
 	BUG_ON(ret && !bringup);
 	return ret;
Signature · 1 line
2.51.0
maintainer
#5 lore raw
Quoted 9 lines · Sebastian Andrzej Siewior

On SMP callbacks in the "starting" range are invoked while the CPU is brought up and interrupts are still disabled. Callbacks which are added later ar invoked via the hotplug-thread on the target CPU and interrupts are explicitly disabled.
In the UP case callbacks which are added later are invoked "directly" without the thread. This is okay since there is just one CPU but with enabled interrupts debug code, such as smp_processor_id(), will issue warnings.

Quoted 8 lines

Disable interrupts before invoking the calback on UP if the state is atomic and interrupts are expected to be disabled.
The "save" part is required because this is also invoked early in the boot process while interrupts are disabled and must not be enabled. The warnings aligns the function with cpuhp_thread_fun().

Fixes 06ddd17521bf1 ("sched/smp: Always define is_percpu_thread() and scheduler_ipi()")

Did we want me to merge this into sched/urgent or what was the hope/intention for this patch.

#6 lore raw
Quoted 16 lines · Peter Zijlstra
Quoted 15 lines · Sebastian Andrzej Siewior

On SMP callbacks in the "starting" range are invoked while the CPU is brought up and interrupts are still disabled. Callbacks which are added later ar invoked via the hotplug-thread on the target CPU and interrupts are explicitly disabled.
In the UP case callbacks which are added later are invoked "directly" without the thread. This is okay since there is just one CPU but with enabled interrupts debug code, such as smp_processor_id(), will issue warnings.

Disable interrupts before invoking the calback on UP if the state is atomic and interrupts are expected to be disabled.
The "save" part is required because this is also invoked early in the boot process while interrupts are disabled and must not be enabled. The warnings aligns the function with cpuhp_thread_fun().

Quoted 5 lines
Quoted 2 lines
Fixes 06ddd17521bf1 ("sched/smp: Always define is_percpu_thread() and scheduler_ipi()")

Did we want me to merge this into sched/urgent or what was the hope/intention for this patch.

I did testing on ARM/UP and run into this. Therefore I would suggest sched/urgent or the matching hotplug/urgent should one exist.

But since it is just a smp_processor_id() debug warning on UP and nothing serious I don't mind to delay it until the next merge window.

Sebastian