[PATCH 1/4] rust: drm: gem: Use NonNull for Object::dev

Lyude Paul posted 4 patches 9 months, 1 week ago
There is a newer version of this series
[PATCH 1/4] rust: drm: gem: Use NonNull for Object::dev
Posted by Lyude Paul 9 months, 1 week ago
There is usually not much of a reason to use a raw pointer in a data
struct, so move this to NonNull instead.

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

diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index 0cafa4a424206..df8f9fdae5c22 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -177,7 +177,7 @@ impl<T> BaseObject for T where Self: crate::types::AlwaysRefCounted + IntoGEMObj
 #[pin_data]
 pub struct Object<T: DriverObject + Send + Sync> {
     obj: Opaque<bindings::drm_gem_object>,
-    dev: *const drm::Device<T::Driver>,
+    dev: NonNull<drm::Device<T::Driver>>,
     #[pin]
     data: T,
 }
@@ -212,7 +212,7 @@ pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {
                 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: dev.into(),
             }),
             GFP_KERNEL,
         )?;
@@ -237,7 +237,7 @@ pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {
     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 }
+        unsafe { self.dev.as_ref() }
     }
 
     fn as_raw(&self) -> *mut bindings::drm_gem_object {
-- 
2.48.1
Re: [PATCH 1/4] rust: drm: gem: Use NonNull for Object::dev
Posted by Danilo Krummrich 9 months ago
On Thu, May 01, 2025 at 02:33:16PM -0400, Lyude Paul wrote:
> There is usually not much of a reason to use a raw pointer in a data
> struct, so move this to NonNull instead.
> 
> Signed-off-by: Lyude Paul <lyude@redhat.com>

Reviewed-by: Danilo Krummrich <dakr@kernel.org>
Re: [PATCH 1/4] rust: drm: gem: Use NonNull for Object::dev
Posted by Daniel Almeida 9 months ago
Hi Lyude,

> On 1 May 2025, at 15:33, Lyude Paul <lyude@redhat.com> wrote:
> 
> There is usually not much of a reason to use a raw pointer in a data
> struct, so move this to NonNull instead.
> 
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
> rust/kernel/drm/gem/mod.rs | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
> index 0cafa4a424206..df8f9fdae5c22 100644
> --- a/rust/kernel/drm/gem/mod.rs
> +++ b/rust/kernel/drm/gem/mod.rs
> @@ -177,7 +177,7 @@ impl<T> BaseObject for T where Self: crate::types::AlwaysRefCounted + IntoGEMObj
> #[pin_data]
> pub struct Object<T: DriverObject + Send + Sync> {
>     obj: Opaque<bindings::drm_gem_object>,
> -    dev: *const drm::Device<T::Driver>,
> +    dev: NonNull<drm::Device<T::Driver>>,
>     #[pin]
>     data: T,
> }
> @@ -212,7 +212,7 @@ pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {
>                 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: dev.into(),
>             }),
>             GFP_KERNEL,
>         )?;
> @@ -237,7 +237,7 @@ pub fn new(dev: &drm::Device<T::Driver>, size: usize) -> Result<ARef<Self>> {
>     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 }
> +        unsafe { self.dev.as_ref() }
>     }
> 
>     fn as_raw(&self) -> *mut bindings::drm_gem_object {
> -- 
> 2.48.1
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>