summary refs log tree commit diff
path: root/kernel/printk/printk.c
diff options
context:
space:
mode:
authorStephen Brennan <stephen.s.brennan@oracle.com>2022-02-02 09:18:21 -0800
committerPetr Mladek <pmladek@suse.com>2022-02-14 13:39:20 +0100
commit8ebc476fd51e6c0fd3174ec1959a20ba99d4c5e5 (patch)
tree793d63aff1ee802d00cf2155182b379570f76c4e /kernel/printk/printk.c
parent13fb0f74d7029df3b8137f11ef955e578a4a4a60 (diff)
downloadlinux-8ebc476fd51e6c0fd3174ec1959a20ba99d4c5e5.tar.gz
printk: Drop console_sem during panic
If another CPU is in panic, we are about to be halted. Try to gracefully
abandon the console_sem, leaving it free for the panic CPU to grab.

Suggested-by: Petr Mladek <pmladek@suse.com>
Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20220202171821.179394-5-stephen.s.brennan@oracle.com
Diffstat (limited to 'kernel/printk/printk.c')
-rw-r--r--kernel/printk/printk.c25
1 files changed, 24 insertions, 1 deletions
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 2ec6b547cda6..6a51907a33b9 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2598,6 +2598,25 @@ static int have_callable_console(void)
 }
 
 /*
+ * Return true when this CPU should unlock console_sem without pushing all
+ * messages to the console. This reduces the chance that the console is
+ * locked when the panic CPU tries to use it.
+ */
+static bool abandon_console_lock_in_panic(void)
+{
+	if (!panic_in_progress())
+		return false;
+
+	/*
+	 * We can use raw_smp_processor_id() here because it is impossible for
+	 * the task to be migrated to the panic_cpu, or away from it. If
+	 * panic_cpu has already been set, and we're not currently executing on
+	 * that CPU, then we never will be.
+	 */
+	return atomic_read(&panic_cpu) != raw_smp_processor_id();
+}
+
+/*
  * Can we actually use the console at this time on this cpu?
  *
  * Console drivers may assume that per-cpu resources have been allocated. So
@@ -2745,6 +2764,10 @@ skip:
 		if (handover)
 			return;
 
+		/* Allow panic_cpu to take over the consoles safely */
+		if (abandon_console_lock_in_panic())
+			break;
+
 		if (do_cond_resched)
 			cond_resched();
 	}
@@ -2762,7 +2785,7 @@ skip:
 	 * flush, no worries.
 	 */
 	retry = prb_read_valid(prb, next_seq, NULL);
-	if (retry && console_trylock())
+	if (retry && !abandon_console_lock_in_panic() && console_trylock())
 		goto again;
 }
 EXPORT_SYMBOL(console_unlock);