From: "Benno Lossin" <lossin@kernel.org>
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>,
"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 v4] rust: check type of `$ptr` in `container_of!`
Date: Thu, 29 May 2025 17:31:57 +0200 [thread overview]
Message-ID: <DA8R4900CNVG.1IMAV3SFPFS0B@kernel.org> (raw)
In-Reply-To: <20250529-b4-container-of-type-check-v4-1-bf3a7ad73cec@gmail.com>
On Thu May 29, 2025 at 3:11 PM CEST, 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:220:45
>> |
>> 220 | $crate::assert_same_type(field_ptr, (&raw const (*container_ptr).$($fields)*).cast_mut());
>> | ------------------------ --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `*mut rb_node`, found `*mut K`
>> | | |
>> | | expected all arguments to be this `*mut bindings::rb_node` type because they need to match the type of this parameter
>> | arguments to this function are incorrect
>> |
>> ::: 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: function defined here
>> --> rust/kernel/lib.rs:227:8
>> |
>> 227 | pub fn assert_same_type<T>(_: T, _: T) {}
>> | ^^^^^^^^^^^^^^^^ - ---- ---- this parameter needs to match the `*mut bindings::rb_node` type of parameter #1
>> | | |
>> | | parameter #2 needs to match the `*mut bindings::rb_node` type of this parameter
>> | parameter #1 and parameter #2 both reference this parameter `T`
>> = note: this error originates in the macro `container_of` (in Nightly builds, run with -Z macro-backtrace for more info)
In the future we could make this a proc-macro and improve the error
message by creating the function inline and setting the spans for the
parameter of the function to the spans coming from the input.
> 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: Benno Lossin <lossin@kernel.org>
---
Cheers,
Benno
> ---
> Changes in v4:
> - Revert back to v1 with assert_same_type extracted out of the macro. (Miguel Ojeda)
> - Drop Benno's RB since the implementation changed.
> - Rebase on rust-next.
> - Link to v3: https://lore.kernel.org/r/20250423-b4-container-of-type-check-v3-1-7994c56cf359@gmail.com
>
> Changes in v3:
> - Fix comment typo.
> - s/^;/ / in commit message and cover letter. (Miguel Ojeda)
> - Evaluate $ptr only once. (Alice Ryhl)
> - Link to v2: https://lore.kernel.org/r/20250412-b4-container-of-type-check-v2-1-f3cc9934c160@gmail.com
>
> Changes in v2:
> - Wrap in `if false` to improve unoptimized codegen. (Alice Ryhl)
> - Shrink implementation using an array literal instead of a function.
> - Link to v1: https://lore.kernel.org/r/20250411-b4-container-of-type-check-v1-1-08262ef67c95@gmail.com
> ---
> rust/kernel/lib.rs | 13 ++++++++++---
> 1 file changed, 10 insertions(+), 3 deletions(-)
next prev parent reply other threads:[~2025-05-29 15:32 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-29 13:11 [PATCH v4] rust: check type of `$ptr` in `container_of!` Tamir Duberstein
2025-05-29 13:24 ` Miguel Ojeda
2025-05-29 13:30 ` Miguel Ojeda
2025-05-29 14:39 ` Tamir Duberstein
2025-05-29 16:06 ` Miguel Ojeda
2025-05-29 16:55 ` Tamir Duberstein
2025-05-29 15:31 ` Benno Lossin [this message]
2025-05-29 21:19 ` Miguel Ojeda
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=DA8R4900CNVG.1IMAV3SFPFS0B@kernel.org \
--to=lossin@kernel.org \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--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.