[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250521204654.1610607-13-lyude@redhat.com>
Date: Wed, 21 May 2025 16:29:19 -0400
From: Lyude Paul <lyude@...hat.com>
To: dri-devel@...ts.freedesktop.org,
rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Cc: David Airlie <airlied@...il.com>,
Simona Vetter <simona@...ll.ch>,
Maarten Lankhorst <maarten.lankhorst@...ux.intel.com>,
Maxime Ripard <mripard@...nel.org>,
Thomas Zimmermann <tzimmermann@...e.de>,
Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Boqun Feng <boqun.feng@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>,
Alice Ryhl <aliceryhl@...gle.com>,
Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>,
Sumit Semwal <sumit.semwal@...aro.org>,
Christian König <christian.koenig@....com>,
Daniel Almeida <daniel.almeida@...labora.com>,
Asahi Lina <lina@...hilina.net>,
Alyssa Rosenzweig <alyssa@...enzweig.io>,
linux-media@...r.kernel.org (open list:DMA BUFFER SHARING FRAMEWORK:Keyword:\bdma_(?:buf|fence|resv)\b),
linaro-mm-sig@...ts.linaro.org (moderated list:DMA BUFFER SHARING FRAMEWORK:Keyword:\bdma_(?:buf|fence|resv)\b)
Subject: [PATCH v2 12/12] rust: drm: gem: Add BaseObject::prime_export()
We just added an export() callback that GEM objects can implement, but
without any way of actually exporting a DmaBuf<T>. So let's add one by
introducing bindings for drm_gem_prime_export().
Signed-off-by: Lyude Paul <lyude@...hat.com>
---
rust/kernel/drm/gem/mod.rs | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/rust/kernel/drm/gem/mod.rs b/rust/kernel/drm/gem/mod.rs
index ef697f323e52c..ee74b9bdb7a1f 100644
--- a/rust/kernel/drm/gem/mod.rs
+++ b/rust/kernel/drm/gem/mod.rs
@@ -225,6 +225,29 @@ fn lookup_handle<D, F, O>(file: &drm::File<F>, handle: u32) -> Result<ARef<Self>
Ok(unsafe { ARef::from_raw(obj.into()) })
}
+ /// Export a [`DmaBuf`] for this GEM object using the DRM prime helper library.
+ ///
+ /// `flags` should be a set of flags from [`fs::file::flags`](kernel::fs::file::flags).
+ fn prime_export(&self, flags: u32) -> Result<DmaBuf<Self>> {
+ // SAFETY:
+ // - `as_raw()` always returns a valid pointer to a `drm_gem_object`.
+ // - `drm_gem_prime_export()` returns either an error pointer, or a valid pointer to an
+ // initialized `dma_buf` on success.
+ let dma_ptr = from_err_ptr(unsafe {
+ bindings::drm_gem_prime_export(self.as_raw(), flags as _)
+ })?;
+
+ // SAFETY:
+ // - We checked that dma_ptr is not an error, so it must point to an initialized dma_buf
+ // - We used drm_gem_prime_export(), so `dma_ptr` will remain valid until a call to
+ // `drm_gem_prime_release()` which we don't call here.
+ let dma_buf = unsafe { dma_buf::DmaBuf::as_ref(dma_ptr) };
+
+ // INVARIANT: We used drm_gem_prime_export() to create this dma_buf, fulfilling the
+ // invariant that this dma_buf came from a GEM object of type `Self`.
+ Ok(DmaBuf(dma_buf.into(), PhantomData))
+ }
+
/// Creates an mmap offset to map the object from userspace.
fn create_mmap_offset(&self) -> Result<u64> {
// SAFETY: The arguments are valid per the type invariant.
--
2.49.0
Powered by blists - more mailing lists