Patch "seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str()" has been added to the 6.6-stable tree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is a note to let you know that I've just added the patch titled

    seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str()

to the 6.6-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     seq_buf-introduce-declare_seq_buf-and-seq_buf_str.patch
and it can be found in the queue-6.6 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@xxxxxxxxxxxxxxx> know about it.



commit 392c7cdcd55506fb088ed61b4b3d474b6c766e4f
Author: Kees Cook <kees@xxxxxxxxxx>
Date:   Fri Oct 27 08:56:38 2023 -0700

    seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str()
    
    [ Upstream commit dcc4e5728eeaeda84878ca0018758cff1abfca21 ]
    
    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/20231027155634.make.260-kees@xxxxxxxxxx
    Link: https://lore.kernel.org/linux-trace-kernel/20231026194033.it.702-kees@xxxxxxxxxx/
    
    Cc: Yosry Ahmed <yosryahmed@xxxxxxxxxx>
    Cc: "Matthew Wilcox (Oracle)" <willy@xxxxxxxxxxxxx>
    Cc: Christoph Hellwig <hch@xxxxxx>
    Cc: Justin Stitt <justinstitt@xxxxxxxxxx>
    Cc: Kent Overstreet <kent.overstreet@xxxxxxxxx>
    Cc: Petr Mladek <pmladek@xxxxxxxx>
    Cc: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
    Cc: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
    Cc: Sergey Senozhatsky <senozhatsky@xxxxxxxxxxxx>
    Cc: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
    Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
    Cc: Arnd Bergmann <arnd@xxxxxxxx>
    Cc: Jonathan Corbet <corbet@xxxxxxx>
    Cc: Yun Zhou <yun.zhou@xxxxxxxxxxxxx>
    Cc: Jacob Keller <jacob.e.keller@xxxxxxxxx>
    Cc: Zhen Lei <thunder.leizhen@xxxxxxxxxx>
    Signed-off-by: Kees Cook <keescook@xxxxxxxxxxxx>
    Signed-off-by: Steven Rostedt (Google) <rostedt@xxxxxxxxxxx>
    Stable-dep-of: afd2627f727b ("tracing: Check "%s" dereference via the field and not the TP_printk format")
    Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>

diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h
index a0fb013cebdf..d9db59f420a4 100644
--- a/include/linux/seq_buf.h
+++ b/include/linux/seq_buf.h
@@ -21,9 +21,18 @@ struct seq_buf {
 	size_t			len;
 };
 
+#define DECLARE_SEQ_BUF(NAME, SIZE)			\
+	char __ ## NAME ## _buffer[SIZE] = "";		\
+	struct seq_buf NAME = {				\
+		.buffer = &__ ## NAME ## _buffer,	\
+		.size = SIZE,				\
+	}
+
 static inline void seq_buf_clear(struct seq_buf *s)
 {
 	s->len = 0;
+	if (s->size)
+		s->buffer[0] = '\0';
 }
 
 static inline void
@@ -69,8 +78,8 @@ static inline unsigned int seq_buf_used(struct seq_buf *s)
 }
 
 /**
- * seq_buf_terminate - Make sure buffer is nul terminated
- * @s: the seq_buf descriptor to terminate.
+ * seq_buf_str - get %NUL-terminated C string from seq_buf
+ * @s: the seq_buf handle
  *
  * This makes sure that the buffer in @s is nul terminated and
  * safe to read as a string.
@@ -81,16 +90,20 @@ static inline unsigned int seq_buf_used(struct seq_buf *s)
  *
  * After this function is called, s->buffer is safe to use
  * in string operations.
+ *
+ * Returns @s->buf after making sure it is terminated.
  */
-static inline void seq_buf_terminate(struct seq_buf *s)
+static inline const char *seq_buf_str(struct seq_buf *s)
 {
 	if (WARN_ON(s->size == 0))
-		return;
+		return "";
 
 	if (seq_buf_buffer_left(s))
 		s->buffer[s->len] = 0;
 	else
 		s->buffer[s->size - 1] = 0;
+
+	return s->buffer;
 }
 
 /**
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 83f6ef4d7419..d9406a1f8795 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -3810,15 +3810,6 @@ static bool trace_safe_str(struct trace_iterator *iter, const char *str,
 	return false;
 }
 
-static const char *show_buffer(struct trace_seq *s)
-{
-	struct seq_buf *seq = &s->seq;
-
-	seq_buf_terminate(seq);
-
-	return seq->buffer;
-}
-
 static DEFINE_STATIC_KEY_FALSE(trace_no_verify);
 
 static int test_can_verify_check(const char *fmt, ...)
@@ -3958,7 +3949,7 @@ void trace_check_vprintf(struct trace_iterator *iter, const char *fmt,
 		 */
 		if (WARN_ONCE(!trace_safe_str(iter, str, star, len),
 			      "fmt: '%s' current_buffer: '%s'",
-			      fmt, show_buffer(&iter->seq))) {
+			      fmt, seq_buf_str(&iter->seq.seq))) {
 			int ret;
 
 			/* Try to safely read the string */
diff --git a/lib/seq_buf.c b/lib/seq_buf.c
index b7477aefff53..23518f77ea9c 100644
--- a/lib/seq_buf.c
+++ b/lib/seq_buf.c
@@ -109,9 +109,7 @@ void seq_buf_do_printk(struct seq_buf *s, const char *lvl)
 	if (s->size == 0 || s->len == 0)
 		return;
 
-	seq_buf_terminate(s);
-
-	start = s->buffer;
+	start = seq_buf_str(s);
 	while ((lf = strchr(start, '\n'))) {
 		int len = lf - start + 1;
 




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux