diff mbox series

[RFC,2/9] rust: drm: gem: add method to return DmaResv from GEMObject

Message ID 20230317121213.93991-3-mcanal@igalia.com (mailing list archive)
State New, archived
Headers show
Series Rust version of the VGEM driver | expand

Commit Message

Maíra Canal March 17, 2023, 12:12 p.m. UTC
A GEMObject, related to struct drm_gem_object, holds a pointer
to reservation object associated with the this GEM object. Therefore,
expose this reservation object through the DmaResv safe abstraction.

Signed-off-by: Maíra Canal <mcanal@igalia.com>
---
 rust/kernel/drm/gem/mod.rs | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Björn Roy Baron March 22, 2023, 7:45 p.m. UTC | #1
On Friday, March 17th, 2023 at 13:12, Maíra Canal <mcanal@igalia.com> wrote:

> A GEMObject, related to struct drm_gem_object, holds a pointer
> to reservation object associated with the this GEM object. Therefore,
> expose this reservation object through the DmaResv safe abstraction.
> 
> Signed-off-by: Maíra Canal <mcanal@igalia.com>
> ---
>  rust/kernel/drm/gem/mod.rs | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
> index c4a42bb2f718..dae95e5748a7 100644
> --- a/rust/kernel/drm/gem/mod.rs
> +++ b/rust/kernel/drm/gem/mod.rs
> @@ -11,6 +11,7 @@ use alloc::boxed::Box;
> 
>  use crate::{
>      bindings,
> +    dma_resv::DmaResv,
>      drm::{device, drv, file},
>      error::{to_result, Result},
>      prelude::*,
> @@ -136,6 +137,12 @@ pub trait BaseObject: IntoGEMObject {
>          self.gem_obj().size
>      }
> 
> +    /// Returns the pointer to reservation object associated with this GEM object.
> +    fn resv(&self) -> DmaResv {
> +        // SAFETY: Every GEM object holds a reference to a reservation object
> +        unsafe { DmaResv::from_raw(self.gem_obj().resv) }
> +    }

There is nothing ensuring that DmaResv doesn't outlive self and thus may have been deallocated. You could change DmaResv to be a newtype around UnsafeCell<dma_resv> and then return an &DmaResv here. This way &DmaResv can't outlive &self.

> +
>      /// Sets the exportable flag, which controls whether the object can be exported via PRIME.
>      fn set_exportable(&mut self, exportable: bool) {
>          self.mut_gem_obj().exportable = exportable;
> --
> 2.39.2

Cheers,
Bjorn
Björn Roy Baron March 22, 2023, 7:57 p.m. UTC | #2
On Wednesday, March 22nd, 2023 at 20:45, Björn Roy Baron <bjorn3_gh@protonmail.com> wrote:

> On Friday, March 17th, 2023 at 13:12, Maíra Canal <mcanal@igalia.com> wrote:
> 
> > A GEMObject, related to struct drm_gem_object, holds a pointer
> > to reservation object associated with the this GEM object. Therefore,
> > expose this reservation object through the DmaResv safe abstraction.
> >
> > Signed-off-by: Maíra Canal <mcanal@igalia.com>
> > ---
> >  rust/kernel/drm/gem/mod.rs | 7 +++++++
> >  1 file changed, 7 insertions(+)
> >
> > diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
> > index c4a42bb2f718..dae95e5748a7 100644
> > --- a/rust/kernel/drm/gem/mod.rs
> > +++ b/rust/kernel/drm/gem/mod.rs
> > @@ -11,6 +11,7 @@ use alloc::boxed::Box;
> >
> >  use crate::{
> >      bindings,
> > +    dma_resv::DmaResv,
> >      drm::{device, drv, file},
> >      error::{to_result, Result},
> >      prelude::*,
> > @@ -136,6 +137,12 @@ pub trait BaseObject: IntoGEMObject {
> >          self.gem_obj().size
> >      }
> >
> > +    /// Returns the pointer to reservation object associated with this GEM object.
> > +    fn resv(&self) -> DmaResv {
> > +        // SAFETY: Every GEM object holds a reference to a reservation object
> > +        unsafe { DmaResv::from_raw(self.gem_obj().resv) }
> > +    }
> 
> There is nothing ensuring that DmaResv doesn't outlive self and thus may have been deallocated. You could change DmaResv to be a newtype around UnsafeCell<dma_resv> and then return an &DmaResv here. This way &DmaResv can't outlive &self.

Actually Opaque<dma_resv> may be better as no fields of dma_resv are used on the rust side.

> 
> > +
> >      /// Sets the exportable flag, which controls whether the object can be exported via PRIME.
> >      fn set_exportable(&mut self, exportable: bool) {
> >          self.mut_gem_obj().exportable = exportable;
> > --
> > 2.39.2
> 
> Cheers,
> Bjorn

Cheers,
Bjorn
diff mbox series

Patch

diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index c4a42bb2f718..dae95e5748a7 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -11,6 +11,7 @@  use alloc::boxed::Box;
 
 use crate::{
     bindings,
+    dma_resv::DmaResv,
     drm::{device, drv, file},
     error::{to_result, Result},
     prelude::*,
@@ -136,6 +137,12 @@  pub trait BaseObject: IntoGEMObject {
         self.gem_obj().size
     }
 
+    /// Returns the pointer to reservation object associated with this GEM object.
+    fn resv(&self) -> DmaResv {
+        // SAFETY: Every GEM object holds a reference to a reservation object
+        unsafe { DmaResv::from_raw(self.gem_obj().resv) }
+    }
+
     /// Sets the exportable flag, which controls whether the object can be exported via PRIME.
     fn set_exportable(&mut self, exportable: bool) {
         self.mut_gem_obj().exportable = exportable;