[PATCHv3] seq_buf: add seq_buf_do_printk() helper

Sergey Senozhatsky posted 1 patch 2 years, 8 months ago
include/linux/seq_buf.h |  2 ++
lib/seq_buf.c           | 32 ++++++++++++++++++++++++++++++++
2 files changed, 34 insertions(+)
[PATCHv3] seq_buf: add seq_buf_do_printk() helper
Posted by Sergey Senozhatsky 2 years, 8 months ago
Sometimes we use seq_buf to format a string buffer, which
we then pass to printk(). However, in certain situations
the seq_buf string buffer can get too big, exceeding the
PRINTKRB_RECORD_MAX bytes limit, and causing printk() to
truncate the string.

Add a new seq_buf helper. This helper prints the seq_buf
string buffer line by line, using \n as a delimiter,
rather than passing the whole string buffer to printk()
at once.

Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
 include/linux/seq_buf.h |  2 ++
 lib/seq_buf.c           | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h
index 5b31c5147969..515d7fcb9634 100644
--- a/include/linux/seq_buf.h
+++ b/include/linux/seq_buf.h
@@ -159,4 +159,6 @@ extern int
 seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary);
 #endif
 
+void seq_buf_do_printk(struct seq_buf *s, const char *lvl);
+
 #endif /* _LINUX_SEQ_BUF_H */
diff --git a/lib/seq_buf.c b/lib/seq_buf.c
index 0a68f7aa85d6..45c450f423fa 100644
--- a/lib/seq_buf.c
+++ b/lib/seq_buf.c
@@ -93,6 +93,38 @@ int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
 }
 EXPORT_SYMBOL_GPL(seq_buf_printf);
 
+/**
+ * seq_buf_do_printk - printk seq_buf line by line
+ * @s: seq_buf descriptor
+ * @lvl: printk level
+ *
+ * printk()-s a multi-line sequential buffer line by line. The function
+ * makes sure that the buffer in @s is nul terminated and safe to read
+ * as a string.
+ */
+void seq_buf_do_printk(struct seq_buf *s, const char *lvl)
+{
+	const char *start, *lf;
+
+	if (s->size == 0 || s->len == 0)
+		return;
+
+	seq_buf_terminate(s);
+
+	start = s->buffer;
+	while ((lf = strchr(start, '\n'))) {
+		int len = lf - start + 1;
+
+		printk("%s%.*s", lvl, len, start);
+		start = ++lf;
+	}
+
+	/* No trailing LF */
+	if (start < s->buffer + s->len)
+		printk("%s%s\n", lvl, start);
+}
+EXPORT_SYMBOL_GPL(seq_buf_do_printk);
+
 #ifdef CONFIG_BINARY_PRINTF
 /**
  * seq_buf_bprintf - Write the printf string from binary arguments
-- 
2.40.0.634.g4ca3ef3211-goog
Re: [PATCHv3] seq_buf: add seq_buf_do_printk() helper
Posted by Yosry Ahmed 2 years, 7 months ago
On Sat, Apr 15, 2023 at 3:01 AM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> Sometimes we use seq_buf to format a string buffer, which
> we then pass to printk(). However, in certain situations
> the seq_buf string buffer can get too big, exceeding the
> PRINTKRB_RECORD_MAX bytes limit, and causing printk() to
> truncate the string.
>
> Add a new seq_buf helper. This helper prints the seq_buf
> string buffer line by line, using \n as a delimiter,
> rather than passing the whole string buffer to printk()
> at once.
>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
> Reviewed-by: Petr Mladek <pmladek@suse.com>
> ---
>  include/linux/seq_buf.h |  2 ++
>  lib/seq_buf.c           | 32 ++++++++++++++++++++++++++++++++
>  2 files changed, 34 insertions(+)
>
> diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h
> index 5b31c5147969..515d7fcb9634 100644
> --- a/include/linux/seq_buf.h
> +++ b/include/linux/seq_buf.h
> @@ -159,4 +159,6 @@ extern int
>  seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary);
>  #endif
>
> +void seq_buf_do_printk(struct seq_buf *s, const char *lvl);
> +
>  #endif /* _LINUX_SEQ_BUF_H */
> diff --git a/lib/seq_buf.c b/lib/seq_buf.c
> index 0a68f7aa85d6..45c450f423fa 100644
> --- a/lib/seq_buf.c
> +++ b/lib/seq_buf.c
> @@ -93,6 +93,38 @@ int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
>  }
>  EXPORT_SYMBOL_GPL(seq_buf_printf);
>
> +/**
> + * seq_buf_do_printk - printk seq_buf line by line
> + * @s: seq_buf descriptor
> + * @lvl: printk level
> + *
> + * printk()-s a multi-line sequential buffer line by line. The function
> + * makes sure that the buffer in @s is nul terminated and safe to read
> + * as a string.
> + */
> +void seq_buf_do_printk(struct seq_buf *s, const char *lvl)
> +{
> +       const char *start, *lf;
> +
> +       if (s->size == 0 || s->len == 0)
> +               return;
> +
> +       seq_buf_terminate(s);
> +
> +       start = s->buffer;
> +       while ((lf = strchr(start, '\n'))) {
> +               int len = lf - start + 1;
> +
> +               printk("%s%.*s", lvl, len, start);
> +               start = ++lf;
> +       }
> +
> +       /* No trailing LF */
> +       if (start < s->buffer + s->len)
> +               printk("%s%s\n", lvl, start);
> +}
> +EXPORT_SYMBOL_GPL(seq_buf_do_printk);
> +
>  #ifdef CONFIG_BINARY_PRINTF
>  /**
>   * seq_buf_bprintf - Write the printf string from binary arguments
> --
> 2.40.0.634.g4ca3ef3211-goog
>

Hey Steven,

Is there a chance this patch makes it in the next merge window? It
would be convenient to have it in Linus's tree so that we can send
patches using the new helper. I am assuming there is no risk involved
in merging an unused helper (but I can easily be wrong :)).

Thanks!
Re: [PATCHv3] seq_buf: add seq_buf_do_printk() helper
Posted by Steven Rostedt 2 years, 7 months ago
On Tue, 25 Apr 2023 06:14:33 -0700
Yosry Ahmed <yosryahmed@google.com> wrote:


> Hey Steven,
> 
> Is there a chance this patch makes it in the next merge window? It
> would be convenient to have it in Linus's tree so that we can send
> patches using the new helper. I am assuming there is no risk involved
> in merging an unused helper (but I can easily be wrong :)).

Yeah, the merge window opened quicker than I expected. I don't think this
would be a problem to pull in. It's not high risk.

-- Steve
Re: [PATCHv3] seq_buf: add seq_buf_do_printk() helper
Posted by Yosry Ahmed 2 years, 7 months ago
On Tue, Apr 25, 2023 at 10:50 AM Steven Rostedt <rostedt@goodmis.org> wrote:
>
> On Tue, 25 Apr 2023 06:14:33 -0700
> Yosry Ahmed <yosryahmed@google.com> wrote:
>
>
> > Hey Steven,
> >
> > Is there a chance this patch makes it in the next merge window? It
> > would be convenient to have it in Linus's tree so that we can send
> > patches using the new helper. I am assuming there is no risk involved
> > in merging an unused helper (but I can easily be wrong :)).
>
> Yeah, the merge window opened quicker than I expected. I don't think this
> would be a problem to pull in. It's not high risk.

Thanks a lot!

>
> -- Steve
>
Re: [PATCHv3] seq_buf: add seq_buf_do_printk() helper
Posted by Yosry Ahmed 2 years, 8 months ago
On Sat, Apr 15, 2023 at 3:01 AM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> Sometimes we use seq_buf to format a string buffer, which
> we then pass to printk(). However, in certain situations
> the seq_buf string buffer can get too big, exceeding the
> PRINTKRB_RECORD_MAX bytes limit, and causing printk() to
> truncate the string.
>
> Add a new seq_buf helper. This helper prints the seq_buf
> string buffer line by line, using \n as a delimiter,
> rather than passing the whole string buffer to printk()
> at once.
>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
> Reviewed-by: Petr Mladek <pmladek@suse.com>

Tested-by: Yosry Ahmed <yosryahmed@google.com>

Thanks!
> ---
>  include/linux/seq_buf.h |  2 ++
>  lib/seq_buf.c           | 32 ++++++++++++++++++++++++++++++++
>  2 files changed, 34 insertions(+)
>
> diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h
> index 5b31c5147969..515d7fcb9634 100644
> --- a/include/linux/seq_buf.h
> +++ b/include/linux/seq_buf.h
> @@ -159,4 +159,6 @@ extern int
>  seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary);
>  #endif
>
> +void seq_buf_do_printk(struct seq_buf *s, const char *lvl);
> +
>  #endif /* _LINUX_SEQ_BUF_H */
> diff --git a/lib/seq_buf.c b/lib/seq_buf.c
> index 0a68f7aa85d6..45c450f423fa 100644
> --- a/lib/seq_buf.c
> +++ b/lib/seq_buf.c
> @@ -93,6 +93,38 @@ int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
>  }
>  EXPORT_SYMBOL_GPL(seq_buf_printf);
>
> +/**
> + * seq_buf_do_printk - printk seq_buf line by line
> + * @s: seq_buf descriptor
> + * @lvl: printk level
> + *
> + * printk()-s a multi-line sequential buffer line by line. The function
> + * makes sure that the buffer in @s is nul terminated and safe to read
> + * as a string.
> + */
> +void seq_buf_do_printk(struct seq_buf *s, const char *lvl)
> +{
> +       const char *start, *lf;
> +
> +       if (s->size == 0 || s->len == 0)
> +               return;
> +
> +       seq_buf_terminate(s);
> +
> +       start = s->buffer;
> +       while ((lf = strchr(start, '\n'))) {
> +               int len = lf - start + 1;
> +
> +               printk("%s%.*s", lvl, len, start);
> +               start = ++lf;
> +       }
> +
> +       /* No trailing LF */
> +       if (start < s->buffer + s->len)
> +               printk("%s%s\n", lvl, start);
> +}
> +EXPORT_SYMBOL_GPL(seq_buf_do_printk);
> +
>  #ifdef CONFIG_BINARY_PRINTF
>  /**
>   * seq_buf_bprintf - Write the printf string from binary arguments
> --
> 2.40.0.634.g4ca3ef3211-goog
>