[PATCH 02/12] userfaultfd: refuse UFFDIO_MOVE into a non-present huge destination PMD
From: Tal Zussman
Date: Wed Nov 11 2026 - 13:00:49 EST
move_pages() refuses to move into a destination PMD that already maps a
THP, but checks for that with pmd_trans_huge(), which is only true for a
present THP. A device-private THP or a THP under migration is a
non-present huge PMD leaf and is not refused.
The destination PMD is then treated as if it pointed to a PTE table. It
is not empty, so pte_alloc() leaves it alone, and move_pages_ptes()
fails to map a PTE table from it and returns -EAGAIN. move_pages()
retries on -EAGAIN, with both VMA read locks held. If the source is a
THP, it is split first for nothing.
For a THP under migration, the loop ends once the migration finishes.
A device-private THP can remain in device memory indefinitely, so the
caller of UFFDIO_MOVE hangs until it receives a fatal signal, and
anything in the mm that needs to write-lock one of the VMAs blocks along
with it. This requires a driver that migrates THPs to device memory.
To fix it, check the destination with pmd_is_huge(), which is true for
any huge leaf, present or not. UFFDIO_MOVE now fails with -EEXIST in
these cases, as it already does for a present THP.
Fixes: adef440691ba ("userfaultfd: UFFDIO_MOVE uABI")
Fixes: a30b48bf1b24 ("mm/migrate_device: implement THP migration of zone device pages")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Tal Zussman <tz2294@xxxxxxxxxxxx>
---
mm/userfaultfd.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 61a712e1f9f5..ad9d243dc64b 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -2021,12 +2021,13 @@ static ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,
dst_pmdval = pmdp_get_lockless(dst_pmd);
/*
- * If the dst_pmd is mapped as THP don't override it and just
- * be strict. If dst_pmd changes into TPH after this check, the
- * move_pages_huge_pmd() will detect the change and retry
- * while move_pages_pte() will detect the change and fail.
+ * If the dst_pmd is a huge leaf, present or not, don't
+ * override it and just be strict. If dst_pmd changes into a
+ * leaf after this check, move_pages_huge_pmd() will detect the
+ * change and retry while move_pages_ptes() will fail to map
+ * the PTE table and the next iteration will see the leaf.
*/
- if (unlikely(pmd_trans_huge(dst_pmdval))) {
+ if (unlikely(pmd_is_huge(dst_pmdval))) {
err = -EEXIST;
break;
}
--
2.39.5