drm/tyr: fix partial 2MB block unmaps on GPUVM remap
Subsystems (6)
Alice Ryhl
maintainercover drm/tyr: fix partial 2MB block unmaps on GPUVM remap
Tyr configures its ARM64 LPAE page tables with 4KB and 2MB block sizes, but arm-lpae rejects unmapping part of a 2MB block. As a result, a partial unmap or overlapping remap that splits a 2MB block in GpuVmData::sm_step_remap() fails in __arm_lpae_unmap().
This series ports the remap fix and supporting accessors from tyr-dev onto drm-rust-next, along with a defensive bounds check in pt_unmap():
1. Add the GpuVa::data_ref() accessor.
2. Add the IoPageTable::iova_to_phys() accessor.
3. Expand sub-block unmaps in sm_step_remap() to 2MB boundaries when the swept head/tail fragment is physically contiguous, widen the AS lock over the expanded range, rebuild the surviving fragments, and take the preallocated GPUVAs before mutating page tables.
4. Check the remaining-length subtraction in pt_unmap() against underflow.
---
Sami Tolvanen (4):
rust: drm/gpuvm: add GpuVa::data_ref accessor
rust: iommu: pgtable: add iova_to_phys
drm/tyr: vm: expand sub-block unmaps to 2MB boundaries on remap drm/tyr: vm: check the remaining length in pt_unmap()
drivers/gpu/drm/tyr/mmu.rs | 13 +++ drivers/gpu/drm/tyr/mmu/address_space.rs | 29 ++++++ drivers/gpu/drm/tyr/vm.rs | 152 ++++++++++++++++++++++++++++--- rust/kernel/drm/gpuvm/va.rs | 6 ++ rust/kernel/iommu/pgtable.rs | 23 +++++ 5 files changed, 209 insertions(+), 14 deletions(-)
---
base-commit: 8045d0e7d208ad389a387f1407ce0db9070805af
change-id: 20261116-tyr-remap-2mb-7a53b8475cfc
Best regards,
Signature · 1 line
Alice Ryhl <[email protected]>
Alice Ryhl
maintainerPATCH 1/4 rust: drm/gpuvm: add GpuVa::data_ref accessor
From: Sami Tolvanen <[email protected]>
Tyr's VM_BIND remap path rebuilds page-table mappings for the fragments that survive a split, and those fragments must keep the protection flags of the original mapping. Add GpuVa::data_ref() so the remap step can read the VaData of an existing mapping.
--- rust/kernel/drm/gpuvm/va.rs | 6 ++++++ 1 file changed, 6 insertions(+)
rust/kernel/drm/gpuvm/va.rs +6 -0
@@ -87,6 +87,12 @@ pub fn obj(&self) -> &T::Object {unsafe { <T ::Object as IntoGEMObject >::from_raw ((*self.as_raw ()).gem.obj) } } +/// Access the driver-private [`VaData`](DriverGpuVm::VaData) associated with this mapping. +#[inline] +pub fn data_ref (&self) -> &T ::VaData { + &self.data + } +/// Returns the underlying [`GpuVmBo`] object that backs this [`GpuVa`]. #[inline] pub fn vm_bo (&self) -> &GpuVmBo <T > {
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Re: [PATCH 1/4] rust: drm/gpuvm: add GpuVa::data_ref accessor
Quoted 10 lines · Alice Ryhl
From: Sami Tolvanen <[email protected]>
Tyr's
VM_BINDremap path rebuilds page-table mappings for the fragments that survive a split, and those fragments must keep the protection flags of the original mapping. AddGpuVa::data_ref()so the remap step can read the VaData of an existing mapping.Signed-off-by Sami Tolvanen <[email protected]>[aliceryhl: split into separate gpuvm and iommu commits]Signed-off-by Alice Ryhl <[email protected]>
One question below, otherwise:
Quoted 9 lines
--- rust/kernel/drm/gpuvm/va.rs | 6 ++++++ 1 file changed, 6 insertions(+)rust/kernel/drm/gpuvm/va.rs +0
-0
rust/kernel/drm/gpuvm/va.rs pub fn obj(&self) -> &T::Object { · line 95
Quoted 9 lines
rust/kernel/drm/gpuvm/va.rs +6
-0@@ -87,6 +87,12 @@ pub fn obj(&self) -> &T::Object {unsafe { <T ::Object as IntoGEMObject >::from_raw ((*self.as_raw ()).gem.obj) } } +/// Access the driver-private [`VaData`](DriverGpuVm::VaData) associated with this mapping. +#[inline] +pub fn data_ref (&self) -> &T ::VaData { + &self.data + } +
Why data_ref() instead of just data() or a Deref impl?
Alice Ryhl
maintainerRe: [PATCH 1/4] rust: drm/gpuvm: add GpuVa::data_ref accessor
Quoted 7 lines · Danilo Krummrich
rust/kernel/drm/gpuvm/va.rs pub fn obj(&self) -> &T::Object { · line 95
Quoted 6 lines
rust/kernel/drm/gpuvm/va.rs +6
-0+/// Access the driver-private [`VaData`](DriverGpuVm::VaData) associated with this mapping. +#[inline] +pub fn data_ref (&self) -> &T ::VaData { + &self.data + } +Why
data_ref()instead of justdata()or a Deref impl?
I guess this came from mirroring UniqueRefGpuVm, which uses the names:data_ref(&self) and data(&mut self).
I think it makes more sense to rename to data(&self)/data_mut(&mut self) for consistency with other similar APIs. But we should also update UniqueRefGpuVm if so.
We don't want to go the Deref/DerefMut route because mutable access needs to return a Pin<&mut GpuVa<T>> which doesn't work with DerefMut.
Alice
Alice Ryhl
maintainerPATCH 2/4 rust: iommu: pgtable: add iova_to_phys
In reply to Alice RyhlFrom: Sami Tolvanen <[email protected]>
Tyr's VM_BIND remap path rebuilds the page-table mappings that a partial unmap sweeps away, recovering their backing from the still-present entries. Add IoPageTable::iova_to_phys(), which returns the physical address that an iova maps to.
--- rust/kernel/iommu/pgtable.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
rust/kernel/iommu/pgtable.rs +23 -0
@@ -199,6 +199,29 @@ pub unsafe fn unmap_pages(&self, iova: usize, pgsize: usize, pgcount: usize) ->// SAFETY: The safety requirements of this method are sufficient to call `unmap_pages`. unsafe { (unmap_pages)(self.raw_ops (), iova, pgsize, pgcount, core::ptr::null_mut ()) } } + +/// Translate an `iova` to the physical address it maps to. +/// +/// Returns [`None`] if `iova` is not mapped. As in the underlying C op, a mapping to physical +/// address 0 is indistinguishable from unmapped and also reports [`None`]. The returned address +/// includes the offset of `iova` within the mapping block, so it is exact for any `iova`, not +/// just block-aligned ones. This walk is lock-free and does not allocate. +/// +/// # Safety +/// +/// No other io-pgtable operation may modify the entry covering `iova` while this walk +/// executes. +#[inline] +pub unsafe fn iova_to_phys (&self, iova:usize ) ->Option <PhysAddr > { +// SAFETY: The `iova_to_phys` function in `io_pgtable_ops` is never null. +let iova_to_phys =unsafe { (*self.raw_ops ()).iova_to_phys.unwrap_unchecked () }; + +// SAFETY: The safety requirements of this method are sufficient to call `iova_to_phys`. +let paddr =unsafe { (iova_to_phys)(self.raw_ops (), iova) }; + +// A zero return means `iova` is not mapped. + (paddr !=0 ).then_some (paddr) + } }// For the initial users of these rust bindings, the GPU FW is managing the IOTLB and performs all
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Alice Ryhl
maintainerRe: [PATCH 2/4] rust: iommu: pgtable: add iova_to_phys
Quoted 11 lines · Alice Ryhl
From: Sami Tolvanen <[email protected]>
Tyr's
VM_BINDremap path rebuilds the page-table mappings that a partial unmap sweeps away, recovering their backing from the still-present entries. AddIoPageTable::iova_to_phys(), which returns the physical address that an iova maps to.Signed-off-by Sami Tolvanen <[email protected]>[aliceryhl: split into separate gpuvm and iommu commits]Signed-off-by Alice Ryhl <[email protected]>
Joerg, Will, any objections to merging this through drm-rust-next?
Alice Ryhl
maintainerPATCH 3/4 drm/tyr: vm: expand sub-block unmaps to 2MB boundaries on remap
In reply to Alice RyhlFrom: Sami Tolvanen <[email protected]>
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.
--- 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(-)
drivers/gpu/drm/tyr/mmu.rs +13 -0
@@ -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
drivers/gpu/drm/tyr/mmu/address_space.rs +29 -0
@@ -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);
drivers/gpu/drm/tyr/vm.rs +134 -12
197 lines not on this page · raw message
Signature · 1 line
2.56.1.703.gf848ed807c-goog
[email protected]
AI reviewRe: [PATCH 3/4] drm/tyr: vm: expand sub-block unmaps to 2MB boundaries on remap
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/tyr: page table desync and GpuVa leak on pt_map() error in sm_step_remap() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/tyr: page table desync and GpuVa leak on pt_map() error in sm_step_remap()
--
commit 890e6d1a0c182be186e5d6f90855d935bae30eda
Author: Sami Tolvanen <[email protected]>
drm/tyr: vm: expand sub-block unmaps to 2MB boundaries on remap
This commit expands sub-block unmaps in sm_step_remap() to 2MB boundaries when the surviving fragments are physically contiguous and remaps the surviving head and tail fragments as 4KB pages.
Quoted 4 lines
drivers/gpu/drm/tyr/vm.rs +0
-0
[ ... ]
Quoted 78 lines
drivers/gpu/drm/tyr/vm.rs +61
-5@@ -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,
drivers/gpu/drm/tyr/vm.rs fn sm_step_remap<'op>( · line 860
Quoted 10 lines
drivers/gpu/drm/tyr/vm.rs +4
-5+ )?; + } -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 });
[Severity: Medium]
Can a pt_map() failure here leave context.pt out of sync with GpuVm and leak the GpuVa?
pt_unmap() has already torn down the expanded 2MB range in context.pt before pt_map() allocates L3 page tables for the surviving 4KB fragments. If either pt_map() fails with ENOMEM, sm_step_remap() returns without restoring the unmapped range in context.pt or updating the GpuVm tree via op.remap().
When Vm::kill() or Vm::unmap_range() later calls sm_step_unmap() on the unsplit GpuVa, pt_unmap() hits the already-unmapped PTEs, triggers a WARN_ON in __arm_lpae_unmap(), and returns Err(EINVAL) before op.remove() can unlink and free the GpuVa and its GEM BO references.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Alice Ryhl
maintainerRe: [PATCH 3/4] drm/tyr: vm: expand sub-block unmaps to 2MB boundaries on remap
Quoted 121 lines · On Tue, Nov 17, 2026 at 9:02 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/tyr: page table desync and GpuVa leak onpt_map()error insm_step_remap()
--commit
890e6d1a0c182be186e5d6f90855d935bae30eda
Author: Sami Tolvanen <[email protected]>drm/tyr: vm: expand sub-block unmaps to 2MB boundaries on remap
This commit expands sub-block unmaps in
sm_step_remap()to 2MB boundaries when the surviving fragments are physically contiguous and remaps the surviving head and tail fragments as 4KB pages.Quoted 4 lines
drivers/gpu/drm/tyr/vm.rs +0
-0[ ... ]
Quoted 78 lines
drivers/gpu/drm/tyr/vm.rs +61
-5@@ -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,drivers/gpu/drm/tyr/vm.rs fn sm_step_remap<'op>( · line 860
Quoted 10 lines
drivers/gpu/drm/tyr/vm.rs +4
-5+ )?; + } -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 });[Severity: Medium]
Can apt_map()failure here leave context.pt out of sync with GpuVm and leak the GpuVa?
pt_unmap()has already torn down the expanded 2MB range in context.pt beforept_map()allocates L3 page tables for the surviving 4KB fragments. If eitherpt_map()fails with ENOMEM,sm_step_remap()returns without restoring the unmapped range in context.pt or updating the GpuVm tree viaop.remap().
Quoted 4 lines
When
Vm::kill()orVm::unmap_range()later callssm_step_unmap()on the unsplit GpuVa,pt_unmap()hits the already-unmapped PTEs, triggers aWARN_ONin__arm_lpae_unmap(), and returns Err(EINVAL) beforeop.remove()can unlink and free the GpuVa and its GEM BO references.
This is valid, but allocation failure in pt_map() is a pre-existing problem. It will go away once these page table entries are pre-allocated.
alice
Alice Ryhl
maintainerPATCH 4/4 drm/tyr: vm: check the remaining length in pt_unmap()
In reply to Alice RyhlFrom: Sami Tolvanen <[email protected]>
pt_unmap() subtracts each unmapped chunk from the remaining length and relies on unmap_pages() never unmapping more than it was asked to.
Check this subtraction and fail with EINVAL on underflow.
--- drivers/gpu/drm/tyr/vm.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/tyr/vm.rs +4 -2
@@ -41,6 +41,7 @@ ARM64LPAES1, // }, new_mutex, + num::casts::FromSafeCast, prelude::*, ptr::{Alignable ,
@@ -1063,8 +1064,9 @@ fn pt_unmap(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range<u64>)return Err (EINVAL ); } - bytes_left_to_unmap -= unmappedas u64 ; - iova += unmappedas u64 ; +let unmapped = u64::from_safe_cast (unmapped); + bytes_left_to_unmap = bytes_left_to_unmap.checked_sub (unmapped).ok_or (EINVAL )?; + iova += unmapped; }Ok (())
Signature · 1 line
2.56.1.703.gf848ed807c-goog
Quoted 8 lines · Alice Ryhl
Tyr configures its ARM64 LPAE page tables with 4KB and 2MB block sizes, but arm-lpae rejects unmapping part of a 2MB block. As a result, a partial unmap or overlapping remap that splits a 2MB block in
GpuVmData::sm_step_remap()fails in__arm_lpae_unmap().This series ports the remap fix and supporting accessors from tyr-dev onto drm-rust-next, along with a defensive bounds check in
pt_unmap():
Quoted 8 lines
1. Add the
GpuVa::data_ref()accessor.
2. Add theIoPageTable::iova_to_phys()accessor.
3. Expand sub-block unmaps insm_step_remap()to 2MB boundaries when the swept head/tail fragment is physically contiguous, widen the AS lock over the expanded range, rebuild the surviving fragments, and take the preallocated GPUVAs before mutating page tables.
4. Check the remaining-length subtraction inpt_unmap()against underflow.
Thank you for this series. I tested it on my Rock5B with the latest Tyr ioctl series [1] and I can no longer trigger the unmap WARNING
"Unmap of a partial large IOPTE is not allowed".
So, for the whole series:
[1] https://lore.kernel.org/rust-for-linux/[email protected]/
Quoted 23 lines
Signed-off-by Alice Ryhl <[email protected]>---
Sami Tolvanen (4):
rust: drm/gpuvm: addGpuVa::data_refaccessor
rust: iommu: pgtable: addiova_to_phys
drm/tyr: vm: expand sub-block unmaps to 2MB boundaries on remap drm/tyr: vm: check the remaining length inpt_unmap()drivers/gpu/drm/tyr/mmu.rs | 13 +++ drivers/gpu/drm/tyr/mmu/address_space.rs | 29 ++++++ drivers/gpu/drm/tyr/vm.rs | 152 ++++++++++++++++++++++++++++--- rust/kernel/drm/gpuvm/va.rs | 6 ++ rust/kernel/iommu/pgtable.rs | 23 +++++ 5 files changed, 209 insertions(+), 14 deletions(-)---
base-commit:8045d0e7d208ad389a387f1407ce0db9070805af
change-id: 20261116-tyr-remap-2mb-7a53b8475cfcBest regards,
Signature · 1 line
Alice Ryhl <[email protected]>