lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <ZnQEPjcLnolJFi6n@pathway.suse.cz>
Date: Thu, 20 Jun 2024 12:28:14 +0200
From: Petr Mladek <pmladek@...e.com>
To: John Ogness <john.ogness@...utronix.de>
Cc: Sergey Senozhatsky <senozhatsky@...omium.org>,
	Steven Rostedt <rostedt@...dmis.org>,
	Thomas Gleixner <tglx@...utronix.de>, linux-kernel@...r.kernel.org
Subject: Re: [PATCH printk v2 10/18] printk: Provide helper for message
 prepending

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@...utronix.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@...utronix.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@...utronix.de>
Reviewed-by: Petr Mladek <pmladek@...e.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


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ