[PATCH] rust: drm: gpuvm: store the driver data in UniqueRefGpuVm

From: Daniel Almeida

Date: Thu Nov 26 2026 - 15:47:22 EST


GpuVm<T> keeps the driver data in an UnsafeCell<T> inside the
refcounted allocation, and UniqueRefGpuVm hands it out through unsafe
accessors. Only UniqueRefGpuVm accesses the data, so it does not need
to live in the shared allocation.

Move the data into UniqueRefGpuVm, which turns data() and data_mut()
into plain field accesses. GpuVm<T> keeps a PhantomData<*mut T> so that
it stays invariant in T without the UnsafeCell.

The data is now dropped with UniqueRefGpuVm rather than when the last
reference to the GpuVm goes away, and UniqueRefGpuVm is Unpin only if T
is.

Suggested-by: Matthew Leach <matthew.leach@xxxxxxxxxxxxx>
Suggested-by: Deborah Brouwer <deborah.brouwer@xxxxxxxxxxxxx>
Signed-off-by: Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>
---
rust/kernel/drm/gpuvm/mod.rs | 35 ++++++++++++++++-------------------
1 file changed, 16 insertions(+), 19 deletions(-)

diff --git a/rust/kernel/drm/gpuvm/mod.rs b/rust/kernel/drm/gpuvm/mod.rs
index 23e73da865fb..d3b11e2a39d5 100644
--- a/rust/kernel/drm/gpuvm/mod.rs
+++ b/rust/kernel/drm/gpuvm/mod.rs
@@ -28,7 +28,6 @@
};

use core::{
- cell::UnsafeCell,
marker::PhantomData,
mem::{
ManuallyDrop,
@@ -62,22 +61,21 @@
/// # Invariants
///
/// * Stored in an allocation managed by the refcount in `self.vm`.
-/// * Access to `data` and the gpuvm interval tree is controlled via the [`UniqueRefGpuVm`] type.
+/// * Access to the gpuvm interval tree is controlled via the [`UniqueRefGpuVm`] type.
/// * Does not contain any sparse [`GpuVa<T>`] instances.
#[pin_data]
pub struct GpuVm<T: DriverGpuVm> {
#[pin]
vm: Opaque<bindings::drm_gpuvm>,
- /// Accessed only through the [`UniqueRefGpuVm`] reference.
- data: UnsafeCell<T>,
+ _invariant: PhantomData<*mut T>,
}

// SAFETY: It is safe to send a `GpuVm<T>` to another thread: all data reachable through it
-// (`T`, `T::VmBoData`, and the GEM `T::Object`) is `Send` by the `DriverGpuVm` bounds.
+// (`T::VmBoData` and the GEM `T::Object`) is `Send` by the `DriverGpuVm` bounds.
unsafe impl<T: DriverGpuVm> Send for GpuVm<T> {}
// SAFETY: It is safe to share a `&GpuVm<T>` between threads: `&self` methods only alias data
-// that is `Sync` by the `DriverGpuVm` bounds, and any thread may drop that data, or upgrade the
-// reference and ultimately drop `T`, which the same bounds make `Send`.
+// that is `Sync` by the `DriverGpuVm` bounds, and any thread may drop that data, which the same
+// bounds make `Send`.
unsafe impl<T: DriverGpuVm> Sync for GpuVm<T> {}

// SAFETY: By type invariants, the allocation is managed by the refcount in `self.vm`.
@@ -132,7 +130,7 @@ pub fn new<E, Ctx: drm::DeviceContext>(
{
let obj = KBox::try_pin_init::<E>(
try_pin_init!(Self {
- data: UnsafeCell::new(data),
+ _invariant: PhantomData,
vm <- Opaque::ffi_init(|vm| {
// SAFETY: These arguments are valid. `vm` is valid until refcount drops to
// zero. The `vm` is zeroed before calling this method by `__GFP_ZERO` flag
@@ -163,7 +161,7 @@ pub fn new<E, Ctx: drm::DeviceContext>(
)))
};
// INVARIANT: This reference is unique.
- Ok(UniqueRefGpuVm(aref))
+ Ok(UniqueRefGpuVm { vm: aref, data })
}

/// Access this [`GpuVm`] from a raw pointer.
@@ -298,29 +296,28 @@ fn sm_step_remap<'op, 'ctx>(

/// The core of the DRM GPU VA manager.
///
-/// This object is a unique reference to the VM that can access the interval tree and the Rust
-/// `data` field.
+/// This object is a unique reference to the VM that can access the interval tree. It also owns
+/// the Rust `data` field.
///
/// # Invariants
///
/// Each `GpuVm` instance has at most one `UniqueRefGpuVm` reference.
-// `Send`/`Sync` derive from `ARef<GpuVm<T>>`; the trait bounds make them correct for the unique
-// handle's `&mut T` access.
-pub struct UniqueRefGpuVm<T: DriverGpuVm>(ARef<GpuVm<T>>);
+pub struct UniqueRefGpuVm<T: DriverGpuVm> {
+ vm: ARef<GpuVm<T>>,
+ data: T,
+}

impl<T: DriverGpuVm> UniqueRefGpuVm<T> {
/// Access the data owned by this `UniqueRefGpuVm` immutably.
#[inline]
pub fn data(&self) -> &T {
- // SAFETY: By the type invariants we may access `data`.
- unsafe { &*self.0.data.get() }
+ &self.data
}

/// Access the data owned by this `UniqueRefGpuVm` mutably.
#[inline]
pub fn data_mut(&mut self) -> &mut T {
- // SAFETY: By the type invariants we may access `data`.
- unsafe { &mut *self.0.data.get() }
+ &mut self.data
}
}

@@ -329,6 +326,6 @@ impl<T: DriverGpuVm> Deref for UniqueRefGpuVm<T> {

#[inline]
fn deref(&self) -> &GpuVm<T> {
- &self.0
+ &self.vm
}
}

---
base-commit: 94c9ad2fd7a15f58782c50e471397d1efe6b6363
change-id: 20261126-gpuvm-data-in-handle-cb4041a661e6

Best regards,
--
Daniel Almeida <daniel.almeida@xxxxxxxxxxxxx>