public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@kernel.org>
To: linux-kernel@vger.kernel.org
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Kees Cook <kees@kernel.org>
Subject: [for-next][PATCH 13/13] overflow: Introduce struct_offset() to get offset of member
Date: Thu, 27 Nov 2025 20:23:44 -0500	[thread overview]
Message-ID: <20251128012413.003751279@kernel.org> (raw)
In-Reply-To: 20251128012331.307010654@kernel.org

From: Steven Rostedt <rostedt@goodmis.org>

The trace_marker_raw file in tracefs takes a buffer from user space that
contains an id as well as a raw data string which is usually a binary
structure. The structure used has the following:

	struct raw_data_entry {
		struct trace_entry	ent;
		unsigned int		id;
		char			buf[];
	};

Since the passed in "cnt" variable is both the size of buf as well as the
size of id, the code to allocate the location on the ring buffer had:

   size = struct_size(entry, buf, cnt - sizeof(entry->id));

Which is quite ugly and hard to understand. Instead, add a helper macro
called struct_offset() which then changes the above to a simple and easy
to understand:

   size = struct_offset(entry, id) + cnt;

This will likely come in handy for other use cases too.

Link: https://lore.kernel.org/all/CAHk-=whYZVoEdfO1PmtbirPdBMTV9Nxt9f09CK0k6S+HJD3Zmg@mail.gmail.com/

Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Link: https://patch.msgid.link/20251126145249.05b1770a@gandalf.local.home
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Reviewed-by: Kees Cook <kees@kernel.org>
Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 include/linux/overflow.h | 12 ++++++++++++
 kernel/trace/trace.c     |  2 +-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index 725f95f7e416..736f633b2d5f 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -458,6 +458,18 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
 #define struct_size_t(type, member, count)					\
 	struct_size((type *)NULL, member, count)
 
+/**
+ * struct_offset() - Calculate the offset of a member within a struct
+ * @p: Pointer to the struct
+ * @member: Name of the member to get the offset of
+ *
+ * Calculates the offset of a particular @member of the structure pointed
+ * to by @p.
+ *
+ * Return: number of bytes to the location of @member.
+ */
+#define struct_offset(p, member) (offsetof(typeof(*(p)), member))
+
 /**
  * __DEFINE_FLEX() - helper macro for DEFINE_FLEX() family.
  * Enables caller macro to pass arbitrary trailing expressions
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 73f8b79f1b0c..3d433a426e5f 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -7642,7 +7642,7 @@ static ssize_t write_raw_marker_to_buffer(struct trace_array *tr,
 	size_t size;
 
 	/* cnt includes both the entry->id and the data behind it. */
-	size = struct_size(entry, buf, cnt - sizeof(entry->id));
+	size = struct_offset(entry, id) + cnt;
 
 	buffer = tr->array_buffer.buffer;
 
-- 
2.51.0



      parent reply	other threads:[~2025-11-28  1:23 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-28  1:23 [for-next][PATCH 00/13] tracing: Updates for v6.19 Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 01/13] fgraph: Make fgraph_no_sleep_time signed Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 02/13] tracing: Remove unused variable in tracing_trace_options_show() Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 03/13] ftrace: Avoid redundant initialization in register_ftrace_direct Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 04/13] tracing: Show the tracer options in boot-time created instance Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 05/13] tracing: Remove get_trigger_ops() and add count_func() from trigger ops Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 06/13] tracing: Merge struct event_trigger_ops into struct event_command Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 07/13] tracing: Remove unneeded event_mutex lock in event_trigger_regex_release() Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 08/13] tracing: Add bulk garbage collection of freeing event_trigger_data Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 09/13] tracing: Use strim() in trigger_process_regex() instead of skip_spaces() Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 10/13] ftrace: Allow tracing of some of the tracing code Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 11/13] tracing: Add boot-time backup of persistent ring buffer Steven Rostedt
2025-11-28  1:23 ` [for-next][PATCH 12/13] function_graph: Enable funcgraph-args and funcgraph-retaddr to work simultaneously Steven Rostedt
2025-11-28  1:23 ` Steven Rostedt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251128012413.003751279@kernel.org \
    --to=rostedt@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=gustavoars@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=torvalds@linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox