[PATCH printk v1 02/18] printk: Add NMI check to down_trylock_console_sem()

From: John Ogness
Date: Thu Mar 02 2023 - 14:58:29 EST


The printk path is NMI safe because it only adds content to the
buffer and then triggers the delayed output via irq_work. If the
console is flushed or unblanked (on panic) from NMI then it can
deadlock in down_trylock_console_sem() because the semaphore is not
NMI safe.

Avoid try-locking the console from NMI and assume it failed.

Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
---
kernel/printk/printk.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 40c5f4170ac7..84af038292d9 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -318,6 +318,10 @@ static int __down_trylock_console_sem(unsigned long ip)
int lock_failed;
unsigned long flags;

+ /* Semaphores are not NMI-safe. */
+ if (in_nmi())
+ return 1;
+
/*
* Here and in __up_console_sem() we need to be in safe mode,
* because spindump/WARN/etc from under console ->lock will
--
2.30.2