This adds support for simple usage of drm_gpuvm_exec() for locking all of the dma reservations and adding fences to them. For more complex usage that does not involve locking all reservations, it is expected that the ww-mutex abstractions will be used instead.
Signed-off-by: Alice Ryhl <[email protected]> --- rust/bindings/bindings_helper.h | 2 ++ rust/helpers/drm_gpuvm.c | 6 ++++ rust/kernel/dma_buf/mod.rs | 17 ++++++++++ rust/kernel/drm/gpuvm/mod.rs | 71 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 1124785e210b..3978682a6f2e 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -151,6 +151,8 @@ const vm_flags_t RUST_CONST_HELPER_VM_MIXEDMAP = VM_MIXEDMAP; const vm_flags_t RUST_CONST_HELPER_VM_HUGEPAGE = VM_HUGEPAGE; const vm_flags_t RUST_CONST_HELPER_VM_NOHUGEPAGE = VM_NOHUGEPAGE; +const u32 RUST_CONST_HELPER_DRM_EXEC_INTERRUPTIBLE_WAIT = DRM_EXEC_INTERRUPTIBLE_WAIT; + #if IS_ENABLED(CONFIG_GPU_BUDDY) const unsigned long RUST_CONST_HELPER_GPU_BUDDY_RANGE_ALLOCATION = GPU_BUDDY_RANGE_ALLOCATION; const unsigned long RUST_CONST_HELPER_GPU_BUDDY_TOPDOWN_ALLOCATION = GPU_BUDDY_TOPDOWN_ALLOCATION; diff --git a/rust/helpers/drm_gpuvm.c b/rust/helpers/drm_gpuvm.c index 4130b6325213..30fb1777fbf2 100644 --- a/rust/helpers/drm_gpuvm.c +++ b/rust/helpers/drm_gpuvm.c @@ -23,4 +23,10 @@ bool rust_helper_drm_gpuvm_is_extobj(struct drm_gpuvm *gpuvm, return drm_gpuvm_is_extobj(gpuvm, obj); } +__rust_helper +void rust_helper_drm_gpuvm_exec_unlock(struct drm_gpuvm_exec *vm_exec) +{ + drm_gpuvm_exec_unlock(vm_exec); +} + #endif // CONFIG_RUST_DRM_GPUVM diff --git a/rust/kernel/dma_buf/mod.rs b/rust/kernel/dma_buf/mod.rs index b66e747c35ad..234936ba8265 100644 --- a/rust/kernel/dma_buf/mod.rs +++ b/rust/kernel/dma_buf/mod.rs @@ -5,3 +5,20 @@ pub mod dma_fence; pub use self::dma_fence::Fence; + +/// How the fences from a `dma_resv` obj are used. +/// +/// Please see [the C-side documentation][dma_resv_usage] for more details. +/// +/// [dma_resv_usage]: https://docs.kernel.org/driver-api/dma-buf.html#c.dma_resv_usage +#[repr(u32)] +pub enum DmaResvUsage { + /// For in kernel memory management only (e.g. copying, clearing memory). + Kernel = bindings::dma_resv_usage_DMA_RESV_USAGE_KERNEL, + /// Implicit write synchronization for userspace submissions. + Write = bindings::dma_resv_usage_DMA_RESV_USAGE_WRITE, + /// Implicit read synchronization for userspace submissions. + Read = bindings::dma_resv_usage_DMA_RESV_USAGE_READ, + /// No implicit sync (e.g. preemption fences, page table updates, TLB flushes). + Bookkeep = bindings::dma_resv_usage_DMA_RESV_USAGE_BOOKKEEP, +} diff --git a/rust/kernel/drm/gpuvm/mod.rs b/rust/kernel/drm/gpuvm/mod.rs index ae58f6f667c1..a52fb25f22ff 100644 --- a/rust/kernel/drm/gpuvm/mod.rs +++ b/rust/kernel/drm/gpuvm/mod.rs @@ -16,6 +16,7 @@ Flags as AllocFlags, // }, bindings, + dma_buf::{DmaResvUsage, Fence}, drm, drm::gem::IntoGEMObject, error::to_result, @@ -216,6 +217,30 @@ pub fn obtain( Ok(GpuVmBoAlloc::new(self, obj, data)?.obtain()) } + /// Prepare this GPUVM. + /// + /// The parameter indicates how many fences to preallocate space for. + #[inline] + pub fn prepare(&self, num_fences: u32) -> impl PinInit<GpuVmExec<'_, T>, Error> { + try_pin_init!(GpuVmExec { + exec <- Opaque::try_ffi_init(|exec: *mut bindings::drm_gpuvm_exec| { + // SAFETY: exec is valid but unused memory, so we can write. + unsafe { + ptr::write_bytes(exec, 0u8, 1usize); + ptr::write(&raw mut (*exec).vm, self.as_raw()); + ptr::write(&raw mut (*exec).flags, bindings::DRM_EXEC_INTERRUPTIBLE_WAIT); + ptr::write(&raw mut (*exec).num_fences, num_fences); + } + + // SAFETY: If successful, this `struct drm_gpuvm_exec` remains valid until this is + // unlocked because it's pinned and unlocks it in drop. The contained pointer to + // `self` remains valid because `GpuVmExec` borrows from `self`. + to_result(unsafe { bindings::drm_gpuvm_exec_lock(exec) }) + }), + _gpuvm: PhantomData, + }) + } + /// Clean up buffer objects that are no longer used. #[inline] pub fn deferred_cleanup(&self) { @@ -326,3 +351,49 @@ fn deref(&self) -> &GpuVm<T> { &self.0 } } + +/// The exec token for preparing the objects. +/// +/// # Invariants +/// +/// Owns a GPUVM exec context. +#[pin_data(PinnedDrop)] +pub struct GpuVmExec<'vm, T: DriverGpuVm> { + #[pin] + exec: Opaque<bindings::drm_gpuvm_exec>, + _gpuvm: PhantomData<&'vm mut GpuVm<T>>, +} + +impl<'vm, T: DriverGpuVm> GpuVmExec<'vm, T> { + /// Add a fence. + /// + /// The caller must ensure that the preallocated space for fences is sufficient. + #[inline] + pub fn resv_add_fence( + &self, + fence: &Fence, + private_usage: DmaResvUsage, + extobj_usage: DmaResvUsage, + ) { + // SAFETY: This method takes a refcount on the fence, so it's only required to be valid for + // the duration of this method. If there is not enough space for the pre-allocated fence, + // then this leads to a BUG_ON() but does not trigger UB. + unsafe { + bindings::drm_gpuvm_resv_add_fence( + (*self.exec.get()).vm, + &raw mut (*self.exec.get()).exec, + fence.as_raw(), + private_usage as u32, + extobj_usage as u32, + ) + } + } +} + +#[pinned_drop] +impl<'vm, T: DriverGpuVm> PinnedDrop for GpuVmExec<'vm, T> { + fn drop(self: Pin<&mut Self>) { + // SAFETY: We hold the lock, so it's safe to unlock. + unsafe { bindings::drm_gpuvm_exec_unlock(self.exec.get()) }; + } +} -- 2.54.0.545.g6539524ca2-goog
