Skip to content

Commit cc8e784

Browse files
Lyudejannau
authored andcommitted
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@redhat.com>
1 parent 95c0537 commit cc8e784

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

rust/kernel/drm/gem/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
bindings, drm,
1212
drm::driver::{AllocImpl, AllocOps},
1313
dma_buf,
14-
error::{to_result, Result},
14+
error::{to_result, from_err_ptr, Result},
1515
prelude::*,
1616
sync::aref::{ARef, AlwaysRefCounted},
1717
types::Opaque,
@@ -219,6 +219,29 @@ pub trait BaseObject: IntoGEMObject {
219219
Ok(unsafe { ARef::from_raw(obj.into()) })
220220
}
221221

222+
/// Export a [`DmaBuf`] for this GEM object using the DRM prime helper library.
223+
///
224+
/// `flags` should be a set of flags from [`fs::file::flags`](kernel::fs::file::flags).
225+
fn prime_export(&self, flags: u32) -> Result<DmaBuf<Self>> {
226+
// SAFETY:
227+
// - `as_raw()` always returns a valid pointer to a `drm_gem_object`.
228+
// - `drm_gem_prime_export()` returns either an error pointer, or a valid pointer to an
229+
// initialized `dma_buf` on success.
230+
let dma_ptr = from_err_ptr(unsafe {
231+
bindings::drm_gem_prime_export(self.as_raw(), flags as _)
232+
})?;
233+
234+
// SAFETY:
235+
// - We checked that dma_ptr is not an error, so it must point to an initialized dma_buf
236+
// - We used drm_gem_prime_export(), so `dma_ptr` will remain valid until a call to
237+
// `drm_gem_prime_release()` which we don't call here.
238+
let dma_buf = unsafe { dma_buf::DmaBuf::as_ref(dma_ptr) };
239+
240+
// INVARIANT: We used drm_gem_prime_export() to create this dma_buf, fulfilling the
241+
// invariant that this dma_buf came from a GEM object of type `Self`.
242+
Ok(DmaBuf(dma_buf.into(), PhantomData))
243+
}
244+
222245
/// Creates an mmap offset to map the object from userspace.
223246
fn create_mmap_offset(&self) -> Result<u64> {
224247
// SAFETY: The arguments are valid per the type invariant.

0 commit comments

Comments
 (0)