All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Zi Yan" <ziy@nvidia.com>, "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>,
	"Lorenzo Stoakes" <ljs@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>
Cc: linux-mm@kvack.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: Sat, 05 Dec 2026 04:19:33 +0000	[thread overview]
Message-ID: <sashiko-outbox-251077@kernel.org> (raw)
In-Reply-To: <20261203-anon_vma_uaf_fix-v2-9-3d4c746e8839@nvidia.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mm: move_pages_huge_pmd() lets 32-bit MIPS GUP-fast pin
  invalidated PMDs
- [Medium] mm: double HPTE/TLB flush in move_pages_huge_pmd() on powerpc
  hash MMU
- [Medium] mm: redundant sparc64 TLB/TSB IPI shootdowns in
  move_pages_huge_pmd()
--

commit 620660dab391564b04de30b5d319a5382bec8aeb
Author: Zi Yan <ziy@nvidia.com>

userfaultfd: avoid transient none PMDs during UFFDIO_MOVE

This commit changes move_pages_huge_pmd() to invalidate the source PMD
with pmdp_invalidate() and an smp_mb() before checking
folio_maybe_dma_pinned(), deferring pmdp_huge_get_and_clear() until after
the pin check so concurrent zaps do not skip a transiently cleared PMD.

> 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.
> +		 */
> +		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();
>  		/* 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);

[Severity: High]
Can 32-bit MIPS GUP-fast still pin src_folio through the invalidated PMD
before pmdp_huge_get_and_clear() runs?

On MIPS, pmd_mkinvalid() clears _PAGE_PRESENT, _PAGE_VALID, and
_PAGE_DIRTY while leaving _PAGE_HUGE, _PAGE_WRITE, and the PFN intact.
Unlike 64-bit MIPS, 32-bit MIPS pmd_present() in
arch/mips/include/asm/pgtable-32.h only checks against invalid_pte_table.

As a result, pmd_present() and the generic pmd_access_permitted() both
return true for an invalidated huge PMD, allowing a concurrent
pin_user_pages_fast() (such as O_DIRECT) to race with
move_pages_huge_pmd():

CPU0                                   CPU1
move_pages_huge_pmd()
  pmdp_invalidate(..., src_pmd)
  smp_mb()
  folio_maybe_dma_pinned(src_folio)
    -> false
                                       gup_fast_pmd_leaf()
                                         pmd_present(orig) -> true
                                         pmd_access_permitted() -> true
                                         try_grab_folio_fast() <-- pins folio
                                         pmd_val(orig) == *src_pmd -> ok
  folio_move_anon_rmap()
  pmdp_huge_get_and_clear(..., src_pmd)

UFFDIO_MOVE then unmaps src_folio from src_addr and remaps it at dst_addr
with a new anon_vma and index while DMA holds a pin on it, risking silent
memory corruption.  Checking _PAGE_PRESENT for _PAGE_HUGE entries in
32-bit MIPS pmd_present() (matching arch/mips/include/asm/pgtable-64.h)
would close this race.

> @@ -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);

[Severity: Medium]
Does calling pmdp_huge_get_and_clear() after pmdp_invalidate() perform a
redundant hash page table flush on PowerPC Book3S64 Hash MMU?

On PowerPC Book3S64 Hash MMU, pmdp_invalidate() runs
hash__pmd_hugepage_update() to clear _PAGE_PRESENT and set _PAGE_INVALID,
which calls hpte_do_hugepage_flush() while leaving H_PAGE_HASHPTE set in
*src_pmd and the valid bits set in hpte_slot_array.

When the success path reaches pmdp_huge_get_and_clear(), pmd_present() is
still true because it checks _PAGE_INVALID, so hash__pmd_hugepage_update()
sees H_PAGE_HASHPTE still set and runs hpte_do_hugepage_flush() a second
time before zeroing hpte_slot_array.

Every successful huge-PMD UFFDIO_MOVE therefore doubles the IRQ-off
tlbie() broadcasts on bare metal or batched hypercalls on pSeries LPARs.
Skipping hpte_do_hugepage_flush() in hash__pmd_hugepage_update() when
_PAGE_INVALID is already set would avoid the extra flush.

[Severity: Medium]
Will this also double TSB and SMP TLB shootdowns on sparc64?

On sparc64, pmdp_invalidate() clears _PAGE_VALID while leaving
_PAGE_PMD_HUGE set and calls __set_pmd_acct().  Because lazy_mmu_mode is
not active in move_pages_huge_pmd(), tlb_batch_add_one() immediately
issues two synchronous flush_tsb_user_page() and smp_flush_tlb_page() IPIs.

When pmdp_huge_get_and_clear() (or set_pmd_at() on the DMA-pinned restore
path) then overwrites the invalidated PMD, __set_pmd_acct() in
arch/sparc/mm/tlb.c checks !pmd_none(orig) and pmd_trans_huge(orig)
without testing _PAGE_VALID, issuing two more redundant TSB and TLB IPIs.

Checking pmd_val(orig) & _PAGE_VALID before calling tlb_batch_add_one() in
__set_pmd_acct() would avoid doubling the shootdowns per moved THP.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-anon_vma_uaf_fix-v2-0-3d4c746e8839@nvidia.com?part=9


  parent reply	other threads:[~2026-12-05  4:19 UTC|newest]

Thread overview: 29+ 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 ` 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-05  4:06   ` Lance Yang
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   ` 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-05  4:19   ` sashiko-bot
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-05  4:19   ` sashiko-bot
2026-12-05  4:35   ` Lance Yang
2026-12-05  5:20     ` Lance Yang
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-05  4:19   ` sashiko-bot
2026-12-05  5:30   ` Lance Yang
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)
2026-12-05  4:19   ` sashiko-bot [this message]
2026-12-05  5:45   ` Lance Yang
2026-12-05  4:19 ` [PATCH v2 0/9] Re-enable discarding lazyfree PMD THPs and transient none PMD fixes sashiko-bot

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=sashiko-outbox-251077@kernel.org \
    --to=sashiko-bot@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-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=nico.pache@linux.dev \
    --cc=peterx@redhat.com \
    --cc=riel@surriel.com \
    --cc=ryan.roberts@arm.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.