mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sam Crossley <samuelcrossley@gmail.com>
To: Thomas Gleixner <tglx@kernel.org>,
	Breno Leitao <leitao@debian.org>,  Petr Mladek <pmladek@suse.com>,
	John Ogness <john.ogness@linutronix.de>,
	 Andrew Lunn <andrew+netdev@lunn.ch>,
	 "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	 Jakub Kicinski <kuba@kernel.org>,
	Jonathan Corbet <corbet@lwn.net>,
	 Paolo Abeni <pabeni@redhat.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	 Sergey Senozhatsky <senozhatsky@chromium.org>,
	 Shuah Khan <skhan@linuxfoundation.org>,
	Simon Horman <horms@kernel.org>,
	 Steven Rostedt <rostedt@goodmis.org>
Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	 linux-doc@vger.kernel.org,
	Sam Crossley <samuelcrossley@gmail.com>
Subject: [PATCH RFC 1/2] printk: add per-console replay helper
Date: Sat, 24 Oct 2026 16:06:03 -0700	[thread overview]
Message-ID: <20261024-netcons-replay-upstream-v1-1-9613f38496e5@gmail.com> (raw)
In-Reply-To: <20261024-netcons-replay-upstream-v1-0-9613f38496e5@gmail.com>

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@gmail.com>
---
 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


  reply	other threads:[~2026-10-24 23:06 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-24 23:06 [PATCH RFC 0/2] netconsole: allow replaying the printk buffer on demand Sam Crossley
2026-10-24 23:06 ` Sam Crossley [this message]
2026-10-24 23:06 ` [PATCH RFC 2/2] netconsole: add configfs trigger to replay printk buffer Sam Crossley
2026-10-26  9:34 ` [PATCH RFC 0/2] netconsole: allow replaying the printk buffer on demand John Ogness
2026-10-26 11:44   ` Breno Leitao
2026-10-26 13:09     ` John Ogness
2026-10-26 14:58       ` Petr Mladek
2026-10-26 16:33         ` John Ogness
2026-10-26 17:49           ` Breno Leitao
2026-10-27 13:47             ` Petr Mladek
2026-10-27 15:54               ` Breno Leitao
2026-10-27 17:45                 ` Petr Mladek
2026-10-27 12:58           ` Petr Mladek
2026-10-26 14:39     ` Chris Down
2026-10-26 15:32       ` Breno Leitao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261024-netcons-replay-upstream-v1-1-9613f38496e5@gmail.com \
    --to=samuelcrossley@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=corbet@lwn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=john.ogness@linutronix.de \
    --cc=kuba@kernel.org \
    --cc=leitao@debian.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pmladek@suse.com \
    --cc=rdunlap@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tglx@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®