On Sat, 13 Dec 2025 19:08:33 +0000
Adrián Larumbe <[email protected]> wrote:

> Commit 33729a5fc0ca ("iommu/io-pgtable-arm: Remove split on unmap
> behavior") did away with the treatment of partial unmaps of huge IOPTEs.
> 
> In the case of Panthor, that means an attempt to run a VM_BIND unmap
> operation on a memory region whose start address and size aren't 2MiB
> aligned, in the event it intersects with a huge page, would lead to ARM
> IOMMU management code to fail and a warning being raised.
> 
> Presently, and for lack of a better alternative, it's best to have
> Panthor handle partial unmaps at the driver level, by unmapping entire
> huge pages and remapping the difference between them and the requested
> unmap region.
> 
> This could change in the future when the VM_BIND uAPI is expanded to
> enforce huge page alignment and map/unmap operational constraints that
> render this code unnecessary.
> 
> Signed-off-by: Adrián Larumbe <[email protected]>
> ---
>  drivers/gpu/drm/panthor/panthor_mmu.c | 66 +++++++++++++++++++++++++++
>  1 file changed, 66 insertions(+)
> 
> diff --git a/drivers/gpu/drm/panthor/panthor_mmu.c 
> b/drivers/gpu/drm/panthor/panthor_mmu.c
> index 183da30fa500..f11340a7f59e 100644
> --- a/drivers/gpu/drm/panthor/panthor_mmu.c
> +++ b/drivers/gpu/drm/panthor/panthor_mmu.c
> @@ -2110,6 +2110,44 @@ static int panthor_gpuva_sm_step_map(struct 
> drm_gpuva_op *op, void *priv)
>       return 0;
>  }
>  
> +static bool
> +iova_mapped_as_huge_page(const struct panthor_vma *vma, u64 addr)
> +{
> +     const struct page *pg;
> +     pgoff_t bo_offset;
> +
> +     bo_offset = addr - vma->base.va.addr + vma->base.gem.offset;
> +     pg = to_panthor_bo(vma->base.gem.obj)->base.pages[bo_offset >> 
> PAGE_SHIFT];
> +
> +     return (folio_size(page_folio(pg)) >= SZ_2M);

nit: you can drop the extra ()

        return folio_size(page_folio(pg)) >= SZ_2M;

> +}
> +
> +static void
> +get_map_unmap_intervals(const struct drm_gpuva_op_remap *op,
> +                     const struct panthor_vma *unmap_vma,
> +                     u64 *unmap_start, u64 *unmap_range)
> +{
> +     u64 aligned_unmap_start, aligned_unmap_end, unmap_end;
> +
> +     drm_gpuva_op_remap_to_unmap_range(op, unmap_start, unmap_range);
> +     unmap_end = *unmap_start + *unmap_range;
> +
> +     aligned_unmap_start = ALIGN_DOWN(*unmap_start, SZ_2M);
> +     if (op->prev && aligned_unmap_start < *unmap_start &&
> +         op->prev->va.addr <= aligned_unmap_start &&
> +         iova_mapped_as_huge_page(unmap_vma, *unmap_start)) {
> +             *unmap_range += *unmap_start - aligned_unmap_start;
> +             *unmap_start = aligned_unmap_start;
> +     }
> +
> +     aligned_unmap_end = ALIGN(unmap_end, SZ_2M);
> +     if (op->next && aligned_unmap_end > unmap_end &&
> +         op->next->va.addr + op->next->va.range >= aligned_unmap_end &&
> +         iova_mapped_as_huge_page(unmap_vma, unmap_end - 1)) {
> +             *unmap_range += aligned_unmap_end - unmap_end;
> +     }
> +}
> +
>  static int panthor_gpuva_sm_step_remap(struct drm_gpuva_op *op,
>                                      void *priv)
>  {
> @@ -2121,16 +2159,44 @@ static int panthor_gpuva_sm_step_remap(struct 
> drm_gpuva_op *op,
>       int ret;
>  
>       drm_gpuva_op_remap_to_unmap_range(&op->remap, &unmap_start, 
> &unmap_range);
> +
> +     /*
> +      * ARM IOMMU page table management code disallows partial unmaps of 
> huge pages,
> +      * so when a partial unmap is requested, we must first unmap the entire 
> huge
> +      * page and then remap the difference between the huge page minus the 
> requested
> +      * unmap region. Calculating the right offsets and ranges for the 
> different unmap
> +      * and map operations is the responsibility of the following function.
> +      */
> +     get_map_unmap_intervals(&op->remap, unmap_vma, &unmap_start, 
> &unmap_range);

Unfortunately, after 5b8fcf4777e7 ("drm/panthor: Add support for atomic
page table updates"), that's not enough, you also need to extend the
locked region (see [1]).

> +
>       ret = panthor_vm_unmap_pages(vm, unmap_start, unmap_range);
>       if (ret)
>               return ret;
>  
>       if (op->remap.prev) {
> +             ret = panthor_vm_map_pages(vm, unmap_start,
> +                                        flags_to_prot(unmap_vma->flags),
> +                                        
> to_drm_gem_shmem_obj(op->remap.prev->gem.obj)->sgt,
> +                                        op->remap.prev->gem.offset +
> +                                        (unmap_start - 
> op->remap.prev->va.addr),
> +                                        op->remap.prev->va.addr + 
> op->remap.prev->va.range -
> +                                        unmap_start);
> +             if (ret)
> +                     return ret;
> +
>               prev_vma = panthor_vm_op_ctx_get_vma(op_ctx);
>               panthor_vma_init(prev_vma, unmap_vma->flags);
>       }
>  
>       if (op->remap.next) {
> +             ret = panthor_vm_map_pages(vm, op->remap.next->va.addr,
> +                                        flags_to_prot(unmap_vma->flags),
> +                                        
> to_drm_gem_shmem_obj(op->remap.next->gem.obj)->sgt,
> +                                        op->remap.next->gem.offset,
> +                                        unmap_start + unmap_range - 
> op->remap.next->va.addr);
> +             if (ret)
> +                     return ret;
> +
>               next_vma = panthor_vm_op_ctx_get_vma(op_ctx);
>               panthor_vma_init(next_vma, unmap_vma->flags);
>       }

[1]https://gitlab.freedesktop.org/bbrezillon/linux/-/commit/b4b677796c8c33b5be60184bca099ef8fd8c5548

Reply via email to