[RFC PATCH v2 05/43] mm: Avoid split pmd ptl if pmd level is run-time folded

From: Eric Curtin

Date: Mon Nov 30 2026 - 20:52:57 EST


From: Ryan Roberts <ryan.roberts@xxxxxxx>

If there are only two levels of translation, the first level (pgd) may
not be an entire page and so may not have a ptdesc backing it (this can
be the case on arm64, depending on VA size and page size). Even if it
does have a ptdesc, pagetable_pmd_ctor() is not called for it, since it
is a pgd and not a pmd table, so the per-ptdesc ptl fields are not
initialised.

So far this has been fine: the arch knows at compile time whether the
pmd level is folded and, if so, does not select
CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK. With boot-time page size selection
the number of levels is not known at compile time, so the choice has to
be made at boot.

When CONFIG_ARCH_ENABLE_SPLIT_PMD_PTLOCK is selected, make
pmd_lockptr() return mm->page_table_lock if mm_pmd_folded() is true, and
have pmd_huge_pte() use mm->pmd_huge_pte in that case. Add
CONFIG_ARCH_RUNTIME_PMD_FOLD for arches that fold at run time, and
CONFIG_MM_PMD_HUGE_PTE to describe when mm_struct needs the pmd_huge_pte
member (THP without split pmd ptlocks, or with run-time pmd folding).

Signed-off-by: Ryan Roberts <ryan.roberts@xxxxxxx>
Signed-off-by: Eric Curtin <eric.curtin@xxxxxxxxxx>
(cherry picked from commit 78715f47a6259f419370cf8089bd79361516be26)
---
include/linux/mm.h | 17 ++++++++++++++++-
include/linux/mm_types.h | 2 +-
kernel/fork.c | 4 ++--
mm/Kconfig | 8 ++++++++
4 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 040cff64381b..14a35440c806 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -4208,6 +4208,8 @@ static inline struct ptdesc *pmd_ptdesc(pmd_t *pmd)

static inline spinlock_t *pmd_lockptr(struct mm_struct *mm, pmd_t *pmd)
{
+ if (mm_pmd_folded(mm))
+ return &mm->page_table_lock;
return ptlock_ptr(pmd_ptdesc(pmd));
}

@@ -4219,7 +4221,20 @@ static inline bool pmd_ptlock_init(struct ptdesc *ptdesc)
return ptlock_init(ptdesc);
}

-#define pmd_huge_pte(mm, pmd) (pmd_ptdesc(pmd)->pmd_huge_pte)
+static inline pgtable_t *__pmd_huge_pte(struct mm_struct *mm, pmd_t *pmd)
+{
+#ifdef CONFIG_TRANSPARENT_HUGEPAGE
+#ifdef CONFIG_ARCH_RUNTIME_PMD_FOLD
+ if (mm_pmd_folded(mm))
+ return &mm->pmd_huge_pte;
+#endif
+ return &pmd_ptdesc(pmd)->pmd_huge_pte;
+#else
+ return NULL;
+#endif
+}
+
+#define pmd_huge_pte(mm, pmd) (*__pmd_huge_pte(mm, pmd))

#else

diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 1ed35d258d2e..ed9277798631 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -1382,7 +1382,7 @@ struct mm_struct {
#ifdef CONFIG_MMU_NOTIFIER
struct mmu_notifier_subscriptions *notifier_subscriptions;
#endif
-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
+#ifdef CONFIG_MM_PMD_HUGE_PTE
pgtable_t pmd_huge_pte; /* protected by page_table_lock */
#endif
#ifdef CONFIG_NUMA_BALANCING
diff --git a/kernel/fork.c b/kernel/fork.c
index d55a7d21a8fb..5a0db5a89615 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -647,7 +647,7 @@ static void check_mm(struct mm_struct *mm)
pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n",
mm_pgtables_bytes(mm));

-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
+#ifdef CONFIG_MM_PMD_HUGE_PTE
VM_BUG_ON_MM(mm->pmd_huge_pte, mm);
#endif
}
@@ -1110,7 +1110,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
RCU_INIT_POINTER(mm->exe_file, NULL);
mmu_notifier_subscriptions_init(mm);
init_tlb_flush_pending(mm);
-#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS)
+#ifdef CONFIG_MM_PMD_HUGE_PTE
mm->pmd_huge_pte = NULL;
#endif
mm_init_uprobes_state(mm);
diff --git a/mm/Kconfig b/mm/Kconfig
index afff1b25b543..f0868970b367 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -611,10 +611,18 @@ config SPLIT_PTE_PTLOCKS
config ARCH_ENABLE_SPLIT_PMD_PTLOCK
bool

+config ARCH_RUNTIME_PMD_FOLD
+ bool
+
config SPLIT_PMD_PTLOCKS
def_bool y
depends on SPLIT_PTE_PTLOCKS && ARCH_ENABLE_SPLIT_PMD_PTLOCK

+config MM_PMD_HUGE_PTE
+ def_bool y
+ depends on TRANSPARENT_HUGEPAGE
+ depends on !SPLIT_PMD_PTLOCKS || ARCH_RUNTIME_PMD_FOLD
+
#
# support for memory balloon
config BALLOON