Skip to contentSkip to conversation

printk: nbcon: Fix nbcon_cpu_emergency_flush() when preemptible

PATCH In review John Ogness 3 participants 5 messages Jun 5, 2024 Printk

lore
Share & tools
lore

John Ogness

author maintainer
#0 lore raw

PATCH 1/1 printk: nbcon: Fix nbcon_cpu_emergency_flush() when preemptible

nbcon_cpu_emergency_flush() can be called in a preemptible context. In that case the CPU is not in an emergency state. However, in order to see that the CPU is not in an emergency state (accessing the per-cpu variable), preemption must be disabled.

Disable preemption when checking the CPU state.

---
 kernel/printk/nbcon.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/printk/nbcon.c +12 -1
index 19f0db6945e4..39eb369880d9 100644
@@ -1443,8 +1443,19 @@ void nbcon_cpu_emergency_exit(void)
  */
 void nbcon_cpu_emergency_flush(void)
 {
+	bool is_emergency;
+
+	/*
+	 * If this context is not an emergency context, preemption might be
+	 * enabled. To be sure, disable preemption when checking if this is
+	 * an emergency context.
+	 */
+	preempt_disable();
+	is_emergency = (*nbcon_get_cpu_emergency_nesting() != 0);
+	preempt_enable();
+
 	/* The explicit flush is needed only in the emergency context. */
-	if (*(nbcon_get_cpu_emergency_nesting()) == 0)
+	if (!is_emergency)
 		return;
 
 	nbcon_atomic_flush_pending();
Signature · 1 line
2.30.2
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · John Ogness

nbcon_cpu_emergency_flush() can be called in a preemptible context. In that case the CPU is not in an emergency state. However, in order to see that the CPU is not in an emergency state (accessing the per-cpu variable), preemption must be disabled.

Quoted 6 lines

Disable preemption when checking the CPU state.

Great catch!

I am going to push it...

Best Regards,
Petr

maintainer
#2 lore raw
Quoted 14 lines · Petr Mladek
Quoted 6 lines · John Ogness

nbcon_cpu_emergency_flush() can be called in a preemptible context. In that case the CPU is not in an emergency state. However, in order to see that the CPU is not in an emergency state (accessing the per-cpu variable), preemption must be disabled.

Quoted 6 lines

Disable preemption when checking the CPU state.

Quoted 5 lines

Great catch!

I am going to push it...

Juri, should/could I add your Tested-by ?

Best Regards,
Petr

Tested-by
#3 lore raw
Quoted 15 lines · Petr Mladek
Quoted 14 lines · Petr Mladek
Quoted 12 lines · John Ogness

nbcon_cpu_emergency_flush() can be called in a preemptible context. In that case the CPU is not in an emergency state. However, in order to see that the CPU is not in an emergency state (accessing the per-cpu variable), preemption must be disabled.

Disable preemption when checking the CPU state.

Quoted 7 lines
Quoted 5 lines

Great catch!

I am going to push it...

Juri, should/could I add your Tested-by ?

A yes please if you like. Thanks!

You guys were so quick to act on it! :)

Best,
Juri

maintainer
#4 lore raw
In reply to John Ogness
Quoted 6 lines · John Ogness

nbcon_cpu_emergency_flush() can be called in a preemptible context. In that case the CPU is not in an emergency state. However, in order to see that the CPU is not in an emergency state (accessing the per-cpu variable), preemption must be disabled.

Quoted 6 lines

Disable preemption when checking the CPU state.

JFYI, the patch has been comitted into printk/linux.git, branch rework/write-atomic.

Best Regards,
Petr