[PATCH] rust: drm/gem: Remove Object.dev

Lyude Paul posted 1 patch 3 months, 3 weeks ago
There is a newer version of this series
rust/kernel/drm/gem/mod.rs | 11 +++--------
1 file changed, 3 insertions(+), 8 deletions(-)
[PATCH] rust: drm/gem: Remove Object.dev
Posted by Lyude Paul 3 months, 3 weeks ago
I noticed by chance that there's actually already a pointer to this in
struct drm_gem_object. So, no use in carrying this around!

Signed-off-by: Lyude Paul <lyude@redhat.com>
---
 rust/kernel/drm/gem/mod.rs | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index 30c853988b942..28d929edae267 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -187,12 +187,10 @@ impl<T: IntoGEMObject> BaseObject for T {}
 /// Invariants
 ///
 /// - `self.obj` is a valid instance of a `struct drm_gem_object`.
-/// - `self.dev` is always a valid pointer to a `struct drm_device`.
 #[repr(C)]
 #[pin_data]
 pub struct Object<T: DriverObject + Send + Sync> {
     obj: Opaque<bindings::drm_gem_object>,
-    dev: NonNull<drm::Device<T::Driver>>,
     #[pin]
     data: T,
 }
@@ -222,9 +220,6 @@ pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {
             try_pin_init!(Self {
                 obj: Opaque::new(bindings::drm_gem_object::default()),
                 data <- T::new(dev, size),
-                // INVARIANT: The drm subsystem guarantees that the `struct drm_device` will live
-                // as long as the GEM object lives.
-                dev: dev.into(),
             }),
             GFP_KERNEL,
         )?;
@@ -247,9 +242,9 @@ pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {
 
     /// Returns the `Device` that owns this GEM object.
     pub fn dev(&self) -> &drm::Device<T::Driver> {
-        // SAFETY: The DRM subsystem guarantees that the `struct drm_device` will live as long as
-        // the GEM object lives, hence the pointer must be valid.
-        unsafe { self.dev.as_ref() }
+        // SAFETY: `struct drm_gem_object.dev` is initialized and valid for as long as the GEM
+        // object lives.
+        unsafe { drm::Device::from_raw((*self.as_raw()).dev) }
     }
 
     fn as_raw(&self) -> *mut bindings::drm_gem_object {

base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
-- 
2.51.0
Re: [PATCH] rust: drm/gem: Remove Object.dev
Posted by Alice Ryhl 3 months, 3 weeks ago
On Thu, Oct 16, 2025 at 04:18:58PM -0400, Lyude Paul wrote:
> I noticed by chance that there's actually already a pointer to this in
> struct drm_gem_object. So, no use in carrying this around!
> 
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
>  rust/kernel/drm/gem/mod.rs | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
> index 30c853988b942..28d929edae267 100644
> --- a/rust/kernel/drm/gem/mod.rs
> +++ b/rust/kernel/drm/gem/mod.rs
> @@ -187,12 +187,10 @@ impl<T: IntoGEMObject> BaseObject for T {}
>  /// Invariants
>  ///
>  /// - `self.obj` is a valid instance of a `struct drm_gem_object`.
> -/// - `self.dev` is always a valid pointer to a `struct drm_device`.
>  #[repr(C)]
>  #[pin_data]
>  pub struct Object<T: DriverObject + Send + Sync> {
>      obj: Opaque<bindings::drm_gem_object>,
> -    dev: NonNull<drm::Device<T::Driver>>,
>      #[pin]
>      data: T,
>  }
> @@ -222,9 +220,6 @@ pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {
>              try_pin_init!(Self {
>                  obj: Opaque::new(bindings::drm_gem_object::default()),
>                  data <- T::new(dev, size),
> -                // INVARIANT: The drm subsystem guarantees that the `struct drm_device` will live
> -                // as long as the GEM object lives.
> -                dev: dev.into(),
>              }),
>              GFP_KERNEL,
>          )?;
> @@ -247,9 +242,9 @@ pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {
>  
>      /// Returns the `Device` that owns this GEM object.
>      pub fn dev(&self) -> &drm::Device<T::Driver> {
> -        // SAFETY: The DRM subsystem guarantees that the `struct drm_device` will live as long as
> -        // the GEM object lives, hence the pointer must be valid.
> -        unsafe { self.dev.as_ref() }
> +        // SAFETY: `struct drm_gem_object.dev` is initialized and valid for as long as the GEM
> +        // object lives.
> +        unsafe { drm::Device::from_raw((*self.as_raw()).dev) }

The wording here and in the invariants could be improved to say that
T::Driver is the right choice for the generic parameter.

Alice
Re: [PATCH] rust: drm/gem: Remove Object.dev
Posted by Danilo Krummrich 3 months, 3 weeks ago
On 10/16/25 10:18 PM, Lyude Paul wrote:
> I noticed by chance that there's actually already a pointer to this in
> struct drm_gem_object. So, no use in carrying this around!
> 
> Signed-off-by: Lyude Paul <lyude@redhat.com>

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