From: Kees Cook <kees@kernel.org>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: LKML <linux-kernel@vger.kernel.org>,
linux-hardening@vger.kernel.org,
Linux Trace Kernel <linux-trace-kernel@vger.kernel.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
"Gustavo A. R. Silva" <gustavoars@kernel.org>
Subject: Re: [PATCH] overflow: Introduce struct_offset() to get offset of member
Date: Wed, 26 Nov 2025 23:58:01 -0800 [thread overview]
Message-ID: <202511262356.6FE5084CB0@keescook> (raw)
In-Reply-To: <20251126145249.05b1770a@gandalf.local.home>
On Wed, Nov 26, 2025 at 02:52:49PM -0500, Steven Rostedt wrote:
> 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/
>
> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
Works for me!
Reviewed-by: Kees Cook <kees@kernel.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))
I wonder if the kerndoc for this and offsetof() should reference each
other? "For a type instead of a pointer, use offsetof()" etc...
--
Kees Cook
next prev parent reply other threads:[~2025-11-27 7:58 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-26 19:52 [PATCH] overflow: Introduce struct_offset() to get offset of member Steven Rostedt
2025-11-26 20:08 ` Steven Rostedt
2025-11-27 7:58 ` Kees Cook [this message]
2025-11-28 1:43 ` Steven Rostedt
2025-11-28 2:00 ` Kees Cook
2025-11-28 3:27 ` Steven Rostedt
2025-11-28 5:35 ` Linus Torvalds
2025-11-28 17:19 ` Steven Rostedt
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=202511262356.6FE5084CB0@keescook \
--to=kees@kernel.org \
--cc=gustavoars@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=rostedt@goodmis.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;
as well as URLs for NNTP newsgroup(s).