[RFC 02/10] mm: introduce pgtable_has_pmd_leaves()

From: Luiz Capitulino

Date: Thu Nov 06 2025 - 16:29:24 EST


This is a new helper which can be used to check if the architecture
supports PMD-sized pages. It's intended to replace two existing helpers:

- has_transparent_hugepages(): it's used to check for PMD-sized pages
(not exactly THP support), but it may perform a hardware check so it's
not intended to be used in fast paths

- thp_disabled_by_hw(): also checks for PMD-sized pages support, but
uses a cached value

pgtable_has_pmd_leaves() implementation is split in two parts:

1. init_arch_has_pmd_leaves(): runs at boottime as early_initcall as a
wrapper to has_transparent_hugepages(). The result is cached

2. pgtable_has_pmd_leaves(): just returns the cached value

The next commits will convert users of both has_transparent_hugepages()
and thp_disabled_by_hw() to pgtable_has_pmd_leaves().

Signed-off-by: Luiz Capitulino <luizcap@xxxxxxxxxx>
---
include/linux/pgtable.h | 10 ++++++++++
mm/memory.c | 10 ++++++++++
2 files changed, 20 insertions(+)

diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h
index 32e8457ad535..e4c5f70b0a01 100644
--- a/include/linux/pgtable.h
+++ b/include/linux/pgtable.h
@@ -2001,6 +2001,16 @@ static inline const char *pgtable_level_to_str(enum pgtable_level level)
}
}

+/*
+ * IMPORTANT: pgtable_has_pmd_leaves() can only be called after
+ * early_initcall, since that's when __arch_has_pmd_leaves is set
+ */
+extern bool __arch_has_pmd_leaves;
+static inline bool pgtable_has_pmd_leaves(void)
+{
+ return __arch_has_pmd_leaves;
+}
+
#endif /* !__ASSEMBLY__ */

#if !defined(MAX_POSSIBLE_PHYSMEM_BITS) && !defined(CONFIG_64BIT)
diff --git a/mm/memory.c b/mm/memory.c
index 74b45e258323..7b50f3ec9b37 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -176,6 +176,16 @@ static int __init init_zero_pfn(void)
}
early_initcall(init_zero_pfn);

+bool __arch_has_pmd_leaves __read_mostly;
+EXPORT_SYMBOL(__arch_has_pmd_leaves);
+
+static int __init init_arch_has_pmd_leaves(void)
+{
+ __arch_has_pmd_leaves = has_transparent_hugepage();
+ return 0;
+}
+early_initcall(init_arch_has_pmd_leaves);
+
void mm_trace_rss_stat(struct mm_struct *mm, int member)
{
trace_rss_stat(mm, member);
--
2.51.1