[PATCH 2/2] drm/tyr: unmap regions out-of-sync with gpuvm
From: Deborah Brouwer
Date: Fri Dec 04 2026 - 18:54:21 EST
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@xxxxxxxxxx>
Signed-off-by: Sami Tolvanen <samitolvanen@xxxxxxxxxx>
Signed-off-by: Deborah Brouwer <deborah.brouwer@xxxxxxxxxxxxx>
---
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, ®ion))
+ }
+
+ /// 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, ®ion);
+ 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