From nobody Wed Dec 31 18:57:58 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9476C4332F for ; Mon, 30 Oct 2023 14:47:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233469AbjJ3Orw (ORCPT ); Mon, 30 Oct 2023 10:47:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231598AbjJ3Oru (ORCPT ); Mon, 30 Oct 2023 10:47:50 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B78C3C2 for ; Mon, 30 Oct 2023 07:47:47 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4D22C433C7; Mon, 30 Oct 2023 14:47:46 +0000 (UTC) Date: Mon, 30 Oct 2023 10:47:44 -0400 From: Steven Rostedt To: LKML Cc: Masami Hiramatsu , Mark Rutland , Kees Cook Subject: [for-next][PATCH] seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str() Message-ID: <20231030104744.11e776ce@gandalf.local.home> X-Mailer: Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git trace/for-next Head SHA1: dcc4e5728eeaeda84878ca0018758cff1abfca21 Kees Cook (1): seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str() ---- include/linux/seq_buf.h | 21 +++++++++++++++++---- kernel/trace/trace.c | 11 +---------- lib/seq_buf.c | 4 +--- 3 files changed, 19 insertions(+), 17 deletions(-) --------------------------- commit dcc4e5728eeaeda84878ca0018758cff1abfca21 Author: Kees Cook Date: Fri Oct 27 08:56:38 2023 -0700 seq_buf: Introduce DECLARE_SEQ_BUF and seq_buf_str() =20 Solve two ergonomic issues with struct seq_buf; =20 1) Too much boilerplate is required to initialize: =20 struct seq_buf s; char buf[32]; =20 seq_buf_init(s, buf, sizeof(buf)); =20 Instead, we can build this directly on the stack. Provide DECLARE_SEQ_BUF() macro to do this: =20 DECLARE_SEQ_BUF(s, 32); =20 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): =20 seq_buf_terminate(s); do_something(s->buffer); =20 Instead, we can just return s->buffer directly after terminating it in the refactored seq_buf_terminate(), now known as seq_buf_str(): =20 do_something(seq_buf_str(s)); =20 Link: https://lore.kernel.org/linux-trace-kernel/20231027155634.make.26= 0-kees@kernel.org Link: https://lore.kernel.org/linux-trace-kernel/20231026194033.it.702-= kees@kernel.org/ =20 Cc: Yosry Ahmed Cc: "Matthew Wilcox (Oracle)" Cc: Christoph Hellwig Cc: Justin Stitt Cc: Kent Overstreet Cc: Petr Mladek Cc: Andy Shevchenko Cc: Rasmus Villemoes Cc: Sergey Senozhatsky Cc: Masami Hiramatsu Cc: Greg Kroah-Hartman Cc: Arnd Bergmann Cc: Jonathan Corbet Cc: Yun Zhou Cc: Jacob Keller Cc: Zhen Lei Signed-off-by: Kees Cook Signed-off-by: Steven Rostedt (Google) diff --git a/include/linux/seq_buf.h b/include/linux/seq_buf.h index 8483e4b2d0d2..5fb1f12c33f9 100644 --- a/include/linux/seq_buf.h +++ b/include/linux/seq_buf.h @@ -21,9 +21,18 @@ struct seq_buf { size_t len; }; =20 +#define DECLARE_SEQ_BUF(NAME, SIZE) \ + char __ ## NAME ## _buffer[SIZE] =3D ""; \ + struct seq_buf NAME =3D { \ + .buffer =3D &__ ## NAME ## _buffer, \ + .size =3D SIZE, \ + } + static inline void seq_buf_clear(struct seq_buf *s) { s->len =3D 0; + if (s->size) + s->buffer[0] =3D '\0'; } =20 static inline void @@ -69,8 +78,8 @@ static inline unsigned int seq_buf_used(struct seq_buf *s) } =20 /** - * 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 =3D=3D 0)) - return; + return ""; =20 if (seq_buf_buffer_left(s)) s->buffer[s->len] =3D 0; else s->buffer[s->size - 1] =3D 0; + + return s->buffer; } =20 /** diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index d629065c2383..2539cfc20a97 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -3828,15 +3828,6 @@ static bool trace_safe_str(struct trace_iterator *it= er, const char *str, return false; } =20 -static const char *show_buffer(struct trace_seq *s) -{ - struct seq_buf *seq =3D &s->seq; - - seq_buf_terminate(seq); - - return seq->buffer; -} - static DEFINE_STATIC_KEY_FALSE(trace_no_verify); =20 static int test_can_verify_check(const char *fmt, ...) @@ -3976,7 +3967,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; =20 /* 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 *l= vl) if (s->size =3D=3D 0 || s->len =3D=3D 0) return; =20 - seq_buf_terminate(s); - - start =3D s->buffer; + start =3D seq_buf_str(s); while ((lf =3D strchr(start, '\n'))) { int len =3D lf - start + 1;