[PATCH 20/49] mm: rename vmemmap optimization macros to generic names

Muchun Song
2026-04-05T12:55:38Z

In preparation for unifying the vmemmap optimization paths for both
DAX and HugeTLB, rename the existing vmemmap tail page macros to
more generic, semantic-based names.

The original names (e.g., VMEMMAP_TAIL_MIN_ORDER) fail to clearly express
the actual requirement: it represents the minimum order of a folio that
can satisfy the vmemmap optimization. To provide a broader and clearer
abstraction for other users like DAX, replace them with newly introduced
macros like OPTIMIZABLE_FOLIO_MIN_ORDER and NR_OPTIMIZABLE_FOLIO_SIZES.

These new macros, along with OPTIMIZED_FOLIO_VMEMMAP_PAGES,
OPTIMIZED_FOLIO_VMEMMAP_SIZE, and OPTIMIZED_FOLIO_VMEMMAP_PAGE_STRUCTS,
are explicitly bound to the 'folio' concept. This systematic naming
makes it clearer to describe the properties of a vmemmap-optimized
folio rather than just raw pages.

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
include/linux/mmzone.h | 18 ++++++++++--------
mm/hugetlb_vmemmap.c | 6 +++---
mm/sparse-vmemmap.c | 4 ++--
3 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 8ee9dc60120a..378feaf4e4ed 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -107,13 +107,15 @@
is_power_of_2(sizeof(struct page)) ? \
MAX_FOLIO_NR_PAGES * sizeof(struct page) : 0)

-/*
- * vmemmap optimization (like HVO) is only possible for page orders that fill
- * two or more pages with struct pages.
- */
-#define VMEMMAP_TAIL_MIN_ORDER (ilog2(2 * PAGE_SIZE / sizeof(struct page)))
-#define __NR_VMEMMAP_TAILS (MAX_FOLIO_ORDER - VMEMMAP_TAIL_MIN_ORDER + 1)
-#define NR_VMEMMAP_TAILS (__NR_VMEMMAP_TAILS > 0 ? __NR_VMEMMAP_TAILS : 0)
+/* The number of vmemmap pages required by a vmemmap-optimized folio. */
+#define OPTIMIZED_FOLIO_VMEMMAP_PAGES 1
+#define OPTIMIZED_FOLIO_VMEMMAP_SIZE (OPTIMIZED_FOLIO_VMEMMAP_PAGES * PAGE_SIZE)
+#define OPTIMIZED_FOLIO_VMEMMAP_PAGE_STRUCTS (OPTIMIZED_FOLIO_VMEMMAP_SIZE / sizeof(struct page))
+#define OPTIMIZABLE_FOLIO_MIN_ORDER (ilog2(OPTIMIZED_FOLIO_VMEMMAP_PAGE_STRUCTS) + 1)
+
+#define __NR_OPTIMIZABLE_FOLIO_SIZES (MAX_FOLIO_ORDER - OPTIMIZABLE_FOLIO_MIN_ORDER + 1)
+#define NR_OPTIMIZABLE_FOLIO_SIZES \
+ (__NR_OPTIMIZABLE_FOLIO_SIZES > 0 ? __NR_OPTIMIZABLE_FOLIO_SIZES : 0)

enum migratetype {
MIGRATE_UNMOVABLE,
@@ -1144,7 +1146,7 @@ struct zone {
atomic_long_t vm_stat[NR_VM_ZONE_STAT_ITEMS];
atomic_long_t vm_numa_event[NR_VM_NUMA_EVENT_ITEMS];
#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
- struct page *vmemmap_tails[NR_VMEMMAP_TAILS];
+ struct page *vmemmap_tails[NR_OPTIMIZABLE_FOLIO_SIZES];
#endif
} ____cacheline_internodealigned_in_smp;

diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c
index 535f0369a496..d6dd47c232e0 100644
--- a/mm/hugetlb_vmemmap.c
+++ b/mm/hugetlb_vmemmap.c
@@ -495,7 +495,7 @@ static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *

static struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
{
- const unsigned int idx = order - VMEMMAP_TAIL_MIN_ORDER;
+ const unsigned int idx = order - OPTIMIZABLE_FOLIO_MIN_ORDER;
struct page *tail, *p;
int node = zone_to_nid(zone);

@@ -828,7 +828,7 @@ static int __init hugetlb_vmemmap_init(void)
BUILD_BUG_ON(__NR_USED_SUBPAGE > HUGETLB_VMEMMAP_RESERVE_PAGES);

for_each_zone(zone) {
- for (int i = 0; i < NR_VMEMMAP_TAILS; i++) {
+ for (int i = 0; i < NR_OPTIMIZABLE_FOLIO_SIZES; i++) {
struct page *tail, *p;
unsigned int order;

@@ -836,7 +836,7 @@ static int __init hugetlb_vmemmap_init(void)
if (!tail)
continue;

- order = i + VMEMMAP_TAIL_MIN_ORDER;
+ order = i + OPTIMIZABLE_FOLIO_MIN_ORDER;
p = page_to_virt(tail);
for (int j = 0; j < PAGE_SIZE / sizeof(struct page); j++)
init_compound_tail(p + j, NULL, order, zone);
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index 3fdb6808e8ab..9f70559df4e8 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -330,12 +330,12 @@ static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone *
unsigned int idx;
int node = zone_to_nid(zone);

- if (WARN_ON_ONCE(order < VMEMMAP_TAIL_MIN_ORDER))
+ if (WARN_ON_ONCE(order < OPTIMIZABLE_FOLIO_MIN_ORDER))
return NULL;
if (WARN_ON_ONCE(order > MAX_FOLIO_ORDER))
return NULL;

- idx = order - VMEMMAP_TAIL_MIN_ORDER;
+ idx = order - OPTIMIZABLE_FOLIO_MIN_ORDER;
tail = zone->vmemmap_tails[idx];
if (tail)
return tail;
--
2.20.1