[PATCH printk v2 3/6] printk: Evaluate flush policy in nbcon_allow_unsafe_takeover()
From: John Ogness
Date: Wed Nov 11 2026 - 13:32:53 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.
Allow unsafe flushing in other contexts by modifying
nbcon_allow_unsafe_takeover() to take a console and flush reason as
input and, based on the unsafe flush policies of that console, determine
if unsafe flushing is allowed. This also allows the global variable
@panic_nbcon_allow_unsafe_takeover to be removed.
Remove the hard-coded policy for CON_NBCON_ATOMIC_UNSAFE from
console_is_usable() and implement that policy logic within
nbcon_allow_unsafe_takeover().
Note that this changes the flushing flow for CON_NBCON_ATOMIC_UNSAFE
consoles. Rather than console_is_usable() reporting that the console
is not usable, __nbcon_atomic_flush_pending_con() will report that it
is unable to acquire ownership.
Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
---
include/linux/console.h | 11 +----
kernel/printk/nbcon.c | 91 +++++++++++++++++++++++++++++++----------
2 files changed, 72 insertions(+), 30 deletions(-)
diff --git a/include/linux/console.h b/include/linux/console.h
index beb1ee04535b7..b5024a879b96a 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -179,7 +179,8 @@ static inline void con_debug_leave(void) { }
* @CON_NBCON: Console can operate outside of the legacy style console_lock
* constraints.
* @CON_NBCON_ATOMIC_UNSAFE: The write_atomic() callback is not safe and is
- * therefore only used by nbcon_atomic_flush_unsafe().
+ * therefore only called when allowed by
+ * nbcon_allow_unsafe_takeover().
*/
enum cons_flags {
CON_PRINTBUFFER = BIT(0),
@@ -637,7 +638,6 @@ extern void nbcon_write_context_set_buf(struct nbcon_write_context *wctxt,
extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt);
extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt);
extern void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt);
-extern bool nbcon_allow_unsafe_takeover(void);
extern bool nbcon_kdb_try_acquire(struct console *con,
struct nbcon_write_context *wctxt);
extern void nbcon_kdb_release(struct nbcon_write_context *wctxt);
@@ -670,13 +670,6 @@ static inline bool console_is_usable(struct console *con, short flags, bool use_
/* The write_atomic() callback is optional. */
if (!con->write_atomic)
return false;
-
- /*
- * An unsafe write_atomic() callback is only usable
- * when unsafe takeovers are allowed.
- */
- if ((flags & CON_NBCON_ATOMIC_UNSAFE) && !nbcon_allow_unsafe_takeover())
- return false;
}
/*
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 935fd6b86bac7..d4d2fc51f2aa7 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -557,10 +557,6 @@ static int nbcon_context_try_acquire_hostile(struct nbcon_context *ctxt,
if (!ctxt->allow_unsafe_takeover)
return -EPERM;
- /* Ensure caller is allowed to perform unsafe hostile takeovers. */
- if (WARN_ON_ONCE(ctxt->prio != NBCON_PRIO_PANIC))
- return -EPERM;
-
/*
* Check that try_acquire_direct() and try_acquire_handover() returned
* -EBUSY in the right situation.
@@ -1453,24 +1449,60 @@ 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
*/
-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;
+ int unsafe_flags = con->unsafe_flush_flags;
+ bool in_panic = panic_on_this_cpu();
+ bool allow = false;
+
+ switch (reason) {
+ case PR_FLUSH_RUNTIME:
+ if (in_panic) {
+ if (unsafe_flags & NBCON_PANIC_PRINTK)
+ allow = true;
+
+ } else {
+ /* Only when the kthreads have been shutdown. */
+ if ((unsafe_flags & NBCON_NORMAL_NOTHREAD) &&
+ !con->kthread &&
+ system_state > SYSTEM_RUNNING) {
+ allow = true;
+ }
+ }
+ break;
+ case PR_FLUSH_RESTART:
+ /* kexec, reboot */
+
+ if (in_panic) {
+ if (unsafe_flags & NBCON_PANIC_FLUSH)
+ allow = true;
+
+ } else {
+ /* Printing kthreads not reliable. */
+ if (unsafe_flags & NBCON_NORMAL_NOTHREAD)
+ allow = true;
+ }
+ break;
+ case PR_FLUSH_HANG:
+ /* Always unsafe flush before hang. */
+ allow = true;
+ break;
+ case PR_FLUSH_KDB:
+ if (unsafe_flags & NBCON_KDB)
+ allow = true;
+ break;
+ }
+
+ return allow;
}
/**
@@ -1515,6 +1547,14 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
ctxt->prio = nbcon_get_default_prio();
if (use_atomic) {
+ ctxt->allow_unsafe_takeover = nbcon_allow_unsafe_takeover(con,
+ legacy_flush_reason);
+
+ if ((console_srcu_read_flags(con) & CON_NBCON_ATOMIC_UNSAFE) &&
+ !ctxt->allow_unsafe_takeover) {
+ return false;
+ }
+
/*
* In an atomic or unknown context, use the same procedure as
* in console_emit_next_record(). It allows to handover.
@@ -1572,7 +1612,12 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, enum pr_flush_r
ctxt->console = con;
ctxt->spinwait_max_us = 2000;
ctxt->prio = nbcon_get_default_prio();
- ctxt->allow_unsafe_takeover = nbcon_allow_unsafe_takeover();
+ ctxt->allow_unsafe_takeover = nbcon_allow_unsafe_takeover(con, reason);
+
+ if ((console_srcu_read_flags(con) & CON_NBCON_ATOMIC_UNSAFE) &&
+ !ctxt->allow_unsafe_takeover) {
+ return -EPERM;
+ }
while (nbcon_seq_read(con) < stop_seq) {
/*
@@ -1707,9 +1752,7 @@ void nbcon_atomic_flush_pending(enum pr_flush_reason reason)
*/
void nbcon_atomic_flush_unsafe(void)
{
- panic_nbcon_allow_unsafe_takeover = true;
__nbcon_atomic_flush_pending(PR_FLUSH_HANG, prb_next_reserve_seq(prb));
- panic_nbcon_allow_unsafe_takeover = false;
}
/**
@@ -1976,8 +2019,14 @@ bool nbcon_kdb_try_acquire(struct console *con,
struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
memset(ctxt, 0, sizeof(*ctxt));
- ctxt->console = con;
- ctxt->prio = NBCON_PRIO_EMERGENCY;
+ ctxt->console = con;
+ ctxt->prio = NBCON_PRIO_EMERGENCY;
+ ctxt->allow_unsafe_takeover = nbcon_allow_unsafe_takeover(con, PR_FLUSH_KDB);
+
+ if ((console_srcu_read_flags(con) & CON_NBCON_ATOMIC_UNSAFE) &&
+ !ctxt->allow_unsafe_takeover) {
+ return false;
+ }
if (!nbcon_context_try_acquire(ctxt, false))
return false;
--
2.47.3