mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Daniel Almeida <daniel.almeida@collabora.com>
To: "Danilo Krummrich" <dakr@kernel.org>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>
Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
	 linux-kernel@vger.kernel.org,
	Matthew Leach <matthew.leach@collabora.com>,
	 Deborah Brouwer <deborah.brouwer@collabora.com>,
	 Daniel Almeida <daniel.almeida@collabora.com>
Subject: [PATCH] rust: drm: gpuvm: store the driver data in UniqueRefGpuVm
Date: Thu, 26 Nov 2026 17:44:40 -0300	[thread overview]
Message-ID: <20261126-gpuvm-data-in-handle-v1-1-47b1e9928f20@collabora.com> (raw)

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@collabora.com>
Suggested-by: Deborah Brouwer <deborah.brouwer@collabora.com>
Signed-off-by: Daniel Almeida <daniel.almeida@collabora.com>
---
 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@collabora.com>


                 reply	other threads:[~2026-11-26 20:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126-gpuvm-data-in-handle-v1-1-47b1e9928f20@collabora.com \
    --to=daniel.almeida@collabora.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=deborah.brouwer@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=matthew.brost@intel.com \
    --cc=matthew.leach@collabora.com \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tamird@kernel.org \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®