[PATCH v2 12/12] rust: drm: gem: Add BaseObject::prime_export()
Lyude Paul
lyude at redhat.com
Wed May 21 20:29:19 UTC 2025
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 at redhat.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
More information about the dri-devel
mailing list