[PATCH v3 05/14] drm/panfrost: Unwind a failed GPU mapping without dropping its pages

From: Arun Iyengar via B4 Relay

Date: Fri Nov 13 2026 - 04:27:27 EST


From: Arun Iyengar <aiyengar1990@xxxxxxxxx>

Two error paths of a BO mapping leave wrong state behind when an
allocation fails part-way (memory cgroup limit, io-pgtable out of
memory):

- When mmu_map_sg() fails, panfrost_mmu_map() calls
drm_gem_shmem_put_pages_locked(), without the reservation lock it
asserts. The only page reference drm_gem_shmem_get_pages_sgt() took
belongs to the sg table cached in shmem->sgt, so this drops pages that
shmem->sgt still maps (for an imported BO it underflows
pages_use_count). Keep the pages and return the error.

- When ops->map_pages() fails, mmu_map_sg() does not unmap what the
failing call itself had mapped (reported in @mapped). Those PTEs stay
in a VA range that drm_mm hands out again. Unmap that part too, and
flush the unmapped range.

Fixes: 4da352cf8fb1 ("drm/panfrost: Handle page mapping failure")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Arun Iyengar <aiyengar1990@xxxxxxxxx>
---
drivers/gpu/drm/panfrost/panfrost_mmu.c | 17 ++++++++++-------
1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/gpu/drm/panfrost/panfrost_mmu.c b/drivers/gpu/drm/panfrost/panfrost_mmu.c
index 74fd159de..efa4a3bb1 100644
--- a/drivers/gpu/drm/panfrost/panfrost_mmu.c
+++ b/drivers/gpu/drm/panfrost/panfrost_mmu.c
@@ -442,8 +442,11 @@ static int mmu_map_sg(struct panfrost_device *pfdev, struct panfrost_mmu *mmu,

ret = ops->map_pages(ops, iova, paddr, pgsize, pgcount, prot,
GFP_KERNEL, &mapped);
- if (ret)
+ if (ret) {
+ /* Unwind the pages this call mapped too. */
+ total_mapped += mapped;
goto err_unmap_pages;
+ }

/* Don't get stuck if things have gone wrong */
mapped = max(mapped, pgsize);
@@ -459,7 +462,10 @@ static int mmu_map_sg(struct panfrost_device *pfdev, struct panfrost_mmu *mmu,
return 0;

err_unmap_pages:
- mmu_unmap_range(mmu, start_iova, total_mapped);
+ if (total_mapped) {
+ mmu_unmap_range(mmu, start_iova, total_mapped);
+ panfrost_mmu_flush_range(pfdev, mmu, start_iova, total_mapped);
+ }
return ret;
}

@@ -483,18 +489,15 @@ int panfrost_mmu_map(struct panfrost_gem_mapping *mapping)
if (IS_ERR(sgt))
return PTR_ERR(sgt);

+ /* On failure keep the pages: shmem->sgt holds their only reference. */
ret = mmu_map_sg(pfdev, mapping->mmu, mapping->mmnode.start << PAGE_SHIFT,
prot, sgt);
if (ret)
- goto err_put_pages;
+ return ret;

mapping->active = true;

return 0;
-
-err_put_pages:
- drm_gem_shmem_put_pages_locked(shmem);
- return ret;
}

void panfrost_mmu_unmap(struct panfrost_gem_mapping *mapping)

--
2.55.0