[PATCH 08/12] mm/migrate_device: do not migrate a uffd-protected zero page

From: Tal Zussman

Date: Wed Nov 11 2026 - 13:01:57 EST


migrate_vma_setup() installs a migration entry for each page it
collects, which carries the uffd bit over to the new PTE. However, a PTE
that maps the zero page is left in place without a migration entry.
migrate_vma_insert_page() later overwrites it with a new PTE that is
built only from the VMA flags, so the uffd-wp bit or the RWP PAGE_NONE
protection of the old PTE is lost. The same applies to the huge zero
PMD.

Once a driver has migrated such a zero page, writes and accesses to it
no longer fault into userfaultfd, so the manager misses them. With async
tracking, PAGEMAP_SCAN unconditionally reports the page as written or
accessed.

This requires a driver that migrates memory to a device.

To fix it, do not mark a zero page for migration if its entry has the
uffd bit set in a VMA registered for WP or RWP. The driver sees an
address that was not migrated, which drivers already have to deal with.

The uffd bit can also be set after the zero page was collected, as
write-protecting only takes the mmap lock for read. So check again under
the page table lock in migrate_vma_insert_page() and
migrate_vma_insert_huge_pmd_page().

The alternative would be to carry the uffd state over in
migrate_vma_insert_page() and migrate_vma_insert_huge_pmd_page(). Not
migrating the zero page is simpler, and a zero page that is protected by
userfaultfd in a range that a driver migrates should be rare.

Fixes: 292924b26024 ("userfaultfd: wp: apply _PAGE_UFFD_WP bit")
Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
Signed-off-by: Tal Zussman <tz2294@xxxxxxxxxxxx>
---
mm/migrate_device.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/mm/migrate_device.c b/mm/migrate_device.c
index 7919e04abc1c..ace216ea834c 100644
--- a/mm/migrate_device.c
+++ b/mm/migrate_device.c
@@ -159,7 +159,16 @@ static int migrate_vma_collect_huge_pmd(pmd_t *pmdp, unsigned long start,

folio = pmd_folio(*pmdp);
if (is_huge_zero_folio(folio)) {
+ /*
+ * Nothing carries the uffd state of the zero page
+ * over, so skip it if its uffd bit is set.
+ */
+ bool protected = userfaultfd_protected(walk->vma) &&
+ pmd_uffd(*pmdp);
+
spin_unlock(ptl);
+ if (protected)
+ return migrate_vma_collect_skip(start, end, walk);
return migrate_vma_collect_hole(start, end, -1, walk);
}
if (pmd_write(*pmdp))
@@ -337,6 +346,9 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
pfn = pte_pfn(pte);
if (is_zero_pfn(pfn) &&
(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) {
+ /* See migrate_vma_collect_huge_pmd(). */
+ if (userfaultfd_protected(vma) && pte_uffd(pte))
+ goto next;
mpfn = MIGRATE_PFN_MIGRATE;
migrate->cpages++;
goto next;
@@ -878,10 +890,14 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
if (userfaultfd_missing(vma))
goto unlock_abort;

- if (is_huge_zero_pmd(*pmdp))
+ if (is_huge_zero_pmd(*pmdp)) {
+ /* The uffd bit can have been set since the collection. */
+ if (userfaultfd_protected(vma) && pmd_uffd(*pmdp))
+ goto unlock_abort;
flush = true;
- else if (!pmd_none(*pmdp))
+ } else if (!pmd_none(*pmdp)) {
goto unlock_abort;
+ }

add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
@@ -1081,6 +1097,9 @@ static void migrate_vma_insert_page(struct migrate_vma *migrate,

if (!is_zero_pfn(pfn))
goto unlock_abort;
+ /* The uffd bit can have been set since the collection. */
+ if (userfaultfd_protected(vma) && pte_uffd(orig_pte))
+ goto unlock_abort;
flush = true;
} else if (!pte_none(orig_pte))
goto unlock_abort;

--
2.39.5