The patch "mm/rmap: don't discard lazyfree THPs at PMD level" disabled the PMD-level discard, because it cleared the PMD before checking whether the folio could be discarded, and munmap() or mremap() could skip that transiently none PMD. __discard_anon_folio_pmd_locked() now keeps the PMD invalidated, not cleared, until the discard is committed, GUP-fast no longer follows an invalidated PMD on any architecture, powerpc page table check no longer counts an invalidated PMD as mapped, and sparc64 keeps thp_pte_count balanced when an invalidated PMD is set again or cleared. Discard lazyfree THPs at PMD level again. Also call update_hiwater_rss() before unmap_huge_pmd_locked(), as the PTE path does before lowering RSS. The PMD path has skipped it since the PMD-level discard was added in commit 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()"), so a discard could lower RSS without recording the high watermark first, and VmHWM could be under-reported. Reported-by: Sashiko Closes: https://sashiko.dev/#/patchset/20261015-anon_vma_uaf_fix-v1-0-7b977303b42b%40nvidia.com?part=7 Signed-off-by: Zi Yan To: Andrew Morton To: David Hildenbrand To: Lorenzo Stoakes Cc: Rik van Riel Cc: "Liam R. Howlett" Cc: Vlastimil Babka Cc: Harry Yoo Cc: Jann Horn Cc: Lance Yang Cc: Kyle Zeng Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- mm/rmap.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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; + /* + * unmap_huge_pmd_locked has either already marked + * the folio as swap-backed or decided to retain it + * due to GUP or speculative references. + */ + goto walk_abort; + } + if (flags & TTU_SPLIT_HUGE_PMD) { /* * We temporarily have to drop the PTL and -- 2.53.0