[PATCH printk v3 1/7] printk: Introduce and specify flush reasons

From: John Ogness

Date: Wed Nov 18 2026 - 18:04:04 EST


Prepare for per-console unsafe atomic flushing policies by
introducing enum pr_flush_reason to classify various reasons
for performing a printk backlog flush. Specify the reason for
nbcon_atomic_flush_unsafe().

Propagate the reason down to __nbcon_atomic_flush_pending(),
where consoles are selected for atomic flushing.

Update the nbcon_atomic_flush_unsafe() and
__nbcon_atomic_flush_pending() call sites to specify the flush
reason.

Note that PR_FLUSH_RUNTIME is used for cases where the reason
for the flush is not clearly defined.

No functional change.

Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
---
include/linux/printk.h | 17 +++++++++++++++--
kernel/panic.c | 2 +-
kernel/printk/nbcon.c | 11 +++++++----
3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/include/linux/printk.h b/include/linux/printk.h
index f594c1266bfd4..68cf51db5f0ea 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -145,6 +145,19 @@ void early_printk(const char *s, ...) { }

struct dev_printk_info;

+/**
+ * enum pr_flush_reason - Reason for a flush
+ * @PR_FLUSH_RUNTIME: A flush while the kernel is running
+ * @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
+ * performed, based on individual console policies.
+ */
+enum pr_flush_reason {
+ PR_FLUSH_RUNTIME,
+ PR_FLUSH_HANG,
+};
+
#ifdef CONFIG_PRINTK
asmlinkage __printf(4, 0)
int vprintk_emit(int facility, int level,
@@ -207,7 +220,7 @@ void console_try_replay_all(void);
void printk_legacy_allow_panic_sync(void);
extern bool nbcon_device_try_acquire(struct console *con);
extern void nbcon_device_release(struct console *con);
-void nbcon_atomic_flush_unsafe(void);
+void nbcon_atomic_flush_unsafe(enum pr_flush_reason reason);
bool pr_flush(int timeout_ms, bool reset_on_progress);
#else
static inline __printf(1, 0)
@@ -318,7 +331,7 @@ static inline void nbcon_device_release(struct console *con)
{
}

-static inline void nbcon_atomic_flush_unsafe(void)
+static inline void nbcon_atomic_flush_unsafe(enum pr_flush_reason reason)
{
}

diff --git a/kernel/panic.c b/kernel/panic.c
index a4dbb6b8cd2bf..21d0ca892c64f 100644
--- a/kernel/panic.c
+++ b/kernel/panic.c
@@ -765,7 +765,7 @@ void vpanic(const char *fmt, va_list args)
* Explicitly flush the kernel log buffer one last time.
*/
console_flush_on_panic(CONSOLE_FLUSH_PENDING);
- nbcon_atomic_flush_unsafe();
+ nbcon_atomic_flush_unsafe(PR_FLUSH_HANG);

local_irq_enable();
for (i = 0; ; i += PANIC_TIMER_STEP) {
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index 5f8afaca88665..72fb49ade6fe5 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1652,9 +1652,10 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq)
/**
* __nbcon_atomic_flush_pending - Flush all nbcon consoles using their
* write_atomic() callback
+ * @reason: The reason for the flush
* @stop_seq: Flush up until this record
*/
-static void __nbcon_atomic_flush_pending(u64 stop_seq)
+static void __nbcon_atomic_flush_pending(enum pr_flush_reason reason, u64 stop_seq)
{
struct console *con;
int cookie;
@@ -1688,20 +1689,22 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq)
*/
void nbcon_atomic_flush_pending(void)
{
- __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb));
+ __nbcon_atomic_flush_pending(PR_FLUSH_RUNTIME, prb_next_reserve_seq(prb));
}

/**
* nbcon_atomic_flush_unsafe - Flush all nbcon consoles using their
* write_atomic() callback and allowing unsafe hostile takeovers
*
+ * @reason: The reason for the flush
+ *
* Flush the backlog up through the currently newest record. Unsafe hostile
* takeovers will be performed, if necessary.
*/
-void nbcon_atomic_flush_unsafe(void)
+void nbcon_atomic_flush_unsafe(enum pr_flush_reason reason)
{
panic_nbcon_allow_unsafe_takeover = true;
- __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb));
+ __nbcon_atomic_flush_pending(reason, prb_next_reserve_seq(prb));
panic_nbcon_allow_unsafe_takeover = false;
}

--
2.47.3