[PATCH v2 1/9] mm/migrate: do not clear device private entry in set_pmd_migration_entry()

From: Zi Yan

Date: Thu Dec 03 2026 - 23:18:33 EST


A device private entry is never followed by GUP-fast, so there is no need
to clear the entry to avoid it. Furthermore, clearing the entry creates a
none PMD and a concurrent zap skips it. set_pmd_migration_entry() might not
hold a VMA lock nor a mmap lock. As a result:

1. When racing with munmap(), a "bad pmd" can appear, deposited page table
can be lost, and RSS stats can be off.
2. When racing with mremap(), the PMD is not moved, so the new address
faults and sees zeros instead of the original data.

Fix it by not clearing device private entry.

Fixes: 65edfda6f3f2e ("mm/rmap: extend rmap and migration support device-private entries")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>
To: David Hildenbrand <david@xxxxxxxxxx>
To: Zi Yan <ziy@xxxxxxxxxx>
To: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Baolin Wang <baolin.wang@xxxxxxxxxxxxxxxxx>
Cc: "Liam R. Howlett" <liam@xxxxxxxxxxxxx>
Cc: Nico Pache <nico.pache@xxxxxxxxx>
Cc: Ryan Roberts <ryan.roberts@xxxxxxx>
Cc: Dev Jain <dev.jain@xxxxxxx>
Cc: Barry Song <baohua@xxxxxxxxxx>
Cc: Lance Yang <lance.yang@xxxxxxxxx>
Cc: Usama Arif <usama.arif@xxxxxxxxx>
Cc: Kiryl Shutsemau <kas@xxxxxxxxxx>
Cc: Balbir Singh <balbirs@xxxxxxxxxx>
Cc: Alistair Popple <apopple@xxxxxxxxxx>
Cc: linux-mm@xxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
---
mm/huge_memory.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 6305b4e2a6b68..91f3c7e7a044a 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -5223,7 +5223,12 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
} else {
softleaf_t old_entry;

- pmdval = pmdp_huge_get_and_clear(vma->vm_mm, address, pvmw->pmd);
+ /*
+ * The device private entry stays as is since GUP-fast
+ * never follows it and folio_try_share_anon_rmap_pmd()
+ * below never fails for it.
+ */
+ pmdval = pmdp_get(pvmw->pmd);
old_entry = softleaf_from_pmd(pmdval);

writable = softleaf_is_device_private_write(old_entry);

--
2.53.0