Linux-mm Archive on 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, "John Hubbard" <jhubbard@nvidia.com>,
	"Peter Xu" <peterx@redhat.com>, "Jason Gunthorpe" <jgg@ziepe.ca>
Subject: Re: [PATCH v2 7/9] mm/huge_memory: avoid transient none PMDs during lazyfree reclaim
Date: Sat, 05 Dec 2026 04:19:31 +0000	[thread overview]
Message-ID: <sashiko-outbox-251075@kernel.org> (raw)
In-Reply-To: <20261203-anon_vma_uaf_fix-v2-7-3d4c746e8839@nvidia.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] mm/huge_memory: pmdp_invalidate() breaks GUP-fast sync on
  32-bit MIPS
- [Medium] mm: doubled flush_hash_hugepage() on PowerPC64 Hash MMU in
  lazyfree discard
- [Medium] mm: doubled SMP TLB cross-calls on sparc64 in lazyfree THP
  discard
- [Low] mm/huge_memory: commit message omits that PMD discard is
  currently unused
--

commit eac5b1c8b38dc617c570fb7825525d02213dcb00
Author: Kyle Zeng <kylebot@openai.com>

mm/huge_memory: avoid transient none PMDs during lazyfree reclaim

This commit changes __discard_anon_folio_pmd_locked() to invalidate the
huge PMD with pmdp_invalidate() instead of clearing it with
pmdp_huge_clear_flush(), deferring pmdp_huge_get_and_clear() until after
the dirty and reference count checks succeed.

> Once the remaining VMA
> links are removed, the folio's positive mapcount no longer guarantees a
> live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an
> unprivileged user reproduces a KASAN use-after-free in
> folio_lock_anon_vma_read().

[Severity: Low]
Is the present-tense description of the use-after-free accurate at this
commit?

Commit 984c0700854e ("mm/rmap: don't discard lazyfree THPs at PMD level")
already removed the sole call to unmap_huge_pmd_locked(), leaving
__discard_anon_folio_pmd_locked() unused until a follow-up patch
re-enables PMD-level lazyfree discard.  Mentioning that context would
help reviewers and explain why Fixes and Cc: stable trailers are omitted.

> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index 716bcf6873656..72322bb86ff4f 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -3660,11 +3660,11 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
>  		return false;
>  	}
>  
> -	orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
> +	orig_pmd = pmdp_invalidate(vma, addr, pmdp);
>  
>  	/*
>  	 * Syncing against concurrent GUP-fast:
> -	 * - clear PMD; barrier; read refcount
> +	 * - invalidate PMD; barrier; read refcount
>  	 * - inc refcount; barrier; read PMD
>  	 */
>  	smp_mb();

[Severity: High]
Can switching to pmdp_invalidate() break GUP-fast synchronization on
32-bit MIPS?

On MIPS, pmd_mkinvalid() clears _PAGE_PRESENT, _PAGE_VALID, and
_PAGE_DIRTY, leaving _PAGE_HUGE, _PAGE_WRITE, and PFN bits set.  Unlike
64-bit MIPS, 32-bit MIPS pmd_present() in
arch/mips/include/asm/pgtable-32.h only checks
pmd_val(pmd) != (unsigned long) invalid_pte_table and ignores
_PAGE_PRESENT.  Because MIPS uses the generic pmd_access_permitted(),
gup_fast_pmd_leaf() still treats the invalidated huge PMD as present and
writable on 32-bit SMP kernels with THP and GUP-fast enabled:

CPU0                                 CPU1
__discard_anon_folio_pmd_locked()
  pmdp_invalidate(vma, addr, pmdp)
  smp_mb()
  folio_ref_count() == map_count + 1
                                     gup_fast_pmd_leaf()
                                       pmd_access_permitted() -> true
                                       try_grab_folio_fast()  <-- pins folio
                                       pmd_val() recheck passes
  pmdp_huge_get_and_clear()
  folio_remove_rmap_pmd()

An O_DIRECT read racing against MADV_FREE reclaim can then pin and write
to the detached folio while subsequent userspace accesses fault in a
fresh zero page, causing silent data loss once PMD-level lazyfree
discard is re-enabled.

A prerequisite MIPS fix is needed before this change, either checking
_PAGE_PRESENT for _PAGE_HUGE entries in 32-bit pmd_present() or defining
a MIPS pmd_access_permitted() that requires _PAGE_PRESENT.

> @@ -3699,6 +3699,7 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
>  		return false;
>  	}
>  
> +	pmdp_huge_get_and_clear(mm, addr, pmdp);
>  	folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma);
>  	zap_deposited_table(mm, pmdp);
>  	add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);

[Severity: Medium]
Does calling pmdp_huge_get_and_clear() after pmdp_invalidate() double
flush_hash_hugepage() calls on PowerPC64 Hash MMU?

On Book3S 64-bit Hash MMU, pmdp_invalidate() clears _PAGE_PRESENT and
sets _PAGE_INVALID while leaving H_PAGE_HASHPTE set in *pmdp and leaving
hpte_slot_array intact after running flush_hash_hugepage().  When
pmdp_huge_get_and_clear() runs next, pmd_present(*pmdp) is still true
due to _PAGE_INVALID, so hash__pmdp_huge_get_and_clear() calls
hash__pmd_hugepage_update() before zeroing hpte_slot_array and runs a
second full flush_hash_hugepage().

This doubles the 16MB HPTE and TLB invalidations on every discarded
lazyfree THP, adding redundant broadcast tlbie instructions with IRQs
disabled or repeated H_BULK_REMOVE/H_BLOCK_REMOVE hypercalls on pSeries
LPARs.

[Severity: Medium]
Does this also double synchronous SMP TLB cross-calls on sparc64?

In __set_pmd_acct(), sparc64 checks !pmd_none(orig) and
pmd_trans_huge(orig) without testing _PAGE_VALID.  Because
try_to_unmap_one() does not activate lazy_mmu_mode, pmdp_invalidate()
triggers two immediate flush_tsb_user_page() and smp_flush_tlb_page()
cross-calls (one per REAL_HPAGE_SIZE half).  Clearing the invalidated
PMD in pmdp_huge_get_and_clear() (or restoring orig_pmd on abort) then
runs __set_pmd_acct() over the non-none invalidated entry and triggers
two more flush_tsb_user_page() and smp_flush_tlb_page() cross-calls.

Every lazyfree THP discard or abort on SMP sparc64 therefore pays 4
synchronous TLB shootdown IPIs and mm->context.lock acquisitions instead
of 2.

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


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

Thread overview: 23+ 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-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 ` [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 [this message]
2026-12-05  4:35   ` 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-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

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-251075@kernel.org \
    --to=sashiko-bot@kernel.org \
    --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=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.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=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