[PATCH v2 09/17] mm/sparse: initialize memory sections earlier

From: Muchun Song

Date: Mon Jul 20 2026 - 05:47:25 EST


Upcoming HugeTLB bootmem changes need sparsemem section metadata before
the HugeTLB bootmem allocation path runs. The memory sections are
initialized from sparse_init(), which is called too late for that setup.

Move the code that initializes sparsemem section metadata for memblock
ranges into mm_core_init_early(), before free_area_init() and the HugeTLB
bootmem setup. Rename the helper to sparse_sections_init() so the new
caller describes the sparsemem-specific initialization step.

This is a preparatory change.

Signed-off-by: Muchun Song <songmuchun@xxxxxxxxxxxxx>
---
v2:
- Rename the helper to sparse_sections_init() to describe the section
metadata initialization (suggested by Mike Rapoport)
- Fix the !SPARSEMEM stub name so SPARSEMEM=n builds compile
(reported by Sashiko)
---
mm/mm_init.c | 1 +
mm/sparse.c | 10 ++--------
mm/sparse.h | 2 ++
3 files changed, 5 insertions(+), 8 deletions(-)

diff --git a/mm/mm_init.c b/mm/mm_init.c
index 191d4ebe9314..b3ebecf077a0 100644
--- a/mm/mm_init.c
+++ b/mm/mm_init.c
@@ -2636,6 +2636,7 @@ void __init __weak mem_init(void)

void __init mm_core_init_early(void)
{
+ sparse_sections_init();
free_area_init();

hugetlb_cma_reserve();
diff --git a/mm/sparse.c b/mm/sparse.c
index 24555a32a5d9..d4641aa70497 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -191,12 +191,8 @@ static void __init memory_present(int nid, unsigned long start, unsigned long en
}
}

-/*
- * Mark all memblocks as present using memory_present().
- * This is a convenience function that is useful to mark all of the systems
- * memory as present during initialization.
- */
-static void __init memblocks_present(void)
+/* Initialize memory section metadata for all system memory. */
+void __init sparse_sections_init(void)
{
unsigned long start, end;
int i, nid;
@@ -332,8 +328,6 @@ void __init sparse_init(void)
unsigned long pnum_end, pnum_begin, map_count = 1;
int nid_begin;

- memblocks_present();
-
if (compound_info_has_mask()) {
VM_WARN_ON_ONCE(!IS_ALIGNED((unsigned long) pfn_to_page(0),
MAX_FOLIO_VMEMMAP_ALIGN));
diff --git a/mm/sparse.h b/mm/sparse.h
index 4377b097dc8d..b2dc7362af4f 100644
--- a/mm/sparse.h
+++ b/mm/sparse.h
@@ -14,6 +14,7 @@
* mm/sparse.c
*/
#ifdef CONFIG_SPARSEMEM
+void sparse_sections_init(void);
void sparse_init(void);
int sparse_index_init(unsigned long section_nr, int nid);

@@ -48,6 +49,7 @@ static inline void __section_mark_present(struct mem_section *ms,
ms->section_mem_map |= SECTION_MARKED_PRESENT;
}
#else
+static inline void sparse_sections_init(void) {}
static inline void sparse_init(void) {}
#endif /* CONFIG_SPARSEMEM */

--
2.54.0