From: Alice Ryhl <aliceryhl@google.com>
To: John Hubbard <jhubbard@nvidia.com>
Cc: "Tamir Duberstein" <tamird@gmail.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] rust: check type of `$ptr` in `container_of!`
Date: Mon, 28 Apr 2025 09:40:03 +0000 [thread overview]
Message-ID: <aA9M8_K0MQfWg52t@google.com> (raw)
In-Reply-To: <34457c78-fdcd-4f1b-a349-4ca9bcc2febc@nvidia.com>
On Sun, Apr 27, 2025 at 03:59:48PM -0700, John Hubbard wrote:
> On 4/23/25 10:40 AM, Tamir Duberstein wrote:
> ...
> > diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
> > index 1df11156302a..d14ed86efb68 100644
> > --- a/rust/kernel/lib.rs
> > +++ b/rust/kernel/lib.rs
> > @@ -198,9 +198,15 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
> > /// ```
> > #[macro_export]
> > macro_rules! container_of {
> > - ($ptr:expr, $type:ty, $($f:tt)*) => {{
> > - let offset: usize = ::core::mem::offset_of!($type, $($f)*);
> > - $ptr.byte_sub(offset).cast::<$type>()
> > + ($field_ptr:expr, $Container:ty, $($fields:tt)*) => {{
> > + let offset: usize = ::core::mem::offset_of!($Container, $($fields)*);
> > + let field_ptr = $field_ptr;
> > + let container_ptr = field_ptr.byte_sub(offset).cast::<$Container>();
> > + if false {
>
> This jumped out at me. It's something that I'd like to recommend NOT
> doing, here or anywhere else, because:
>
> a) Anything of the form "if false" will get removed by any compiler
> worthy of the name, especially in kernel builds.
The `if false` branch is used to trigger a compilation failure when the
macro is used incorrectly. The intent is that the compiler should
optimize it out. I don't think there's anything wrong with that pattern.
Alice
next prev parent reply other threads:[~2025-04-28 9:40 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-23 17:40 Tamir Duberstein
2025-04-24 11:53 ` Alice Ryhl
2025-04-24 13:47 ` Tamir Duberstein
2025-04-25 9:50 ` Alice Ryhl
2025-04-24 12:48 ` Miguel Ojeda
2025-04-24 13:47 ` Tamir Duberstein
2025-04-27 22:59 ` John Hubbard
2025-04-28 9:40 ` Alice Ryhl [this message]
2025-04-28 19:54 ` John Hubbard
2025-04-29 8:20 ` Alice Ryhl
2025-04-29 20:35 ` John Hubbard
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=aA9M8_K0MQfWg52t@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=jhubbard@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@gmail.com \
--cc=tmgross@umich.edu \
/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
all inboxes | Powered by JetHome®