mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: John Ogness <john.ogness@linutronix.de>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH printk v2 10/18] printk: Provide helper for message prepending
Date: Thu, 20 Jun 2024 12:28:14 +0200	[thread overview]
Message-ID: <ZnQEPjcLnolJFi6n@pathway.suse.cz> (raw)
In-Reply-To: <20240603232453.33992-11-john.ogness@linutronix.de>

On Tue 2024-06-04 01:30:45, John Ogness wrote:
> In order to support prepending different texts to printk
> messages, split out the prepending code into a helper
> function.
> 
> Signed-off-by: John Ogness <john.ogness@linutronix.de>
> ---
>  kernel/printk/printk.c | 38 +++++++++++++++++++++++++++-----------
>  1 file changed, 27 insertions(+), 11 deletions(-)
> 
> diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
> index 4613f9145a8e..fcbf794a0aaf 100644
> --- a/kernel/printk/printk.c
> +++ b/kernel/printk/printk.c
> @@ -2806,30 +2806,25 @@ static void __console_unlock(void)
>  #ifdef CONFIG_PRINTK
>  
>  /*
> - * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". This
> - * is achieved by shifting the existing message over and inserting the dropped
> - * message.
> + * Prepend the message in @pmsg->pbufs->outbuf with the message in
> + * @pmsg->pbufs->scratchbuf. This is achieved by shifting the existing message
> + * over and inserting the scratchbuf message.
>   *
>   * @pmsg is the printk message to prepend.
>   *
> - * @dropped is the dropped count to report in the dropped message.
> + * @len is the length of the message in @pmsg->pbufs->scratchbuf.
>   *
>   * If the message text in @pmsg->pbufs->outbuf does not have enough space for
> - * the dropped message, the message text will be sufficiently truncated.
> + * the scratchbuf message, the message text will be sufficiently truncated.
>   *
>   * If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated.
>   */
> -void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
> +static void __console_prepend_scratch(struct printk_message *pmsg, size_t len)
>  {
>  	struct printk_buffers *pbufs = pmsg->pbufs;
> -	const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf);
>  	const size_t outbuf_sz = sizeof(pbufs->outbuf);
>  	char *scratchbuf = &pbufs->scratchbuf[0];
>  	char *outbuf = &pbufs->outbuf[0];
> -	size_t len;
> -
> -	len = scnprintf(scratchbuf, scratchbuf_sz,
> -		       "** %lu printk messages dropped **\n", dropped);
>  
>  	/*
>  	 * Make sure outbuf is sufficiently large before prepending.
> @@ -2851,6 +2846,27 @@ void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
>  	pmsg->outbuf_len += len;
>  }
>  
> +/*
> + * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message".
> + * @pmsg->outbuf_len is updated appropriately.
> + *
> + * @pmsg is the printk message to prepend.
> + *
> + * @dropped is the dropped count to report in the dropped message.
> + */
> +void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
> +{
> +	struct printk_buffers *pbufs = pmsg->pbufs;
> +	const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf);
> +	char *scratchbuf = &pbufs->scratchbuf[0];
> +	size_t len;
> +
> +	len = scnprintf(scratchbuf, scratchbuf_sz,
> +		       "** %lu printk messages dropped **\n", dropped);
> +
> +	__console_prepend_scratch(pmsg, len);
> +}

There is a lot of boilerplate code around the custom part. A better
solution might be to allow passing a printf format to the function
prepending the existing message.

I have tried how it would look like. Here is the patch. Note that it
is only compile-only tested:

From 853a3761f4e0f13970ffc71d9463f79eaa06fca1 Mon Sep 17 00:00:00 2001
From: John Ogness <john.ogness@linutronix.de>
Date: Tue, 4 Jun 2024 01:30:45 +0206
Subject: [PATCH] printk: Provide helper for message prepending

In order to support prepending different texts to printk
messages, split out the prepending code into a helper
function.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
 kernel/printk/printk.c | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 3e07e9884907..b0a6aab792fc 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2806,30 +2806,31 @@ static void __console_unlock(void)
 #ifdef CONFIG_PRINTK
 
 /*
- * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". This
- * is achieved by shifting the existing message over and inserting the dropped
- * message.
+ * Prepend the message in @pmsg->pbufs->outbuf. This is achieved by shifting
+ * the existing message over and inserting the scratchbuf message.
  *
- * @pmsg is the printk message to prepend.
+ * @pmsg is the original printk message.
+ * @fmt is the printf format of the message which will prepend the existing one.
  *
- * @dropped is the dropped count to report in the dropped message.
- *
- * If the message text in @pmsg->pbufs->outbuf does not have enough space for
- * the dropped message, the message text will be sufficiently truncated.
+ * If there is not enough space in @pmsg->pbufs->outbuf, the existing
+ * message text will be sufficiently truncated.
  *
  * If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated.
  */
-void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
+__printf(2, 3)
+static void console_prepend_message(struct printk_message *pmsg, const char *fmt, ...)
 {
 	struct printk_buffers *pbufs = pmsg->pbufs;
 	const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf);
 	const size_t outbuf_sz = sizeof(pbufs->outbuf);
 	char *scratchbuf = &pbufs->scratchbuf[0];
 	char *outbuf = &pbufs->outbuf[0];
+	va_list args;
 	size_t len;
 
-	len = scnprintf(scratchbuf, scratchbuf_sz,
-		       "** %lu printk messages dropped **\n", dropped);
+	va_start(args, fmt);
+	len = vscnprintf(scratchbuf, scratchbuf_sz, fmt, args);
+	va_end(args);
 
 	/*
 	 * Make sure outbuf is sufficiently large before prepending.
@@ -2851,6 +2852,19 @@ void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
 	pmsg->outbuf_len += len;
 }
 
+/*
+ * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message".
+ * @pmsg->outbuf_len is updated appropriately.
+ *
+ * @pmsg is the printk message to prepend.
+ *
+ * @dropped is the dropped count to report in the dropped message.
+ */
+void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
+{
+	console_prepend_message(pmsg, "** %lu printk messages dropped **\n", dropped);
+}
+
 /*
  * Read and format the specified record (or a later record if the specified
  * record is not available).
-- 
2.45.2


  reply	other threads:[~2024-06-20 10:28 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-03 23:24 [PATCH printk v2 00/18] add threaded printing + the rest John Ogness
2024-06-03 23:24 ` [PATCH printk v2 01/18] printk: Add function to replay kernel log on consoles John Ogness
2024-06-03 23:24 ` [PATCH printk v2 02/18] tty/sysrq: Replay kernel log messages on consoles via sysrq John Ogness
2024-06-03 23:24 ` [PATCH printk v2 03/18] printk: Rename console_replay_all() and update context John Ogness
2024-06-03 23:24 ` [PATCH printk v2 04/18] printk: nbcon: Introduce printing kthreads John Ogness
2024-06-07 13:17   ` Petr Mladek
2024-06-10 12:09     ` John Ogness
2024-06-11 14:51       ` Petr Mladek
2024-06-12  8:51         ` John Ogness
2024-06-12  9:24           ` Petr Mladek
2024-06-12 11:18             ` John Ogness
2024-06-12 11:33               ` Petr Mladek
2024-06-13 15:21           ` John Ogness
2024-06-14  7:40             ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 05/18] printk: Atomic print in printk context on shutdown John Ogness
2024-06-13 12:32   ` Petr Mladek
2024-06-13 12:44   ` atomic_flush vs boot consoles - was: " Petr Mladek
2024-06-13 12:52     ` [PATCH] printk: nbcon_atomic_flush_pending() is safe only when there is no boot console Petr Mladek
2024-06-13 15:10       ` Petr Mladek
2024-06-25 20:53       ` John Ogness
2024-06-28 15:51         ` how to flush consoles: was: " Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 06/18] printk: nbcon: Add context to console_is_usable() John Ogness
2024-06-13 13:22   ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 07/18] printk: nbcon: Add printer thread wakeups John Ogness
2024-06-13 15:08   ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 08/18] printk: nbcon: Stop threads on shutdown/reboot John Ogness
2024-06-17 15:21   ` Petr Mladek
2024-06-25 19:56     ` John Ogness
2024-06-26 12:14       ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 09/18] printk: nbcon: Start printing threads John Ogness
2024-06-18 15:34   ` Petr Mladek
2024-06-19 15:13     ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 10/18] printk: Provide helper for message prepending John Ogness
2024-06-20 10:28   ` Petr Mladek [this message]
2024-06-03 23:24 ` [PATCH printk v2 11/18] printk: nbcon: Show replay message on takeover John Ogness
2024-06-20 13:02   ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 12/18] printk: Add kthread for all legacy consoles John Ogness
2024-06-28 11:46   ` Petr Mladek
2024-06-28 12:22     ` John Ogness
2024-06-28 13:32       ` Petr Mladek
2024-06-28 14:11         ` John Ogness
2024-06-28 15:56           ` John Ogness
2024-07-01 15:33             ` Petr Mladek
2024-07-01 21:01               ` John Ogness
2024-07-02  9:11                 ` Petr Mladek
2024-07-01 14:50           ` Petr Mladek
2024-07-02  9:30             ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 13/18] proc: consoles: Add notation to c_start/c_stop John Ogness
2024-07-01 15:43   ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 14/18] proc: Add nbcon support for /proc/consoles John Ogness
2024-07-01 15:47   ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 15/18] tty: sysfs: Add nbcon support for 'active' John Ogness
2024-06-04 11:48   ` Greg Kroah-Hartman
2024-07-01 15:50   ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 16/18] printk: Provide threadprintk boot argument John Ogness
2024-07-02 12:12   ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 17/18] printk: Avoid false positive lockdep report for legacy printing John Ogness
2024-07-02 13:26   ` Petr Mladek
2024-06-03 23:24 ` [PATCH printk v2 18/18] printk: nbcon: Add function for printers to reacquire ownership John Ogness
2024-07-02 14:31   ` Petr Mladek
2024-06-04 13:31 ` [PATCH printk v2 00/18] add threaded printing + the rest Juri Lelli
2024-06-05  8:09   ` John Ogness
2024-06-05  9:32     ` Juri Lelli

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=ZnQEPjcLnolJFi6n@pathway.suse.cz \
    --to=pmladek@suse.com \
    --cc=john.ogness@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=senozhatsky@chromium.org \
    --cc=tglx@linutronix.de \
    /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

Powered by JetHome