rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Hubbard <jhubbard@nvidia.com>
To: "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>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] rust: check type of `$ptr` in `container_of!`
Date: Sun, 27 Apr 2025 15:59:48 -0700	[thread overview]
Message-ID: <34457c78-fdcd-4f1b-a349-4ca9bcc2febc@nvidia.com> (raw)
In-Reply-To: <20250423-b4-container-of-type-check-v3-1-7994c56cf359@gmail.com>

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.

     b) It is a "magic trick", in that the code is on the face of it,
     unnecessary. So that's not something that you would pick as your
     first choice anyway. But as I see now that Miguel has also pointed
     out, the -O2 optimization level that we build at makes it either
     unreliable or broken, so it's Bad Magic. :)

Anyway, I don't know where this pattern came from, but it's not a good
one for kernel builds.


thanks,
-- 
John Hubbard


  parent reply	other threads:[~2025-04-27 22:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-23 17:40 [PATCH v3] rust: check type of `$ptr` in `container_of!` 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 [this message]
2025-04-28  9:40   ` Alice Ryhl
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=34457c78-fdcd-4f1b-a349-4ca9bcc2febc@nvidia.com \
    --to=jhubbard@nvidia.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.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=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;
as well as URLs for NNTP newsgroup(s).