[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAJ-ks9nMqjj85rK6LsSnTPqsTHmMUDJsQxqJ7n2+r4oSZqBibA@mail.gmail.com>
Date: Thu, 24 Apr 2025 09:47:52 -0400
From: Tamir Duberstein <tamird@...il.com>
To: Alice Ryhl <aliceryhl@...gle.com>
Cc: 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>,
Trevor Gross <tmgross@...ch.edu>, Danilo Krummrich <dakr@...nel.org>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v3] rust: check type of `$ptr` in `container_of!`
On Thu, Apr 24, 2025 at 7:53 AM Alice Ryhl <aliceryhl@...gle.com> wrote:
>
> 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@...ton.me>
> > Suggested-by: Alice Ryhl <aliceryhl@...gle.com>
> > Link: https://lore.kernel.org/all/CAH5fLgh6gmqGBhPMi2SKn7mCmMWfOSiS0WP5wBuGPYh9ZTAiww@mail.gmail.com/
> > Signed-off-by: Tamir Duberstein <tamird@...il.com>
>
> Reviewed-by: Alice Ryhl <aliceryhl@...gle.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.
I took the parameter name from `offset_of`:
https://doc.rust-lang.org/stable/std/mem/macro.offset_of.html.
Powered by blists - more mailing lists