rust: drm: Add GPUVM abstraction
Subsystems (4)
Daniel Almeida
reviewerPATCH v3 1/1 rust: drm: Add GPUVM 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>.
The DriverGpuVm trait should be implemented by drivers, as they should provide the hardware-specific implementation of the mapping/unmapping and remapping steps. The steps themselves are computed by the GPUVM C code.
This abstraction for drivers looking to support bind and unbind operations (known as the VM_BIND API) commonly used to implement Vulkan 'sparse resources', which includes all upcoming Rust GPU drivers so far.
We require &mut self for any operations that modify the interval tree used to keep track of the current bindings in a VM. In this sense, Rust drivers are expected to provide some kind of interior mutability in order to use any operation that would otherwise race to access the tree.
Locking the BO va list is still todo, although it should be implementable in a future iteration of this patch, as soon as the pre-requisite patches land. Asynchronous VM_BINDS should also be possible.
This patch notably misses any other type of locking helpers, like any drm_exec or prepare_vm helpers. That's because it is not clear how these should look like so far, such that it is safer to proceed with the current subset of operations, which are deemed both somewhat sufficient and required for upcoming drivers.
Note that all operations provided were tested on a real driver and on a real device. Specifically, all operations so far are required.
---
Hi all,
I'm sending v3 while we wait for the patches that rework the locking of the per-BO VA list. It should be a small delta to acquire the list lock then.
Async VM_BINDS should be possible using the current version, as far as I can tell.
I have rebased on top of Lyude's gem-shmem branch [0], to pickup the new changes to gem::Object<T> which we will invariably have to account for anyways. Besides, that branch introduces IntoGEMObject::as_ref(), which is very convenient.
In order to be useful, this patch needs a few changes to ARef.
In particular, we should make ARef::into_inner_mut(), which was introduced in "rust: types: Add ARef::as_inner_mut()". I looked around a little bit and it appears like this patch hasn't landed yet.
We need that patch itself, but also we need to change the signature to pub instead of pub(crate), because if drivers can prove that it's sound to derive a &mut T from an ARef<T>, they should be able to do it.
This is notably the case for Tyr, and should also be the case for other drivers, because Tyr keeps a single instance of ARef<GpuVm<T>> inside a driver-specific Arc<Mutex<Vm>> instance. This means that at all points, it is safe to get a &mut GpuVm<T> if we have a &mut tyr::Vm.
Also, we need to account for !Unpin types, as Lyude's version doesn't:
i.e.: if T: !Unpin, a safety requirement of as_inner_mut() should be not using the returned &mut T to move T.
By the way, the docs might be shot in this version of the patch. I did not manage to run rustdoc for whatever reason:
``` error: couldn't generate documentation: invalid template: last line expected to start with a comment | = note: failed to create or modify "./Documentation/output/rust/rustdoc/src-files.js" error: aborting due to 1 previous error ```
As the current version is still a bit far from the one that will be picked, I deferred this to a future iteration.
Lastly, I want to mention that I tested this on my 6.16-rc2 branch of Tyr and it works. Like other drivers, booting the MCU is a bit tricky for us because it expects things to be mapped at their exact spots. The fact that it still works makes me confident in the current approach.
I can share this branch if any of you want to see how this looks like in a real driver.
[0]: https://gitlab.freedesktop.org/lyudess/linux/-/tree/rust/gem-shmem?ref_type=heads
---
Changes in v3:
- Removed LockedGpuVm and UpdatingGpuVm. If we have a &mut GpuVm<T> we are guaranteed by the type system to be the only ones capable of mutating the interval tree.
- Split insert and link, as well as remove and unlink. These are now separate functions.
- Speaking of which, I took the liberty of changing the nomenclature a little bit for some functions. For example, I think "map" and "unmap" are both more expressive than "sm_map" and "sm_unmap".
- Added more features needed by Tyr. Specifically, we definitely need to get the drm_gem_object backing a given GpuVa<T>. For this reason, I've introduced find_va() and find_first_va(). They take a closure because we can only guarantee that the VA is in fact resident while these functions are running. I.e.: further operations may modify the state of the VM, thereby invalidating a given ResidentGpuVa<T>. With a closure, this is not possible.
- Wrapped all bindgen types in Opaque as appropriate.
- Tried to use cast()/&raw and etc as much as possible.
- Reworked some of the SAFETY comments.
- Removed the previous patch (the one adding dma_resv_lock/unlock), as we've decided to not go that way anymore.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes from v1:
- Added more people to cc.
- Used Benno's suggestion to remove the #[allow] (thanks, Benno, that is indeed better!)
- Added markdown as applicable in the docs/comments (please let me know whether I missed any)
- Changed the order between docs and attributes to match the rest of the code in the Rust crate.
- Did *not* remove the # Arguments section from a few functions yet, let's push this dicussion down the line, Miguel.
- Changed the placement of OpRemap, i.e.: it was not next to its impl block by mistake.
- Misc doc fixes, i.e. missing periods, missing ///, lines that would not render properly on the browser, etc (thanks, Miguel)
- Removed spurious clang-format change from patch 1.
- Added a return statement for find_bo/obtain_bo (thanks, Charalampos!)
Changes from v0:
This version changes how LockedGpuVm operates. The previous code assumed
that the interval tree would be protected if all the associated GEM's resvs were locked, but this is completely unrelated. In fact, this initial version only aims to support the core VA range management feature of GPUVM, and not any of the "convenience" functions like "exec_lock()" and friends, so this function was removed accordingly.
LockedGpuVm is now produced by a call to GpuVm::lock(). This takes a generic
guard type to signal that the driver-specific locks have been acquired in order to protect the VMs tree, and hence its view of the address space. This approach is somewhat similar to CondVar, but also incurs in the same pitfall: it is up to the caller to give meaning to the guard by ensuring that it actually protects against concurrent access to the VM. Maybe this is a good candidate to the upcoming "Pitfall" docs section?
Note that LockedGpuVm::obj was removed. I'm not really sure why this field
was there in the first place, but callers should indicate the object through the sm_map() and sm_unmap() APIs instead.
I am not sure why GpuVm::inner uses UnsafeCell, though. I did not touch this
so that we can first discuss whether UnsafeCell is really needed.
The docs were also updated. Care was taken to reuse the C documentation as
much as possible.
Itemized changes:
- Rebased on top of nova-drm
- Use srctree in the docs
- Add docs as appropriate and remove #[allow(missing_docs)]
- Remove impl DriverGpuVa for (). Drivers can to that themselves, if they want.
- Added #[inline] to getters, as Rust cannot inline across crates otherwise (unless this changed recently?)
- Exposed drm_gpuva_op_unmap::keep.
- Removed pub(super) from unsafe fns meant to be called from the C code.
- Added "# Safety" blocks to the above.
- Removed exec_lock_gem_object().
- Removed exec_lock()
- Removed LockedGpuVm::vm_exec. This initial version does not support exec calls at all.
- By the same token, removed LockedGpuVm::drop()
- Removed LockedGpuVm::obj. This object has to be passed explicitly.
--- rust/bindings/bindings_helper.h | 1 + rust/helpers/drm_gpuvm.c | 18 + rust/helpers/helpers.c | 1 + rust/kernel/drm/gpuvm.rs | 951 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/drm/mod.rs | 2 + 5 files changed, 973 insertions(+)
rust/bindings/bindings_helper.h +1 -0
@@ -11,6 +11,7 @@#include <drm/drm_file.h> #include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.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 +18 -0
@@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 or MIT + +#include <drm/drm_gpuvm.h> + +struct drm_gpuvm *rust_helper_drm_gpuvm_get (struct drm_gpuvm *obj) +{ +return drm_gpuvm_get (obj); +} + +void rust_helper_drm_gpuva_init_from_op (struct drm_gpuva *va,struct drm_gpuva_op_map *op) +{ +drm_gpuva_init_from_op (va, op); +} + +struct drm_gpuvm_bo *rust_helper_drm_gpuvm_bo_get (struct drm_gpuvm_bo *vm_bo) +{ +return drm_gpuvm_bo_get (vm_bo); +}
rust/helpers/helpers.c +1 -0
@@ -17,6 +17,7 @@#include "device.c" #include "dma-resv.c" #include "drm.c" +#include "drm_gpuvm.c" #include "err.c" #include "fs.c" #include "io.c"
rust/kernel/drm/gpuvm.rs new file +951 -0
951 lines not on this page · raw message
rust/kernel/drm/mod.rs +2 -0
@@ -6,6 +6,8 @@pub mod driver;pub mod file;pub mod gem; +#[cfg( CONFIG_DRM_GPUVM = "y" )] +pub mod gpuvm;pub mod ioctl;pub use self::device::Device ;
---
base-commit: 2ab157cabbbcd3ae64320078c64bcac88056ba46
change-id: 20250320-gpuvm-29d3e0726f34
Best regards,
Signature · 1 line
Daniel Almeida <[email protected]>