From: Steven Rostedt <rostedt@goodmis.org>
To: Kees Cook <kees@kernel.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: Thu, 27 Nov 2025 20:43:42 -0500 [thread overview]
Message-ID: <20251127204342.05fc985e@robin> (raw)
In-Reply-To: <202511262356.6FE5084CB0@keescook>
On Wed, 26 Nov 2025 23:58:01 -0800
Kees Cook <kees@kernel.org> wrote:
> > +/**
> > + * 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...
I know I pushed this to my for-next branch already, but it's the top
patch. Looking at my code, I actually have a lot of places that use the
offsetof() for a structure variable and not a pointer to a structure.
Thus, I wonder if it is better to have this as:
#define struct_offset(s, member) (offsetof(typeof(s), member))
And then for pointers we have:
size = struct_offset(*entry, id) + cnt;
If you forget the '*' it will error with a complaint that entry is not
a structure type. Then I could make changes like this:
diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 1244d2c5c384..55f1bdab4ffa 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -587,19 +587,19 @@ int ring_buffer_print_page_header(struct trace_buffer *buffer, struct trace_seq
trace_seq_printf(s, "\tfield: local_t commit;\t"
"offset:%u;\tsize:%u;\tsigned:%u;\n",
- (unsigned int)offsetof(typeof(field), commit),
+ (unsigned int)struct_offset(field, commit),
(unsigned int)sizeof(field.commit),
(unsigned int)is_signed_type(long));
trace_seq_printf(s, "\tfield: int overwrite;\t"
"offset:%u;\tsize:%u;\tsigned:%u;\n",
- (unsigned int)offsetof(typeof(field), commit),
+ (unsigned int)struct_offset(field, commit),
1,
(unsigned int)is_signed_type(long));
trace_seq_printf(s, "\tfield: char data;\t"
"offset:%u;\tsize:%u;\tsigned:%u;\n",
- (unsigned int)offsetof(typeof(field), data),
+ (unsigned int)struct_offset(field, data),
(unsigned int)buffer->subbuf_size,
(unsigned int)is_signed_type(char));
I would have a lot more places I can make this update then if
struct_offset() took a pointer instead of the struct itself. As adding
a '*' isn't ugly I think I like this way better.
What are your thoughts?
-- Steve
next prev parent reply other threads:[~2025-11-28 1:43 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
2025-11-28 1:43 ` Steven Rostedt [this message]
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=20251127204342.05fc985e@robin \
--to=rostedt@goodmis.org \
--cc=gustavoars@kernel.org \
--cc=kees@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=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).