mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] gpu: nova-core: mm: document fence signalling lock dependencies
@ 2026-12-03 21:50 Danilo Krummrich
  2026-12-03 21:50 ` [PATCH 2/2] gpu: nova-core: mm: move TLB register access behind the mutex Danilo Krummrich
  2026-12-03 22:13 ` [PATCH 1/2] gpu: nova-core: mm: document fence signalling lock dependencies Gary Guo
  0 siblings, 2 replies; 4+ messages in thread
From: Danilo Krummrich @ 2026-12-03 21:50 UTC (permalink / raw)
  To: dakr, acourbot; +Cc: nova-gpu, dri-devel, linux-kernel

Expand the MM module documentation on locking and DMA fence signalling.

Suggested-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://lore.kernel.org/all/DN6H2GAT9F0Y.2ZZRVU8FZCYZB@nvidia.com/
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 drivers/gpu/nova-core/mm.rs     | 25 +++++++++++++++++++++++--
 drivers/gpu/nova-core/mm/tlb.rs |  8 ++++----
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/nova-core/mm.rs b/drivers/gpu/nova-core/mm.rs
index 795939a5c8ab..0818358ab12c 100644
--- a/drivers/gpu/nova-core/mm.rs
+++ b/drivers/gpu/nova-core/mm.rs
@@ -2,6 +2,27 @@
 // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 
 //! Memory management subsystems.
+//!
+//! # Locking
+//!
+//! A reclaimable allocation while holding a lock that may be acquired in the DMA
+//! fence signalling critical path (such as the PRAMIN or TLB lock) can deadlock:
+//! reclaim may wait for a DMA fence whose signalling path needs the same lock.
+//!
+//! The restriction is transitive: if code holding lock A can block acquiring
+//! lock B, and fence signalling needs A, B must not be held across reclaimable
+//! allocations on any path, even outside the signalling path (the same applies to
+//! every lock in the dependency chain). Otherwise a holder of B can enter reclaim
+//! and wait for a fence whose signalling is blocked on A or B.
+//!
+//! The allocation restriction also applies to work needed to signal a fence that
+//! other threads can already wait on, even when no lock is held. If an allocation
+//! in that work enters reclaim and waits for the same fence, the work cannot
+//! reach the code that signals it.
+//!
+//! See the DMA-fence
+//! [contract](https://docs.kernel.org/driver-api/dma-buf.html#dma-fence-cross-driver-contract)
+//! for the full requirements.
 
 #![cfg_attr(not(CONFIG_NOVA_CORE_SELFTESTS), expect(dead_code))]
 
@@ -116,8 +137,8 @@ pub(crate) fn buddy(&self) -> &GpuBuddy {
 
     /// Runs `f` with exclusive access to the PRAMIN window.
     ///
-    /// Do not perform reclaimable allocations in `f`: reclaim may wait for a DMA
-    /// fence whose signalling path needs the PRAMIN lock, causing a deadlock.
+    /// Do not perform reclaimable allocations in `f`; see the
+    /// [MM locking rules](crate::mm#locking).
     fn with_pramin_locked<R>(&self, f: impl FnOnce(&mut pramin::Pramin<'_>) -> R) -> R {
         self.with_pramin(|pramin| f(&mut pramin.lock()))
     }
diff --git a/drivers/gpu/nova-core/mm/tlb.rs b/drivers/gpu/nova-core/mm/tlb.rs
index cc862e8159a1..fff66dc03549 100644
--- a/drivers/gpu/nova-core/mm/tlb.rs
+++ b/drivers/gpu/nova-core/mm/tlb.rs
@@ -66,10 +66,10 @@ pub(crate) enum TlbAckMode with TryFrom<Bounded<u32, 2>> {
 #[pin_data]
 pub(crate) struct Tlb<'gpu> {
     bar: Bar0<'gpu>,
-    /// TLB flush serialization lock: This lock is designed to be acquired during
-    /// the DMA fence signalling critical path. It should NEVER be held across any
-    /// reclaimable CPU memory allocations because the memory reclaim path can
-    /// call `dma_fence_wait()` (when implemented), which would deadlock if lock held.
+    /// TLB flush serialization lock.
+    ///
+    /// Do not hold this lock across reclaimable memory allocations; see the
+    /// [MM locking rules](crate::mm#locking).
     #[pin]
     lock: Mutex<()>,
 }

base-commit: d9da44362d77bcbf1f5bfd2ba1c514e776fdb07c
-- 
2.56.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] gpu: nova-core: mm: move TLB register access behind the mutex
  2026-12-03 21:50 [PATCH 1/2] gpu: nova-core: mm: document fence signalling lock dependencies Danilo Krummrich
@ 2026-12-03 21:50 ` Danilo Krummrich
  2026-12-03 22:13   ` Gary Guo
  2026-12-03 22:13 ` [PATCH 1/2] gpu: nova-core: mm: document fence signalling lock dependencies Gary Guo
  1 sibling, 1 reply; 4+ messages in thread
From: Danilo Krummrich @ 2026-12-03 21:50 UTC (permalink / raw)
  To: dakr, acourbot; +Cc: nova-gpu, dri-devel, linux-kernel

Store the BAR reference in the TLB mutex and use a with_bar_locked()
helper to serialize register access. This makes the locking requirement
more explicit (e.g. when adding operations such as fault replay and
cancellation).

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 drivers/gpu/nova-core/mm/tlb.rs | 70 ++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 31 deletions(-)

diff --git a/drivers/gpu/nova-core/mm/tlb.rs b/drivers/gpu/nova-core/mm/tlb.rs
index fff66dc03549..eb32b95623be 100644
--- a/drivers/gpu/nova-core/mm/tlb.rs
+++ b/drivers/gpu/nova-core/mm/tlb.rs
@@ -64,57 +64,65 @@ pub(crate) enum TlbAckMode with TryFrom<Bounded<u32, 2>> {
 
 /// TLB manager for GPU translation buffer operations.
 #[pin_data]
-pub(crate) struct Tlb<'gpu> {
-    bar: Bar0<'gpu>,
+pub(crate) struct Tlb<'tlb>
+where
+    // Hide the mutex's invariant resource lifetime so `Tlb` remains covariant.
+    exists<'res>: 'tlb,
+{
     /// TLB flush serialization lock.
     ///
     /// Do not hold this lock across reclaimable memory allocations; see the
     /// [MM locking rules](crate::mm#locking).
     #[pin]
-    lock: Mutex<()>,
+    #[uses('res: invariant)]
+    bar: Mutex<Bar0<'res>>,
 }
 
-impl<'gpu> Tlb<'gpu> {
+impl<'tlb> Tlb<'tlb> {
     /// Create a new TLB manager.
-    pub(super) fn new(bar: Bar0<'gpu>) -> impl PinInit<Self> {
+    pub(super) fn new(bar: Bar0<'tlb>) -> impl PinInit<Self> {
         pin_init!(Self {
-            bar,
-            lock <- new_mutex!((), "tlb_flush"),
+            bar <- new_mutex!(bar, "tlb_flush"),
         })
     }
 
+    /// Runs `f` with the TLB locked.
+    fn with_bar_locked<R>(&self, f: impl FnOnce(Bar0<'_>) -> R) -> R {
+        self.with_bar(|bar| f(*bar.lock()))
+    }
+
     /// Flush the GPU TLB for a specific page directory base.
     ///
     /// This invalidates all TLB entries associated with the given PDB address.
     /// Must be called after modifying page table entries to ensure the GPU sees
     /// the updated mappings.
     pub(super) fn flush(&self, pdb_addr: VramAddress) -> Result {
-        let _guard = self.lock.lock();
+        self.with_bar_locked(|bar| {
+            // Write PDB address.
+            bar.write_reg(regs::NV_TLB_FLUSH_PDB_LO::from_pdb_addr(
+                pdb_addr.into_raw(),
+            ));
+            bar.write_reg(regs::NV_TLB_FLUSH_PDB_HI::from_pdb_addr(
+                pdb_addr.into_raw(),
+            ));
 
-        // Write PDB address.
-        self.bar.write_reg(regs::NV_TLB_FLUSH_PDB_LO::from_pdb_addr(
-            pdb_addr.into_raw(),
-        ));
-        self.bar.write_reg(regs::NV_TLB_FLUSH_PDB_HI::from_pdb_addr(
-            pdb_addr.into_raw(),
-        ));
+            // Trigger flush.
+            bar.write_reg(
+                regs::NV_TLB_FLUSH_CTRL::zeroed()
+                    .with_all_va(true)
+                    .with_ack(TlbAckMode::None)
+                    .with_trigger(true),
+            );
 
-        // Trigger flush.
-        self.bar.write_reg(
-            regs::NV_TLB_FLUSH_CTRL::zeroed()
-                .with_all_va(true)
-                .with_ack(TlbAckMode::None)
-                .with_trigger(true),
-        );
+            // Poll for completion.
+            read_poll_timeout(
+                || Ok(bar.read(regs::NV_TLB_FLUSH_CTRL)),
+                |ctrl: &regs::NV_TLB_FLUSH_CTRL| !ctrl.trigger(),
+                Delta::ZERO,
+                Delta::from_secs(2),
+            )?;
 
-        // Poll for completion.
-        read_poll_timeout(
-            || Ok(self.bar.read(regs::NV_TLB_FLUSH_CTRL)),
-            |ctrl: &regs::NV_TLB_FLUSH_CTRL| !ctrl.trigger(),
-            Delta::ZERO,
-            Delta::from_secs(2),
-        )?;
-
-        Ok(())
+            Ok(())
+        })
     }
 }
-- 
2.56.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] gpu: nova-core: mm: move TLB register access behind the mutex
  2026-12-03 21:50 ` [PATCH 2/2] gpu: nova-core: mm: move TLB register access behind the mutex Danilo Krummrich
@ 2026-12-03 22:13   ` Gary Guo
  0 siblings, 0 replies; 4+ messages in thread
From: Gary Guo @ 2026-12-03 22:13 UTC (permalink / raw)
  To: Danilo Krummrich, acourbot; +Cc: nova-gpu, dri-devel, linux-kernel

On Thu Dec 3, 2026 at 9:50 PM GMT, Danilo Krummrich wrote:
> Store the BAR reference in the TLB mutex and use a with_bar_locked()
> helper to serialize register access. This makes the locking requirement
> more explicit (e.g. when adding operations such as fault replay and
> cancellation).
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

The use of existential lifetime here looks okay to me.

Acked-by: Gary Guo <gary@garyguo.net>

Please have Benno and me is the cc for use of "advanced feature of pin-init"
because we're keeping an eye on it's been used in the kernel. (As mentioned in
my PR to Miguel
https://lore.kernel.org/rust-for-linux/20261012184429.1613374-1-gary@kernel.org/)

Best,
Gary

> ---
>  drivers/gpu/nova-core/mm/tlb.rs | 70 ++++++++++++++++++---------------
>  1 file changed, 39 insertions(+), 31 deletions(-)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] gpu: nova-core: mm: document fence signalling lock dependencies
  2026-12-03 21:50 [PATCH 1/2] gpu: nova-core: mm: document fence signalling lock dependencies Danilo Krummrich
  2026-12-03 21:50 ` [PATCH 2/2] gpu: nova-core: mm: move TLB register access behind the mutex Danilo Krummrich
@ 2026-12-03 22:13 ` Gary Guo
  1 sibling, 0 replies; 4+ messages in thread
From: Gary Guo @ 2026-12-03 22:13 UTC (permalink / raw)
  To: Danilo Krummrich, acourbot; +Cc: nova-gpu, dri-devel, linux-kernel

On Thu Dec 3, 2026 at 9:50 PM GMT, Danilo Krummrich wrote:
> Expand the MM module documentation on locking and DMA fence signalling.
> 
> Suggested-by: Alexandre Courbot <acourbot@nvidia.com>
> Link: https://lore.kernel.org/all/DN6H2GAT9F0Y.2ZZRVU8FZCYZB@nvidia.com/
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>

Reviewed-by: Gary Guo <gary@garyguo.net>

> ---
>  drivers/gpu/nova-core/mm.rs     | 25 +++++++++++++++++++++++--
>  drivers/gpu/nova-core/mm/tlb.rs |  8 ++++----
>  2 files changed, 27 insertions(+), 6 deletions(-)


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-03 22:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-03 21:50 [PATCH 1/2] gpu: nova-core: mm: document fence signalling lock dependencies Danilo Krummrich
2026-12-03 21:50 ` [PATCH 2/2] gpu: nova-core: mm: move TLB register access behind the mutex Danilo Krummrich
2026-12-03 22:13   ` Gary Guo
2026-12-03 22:13 ` [PATCH 1/2] gpu: nova-core: mm: document fence signalling lock dependencies Gary Guo

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®