From: Alice Ryhl <aliceryhl@google.com>
To: Daniel Almeida <daniel.almeida@collabora.com>,
Deborah Brouwer <deborah.brouwer@collabora.com>,
Sami Tolvanen <samitolvanen@google.com>
Cc: "Danilo Krummrich" <dakr@kernel.org>,
"Matthew Brost" <matthew.brost@intel.com>,
"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>,
"Joerg Roedel (AMD)" <joro@8bytes.org>,
"Will Deacon" <will@kernel.org>,
"Robin Murphy" <robin.murphy@arm.com>,
"Boris Brezillon" <boris.brezillon@collabora.com>,
dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
linux-kernel@vger.kernel.org, iommu@lists.linux.dev,
"Alice Ryhl" <aliceryhl@google.com>
Subject: [PATCH 3/4] drm/tyr: vm: expand sub-block unmaps to 2MB boundaries on remap
Date: Tue, 17 Nov 2026 19:32:20 +0000 [thread overview]
Message-ID: <20261117-tyr-remap-2mb-v1-3-2a8efd4c8180@google.com> (raw)
In-Reply-To: <20261117-tyr-remap-2mb-v1-0-2a8efd4c8180@google.com>
From: Sami Tolvanen <samitolvanen@google.com>
Backing GEM shmem with transparent huge pages lets Tyr install 2MB block
entries. arm-lpae refuses to unmap part of a block, so a sparse partial
rebind that splits one in sm_step_remap fails the VM_BIND and poisons
the VM.
Grow such an unmap to the block boundary and rebuild the surviving
fragment, expanding only when contiguous.
Fixes: f2dc32d5a133 ("drm/tyr: add GPU virtual memory (VM) support")
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
[aliceryhl: tweaks for drm-rust-next and use safe casts instead of 'as']
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
drivers/gpu/drm/tyr/mmu.rs | 13 +++
drivers/gpu/drm/tyr/mmu/address_space.rs | 29 ++++++
drivers/gpu/drm/tyr/vm.rs | 146 ++++++++++++++++++++++++++++---
3 files changed, 176 insertions(+), 12 deletions(-)
diff --git a/drivers/gpu/drm/tyr/mmu.rs b/drivers/gpu/drm/tyr/mmu.rs
index 5e88c9e6a4cd..99739a3a8aa1 100644
--- a/drivers/gpu/drm/tyr/mmu.rs
+++ b/drivers/gpu/drm/tyr/mmu.rs
@@ -109,6 +109,19 @@ pub(crate) fn start_vm_update(
self.as_manager.lock().start_vm_update(vm_as_data, region)
}
+ /// Widens the active lock to cover a larger region.
+ ///
+ /// `region` must contain the currently locked range so the GPU stays
+ /// stalled over it while the update completes. If the region is empty or
+ /// the VM is not resident, this is a NOP.
+ pub(crate) fn extend_vm_update(
+ &self,
+ vm_as_data: &VmAsData<'drm>,
+ region: &Range<u64>,
+ ) -> Result {
+ self.as_manager.lock().extend_vm_update(vm_as_data, region)
+ }
+
/// Flags the end of a VM update.
///
/// If the VM is resident, this will let GPU accesses on the updated
diff --git a/drivers/gpu/drm/tyr/mmu/address_space.rs b/drivers/gpu/drm/tyr/mmu/address_space.rs
index 76e3d2df6afd..666af82a37b7 100644
--- a/drivers/gpu/drm/tyr/mmu/address_space.rs
+++ b/drivers/gpu/drm/tyr/mmu/address_space.rs
@@ -475,6 +475,35 @@ pub(super) fn start_vm_update(
}
}
+ /// Widens the active lock to cover a larger region.
+ ///
+ /// Re-issues the LOCKADDR and Lock command over `region`, which the
+ /// caller has computed as the union of the currently locked region and
+ /// the wider range it is about to rebuild. The hardware lock is replaced
+ /// in place, so `region` must contain the previously locked range to keep
+ /// the GPU stalled over it.
+ ///
+ /// If the region is empty or the VM is not resident in a hardware slot,
+ /// this is a no-op.
+ pub(super) fn extend_vm_update(
+ &mut self,
+ vm_as_data: &VmAsData<'drm>,
+ region: &Range<u64>,
+ ) -> Result {
+ if region.is_empty() {
+ return Ok(());
+ }
+
+ let seat = vm_as_data.as_seat.access(self);
+ match seat.slot() {
+ Some(slot) => {
+ let as_nr = usize::from(slot);
+ self.as_start_update(as_nr, region)
+ }
+ _ => Ok(()),
+ }
+ }
+
/// Completes translation table updates and unlocks the region.
pub(super) fn end_vm_update(&mut self, vm_as_data: &VmAsData<'drm>) -> Result {
let seat = vm_as_data.as_seat.access(self);
diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
index 1dd11b2b1253..bcc864b17428 100644
--- a/drivers/gpu/drm/tyr/vm.rs
+++ b/drivers/gpu/drm/tyr/vm.rs
@@ -42,7 +42,12 @@
},
new_mutex,
prelude::*,
+ ptr::{
+ Alignable,
+ Alignment, //
+ },
sizes::{
+ SizeConstants,
SZ_1G,
SZ_2M,
SZ_4K, //
@@ -290,6 +295,22 @@ fn return_preallocated_gpuva(&mut self, gpuva: GpuVaAlloc<GpuVmData<'drm>>) {
*slot = Some(gpuva);
}
}
+
+ /// Widens the AS lock to also cover `region`.
+ ///
+ /// A remap can rebuild fragments beyond the originally locked request,
+ /// so the lock is grown to the union of the two before those fragments
+ /// are torn down. The locked region only ever grows, so several remaps
+ /// in one unmap union onto a single widening lock.
+ fn extend_lock(&mut self, region: Range<u64>) -> Result {
+ let union =
+ u64::min(self.region.start, region.start)..u64::max(self.region.end, region.end);
+ if union != self.region {
+ self.mmu.extend_vm_update(self.as_data, &union)?;
+ self.region = union;
+ }
+ Ok(())
+ }
}
impl Drop for PtUpdateContext<'_, '_> {
@@ -312,6 +333,16 @@ pub(crate) struct GpuVmData<'drm> {
_phantom: PhantomData<&'drm ()>,
}
+/// Per-mapping private data stored on each `GpuVa`.
+///
+/// A remap splits an existing mapping and may have to rebuild page-table
+/// entries for the surviving fragments. Those fragments keep the protection
+/// of the original mapping, so the protection flags are recorded here.
+pub(crate) struct GpuVaData {
+ /// Memory protection flags of this mapping, as passed to `pt_map`.
+ prot: u32,
+}
+
/// GPU virtual address space.
///
/// Each VM can be mapped into a hardware address space slot.
@@ -599,7 +630,7 @@ impl<'drm> DriverGpuVm for GpuVmData<'drm> {
type Driver = TyrDrmDriver;
type Object = Bo;
type VmBoData = ();
- type VaData = ();
+ type VaData = GpuVaData;
type SmContext<'ctx>
= PtUpdateContext<'ctx, 'drm>
where
@@ -701,7 +732,7 @@ fn sm_step_map<'op>(
}
let gpuva = context.preallocated_gpuva()?;
- let op = op.insert(gpuva, pin_init::init_zeroed());
+ let op = op.insert(gpuva, GpuVaData { prot });
Ok(op)
}
@@ -749,10 +780,50 @@ fn sm_step_remap<'op>(
op.va_to_unmap().addr() + op.va_to_unmap().length()
};
- let unmap_length = unmap_end - unmap_start;
+ // The surviving fragments inherit the protection of the mapping being
+ // split.
+ let prot = op.va_to_unmap().data_ref().prot;
+
+ let block = Alignment::SZ_2M;
+ let aligned_start = unmap_start.align_down(block);
+ let aligned_end = unmap_end.align_up(block).ok_or(EINVAL)?;
+
+ // An end may only be expanded if the fragment the expansion sweeps and
+ // rebuilds is fully mapped and physically contiguous, because the
+ // rebuild maps it linearly from a single recovered address. A genuine
+ // 2MB block is always contiguous, so this never misses one and the
+ // partial-block unmap can never reach arm-lpae. A fragmented region is
+ // never expanded, so its sub-range unmap stays correct.
+ let head_paddr = match op.prev() {
+ Some(prev) if aligned_start < unmap_start && prev.addr() <= aligned_start => {
+ contiguous_phys(context.pt, aligned_start..unmap_start)
+ }
+ _ => None,
+ };
+ let tail_paddr = match op.next() {
+ Some(next) if aligned_end > unmap_end && next.addr() + next.length() >= aligned_end => {
+ contiguous_phys(context.pt, unmap_end..aligned_end)
+ }
+ _ => None,
+ };
+
+ let region_start = if head_paddr.is_some() {
+ aligned_start
+ } else {
+ unmap_start
+ };
+ let region_end = if tail_paddr.is_some() {
+ aligned_end
+ } else {
+ unmap_end
+ };
+
+ let prev_va = context.preallocated_gpuva()?;
+ let next_va = context.preallocated_gpuva()?;
- if unmap_length > 0 {
- let region = unmap_start..(unmap_start + unmap_length);
+ 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| {
dev_err!(
context.dev,
@@ -764,14 +835,29 @@ fn sm_step_remap<'op>(
})?;
}
- let prev_va = context.preallocated_gpuva()?;
- let next_va = context.preallocated_gpuva()?;
+ if let Some(head_paddr) = head_paddr {
+ pt_map(
+ context.dev,
+ context.pt,
+ aligned_start,
+ head_paddr,
+ unmap_start - aligned_start,
+ prot,
+ )?;
+ }
+ if let Some(tail_paddr) = tail_paddr {
+ pt_map(
+ context.dev,
+ context.pt,
+ unmap_end,
+ tail_paddr,
+ aligned_end - unmap_end,
+ prot,
+ )?;
+ }
- let (op_remapped, remap_ret) = op.remap(
- [prev_va, next_va],
- pin_init::init_zeroed(),
- pin_init::init_zeroed(),
- );
+ let (op_remapped, remap_ret) =
+ op.remap([prev_va, next_va], GpuVaData { prot }, GpuVaData { prot });
if let Some(unused_va) = remap_ret.unused_va {
context.return_preallocated_gpuva(unused_va);
@@ -781,6 +867,42 @@ fn sm_step_remap<'op>(
}
}
+/// Returns the base physical address of `range` if every 4KB page in it is
+/// mapped and the whole range is physically contiguous, otherwise `None`.
+///
+/// `range` is a sub-block fragment, so its length is below 2MB and the walk
+/// visits at most 511 pages.
+fn contiguous_phys(pt: &IoPageTable<'_, ARM64LPAES1>, range: Range<u64>) -> Option<u64> {
+ let start_usize = usize::try_from(range.start).ok()?;
+
+ // SAFETY: The page table is exclusively accessed through the
+ // `&mut UniqueRefGpuVm` held under the `gpuvm_unique` mutex for the duration of
+ // the VM update, so no other io-pgtable operation runs concurrently.
+ let base = unsafe { pt.iova_to_phys(start_usize) }?;
+ // Expressly convert to u64 to work with arm 32-bit builds.
+ #[allow(clippy::useless_conversion)]
+ let base = u64::from(base);
+
+ let mut iova = range.start + u64::SZ_4K;
+ while iova < range.end {
+ let offset = iova - range.start;
+ let iova_usize = usize::try_from(iova).ok()?;
+ // SAFETY: The page table is exclusively accessed through the
+ // `&mut UniqueRefGpuVm` held under the `gpuvm_unique` mutex, so no
+ // concurrent io-pgtable operation runs.
+ let paddr = unsafe { pt.iova_to_phys(iova_usize) }?;
+ // Expressly convert to u64 to work with arm 32-bit builds.
+ #[allow(clippy::useless_conversion)]
+ let paddr = u64::from(paddr);
+ if paddr != base + offset {
+ return None;
+ }
+ iova += u64::SZ_4K;
+ }
+
+ Some(base)
+}
+
/// This function selects the largest supported block size (currently 4KB or 2MB)
/// that can be used for a mapping at the given address and size, respecting alignment constraints.
///
--
2.56.1.703.gf848ed807c-goog
next prev parent reply other threads:[~2026-11-17 19:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 19:32 [PATCH 0/4] drm/tyr: fix partial 2MB block unmaps on GPUVM remap Alice Ryhl
2026-11-17 19:32 ` [PATCH 1/4] rust: drm/gpuvm: add GpuVa::data_ref accessor Alice Ryhl
2026-11-17 19:40 ` Danilo Krummrich
2026-11-17 19:32 ` [PATCH 2/4] rust: iommu: pgtable: add iova_to_phys Alice Ryhl
2026-11-17 19:32 ` Alice Ryhl [this message]
2026-11-17 19:32 ` [PATCH 4/4] drm/tyr: vm: check the remaining length in pt_unmap() Alice Ryhl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261117-tyr-remap-2mb-v1-3-2a8efd4c8180@google.com \
--to=aliceryhl@google.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=boris.brezillon@collabora.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=deborah.brouwer@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=gary@garyguo.net \
--cc=iommu@lists.linux.dev \
--cc=joro@8bytes.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=matthew.brost@intel.com \
--cc=ojeda@kernel.org \
--cc=robin.murphy@arm.com \
--cc=rust-for-linux@vger.kernel.org \
--cc=samitolvanen@google.com \
--cc=tamird@kernel.org \
--cc=thomas.hellstrom@linux.intel.com \
--cc=tmgross@umich.edu \
--cc=will@kernel.org \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®