[PATCH v2 0/2] Add Opaque::cast_from

Alice Ryhl posted 2 patches 3 months, 2 weeks ago
rust/kernel/configfs.rs                |  2 +-
rust/kernel/drm/device.rs              |  4 +---
rust/kernel/drm/gem/mod.rs             |  4 +---
rust/kernel/init.rs                    |  6 +++---
rust/kernel/lib.rs                     |  7 +++++++
rust/kernel/list.rs                    |  2 +-
rust/kernel/list/impl_list_item_mod.rs |  4 ++--
rust/kernel/time/hrtimer.rs            |  4 ++--
rust/kernel/types.rs                   | 11 ++++++++---
rust/kernel/workqueue.rs               |  2 +-
10 files changed, 27 insertions(+), 19 deletions(-)
[PATCH v2 0/2] Add Opaque::cast_from
Posted by Alice Ryhl 3 months, 2 weeks ago
Since commit b20fbbc08a36 ("rust: check type of `$ptr` in
`container_of!`") we have enforced that the field pointer passed to
container_of! must match the declared field. This caused mismatches when
using a pointer to bindings::x for fields of type Opaque<bindings::x>.

This situation encourages the user to simply pass field.cast() to the
container_of! macro, but this is not great because you might
accidentally pass a *mut bindings::y when the field type is
Opaque<bindings::x>, which would be wrong.

To help catch this kind of mistake, add a new Opaque::cast_from that
wraps a raw pointer in Opaque without changing the inner type. Also
rename raw_get() to cast_into() for naming consistency.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
Changes in v2:
- Use new naming, and rename raw_get().
- Link to v1: https://lore.kernel.org/r/20250617-opaque-from-raw-v1-1-a2e99efa3ba2@google.com

---
Alice Ryhl (2):
      rust: types: add Opaque::cast_from
      rust: types: rename Opaque::raw_get to cast_into

 rust/kernel/configfs.rs                |  2 +-
 rust/kernel/drm/device.rs              |  4 +---
 rust/kernel/drm/gem/mod.rs             |  4 +---
 rust/kernel/init.rs                    |  6 +++---
 rust/kernel/lib.rs                     |  7 +++++++
 rust/kernel/list.rs                    |  2 +-
 rust/kernel/list/impl_list_item_mod.rs |  4 ++--
 rust/kernel/time/hrtimer.rs            |  4 ++--
 rust/kernel/types.rs                   | 11 ++++++++---
 rust/kernel/workqueue.rs               |  2 +-
 10 files changed, 27 insertions(+), 19 deletions(-)
---
base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494
change-id: 20250617-opaque-from-raw-ac5b8ef6faa2

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>
Re: [PATCH v2 0/2] Add Opaque::cast_from
Posted by Miguel Ojeda 2 months, 3 weeks ago
On Tue, Jun 24, 2025 at 5:28 PM Alice Ryhl <aliceryhl@google.com> wrote:
>
> Since commit b20fbbc08a36 ("rust: check type of `$ptr` in
> `container_of!`") we have enforced that the field pointer passed to
> container_of! must match the declared field. This caused mismatches when
> using a pointer to bindings::x for fields of type Opaque<bindings::x>.
>
> This situation encourages the user to simply pass field.cast() to the
> container_of! macro, but this is not great because you might
> accidentally pass a *mut bindings::y when the field type is
> Opaque<bindings::x>, which would be wrong.
>
> To help catch this kind of mistake, add a new Opaque::cast_from that
> wraps a raw pointer in Opaque without changing the inner type. Also
> rename raw_get() to cast_into() for naming consistency.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Applied (yesterday) to `rust-next` -- thanks everyone!

    [ Removed `HrTimer::raw_get` change. - Miguel ]

Cheers,
Miguel
Re: [PATCH v2 0/2] Add Opaque::cast_from
Posted by Danilo Krummrich 2 months, 3 weeks ago
On Tue Jun 24, 2025 at 5:27 PM CEST, Alice Ryhl wrote:
> Since commit b20fbbc08a36 ("rust: check type of `$ptr` in
> `container_of!`") we have enforced that the field pointer passed to
> container_of! must match the declared field. This caused mismatches when
> using a pointer to bindings::x for fields of type Opaque<bindings::x>.
>
> This situation encourages the user to simply pass field.cast() to the
> container_of! macro, but this is not great because you might
> accidentally pass a *mut bindings::y when the field type is
> Opaque<bindings::x>, which would be wrong.
>
> To help catch this kind of mistake, add a new Opaque::cast_from that
> wraps a raw pointer in Opaque without changing the inner type. Also
> rename raw_get() to cast_into() for naming consistency.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

For the series,

Reviewed-by: Danilo Krummrich <dakr@kernel.org>

For the DRM parts,

Acked-by: Danilo Krummrich <dakr@kernel.org>
Re: [PATCH v2 0/2] Add Opaque::cast_from
Posted by Andreas Hindborg 2 months, 3 weeks ago
"Alice Ryhl" <aliceryhl@google.com> writes:

> Since commit b20fbbc08a36 ("rust: check type of `$ptr` in
> `container_of!`") we have enforced that the field pointer passed to
> container_of! must match the declared field. This caused mismatches when
> using a pointer to bindings::x for fields of type Opaque<bindings::x>.
>
> This situation encourages the user to simply pass field.cast() to the
> container_of! macro, but this is not great because you might
> accidentally pass a *mut bindings::y when the field type is
> Opaque<bindings::x>, which would be wrong.
>
> To help catch this kind of mistake, add a new Opaque::cast_from that
> wraps a raw pointer in Opaque without changing the inner type. Also
> rename raw_get() to cast_into() for naming consistency.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

I think your search/replace was a bit too general in hrtimer.rs. When
you fix that:

Acked-by: Andreas Hindborg <a.hindborg@kernel.org>


Best regards,
Andreas Hindborg
Re: [PATCH v2 0/2] Add Opaque::cast_from
Posted by Boqun Feng 3 months, 2 weeks ago
On Tue, Jun 24, 2025 at 03:27:54PM +0000, Alice Ryhl wrote:
> Since commit b20fbbc08a36 ("rust: check type of `$ptr` in
> `container_of!`") we have enforced that the field pointer passed to
> container_of! must match the declared field. This caused mismatches when
> using a pointer to bindings::x for fields of type Opaque<bindings::x>.
> 
> This situation encourages the user to simply pass field.cast() to the
> container_of! macro, but this is not great because you might
> accidentally pass a *mut bindings::y when the field type is
> Opaque<bindings::x>, which would be wrong.
> 
> To help catch this kind of mistake, add a new Opaque::cast_from that
> wraps a raw pointer in Opaque without changing the inner type. Also
> rename raw_get() to cast_into() for naming consistency.
> 
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Acked-by: Boqun Feng <boqun.feng@gmail.com>

Thanks!

Regards,
Boqun
Re: [PATCH v2 0/2] Add Opaque::cast_from
Posted by Danilo Krummrich 3 months, 2 weeks ago
On 6/24/25 5:27 PM, Alice Ryhl wrote:
> Since commit b20fbbc08a36 ("rust: check type of `$ptr` in
> `container_of!`") we have enforced that the field pointer passed to
> container_of! must match the declared field. This caused mismatches when
> using a pointer to bindings::x for fields of type Opaque<bindings::x>.
> 
> This situation encourages the user to simply pass field.cast() to the
> container_of! macro, but this is not great because you might
> accidentally pass a *mut bindings::y when the field type is
> Opaque<bindings::x>, which would be wrong.
> 
> To help catch this kind of mistake, add a new Opaque::cast_from that
> wraps a raw pointer in Opaque without changing the inner type. Also
> rename raw_get() to cast_into() for naming consistency.

Reviewed-by: Danilo Krummrich <dakr@kernel.org>