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.

> diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs
> index 1dd11b2b12532..bcc864b174284 100644
> --- a/drivers/gpu/drm/tyr/vm.rs
> +++ b/drivers/gpu/drm/tyr/vm.rs

[ ... ]

> @@ -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 });

[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.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=3

Reply via email to