mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] drm/tyr: unmap regions out-of-sync with gpuvm
@ 2026-12-04 23:53 Deborah Brouwer
  2026-12-04 23:53 ` [PATCH 1/2] drm/tyr: get preallocated gpuva before mapping starts Deborah Brouwer
  2026-12-04 23:53 ` [PATCH 2/2] drm/tyr: unmap regions out-of-sync with gpuvm Deborah Brouwer
  0 siblings, 2 replies; 3+ messages in thread
From: Deborah Brouwer @ 2026-12-04 23:53 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, David Airlie,
	Simona Vetter
  Cc: dri-devel, rust-for-linux, linux-kernel, Deborah Brouwer, sunke,
	alvin.sun, Sami Tolvanen

This series prepares for the introduction of the vm_bind ioctl [1]
by keeping track of regions that may be out of sync with
gpuvm and allowing these regions to be unmapped.

It depends on the fix for partial unmap requests for 2MB blocks [2]
which needs to be rebased due to some recent casting changes made to 
Tyr in drm-rust-next.

[1] [PATCH v7 00/10] drm/tyr: add VM and BO ioctl support
https://lore.kernel.org/rust-for-linux/20261122-tyr-ioctls-v7-0-3a2af624b992@linux.dev/

[2] [PATCH 0/4] drm/tyr: fix partial 2MB block unmaps on GPUVM remap
https://lore.kernel.org/rust-for-linux/20261117-tyr-remap-2mb-v1-0-2a8efd4c8180@google.com/

Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
Deborah Brouwer (2):
      drm/tyr: get preallocated gpuva before mapping starts
      drm/tyr: unmap regions out-of-sync with gpuvm

 drivers/gpu/drm/tyr/vm.rs | 136 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 124 insertions(+), 12 deletions(-)
---
base-commit: 5c731ac3cd30650b7916556af51e60d4409a06fe
change-id: 20261204-b4-mark_failed-fb1f3ab1b97b
prerequisite-message-id: <20261117-tyr-remap-2mb-v1-0-2a8efd4c8180@google.com>
prerequisite-patch-id: 87382cf093596d57a0799d05ebc84fa2cbbca893
prerequisite-patch-id: 25bbb0f7ee801f6caf290fe3d02def94d5cbf302
prerequisite-patch-id: 5e6cffc4f8b58f1e70315b1d340c9e5f0e3939fe
prerequisite-patch-id: 197fccc7be72e6f52ffa2f82b8dd86add19b6019

Best regards,
-- 
Deborah Brouwer <deborah.brouwer@collabora.com>


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

* [PATCH 1/2] drm/tyr: get preallocated gpuva before mapping starts
  2026-12-04 23:53 [PATCH 0/2] drm/tyr: unmap regions out-of-sync with gpuvm Deborah Brouwer
@ 2026-12-04 23:53 ` Deborah Brouwer
  2026-12-04 23:53 ` [PATCH 2/2] drm/tyr: unmap regions out-of-sync with gpuvm Deborah Brouwer
  1 sibling, 0 replies; 3+ messages in thread
From: Deborah Brouwer @ 2026-12-04 23:53 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, David Airlie,
	Simona Vetter
  Cc: dri-devel, rust-for-linux, linux-kernel, Deborah Brouwer, sunke,
	alvin.sun

Currently, it is possible for a page table mapping to succeed, while the
call to obtain a preallocated gpuva could fail, so that an error is
returned before gpuvm is updated with the new mapping.

Avoid this scenario by obtaining the preallocated gpuva before the page
table is modified.

Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
 drivers/gpu/drm/tyr/vm.rs | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index 323ad647ad19..e80f2cdd0fb5 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -659,6 +659,9 @@ fn sm_step_map<'op>(
             return Err(EINVAL);
         }
 
+        // Return any error here before modifying the page table.
+        let gpuva = context.preallocated_gpuva()?;
+
         let sgt = op.obj().sg_table(context.dev).inspect_err(|e| {
             dev_err!(context.dev, "Failed to get sg_table: {:?}", e);
         })?;
@@ -729,7 +732,6 @@ fn sm_step_map<'op>(
             return Err(EINVAL);
         }
 
-        let gpuva = context.preallocated_gpuva()?;
         let op = op.insert(gpuva, GpuVaData { prot });
 
         Ok(op)

-- 
2.56.0


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

* [PATCH 2/2] drm/tyr: unmap regions out-of-sync with gpuvm
  2026-12-04 23:53 [PATCH 0/2] drm/tyr: unmap regions out-of-sync with gpuvm Deborah Brouwer
  2026-12-04 23:53 ` [PATCH 1/2] drm/tyr: get preallocated gpuva before mapping starts Deborah Brouwer
@ 2026-12-04 23:53 ` Deborah Brouwer
  1 sibling, 0 replies; 3+ messages in thread
From: Deborah Brouwer @ 2026-12-04 23:53 UTC (permalink / raw)
  To: Danilo Krummrich, Alice Ryhl, Daniel Almeida, David Airlie,
	Simona Vetter
  Cc: dri-devel, rust-for-linux, linux-kernel, Deborah Brouwer, sunke,
	alvin.sun, Sami Tolvanen

If a mapping fails either during sm_step_map() or on sm_step_remap(), an
error is returned without updating gpuvm so that a page table may be left
partially mapped and out of sync with gpuvm bookkeeping.

Keep track of the range that may be out of sync and make the unmapping
functions aware of these potentially inconsistent regions so that they can
unmap the remaining mapped pages without failing on holes in the page
table.

Marking the VM unusable and disabling its hardware address space following
a failed page-table update will be addressed in a follow-up patch when
the vm_bind ioctl is enabled.

Co-developed-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Deborah Brouwer <deborah.brouwer@collabora.com>
---
 drivers/gpu/drm/tyr/vm.rs | 132 ++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 121 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index e80f2cdd0fb5..98a8802c2c4b 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -72,6 +72,25 @@
     regs::gpu_control::MMU_FEATURES,
 };
 
+/// With a 4KiB granule, 2MiB is the smallest block mapping size
+/// and corresponds to a 2 MiB huge page.
+///
+/// ARM64 LPAE stage 1 translation (4 KiB granule):
+///
+/// L0 (512 GiB per entry)
+///  |
+/// L1 (1 GiB per entry) ---- 1 GiB block
+///  |
+/// L2 (2 MiB per entry) ---- 2 MiB block
+///  |
+/// L3 (4 KiB per entry) ---- 4 KiB page
+///
+/// See [AArch64 Memory Management Guide - Translation granule].
+///
+/// [AArch64 Memory Management Guide - Translation granule]:
+/// https://support.arm.com/documentation/101811/0106/Translation-granule
+const PT_MIN_BLOCK_SIZE: usize = usize::SZ_2M;
+
 impl_flags!(
     /// Flags controlling virtual memory mapping behavior.
     ///
@@ -328,9 +347,64 @@ fn drop(&mut self) {
 /// Implements [`DriverGpuVm`] to provide VM operation callbacks (map, unmap, remap)
 /// and associated types for buffer objects, virtual addresses, and contexts.
 pub(crate) struct GpuVmData<'drm> {
+    /// Range that failed page-table updates may have left out of sync with
+    /// the VA tree. Outside it, the page table matches the VA tree. It is
+    /// empty until the first failure.
+    pt_out_of_sync: Range<u64>,
     _phantom: PhantomData<&'drm ()>,
 }
 
+impl<'drm> GpuVmData<'drm> {
+    /// Records the region potentially left out of sync with gpuvm
+    /// after a failed page-table update.
+    fn mark_failed(&mut self, _context: &PtUpdateContext<'_, '_>, region: &Range<u64>) {
+        // Out of an abundance of caution, expand the affected region
+        // in case it was mapped as a block (i.e. a huge page).
+        let block = Alignment::new::<{ PT_MIN_BLOCK_SIZE }>();
+        let end = region.end.align_up(block).unwrap_or(u64::MAX);
+        let region = region.start.align_down(block)..end;
+
+        let span = &mut self.pt_out_of_sync;
+        *span = if span.is_empty() {
+            region
+        } else {
+            span.start.min(region.start)..span.end.max(region.end)
+        };
+        // TODO when vm bind is enabled:
+        // 1) Turn the VM into a state where it can't be used.
+        // 2) disable the hardware address space so that gpu can't access failed mapping
+    }
+
+    /// Unmaps `region` from the page table.
+    ///
+    /// A failed update can leave holes in the range it covered, so only the
+    /// part of `region` inside `pt_out_of_sync` is unmapped page by page.
+    fn unmap(&mut self, context: &PtUpdateContext<'_, '_>, region: Range<u64>) -> Result {
+        // Determine if any part of the region may be out of sync with gpuvm.
+        let span = &self.pt_out_of_sync;
+        let to_probe = region.start.max(span.start)..region.end.min(span.end);
+
+        if to_probe.is_empty() {
+            return self.unmap_in_sync(context, region);
+        }
+
+        self.unmap_in_sync(context, region.start..to_probe.start)
+            .and_then(|()| pt_unmap_mapped(context.dev, context.pt, to_probe.clone()))
+            .and_then(|()| self.unmap_in_sync(context, to_probe.end..region.end))
+            .inspect_err(|_| self.mark_failed(context, &region))
+    }
+
+    /// Unmaps `region`, which lies outside `pt_out_of_sync` and so is fully mapped.
+    fn unmap_in_sync(&mut self, context: &PtUpdateContext<'_, '_>, region: Range<u64>) -> Result {
+        if pt_unmap(context.dev, context.pt, region.clone()).is_ok() {
+            return Ok(());
+        }
+
+        self.mark_failed(context, &region);
+        pt_unmap_mapped(context.dev, context.pt, region)
+    }
+}
+
 /// Per-mapping private data stored on each `GpuVa`.
 ///
 /// A remap splits an existing mapping and may have to rebuild page-table
@@ -392,6 +466,7 @@ pub(crate) fn new(
             range.clone(),
             reserve_range,
             GpuVmData::<'drm> {
+                pt_out_of_sync: 0..0,
                 _phantom: PhantomData::<&()>,
             },
         )
@@ -708,6 +783,7 @@ fn sm_step_map<'op>(
                             start_iova..(start_iova + total_mapped),
                         );
                     }
+                    self.mark_failed(context, &(start_iova..start_iova + op.length()));
                     return Err(e);
                 }
             };
@@ -720,7 +796,9 @@ fn sm_step_map<'op>(
             let total_mapped = iova - start_iova;
 
             if total_mapped > 0 {
-                let _ = pt_unmap(context.dev, context.pt, start_iova..iova);
+                if pt_unmap(context.dev, context.pt, start_iova..iova).is_err() {
+                    self.mark_failed(context, &(start_iova..iova));
+                }
             }
 
             dev_err!(
@@ -747,7 +825,7 @@ fn sm_step_unmap<'op>(
         let length = op.va().length();
 
         let region = start_iova..(start_iova + length);
-        pt_unmap(context.dev, context.pt, region.clone()).inspect_err(|e| {
+        self.unmap(context, region.clone()).inspect_err(|e| {
             dev_err!(
                 context.dev,
                 "Failed to unmap region {:#x}..{:#x}: {:?}",
@@ -824,7 +902,7 @@ fn sm_step_remap<'op>(
         if region_end > region_start {
             let region = region_start..region_end;
             context.extend_lock(region.clone())?;
-            pt_unmap(context.dev, context.pt, region.clone()).inspect_err(|e| {
+            self.unmap(context, region.clone()).inspect_err(|e| {
                 dev_err!(
                     context.dev,
                     "Failed to unmap remap region {:#x}..{:#x}: {:?}",
@@ -843,7 +921,8 @@ fn sm_step_remap<'op>(
                 head_paddr,
                 unmap_start - aligned_start,
                 prot,
-            )?;
+            )
+            .inspect_err(|_| self.mark_failed(context, &(region_start..region_end)))?;
         }
         if let Some(tail_paddr) = tail_paddr {
             pt_map(
@@ -853,7 +932,8 @@ fn sm_step_remap<'op>(
                 tail_paddr,
                 aligned_end - unmap_end,
                 prot,
-            )?;
+            )
+            .inspect_err(|_| self.mark_failed(context, &(region_start..region_end)))?;
         }
 
         let (op_remapped, remap_ret) =
@@ -949,8 +1029,8 @@ fn get_pgsize(addr: u64, size: u64) -> (usize, usize) {
 /// virtual address `iova`, using the protection flags specified in `prot`. It
 /// automatically selects optimal page sizes to minimize page table overhead.
 ///
-/// If the mapping fails partway through, all successfully mapped pages are
-/// unmapped before returning an error.
+/// If the mapping fails partway through, an attempt is made to unmap
+/// all successfully mapped pages before returning an error.
 ///
 /// Returns the number of bytes successfully mapped.
 fn pt_map(
@@ -1055,10 +1135,10 @@ fn pt_unmap(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range<u64>)
 
         // SAFETY:
         // No other io-pgtable operation can currently access this range because Tyr holds
-        // the gpuvm_unique mutex for the entire sm_unmap() operation.
-        // We know that this page table has one or more consecutive mappings
-        // starting at `iova` with the total size of `pgcount * pgsize` because
-        // gpuvm callbacks provide exactly the range that was previously mapped.
+        // the gpuvm_unique mutex for all vm update operations.
+        // Every range passed here is fully mapped, so this page table has one
+        // or more consecutive mappings starting at `iova` with the total size
+        // of `pgcount * pgsize`.
         let unmapped = unsafe { pt.unmap_pages(iova_usize, pgsize, pgcount) };
 
         if unmapped == 0 {
@@ -1073,3 +1153,33 @@ fn pt_unmap(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range<u64>)
 
     Ok(())
 }
+
+/// Unmaps the pages of `range` that are still mapped, skipping any holes.
+fn pt_unmap_mapped(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range<u64>) -> Result {
+    let mut run_start = None;
+    let mut iova = range.start;
+
+    while iova < range.end {
+        let iova_usize = usize::try_from(iova).map_err(|_| EOVERFLOW)?;
+
+        // SAFETY: The VM update holds the `gpuvm_unique` mutex, so no other
+        // io-pgtable operation runs on this page table.
+        let mapped = unsafe { pt.iova_to_phys(iova_usize) }.is_some();
+
+        match (mapped, run_start) {
+            (true, None) => run_start = Some(iova),
+            (false, Some(start)) => {
+                pt_unmap(dev, pt, start..iova)?;
+                run_start = None;
+            }
+            _ => {}
+        }
+
+        iova += u64::SZ_4K;
+    }
+
+    match run_start {
+        Some(start) => pt_unmap(dev, pt, start..range.end),
+        None => Ok(()),
+    }
+}

-- 
2.56.0


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

end of thread, other threads:[~2026-12-04 23:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04 23:53 [PATCH 0/2] drm/tyr: unmap regions out-of-sync with gpuvm Deborah Brouwer
2026-12-04 23:53 ` [PATCH 1/2] drm/tyr: get preallocated gpuva before mapping starts Deborah Brouwer
2026-12-04 23:53 ` [PATCH 2/2] drm/tyr: unmap regions out-of-sync with gpuvm Deborah Brouwer

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®