Skip to contentSkip to conversation

Rust GPUVM immediate mode

PATCH Superseded Alice Ryhl 4 participants 24 messages Jan 30 – Feb 25

Subsystems (5)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Alice Ryhl

author maintainer
#0 lore raw

v4 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 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        |  18 ++
 rust/helpers/helpers.c          |   2 +
 rust/kernel/drm/gpuvm/mod.rs    | 299 ++++++++++++++++++++++++++++
 rust/kernel/drm/gpuvm/sm_ops.rs | 417 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/drm/gpuvm/va.rs     | 148 ++++++++++++++
 rust/kernel/drm/gpuvm/vm_bo.rs  | 227 ++++++++++++++++++++++
 rust/kernel/drm/mod.rs          |   1 +
 10 files changed, 1131 insertions(+)

---
base-commit: 263e9ef3f5736697483af66babb8bc72f145b3f4
change-id: 20251128-gpuvm-rust-b719cac27ad6

Best regards,

Signature · 1 line
Alice Ryhl <[email protected]>
author maintainer
#1 lore raw

PATCH v4 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 GpuVmCore, 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        |  18 ++++
 rust/helpers/helpers.c          |   1 +
 rust/kernel/drm/gpuvm/mod.rs    | 231 ++++++++++++++++++++++++++++++++++++++++
 rust/kernel/drm/mod.rs          |   1 +
 6 files changed, 254 insertions(+)
MAINTAINERS +2 -0
index 3b84ad595e226f231b256d24f0da6bac459e93a8..618becae72985b9dfdca8469ee48d4752fd0ca41 100644
@@ -8720,6 +8720,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
index a067038b4b422b4256f4a2b75fe644d47e6e82c8..dd60a5c6b142ec2c5fd6df80279ab6813163791c 100644
@@ -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 +18 -0
new file mode 100644
index 0000000000000000000000000000000000000000..d1471e5844ec81f994af9252d9054053ab13f352
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0 or MIT
+
+#ifdef CONFIG_DRM_GPUVM
+
+#include <drm/drm_gpuvm.h>
+
+struct drm_gpuvm *rust_helper_drm_gpuvm_get(struct drm_gpuvm *obj)
+{
+	return drm_gpuvm_get(obj);
+}
+
+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
index 79c72762ad9c4b473971e6210c9577860d2e2b08..0943d589b7578d3c0e207937f63a5e02719c6146 100644
@@ -26,6 +26,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 +231 -0

231 lines not on this page · raw message

rust/kernel/drm/mod.rs +1 -0
index 1b82b6945edf25b947afc08300e211bd97150d6b..a4b6c5430198571ec701af2ef452cc9ac55870e6 100644
@@ -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.rc1.225.gd81095ad13-goog
author maintainer
#6 lore raw

PATCH v4 2/6 rust: helpers: Add bindings/wrappers for dma_resv_lock

In reply to Alice Ryhl

From: 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 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
index 618becae72985b9dfdca8469ee48d4752fd0ca41..8d44728261ffa82fc36fa0c5df3b11a5bfb4339b 100644
@@ -7467,6 +7467,7 @@ L:	[email protected]
 S:	Supported
 W:	https://rust-for-linux.com
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
+F:	rust/helpers/dma-resv.c
 F:	rust/helpers/dma.c
 F:	rust/helpers/scatterlist.c
 F:	rust/kernel/dma.rs
rust/bindings/bindings_helper.h +1 -0
index dd60a5c6b142ec2c5fd6df80279ab6813163791c..ed80dd8088bc60c67b02f7666e602e33158bb962 100644
@@ -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
new file mode 100644
index 0000000000000000000000000000000000000000..71914d8241e297511fdf7770336756c3e953a4f4
@@ -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
index 0943d589b7578d3c0e207937f63a5e02719c6146..aae78c938b0b5848b1740fb3f2dc4b7f93b1a760 100644
@@ -25,6 +25,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.rc1.225.gd81095ad13-goog
author maintainer
#9 lore raw

PATCH v4 3/6 rust: gpuvm: add GpuVm::obtain()

In reply to Alice Ryhl

This 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/kernel/drm/gpuvm/mod.rs   |  32 +++++-
 rust/kernel/drm/gpuvm/vm_bo.rs | 219 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 248 insertions(+), 3 deletions(-)
rust/kernel/drm/gpuvm/mod.rs +29 -3
index dcb1fccc766115c6a0ca03bda578e3f3e5791492..8f2f1c135e9dd071fd4b4ad0762a3e79dc922eea 100644
@@ -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 "core" is only accessible using a special unique handle. The
@@ -68,8 +75,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,
@@ -166,6 +173,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<GpuVmBoRegistered<T>, AllocError> {
+        Ok(GpuVmBoAlloc::new(self, obj, data)?.obtain())
+    }
+
     /// Clean up buffer objects that are no longer used.
     #[inline]
     pub fn deferred_cleanup(&self) {
@@ -191,6 +208,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.
@@ -200,6 +223,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 +219 -0

219 lines not on this page · raw message

Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#19 lore raw

PATCH v4 4/6 rust: gpuvm: add GpuVa struct

In reply to Alice Ryhl

This 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  | 149 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 164 insertions(+), 2 deletions(-)
rust/kernel/drm/gpuvm/mod.rs +15 -2
index 8f2f1c135e9dd071fd4b4ad0762a3e79dc922eea..c8c024ec47b0053d9941465858c0597f0dfd4950 100644
@@ -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::*;
 
@@ -224,6 +234,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 +149 -0

149 lines not on this page · raw message

Signature · 1 line
2.53.0.rc1.225.gd81095ad13-goog
author maintainer
#20 lore raw

PATCH v4 5/6 rust: gpuvm: add GpuVmCore::sm_unmap()

In reply to Alice Ryhl

Add 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
index c8c024ec47b0053d9941465858c0597f0dfd4950..fd4c662f84a4830515c2ddd18d5d503e4ee9fc8f 100644
@@ -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 "core" is only accessible using a special unique handle. The
@@ -89,8 +94,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),
         }
     }
 
@@ -239,6 +244,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
index c96796a6b2c8c7c4b5475324562968ca0f07fd09..a31122ff22282186a1d76d4bb085714f6465722b 100644
@@ -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
index 272e1a83c2d5f43c42dbdd9e09f51394a1e855b6..e8570f321c107be44fe2e463c88e2760fa197dfa 100644
@@ -100,6 +100,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.rc1.225.gd81095ad13-goog
author maintainer
#21 lore raw

PATCH v4 6/6 rust: gpuvm: add GpuVmCore::sm_map()

In reply to Alice Ryhl

Finally 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
index fd4c662f84a4830515c2ddd18d5d503e4ee9fc8f..20e512842dfc6f2bd461cd3d22361ef8bff2f204 100644
@@ -93,7 +93,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),
         }
@@ -248,6 +248,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.rc1.225.gd81095ad13-goog
#2 lore raw

Re: [PATCH v4 1/6] rust: drm: add base GPUVM immediate mode abstraction

In reply to Alice Ryhl

Hi Alice,

Quoted 19 lines · On Fri, 30 Jan 2026 14:24:10 +0000 Alice Ryhl <[email protected]> wrote:
rust/kernel/drm/gpuvm/mod.rs new file +19 -0
+/// A DRM GPU VA manager.
+///
+/// This object is refcounted, but the "core" is only accessible using a special unique handle. The
+/// core consists of the `core` field and the GPUVM's interval tree.
+///
+/// # Invariants
+///
+/// * Stored in an allocation managed by the refcount in `self.vm`.
+/// * Access to `data` and the gpuvm interval tree is controlled via the [`GpuVmCore`] type.
+#[pin_data]
+pub struct GpuVm<T: DriverGpuVm> {
+    #[pin]
+    vm: Opaque<bindings::drm_gpuvm>,
+    /// Accessed only through the [`GpuVmCore`] reference.
+    data: UnsafeCell<T>,
+}
+
+// SAFETY: By type invariants, the allocation is managed by the refcount in `self.vm`.
+unsafe impl<T: DriverGpuVm> AlwaysRefCounted for GpuVm<T> {

rust/kernel/drm/gpuvm/mod.rs line 63

Quoted 10 lines
rust/kernel/drm/gpuvm/mod.rs new file +10 -0
+    fn inc_ref(&self) {
+        // SAFETY: By type invariants, the allocation is managed by the refcount in `self.vm`.
+        unsafe { bindings::drm_gpuvm_get(self.vm.get()) };
+    }
+
+    unsafe fn dec_ref(obj: NonNull<Self>) {
+        // SAFETY: By type invariants, the allocation is managed by the refcount in `self.vm`.
+        unsafe { bindings::drm_gpuvm_put((*obj.as_ptr()).vm.get()) };
+    }
+}

As discussed on Zulip, in Tyr, we're gonna need Sync+Send on GpuVm<T> if we want to be able to call some of the thread-safe functions concurrently (thinking of obtain(), but other read-only info might be needed to).

Regards,

Boris

#22 lore raw

Re: [PATCH v4 6/6] rust: gpuvm: add GpuVmCore::sm_map()

In reply to Alice Ryhl

Hi Alice, I got a build error testing gpuvm v4 with Tyr.

Quoted 41 lines · Alice Ryhl

Finally 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/sm_ops.rs +32 -0
+}
+
+impl<'op, T: DriverGpuVm> OpMap<'op, T> {
+    /// The base address of the new mapping.
+    pub fn addr(&self) -> u64 {
+        self.op.va.addr
+    }
+
+    /// The length of the new mapping.
+    pub fn length(&self) -> u64 {
+        self.op.va.range
+    }
+
+    /// The offset within the [`drm_gem_object`](crate::gem::Object).
+    pub fn gem_offset(&self) -> u64 {
+        self.op.gem.offset
+    }
+
+    /// The [`drm_gem_object`](crate::gem::Object) to map.
+    pub fn obj(&self) -> &T::Object {
+        // SAFETY: The `obj` pointer is guaranteed to be valid.
+        unsafe { <T::Object as IntoGEMObject>::from_raw(self.op.gem.obj) }
+    }
+
+    /// The [`GpuVmBo`] that the new VA will be associated with.
+    pub fn vm_bo(&self) -> &GpuVmBo<T> {
+        self.vm_bo
+    }
+
+    /// Use the pre-allocated VA to carry out this map operation.
+    pub fn insert(self, va: GpuVaAlloc<T>, va_data: impl PinInit<T::VaData>) -> OpMapped<'op, T> {
+        let va = va.prepare(va_data);

rust/kernel/drm/gpuvm/sm_ops.rs struct SmData<'a, 'ctx, T: DriverGpuVm> { · line 99

Quoted 10 lines
rust/kernel/drm/gpuvm/sm_ops.rs +10 -0
+        // SAFETY: By the type invariants we may access the interval tree.
+        unsafe { bindings::drm_gpuva_map(self.vm_bo.gpuvm().as_raw(), va, self.op) };
+
+        let _gpuva_guard = self.vm_bo().lock_gpuva();
+        // SAFETY: The va is prepared for insertion, and we hold the GEM lock.
+        unsafe { bindings::drm_gpuva_link(va, self.vm_bo.as_raw()) };
+
+        OpMapped {
+            _invariant: self._invariant,
+        }

error[E0308]: mismatched types
--> rust/kernel/drm/gpuvm/sm_ops.rs:98:25

   |
98 |             _invariant: self._invariant,
   |                         ^^^^^^^^^^^^^^^ expected `PhantomData<*mut &mut T>`, found `PhantomData<fn(&mut T) -> fn(&mut T)>`
   |
   = note: expected struct `core::marker::PhantomData<*mut &mut T>`
              found struct `core::marker::PhantomData<fn(&'op mut T) -> fn(&'op mut T)>`

Updating the PhantomData type for OpMapped to match OpMap seems to fix it.

Deborah

author maintainer
#23 lore raw

Re: [PATCH v4 6/6] rust: gpuvm: add GpuVmCore::sm_map()

Quoted 64 lines · Deborah Brouwer

Hi Alice, I got a build error testing gpuvm v4 with Tyr.

Quoted 41 lines · Alice Ryhl

Finally 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/sm_ops.rs +32 -0
+}
+
+impl<'op, T: DriverGpuVm> OpMap<'op, T> {
+    /// The base address of the new mapping.
+    pub fn addr(&self) -> u64 {
+        self.op.va.addr
+    }
+
+    /// The length of the new mapping.
+    pub fn length(&self) -> u64 {
+        self.op.va.range
+    }
+
+    /// The offset within the [`drm_gem_object`](crate::gem::Object).
+    pub fn gem_offset(&self) -> u64 {
+        self.op.gem.offset
+    }
+
+    /// The [`drm_gem_object`](crate::gem::Object) to map.
+    pub fn obj(&self) -> &T::Object {
+        // SAFETY: The `obj` pointer is guaranteed to be valid.
+        unsafe { <T::Object as IntoGEMObject>::from_raw(self.op.gem.obj) }
+    }
+
+    /// The [`GpuVmBo`] that the new VA will be associated with.
+    pub fn vm_bo(&self) -> &GpuVmBo<T> {
+        self.vm_bo
+    }
+
+    /// Use the pre-allocated VA to carry out this map operation.
+    pub fn insert(self, va: GpuVaAlloc<T>, va_data: impl PinInit<T::VaData>) -> OpMapped<'op, T> {
+        let va = va.prepare(va_data);

rust/kernel/drm/gpuvm/sm_ops.rs struct SmData<'a, 'ctx, T: DriverGpuVm> { · line 99

Quoted 10 lines
rust/kernel/drm/gpuvm/sm_ops.rs +10 -0
+        // SAFETY: By the type invariants we may access the interval tree.
+        unsafe { bindings::drm_gpuva_map(self.vm_bo.gpuvm().as_raw(), va, self.op) };
+
+        let _gpuva_guard = self.vm_bo().lock_gpuva();
+        // SAFETY: The va is prepared for insertion, and we hold the GEM lock.
+        unsafe { bindings::drm_gpuva_link(va, self.vm_bo.as_raw()) };
+
+        OpMapped {
+            _invariant: self._invariant,
+        }

error[E0308]: mismatched types
--> rust/kernel/drm/gpuvm/sm_ops.rs:98:25

   |
98 |             _invariant: self._invariant,
   |                         ^^^^^^^^^^^^^^^ expected `PhantomData<*mut &mut T>`, found `PhantomData<fn(&mut T) -> fn(&mut T)>`
   |
   = note: expected struct `core::marker::PhantomData<*mut &mut T>`
              found struct `core::marker::PhantomData<fn(&'op mut T) -> fn(&'op mut T)>`
Quoted 2 lines

Updating the PhantomData type for OpMapped to match OpMap seems to fix it.

Hm. I thought I fixed this error. But perhaps it did not included in the sent version for some reason?

Thanks in any case.

Alice

maintainer
#3 lore raw

Re: [PATCH v4 1/6] rust: drm: add base GPUVM immediate mode abstraction

In reply to Alice Ryhl

rust/kernel/drm/gpuvm/mod.rs line 38

Quoted 4 lines · Alice Ryhl
rust/kernel/drm/gpuvm/mod.rs new file +4 -0
+/// A DRM GPU VA manager.
+///
+/// This object is refcounted, but the "core" is only accessible using a special unique handle. The
+/// core consists of the `core` field and the GPUVM's interval tree.

I think this is still a bit confusing, I think we should just rename GpuVmCore to UniqueGpuVm and rewrite this to something like:

"The driver specific data of of GpuVm is only accessible through a [UniqueGpuVm], which guarantees exclusive access."

Quoted 39 lines
rust/kernel/drm/gpuvm/mod.rs new file +39 -0
+/// # Invariants
+///
+/// * Stored in an allocation managed by the refcount in `self.vm`.
+/// * Access to `data` and the gpuvm interval tree is controlled via the [`GpuVmCore`] type.
+#[pin_data]
+pub struct GpuVm<T: DriverGpuVm> {
+    #[pin]
+    vm: Opaque<bindings::drm_gpuvm>,
+    /// Accessed only through the [`GpuVmCore`] reference.
+    data: UnsafeCell<T>,
+}
+
+// SAFETY: By type invariants, the allocation is managed by the refcount in `self.vm`.
+unsafe impl<T: DriverGpuVm> AlwaysRefCounted for GpuVm<T> {
+    fn inc_ref(&self) {
+        // SAFETY: By type invariants, the allocation is managed by the refcount in `self.vm`.
+        unsafe { bindings::drm_gpuvm_get(self.vm.get()) };
+    }
+
+    unsafe fn dec_ref(obj: NonNull<Self>) {
+        // SAFETY: By type invariants, the allocation is managed by the refcount in `self.vm`.
+        unsafe { bindings::drm_gpuvm_put((*obj.as_ptr()).vm.get()) };
+    }
+}
+
+impl<T: DriverGpuVm> GpuVm<T> {
+    const fn vtable() -> &'static bindings::drm_gpuvm_ops {
+        &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_validate: None,
+            sm_step_map: None,
+            sm_step_unmap: None,
+            sm_step_remap: None,
+        }
+    }

rust/kernel/drm/gpuvm/mod.rs line 88

Quoted 10 lines
rust/kernel/drm/gpuvm/mod.rs new file +10 -0
+
+    /// Creates a GPUVM instance.
+    #[expect(clippy::new_ret_no_self)]
+    pub fn new<E>(
+        name: &'static CStr,
+        dev: &drm::Device<T::Driver>,
+        r_obj: &T::Object,
+        range: Range<u64>,
+        reserve_range: Range<u64>,
+        data: T,

Let's be flexibile and also accept an impl PinInit<T, E> instead.

Quoted 39 lines
rust/kernel/drm/gpuvm/mod.rs new file +39 -0
+    ) -> Result<GpuVmCore<T>, E>
+    where
+        E: From<AllocError>,
+        E: From<core::convert::Infallible>,
+    {
+        let obj = KBox::try_pin_init::<E>(
+            try_pin_init!(Self {
+                data: UnsafeCell::new(data),
+                vm <- Opaque::ffi_init(|vm| {
+                    // SAFETY: These arguments are valid. `vm` is valid until refcount drops to
+                    // zero.
+                    unsafe {
+                        bindings::drm_gpuvm_init(
+                            vm,
+                            name.as_char_ptr(),
+                            bindings::drm_gpuvm_flags_DRM_GPUVM_IMMEDIATE_MODE
+                                | bindings::drm_gpuvm_flags_DRM_GPUVM_RESV_PROTECTED,
+                            dev.as_raw(),
+                            r_obj.as_raw(),
+                            range.start,
+                            range.end - range.start,
+                            reserve_range.start,
+                            reserve_range.end - reserve_range.start,
+                            const { Self::vtable() },
+                        )
+                    }
+                }),
+            }? E),
+            GFP_KERNEL,
+        )?;
+        // SAFETY: This transfers the initial refcount to the ARef.
+        Ok(GpuVmCore(unsafe {
+            ARef::from_raw(NonNull::new_unchecked(KBox::into_raw(
+                Pin::into_inner_unchecked(obj),
+            )))
+        }))
+    }
+
+    /// Access this [`GpuVm`] from a raw pointer.

rust/kernel/drm/gpuvm/mod.rs line 137

Quoted 10 lines
rust/kernel/drm/gpuvm/mod.rs new file +10 -0
+    ///
+    /// # Safety
+    ///
+    /// The pointer must reference the `struct drm_gpuvm` in a valid [`GpuVm<T>`] that remains
+    /// valid for at least `'a`.
+    #[inline]
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm) -> &'a Self {
+        // SAFETY: Caller passes a pointer to the `drm_gpuvm` in a `GpuVm<T>`. Caller ensures the
+        // pointer is valid for 'a.
+        unsafe { &*kernel::container_of!(Opaque::cast_from(ptr), Self, vm) }

I'd pull the Opaque::cast_from() call out of the unsafe block.

Quoted 49 lines
rust/kernel/drm/gpuvm/mod.rs new file +49 -0
+    }
+
+    /// Returns a raw pointer to the embedded `struct drm_gpuvm`.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::drm_gpuvm {
+        self.vm.get()
+    }
+
+    /// The start of the VA space.
+    #[inline]
+    pub fn va_start(&self) -> u64 {
+        // SAFETY: The `mm_start` field is immutable.
+        unsafe { (*self.as_raw()).mm_start }
+    }
+
+    /// The length of the GPU's virtual address space.
+    #[inline]
+    pub fn va_length(&self) -> u64 {
+        // SAFETY: The `mm_range` field is immutable.
+        unsafe { (*self.as_raw()).mm_range }
+    }
+
+    /// Returns the range of the GPU virtual address space.
+    #[inline]
+    pub fn va_range(&self) -> Range<u64> {
+        let start = self.va_start();
+        // OVERFLOW: This reconstructs the Range<u64> passed to the constructor, so it won't fail.
+        let end = start + self.va_length();
+        Range { start, end }
+    }
+
+    /// Clean up buffer objects that are no longer used.
+    #[inline]
+    pub fn deferred_cleanup(&self) {
+        // SAFETY: This GPUVM uses immediate mode.
+        unsafe { bindings::drm_gpuvm_bo_deferred_cleanup(self.as_raw()) }
+    }
+
+    /// Check if this GEM object is an external object for this GPUVM.
+    #[inline]
+    pub fn is_extobj(&self, obj: &T::Object) -> bool {
+        // SAFETY: We may call this with any GPUVM and GEM object.
+        unsafe { bindings::drm_gpuvm_is_extobj(self.as_raw(), obj.as_raw()) }
+    }
+
+    /// Free this GPUVM.
+    ///
+    /// # Safety
+    ///

rust/kernel/drm/gpuvm/mod.rs line 196

Quoted 10 lines
rust/kernel/drm/gpuvm/mod.rs new file +10 -0
+    /// Called when refcount hits zero.
+    unsafe extern "C" fn vm_free(me: *mut bindings::drm_gpuvm) {
+        // SAFETY: Caller passes a pointer to the `drm_gpuvm` in a `GpuVm<T>`.
+        let me = unsafe { kernel::container_of!(Opaque::cast_from(me), Self, vm).cast_mut() };
+        // SAFETY: By type invariants we can free it when refcount hits zero.
+        drop(unsafe { KBox::from_raw(me) })
+    }
+}
+
+/// The manager for a GPUVM.

This description seems a bit odd. In the end, the trait makes GPUVM aware of other driver specific types. So, maybe a better name would be gpuvm::DriverAttributes, gpuvm::DriverTypes, gpuvm::DriverInfo or just gpuvm::Driver. My favorite is gpuvm::DriverInfo.

We should also change the doc-comment accordingly. Maybe somthing like: "This trait make the [GpuVm] aware of the other driver specific DRM types."

Quoted 7 lines
rust/kernel/drm/gpuvm/mod.rs new file +7 -0
+pub trait DriverGpuVm: Sized {
+    /// Parent `Driver` for this object.
+    type Driver: drm::Driver<Object = Self::Object>;
+
+    /// The kind of GEM object stored in this GPUVM.
+    type Object: IntoGEMObject;
+}
maintainer
#7 lore raw

Re: [PATCH v4 2/6] rust: helpers: Add bindings/wrappers for dma_resv_lock

In reply to Alice Ryhl
Quoted 28 lines · Alice Ryhl

From: 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 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 +0 -0
index 618becae72985b9dfdca8469ee48d4752fd0ca41..8d44728261ffa82fc36fa0c5df3b11a5bfb4339b 100644

MAINTAINERS L: [email protected] · line 7473

Quoted 7 lines
MAINTAINERS +1 -0
@@ -7467,6 +7467,7 @@ L:	[email protected]
 S:	Supported
 W:	https://rust-for-linux.com
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
+F:	rust/helpers/dma-resv.c
 F:	rust/helpers/dma.c
 F:	rust/helpers/scatterlist.c
 F:	rust/kernel/dma.rs

That's fine for me in general, but maybe the DMA BUFFER SHARING FRAMEWORK is probably the correct place. DMA BUFFER SHARING FRAMEWORK is also under the DRM umbrella, so we should also add it to the drm-rust entry as well.

author maintainer
#4 lore raw

Re: [PATCH v4 1/6] rust: drm: add base GPUVM immediate mode abstraction

In reply to Danilo Krummrich
Quoted 11 lines · Danilo Krummrich

rust/kernel/drm/gpuvm/mod.rs line 38

Quoted 4 lines · Alice Ryhl
rust/kernel/drm/gpuvm/mod.rs new file +4 -0
+/// A DRM GPU VA manager.
+///
+/// This object is refcounted, but the "core" is only accessible using a special unique handle. The
+/// core consists of the `core` field and the GPUVM's interval tree.

I think this is still a bit confusing, I think we should just rename GpuVmCore to UniqueGpuVm and rewrite this to something like:

"The driver specific data of of GpuVm is only accessible through a [UniqueGpuVm], which guarantees exclusive access."

But it's not really the same as e.g. UniqueArc<_>, which implies that there are no normal Arc<_>s whatsoever. This one is just a special ref, but there may also be normal refs at the same time.

Quoted 6 lines

rust/kernel/drm/gpuvm/mod.rs line 137

Quoted 4 lines
rust/kernel/drm/gpuvm/mod.rs new file +4 -0
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm) -> &'a Self {
+        // SAFETY: Caller passes a pointer to the `drm_gpuvm` in a `GpuVm<T>`. Caller ensures the
+        // pointer is valid for 'a.
+        unsafe { &*kernel::container_of!(Opaque::cast_from(ptr), Self, vm) }

I'd pull the Opaque::cast_from() call out of the unsafe block.

I think that's a bit verbose, and all existing uses do it inside the container_of!.

Quoted 9 lines
Quoted 1 line

+/// The manager for a GPUVM.

This description seems a bit odd. In the end, the trait makes GPUVM aware of other driver specific types. So, maybe a better name would be gpuvm::DriverAttributes, gpuvm::DriverTypes, gpuvm::DriverInfo or just gpuvm::Driver. My favorite is gpuvm::DriverInfo.

We should also change the doc-comment accordingly. Maybe somthing like: "This trait make the [GpuVm] aware of the other driver specific DRM types."

I mean, it doesn't just do that. The type implementing this is the type stored in the core field, so you really do get more than just some type relationships from it.

Alice

Quoted 7 lines
Quoted 7 lines
rust/kernel/drm/gpuvm/mod.rs new file +7 -0
+pub trait DriverGpuVm: Sized {
+    /// Parent `Driver` for this object.
+    type Driver: drm::Driver<Object = Self::Object>;
+
+    /// The kind of GEM object stored in this GPUVM.
+    type Object: IntoGEMObject;
+}
author maintainer
#8 lore raw

Re: [PATCH v4 2/6] rust: helpers: Add bindings/wrappers for dma_resv_lock

In reply to Danilo Krummrich
Quoted 24 lines · Danilo Krummrich
Quoted 23 lines · Alice Ryhl

From: 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 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(+)
Quoted 16 lines

MAINTAINERS L: [email protected] · line 7473

Quoted 12 lines
MAINTAINERS +1 -0
index 618becae72985b9dfdca8469ee48d4752fd0ca41..8d44728261ffa82fc36fa0c5df3b11a5bfb4339b 100644
@@ -7467,6 +7467,7 @@ L:	[email protected]
 S:	Supported
 W:	https://rust-for-linux.com
 T:	git git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
+F:	rust/helpers/dma-resv.c
 F:	rust/helpers/dma.c
 F:	rust/helpers/scatterlist.c
 F:	rust/kernel/dma.rs

That's fine for me in general, but maybe the DMA BUFFER SHARING FRAMEWORK is probably the correct place. DMA BUFFER SHARING FRAMEWORK is also under the DRM umbrella, so we should also add it to the drm-rust entry as well.

Sumit, Christian, are you ok with listing this under DMA BUFFER SHARING FRAMEWORK?

Alice

maintainer
#5 lore raw

Re: [PATCH v4 1/6] rust: drm: add base GPUVM immediate mode abstraction

In reply to Alice Ryhl
Quoted 10 lines · Alice Ryhl
Quoted 9 lines · Danilo Krummrich

rust/kernel/drm/gpuvm/mod.rs line 38

Quoted 4 lines · Alice Ryhl
rust/kernel/drm/gpuvm/mod.rs new file +4 -0
+/// A DRM GPU VA manager.
+///
+/// This object is refcounted, but the "core" is only accessible using a special unique handle. The
+/// core consists of the `core` field and the GPUVM's interval tree.

I think this is still a bit confusing, I think we should just rename GpuVmCore to UniqueGpuVm and rewrite this to something like:

Quoted 6 lines
Quoted 2 lines

"The driver specific data of of GpuVm is only accessible through a [UniqueGpuVm], which guarantees exclusive access."

But it's not really the same as e.g. UniqueArc<_>, which implies that there are no normal Arc<_>s whatsoever. This one is just a special ref, but there may also be normal refs at the same time.

Fair enough, what about UniqueRefGpuVm then? I think that's more descriptive than GpuVmCore.

Quoted 17 lines
Quoted 6 lines

rust/kernel/drm/gpuvm/mod.rs line 137

Quoted 4 lines
rust/kernel/drm/gpuvm/mod.rs new file +4 -0
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm) -> &'a Self {
+        // SAFETY: Caller passes a pointer to the `drm_gpuvm` in a `GpuVm<T>`. Caller ensures the
+        // pointer is valid for 'a.
+        unsafe { &*kernel::container_of!(Opaque::cast_from(ptr), Self, vm) }

I'd pull the Opaque::cast_from() call out of the unsafe block.

I think that's a bit verbose, and all existing uses do it inside the container_of!.

Quoted 7 lines
Quoted 1 line

+/// The manager for a GPUVM.

This description seems a bit odd. In the end, the trait makes GPUVM aware of other driver specific types. So, maybe a better name would be gpuvm::DriverAttributes, gpuvm::DriverTypes, gpuvm::DriverInfo or just gpuvm::Driver. My favorite is gpuvm::DriverInfo.

Quoted 6 lines
Quoted 2 lines

We should also change the doc-comment accordingly. Maybe somthing like: "This trait make the [GpuVm] aware of the other driver specific DRM types."

I mean, it doesn't just do that. The type implementing this is the type stored in the core field, so you really do get more than just some type relationships from it.

The only types subsequently added to this trait are VaData and VmBoData, i.e. type relationships.

The trait is not related to the private data type T, i.e. it is theoretically possible to have T for the private data and I: gpuvm::DriverInfo for the type relationships, right?.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
rust/kernel/drm/gpuvm/mod.rs new file +7 -0
+pub trait DriverGpuVm: Sized {
+    /// Parent `Driver` for this object.
+    type Driver: drm::Driver<Object = Self::Object>;
+
+    /// The kind of GEM object stored in this GPUVM.
+    type Object: IntoGEMObject;
+}
maintainer
#10 lore raw

Re: [PATCH v4 3/6] rust: gpuvm: add GpuVm::obtain()

In reply to Alice Ryhl
Quoted 13 lines · Alice Ryhl
rust/kernel/drm/gpuvm/vm_bo.rs new file +13 -0
+/// Represents that a given GEM object has at least one mapping on this [`GpuVm`] instance.
+///
+/// Does not assume that GEM lock is held.
+#[repr(C)]
+#[pin_data]
+pub struct GpuVmBo<T: DriverGpuVm> {
+    #[pin]
+    inner: Opaque<bindings::drm_gpuvm_bo>,
+    #[pin]
+    data: T::VmBoData,
+}
+
+impl<T: DriverGpuVm> GpuVmBo<T> {

rust/kernel/drm/gpuvm/vm_bo.rs line 27

Quoted 10 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +10 -0
+    pub(super) const ALLOC_FN: Option<unsafe extern "C" fn() -> *mut bindings::drm_gpuvm_bo> = {
+        use core::alloc::Layout;
+        let base = Layout::new::<bindings::drm_gpuvm_bo>();
+        let rust = Layout::new::<Self>();
+        assert!(base.size() <= rust.size());
+        if base.size() != rust.size() || base.align() != rust.align() {
+            Some(Self::vm_bo_alloc)
+        } else {
+            // This causes GPUVM to allocate a `GpuVmBo<T>` with `kzalloc(sizeof(drm_gpuvm_bo))`.
+            None

So, if T::VmBoData is a ZST *and* needs drop, we may end up allocating on the C side and freeing on the Rust side.

I assume this is intentional and there is nothing wrong with it, but without a comment it might be a bit subtle.

Another subtlety is that vm_bo_free() and vm_bo_alloc() assume that inner is always the first member. I'd probably add a brief comment why this even has to be the case, i.e. vm_bo_alloc() does not return *mut c_void, but *mut bindings::drm_gpuvm_bo.

Quoted 36 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +36 -0
+        }
+    };
+
+    pub(super) const FREE_FN: Option<unsafe extern "C" fn(*mut bindings::drm_gpuvm_bo)> = {
+        if core::mem::needs_drop::<Self>() {
+            Some(Self::vm_bo_free)
+        } else {
+            // This causes GPUVM to free a `GpuVmBo<T>` with `kfree`.
+            None
+        }
+    };
+
+    /// Custom function for allocating a `drm_gpuvm_bo`.
+    ///
+    /// # Safety
+    ///
+    /// Always safe to call.
+    unsafe extern "C" fn vm_bo_alloc() -> *mut bindings::drm_gpuvm_bo {
+        KBox::<Self>::new_uninit(GFP_KERNEL | __GFP_ZERO)
+            .map(KBox::into_raw)
+            .unwrap_or(ptr::null_mut())
+            .cast()
+    }
+
+    /// Custom function for freeing a `drm_gpuvm_bo`.
+    ///
+    /// # Safety
+    ///
+    /// The pointer must have been allocated with [`GpuVmBo::ALLOC_FN`], and must not be used after
+    /// this call.
+    unsafe extern "C" fn vm_bo_free(ptr: *mut bindings::drm_gpuvm_bo) {
+        // SAFETY:
+        // * The ptr was allocated from kmalloc with the layout of `GpuVmBo<T>`.
+        // * `ptr->inner` has no destructor.
+        // * `ptr->data` contains a valid `T::VmBoData` that we can drop.
+        drop(unsafe { KBox::<Self>::from_raw(ptr.cast()) });

rust/kernel/drm/gpuvm/vm_bo.rs line 73

Quoted 10 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +10 -0
+    }
+
+    /// Access this [`GpuVmBo`] from a raw pointer.
+    ///
+    /// # Safety
+    ///
+    /// For the duration of `'a`, the pointer must reference a valid `drm_gpuvm_bo` associated with
+    /// a [`GpuVm<T>`].
+    #[inline]
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm_bo) -> &'a Self {

I think this a good candidate for crate private, as we don't want drivers to use this, but still use it in other DRM core modules.

rust/kernel/drm/gpuvm/vm_bo.rs line 80

Quoted 7 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +7 -0
+        // SAFETY: `drm_gpuvm_bo` is first field and `repr(C)`.
+        unsafe { &*ptr.cast() }
+    }
+
+    /// Returns a raw pointer to underlying C value.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo {

Less important, but probably also only needed in core DRM code.

Quoted 67 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +67 -0
+        self.inner.get()
+    }
+
+    /// The [`GpuVm`] that this GEM object is mapped in.
+    #[inline]
+    pub fn gpuvm(&self) -> &GpuVm<T> {
+        // SAFETY: The `obj` pointer is guaranteed to be valid.
+        unsafe { GpuVm::<T>::from_raw((*self.inner.get()).vm) }
+    }
+
+    /// The [`drm_gem_object`](crate::gem::Object) for these mappings.
+    #[inline]
+    pub fn obj(&self) -> &T::Object {
+        // SAFETY: The `obj` pointer is guaranteed to be valid.
+        unsafe { <T::Object as IntoGEMObject>::from_raw((*self.inner.get()).obj) }
+    }
+
+    /// The driver data with this buffer object.
+    #[inline]
+    pub fn data(&self) -> &T::VmBoData {
+        &self.data
+    }
+}
+
+/// A pre-allocated [`GpuVmBo`] object.
+///
+/// # Invariants
+///
+/// Points at a `drm_gpuvm_bo` that contains a valid `T::VmBoData`, has a refcount of one, and is
+/// absent from any gem, extobj, or evict lists.
+pub(super) struct GpuVmBoAlloc<T: DriverGpuVm>(NonNull<GpuVmBo<T>>);
+
+impl<T: DriverGpuVm> GpuVmBoAlloc<T> {
+    /// Create a new pre-allocated [`GpuVmBo`].
+    ///
+    /// It's intentional that the initializer is infallible because `drm_gpuvm_bo_put` will call
+    /// drop on the data, so we don't have a way to free it when the data is missing.
+    #[inline]
+    pub(super) fn new(
+        gpuvm: &GpuVm<T>,
+        gem: &T::Object,
+        value: impl PinInit<T::VmBoData>,
+    ) -> Result<GpuVmBoAlloc<T>, AllocError> {
+        // CAST: `GpuVmBoAlloc::vm_bo_alloc` ensures that this memory was allocated with the layout
+        // of `GpuVmBo<T>`. The type is repr(C), so `container_of` is not required.
+        // SAFETY: The provided gpuvm and gem ptrs are valid for the duration of this call.
+        let raw_ptr = unsafe {
+            bindings::drm_gpuvm_bo_create(gpuvm.as_raw(), gem.as_raw()).cast::<GpuVmBo<T>>()
+        };
+        let ptr = NonNull::new(raw_ptr).ok_or(AllocError)?;
+        // SAFETY: `ptr->data` is a valid pinned location.
+        let Ok(()) = unsafe { value.__pinned_init(&raw mut (*raw_ptr).data) };
+        // INVARIANTS: We just created the vm_bo so it's absent from lists, and the data is valid
+        // as we just initialized it.
+        Ok(GpuVmBoAlloc(ptr))
+    }
+
+    /// Returns a raw pointer to underlying C value.
+    #[inline]
+    pub(super) fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo {
+        // SAFETY: The pointer references a valid `drm_gpuvm_bo`.
+        unsafe { (*self.0.as_ptr()).inner.get() }
+    }
+
+    /// Look up whether there is an existing [`GpuVmBo`] for this gem object.
+    #[inline]
+    pub(super) fn obtain(self) -> GpuVmBoRegistered<T> {

rust/kernel/drm/gpuvm/vm_bo.rs line 157

Quoted 10 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +10 -0
+        let me = ManuallyDrop::new(self);
+        // SAFETY: Valid `drm_gpuvm_bo` not already in the lists.
+        let ptr = unsafe { bindings::drm_gpuvm_bo_obtain_prealloc(me.as_raw()) };
+
+        // Add the vm_bo to the extobj list if it's an external object, and if the vm_bo does not
+        // already exist. (If we are using an existing vm_bo, it's already in the extobj list.)
+        if ptr::eq(ptr, me.as_raw()) && me.gpuvm().is_extobj(me.obj()) {
+            let resv_lock = me.gpuvm().raw_resv();
+            // SAFETY: The GPUVM is still alive, so its resv lock is too.
+            unsafe { bindings::dma_resv_lock(resv_lock, ptr::null_mut()) };

Maybe add a TODO to replace this with a proper lock guard once available?

Quoted 26 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +26 -0
+            // SAFETY: We hold the GPUVMs resv lock.
+            unsafe { bindings::drm_gpuvm_bo_extobj_add(ptr) };
+            // SAFETY: We took the lock, so we can unlock it.
+            unsafe { bindings::dma_resv_unlock(resv_lock) };
+        }
+
+        // INVARIANTS: Valid `drm_gpuvm_bo` in the GEM list.
+        // SAFETY: `drm_gpuvm_bo_obtain_prealloc` always returns a non-null ptr
+        GpuVmBoRegistered(unsafe { NonNull::new_unchecked(ptr.cast()) })
+    }
+}
+
+impl<T: DriverGpuVm> Deref for GpuVmBoAlloc<T> {
+    type Target = GpuVmBo<T>;
+    #[inline]
+    fn deref(&self) -> &GpuVmBo<T> {
+        // SAFETY: By the type invariants we may deref while `Self` exists.
+        unsafe { self.0.as_ref() }
+    }
+}
+
+impl<T: DriverGpuVm> Drop for GpuVmBoAlloc<T> {
+    #[inline]
+    fn drop(&mut self) {
+        // TODO: Call drm_gpuvm_bo_destroy_not_in_lists() directly.
+        // SAFETY: It's safe to perform a deferred put in any context.

rust/kernel/drm/gpuvm/vm_bo.rs line 193

Quoted 10 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +10 -0
+        unsafe { bindings::drm_gpuvm_bo_put_deferred(self.as_raw()) };
+    }
+}
+
+/// A [`GpuVmBo`] object in the GEM list.
+///
+/// # Invariants
+///
+/// Points at a `drm_gpuvm_bo` that contains a valid `T::VmBoData` and is present in the gem list.
+pub struct GpuVmBoRegistered<T: DriverGpuVm>(NonNull<GpuVmBo<T>>);

I know that I proposed to rename this from GpuVmBoResident to GpuVmBoRegistered in a drive-by comment on v3.

But now that I have a closer look, I think it would be nice to just have GpuVmBo being the registered one and GpuVmBoAlloc being the pre-allocated one.

As it is currently, I think it is bad to ever present a &GpuVmBo to a driver because it implies that we don't know whether it is a pre-allocated one or a "normal", registered one. But we do always know.

For instance, in patch 6 we give out &'op GpuVmBo<T>, but it actually carries the invariant of being registered.

Of course, we could fix this by giving out a &'op GpuVmBoRegistered<T> instead, but it would be nice to not have drivers be in touch with a type that can be one or the other.

I know that the current GpuVmBo<T> also serves the purpose of storing common code. Maybe we can make it private, call it GpuVmBoInner<T> and have inline forwarding methods for GpuVmBo<T> and GpuVmBoAlloc<T>. This is slightly more overhead in this implementation due to the forwarding methods, but less ambiguity for drivers, which I think is more important.

Quoted 25 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +25 -0
+impl<T: DriverGpuVm> GpuVmBoRegistered<T> {
+    /// Returns a raw pointer to underlying C value.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo {
+        // SAFETY: The pointer references a valid `drm_gpuvm_bo`.
+        unsafe { (*self.0.as_ptr()).inner.get() }
+    }
+}
+
+impl<T: DriverGpuVm> Deref for GpuVmBoRegistered<T> {
+    type Target = GpuVmBo<T>;
+    #[inline]
+    fn deref(&self) -> &GpuVmBo<T> {
+        // SAFETY: By the type invariants we may deref while `Self` exists.
+        unsafe { self.0.as_ref() }
+    }
+}
+
+impl<T: DriverGpuVm> Drop for GpuVmBoRegistered<T> {
+    #[inline]
+    fn drop(&mut self) {
+        // SAFETY: It's safe to perform a deferred put in any context.
+        unsafe { bindings::drm_gpuvm_bo_put_deferred(self.as_raw()) };
+    }
+}
author maintainer
#11 lore raw

Re: [PATCH v4 3/6] rust: gpuvm: add GpuVm::obtain()

Quoted 28 lines · Danilo Krummrich
Quoted 13 lines · Alice Ryhl
rust/kernel/drm/gpuvm/vm_bo.rs new file +13 -0
+/// Represents that a given GEM object has at least one mapping on this [`GpuVm`] instance.
+///
+/// Does not assume that GEM lock is held.
+#[repr(C)]
+#[pin_data]
+pub struct GpuVmBo<T: DriverGpuVm> {
+    #[pin]
+    inner: Opaque<bindings::drm_gpuvm_bo>,
+    #[pin]
+    data: T::VmBoData,
+}
+
+impl<T: DriverGpuVm> GpuVmBo<T> {

rust/kernel/drm/gpuvm/vm_bo.rs line 27

Quoted 10 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +10 -0
+    pub(super) const ALLOC_FN: Option<unsafe extern "C" fn() -> *mut bindings::drm_gpuvm_bo> = {
+        use core::alloc::Layout;
+        let base = Layout::new::<bindings::drm_gpuvm_bo>();
+        let rust = Layout::new::<Self>();
+        assert!(base.size() <= rust.size());
+        if base.size() != rust.size() || base.align() != rust.align() {
+            Some(Self::vm_bo_alloc)
+        } else {
+            // This causes GPUVM to allocate a `GpuVmBo<T>` with `kzalloc(sizeof(drm_gpuvm_bo))`.
+            None

So, if T::VmBoData is a ZST *and* needs drop, we may end up allocating on the C side and freeing on the Rust side.

Quoted 2 lines

I assume this is intentional and there is nothing wrong with it, but without a comment it might be a bit subtle.

Yeah it's subtle but correct.

Quoted 4 lines

Another subtlety is that vm_bo_free() and vm_bo_alloc() assume that inner is always the first member. I'd probably add a brief comment why this even has to be the case, i.e. vm_bo_alloc() does not return *mut c_void, but *mut bindings::drm_gpuvm_bo.

I will add comments.

Quoted 20 lines

rust/kernel/drm/gpuvm/vm_bo.rs line 73

Quoted 8 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +8 -0
+    /// Access this [`GpuVmBo`] from a raw pointer.
+    ///
+    /// # Safety
+    ///
+    /// For the duration of `'a`, the pointer must reference a valid `drm_gpuvm_bo` associated with
+    /// a [`GpuVm<T>`].
+    #[inline]
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm_bo) -> &'a Self {

I think this a good candidate for crate private, as we don't want drivers to use this, but still use it in other DRM core modules.

rust/kernel/drm/gpuvm/vm_bo.rs line 80

Quoted 7 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +7 -0
+        // SAFETY: `drm_gpuvm_bo` is first field and `repr(C)`.
+        unsafe { &*ptr.cast() }
+    }
+
+    /// Returns a raw pointer to underlying C value.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo {
Quoted 1 line

Less important, but probably also only needed in core DRM code.

For cases like these two, I do think one can run into cases where you want them to be public. E.g. the vma confusion bugfix uses a raw pointer for now:
https://lore.kernel.org/all/[email protected]/

I'm generally not so worried about methods like these being public because they can't be used without unsafe.

Quoted 14 lines

rust/kernel/drm/gpuvm/vm_bo.rs line 157

Quoted 13 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +13 -0
+    /// Look up whether there is an existing [`GpuVmBo`] for this gem object.
+    #[inline]
+    pub(super) fn obtain(self) -> GpuVmBoRegistered<T> {
+        let me = ManuallyDrop::new(self);
+        // SAFETY: Valid `drm_gpuvm_bo` not already in the lists.
+        let ptr = unsafe { bindings::drm_gpuvm_bo_obtain_prealloc(me.as_raw()) };
+
+        // Add the vm_bo to the extobj list if it's an external object, and if the vm_bo does not
+        // already exist. (If we are using an existing vm_bo, it's already in the extobj list.)
+        if ptr::eq(ptr, me.as_raw()) && me.gpuvm().is_extobj(me.obj()) {
+            let resv_lock = me.gpuvm().raw_resv();
+            // SAFETY: The GPUVM is still alive, so its resv lock is too.
+            unsafe { bindings::dma_resv_lock(resv_lock, ptr::null_mut()) };
Quoted 1 line

Maybe add a TODO to replace this with a proper lock guard once available?

Ok.

Quoted 10 lines

rust/kernel/drm/gpuvm/vm_bo.rs line 193

Quoted 6 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +6 -0
+/// A [`GpuVmBo`] object in the GEM list.
+///
+/// # Invariants
+///
+/// Points at a `drm_gpuvm_bo` that contains a valid `T::VmBoData` and is present in the gem list.
+pub struct GpuVmBoRegistered<T: DriverGpuVm>(NonNull<GpuVmBo<T>>);

I know that I proposed to rename this from GpuVmBoResident to GpuVmBoRegistered in a drive-by comment on v3.

Quoted 6 lines

But now that I have a closer look, I think it would be nice to just have GpuVmBo being the registered one and GpuVmBoAlloc being the pre-allocated one.

As it is currently, I think it is bad to ever present a &GpuVmBo to a driver because it implies that we don't know whether it is a pre-allocated one or a "normal", registered one. But we do always know.

Actually, I think GpuVmBo is already the registered one. GpuVmBoRegistered is just ARef<GpuVmBo<T>>.

Quoted 7 lines

For instance, in patch 6 we give out &'op GpuVmBo<T>, but it actually carries the invariant of being registered.

Of course, we could fix this by giving out a &'op GpuVmBoRegistered<T> instead, but it would be nice to not have drivers be in touch with a type that can be one or the other.

Quoted 5 lines

I know that the current GpuVmBo<T> also serves the purpose of storing common code. Maybe we can make it private, call it GpuVmBoInner<T> and have inline forwarding methods for GpuVmBo<T> and GpuVmBoAlloc<T>. This is slightly more overhead in this implementation due to the forwarding methods, but less ambiguity for drivers, which I think is more important.

I think we should keep the current state that GpuVmBo is registered, and only GpuVmBoAlloc is not. That is most useful.

Alice

maintainer
#12 lore raw

Re: [PATCH v4 3/6] rust: gpuvm: add GpuVm::obtain()

Quoted 20 lines · Alice Ryhl
Quoted 20 lines

rust/kernel/drm/gpuvm/vm_bo.rs line 73

Quoted 8 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +8 -0
+    /// Access this [`GpuVmBo`] from a raw pointer.
+    ///
+    /// # Safety
+    ///
+    /// For the duration of `'a`, the pointer must reference a valid `drm_gpuvm_bo` associated with
+    /// a [`GpuVm<T>`].
+    #[inline]
+    pub unsafe fn from_raw<'a>(ptr: *mut bindings::drm_gpuvm_bo) -> &'a Self {

I think this a good candidate for crate private, as we don't want drivers to use this, but still use it in other DRM core modules.

Quoted 7 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +7 -0
+        // SAFETY: `drm_gpuvm_bo` is first field and `repr(C)`.
+        unsafe { &*ptr.cast() }
+    }
+
+    /// Returns a raw pointer to underlying C value.
+    #[inline]
+    pub fn as_raw(&self) -> *mut bindings::drm_gpuvm_bo {
Quoted 6 lines
Quoted 1 line

Less important, but probably also only needed in core DRM code.

For cases like these two, I do think one can run into cases where you want them to be public. E.g. the vma confusion bugfix uses a raw pointer for now:
https://lore.kernel.org/all/[email protected]/

I think we should make them public once actually needed.

Quoted 2 lines

I'm generally not so worried about methods like these being public because they can't be used without unsafe.

Yeah, but my experience is that drivers can get very creative in figuring out how to abuse APIs. I think it's best to keep the surface for this as small as possible.

Quoted 13 lines
Quoted 13 lines

rust/kernel/drm/gpuvm/vm_bo.rs line 193

Quoted 6 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +6 -0
+/// A [`GpuVmBo`] object in the GEM list.
+///
+/// # Invariants
+///
+/// Points at a `drm_gpuvm_bo` that contains a valid `T::VmBoData` and is present in the gem list.
+pub struct GpuVmBoRegistered<T: DriverGpuVm>(NonNull<GpuVmBo<T>>);

I know that I proposed to rename this from GpuVmBoResident to GpuVmBoRegistered in a drive-by comment on v3.

But now that I have a closer look, I think it would be nice to just have GpuVmBo being the registered one and GpuVmBoAlloc being the pre-allocated one.

Quoted 6 lines
Quoted 3 lines

As it is currently, I think it is bad to ever present a &GpuVmBo to a driver because it implies that we don't know whether it is a pre-allocated one or a "normal", registered one. But we do always know.

Actually, I think GpuVmBo is already the registered one. GpuVmBoRegistered is just ARef<GpuVmBo<T>>.

GpuVmBoAlloc<T> dereferences to GpuVmBo<T>, so currently it is not.

Quoted 7 lines
Quoted 7 lines

For instance, in patch 6 we give out &'op GpuVmBo<T>, but it actually carries the invariant of being registered.

Of course, we could fix this by giving out a &'op GpuVmBoRegistered<T> instead, but it would be nice to not have drivers be in touch with a type that can be one or the other.

Quoted 8 lines
Quoted 5 lines

I know that the current GpuVmBo<T> also serves the purpose of storing common code. Maybe we can make it private, call it GpuVmBoInner<T> and have inline forwarding methods for GpuVmBo<T> and GpuVmBoAlloc<T>. This is slightly more overhead in this implementation due to the forwarding methods, but less ambiguity for drivers, which I think is more important.

I think we should keep the current state that GpuVmBo is registered, and only GpuVmBoAlloc is not. That is most useful.

We seem to agree then: What I want is that from a driver perspective there is only GpuVmBo<T> (which is the registered thing) and GpuVmBoAlloc<T> which is the pre-allocated thing, i.e. no separate GpuVmBoRegistered<T> type.

author maintainer
#13 lore raw

Re: [PATCH v4 3/6] rust: gpuvm: add GpuVm::obtain()

Quoted 14 lines · Danilo Krummrich
Quoted 13 lines · Alice Ryhl
Quoted 13 lines

rust/kernel/drm/gpuvm/vm_bo.rs line 193

Quoted 6 lines
rust/kernel/drm/gpuvm/vm_bo.rs new file +6 -0
+/// A [`GpuVmBo`] object in the GEM list.
+///
+/// # Invariants
+///
+/// Points at a `drm_gpuvm_bo` that contains a valid `T::VmBoData` and is present in the gem list.
+pub struct GpuVmBoRegistered<T: DriverGpuVm>(NonNull<GpuVmBo<T>>);

I know that I proposed to rename this from GpuVmBoResident to GpuVmBoRegistered in a drive-by comment on v3.

But now that I have a closer look, I think it would be nice to just have GpuVmBo being the registered one and GpuVmBoAlloc being the pre-allocated one.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

As it is currently, I think it is bad to ever present a &GpuVmBo to a driver because it implies that we don't know whether it is a pre-allocated one or a "normal", registered one. But we do always know.

Actually, I think GpuVmBo is already the registered one. GpuVmBoRegistered is just ARef<GpuVmBo<T>>.

GpuVmBoAlloc<T> dereferences to GpuVmBo<T>, so currently it is not.

I will drop the Deref impl.

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

For instance, in patch 6 we give out &'op GpuVmBo<T>, but it actually carries the invariant of being registered.

Of course, we could fix this by giving out a &'op GpuVmBoRegistered<T> instead, but it would be nice to not have drivers be in touch with a type that can be one or the other.

I know that the current GpuVmBo<T> also serves the purpose of storing common code. Maybe we can make it private, call it GpuVmBoInner<T> and have inline forwarding methods for GpuVmBo<T> and GpuVmBoAlloc<T>. This is slightly more overhead in this implementation due to the forwarding methods, but less ambiguity for drivers, which I think is more important.

Quoted 6 lines
Quoted 2 lines

I think we should keep the current state that GpuVmBo is registered, and only GpuVmBoAlloc is not. That is most useful.

We seem to agree then: What I want is that from a driver perspective there is only GpuVmBo<T> (which is the registered thing) and GpuVmBoAlloc<T> which is the pre-allocated thing, i.e. no separate GpuVmBoRegistered<T> type.

So, should we get rid of GpuVmBoRegistered in favor of ARef<GpuVm<T>>?

Alice

maintainer
#14 lore raw

Re: [PATCH v4 3/6] rust: gpuvm: add GpuVm::obtain()

Quoted 1 line · Alice Ryhl

So, should we get rid of GpuVmBoRegistered in favor of ARef<GpuVm<T>>?

I wanted to avoid exposing the reference count, as I suspect drivers might not need it in Rust, but I don't know for sure.

We could also define it as GpuVmBo<T>(ARef<GpuVmBoInner<T>>), where GpuVmBoInner<T> is private, but I also don't want you to go back and forth with this in case it turns out we do need drivers to be able to take a reference count and I also don't think it hurts too much exposing the reference count, even if not needed.

So, either is fine with me, ARef<GpuVmBo<T>> or
GpuVmBo<T>(ARef<GpuVmBoInner<T>>).

author maintainer
#15 lore raw

Re: [PATCH v4 3/6] rust: gpuvm: add GpuVm::obtain()

Quoted 6 lines · Danilo Krummrich
Quoted 1 line · Alice Ryhl

So, should we get rid of GpuVmBoRegistered in favor of ARef<GpuVm<T>>?

I wanted to avoid exposing the reference count, as I suspect drivers might not need it in Rust, but I don't know for sure.

Quoted 8 lines

We could also define it as GpuVmBo<T>(ARef<GpuVmBoInner<T>>), where GpuVmBoInner<T> is private, but I also don't want you to go back and forth with this in case it turns out we do need drivers to be able to take a reference count and I also don't think it hurts too much exposing the reference count, even if not needed.

So, either is fine with me, ARef<GpuVmBo<T>> or
GpuVmBo<T>(ARef<GpuVmBoInner<T>>).

I don't think GpuVmBo<T>(ARef<GpuVmBoInner<T>>) works because we need to be able to talk about both ARef<GpuVmBoInner<T>> and &GpuVmBoInner<T>. The reference type is returned by several different APIs, so the Inner type can't be private.

Alice

maintainer
#16 lore raw

Re: [PATCH v4 3/6] rust: gpuvm: add GpuVm::obtain()

Quoted 13 lines · Alice Ryhl
Quoted 12 lines · Danilo Krummrich
Quoted 1 line · Alice Ryhl

So, should we get rid of GpuVmBoRegistered in favor of ARef<GpuVm<T>>?

I wanted to avoid exposing the reference count, as I suspect drivers might not need it in Rust, but I don't know for sure.

We could also define it as GpuVmBo<T>(ARef<GpuVmBoInner<T>>), where GpuVmBoInner<T> is private, but I also don't want you to go back and forth with this in case it turns out we do need drivers to be able to take a reference count and I also don't think it hurts too much exposing the reference count, even if not needed.

Quoted 7 lines
Quoted 2 lines

So, either is fine with me, ARef<GpuVmBo<T>> or
GpuVmBo<T>(ARef<GpuVmBoInner<T>>).

I don't think GpuVmBo<T>(ARef<GpuVmBoInner<T>>) works because we need to be able to talk about both ARef<GpuVmBoInner<T>> and &GpuVmBoInner<T>. The reference type is returned by several different APIs, so the Inner type can't be private.

I meant NonNull<GpuVmBoInner<T>>, analogous to the current GpuVmBoRegistered.

author maintainer
#17 lore raw

Re: [PATCH v4 3/6] rust: gpuvm: add GpuVm::obtain()

Quoted 17 lines · Danilo Krummrich
Quoted 16 lines · Alice Ryhl
Quoted 14 lines · Danilo Krummrich
Quoted 1 line · Alice Ryhl

So, should we get rid of GpuVmBoRegistered in favor of ARef<GpuVm<T>>?

I wanted to avoid exposing the reference count, as I suspect drivers might not need it in Rust, but I don't know for sure.

We could also define it as GpuVmBo<T>(ARef<GpuVmBoInner<T>>), where GpuVmBoInner<T> is private, but I also don't want you to go back and forth with this in case it turns out we do need drivers to be able to take a reference count and I also don't think it hurts too much exposing the reference count, even if not needed.

So, either is fine with me, ARef<GpuVmBo<T>> or
GpuVmBo<T>(ARef<GpuVmBoInner<T>>).

Quoted 6 lines
Quoted 4 lines

I don't think GpuVmBo<T>(ARef<GpuVmBoInner<T>>) works because we need to be able to talk about both ARef<GpuVmBoInner<T>> and &GpuVmBoInner<T>. The reference type is returned by several different APIs, so the Inner type can't be private.

I meant NonNull<GpuVmBoInner<T>>, analogous to the current GpuVmBoRegistered.

Not sure what you mean.

Ultimately there is GpuVmBo<T> which is just an Opaque wrapper. Around that, we need two pointer types: &_ and owned. The owned one can be ARef<_>, or it can be a custom pointer type like the patch has right now. Or maybe it could be Owned<_>, but it's honestly not a great match as it's not really unique.

Alice

maintainer
#18 lore raw

Re: [PATCH v4 3/6] rust: gpuvm: add GpuVm::obtain()

Quoted 18 lines · Alice Ryhl
Quoted 17 lines · Danilo Krummrich
Quoted 16 lines · Alice Ryhl
Quoted 14 lines · Danilo Krummrich
Quoted 1 line · Alice Ryhl

So, should we get rid of GpuVmBoRegistered in favor of ARef<GpuVm<T>>?

I wanted to avoid exposing the reference count, as I suspect drivers might not need it in Rust, but I don't know for sure.

We could also define it as GpuVmBo<T>(ARef<GpuVmBoInner<T>>), where GpuVmBoInner<T> is private, but I also don't want you to go back and forth with this in case it turns out we do need drivers to be able to take a reference count and I also don't think it hurts too much exposing the reference count, even if not needed.

So, either is fine with me, ARef<GpuVmBo<T>> or
GpuVmBo<T>(ARef<GpuVmBoInner<T>>).

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

I don't think GpuVmBo<T>(ARef<GpuVmBoInner<T>>) works because we need to be able to talk about both ARef<GpuVmBoInner<T>> and &GpuVmBoInner<T>. The reference type is returned by several different APIs, so the Inner type can't be private.

I meant NonNull<GpuVmBoInner<T>>, analogous to the current GpuVmBoRegistered.

Not sure what you mean.

What I meant was doing what you currently have, with the following changes:

- Rename GpuVmBoRegistered<T> to GpuVmBo<T>.
- Rename GpuVmBo<T> to GpuVmBoInner<T> and make it private.

The downside is that both GpuVmBo<T> and GpuVmBoAlloc<T> can't dereference to GpuVmBoInner<T>, so we'd need some forwarding methods.

The advantage is that we can hide the reference count and keep it simple with only two driver types, GpuVmBo<T> and GpuVmBoAlloc<T>.

But I'm also fine with just ARef<GpuVmBo<T>> and GpuVmBoAlloc<T>.