[PATCH v2 05/17] mm/sparse-vmemmap: support section-based vmemmap accounting

From: Muchun Song

Date: Mon Jul 20 2026 - 05:46:55 EST


section_nr_vmemmap_pages() can account ordinary sections and DAX sections,
but section-based vmemmap optimization keeps its compound order in struct
mem_section and retains a different number of vmemmap pages.

Teach section_nr_vmemmap_pages() to recognize section-based optimized
sections and calculate their vmemmap page count from the section order
and the HVO retained page count.

Signed-off-by: Muchun Song <songmuchun@xxxxxxxxxxxxx>
---
Hi Mike,

I reintroduced VMEMMAP_OPTIMIZATION_PAGES (the intermediate macro), which we
discussed removing in v1, so the section-based HVO path doesn't have to
hard-code its retained vmemmap page count (i.e. 1) in section_nr_vmemmap_pages().
To better unify the codebase in the future, the new macro introduced in this patch
will also be applied to the HugeTLB code (e.g., to calculate
HUGETLB_VMEMMAP_RESERVE_SIZE from this new macro as well).

v2:
- Remove an unnecessary vmemmap_can_optimize() call to simplify the code
(suggested by Mike Rapoport).
- Rewrite the commit message for better understanding.
---
include/linux/mmzone.h | 6 ++++--
mm/sparse-vmemmap.c | 10 ++++++----
mm/sparse.h | 8 ++++++++
3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 2a32101d55e6..025ea69f57d8 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -107,8 +107,10 @@
is_power_of_2(sizeof(struct page)) ? \
MAX_FOLIO_NR_PAGES * sizeof(struct page) : 0)

-/* The number of struct pages covered by the retained vmemmap pages with HVO enabled. */
-#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES (PAGE_SIZE / sizeof(struct page))
+/* The number of retained vmemmap pages with HVO enabled. */
+#define VMEMMAP_OPTIMIZATION_PAGES 1
+#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES \
+ (VMEMMAP_OPTIMIZATION_PAGES * PAGE_SIZE / sizeof(struct page))
#define VMEMMAP_OPTIMIZATION_MIN_ORDER (ilog2(VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) + 1)

#define __VMEMMAP_OPTIMIZATION_NR_ORDERS \
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index 107215cf8488..b7abc5494bb9 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -649,24 +649,26 @@ void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
static int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
{
- const unsigned int order = pgmap ? pgmap->vmemmap_shift : 0;
+ const struct mem_section *ms = __pfn_to_section(pfn);
+ const int order = pgmap ? pgmap->vmemmap_shift : section_order(ms);
+ const int vmemmap_pages = pgmap ? VMEMMAP_RESERVE_NR : VMEMMAP_OPTIMIZATION_PAGES;
const unsigned long pages_per_compound = 1UL << order;

VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION));
VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION);

- if (!vmemmap_can_optimize(altmap, pgmap))
+ if (!vmemmap_can_optimize(altmap, pgmap) && !section_vmemmap_optimizable(ms))
return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);

if (order < PFN_SECTION_SHIFT) {
VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, pages_per_compound));
- return VMEMMAP_RESERVE_NR * nr_pages / pages_per_compound;
+ return vmemmap_pages * nr_pages / pages_per_compound;
}

VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION));

if (IS_ALIGNED(pfn, pages_per_compound))
- return VMEMMAP_RESERVE_NR;
+ return vmemmap_pages;

return 0;
}
diff --git a/mm/sparse.h b/mm/sparse.h
index 030248030dc7..1e9fcea10181 100644
--- a/mm/sparse.h
+++ b/mm/sparse.h
@@ -74,6 +74,14 @@ static inline bool pfn_vmemmap_optimizable(unsigned long pfn)
return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES;
}

+static inline bool section_vmemmap_optimizable(const struct mem_section *section)
+{
+ if (!is_power_of_2(sizeof(struct page)))
+ return false;
+
+ return section_order(section) >= VMEMMAP_OPTIMIZATION_MIN_ORDER;
+}
+
/*
* mm/sparse-vmemmap.c
*/
--
2.54.0