On Fri Nov 27, 2026 at 6:36 AM JST, Danilo Krummrich wrote:
> PRAMIN window selection and access must be serialized so that the window
> cannot move while it is in use. Give PRAMIN its own synchronization,
> independent of its GpuMm container, so sharing the memory manager does
> not require serializing unrelated VRAM allocation and TLB operations.
>
> Keep the lock across page table walks and update batches to avoid
> acquiring it separately for every entry.
>
> Signed-off-by: Danilo Krummrich <[email protected]>
> ---
>  drivers/gpu/nova-core/mm.rs                | 21 ++++++++----
>  drivers/gpu/nova-core/mm/bar_user.rs       | 39 ++++++++++++++--------
>  drivers/gpu/nova-core/mm/pagetable/map.rs  | 34 +++++++++----------
>  drivers/gpu/nova-core/mm/pagetable/walk.rs |  2 +-
>  drivers/gpu/nova-core/mm/pramin.rs         | 34 +++++++++++++++++++
>  5 files changed, 91 insertions(+), 39 deletions(-)
>
> diff --git a/drivers/gpu/nova-core/mm.rs b/drivers/gpu/nova-core/mm.rs
> index f53886f9ed91..bbe23dc6351f 100644
> --- a/drivers/gpu/nova-core/mm.rs
> +++ b/drivers/gpu/nova-core/mm.rs
> @@ -72,11 +72,11 @@ macro_rules! impl_pfn_bounded {
>  ///
>  /// Provides centralized ownership of memory management resources:
>  /// - [`GpuBuddy`] allocator for VRAM page table allocation.
> -/// - [`pramin::Pramin`] for direct VRAM access.
> +/// - [`pramin::SharedPramin`] for direct VRAM access.
>  /// - [`Tlb`] manager for translation buffer flush operations.
>  pub(crate) struct GpuMm<'gpu> {
>      buddy: GpuBuddy,
> -    pramin: pramin::Pramin<'gpu>,
> +    pramin: Pin<KBox<pramin::SharedPramin<'gpu>>>,

Nice, this is probably going to remove the `SharedGpuMm` I introduced
(and hated) in the vGPU series. :)

A couple of comments below.

<...>
> index f7774b9cc972..ba23cd3c2b25 100644
> --- a/drivers/gpu/nova-core/mm/pagetable/map.rs
> +++ b/drivers/gpu/nova-core/mm/pagetable/map.rs
> @@ -96,12 +96,13 @@ fn alloc_and_zero_page(mm: &mut GpuMm<'_>, level: 
> PageTableLevel) -> Result<Prep
>          let page_vram = 
> VramAddress::from_raw(blocks.iter().next().ok_or(ENOMEM)?.offset());
>  
>          // Zero via PRAMIN.
> -        let window = mm
> -            .pramin_mut()
> -            .window_at::<[u64; PAGE_SIZE / 8]>(page_vram)?;
> -        for i in 0..PAGE_SIZE / 8 {
> -            io_write!(window.view(), [build: i], 0);
> -        }
> +        mm.with_pramin_locked(|pramin| -> Result {
> +            let window = pramin.window_at::<[u64; PAGE_SIZE / 
> 8]>(page_vram)?;
> +            for i in 0..PAGE_SIZE / 8 {
> +                io_write!(window.view(), [build: i], 0);
> +            }
> +            Ok(())
> +        })?;
>  
>          Ok(PreparedPtPage {
>              alloc: blocks,
> @@ -121,16 +122,16 @@ fn ensure_single_pte_path(
>          let max_iter = 2 * M::PDE_LEVELS.len();
>  
>          for _ in 0..max_iter {
> -            let result = self
> -                .walker
> -                .walk_pde_levels(mm.pramin_mut(), vfn, |install_addr| {
> +            let result = mm.with_pramin_locked(|pramin| {

nit: With this the method-level doccomment should be about the PRAMIN
lock being released instead of the borrow.

<...>
> diff --git a/drivers/gpu/nova-core/mm/pramin.rs 
> b/drivers/gpu/nova-core/mm/pramin.rs
> index c83b1681d241..841289d7bccc 100644
> --- a/drivers/gpu/nova-core/mm/pramin.rs
> +++ b/drivers/gpu/nova-core/mm/pramin.rs
> @@ -13,6 +13,7 @@
>          Io,
>          Mmio, //
>      },
> +    new_mutex,
>      prelude::*,
>      ptr::{
>          Alignable,
> @@ -22,6 +23,7 @@
>          SizeConstants,
>          SZ_1M, //
>      },
> +    sync::Mutex, //
>  };
>  
>  use crate::{
> @@ -54,6 +56,38 @@
>      PRAMIN: PraminWindow @ 0x700000;
>  }
>  
> +/// A [`Pramin`] shared between users, serialized by a mutex.
> +#[pin_data]
> +pub(super) struct SharedPramin<'gpu>
> +where
> +    // Hide the mutex's invariant resource lifetime so `SharedPramin` 
> remains covariant.
> +    exists<'res>: 'gpu,
> +{
> +    #[pin]
> +    #[uses('res: invariant)]
> +    inner: Mutex<Pramin<'res>>,
> +}
> +
> +impl<'gpu> SharedPramin<'gpu> {
> +    /// Creates the shared window manager for the given VRAM region.
> +    pub(super) fn new(
> +        bar: Bar0<'gpu>,
> +        chipset: Chipset,
> +        vram_range: Range<VramAddress>,
> +    ) -> impl PinInit<Self, Error> {
> +        try_pin_init!(Self {
> +            inner <- new_mutex!(Pramin::new(bar, chipset, vram_range)?, 
> "pramin"),
> +        })
> +    }
> +
> +    /// Runs `f` with exclusive access to the PRAMIN window.
> +    ///
> +    /// Do not perform reclaimable allocations in `f`.

A little elaboration on why (deadlocks in the fence signaling path)
would be nice here. Something similar to what Tlb::lock has maybe, and
maybe we should also put that information into some module-level
paragraph that we can refer to instead of repeating it as it is relevant
in several places.

Reply via email to