From: Alice Ryhl <aliceryhl@google.com>
To: Tamir Duberstein <tamird@gmail.com>
Cc: "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: Thu, 24 Apr 2025 11:53:40 +0000 [thread overview]
Message-ID: <aAomRMzyu3EX5Xal@google.com> (raw)
In-Reply-To: <20250423-b4-container-of-type-check-v3-1-7994c56cf359@gmail.com>
On Wed, Apr 23, 2025 at 01:40:10PM -0400, Tamir Duberstein wrote:
> Add a compile-time check that `*$ptr` is of the type of `$type->$($f)*`.
> Rename those placeholders for clarity.
>
> Given the incorrect usage:
>
> > diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
> > index 8d978c896747..6a7089149878 100644
> > --- a/rust/kernel/rbtree.rs
> > +++ b/rust/kernel/rbtree.rs
> > @@ -329,7 +329,7 @@ fn raw_entry(&mut self, key: &K) -> RawEntry<'_, K, V> {
> > while !(*child_field_of_parent).is_null() {
> > let curr = *child_field_of_parent;
> > // SAFETY: All links fields we create are in a `Node<K, V>`.
> > - let node = unsafe { container_of!(curr, Node<K, V>, links) };
> > + let node = unsafe { container_of!(curr, Node<K, V>, key) };
> >
> > // SAFETY: `node` is a non-null node so it is valid by the type invariants.
> > match key.cmp(unsafe { &(*node).key }) {
>
> this patch produces the compilation error:
>
> > error[E0308]: mismatched types
> > --> rust/kernel/lib.rs:207:25
> > |
> > 207 | [field_ptr, container_field_ptr]; // typeof(`$field_ptr`) == typeof(`$Container.$($fields)*`)
> > | ^^^^^^^^^^^^^^^^^^^ expected `*mut rb_node`, found `*mut K`
> > |
> > ::: rust/kernel/rbtree.rs:270:6
> > |
> > 270 | impl<K, V> RBTree<K, V>
> > | - found this type parameter
> > ...
> > 332 | let node = unsafe { container_of!(curr, Node<K, V>, key) };
> > | ------------------------------------ in this macro invocation
> > |
> > = note: expected raw pointer `*mut bindings::rb_node`
> > found raw pointer `*mut K`
> > = note: this error originates in the macro `container_of` (in Nightly builds, run with -Z macro-backtrace for more info)
> >
> > error: aborting due to 1 previous error
>
> Reviewed-by: Benno Lossin <benno.lossin@proton.me>
> Suggested-by: Alice Ryhl <aliceryhl@google.com>
> Link: https://lore.kernel.org/all/CAH5fLgh6gmqGBhPMi2SKn7mCmMWfOSiS0WP5wBuGPYh9ZTAiww@mail.gmail.com/
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
> rust/kernel/lib.rs | 12 +++++++++---
> 1 file changed, 9 insertions(+), 3 deletions(-)
>
> 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)*) => {{
It's rather unusual to use an uppercase C in the name of this parameter.
Alice
> + 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 {
> + let container_field_ptr = ::core::ptr::addr_of!((*container_ptr).$($fields)*).cast_mut();
> + [field_ptr, container_field_ptr]; // typeof(`$field_ptr`) == typeof(`$Container.$($fields)*`)
> + }
> + container_ptr
> }}
> }
>
>
> ---
> base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8
> change-id: 20250411-b4-container-of-type-check-06af1c204f59
> prerequisite-change-id: 20250409-container-of-mutness-b153dab4388d:v1
> prerequisite-patch-id: 53d5889db599267f87642bb0ae3063c29bc24863
>
> Best regards,
> --
> Tamir Duberstein <tamird@gmail.com>
>
next prev parent reply other threads:[~2025-04-24 11:53 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 [this message]
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
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=aAomRMzyu3EX5Xal@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=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.