[PATCH 41/49] mm/sparse: simplify section_vmemmap_pages()

Muchun Song
2026-04-05T12:58:05Z

After unifying DAX and HugeTLB vmemmap optimizations, we can now
simplify section_vmemmap_pages().

Previously, section_vmemmap_pages() needed to take altmap and pgmap
arguments to determine if vmemmap optimization was enabled. However,
sparse_add_section() already sets the section order using
section_set_order(ms, pgmap->vmemmap_shift) if vmemmap_can_optimize()
is true and the size is aligned to PAGES_PER_SECTION.

As a result, section_vmemmap_optimizable(ms) is sufficient to determine
if the section can be optimized, and section_order(ms) can directly
provide the order, making the altmap and pgmap arguments redundant.

Remove the unused altmap and pgmap arguments from
section_vmemmap_pages().

Signed-off-by: Muchun Song <songmuchun@bytedance.com>
---
mm/internal.h | 3 +--
mm/sparse-vmemmap.c | 8 +++-----
mm/sparse.c | 18 ++++++------------
3 files changed, 10 insertions(+), 19 deletions(-)

diff --git a/mm/internal.h b/mm/internal.h
index b569d8309f4d..7f0731e5c84f 100644
--- a/mm/internal.h
+++ b/mm/internal.h
@@ -998,8 +998,7 @@ static inline void __section_mark_present(struct mem_section *ms,
ms->section_mem_map |= SECTION_MARKED_PRESENT;
}

-int section_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
- struct vmem_altmap *altmap, struct dev_pagemap *pgmap);
+int section_vmemmap_pages(unsigned long pfn, unsigned long nr_pages);
#else
static inline void memblocks_present(void) {}
static inline void sparse_init(void) {}
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index ba8c0c64f160..ac2efba9ef92 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -608,12 +608,10 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
* section_activate() and pfn_valid() .
*/
if (!section_is_early) {
- memmap_pages_add(-section_vmemmap_pages(pfn, nr_pages, altmap,
- pgmap));
+ memmap_pages_add(-section_vmemmap_pages(pfn, nr_pages));
depopulate_section_memmap(pfn, nr_pages, altmap);
} else if (memmap) {
- memmap_pages_add(-section_vmemmap_pages(pfn, nr_pages, altmap,
- pgmap));
+ memmap_pages_add(-section_vmemmap_pages(pfn, nr_pages));
free_map_bootmem(memmap);
}

@@ -658,7 +656,7 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,
return pfn_to_page(pfn);

memmap = populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap);
- memmap_pages_add(section_vmemmap_pages(pfn, nr_pages, altmap, pgmap));
+ memmap_pages_add(section_vmemmap_pages(pfn, nr_pages));
if (!memmap) {
section_deactivate(pfn, nr_pages, altmap, pgmap);
return ERR_PTR(-ENOMEM);
diff --git a/mm/sparse.c b/mm/sparse.c
index 04c641b97325..163bb17bba96 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -345,28 +345,23 @@ static void __init sparse_usage_fini(void)
sparse_usagebuf = sparse_usagebuf_end = NULL;
}

-int __meminit section_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
- struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
+int __meminit section_vmemmap_pages(unsigned long pfn, unsigned long nr_pages)
{
const struct mem_section *ms = __pfn_to_section(pfn);
- unsigned int order = pgmap ? pgmap->vmemmap_shift : section_order(ms);
+ unsigned int order = section_order(ms);
unsigned long pages_per_compound = 1L << order;
- unsigned int vmemmap_pages = OPTIMIZED_FOLIO_VMEMMAP_PAGES;
-
- if (vmemmap_can_optimize(altmap, pgmap))
- vmemmap_pages = VMEMMAP_RESERVE_NR;

VM_BUG_ON(!IS_ALIGNED(pfn | nr_pages, min(pages_per_compound, PAGES_PER_SECTION)));
VM_BUG_ON(pfn_to_section_nr(pfn) != pfn_to_section_nr(pfn + nr_pages - 1));

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

if (order < PFN_SECTION_SHIFT)
- return vmemmap_pages * nr_pages / pages_per_compound;
+ return OPTIMIZED_FOLIO_VMEMMAP_PAGES * nr_pages / pages_per_compound;

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

return 0;
}
@@ -396,8 +391,7 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
nid, NULL, NULL);
if (!map)
panic("Populate section (%ld) on node[%d] failed\n", pnum, nid);
- memmap_boot_pages_add(section_vmemmap_pages(pfn, PAGES_PER_SECTION,
- NULL, NULL));
+ memmap_boot_pages_add(section_vmemmap_pages(pfn, PAGES_PER_SECTION));
sparse_init_early_section(nid, map, pnum, 0);
}
sparse_usage_fini();
--
2.20.1