From: Matthew Brost <[email protected]>

commit df72e55e754c8d449321ddddad19a8bd3cb8d032 upstream.

drm_pagemap_migrate_populate_ram_pfn() had two issues when populating
RAM PFNs with higher-order folios:

1. The higher-order vma_alloc_folio()/folio_alloc() calls did not pass
   __GFP_NOWARN, so a THP allocation failure under memory pressure
   would spam the kernel log, and there was no fallback path despite a
   TODO comment stating one was needed. Add __GFP_NOWARN to the
   higher-order allocation and, on failure, fall back to order-0
   allocations for the entire range originally covered by the failed
   higher-order allocation, leaving MIGRATE_PFN_COMPOUND unset for
   those PFNs.

2. In the free_pages error path, order was computed via
   folio_order(page_folio(page)) *after* put_page(page) had already
   dropped the reference, resulting in a use-after-free/put when that
   was the last reference on the page. Compute order before releasing
   the page.

Introducing the fallback in 1. also requires the source page array
handed to ->copy_to_ram() to be built differently. Both callers only
populated the entry at the head of each source folio, relying on the
copy callback to derive the rest of the folio from the order recorded
in the matching drm_pagemap_addr. Once the destination has been demoted
to order-0 folios the drm_pagemap_addr entries are per-page, so a source
page is needed for every one of them; leaving them NULL makes the copy
callback stop after the first page and the remainder of the range is
never copied.

The source folio is only split later, by migrate_vma_pages() /
migrate_device_pages(), so its order cannot be used to detect the
demotion - test the destination for MIGRATE_PFN_COMPOUND instead. Factor
the array population out into drm_pagemap_migrate_populate_src_pages()
and use it from both drm_pagemap_evict_to_ram() and
__drm_pagemap_migrate_to_ram().

Fixes: ddeda6136038 ("drm/pagemap: Allocate folios when possible")
Cc: [email protected]
Assisted-by: GitHub_Copilot:claude-opus-5
Signed-off-by: Matthew Brost <[email protected]>
Reviewed-by: Himal Prasad Ghimiray <[email protected]>
Link: https://patch.msgid.link/[email protected]
[ adapted to 6.18.y: only the use-after-put hunk; no large device folios ]
Signed-off-by: Dickson Wu <[email protected]>
---
6.18.y has no large device-private folios (no MIGRATE_PFN_COMPOUND
collection), so the order-0 fallback and the source page array rework
have nothing to demote here. Only the hunk that reads the folio order
before put_page() applies.

Tested on v6.18.66:
- x86_64 allmodconfig W=1 build of drivers/gpu/drm/drm_pagemap.o, base
  vs patched, no new warnings
- booted base and patched kernels under QEMU/KVM with Xe, GPU SVM and
  DEVICE_PRIVATE built in; the xe_migrate KUnit suite ran on both with
  the same two skips (no Xe GPU in the guest). The allocation failure
  path itself was not exercised at runtime.

The backport and the test runs were prepared with AI coding tools
(OpenAI Codex agents, with review by Claude). I went over the result
before signing off.
---
 drivers/gpu/drm/drm_pagemap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/drm_pagemap.c b/drivers/gpu/drm/drm_pagemap.c
index 4cd5182f8205..59459adf84cc 100644
--- a/drivers/gpu/drm/drm_pagemap.c
+++ b/drivers/gpu/drm/drm_pagemap.c
@@ -532,11 +532,11 @@ static int drm_pagemap_migrate_populate_ram_pfn(struct 
vm_area_struct *vas,
                if (!page)
                        goto next_put;
 
+               order = folio_order(page_folio(page));
+
                put_page(page);
                mpfn[i] = 0;
 
-               order = folio_order(page_folio(page));
-
 next_put:
                i += NR_PAGES(order);
        }

---
base-commit: 2c7bb1d0212c23234a9f85123eb7a36eecc76776
change-id: 20261205-drm-pagemap-618-b0670a7806d4

Best regards,
--  
Dickson Wu <[email protected]>


Reply via email to