Re: [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE

From: Lance Yang

Date: Sat Dec 05 2026 - 00:45:41 EST



On Thu, Dec 03, 2026 at 11:17:57PM -0500, Zi Yan wrote:
[...]
>--- a/mm/huge_memory.c
>+++ b/mm/huge_memory.c
>@@ -2993,7 +2993,17 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
> goto unlock_ptls;
> }
>
>- src_pmdval = pmdp_huge_clear_flush(src_vma, src_addr, src_pmd);
>+ /*
>+ * Invalidate the PMD so that a concurrent zap does not skip it
>+ * in case a restore is needed afterwards.
>+ */
>+ src_pmdval = pmdp_invalidate(src_vma, src_addr, src_pmd);
>+ /*
>+ * Syncing against concurrent GUP-fast:
>+ * - invalidate PMD; barrier; read pincount
>+ * - inc pincount; barrier; read PMD
>+ */
>+ smp_mb();

Wait ... we didn't even have the smp_mb() against GUP-fast here before ...
good catch fixing that too!

Just thinking aloud, since we now have the same pmdp_invalidate() +
smp_mb() dance here and elsewhere (and it's pretty easy to forget the
barrier), wonder if it's worth wrapping that in a small helper like
pmdp_invalidate_sync_gup() (not a blocker though) :D

> /* Folio got pinned from under us. Put it back and fail the move. */
> if (folio_maybe_dma_pinned(src_folio)) {
> set_pmd_at(mm, src_addr, src_pmd, src_pmdval);
>@@ -3004,6 +3014,7 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm
> folio_move_anon_rmap(src_folio, dst_vma);
> src_folio->index = linear_anon_page_index(dst_vma, dst_addr);
>
>+ pmdp_huge_get_and_clear(mm, src_addr, src_pmd);

LGTM!

Reviewed-by: Lance Yang <lance.yang@xxxxxxxxx>