Re: [PATCH v2 2/5] drm/panthor: Fix iova_mapped_as_huge_page() for imported BOs

From: Boris Brezillon

Date: Thu Dec 03 2026 - 04:47:05 EST


On Mon, 5 Oct 2026 11:28:43 +0100
Steven Price <steven.price@xxxxxxx> wrote:

> On 24/09/2026 12:04, Boris Brezillon wrote:
> > Imported BOs have no backing.pages array allocated, leading to a NULL
> > deref when iova_mapped_as_huge_page() gets called on them.
> >
> > Implement this check through and sgt walk to reach the position of the
> NIT: ^^^
> s/and/an/
>
> > sgt targeted by a VA, and check that the DMA address is properly aligned
> > and the size remaining in the SG entry is bigger than a huge page.
> > This is basically matching the logic in vm_map_pages(), with get_pgsize()
> > being replaced by a simpler test, because we don't care about the pgcount
> > info.
> >
> > Reported-by: Akash Goel <akash.goel@xxxxxxx>
> > Fixes: 8e7460eac786 ("drm/panthor: Support partial unmaps of huge pages")
> > Reviewed-by: Liviu Dudau <liviu.dudau@xxxxxxx>
> > Reviewed-by: Akash Goel <akash.goel@xxxxxxx>
> > Signed-off-by: Boris Brezillon <boris.brezillon@xxxxxxxxxxxxx>
>
> Reviewed-by: Steven Price <stevne.price@xxxxxxx>
>
> Although there's a minor style issue below if you do respin (see below).
>
> > ---
> > drivers/gpu/drm/panthor/panthor_mmu.c | 44 ++++++++++++++++++++++++++++-------
> > 1 file changed, 36 insertions(+), 8 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/panthor/panthor_mmu.c b/drivers/gpu/drm/panthor/panthor_mmu.c
> > index b0a7033480e6..6cef954e2cba 100644
> > --- a/drivers/gpu/drm/panthor/panthor_mmu.c
> > +++ b/drivers/gpu/drm/panthor/panthor_mmu.c
> > @@ -2299,7 +2299,6 @@ iova_mapped_as_huge_page(struct drm_gpuva *mapping, u64 va)
> > {
> > struct panthor_gem_object *bo = to_panthor_bo(mapping->gem.obj);
> > u64 aligned_va = ALIGN_DOWN(va, SZ_2M);
> > - const struct page *pg;
> > pgoff_t bo_offset;
> >
> > /* If the 2M-aligned VA is outside the mapping being tested, we know
> > @@ -2309,16 +2308,45 @@ iova_mapped_as_huge_page(struct drm_gpuva *mapping, u64 va)
> > return false;
> >
> > bo_offset = aligned_va - mapping->va.addr + mapping->gem.offset;
> > - pg = bo->backing.pages[bo_offset >> PAGE_SHIFT];
> >
> > - /* In case of shmem backing, we know we can only have a huge mapping
> > - * if the bo_offset is 2M aligned, meaning we can skip the folio size
> > - * check if it's not the case.
> > - */
> > - if (!IS_ALIGNED(bo_offset, SZ_2M))
> > + if (drm_gem_is_imported(&bo->base)) {
> > + struct sg_table *sgt = bo->dmap.sgt;
> > + struct scatterlist *sgl;
> > + unsigned int count;
> > +
> > + /* If this is an imported BO, we have to walk the SGT and
> > + * check the dma address/size alignment to determine if it's
> > + * a huge map or not.
> > + */
> > + for_each_sgtable_dma_sg(sgt, sgl, count) {
> > + size_t len = sg_dma_len(sgl);
> > + dma_addr_t daddr;
> > +
> > + if (len <= bo_offset) {
> > + bo_offset -= len;
> > + continue;
> > + }
> > +
> > + len -= bo_offset;
> > + daddr = sg_dma_address(sgl) + bo_offset;
> > +
> > + return IS_ALIGNED(daddr, SZ_2M) &&
> > + len >= SZ_2M;
> > + }
> > +
> > return false;
> > + } else {
>
> This 'else' branch isn't needed - since the above branch returns early.
> Dropping the below back out of the braces would make the diff easier to
> read - effectively the drm_gem_is_imported() branch is just added and
> the rest of the code is left as is.

I ended up not going for this approach, so the pg variable is scoped to
this branch instead of being declared at the top, with the risk of
dereferencing it when it's uninitialized in other places. We can always
change that in a follow-up patch if you think it's still preferable
over the extra indentation level the else branch adds.