[PATCH v3 06/14] drm/panfrost: Give back the pages of a heap chunk that failed to map

From: Arun Iyengar via B4 Relay

Date: Fri Nov 13 2026 - 08:30:08 EST


From: Arun Iyengar <aiyengar1990@xxxxxxxxx>

panfrost_mmu_map_fault_addr() grows a heap BO by one 2 MB chunk per
fault. If reading the chunk's folios, building its sg table, DMA mapping
it or mapping it on the GPU fails, the folios already read stay in
bo->base.pages while the chunk has no sg table. The next fault in the
chunk reads them again, finds their pages owned and hits

if (drm_WARN_ON(&pfdev->base, already_owned))
folio_put(folio);

which commit b49c35ba0ce1 ("drm/panfrost: Fix a page leak in
panfrost_mmu_map_fault_addr() when THP is on") added; before it, pages
already filled in were skipped.

On those error paths, put the folios read and clear their entries. The
range to clear is what the fill loop wrote, whole folios: a folio larger
than 2 MB (possible with 16K or 64K pages) also fills entries of the
neighbouring chunks, and a folio the loop found already owned (by such a
neighbour) is not ours to put.

Tested with 4K pages, where a folio never crosses a chunk; the
larger-folio case is from reading the code.

Fixes: b49c35ba0ce1 ("drm/panfrost: Fix a page leak in panfrost_mmu_map_fault_addr() when THP is on")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Arun Iyengar <aiyengar1990@xxxxxxxxx>
---
drivers/gpu/drm/panfrost/panfrost_mmu.c | 45 ++++++++++++++++++++++++++++++---
1 file changed, 41 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/panfrost/panfrost_mmu.c b/drivers/gpu/drm/panfrost/panfrost_mmu.c
index efa4a3bb1..e1c4e57d0 100644
--- a/drivers/gpu/drm/panfrost/panfrost_mmu.c
+++ b/drivers/gpu/drm/panfrost/panfrost_mmu.c
@@ -596,6 +596,30 @@ addr_to_mapping(struct panfrost_device *pfdev, int as, u64 addr)

#define NUM_FAULT_PAGES (SZ_2M / PAGE_SIZE)

+/*
+ * Put the folios a failed heap fault took and clear their page array entries.
+ * [start, end) covers whole folios, so it can reach past the 2 MB chunk.
+ */
+static void panfrost_heap_put_pages(struct page **pages, pgoff_t start,
+ pgoff_t end)
+{
+ while (start < end) {
+ struct folio *folio;
+
+ if (!pages[start]) {
+ start++;
+ continue;
+ }
+
+ folio = page_folio(pages[start]);
+ do {
+ pages[start++] = NULL;
+ } while (start < end && pages[start] &&
+ page_folio(pages[start]) == folio);
+ folio_put(folio);
+ }
+}
+
static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
u64 addr)
{
@@ -604,7 +628,7 @@ static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
struct panfrost_gem_object *bo;
struct address_space *mapping;
struct drm_gem_object *obj;
- pgoff_t page_offset, nr_pages;
+ pgoff_t page_offset, nr_pages, got_start, got_end;
struct sg_table *sgt;
struct page **pages;

@@ -663,17 +687,23 @@ static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
mapping = bo->base.base.filp->f_mapping;
mapping_set_unevictable(mapping);

+ /* The page array range whose folio references this fault takes. */
+ got_start = page_offset;
+ got_end = page_offset;
+
for (pgoff_t pg = page_offset; pg < page_offset + NUM_FAULT_PAGES;) {
bool already_owned = false;
struct folio *folio;
+ pgoff_t first;

folio = shmem_read_folio(mapping, pg);
if (IS_ERR(folio)) {
ret = PTR_ERR(folio);
- goto err_unlock;
+ goto err_put_chunk;
}

pg &= ~(folio_nr_pages(folio) - 1);
+ first = pg;
for (u32 i = 0; i < folio_nr_pages(folio) && pg < nr_pages; i++) {
if (pages[pg])
already_owned = true;
@@ -685,14 +715,18 @@ static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
* there's no valid reason for a folio range to be partially
* populated.
*/
- if (drm_WARN_ON(&pfdev->base, already_owned))
+ if (drm_WARN_ON(&pfdev->base, already_owned)) {
folio_put(folio);
+ } else {
+ got_start = min(got_start, first);
+ got_end = max(got_end, pg);
+ }
}

ret = sg_alloc_table_from_pages(sgt, pages + page_offset,
NUM_FAULT_PAGES, 0, SZ_2M, GFP_KERNEL);
if (ret)
- goto err_unlock;
+ goto err_put_chunk;

ret = dma_map_sgtable(pfdev->base.dev, sgt, DMA_BIDIRECTIONAL, 0);
if (ret)
@@ -719,6 +753,9 @@ static int panfrost_mmu_map_fault_addr(struct panfrost_device *pfdev, int as,
dma_unmap_sgtable(pfdev->base.dev, sgt, DMA_BIDIRECTIONAL, 0);
err_map:
sg_free_table(sgt);
+err_put_chunk:
+ /* The next fault in this chunk must find its page range empty. */
+ panfrost_heap_put_pages(pages, got_start, got_end);
err_unlock:
dma_resv_unlock(obj->resv);
err_bo:

--
2.55.0