mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zi Yan <ziy@nvidia.com>
To: 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, linux-kernel@vger.kernel.org,
	 Zi Yan <ziy@nvidia.com>, "David S. Miller" <davem@davemloft.net>,
	 Andreas Larsson <andreas@gaisler.com>,
	sparclinux@vger.kernel.org
Subject: [PATCH v2 6/9] sparc64: mm: fix thp_pte_count accounting around pmdp_invalidate()
Date: Thu, 03 Dec 2026 23:17:54 -0500	[thread overview]
Message-ID: <20261203-anon_vma_uaf_fix-v2-6-3d4c746e8839@nvidia.com> (raw)
In-Reply-To: <20261203-anon_vma_uaf_fix-v2-0-3d4c746e8839@nvidia.com>

thp_pte_count is maintained in __set_pmd_acct() whenever _PAGE_PMD_HUGE
flips. pmdp_invalidate() only clears _PAGE_VALID, so it does not flip
that bit. But it decrements thp_pte_count on its own, because THP
split replaces the invalidated PMD with a page table via pmd_populate(),
which does not change thp_pte_count.

This only accounts for the PMD split case, where a PMD is invalidated and
then replaced by a page table, but other cases exist:

1. Several callers invalidate a huge PMD and then install a valid one again
   with set_pmd_at(): MADV_FREE, page_mkclean_one(), and change_huge_pmd().
   The valid PMD is not counted, as _PAGE_PMD_HUGE did not flip, so each
   such call loses one count.

2. When an invalidated PMD is cleared, _PAGE_PMD_HUGE flips and the count
   is decreased again.

Eventually, thp_pte_count can wrap around and cause wrong TSB sizing.

Fix it by moving the decrement from pmdp_invalidate() to pmd_set(), which
backs pmd_populate(): THP split replaces the invalidated huge PMD with a
page table through it, so pmd_set() decrements the count when the entry
it overwrites is a huge PMD. Other pmd_populate() callers overwrite a none
PMD.

THP split also calls pmd_populate() on a local pmd_t to fill in the PTEs
before installing the page table. Initialize it to none, so pmd_set() does
not read an uninitialized value.

Fixes: 1e953d846ac0 ("sparc64 mm: Fix more TSB sizing issues")
Signed-off-by: Zi Yan <ziy@nvidia.com>
To: "David S. Miller" <davem@davemloft.net>
To: Andreas Larsson <andreas@gaisler.com>
Cc: sparclinux@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 arch/sparc/include/asm/pgtable_64.h |  7 +++++++
 arch/sparc/mm/tlb.c                 | 27 +++++++++++++++------------
 mm/huge_memory.c                    |  4 ++--
 3 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/arch/sparc/include/asm/pgtable_64.h b/arch/sparc/include/asm/pgtable_64.h
index 44d1333065a6a..141c204975219 100644
--- a/arch/sparc/include/asm/pgtable_64.h
+++ b/arch/sparc/include/asm/pgtable_64.h
@@ -803,19 +803,26 @@ static inline int pmd_present(pmd_t pmd)
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 void set_pmd_at(struct mm_struct *mm, unsigned long addr,
 		pmd_t *pmdp, pmd_t pmd);
+void pmd_set_acct(struct mm_struct *mm, pmd_t old);
 #else
 static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
 			      pmd_t *pmdp, pmd_t pmd)
 {
 	*pmdp = pmd;
 }
+
+static inline void pmd_set_acct(struct mm_struct *mm, pmd_t old)
+{
+}
 #endif
 
 static inline void pmd_set(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
 {
 	unsigned long val = __pa((unsigned long) (ptep));
+	pmd_t old = *pmdp;
 
 	pmd_val(*pmdp) = val;
+	pmd_set_acct(mm, old);
 }
 
 #define pud_set(pudp, pmdp)	\
diff --git a/arch/sparc/mm/tlb.c b/arch/sparc/mm/tlb.c
index 6d9dd5eb13287..909db4f7b6389 100644
--- a/arch/sparc/mm/tlb.c
+++ b/arch/sparc/mm/tlb.c
@@ -228,6 +228,21 @@ static void __set_pmd_acct(struct mm_struct *mm, unsigned long addr,
 	}
 }
 
+/*
+ * Called by pmd_set() and thus pmd_populate(). THP split uses pmd_populate()
+ * on an invalidated huge PMD and needs the accounting.
+ */
+void pmd_set_acct(struct mm_struct *mm, pmd_t old)
+{
+	if (mm == &init_mm || !(pmd_val(old) & _PAGE_PMD_HUGE))
+		return;
+
+	if (is_huge_zero_pmd(old))
+		mm->context.hugetlb_pte_count--;
+	else
+		mm->context.thp_pte_count--;
+}
+
 void set_pmd_at(struct mm_struct *mm, unsigned long addr,
 		pmd_t *pmdp, pmd_t pmd)
 {
@@ -250,9 +265,6 @@ static inline pmd_t pmdp_establish(struct vm_area_struct *vma,
 	return old;
 }
 
-/*
- * This routine is only called when splitting a THP
- */
 pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
 		     pmd_t *pmdp)
 {
@@ -263,15 +275,6 @@ pmd_t pmdp_invalidate(struct vm_area_struct *vma, unsigned long address,
 	old = pmdp_establish(vma, address, pmdp, entry);
 	flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
 
-	/*
-	 * set_pmd_at() will not be called in a way to decrement
-	 * thp_pte_count when splitting a THP, so do it now.
-	 * Sanity check pmd before doing the actual decrement.
-	 */
-	if ((pmd_val(entry) & _PAGE_PMD_HUGE) &&
-	    !is_huge_zero_pmd(entry))
-		(vma->vm_mm)->context.thp_pte_count--;
-
 	return old;
 }
 
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 91f3c7e7a044a..716bcf6873656 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -3170,7 +3170,7 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
 {
 	struct mm_struct *mm = vma->vm_mm;
 	pgtable_t pgtable;
-	pmd_t _pmd, old_pmd;
+	pmd_t _pmd = __pmd(0), old_pmd;
 	unsigned long addr;
 	pte_t *pte;
 	int i;
@@ -3218,7 +3218,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 	struct folio *folio;
 	struct page *page;
 	pgtable_t pgtable;
-	pmd_t old_pmd, _pmd;
+	pmd_t old_pmd, _pmd = __pmd(0);
 	bool soft_dirty, uffd_wp = false, young = false, write = false;
 	bool anon_exclusive = false, dirty = false;
 	unsigned long addr;

-- 
2.53.0


  parent reply	other threads:[~2026-12-04  4:18 UTC|newest]

Thread overview: 22+ 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-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 ` Zi Yan [this message]
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: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  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  5:45   ` Lance Yang

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=20261203-anon_vma_uaf_fix-v2-6-3d4c746e8839@nvidia.com \
    --to=ziy@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreas@gaisler.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=davem@davemloft.net \
    --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=ljs@kernel.org \
    --cc=nico.pache@linux.dev \
    --cc=riel@surriel.com \
    --cc=ryan.roberts@arm.com \
    --cc=sparclinux@vger.kernel.org \
    --cc=usama.arif@linux.dev \
    --cc=vbabka@kernel.org \
    /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

all inboxes | Powered by JetHome®