[PATCH printk v3 4/7] printk: Use @reason instead of global variable to check flush policy
From: John Ogness
Date: Wed Nov 18 2026 - 18:04:15 EST
Currently nbcon_allow_unsafe_takeover() is a hack, relying on the
temporary setting of a global variable @panic_nbcon_allow_unsafe_takeover
as a gate to determine if unsafe flushing is allowed. This hack works
because unsafe flushing is only performed at the end of panic().
However, this hack is _not_ sufficient if unsafe flushing is to be
allowed in other contexts.
Since at the end of panic() nbcon_atomic_flush_unsafe() is called with
the @reason PR_FLUSH_HANG, it is possible to identify when unsafe
atomic flushing is allowed using:
PR_FLUSH_HANG && panic_on_this_cpu()
rather than:
panic_nbcon_allow_unsafe_takeover && panic_on_this_cpu()
This allows removing the global variable hack and opens the door for
unsafe flushing to be allowed in other contexts.
Provide nbcon_allow_unsafe_takeover() the flush @reason to implement
the check.
No functional change.
Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
---
kernel/printk/nbcon.c | 35 ++++++++++++++++++++---------------
1 file changed, 20 insertions(+), 15 deletions(-)
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 9073d1f258a89..c3307b5b2c922 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1453,24 +1453,31 @@ enum nbcon_prio nbcon_get_default_prio(void)
return NBCON_PRIO_NORMAL;
}
-/*
- * Track if it is allowed to perform unsafe hostile takeovers of console
- * ownership. When true, console drivers might perform unsafe actions while
- * printing. It is externally available via nbcon_allow_unsafe_takeover().
- */
-static bool panic_nbcon_allow_unsafe_takeover;
-
/**
* nbcon_allow_unsafe_takeover - Check if unsafe console takeovers are allowed
+ * for a specified console and reason
*
- * Return: True, when it is permitted to perform unsafe console printing
+ * @con: The console to check
+ * @reason: The reason for the unsafe takeover
*
- * This is also used by console_is_usable() to determine if it is allowed to
- * call write_atomic() callbacks flagged as unsafe (CON_NBCON_ATOMIC_UNSAFE).
+ * Return: True, when @con is allowed to perform unsafe console printing
*/
-static bool nbcon_allow_unsafe_takeover(void)
+static bool nbcon_allow_unsafe_takeover(struct console *con, enum pr_flush_reason reason)
{
- return panic_on_this_cpu() && panic_nbcon_allow_unsafe_takeover;
+ enum nbcon_prio prio = nbcon_get_default_prio();
+ bool allow = false;
+
+ switch (reason) {
+ case PR_FLUSH_RUNTIME:
+ /* Never unsafe flush during runtime. */
+ break;
+ case PR_FLUSH_HANG:
+ /* Always unsafe flush on panic hang. */
+ allow = (prio == NBCON_PRIO_PANIC);
+ break;
+ }
+
+ return allow;
}
/**
@@ -1680,7 +1687,7 @@ static void __nbcon_atomic_flush_pending(enum pr_flush_reason reason, u64 stop_s
if (!nbcon_is_unsafe_usable(con, flags))
continue;
- allow_unsafe_takeover = nbcon_allow_unsafe_takeover();
+ allow_unsafe_takeover = nbcon_allow_unsafe_takeover(con, reason);
/*
* Since nbcon_is_unsafe_usable() was used, ensure @con does
@@ -1723,9 +1730,7 @@ void nbcon_atomic_flush_pending(void)
*/
void nbcon_atomic_flush_unsafe(enum pr_flush_reason reason)
{
- panic_nbcon_allow_unsafe_takeover = true;
__nbcon_atomic_flush_pending(reason, prb_next_reserve_seq(prb));
- panic_nbcon_allow_unsafe_takeover = false;
}
/**
--
2.47.3