Skip to content

Commit dcc4e57

Browse files
keesrostedt
authored andcommitted
seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str()
Solve two ergonomic issues with struct seq_buf; 1) Too much boilerplate is required to initialize: struct seq_buf s; char buf[32]; seq_buf_init(s, buf, sizeof(buf)); Instead, we can build this directly on the stack. Provide DECLARE_SEQ_BUF() macro to do this: DECLARE_SEQ_BUF(s, 32); 2) %NUL termination is fragile and requires 2 steps to get a valid C String (and is a layering violation exposing the "internals" of seq_buf): seq_buf_terminate(s); do_something(s->buffer); Instead, we can just return s->buffer directly after terminating it in the refactored seq_buf_terminate(), now known as seq_buf_str(): do_something(seq_buf_str(s)); Link: https://lore.kernel.org/linux-trace-kernel/[email protected] Link: https://lore.kernel.org/linux-trace-kernel/[email protected]/ Cc: Yosry Ahmed <[email protected]> Cc: "Matthew Wilcox (Oracle)" <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Justin Stitt <[email protected]> Cc: Kent Overstreet <[email protected]> Cc: Petr Mladek <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: Rasmus Villemoes <[email protected]> Cc: Sergey Senozhatsky <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Yun Zhou <[email protected]> Cc: Jacob Keller <[email protected]> Cc: Zhen Lei <[email protected]> Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 29e06c1 commit dcc4e57

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

include/linux/seq_buf.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ struct seq_buf {
2121
size_t len;
2222
};
2323

24+
#define DECLARE_SEQ_BUF(NAME, SIZE) \
25+
char __ ## NAME ## _buffer[SIZE] = ""; \
26+
struct seq_buf NAME = { \
27+
.buffer = &__ ## NAME ## _buffer, \
28+
.size = SIZE, \
29+
}
30+
2431
static inline void seq_buf_clear(struct seq_buf *s)
2532
{
2633
s->len = 0;
34+
if (s->size)
35+
s->buffer[0] = '\0';
2736
}
2837

2938
static inline void
@@ -69,8 +78,8 @@ static inline unsigned int seq_buf_used(struct seq_buf *s)
6978
}
7079

7180
/**
72-
* seq_buf_terminate - Make sure buffer is nul terminated
73-
* @s: the seq_buf descriptor to terminate.
81+
* seq_buf_str - get %NUL-terminated C string from seq_buf
82+
* @s: the seq_buf handle
7483
*
7584
* This makes sure that the buffer in @s is nul terminated and
7685
* safe to read as a string.
@@ -81,16 +90,20 @@ static inline unsigned int seq_buf_used(struct seq_buf *s)
8190
*
8291
* After this function is called, s->buffer is safe to use
8392
* in string operations.
93+
*
94+
* Returns @s->buf after making sure it is terminated.
8495
*/
85-
static inline void seq_buf_terminate(struct seq_buf *s)
96+
static inline const char *seq_buf_str(struct seq_buf *s)
8697
{
8798
if (WARN_ON(s->size == 0))
88-
return;
99+
return "";
89100

90101
if (seq_buf_buffer_left(s))
91102
s->buffer[s->len] = 0;
92103
else
93104
s->buffer[s->size - 1] = 0;
105+
106+
return s->buffer;
94107
}
95108

96109
/**

kernel/trace/trace.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,15 +3828,6 @@ static bool trace_safe_str(struct trace_iterator *iter, const char *str,
38283828
return false;
38293829
}
38303830

3831-
static const char *show_buffer(struct trace_seq *s)
3832-
{
3833-
struct seq_buf *seq = &s->seq;
3834-
3835-
seq_buf_terminate(seq);
3836-
3837-
return seq->buffer;
3838-
}
3839-
38403831
static DEFINE_STATIC_KEY_FALSE(trace_no_verify);
38413832

38423833
static int test_can_verify_check(const char *fmt, ...)
@@ -3976,7 +3967,7 @@ void trace_check_vprintf(struct trace_iterator *iter, const char *fmt,
39763967
*/
39773968
if (WARN_ONCE(!trace_safe_str(iter, str, star, len),
39783969
"fmt: '%s' current_buffer: '%s'",
3979-
fmt, show_buffer(&iter->seq))) {
3970+
fmt, seq_buf_str(&iter->seq.seq))) {
39803971
int ret;
39813972

39823973
/* Try to safely read the string */

lib/seq_buf.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ void seq_buf_do_printk(struct seq_buf *s, const char *lvl)
109109
if (s->size == 0 || s->len == 0)
110110
return;
111111

112-
seq_buf_terminate(s);
113-
114-
start = s->buffer;
112+
start = seq_buf_str(s);
115113
while ((lf = strchr(start, '\n'))) {
116114
int len = lf - start + 1;
117115

0 commit comments

Comments
 (0)