Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Zi Yan <ziy@nvidia.com>
Cc: David Hildenbrand <david@kernel.org>,
	 Baolin Wang <baolin.wang@linux.alibaba.com>,
	"Liam R. Howlett" <liam@infradead.org>,
	 Nico Pache <nico.pache@linux.dev>,
	Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
	 Barry Song <baohua@kernel.org>,
	Lance Yang <lance.yang@linux.dev>,
	 Usama Arif <usama.arif@linux.dev>,
	Kiryl Shutsemau <kas@kernel.org>,
	 Rik van Riel <riel@surriel.com>,
	Vlastimil Babka <vbabka@kernel.org>,
	 Harry Yoo <harry@kernel.org>, Jann Horn <jannh@google.com>,
	Kyle Zeng <kylebot@openai.com>,
	 Andrew Morton <akpm@linux-foundation.org>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	 Andrea Arcangeli <aarcange@redhat.com>,
	Suren Baghdasaryan <surenb@google.com>,
	 Peter Xu <peterx@redhat.com>
Subject: Re: [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE
Date: Fri, 4 Dec 2026 12:52:32 +0000	[thread overview]
Message-ID: <axKxBT81SLDf5ngA@gremlin> (raw)
In-Reply-To: <20261203-anon_vma_uaf_fix-v2-9-3d4c746e8839@nvidia.com>

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@nvidia.com>

With nits addressed, LGTM, so:

Reviewed-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>

> To: David Hildenbrand <david@kernel.org>
> To: Zi Yan <ziy@nvidia.com>
> To: Andrew Morton <akpm@linux-foundation.org>
> To: Andrea Arcangeli <aarcange@redhat.com>
> To: Suren Baghdasaryan <surenb@google.com>
> Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
> Cc: "Liam R. Howlett" <liam@infradead.org>
> Cc: Nico Pache <nico.pache@linux.dev>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: Dev Jain <dev.jain@arm.com>
> Cc: Barry Song <baohua@kernel.org>
> Cc: Lance Yang <lance.yang@linux.dev>
> Cc: Usama Arif <usama.arif@linux.dev>
> Cc: Kiryl Shutsemau <kas@kernel.org>
> Cc: Peter Xu <peterx@redhat.com>
> Cc: linux-mm@kvack.org
> Cc: linux-kernel@vger.kernel.org
> ---
>  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


      reply	other threads:[~2026-12-04 12:52 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  4:17 [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes Zi Yan
2026-12-04  4:17 ` [PATCH v2 1/9] mm/migrate: do not clear device private entry in set_pmd_migration_entry() Zi Yan
2026-12-04  4:17 ` [PATCH v2 2/9] powerpc/book3s64/mm: don't count invalidated PMDs in page table check Zi Yan
2026-12-04  4:17 ` [PATCH v2 3/9] riscv: mm: don't let GUP-fast follow entries without _PAGE_READ Zi Yan
2026-12-04  4:17 ` [PATCH v2 4/9] LoongArch: mm: don't let GUP-fast follow invalidated THP PMDs Zi Yan
2026-12-04  4:17 ` [PATCH v2 5/9] s390/mm: always clear _SEGMENT_ENTRY_READ in pmdp_invalidate() Zi Yan
2026-12-04 12:31   ` Alexander Gordeev
2026-12-04  4:17 ` [PATCH v2 6/9] sparc64: mm: fix thp_pte_count accounting around pmdp_invalidate() Zi Yan
2026-12-04  4:17 ` [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim Zi Yan
2026-12-04  4:35   ` Liam R. Howlett
2026-12-04  9:38   ` Baolin Wang
2026-12-04 13:23   ` Lorenzo Stoakes (ARM)
2026-12-04  4:17 ` [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level Zi Yan
2026-12-04  4:37   ` Liam R. Howlett
2026-12-04  9:49   ` Baolin Wang
2026-12-04  4:17 ` [PATCH v2 9/9] userfaultfd: avoid transient none PMDs during UFFDIO_MOVE Zi Yan
2026-12-04 12:52   ` Lorenzo Stoakes (ARM) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axKxBT81SLDf5ngA@gremlin \
    --to=ljs@kernel.org \
    --cc=aarcange@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=dev.jain@arm.com \
    --cc=harry@kernel.org \
    --cc=jannh@google.com \
    --cc=kas@kernel.org \
    --cc=kylebot@openai.com \
    --cc=lance.yang@linux.dev \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nico.pache@linux.dev \
    --cc=peterx@redhat.com \
    --cc=riel@surriel.com \
    --cc=ryan.roberts@arm.com \
    --cc=surenb@google.com \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox