[PATCH printk v3 5/7] console: Support unsafe flushing for panic/emergency restarts
From: John Ogness
Date: Wed Nov 18 2026 - 18:04:12 EST
Introduce a new nbcon-specific console field @allow_unsafe_policy
to store the unsafe flush policies for a console. The policies are
represented by bits, specified by enum nbcon_unsafe_flush_flags.
Define unsafe flush policies for:
panic+restart: This is for kexec and reboot from panic.
emergency+restart: This is for emergency_restart() when in
the emergency state.
Define a new flush type:
PR_FLUSH_RESTART: For flushing before kexec and reboot.
Implement the policy check within nbcon_allow_unsafe_takeover().
Since this adds support for unsafe flushing in non-panic situations,
remove the related WARN from nbcon_context_try_acquire_hostile().
Except for the removal of a WARN, no functional change.
Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
---
include/linux/console.h | 20 ++++++++++++++++++++
include/linux/printk.h | 3 +++
kernel/printk/nbcon.c | 11 +++++++----
3 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/include/linux/console.h b/include/linux/console.h
index 227a7cf296f60..1afeb90692395 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -194,6 +194,23 @@ enum cons_flags {
CON_NBCON_ATOMIC_UNSAFE = BIT(9),
};
+/**
+ * enum nbcon_unsafe_flush_flags - Unsafe atomic flush policy flags
+ * @NBCON_PANIC_RESTART_FLUSH: In panic, unsafe flush before kexec
+ * or reboot.
+ * @NBCON_EMERGENCY_RESTART_FLUSH: Unsafe flush before emergency restart.
+ *
+ * NBCON unsafe atomic flushing always occurs at the end of panic() before
+ * the kernel enters an infinite loop. Users may choose to perform unsafe
+ * flushing in additional scenarios where the current kernel is ending in
+ * order to increase the chances of seeing console messages, even if it is
+ * not safe to do so.
+ */
+enum nbcon_unsafe_flush_flags {
+ NBCON_PANIC_RESTART_FLUSH = BIT(0),
+ NBCON_EMERGENCY_RESTART_FLUSH = BIT(1),
+};
+
/**
* struct nbcon_state - console state for nbcon consoles
* @atom: Compound of the state fields for atomic operations
@@ -326,6 +343,7 @@ struct nbcon_write_context {
* @data: Driver private data
* @node: hlist node for the console list
*
+ * @allow_unsafe_policy: Unsafe atomic flush policy. See nbcon_unsafe_flush_flags
* @nbcon_state: State for nbcon consoles
* @nbcon_seq: Sequence number of the next record for nbcon to print
* @nbcon_device_ctxt: Context available for non-printing operations
@@ -461,6 +479,8 @@ struct console {
*/
void (*device_unlock)(struct console *con, unsigned long flags);
+ unsigned int allow_unsafe_policy;
+
atomic_t __private nbcon_state;
atomic_long_t __private nbcon_seq;
struct nbcon_context __private nbcon_device_ctxt;
diff --git a/include/linux/printk.h b/include/linux/printk.h
index 68cf51db5f0ea..267a0a6fd23cf 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -148,6 +148,8 @@ struct dev_printk_info;
/**
* enum pr_flush_reason - Reason for a flush
* @PR_FLUSH_RUNTIME: A flush while the kernel is running
+ * @PR_FLUSH_RESTART: The kernel is ending, but another instance should
+ * follow (reboot, kexec)
* @PR_FLUSH_HANG: The kernel is ending and will enter an infinite loop
*
* Flush reasons are used to determine if unsafe atomic flushing should be
@@ -155,6 +157,7 @@ struct dev_printk_info;
*/
enum pr_flush_reason {
PR_FLUSH_RUNTIME,
+ PR_FLUSH_RESTART,
PR_FLUSH_HANG,
};
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index c3307b5b2c922..daf1fe8522117 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.
@@ -1465,12 +1461,19 @@ enum nbcon_prio nbcon_get_default_prio(void)
static bool nbcon_allow_unsafe_takeover(struct console *con, enum pr_flush_reason reason)
{
enum nbcon_prio prio = nbcon_get_default_prio();
+ int unsafe_policy = con->allow_unsafe_policy;
bool allow = false;
switch (reason) {
case PR_FLUSH_RUNTIME:
/* Never unsafe flush during runtime. */
break;
+ case PR_FLUSH_RESTART:
+ if (prio == NBCON_PRIO_PANIC)
+ allow = (unsafe_policy & NBCON_PANIC_RESTART_FLUSH);
+ else if (prio == NBCON_PRIO_EMERGENCY)
+ allow = (unsafe_policy & NBCON_EMERGENCY_RESTART_FLUSH);
+ break;
case PR_FLUSH_HANG:
/* Always unsafe flush on panic hang. */
allow = (prio == NBCON_PRIO_PANIC);
--
2.47.3