[PATCH RFC 1/2] printk: add per-console replay helper

From: Sam Crossley

Date: Sat Oct 24 2026 - 19:07:32 EST


console_try_replay_all() rewinds every registered console, which is too
broad for a caller that needs to resend records through one console.

Add console_try_replay() for that, sharing the locking and flush logic so
existing callers are unaffected. A NULL console is rejected, since the
shared helper reads it as "every console".

Like the existing all-console operation this is best effort: it does
nothing if the console lock cannot be acquired immediately. That lock is
global, so unrelated console activity can cause a replay to be skipped.
Records have not necessarily been printed by the time it returns either --
consoles served by a printer thread are only woken here.

Signed-off-by: Sam Crossley <samuelcrossley@xxxxxxxxx>
---
include/linux/printk.h | 6 +++++
kernel/printk/printk.c | 67 +++++++++++++++++++++++++++++++++++++-------------
2 files changed, 56 insertions(+), 17 deletions(-)

diff --git a/include/linux/printk.h b/include/linux/printk.h
index f594c1266bfd4..9cd06a67e7c83 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -203,6 +203,7 @@ void show_regs_print_info(const char *log_lvl);
extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
extern asmlinkage void dump_stack(void) __cold;
void printk_trigger_flush(void);
+void console_try_replay(struct console *con);
void console_try_replay_all(void);
void printk_legacy_allow_panic_sync(void);
extern bool nbcon_device_try_acquire(struct console *con);
@@ -301,6 +302,11 @@ static inline void dump_stack(void)
static inline void printk_trigger_flush(void)
{
}
+
+static inline void console_try_replay(struct console *con)
+{
+}
+
static inline void console_try_replay_all(void)
{
}
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 9a205f94bfac7..59e6df8a57de2 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3494,13 +3494,14 @@ void console_unblank(void)
}

/*
- * Rewind all consoles to the oldest available record.
+ * Rewind a console, or all consoles if @target is NULL, to the oldest
+ * available record.
*
* IMPORTANT: The function is safe only when called under
* console_lock(). It is not enforced because
* it is used as a best effort in panic().
*/
-static void __console_rewind_all(void)
+static void __console_rewind(struct console *target)
{
struct console *c;
short flags;
@@ -3511,6 +3512,9 @@ static void __console_rewind_all(void)

cookie = console_srcu_read_lock();
for_each_console_srcu(c) {
+ if (target && c != target)
+ continue;
+
flags = console_srcu_read_flags(c);

if (flags & CON_NBCON) {
@@ -3557,7 +3561,7 @@ void console_flush_on_panic(enum con_flush_mode mode)
console_may_schedule = 0;

if (mode == CONSOLE_REPLAY_ALL)
- __console_rewind_all();
+ __console_rewind(NULL);

printk_get_console_flush_type(&ft);
if (ft.nbcon_atomic)
@@ -5047,6 +5051,48 @@ void kmsg_dump_rewind(struct kmsg_dump_iter *iter)
}
EXPORT_SYMBOL_GPL(kmsg_dump_rewind);

+static void __console_try_replay(struct console *con)
+{
+ struct console_flush_type ft;
+
+ printk_get_console_flush_type(&ft);
+ /*
+ * The console lock is global, so the replay is silently skipped when
+ * an unrelated console holds it. A trylock is what keeps this
+ * callable from any context.
+ */
+ if (!console_trylock())
+ return;
+
+ __console_rewind(con);
+ if (ft.nbcon_atomic)
+ nbcon_atomic_flush_pending();
+ if (ft.nbcon_offload)
+ nbcon_kthreads_wake();
+ if (ft.legacy_offload)
+ defer_console_output();
+ /* Consoles are flushed as part of console_unlock(). */
+ console_unlock();
+}
+
+/**
+ * console_try_replay - try to replay kernel log on a console
+ * @con: The console to replay the available records on
+ *
+ * Try to obtain the console lock and replay all available records in the
+ * printk buffer on @con. Does nothing if @con is not registered.
+ *
+ * Context: Any, except for NMI.
+ */
+void console_try_replay(struct console *con)
+{
+ if (WARN_ON_ONCE(!con))
+ return;
+
+ __console_try_replay(con);
+}
+EXPORT_SYMBOL_GPL(console_try_replay);
+
/**
* console_try_replay_all - try to replay kernel log on consoles
*
@@ -5058,20 +5104,7 @@ EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
*/
void console_try_replay_all(void)
{
- struct console_flush_type ft;
-
- printk_get_console_flush_type(&ft);
- if (console_trylock()) {
- __console_rewind_all();
- if (ft.nbcon_atomic)
- nbcon_atomic_flush_pending();
- if (ft.nbcon_offload)
- nbcon_kthreads_wake();
- if (ft.legacy_offload)
- defer_console_output();
- /* Consoles are flushed as part of console_unlock(). */
- console_unlock();
- }
+ __console_try_replay(NULL);
}
#endif


--
2.53.0-Meta