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
Subject: Re: [PATCH v2 8/9] mm/rmap: re-enable discarding lazyfree THPs at PMD level
Date: Sat, 05 Dec 2026 04:19:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-251076@kernel.org> (raw)
In-Reply-To: <20261203-anon_vma_uaf_fix-v2-8-3d4c746e8839@nvidia.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] mm/rmap: 32-bit MIPS GUP-fast still follows invalidated THP
  PMDs on discard
- [High] mm/rmap: lazyfree THP PMD discard skips flush_cache_range()
--

commit 479154fc094b2551853b0d7d30d7d879d05e867b
Author: Zi Yan <ziy@nvidia.com>

mm/rmap: re-enable discarding lazyfree THPs at PMD level

This commit re-enables PMD-level discarding of lazyfree THPs in
try_to_unmap_one() via unmap_huge_pmd_locked(), and calls
update_hiwater_rss() before lowering RSS.

> diff --git a/mm/rmap.c b/mm/rmap.c
> index f0c17d2e04847..32acae9efc313 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -2285,6 +2285,19 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>  		}
>  
>  		if (!pvmw.pte) {
> +			if (folio_test_lazyfree(folio)) {
> +				/* Update high watermark before we lower rss */
> +				update_hiwater_rss(mm);
> +				if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
> +					goto walk_done;

[Severity: High]
Can 32-bit MIPS GUP-fast still follow the invalidated PMD during
unmap_huge_pmd_locked()?

__discard_anon_folio_pmd_locked() keeps the invalidated PMD in place
across the folio_ref_count() check until pmdp_huge_get_and_clear(),
relying on GUP-fast to back off.  On 32-bit MIPS with THP enabled,
pmd_mkinvalid() clears _PAGE_PRESENT, but pmd_present() ignores it:

arch/mips/include/asm/pgtable-32.h:pmd_present() {
	return pmd_val(pmd) != (unsigned long) invalid_pte_table;
}

Because MIPS uses the generic pmd_access_permitted() and has no
pmd_protnone(), gup_fast_pmd_leaf() still accepts the invalidated PMD:

CPU0 (reclaim)                     CPU1 (GUP-fast on 32-bit MIPS)
__discard_anon_folio_pmd_locked()
  pmdp_invalidate()
  smp_mb()
  folio_ref_count() == mapcount+1
                                   gup_fast_pmd_leaf()
                                     pmd_access_permitted() -> true
                                     try_grab_folio_fast()  <-- grabs folio
                                     pmd_val(orig) == *pmdp -> true
  pmdp_huge_get_and_clear()
  folio_remove_rmap_pmd()

Reclaim then discards and unmaps the THP while GUP-fast holds a live
reference or DMA write pin on the detached folio.  Later faults allocate
a fresh zero page while the GUP-fast caller reads or writes the old folio.

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.

[Severity: High]
Does this path skip flush_cache_range() before unmapping the huge PMD?

Unlike the PTE path in try_to_unmap_one() and other PMD unmap paths such
as set_pmd_migration_entry(), __discard_anon_folio_pmd_locked() calls
pmdp_invalidate() and pmdp_huge_get_and_clear() without calling
flush_cache_range(vma, addr, addr + HPAGE_PMD_SIZE) first.

A MAP_DROPPABLE THP can be discarded while dirty without madvise(MADV_FREE)
ever flushing it.  On MIPS R4K with cpu_has_dc_aliases, flush_tlb_range()
assumes flush_cache_range() already flushed aliasing VIPT dcache lines, so
delayed writeback after folio reuse can corrupt reallocated pages.

Executable mappings on MIPS and 32-bit ARM LPAE and user register windows
on SPARC64 also rely on flush_cache_range().  Calling flush_cache_range()
before pmdp_invalidate() in __discard_anon_folio_pmd_locked() would match
set_pmd_migration_entry().

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


  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
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 [this message]
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-251076@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=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=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