[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <34457c78-fdcd-4f1b-a349-4ca9bcc2febc@nvidia.com>
Date: Sun, 27 Apr 2025 15:59:48 -0700
From: John Hubbard <jhubbard@...dia.com>
To: Tamir Duberstein <tamird@...il.com>, Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>, Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>, Björn Roy Baron
<bjorn3_gh@...tonmail.com>, Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>, Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>
Cc: rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v3] rust: check type of `$ptr` in `container_of!`
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
Powered by blists - more mailing lists