From: Alexandre Courbot <[email protected]>

Share the memory manager through `SharedGpuMm`, which hides the mutex's
invariant resource lifetime behind an existential lifetime so that it
and its users remain covariant, like `Cmdq` does.

Signed-off-by: Alexandre Courbot <[email protected]>
Signed-off-by: Alok Kumar <[email protected]>
---
 drivers/gpu/nova-core/mm.rs          | 31 +++++++++++++++++++++++++++-
 drivers/gpu/nova-core/mm/bar_user.rs |  9 ++++----
 2 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/drivers/gpu/nova-core/mm.rs b/drivers/gpu/nova-core/mm.rs
index a0aeb96bfa36..d4cba022ccad 100644
--- a/drivers/gpu/nova-core/mm.rs
+++ b/drivers/gpu/nova-core/mm.rs
@@ -44,13 +44,15 @@ macro_rules! impl_pfn_bounded {
         GpuBuddy,
         GpuBuddyParams, //
     },
+    new_mutex,
     num::Bounded,
     prelude::*,
     ptr::{
         Alignable,
         Alignment, //
     },
-    sizes::SZ_4K, //
+    sizes::SZ_4K,
+    sync::Mutex, //
 };
 
 use crate::{
@@ -117,6 +119,33 @@ pub(crate) fn tlb(&self) -> &Tlb<'gpu> {
     }
 }
 
+/// A [`GpuMm`] shared between several users, serialized by a mutex.
+#[pin_data]
+pub(crate) struct SharedGpuMm<'gpu>
+where
+    // Hide the mutex's invariant resource lifetime so `SharedGpuMm` remains 
covariant.
+    exists<'res>: 'gpu,
+{
+    #[pin]
+    #[uses('res: invariant)]
+    inner: Mutex<GpuMm<'res>>,
+}
+
+impl<'gpu> SharedGpuMm<'gpu> {
+    /// Creates a pin-initializer sharing `mm`.
+    #[expect(dead_code)]
+    pub(crate) fn new(mm: GpuMm<'gpu>) -> impl PinInit<Self> + 'gpu {
+        pin_init!(Self {
+            inner <- new_mutex!(mm, "nova-core::gpu-mm"),
+        })
+    }
+
+    /// Runs `f` with the memory manager locked.
+    pub(crate) fn with_locked<R>(&self, f: impl FnOnce(&mut GpuMm<'_>) -> R) 
-> R {
+        self.with_inner(|inner| f(&mut inner.lock()))
+    }
+}
+
 /// Page size in bytes (4 KiB).
 pub(crate) const PAGE_SIZE: usize = SZ_4K;
 
diff --git a/drivers/gpu/nova-core/mm/bar_user.rs 
b/drivers/gpu/nova-core/mm/bar_user.rs
index 64c63cf2e977..b0d073b4f074 100644
--- a/drivers/gpu/nova-core/mm/bar_user.rs
+++ b/drivers/gpu/nova-core/mm/bar_user.rs
@@ -26,6 +26,7 @@
         vram::VramRegion,
         GpuMm,
         Pfn,
+        SharedGpuMm,
         Vfn,
         VirtualAddress,
         VramAddress,
@@ -178,7 +179,7 @@ fn drop(&mut self) {
 pub(crate) struct BarMapping<'pool, 'map, 'gpu> {
     /// Mapping of `region`.
     access: BarUserAccess<'map, 'gpu>,
-    mm: &'map Mutex<GpuMm<'gpu>>,
+    mm: &'map SharedGpuMm<'gpu>,
     /// The VRAM region that is mapped.
     region: VramRegion<'pool>,
     unmap_error: Option<Error>,
@@ -189,7 +190,7 @@ impl<'pool, 'map, 'gpu> BarMapping<'pool, 'map, 'gpu> {
     /// Maps `region`, which must start and end on a page boundary.
     pub(crate) fn new(
         bar_user: &'map BarUser<'gpu>,
-        mm: &'map Mutex<GpuMm<'gpu>>,
+        mm: &'map SharedGpuMm<'gpu>,
         region: VramRegion<'pool>,
         writable: bool,
     ) -> Result<Self> {
@@ -210,7 +211,7 @@ pub(crate) fn new(
             pfns.push(Pfn::from(VramAddress::from_raw(address)), GFP_KERNEL)?;
         }
 
-        let access = bar_user.map(&mut mm.lock(), &pfns, writable)?;
+        let access = mm.with_locked(|mm| bar_user.map(mm, &pfns, writable))?;
 
         Ok(Self {
             access,
@@ -250,7 +251,7 @@ pub(crate) fn unmap(&mut self) -> Result {
         if let Some(error) = self.unmap_error {
             return Err(error);
         }
-        let result = self.access.unmap(&mut self.mm.lock());
+        let result = self.mm.with_locked(|mm| self.access.unmap(mm));
         if let Err(error) = result {
             self.unmap_error = Some(error);
         }

Reply via email to