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 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 <[email protected]> Fixes: 8e7460eac786 ("drm/panthor: Support partial unmaps of huge pages") Signed-off-by: Boris Brezillon <[email protected]> --- 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 { + const struct page *pg = bo->backing.pages[bo_offset >> PAGE_SHIFT]; - return folio_size(page_folio(pg)) >= SZ_2M; + /* 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)) + return false; + + return folio_size(page_folio(pg)) >= SZ_2M; + } } static void -- 2.55.0
