[PATCH v3 06/14] rust: drm: gem: Add raw_dma_resv() function

Lyude Paul posted 14 patches 1 month ago
[PATCH v3 06/14] rust: drm: gem: Add raw_dma_resv() function
Posted by Lyude Paul 1 month ago
For retrieving a pointer to the struct dma_resv for a given GEM object. We
also introduce it in a new trait, BaseObjectPrivate, which we automatically
implement for all gem objects and don't expose to users outside of the
crate.

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

diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index ec36cd9ea69ed..f901d4263ee87 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -186,6 +186,18 @@ fn create_mmap_offset(&self) -> Result<u64> {
 
 impl<T: IntoGEMObject> BaseObject for T {}
 
+/// Crate-private base operations shared by all GEM object classes.
+#[expect(unused)]
+pub(crate) trait BaseObjectPrivate: IntoGEMObject {
+    /// Return a pointer to this object's dma_resv.
+    fn raw_dma_resv(&self) -> *mut bindings::dma_resv {
+        // SAFETY: `as_gem_obj()` always returns a valid pointer to the base DRM gem object
+        unsafe { (*self.as_raw()).resv }
+    }
+}
+
+impl<T: IntoGEMObject> BaseObjectPrivate for T {}
+
 /// A base GEM object.
 ///
 /// Invariants
-- 
2.50.0
Re: [PATCH v3 06/14] rust: drm: gem: Add raw_dma_resv() function
Posted by Alice Ryhl 4 weeks, 1 day ago
On Sat, Aug 30, 2025 at 12:42 AM Lyude Paul <lyude@redhat.com> wrote:
>
> For retrieving a pointer to the struct dma_resv for a given GEM object. We
> also introduce it in a new trait, BaseObjectPrivate, which we automatically
> implement for all gem objects and don't expose to users outside of the
> crate.
>
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
>  rust/kernel/drm/gem/mod.rs | 12 ++++++++++++
>  1 file changed, 12 insertions(+)
>
> diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
> index ec36cd9ea69ed..f901d4263ee87 100644
> --- a/rust/kernel/drm/gem/mod.rs
> +++ b/rust/kernel/drm/gem/mod.rs
> @@ -186,6 +186,18 @@ fn create_mmap_offset(&self) -> Result<u64> {
>
>  impl<T: IntoGEMObject> BaseObject for T {}
>
> +/// Crate-private base operations shared by all GEM object classes.
> +#[expect(unused)]
> +pub(crate) trait BaseObjectPrivate: IntoGEMObject {
> +    /// Return a pointer to this object's dma_resv.
> +    fn raw_dma_resv(&self) -> *mut bindings::dma_resv {
> +        // SAFETY: `as_gem_obj()` always returns a valid pointer to the base DRM gem object
> +        unsafe { (*self.as_raw()).resv }
> +    }
> +}
> +
> +impl<T: IntoGEMObject> BaseObjectPrivate for T {}

I think this use of a trait is somewhat of an antipattern. I would
probably have suggested a standalone method instead.

Alice
Re: [PATCH v3 06/14] rust: drm: gem: Add raw_dma_resv() function
Posted by Daniel Almeida 4 weeks, 1 day ago

> On 29 Aug 2025, at 19:35, Lyude Paul <lyude@redhat.com> wrote:
> 
> For retrieving a pointer to the struct dma_resv for a given GEM object. We
> also introduce it in a new trait, BaseObjectPrivate, which we automatically
> implement for all gem objects and don't expose to users outside of the
> crate.
> 
> Signed-off-by: Lyude Paul <lyude@redhat.com>
> ---
> rust/kernel/drm/gem/mod.rs | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
> 
> diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
> index ec36cd9ea69ed..f901d4263ee87 100644
> --- a/rust/kernel/drm/gem/mod.rs
> +++ b/rust/kernel/drm/gem/mod.rs
> @@ -186,6 +186,18 @@ fn create_mmap_offset(&self) -> Result<u64> {
> 
> impl<T: IntoGEMObject> BaseObject for T {}
> 
> +/// Crate-private base operations shared by all GEM object classes.
> +#[expect(unused)]
> +pub(crate) trait BaseObjectPrivate: IntoGEMObject {
> +    /// Return a pointer to this object's dma_resv.
> +    fn raw_dma_resv(&self) -> *mut bindings::dma_resv {
> +        // SAFETY: `as_gem_obj()` always returns a valid pointer to the base DRM gem object

This apparently does not match the actual function call below anymore.

> +        unsafe { (*self.as_raw()).resv }
> +    }
> +}
> +
> +impl<T: IntoGEMObject> BaseObjectPrivate for T {}
> +
> /// A base GEM object.
> ///
> /// Invariants
> -- 
> 2.50.0
>