Skip to content

Commit 8802e16

Browse files
Darksonnojeda
authored andcommitted
rust: types: add Opaque::cast_from
Since commit b20fbbc ("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 update the docs to reflect this as well as some existing users. Signed-off-by: Alice Ryhl <aliceryhl@google.com> Acked-by: Andreas Hindborg <a.hindborg@kernel.org> Acked-by: Boqun Feng <boqun.feng@gmail.com> Reviewed-by: Danilo Krummrich <dakr@kernel.org> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20250624-opaque-from-raw-v2-1-e4da40bdc59c@google.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent a68a6be commit 8802e16

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

rust/kernel/drm/device.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,9 @@ impl<T: drm::Driver> Device<T> {
135135
///
136136
/// `ptr` must be a valid pointer to a `struct device` embedded in `Self`.
137137
unsafe fn from_drm_device(ptr: *const bindings::drm_device) -> *mut Self {
138-
let ptr: *const Opaque<bindings::drm_device> = ptr.cast();
139-
140138
// SAFETY: By the safety requirements of this function `ptr` is a valid pointer to a
141139
// `struct drm_device` embedded in `Self`.
142-
unsafe { crate::container_of!(ptr, Self, dev) }.cast_mut()
140+
unsafe { crate::container_of!(Opaque::cast_from(ptr), Self, dev) }.cast_mut()
143141
}
144142

145143
/// Not intended to be called externally, except via declare_drm_ioctls!()

rust/kernel/drm/gem/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,9 @@ impl<T: DriverObject> IntoGEMObject for Object<T> {
125125
}
126126

127127
unsafe fn as_ref<'a>(self_ptr: *mut bindings::drm_gem_object) -> &'a Self {
128-
let self_ptr: *mut Opaque<bindings::drm_gem_object> = self_ptr.cast();
129-
130128
// SAFETY: `obj` is guaranteed to be in an `Object<T>` via the safety contract of this
131129
// function
132-
unsafe { &*crate::container_of!(self_ptr, Object<T>, obj) }
130+
unsafe { &*crate::container_of!(Opaque::cast_from(self_ptr), Object<T>, obj) }
133131
}
134132
}
135133

rust/kernel/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
204204

205205
/// Produces a pointer to an object from a pointer to one of its fields.
206206
///
207+
/// If you encounter a type mismatch due to the [`Opaque`] type, then use [`Opaque::raw_get`] or
208+
/// [`Opaque::cast_from`] to resolve the mismatch.
209+
///
210+
/// [`Opaque`]: crate::types::Opaque
211+
/// [`Opaque::raw_get`]: crate::types::Opaque::raw_get
212+
/// [`Opaque::cast_from`]: crate::types::Opaque::cast_from
213+
///
207214
/// # Safety
208215
///
209216
/// The pointer passed to this macro, and the pointer returned by this macro, must both be in

rust/kernel/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ impl<T> Opaque<T> {
413413
pub const fn raw_get(this: *const Self) -> *mut T {
414414
UnsafeCell::raw_get(this.cast::<UnsafeCell<MaybeUninit<T>>>()).cast::<T>()
415415
}
416+
417+
/// The opposite operation of [`Opaque::raw_get`].
418+
pub const fn cast_from(this: *const T) -> *const Self {
419+
this.cast()
420+
}
416421
}
417422

418423
/// Types that are _always_ reference counted.

0 commit comments

Comments
 (0)