[PATCH printk v2] printk/nbcon: WARN on unsafe reentrance

From: John Ogness

Date: Fri Jul 31 2026 - 10:55:16 EST


Since the nbcon unsafe enter/exit functions are simply toggling a
state boolean, a buggy nbcon driver might enter an unsafe section
when the context is already in an unsafe section and it would go
unnoticed, even though doing so is a bug. Unsafe sections are not
reentrant!

Add a WARN_ON_ONCE() to nbcon_enter_unsafe() if the context is
already in an unsafe section.

Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
Link: https://lore.kernel.org/lkml/87o6fwms02.fsf@xxxxxxxxxxxxxxxxxxxxx
---
Changes since v1:

- Move the WARN to after the can_proceed() check. Otherwise
cur.unsafe could be referring to a different owning context.

kernel/printk/nbcon.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 4b03b019cd5ee..e88ddd742d5a0 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -848,6 +848,9 @@ static bool __nbcon_context_update_unsafe(struct nbcon_context *ctxt, bool unsaf
if (!nbcon_context_can_proceed(ctxt, &cur))
return false;

+ /* Unsafe sections are not reentrant. */
+ WARN_ON_ONCE(unsafe && cur.unsafe);
+
new.atom = cur.atom;
new.unsafe = unsafe;
} while (!nbcon_state_try_cmpxchg(con, &cur, &new));

base-commit: 703a9d76a45dd97b19b2432fc1ba4beb4d67a94f
--
2.47.3