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

From: Lorenzo Stoakes (ARM)

Date: Fri Dec 04 2026 - 07:52:42 EST


On Thu, Dec 03, 2026 at 11:17:57PM -0500, Zi Yan wrote:
> UFFDIO_MOVE under the page table lock clears a PMD, checks if the folio is
> DMA pinned, and restores it if that is the case. A concurrent zap can skip
> a none PMD without taking the page table lock. Since UFFDIO_MOVE holds the
> VMA or mmap read lock, those taking the read lock can race with it, e.g.,
> MADV_DONTNEED. In the MADV_DONTNEED case, the affected memory can be
> neither discarded nor zeroed.
>
> Fix it by invalidating the PMD with pmdp_invalidate() instead. Also add
> smp_mb() between pmdp_invalidate() and reading folio pincount to pair with
> GUP-fast.
>
> Fixes: adef440691bab ("userfaultfd: UFFDIO_MOVE uABI")

No Cc: stable, on purpose?

> Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>

With nits addressed, LGTM, so:

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>

> To: David Hildenbrand <david@xxxxxxxxxx>
> To: Zi Yan <ziy@xxxxxxxxxx>
> To: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
> To: Andrea Arcangeli <aarcange@xxxxxxxxxx>
> To: Suren Baghdasaryan <surenb@xxxxxxxxxx>
> 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: Peter Xu <peterx@xxxxxxxxxx>
> Cc: linux-mm@xxxxxxxxx
> Cc: linux-kernel@xxxxxxxxxxxxxxx
> ---
> mm/huge_memory.c | 13 ++++++++++++-
> 1 file changed, 12 insertions(+), 1 deletion(-)
>
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 72322bb86ff4f..6a9635c7e0b48 100644
> --- 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.
> + */

OK had to read it a few times to get it - it being cleared would result in zap
skipping it (+ thus not blocking on PMD PTL) + potentially a restore would then
leave an unzapped entry.

Maybe slightly rephrase like:

/*
* A concurrent zap skips none entries without taking the PTL, so
* invalidate rather than clear. The zap then blocks on the PTL until
* the move has either completed or been aborted and the entry restored.
*/

> + 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

Worth saying specifically 'pairs with' and the relevant function by convention.

> + */
> + smp_mb();

Real nitty but maybe newline here to highlight that you have a fence here :)

> /* 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);
> _dst_pmd = folio_mk_pmd(src_folio, dst_vma->vm_page_prot);
> /* Follow mremap() behavior and treat the entry dirty after the move */
> _dst_pmd = pmd_mkwrite(pmd_mkdirty(_dst_pmd), dst_vma);
>
> --
> 2.53.0
>

--
Cheers, Lorenzo