Skip to content

Commit 96928d9

Browse files
sergey-senozhatskyrostedt
authored andcommitted
seq_buf: Add seq_buf_do_printk() helper
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. Link: https://lkml.kernel.org/r/20230415100110.1419872-1-senozhatsky@chromium.org Cc: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org> Reviewed-by: Petr Mladek <pmladek@suse.com> Tested-by: Yosry Ahmed <yosryahmed@google.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent c7bdb07 commit 96928d9

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

include/linux/seq_buf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,6 @@ extern int
159159
seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary);
160160
#endif
161161

162+
void seq_buf_do_printk(struct seq_buf *s, const char *lvl);
163+
162164
#endif /* _LINUX_SEQ_BUF_H */

lib/seq_buf.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,38 @@ int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
9393
}
9494
EXPORT_SYMBOL_GPL(seq_buf_printf);
9595

96+
/**
97+
* seq_buf_do_printk - printk seq_buf line by line
98+
* @s: seq_buf descriptor
99+
* @lvl: printk level
100+
*
101+
* printk()-s a multi-line sequential buffer line by line. The function
102+
* makes sure that the buffer in @s is nul terminated and safe to read
103+
* as a string.
104+
*/
105+
void seq_buf_do_printk(struct seq_buf *s, const char *lvl)
106+
{
107+
const char *start, *lf;
108+
109+
if (s->size == 0 || s->len == 0)
110+
return;
111+
112+
seq_buf_terminate(s);
113+
114+
start = s->buffer;
115+
while ((lf = strchr(start, '\n'))) {
116+
int len = lf - start + 1;
117+
118+
printk("%s%.*s", lvl, len, start);
119+
start = ++lf;
120+
}
121+
122+
/* No trailing LF */
123+
if (start < s->buffer + s->len)
124+
printk("%s%s\n", lvl, start);
125+
}
126+
EXPORT_SYMBOL_GPL(seq_buf_do_printk);
127+
96128
#ifdef CONFIG_BINARY_PRINTF
97129
/**
98130
* seq_buf_bprintf - Write the printf string from binary arguments

0 commit comments

Comments
 (0)