Rust GPUVM immediate mode
Subsystems (6)
Alice Ryhl
maintainerv5 cover Rust GPUVM immediate mode
This series provides an immediate mode GPUVM implementation.
Only immediate mode is provided for Rust code, as all planned Rust drivers intend to use GPUVM in immediate mode.
---
Changes in v5:
- Rename GpuVmCore to UniqueRefGpuVm.
- Implement Send/Sync for GpuVm.
- Move helpers/dma-resv.c in MAINTAINERS
- Improve GpuVmBo allocation and free docs
- Add "in GEM list" invariant to GpuVmBo
- also mention said invariant on GpuVa
- Replace GpuVmBoRegistered with ARef<GpuVmBo>
- Add TODO about dma-resv lock usage
- Add __rust_helper to helpers/drm_gpuvm.c
- Change _invariant type to silence type complexity clippy lint
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Add trait bound so that DriverGpuVm::Object must be the same type as Driver::Object.
- Add rust/helpers/dma-resv.c to MAINTAINERS.
- Add __rust_helper to dma_resv_lock and dma_resv_unlock.
- Rename GpuVm::raw_resv_lock() to GpuVm::raw_resv()
- Reword comment in obtain() for adding to extobj list.
- Fix typo in commit message referring to GpuVm<_> instead of GpuVmBo<_>.
- Changed the PhantomData type used for invariance and add comment. (No functional change.)
- Rename offset to gem_offset in args to sm_map().
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- C prerequisites have landed, so only Rust part is present.
- The logic for drm_exec was removed, and is for a follow-up.
- Split up into patches.
- Add lifetime to SmStepContext.
- Docs filled out.
- Mutex abstractions used for GEM gpuva lock.
- Drop 'shared data' concept for now. (Can be added back later if required.)
- Rename 'core' field to 'data'.
- GpuVmCore<T> now derefs to GpuVm<T> instead of T.
- Renamed GpuVmBoObtain to GpuVmBoResident.
- Probably more changes I forgot.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- For this version, only the C prerequisites are included. Rust will be sent as follow-up.
- Add comment to drm_gpuvm_bo_destroy_not_in_lists()
- Add Fixes: tag.
- Pick up Reviewed-by tags.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Alice Ryhl (4):
rust: gpuvm: add GpuVm::obtain()
rust: gpuvm: add GpuVa struct
rust: gpuvm: add GpuVmCore::sm_unmap()
rust: gpuvm: add GpuVmCore::sm_map()
Asahi Lina (2):
rust: drm: add base GPUVM immediate mode abstraction
rust: helpers: Add bindings/wrappers for dma_resv_lock
MAINTAINERS | 3 + rust/bindings/bindings_helper.h | 2 + rust/helpers/dma-resv.c | 14 ++ rust/helpers/drm_gpuvm.c | 26 +++ rust/helpers/helpers.c | 2 + rust/kernel/drm/gpuvm/mod.rs | 306 +++++++++++++++++++++++++++++ rust/kernel/drm/gpuvm/sm_ops.rs | 417 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/drm/gpuvm/va.rs | 150 +++++++++++++++ rust/kernel/drm/gpuvm/vm_bo.rs | 234 ++++++++++++++++++++++ rust/kernel/drm/mod.rs | 1 + 10 files changed, 1155 insertions(+)
---
base-commit: a19457958c3018783881c4416f272cd594f13049
change-id: 20251128-gpuvm-rust-b719cac27ad6
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Alice Ryhl
maintainerPATCH v5 1/6 rust: drm: add base GPUVM immediate mode abstraction
From: Asahi Lina <[email protected]>
Add a GPUVM abstraction to be used by Rust GPU drivers.
GPUVM keeps track of a GPU's virtual address (VA) space and manages the corresponding virtual mappings represented by "GPU VA" objects. It also keeps track of the gem::Object<T> used to back the mappings through GpuVmBo<T>.
This abstraction is only usable by drivers that wish to use GPUVM in immediate mode. This allows us to build the locking scheme into the API design. It means that the GEM mutex is used for the GEM gpuva list, and that the resv lock is used for the extobj list. The evicted list is not yet used in this version.
This abstraction provides a special handle called the UniqueRefGpuVm, which is a wrapper around ARef<GpuVm> that provides access to the interval tree. Generally, all changes to the address space requires mutable access to this unique handle.
--- MAINTAINERS | 2 + rust/bindings/bindings_helper.h | 1 + rust/helpers/drm_gpuvm.c | 20 ++++ rust/helpers/helpers.c | 1 + rust/kernel/drm/gpuvm/mod.rs | 238 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/drm/mod.rs | 1 + 6 files changed, 263 insertions(+)
MAINTAINERS +2 -0
@@ -8840,6 +8840,8 @@ S: Supported T: git https://gitlab.freedesktop.org/drm/misc/kernel.git F: drivers/gpu/drm/drm_gpuvm.c F: include/drm/drm_gpuvm.h +F: rust/helpers/drm_gpuvm.c +F: rust/kernel/drm/gpuvm/ DRM LOG M: Jocelyn Falempe <[email protected]>
rust/bindings/bindings_helper.h +1 -0
@@ -33,6 +33,7 @@#include <drm/drm_drv.h> #include <drm/drm_file.h> #include <drm/drm_gem.h> +#include <drm/drm_gpuvm.h> #include <drm/drm_ioctl.h> #include <kunit/test.h> #include <linux/auxiliary_bus.h>
rust/helpers/drm_gpuvm.c new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 or MIT + +#ifdef CONFIG_DRM_GPUVM + +#include <drm/drm_gpuvm.h> + +__rust_helper +struct drm_gpuvm *rust_helper_drm_gpuvm_get(struct drm_gpuvm *obj) +{ +return drm_gpuvm_get (obj); +} + +__rust_helper +bool rust_helper_drm_gpuvm_is_extobj(struct drm_gpuvm *gpuvm, +struct drm_gem_object *obj) +{ +return drm_gpuvm_is_extobj (gpuvm, obj); +} + +#endif // CONFIG_DRM_GPUVM
rust/helpers/helpers.c +1 -0
@@ -29,6 +29,7 @@#include "device.c" #include "dma.c" #include "drm.c" +#include "drm_gpuvm.c" #include "err.c" #include "irq.c" #include "fs.c"
rust/kernel/drm/gpuvm/mod.rs new file +238 -0
238 lines not on this page · raw message
rust/kernel/drm/mod.rs +1 -0
@@ -6,6 +6,7 @@pub mod driver;pub mod file;pub mod gem; +pub mod gpuvm;pub mod ioctl;pub use self::device::Device ;
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
Alice Ryhl
maintainerPATCH v5 2/6 rust: helpers: Add bindings/wrappers for dma_resv_lock
In reply to Alice RyhlFrom: Asahi Lina <[email protected]>
This is just for basic usage in the DRM shmem abstractions for implied locking, not intended as a full DMA Reservation abstraction yet.
---
Taken from:
https://lore.kernel.org/all/[email protected]/
with __rust_helper and MAINTAINERS file added.
--- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/helpers/dma-resv.c | 14 ++++++++++++++ rust/helpers/helpers.c | 1 + 4 files changed, 17 insertions(+)
MAINTAINERS +1 -0
@@ -7534,6 +7534,7 @@ F: include/linux/*fence.h F: include/linux/dma-buf.h F: include/linux/dma-buf/ F: include/linux/dma-resv.h +F: rust/helpers/dma-resv.c K: \bdma_(?:buf|fence|resv)\b DMA GENERIC OFFLOAD ENGINE SUBSYSTEM
rust/bindings/bindings_helper.h +1 -0
@@ -52,6 +52,7 @@#include <linux/device/faux.h> #include <linux/dma-direction.h> #include <linux/dma-mapping.h> +#include <linux/dma-resv.h> #include <linux/errname.h> #include <linux/ethtool.h> #include <linux/fdtable.h>
rust/helpers/dma-resv.c new file +14 -0
@@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/dma-resv.h> + +__rust_helper +int rust_helper_dma_resv_lock(struct dma_resv *obj, struct ww_acquire_ctx *ctx) +{ +return dma_resv_lock (obj, ctx); +} + +__rust_helper void rust_helper_dma_resv_unlock(struct dma_resv *obj) +{ +dma_resv_unlock (obj); +}
rust/helpers/helpers.c +1 -0
@@ -28,6 +28,7 @@#include "cred.c" #include "device.c" #include "dma.c" +#include "dma-resv.c" #include "drm.c" #include "drm_gpuvm.c" #include "err.c"
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
Alice Ryhl
maintainerPATCH v5 3/6 rust: gpuvm: add GpuVm::obtain()
In reply to Alice RyhlThis provides a mechanism to create (or look up) VMBO instances, which represent the mapping between GPUVM and GEM objects.
The GpuVmBoRegistered<T> type can be considered like ARef<GpuVm<T>>, except that no way to increment the refcount is provided.
The GpuVmBoAlloc<T> type is more akin to a pre-allocated GpuVmBo<T>, so it's not really a GpuVmBo<T> yet. Its destructor could call drm_gpuvm_bo_destroy_not_in_lists(), but as the type is currently private and never called anywhere, this perf optimization does not need to happen now.
Pre-allocating and obtaining the gpuvm_bo object is exposed as a single step. This could theoretically be a problem if one wanted to call drm_gpuvm_bo_obtain_prealloc() during the fence signalling critical path, but that's not a possibility because:
1. Adding the BO to the extobj list requires the resv lock, so it cannot happen during the fence signalling critical path.
2. obtain() requires that the BO is not in the extobj list, so obtain() must be called before adding the BO to the extobj list.
Thus, drm_gpuvm_bo_obtain_prealloc() cannot be called during the fence signalling critical path. (For extobjs at least.)
--- rust/helpers/drm_gpuvm.c | 6 ++ rust/kernel/drm/gpuvm/mod.rs | 32 +++++- rust/kernel/drm/gpuvm/vm_bo.rs | 227 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 262 insertions(+), 3 deletions(-)
rust/helpers/drm_gpuvm.c +6 -0
@@ -4,6 +4,12 @@#include <drm/drm_gpuvm.h> +__rust_helper +struct drm_gpuvm_bo *rust_helper_drm_gpuvm_bo_get(struct drm_gpuvm_bo *vm_bo) +{ +return drm_gpuvm_bo_get (vm_bo); +} +__rust_helper struct drm_gpuvm *rust_helper_drm_gpuvm_get(struct drm_gpuvm *obj) {
rust/kernel/drm/gpuvm/mod.rs +29 -3
@@ -25,13 +25,20 @@use core::{ cell::UnsafeCell , + mem::ManuallyDrop , ops::{Deref ,Range ,// }, - ptr::NonNull ,// + ptr::{ + self, +NonNull ,// + },// }; +mod vm_bo; +pub use self::vm_bo::*; +/// A DRM GPU VA manager. /// /// This object is refcounted, but the locations of mapped ranges may only be accessed or changed
@@ -73,8 +80,8 @@ const fn vtable() -> &'static bindings::drm_gpuvm_ops { vm_free: Some(Self::vm_free), op_alloc: None, op_free: None, - vm_bo_alloc: None, - vm_bo_free: None, + vm_bo_alloc: GpuVmBo::<T>::ALLOC_FN, + vm_bo_free: GpuVmBo::<T>::FREE_FN, vm_bo_validate: None, sm_step_map: None, sm_step_unmap: None,
@@ -173,6 +180,16 @@ pub fn va_range(&self) -> Range<u64> {Range { start, end } } +/// Get or create the [`GpuVmBo`] for this gem object. +#[inline] +pub fn obtain ( + &self, + obj: &T ::Object , + data:impl PinInit <T ::VmBoData >, + ) ->Result <ARef <GpuVmBo <T >>,AllocError > { +Ok (GpuVmBoAlloc ::new (self, obj, data)?.obtain ()) + } +/// Clean up buffer objects that are no longer used. #[inline] pub fn deferred_cleanup (&self) {
@@ -198,6 +215,12 @@ pub fn is_extobj(&self, obj: &T::Object) -> bool {// SAFETY: By type invariants we can free it when refcount hits zero. drop (unsafe {KBox ::from_raw (me) }) } + +#[inline] +fn raw_resv (&self) -> *mut bindings::dma_resv { +// SAFETY: `r_obj` is immutable and valid for duration of GPUVM. +unsafe { (*(*self.as_raw ()).r_obj).resv } + } }/// The manager for a GPUVM.
@@ -207,6 +230,9 @@ pub trait DriverGpuVm: Sized {/// The kind of GEM object stored in this GPUVM. type Object :IntoGEMObject ; + +/// Data stored with each `struct drm_gpuvm_bo`. +type VmBoData ; }/// The core of the DRM GPU VA manager.
rust/kernel/drm/gpuvm/vm_bo.rs new file +227 -0
227 lines not on this page · raw message
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
Alice Ryhl
maintainerPATCH v5 4/6 rust: gpuvm: add GpuVa struct
In reply to Alice RyhlThis struct will be used to keep track of individual mapped ranges in the GPU's virtual memory.
--- rust/kernel/drm/gpuvm/mod.rs | 17 ++++- rust/kernel/drm/gpuvm/va.rs | 151 +++++++++++++++++++++++++++++++++++++++++ rust/kernel/drm/gpuvm/vm_bo.rs | 1 - 3 files changed, 166 insertions(+), 3 deletions(-)
rust/kernel/drm/gpuvm/mod.rs +15 -2
@@ -11,7 +11,10 @@//! C header: [`include/drm/drm_gpuvm.h`](srctree/include/drm/drm_gpuvm.h) use kernel::{ - alloc::AllocError , + alloc::{ +AllocError , +Flags as AllocFlags ,// + }, bindings, drm, drm::gem::IntoGEMObject ,
@@ -25,9 +28,13 @@use core::{ cell::UnsafeCell , - mem::ManuallyDrop , + mem::{ +ManuallyDrop , +MaybeUninit ,// + }, ops::{Deref , +DerefMut ,Range ,// }, ptr::{
@@ -36,6 +43,9 @@ }, // }; +mod va; +pub use self::va::*; +mod vm_bo;pub use self::vm_bo::*;
@@ -231,6 +241,9 @@ pub trait DriverGpuVm: Sized {/// The kind of GEM object stored in this GPUVM. type Object :IntoGEMObject ; +/// Data stored with each `struct drm_gpuva`. +type VaData ; +/// Data stored with each `struct drm_gpuvm_bo`. type VmBoData ; }
rust/kernel/drm/gpuvm/va.rs new file +151 -0
151 lines not on this page · raw message
rust/kernel/drm/gpuvm/vm_bo.rs +0 -1
@@ -106,7 +106,6 @@ impl<T: DriverGpuVm> GpuVmBo<T> {/// For the duration of `'a`, the pointer must reference a valid `drm_gpuvm_bo` associated with /// a [`GpuVm<T>`]. The BO must also be present in the GEM list. #[inline] -#[expect(dead_code)] pub (crate )unsafe fn from_raw <'a>(ptr: *mut bindings::drm_gpuvm_bo ) -> &'aSelf {// SAFETY: `drm_gpuvm_bo` is first field and `repr(C)`. unsafe { &*ptr.cast () }
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
Alice Ryhl
maintainerPATCH v5 5/6 rust: gpuvm: add GpuVmCore::sm_unmap()
In reply to Alice RyhlAdd the entrypoint for unmapping ranges in the GPUVM, and provide callbacks and VA types for the implementation.
--- rust/kernel/drm/gpuvm/mod.rs | 30 ++++- rust/kernel/drm/gpuvm/sm_ops.rs | 270 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/drm/gpuvm/va.rs | 1 - rust/kernel/drm/gpuvm/vm_bo.rs | 8 ++ 4 files changed, 304 insertions(+), 5 deletions(-)
rust/kernel/drm/gpuvm/mod.rs +26 -4
@@ -18,6 +18,7 @@ bindings, drm, drm::gem::IntoGEMObject, + error::to_result, prelude::*, sync::aref::{ARef ,
@@ -28,6 +29,7 @@use core::{ cell::UnsafeCell , + marker::PhantomData , mem::{ManuallyDrop ,MaybeUninit ,//
@@ -43,12 +45,15 @@ }, // }; -mod va; -pub use self::va::*; +mod sm_ops; +pub use self::sm_ops::*;mod vm_bo;pub use self::vm_bo::*; +mod va; +pub use self::va::*; +/// A DRM GPU VA manager. /// /// This object is refcounted, but the locations of mapped ranges may only be accessed or changed
@@ -94,8 +99,8 @@ const fn vtable() -> &'static bindings::drm_gpuvm_ops { vm_bo_free: GpuVmBo::<T>::FREE_FN, vm_bo_validate: None, sm_step_map: None, - sm_step_unmap: None, - sm_step_remap: None, + sm_step_unmap: Some(Self::sm_step_unmap), + sm_step_remap: Some(Self::sm_step_remap), } }
@@ -246,6 +251,23 @@ pub trait DriverGpuVm: Sized {/// Data stored with each `struct drm_gpuvm_bo`. type VmBoData ; + +/// The private data passed to callbacks. +type SmContext <'ctx>; + +/// Indicates that an existing mapping should be removed. +fn sm_step_unmap <'op, 'ctx>( + &mut self, + op:OpUnmap <'op,Self >, + context: &mut Self ::SmContext <'ctx>, + ) ->Result <OpUnmapped <'op,Self >,Error >; + +/// Indicates that an existing mapping should be split up. +fn sm_step_remap <'op, 'ctx>( + &mut self, + op:OpRemap <'op,Self >, + context: &mut Self ::SmContext <'ctx>, + ) ->Result <OpRemapped <'op,Self >,Error >; }/// The core of the DRM GPU VA manager.
rust/kernel/drm/gpuvm/sm_ops.rs new file +270 -0
270 lines not on this page · raw message
rust/kernel/drm/gpuvm/va.rs +0 -1
@@ -1,6 +1,5 @@// SPDX-License-Identifier: GPL-2.0 OR MIT -#![expect(dead_code)] use super ::*;/// Represents that a range of a GEM object is mapped in this [`GpuVm`] instance.
rust/kernel/drm/gpuvm/vm_bo.rs +8 -0
@@ -136,6 +136,14 @@ pub fn obj(&self) -> &T::Object {pub fn data (&self) -> &T ::VmBoData { &self.data } + +pub (super )fn lock_gpuva (&self) ->crate ::sync::MutexGuard <'_, ()> { +// SAFETY: The GEM object is valid. +let ptr =unsafe { &raw mut (*self.obj ().as_raw ()).gpuva.lock }; +// SAFETY: The GEM object is valid, so the mutex is properly initialized. +let mutex =unsafe {crate ::sync::Mutex ::from_raw (ptr) }; + mutex.lock () + } }/// A pre-allocated [`GpuVmBo`] object.
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
Alice Ryhl
maintainerPATCH v5 6/6 rust: gpuvm: add GpuVmCore::sm_map()
In reply to Alice RyhlFinally also add the operation for creating new mappings. Mapping operations need extra data in the context since they involve a vm_bo coming from the outside.
--- rust/kernel/drm/gpuvm/mod.rs | 9 ++- rust/kernel/drm/gpuvm/sm_ops.rs | 157 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 160 insertions(+), 6 deletions(-)
rust/kernel/drm/gpuvm/mod.rs +8 -1
@@ -98,7 +98,7 @@ const fn vtable() -> &'static bindings::drm_gpuvm_ops { vm_bo_alloc: GpuVmBo::<T>::ALLOC_FN, vm_bo_free: GpuVmBo::<T>::FREE_FN, vm_bo_validate: None, - sm_step_map: None, + sm_step_map: Some(Self::sm_step_map), sm_step_unmap: Some(Self::sm_step_unmap), sm_step_remap: Some(Self::sm_step_remap), }
@@ -255,6 +255,13 @@ pub trait DriverGpuVm: Sized {/// The private data passed to callbacks. type SmContext <'ctx>; +/// Indicates that a new mapping should be created. +fn sm_step_map <'op, 'ctx>( + &mut self, + op:OpMap <'op,Self >, + context: &mut Self ::SmContext <'ctx>, + ) ->Result <OpMapped <'op,Self >,Error >; +/// Indicates that an existing mapping should be removed. fn sm_step_unmap <'op, 'ctx>( &mut self,
rust/kernel/drm/gpuvm/sm_ops.rs +152 -5
195 lines not on this page · raw message
Signature · 1 line
2.53.0.959.g497ff81fa9-goog
(subset) Re: [PATCH v5 0/6] Rust GPUVM immediate mode
In reply to Alice RyhlQuoted 1 line · Alice Ryhl
rust: helpers: Add bindings/wrappers for
dma_resv_lock
Applied to drm-rust-next, thanks!