mm: Refactor bootmem gigantic hugepage allocation
Subsystems (8)
v4 cover mm: Refactor bootmem gigantic hugepage allocation
This series is split out from the earlier larger series "mm: Generalize HVO for HugeTLB and device DAX" [1]. It collects the first 19 patches of that series as a standalone set of fixes and preparatory cleanups around bootmem HugeTLB handling, sparse initialization ordering, and related vmemmap setup.
The first patches fix a few bugs found while reviewing the existing code, including incorrect bootmem HVO handling, wrong vmemmap registration arguments, a powerpc compound-vmemmap tracking bug, and too-late initialization of gigantic bootmem HugeTLB struct pages.
The rest of the series reorders early memory initialization so the relevant zone state is available before sparse and HugeTLB boot-time setup runs, then simplifies the remaining bootmem gigantic hugepage allocation path and removes code made obsolete by that rework.
At a high level:
- patches [1-4] fix boot-time and arch-specific bugs
- patches [5-12] reorder and simplify sparse/mm/hugetlb early init
- patches [13-19] refactor bootmem gigantic hugepage allocation and remove obsolete helpers and state
Changes since v3:
- patches 2-4: add Cc: [email protected] to the applicable fixes
- patch 3: collect Reviewed-by from Ritesh Harjani
- patch 15: move pfn_to_zone() before hugetlb_vmemmap_init_early() instead of using a forward declaration
- patches 15-17: collect Acked-by from Mike Rapoport where applicable
- patch 15: collect Acked-by from Usama Arif
- patch 18: clarify that the changelog also covers the early CMA bootmem allocation fallback refactoring
- patch 19: initialize pgdat at declaration in __init_deferred_page() and collect Reviewed-by from Mike Rapoport
[1] https://lore.kernel.org/linux-mm/[email protected]/
Muchun Song (19):
mm/hugetlb: Fix boot panic with CONFIG_DEBUG_VM and HVO bootmem pages mm/hugetlb_vmemmap: Fix __hugetlb_vmemmap_optimize_folios() powerpc/mm: Fix wrong addr_pfn tracking in compound vmemmap population mm/hugetlb: Initialize gigantic bootmem hugepage struct pages earlier mm/mm_init: Simplify deferred_free_pages() migratetype init mm/sparse: Panic on memmap and usemap allocation failure mm/sparse: Move subsection_map_init() into sparse_init() mm/mm_init: Defer sparse_init() until after zone initialization mm/mm_init: Defer hugetlb reservation until after zone initialization mm/mm_init: Remove set_pageblock_order() call from sparse_init() mm/sparse: Move sparse_vmemmap_init_nid_late() into sparse_init_nid() mm/hugetlb_cma: Validate hugetlb CMA range by zone at reserve time mm/hugetlb: Refactor early boot gigantic hugepage allocation mm/hugetlb: Free cross-zone bootmem gigantic pages after allocation mm/hugetlb_vmemmap: Move bootmem HVO setup to early init mm/hugetlb: Remove obsolete bootmem cross-zone checks mm/sparse-vmemmap: Remove sparse_vmemmap_init_nid_late() mm/hugetlb: Remove unused bootmem cma field
mm/mm_init: Fold __init_page_from_nid() into __init_deferred_page()
arch/powerpc/mm/book3s64/radix_pgtable.c | 7 +- arch/powerpc/mm/hugetlbpage.c | 13 +- include/linux/hugetlb.h | 24 +-- include/linux/mmzone.h | 7 - mm/cma.c | 3 +- mm/hugetlb.c | 259 +++++++++++------------ mm/hugetlb_cma.c | 44 ++-- mm/hugetlb_cma.h | 8 +- mm/hugetlb_vmemmap.c | 120 +++-------- mm/hugetlb_vmemmap.h | 5 - mm/internal.h | 14 +- mm/mm_init.c | 86 +++----- mm/sparse-vmemmap.c | 26 ++- mm/sparse.c | 48 +---- 14 files changed, 251 insertions(+), 413 deletions(-)
base-commit: ec039126b7fac4e3af35ebccaa7c6f9b6875ba81
Signature · 1 line
2.54.0
PATCH v4 1/19 mm/hugetlb: Fix boot panic with CONFIG_DEBUG_VM and HVO bootmem pages
Commit 622026e87c40 ("mm/hugetlb: remove fake head pages") switched HVO to reuse per-zone shared tail pages from zone->vmemmap_tails[].
Those shared tail pages were initialized in hugetlb_vmemmap_init(), but bootmem HugeTLB folios are prepared earlier from gather_bootmem_prealloc(). With hugetlb_free_vmemmap=on, prep_and_add_bootmem_folios() can access pageblock flags on bootmem HugeTLB pages whose mirrored tail struct pages already point to the shared tail page. On CONFIG_DEBUG_VM kernels, get_pfnblock_bitmap_bitidx() then dereferences the still-uninitialized shared tail page and can panic during boot.
Initialize zone->vmemmap_tails[] from gather_bootmem_prealloc(), before bootmem HugeTLB folios are processed, and drop the later initialization from hugetlb_vmemmap_init().
This bug only affects CONFIG_DEBUG_VM kernels, where the relevant assertion is evaluated.
--- mm/hugetlb.c | 25 +++++++++++++++++++++++++ mm/hugetlb_vmemmap.c | 17 ----------------- mm/sparse-vmemmap.c | 2 +- 3 files changed, 26 insertions(+), 18 deletions(-)
mm/hugetlb.c +25 -0
@@ -3365,6 +3365,31 @@ static void __init gather_bootmem_prealloc(void) .max_threads = num_node_state(N_MEMORY), .numa_aware = true, }; +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +struct zone *zone; + +for_each_zone (zone) { +for (int i =0 ; i <NR_VMEMMAP_TAILS ; i++) { +struct page *tail, *p; +unsigned int order; + + tail = zone->vmemmap_tails[i]; +if (!tail) +continue ; + + order = i +VMEMMAP_TAIL_MIN_ORDER ; + p =page_to_virt (tail); +/* +* prep_and_add_bootmem_folios() can access pageblock +* flags on bootmem HugeTLB pages, so initialize the +* shared tail struct pages here before bootmem folios +* start using them. +*/ +for (int j =0 ; j <PAGE_SIZE /sizeof (struct page ); j++) +init_compound_tail (p + j,NULL , order, zone); + } + } +#endif padata_do_multithreaded (&job); }
mm/hugetlb_vmemmap.c +0 -17
@@ -870,27 +870,10 @@ static const struct ctl_table hugetlb_vmemmap_sysctls[] = {static int __init hugetlb_vmemmap_init (void ) {const struct hstate *h; -struct zone *zone;/* HUGETLB_VMEMMAP_RESERVE_SIZE should cover all used struct pages */ BUILD_BUG_ON (__NR_USED_SUBPAGE >HUGETLB_VMEMMAP_RESERVE_PAGES ); -for_each_zone (zone) { -for (int i =0 ; i <NR_VMEMMAP_TAILS ; i++) { -struct page *tail, *p; -unsigned int order; - - tail = zone->vmemmap_tails[i]; -if (!tail) -continue ; - - order = i +VMEMMAP_TAIL_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); - } - } -for_each_hstate (h) {if (hugetlb_vmemmap_optimizable (h)) {register_sysctl_init ("vm" , hugetlb_vmemmap_sysctls);
mm/sparse-vmemmap.c +1 -1
@@ -342,7 +342,7 @@ static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone ** * Any initialization done here will be overwritten by memmap_init(). * -* hugetlb_vmemmap_init() will take care of initialization after +* gather_bootmem_prealloc() will take care of initialization after * memmap_init(). */
Signature · 1 line
2.54.0
Re: [PATCH v4 01/19] mm/hugetlb: Fix boot panic with CONFIG_DEBUG_VM and HVO bootmem pages
Quoted 50 lines · Muchun Song
Commit
622026e87c40("mm/hugetlb: remove fake head pages") switched HVO to reuse per-zone shared tail pages from zone->vmemmap_tails[].Those shared tail pages were initialized in
hugetlb_vmemmap_init(), but bootmem HugeTLB folios are prepared earlier fromgather_bootmem_prealloc(). With hugetlb_free_vmemmap=on,prep_and_add_bootmem_folios()can access pageblock flags on bootmem HugeTLB pages whose mirrored tail struct pages already point to the shared tail page. OnCONFIG_DEBUG_VMkernels,get_pfnblock_bitmap_bitidx()then dereferences the still-uninitialized shared tail page and can panic during boot.Initialize zone->vmemmap_tails[] from
gather_bootmem_prealloc(), before bootmem HugeTLB folios are processed, and drop the later initialization fromhugetlb_vmemmap_init().This bug only affects
CONFIG_DEBUG_VMkernels, where the relevant assertion is evaluated.Fixes 622026e87c40 ("mm/hugetlb: remove fake head pages")Signed-off-by Muchun Song <[email protected]>Acked-by Oscar Salvador <[email protected]>--- mm/hugetlb.c | 25 +++++++++++++++++++++++++ mm/hugetlb_vmemmap.c | 17 ----------------- mm/sparse-vmemmap.c | 2 +- 3 files changed, 26 insertions(+), 18 deletions(-)mm/hugetlb.c +15
-0@@ -3365,6 +3365,31 @@ static void __init gather_bootmem_prealloc(void) .max_threads = num_node_state(N_MEMORY), .numa_aware = true, }; +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +struct zone *zone; + +for_each_zone (zone) { +for (int i =0 ; i <NR_VMEMMAP_TAILS ; i++) { +struct page *tail, *p; +unsigned int order; + + tail = zone->vmemmap_tails[i]; +if (!tail) +continue ; + + order = i +VMEMMAP_TAIL_MIN_ORDER ; + p =page_to_virt (tail); +/*
mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 3392
Quoted 10 lines
mm/hugetlb.c +10
-0+* prep_and_add_bootmem_folios() can access pageblock +* flags on bootmem HugeTLB pages, so initialize the +* shared tail struct pages here before bootmem folios +* start using them. +*/ +for (int j =0 ; j <PAGE_SIZE /sizeof (struct page ); j++) + init_compound_tail(p + j, NULL, order, zone); + } + } +#endif
I think it would be better if this was located in mm/hugetlb_vmemmap.c as a separate function.
Quoted 40 lines
mm/hugetlb.c +0
-0padata_do_multithreaded (&job); }mm/hugetlb_vmemmap.c +0
-17@@ -870,27 +870,10 @@ static const struct ctl_table hugetlb_vmemmap_sysctls[] = {static int __init hugetlb_vmemmap_init (void ) {const struct hstate *h; -struct zone *zone;/* HUGETLB_VMEMMAP_RESERVE_SIZE should cover all used struct pages */ BUILD_BUG_ON (__NR_USED_SUBPAGE >HUGETLB_VMEMMAP_RESERVE_PAGES ); -for_each_zone (zone) { -for (int i =0 ; i <NR_VMEMMAP_TAILS ; i++) { -struct page *tail, *p; -unsigned int order; - - tail = zone->vmemmap_tails[i]; -if (!tail) -continue ; - - order = i +VMEMMAP_TAIL_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); - } - } -for_each_hstate (h) {if (hugetlb_vmemmap_optimizable (h)) {register_sysctl_init ("vm" , hugetlb_vmemmap_sysctls);mm/sparse-vmemmap.c +0
-0
mm/sparse-vmemmap.c static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone * · line 348
Quoted 8 lines
mm/sparse-vmemmap.c +1
-1@@ -342,7 +342,7 @@ static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone ** * Any initialization done here will be overwritten by memmap_init(). * -* hugetlb_vmemmap_init() will take care of initialization after +* gather_bootmem_prealloc() will take care of initialization after * memmap_init(). */
It gets the job done.
Muchun Song
maintainerRe: [PATCH v4 01/19] mm/hugetlb: Fix boot panic with CONFIG_DEBUG_VM and HVO bootmem pages
Quoted 54 lines · Michał Cłapiński
Quoted 49 lines · Muchun Song
Commit
622026e87c40("mm/hugetlb: remove fake head pages") switched HVO to reuse per-zone shared tail pages from zone->vmemmap_tails[]. Those shared tail pages were initialized inhugetlb_vmemmap_init(), but bootmem HugeTLB folios are prepared earlier fromgather_bootmem_prealloc(). With hugetlb_free_vmemmap=on,prep_and_add_bootmem_folios()can access pageblock flags on bootmem HugeTLB pages whose mirrored tail struct pages already point to the shared tail page. OnCONFIG_DEBUG_VMkernels,get_pfnblock_bitmap_bitidx()then dereferences the still-uninitialized shared tail page and can panic during boot.
Initialize zone->vmemmap_tails[] fromgather_bootmem_prealloc(), before bootmem HugeTLB folios are processed, and drop the later initialization fromhugetlb_vmemmap_init().
This bug only affectsCONFIG_DEBUG_VMkernels, where the relevant assertion is evaluated.Fixes 622026e87c40 ("mm/hugetlb: remove fake head pages")Signed-off-by Muchun Song <[email protected]>Acked-by Oscar Salvador <[email protected]>--- mm/hugetlb.c | 25 +++++++++++++++++++++++++ mm/hugetlb_vmemmap.c | 17 ----------------- mm/sparse-vmemmap.c | 2 +- 3 files changed, 26 insertions(+), 18 deletions(-)mm/hugetlb.c +19
-0@@ -3365,6 +3365,31 @@ static void __init gather_bootmem_prealloc(void) .max_threads = num_node_state(N_MEMORY), .numa_aware = true, }; +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +struct zone *zone; + +for_each_zone (zone) { +for (int i =0 ; i <NR_VMEMMAP_TAILS ; i++) { +struct page *tail, *p; +unsigned int order; + + tail = zone->vmemmap_tails[i]; +if (!tail) +continue ; + + order = i +VMEMMAP_TAIL_MIN_ORDER ; + p =page_to_virt (tail); +/* +* prep_and_add_bootmem_folios() can access pageblock +* flags on bootmem HugeTLB pages, so initialize the +* shared tail struct pages here before bootmem folios +* start using them.
Quoted 8 lines
mm/hugetlb.c static void __init gather_bootmem_prealloc(void) · line 3392
Quoted 6 lines
mm/hugetlb.c +6
-0+*/ +for (int j =0 ; j <PAGE_SIZE /sizeof (struct page ); j++) + init_compound_tail(p + j, NULL, order, zone); + } + } +#endif I think it would be better if this was located in
mm/hugetlb_vmemmap.cas a separate function.
This is actually just temporary. I already have another patch [1] that completely removes this code later on. So, to avoid having to update the entire patchset unnecessarily, I figured it's best to just leave it as is for now, as it's not a major issue..
[1] https://lore.kernel.org/all/[email protected]/
Quoted 49 lines
Quoted 38 lines
mm/hugetlb.c +0
-0padata_do_multithreaded (&job); }mm/hugetlb_vmemmap.c +0
-16@@ -870,27 +870,10 @@ static const struct ctl_table hugetlb_vmemmap_sysctls[] = {static int __init hugetlb_vmemmap_init (void ) {const struct hstate *h; -struct zone *zone;/* HUGETLB_VMEMMAP_RESERVE_SIZE should cover all used struct pages */ BUILD_BUG_ON (__NR_USED_SUBPAGE >HUGETLB_VMEMMAP_RESERVE_PAGES ); - for_each_zone(zone) { -for (int i =0 ; i <NR_VMEMMAP_TAILS ; i++) { -struct page *tail, *p; -unsigned int order; - - tail = zone->vmemmap_tails[i]; -if (!tail) -continue ; - - order = i +VMEMMAP_TAIL_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); - } - } -for_each_hstate (h) {if (hugetlb_vmemmap_optimizable (h)) {register_sysctl_init ("vm" , hugetlb_vmemmap_sysctls);mm/sparse-vmemmap.c +0
-0mm/sparse-vmemmap.c static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone * · line 348
Quoted 8 lines
mm/sparse-vmemmap.c +1
-1@@ -342,7 +342,7 @@ static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone ** * Any initialization done here will be overwritten by memmap_init(). * -* hugetlb_vmemmap_init() will take care of initialization after +* gather_bootmem_prealloc() will take care of initialization after * memmap_init(). */ It gets the job done.
Quoted 2 lines
Tested-by Michal Clapinski <[email protected]>Reviewed-by Michal Clapinski <[email protected]>
Really thanks for your test.
Muchun,
Thanks.
PATCH v4 2/19 mm/hugetlb_vmemmap: Fix __hugetlb_vmemmap_optimize_folios()
In reply to Muchun Song__hugetlb_vmemmap_optimize_folios() uses incorrect arguments when handling bootmem HugeTLB folios.
The section number passed to register_page_bootmem_memmap() is derived from the vmemmap virtual address of folio->page instead of the folio PFN, so the bootmem memmap metadata can be registered against the wrong section. The helper is also given HUGETLB_VMEMMAP_RESERVE_SIZE even though it expects a page count, not a size in bytes. In addition, the write-protect range is based on pages_per_huge_page(h), which does not cover the full HugeTLB vmemmap area and can leave part of the shared tail vmemmap mapping writable.
Fix the section lookup to use folio_pfn(folio), use HUGETLB_VMEMMAP_RESERVE_PAGES when registering the reserved memmap pages, and use hugetlb_vmemmap_size(h) for the write-protect range.
--- mm/hugetlb_vmemmap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
mm/hugetlb_vmemmap.c +3 -3
@@ -635,12 +635,12 @@ static void __hugetlb_vmemmap_optimize_folios(struct hstate *h,* mirrored tail page structs RO. */ spfn = (unsigned long )&folio->page; - epfn = spfn +pages_per_huge_page (h); + epfn = spfn +hugetlb_vmemmap_size (h);vmemmap_wrprotect_hvo (spfn, epfn,folio_nid (folio),HUGETLB_VMEMMAP_RESERVE_SIZE ); -register_page_bootmem_memmap (pfn_to_section_nr (spfn), +register_page_bootmem_memmap (pfn_to_section_nr (folio_pfn (folio)), &folio->page, -HUGETLB_VMEMMAP_RESERVE_SIZE ); +HUGETLB_VMEMMAP_RESERVE_PAGES );continue ; }
Signature · 1 line
2.54.0
Re: [PATCH v4 02/19] mm/hugetlb_vmemmap: Fix __hugetlb_vmemmap_optimize_folios()
Quoted 34 lines · Muchun Song
__hugetlb_vmemmap_optimize_folios()uses incorrect arguments when handling bootmem HugeTLB folios.The section number passed to
register_page_bootmem_memmap()is derived from the vmemmap virtual address offolio->pageinstead of the folio PFN, so the bootmem memmap metadata can be registered against the wrong section. The helper is also givenHUGETLB_VMEMMAP_RESERVE_SIZEeven though it expects a page count, not a size in bytes. In addition, the write-protect range is based onpages_per_huge_page(h), which does not cover the full HugeTLB vmemmap area and can leave part of the shared tail vmemmap mapping writable.Fix the section lookup to use
folio_pfn(folio), useHUGETLB_VMEMMAP_RESERVE_PAGESwhen registering the reserved memmap pages, and usehugetlb_vmemmap_size(h)for the write-protect range.Fixes 752fe17af693 ("mm/hugetlb: add pre-HVO framework")Signed-off-by Muchun Song <[email protected]>Acked-by Oscar Salvador <[email protected]>--- mm/hugetlb_vmemmap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)mm/hugetlb_vmemmap.c +1
-1@@ -635,12 +635,12 @@ static void __hugetlb_vmemmap_optimize_folios(struct hstate *h,* mirrored tail page structs RO. */ spfn = (unsigned long )&folio->page; - epfn = spfn +pages_per_huge_page (h); + epfn = spfn +hugetlb_vmemmap_size (h);
mm/hugetlb_vmemmap.c static void __hugetlb_vmemmap_optimize_folios(struct hstate *h, · line 646
Quoted 10 lines
mm/hugetlb_vmemmap.c +2
-2vmemmap_wrprotect_hvo (spfn, epfn,folio_nid (folio),HUGETLB_VMEMMAP_RESERVE_SIZE ); -register_page_bootmem_memmap (pfn_to_section_nr (spfn), +register_page_bootmem_memmap (pfn_to_section_nr (folio_pfn (folio)), &folio->page, -HUGETLB_VMEMMAP_RESERVE_SIZE ); +HUGETLB_VMEMMAP_RESERVE_PAGES );continue ; }
Thanks for fixing my mistakes!
PATCH v4 3/19 powerpc/mm: Fix wrong addr_pfn tracking in compound vmemmap population
In reply to Muchun Songvmemmap_populate_compound_pages() uses addr_pfn to determine the PFN offset within a compound page and to decide whether the current vmemmap slot should be populated as a head page mapping or should reuse a tail page mapping.
However, addr_pfn is advanced manually in parallel with addr. The loop itself progresses in vmemmap address space, so each PAGE_SIZE step in addr covers PAGE_SIZE / sizeof(struct page) struct page slots. Since addr_pfn is compared against nr_pages in data-PFN units, it should advance by the same number of PFNs. The existing manual increments do not match that and therefore do not reliably track the PFN corresponding to the current addr.
As a result, pfn_offset can be computed from the wrong PFN and the code can make the head/tail decision for the wrong compound-page position.
Fix this by deriving addr_pfn directly from the current vmemmap address instead of carrying it as loop state.
---v3->v4:
- Add Cc: [email protected] (suggested by Ritesh Harjani)
- Collect Reviewed-by from Ritesh Harjani
--- arch/powerpc/mm/book3s64/radix_pgtable.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
arch/powerpc/mm/book3s64/radix_pgtable.c +1 -6
@@ -1314,7 +1314,6 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,* covering out both edges. */ unsigned long addr; -unsigned long addr_pfn = start_pfn;unsigned long next;pgd_t *pgd;p4d_t *p4d;
@@ -1335,7 +1334,6 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,if (pmd_leaf (READ_ONCE (*pmd))) {/* existing huge mapping. Skip the range */ - addr_pfn += (PMD_SIZE >>PAGE_SHIFT ); next =pmd_addr_end (addr, end);continue ; }
@@ -1348,11 +1346,11 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,* page whose VMEMMAP_RESERVE_NR pages were mapped and * this request fall in those pages. */ - addr_pfn +=1 ; next = addr +PAGE_SIZE ;continue ; }else {unsigned long nr_pages =pgmap_vmemmap_nr (pgmap); +unsigned long addr_pfn =page_to_pfn ((struct page *)addr);unsigned long pfn_offset = addr_pfn -ALIGN_DOWN (addr_pfn, nr_pages);pte_t *tail_page_pte;
@@ -1376,7 +1374,6 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,if (!pte)return -ENOMEM ; - addr_pfn +=2 ; next = addr +2 *PAGE_SIZE ;continue ; }
@@ -1392,7 +1389,6 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,return -ENOMEM ;vmemmap_verify (pte, node, addr, addr +PAGE_SIZE ); - addr_pfn +=1 ; next = addr +PAGE_SIZE ;continue ; }
@@ -1402,7 +1398,6 @@ int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,return -ENOMEM ;vmemmap_verify (pte, node, addr, addr +PAGE_SIZE ); - addr_pfn +=1 ; next = addr +PAGE_SIZE ;continue ; }
Signature · 1 line
2.54.0
PATCH v4 4/19 mm/hugetlb: Initialize gigantic bootmem hugepage struct pages earlier
In reply to Muchun SongGigantic bootmem HugeTLB pages are currently initialized from hugetlb_init(), but page_alloc_init_late() runs earlier and walks pageblocks to determine zone contiguity.
If a bootmem HugeTLB region is marked noinit, set_zone_contiguous() can observe still-uninitialized struct pages through __pageblock_pfn_to_page(). This may not trigger an immediate failure, but it can make set_zone_contiguous() compute the wrong zone contiguity state. If extra poisoned-page checks are added in this path, such as PF_POISONED_CHECK() in page_zone_id(), it can also trigger an early boot panic.
Initialize gigantic bootmem HugeTLB struct pages from page_alloc_init_late(), before zone contiguity is evaluated, so later page allocator setup only sees valid struct page state. This also makes the initialization order more natural, as struct pages should be initialized before later code inspects them.
--- include/linux/hugetlb.h | 5 +++++ mm/hugetlb.c | 5 ++--- mm/mm_init.c | 1 + mm/sparse-vmemmap.c | 4 ++-- 4 files changed, 10 insertions(+), 5 deletions(-)
include/linux/hugetlb.h +5 -0
@@ -171,6 +171,7 @@ extern int movable_gigantic_pages __read_mostly;extern int sysctl_hugetlb_shm_group__read_mostly ;extern struct list_head huge_boot_pages[MAX_NUMNODES ]; +void hugetlb_bootmem_struct_page_init (void );void hugetlb_bootmem_alloc (void );extern nodemask_t hugetlb_bootmem_nodes;void hugetlb_bootmem_set_nodes (void );
@@ -1293,6 +1294,10 @@ static inline bool hugetlbfs_pagecache_present(static inline void hugetlb_bootmem_alloc (void ) { } + +static inline void hugetlb_bootmem_struct_page_init (void ) +{ +}#endif /* CONFIG_HUGETLB_PAGE */ static inline spinlock_t *huge_pte_lock(struct hstate *h,
mm/hugetlb.c +2 -3
@@ -3353,7 +3353,7 @@ static void __init gather_bootmem_prealloc_parallel(unsigned long start,gather_bootmem_prealloc_node (nid); } -static void __init gather_bootmem_prealloc (void ) +void __init hugetlb_bootmem_struct_page_init (void ) {struct padata_mt_job job = { .thread_fn = gather_bootmem_prealloc_parallel,
@@ -3582,7 +3582,7 @@ static unsigned long __init hugetlb_pages_alloc_boot(struct hstate *h)* - For gigantic pages, this is called early in the boot process and * pages are allocated from memblock allocated or something similar. * Gigantic pages are actually added to pools later with the routine -* gather_bootmem_prealloc. +* hugetlb_bootmem_struct_page_init. * - For non-gigantic pages, this is called later in the boot process after * all of mm is up and functional. Pages are allocated from buddy and * then added to hugetlb pools.
@@ -4152,7 +4152,6 @@ static int __init hugetlb_init(void) }hugetlb_init_hstates (); -gather_bootmem_prealloc ();report_hugepages ();hugetlb_sysfs_init ();
mm/mm_init.c +1 -0
@@ -2323,6 +2323,7 @@ void __init page_alloc_init_late(void)/* Reinit limits that are based on free pages after the kernel is up */ files_maxfiles_init ();#endif +hugetlb_bootmem_struct_page_init ();/* Accounting of total+free memory is stable at this point. */ mem_init_print_info ();
mm/sparse-vmemmap.c +2 -2
@@ -342,8 +342,8 @@ static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone ** * Any initialization done here will be overwritten by memmap_init(). * -* gather_bootmem_prealloc() will take care of initialization after -* memmap_init(). +* hugetlb_bootmem_struct_page_init() will take care of initialization +* after memmap_init(). */ p =vmemmap_alloc_block_zero (PAGE_SIZE , node);
Signature · 1 line
2.54.0
PATCH v4 5/19 mm/mm_init: Simplify deferred_free_pages() migratetype init
In reply to Muchun Songdeferred_free_pages() open-codes two loops to initialize the pageblock migratetype for a range of pages.
Replace them with pageblock_migratetype_init_range() to remove the duplication and make the code clearer (Note that deferred_free_pages() may be called from atomic context).
--- mm/mm_init.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-)
mm/mm_init.c +8 -11
@@ -674,15 +674,15 @@ static inline void fixup_hashdist(void)static inline void fixup_hashdist (void ) {}#endif /* CONFIG_NUMA */ -#ifdef CONFIG_ZONE_DEVICE +#if defined(CONFIG_ZONE_DEVICE ) || defined(CONFIG_DEFERRED_STRUCT_PAGE_INIT )static __meminit void pageblock_migratetype_init_range (unsigned long pfn, -unsigned long nr_pages,int migratetype) +unsigned long nr_pages,int migratetype,bool atomic) {const unsigned long end = pfn + nr_pages;for (pfn =pageblock_align (pfn); pfn < end; pfn += pageblock_nr_pages) {init_pageblock_migratetype (pfn_to_page (pfn), migratetype, false); -if (IS_ALIGNED (pfn,PAGES_PER_SECTION )) +if (!atomic &&IS_ALIGNED (pfn,PAGES_PER_SECTION ))cond_resched (); } }
@@ -1142,7 +1142,7 @@ void __ref memmap_init_zone_device(struct zone *zone, compound_nr_pages(pfn, altmap, pgmap)); } -pageblock_migratetype_init_range (start_pfn, nr_pages,MIGRATE_MOVABLE ); +pageblock_migratetype_init_range (start_pfn, nr_pages,MIGRATE_MOVABLE , false);pr_debug ("%s initialised %lu pages in %ums\n" , __func__, nr_pages,jiffies_to_msecs (jiffies - start));
@@ -1981,12 +1981,12 @@ static void __init deferred_free_pages(unsigned long pfn,if (!nr_pages)return ; +pageblock_migratetype_init_range (pfn, nr_pages, mt, true); + page =pfn_to_page (pfn);/* Free a large naturally-aligned chunk if possible */ if (nr_pages ==MAX_ORDER_NR_PAGES &&IS_MAX_ORDER_ALIGNED (pfn)) { -for (i =0 ; i < nr_pages; i += pageblock_nr_pages) -init_pageblock_migratetype (page + i, mt, false);__free_pages_core (page,MAX_PAGE_ORDER ,MEMINIT_EARLY );return ; }
@@ -1994,11 +1994,8 @@ static void __init deferred_free_pages(unsigned long pfn,/* Accept chunks smaller than MAX_PAGE_ORDER upfront */ accept_memory (PFN_PHYS (pfn), nr_pages *PAGE_SIZE ); -for (i =0 ; i < nr_pages; i++, page++, pfn++) { -if (pageblock_aligned (pfn)) -init_pageblock_migratetype (page, mt, false); -__free_pages_core (page,0 ,MEMINIT_EARLY ); - } +for (i =0 ; i < nr_pages; i++) +__free_pages_core (page + i,0 ,MEMINIT_EARLY ); }/* Completion tracking for deferred_init_memmap() threads */
Signature · 1 line
2.54.0
PATCH v4 6/19 mm/sparse: Panic on memmap and usemap allocation failure
In reply to Muchun SongWhen vmemmap or usemap allocation fails, sparse_init_nid() currently marks the section non-present and continues. Later boot-time code can still walk PFNs in that section without checking for this partial setup, which leads to invalid accesses. subsection_map_init() can also touch an unallocated usemap.
Auditing and fixing all early PFN walkers for this case is not worth the complexity. These allocation failures are expected to be fatal anyway, and other memory models already treat them that way.
Make memmap and usemap allocation failures panic immediately instead of trying to recover and crashing later in less obvious ways. This is also consistent with how other memory model configurations handle memmap allocation failures.
--- mm/sparse.c | 44 +++++++++----------------------------------- 1 file changed, 9 insertions(+), 35 deletions(-)
mm/sparse.c +9 -35
@@ -239,15 +239,8 @@ struct page __init *__populate_section_memmap(unsigned long pfn, struct dev_pagemap *pgmap) {unsigned long size =section_map_size (); -struct page *map; -phys_addr_t addr =__pa (MAX_DMA_ADDRESS ); - map =memmap_alloc (size, size, addr, nid, false); -if (!map) -panic ("%s: Failed to allocate %lu bytes align=0x%lx nid=%d from=%pa\n" , - __func__, size,PAGE_SIZE , nid, &addr); - -return map; +return memmap_alloc (size, size,__pa (MAX_DMA_ADDRESS ), nid, false); }#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
@@ -300,17 +293,14 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin, unsigned long map_count) {unsigned long pnum; -struct page *map; -struct mem_section *ms; -if (sparse_usage_init (nid, map_count)) { -pr_err ("%s: node[%d] usemap allocation failed" , __func__, nid); - goto failed; - } +if (sparse_usage_init (nid, map_count)) +panic ("Failed to allocate usemap for node %d\n" , nid);sparse_vmemmap_init_nid_early (nid);for_each_present_section_nr (pnum_begin, pnum) { +struct mem_section *ms;unsigned long pfn =section_nr_to_pfn (pnum);if (pnum >= pnum_end)
@@ -318,34 +308,18 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin, ms =__nr_to_section (pnum);if (!preinited_vmemmap_section (ms)) { +struct page *map; + map =__populate_section_memmap (pfn,PAGES_PER_SECTION , - nid,NULL ,NULL ); -if (!map) { -pr_err ("%s: node[%d] memory map backing failed. Some memory will not be available." , - __func__, nid); - pnum_begin = pnum; -sparse_usage_fini (); - goto failed; - } + nid,NULL ,NULL ); +if (!map) +panic ("Failed to allocate memmap for section %lu\n" , pnum); memmap_boot_pages_add(DIV_ROUND_UP(PAGES_PER_SECTION * sizeof(struct page),PAGE_SIZE ));sparse_init_early_section (nid, map, pnum,0 ); } }sparse_usage_fini (); -return ; -failed: -/* -* We failed to allocate, mark all the following pnums as not present, -* except the ones already initialized earlier. -*/ -for_each_present_section_nr (pnum_begin, pnum) { -if (pnum >= pnum_end) -break ; - ms =__nr_to_section (pnum); -if (!preinited_vmemmap_section (ms)) - ms->section_mem_map =0 ; - } }/*
Signature · 1 line
2.54.0
PATCH v4 7/19 mm/sparse: Move subsection_map_init() into sparse_init()
In reply to Muchun Songsubsection_map_init() is part of sparse memory initialization, but it is currently called from free_area_init().
Move it into sparse_init() so the sparse-specific setup stays together instead of being split across the generic free_area_init() path.
--- mm/internal.h | 5 ++--- mm/mm_init.c | 10 ++-------- mm/sparse-vmemmap.c | 11 ++++++++++- mm/sparse.c | 1 + 4 files changed, 15 insertions(+), 12 deletions(-)
mm/internal.h +2 -3
@@ -994,10 +994,9 @@ static inline void sparse_init(void) {}* mm/sparse-vmemmap.c */ #ifdef CONFIG_SPARSEMEM_VMEMMAP -void sparse_init_subsection_map (unsigned long pfn,unsigned long nr_pages); +void sparse_init_subsection_map (void );#else -static inline void sparse_init_subsection_map (unsigned long pfn, -unsigned long nr_pages) +static inline void sparse_init_subsection_map (void ) { }#endif /* CONFIG_SPARSEMEM_VMEMMAP */
mm/mm_init.c +2 -8
@@ -1864,18 +1864,12 @@ static void __init free_area_init(void) (u64)zone_movable_pfn[i] << PAGE_SHIFT); } -/* -* Print out the early node map, and initialize the -* subsection-map relative to active online memory ranges to -* enable future "sub-section" extensions of the memory map. -*/ +/* Print out the early node map. */ pr_info ("Early memory node ranges\n" ); -for_each_mem_pfn_range (i,MAX_NUMNODES , &start_pfn, &end_pfn, &nid) { +for_each_mem_pfn_range (i,MAX_NUMNODES , &start_pfn, &end_pfn, &nid)pr_info (" node %3d: [mem %#018Lx-%#018Lx]\n" , nid, (u64 )start_pfn <<PAGE_SHIFT , ((u64 )end_pfn <<PAGE_SHIFT ) -1 ); -sparse_init_subsection_map (start_pfn, end_pfn - start_pfn); - }/* Initialise every node */ mminit_verify_pageflags_layout ();
mm/sparse-vmemmap.c +10 -1
@@ -601,7 +601,7 @@ static void subsection_mask_set(unsigned long *map, unsigned long pfn,bitmap_set (map, idx, end - idx +1 ); } -void __init sparse_init_subsection_map (unsigned long pfn,unsigned long nr_pages) +static void __init sparse_init_subsection_map_range (unsigned long pfn,unsigned long nr_pages) {int end_sec_nr =pfn_to_section_nr (pfn + nr_pages -1 );unsigned long nr, start_sec_nr =pfn_to_section_nr (pfn);
@@ -624,6 +624,15 @@ void __init sparse_init_subsection_map(unsigned long pfn, unsigned long nr_pages } } +void __init sparse_init_subsection_map (void ) +{ +int i, nid; +unsigned long start, end; + +for_each_mem_pfn_range (i,MAX_NUMNODES , &start, &end, &nid) +sparse_init_subsection_map_range (start, end - start); +} +#ifdef CONFIG_MEMORY_HOTPLUG /* Mark all memory sections within the pfn range as online */
mm/sparse.c +1 -0
@@ -361,5 +361,6 @@ void __init sparse_init(void) }/* cover the last node */ sparse_init_nid (nid_begin, pnum_begin, pnum_end, map_count); +sparse_init_subsection_map ();vmemmap_populate_print_last (); }
Signature · 1 line
2.54.0
Re: [PATCH v4 07/19] mm/sparse: Move subsection_map_init() into sparse_init()
Hi Muchun,
Quoted 5 lines · Muchun Song
mm/sparse: Move
subsection_map_init()intosparse_init()This commit moves
subsection_map_init()fromfree_area_init()intosparse_init()so that sparse-specific setup stays together instead of being split across the genericfree_area_init()path.
This patch introduces a new sparse_init_subsection_map() that iterates over all memblock ranges and calls sparse_init_subsection_map_range():
Review
Quoted 7 lines
(quoted diff) +7
-0+void __init sparse_init_subsection_map(void) +{ + int i, nid; + unsigned long start, end; + + for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) + sparse_init_subsection_map_range(start, end - start);
However, earlier in sparse_init(), memblocks_present() calls memory_present(), which internally caps PFN ranges at max_sparsemem_pfn via mminit_validate_memmodel_limits(). Sections beyond this cap never have ms->usage allocated.
for_each_mem_pfn_range() returns the raw, uncapped memblock ranges. If a range extends beyond max_sparsemem_pfn, then inside sparse_init_subsection_map_range():
ms = __nr_to_section(nr); subsection_mask_set(ms->usage->subsection_map, pfn, pfns);
ms->usage is NULL because sparse_init_early_section() was never called for this section, causing a NULL pointer dereference.
I was able to reproduce this on x86_64 with 4-level paging by booting with memmap=4G@0x400080000000 to place a memblock range beyond the ~64 TiB max_sparsemem_pfn limit. The kernel crashes during early boot:
node -1: [mem 0x0000400080000000-0x000040017fffffff]
------------[ cut here ]------------ WARNING: mm/sparse.c:142 at sparse_init+0x1ac/0x8a0 ... PANIC: early exception 0x0d IP 10:...sparse_init_subsection_map+0x12f/0x250 RIP: 0010:sparse_init_subsection_map+0x12f/0x250 Call Trace: sparse_init+0x69f/0x8a0 mm_core_init_early+0x12fa/0x20c0 start_kernel+0x89/0x4e0
The fix is a one-line NULL check in sparse_init_subsection_map_range():
mm/sparse-vmemmap.c +0 -0
@@ -608,6 +608,8 @@ void __init sparse_init_subsection_map(unsigned long pfn,
pfns = min(nr_pages, PAGES_PER_SECTION
- (pfn & ~PAGE_SECTION_MASK));
ms = __nr_to_section(nr);
+ if (!ms->usage)
+ continue; subsection_mask_set(ms->usage->subsection_map, pfn, pfns);
On most systems max_sparsemem_pfn is large enough that this is never hit, but on 32-bit or PAE configurations where the limit is much lower, the mismatch between for_each_mem_pfn_range() and mminit_validate_memmodel_limits() can trigger with reasonable memory sizes.
Thanks,
Xiao
Muchun Song
maintainerRe: [PATCH v4 07/19] mm/sparse: Move subsection_map_init() into sparse_init()
Quoted 5 lines · XIAO WU
Hi Muchun,
Hi,
Quoted 28 lines
Quoted 5 lines · Muchun Song
mm/sparse: Move
subsection_map_init()intosparse_init()This commit moves
subsection_map_init()fromfree_area_init()intosparse_init()so that sparse-specific setup stays together instead of being split across the genericfree_area_init()path.This patch introduces a new
sparse_init_subsection_map()that iterates over all memblock ranges and callssparse_init_subsection_map_range():Review
Quoted 7 lines
(quoted diff) +7
-0+void __init sparse_init_subsection_map(void) +{ + int i, nid; + unsigned long start, end; + + for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) + sparse_init_subsection_map_range(start, end - start);However, earlier in
sparse_init(),memblocks_present()callsmemory_present(), which internally caps PFN ranges atmax_sparsemem_pfnviamminit_validate_memmodel_limits(). Sections beyond this cap never havems->usageallocated.
for_each_mem_pfn_range()returns the raw, uncapped memblock ranges. If a range extends beyondmax_sparsemem_pfn, then insidesparse_init_subsection_map_range():
Quoted 5 lines
ms = __nr_to_section(nr); subsection_mask_set(ms->usage->subsection_map, pfn, pfns);
ms->usageis NULL becausesparse_init_early_section()was never called for this section, causing a NULL pointer dereference.
Thanks for your report. IIUC, it is a pre-existing issue.
Quoted 16 lines
I was able to reproduce this on
x86_64with 4-level paging by booting withmemmap=4G@0x400080000000to place a memblock range beyond the ~64 TiBmax_sparsemem_pfnlimit. The kernel crashes during early boot:node -1: [mem 0x0000400080000000-0x000040017fffffff]
------------[ cut here ]------------ WARNING: mm/sparse.c:142 at sparse_init+0x1ac/0x8a0 ... PANIC: early exception 0x0d IP 10:...sparse_init_subsection_map+0x12f/0x250 RIP: 0010:sparse_init_subsection_map+0x12f/0x250 Call Trace: sparse_init+0x69f/0x8a0 mm_core_init_early+0x12fa/0x20c0 start_kernel+0x89/0x4e0
Quoted 1 line
The fix is a one-line NULL check in
sparse_init_subsection_map_range():
Would you mind sending a separate fix patch since it is not introduced by this patch? I would like to review it.
Muchun,
Thanks.
Quoted 21 lines
mm/sparse-vmemmap.c +2
-0@@ -608,6 +608,8 @@ void __init sparse_init_subsection_map(unsigned long pfn, pfns =min (nr_pages,PAGES_PER_SECTION - (pfn & ~PAGE_SECTION_MASK )); ms =__nr_to_section (nr); +if (!ms->usage) +continue ;subsection_mask_set (ms->usage->subsection_map, pfn, pfns);On most systems
max_sparsemem_pfnis large enough that this is never hit, but on 32-bit or PAE configurations where the limit is much lower, the mismatch betweenfor_each_mem_pfn_range()andmminit_validate_memmodel_limits()can trigger with reasonable memory sizes.Thanks,
Xiao
PATCH v4 8/19 mm/mm_init: Defer sparse_init() until after zone initialization
In reply to Muchun Songfree_area_init() is responsible for initializing pgdat and zone state. Calling sparse_init() from there mixes in later vmemmap and struct page setup, which makes the initialization flow less clear.
Defer sparse_init(), sparse_vmemmap_init_nid_late(), and memmap_init() until after free_area_init() completes, when zone initialization is fully done. This keeps free_area_init() focused on zone setup and ensures that sparse_init() runs with the relevant zone state already available.
This is also a prerequisite for later hugetlb vmemmap changes that need zone information during early sparse vmemmap setup.
--- mm/mm_init.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-)
mm/mm_init.c +7 -5
@@ -1814,7 +1814,6 @@ static void __init free_area_init(void)bool descending;arch_zone_limits_init (max_zone_pfn); -sparse_init (); start_pfn =PHYS_PFN (memblock_start_of_DRAM ()); descending =arch_has_descending_max_zone_pfns ();
@@ -1903,11 +1902,7 @@ static void __init free_area_init(void) } } -for_each_node_state (nid,N_MEMORY ) -sparse_vmemmap_init_nid_late (nid); -calc_nr_kernel_pages (); -memmap_init ();/* disable hash distribution for systems with a single node */ fixup_hashdist ();
@@ -2679,10 +2674,17 @@ void __init __weak mem_init(void)void __init mm_core_init_early (void ) { +int nid; +hugetlb_cma_reserve ();hugetlb_bootmem_alloc ();free_area_init (); + +sparse_init (); +for_each_node_state (nid,N_MEMORY ) +sparse_vmemmap_init_nid_late (nid); +memmap_init (); }/*
Signature · 1 line
2.54.0
PATCH v4 9/19 mm/mm_init: Defer hugetlb reservation until after zone initialization
In reply to Muchun Songhugetlb_cma_reserve() and hugetlb_bootmem_alloc() currently run before free_area_init(), so HugeTLB reservation happens before zone state is initialized.
Move the reservation step after free_area_init() so the relevant zone information is available before HugeTLB reserves memory. This is needed for later hugetlb changes that validate boot-time HugeTLB reservations against zone boundaries.
--- mm/mm_init.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mm_init.c +2 -2
@@ -2676,11 +2676,11 @@ void __init mm_core_init_early(void) {int nid; +free_area_init (); +hugetlb_cma_reserve ();hugetlb_bootmem_alloc (); -free_area_init (); -sparse_init ();for_each_node_state (nid,N_MEMORY )sparse_vmemmap_init_nid_late (nid);
Signature · 1 line
2.54.0
PATCH v4 10/19 mm/mm_init: Remove set_pageblock_order() call from sparse_init()
In reply to Muchun Songfree_area_init() already sets pageblock_order before sparse_init() runs for CONFIG_HUGETLB_PAGE_SIZE_VARIABLE, so sparse_init() does not need to call set_pageblock_order() again.
With that call removed, set_pageblock_order() is only used in mm/mm_init.c. Make it static.
--- mm/internal.h | 1 - mm/mm_init.c | 4 ++-- mm/sparse.c | 3 --- 3 files changed, 2 insertions(+), 6 deletions(-)
mm/internal.h +0 -1
@@ -1435,7 +1435,6 @@ extern unsigned long __must_check vm_mmap_pgoff(struct file *, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long); -extern void set_pageblock_order (void );unsigned long reclaim_pages (struct list_head *folio_list);unsigned int reclaim_clean_pages_from_list (struct zone *zone,struct list_head *folio_list);
mm/mm_init.c +2 -2
@@ -1496,7 +1496,7 @@ static inline void setup_usemap(struct zone *zone) {}#ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */ -void __init set_pageblock_order (void ) +static void __init set_pageblock_order (void ) {unsigned int order =PAGE_BLOCK_MAX_ORDER ;
@@ -1522,7 +1522,7 @@ void __init set_pageblock_order(void)* include/linux/pageblock-flags.h for the values of pageblock_order based on * the kernel config */ -void __init set_pageblock_order (void ) +static inline void __init set_pageblock_order (void ) { }
mm/sparse.c +0 -3
@@ -343,9 +343,6 @@ void __init sparse_init(void) pnum_begin =first_present_section_nr (); nid_begin =sparse_early_nid (__nr_to_section (pnum_begin)); -/* Setup pageblock_order for HUGETLB_PAGE_SIZE_VARIABLE */ -set_pageblock_order (); -for_each_present_section_nr (pnum_begin +1 , pnum_end) {int nid =sparse_early_nid (__nr_to_section (pnum_end));
Signature · 1 line
2.54.0
PATCH v4 11/19 mm/sparse: Move sparse_vmemmap_init_nid_late() into sparse_init_nid()
In reply to Muchun Songsparse_vmemmap_init_nid_late() is still called separately from mm_core_init_early(), away from the rest of the sparse initialization path.
Now that sparse_init() runs after zone initialization, call sparse_vmemmap_init_nid_late() from sparse_init_nid() instead. This keeps both sparse_vmemmap_init_nid_early() and sparse_vmemmap_init_nid_late() in the sparse setup path.
--- mm/mm_init.c | 4 ---- mm/sparse.c | 1 + 2 files changed, 1 insertion(+), 4 deletions(-)
mm/mm_init.c +0 -4
@@ -2674,16 +2674,12 @@ void __init __weak mem_init(void)void __init mm_core_init_early (void ) { -int nid; -free_area_init ();hugetlb_cma_reserve ();hugetlb_bootmem_alloc ();sparse_init (); -for_each_node_state (nid,N_MEMORY ) -sparse_vmemmap_init_nid_late (nid);memmap_init (); }
mm/sparse.c +1 -0
@@ -320,6 +320,7 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin, } }sparse_usage_fini (); +sparse_vmemmap_init_nid_late (nid); }/*
Signature · 1 line
2.54.0
PATCH v4 12/19 mm/hugetlb_cma: Validate hugetlb CMA range by zone at reserve time
In reply to Muchun SongHugetlb CMA allocation currently has to cope with CMA areas that span multiple zones.
Validate the reserved CMA range up front in hugetlb_cma_reserve() so later hugetlb CMA allocations can assume a zone-consistent area.
Also drop the pfn_valid() check from cma_validate_zones(). mem_section is not fully initialized at this point, so the check can trigger false warnings. Keep the sanity check in cma_activate_area() instead.
--- mm/cma.c | 3 ++- mm/hugetlb_cma.c | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-)
mm/cma.c +2 -1
@@ -126,7 +126,6 @@ bool cma_validate_zones(struct cma *cma)* to be in the same zone. Simplify by forcing the entire * CMA resv range to be in the same zone. */ -WARN_ON_ONCE (!pfn_valid (base_pfn));if (pfn_range_intersects_zones (cma->nid, base_pfn, cmr->count)) {set_bit (CMA_ZONES_INVALID , &cma->flags);return false;
@@ -165,6 +164,8 @@ static void __init cma_activate_area(struct cma *cma)bitmap_set (cmr->bitmap,0 , bitmap_count); } +WARN_ON_ONCE (!pfn_valid (cmr->base_pfn)); +for (pfn = early_pfn[r]; pfn < cmr->base_pfn + cmr->count; pfn += pageblock_nr_pages)init_cma_reserved_pageblock (pfn_to_page (pfn));
mm/hugetlb_cma.c +4 -2
@@ -231,9 +231,11 @@ void __init hugetlb_cma_reserve(void) res =cma_declare_contiguous_multi (size, gigantic_page_size,HUGETLB_PAGE_ORDER , name, &hugetlb_cma[nid], nid); -if (res) { -pr_warn ("hugetlb_cma: reservation failed: err %d, node %d" , +if (res || !cma_validate_zones (hugetlb_cma[nid])) { +pr_warn ("hugetlb_cma: %s: err %d, node %d\n" , + res ?"reservation failed" :"reserved area spans zones" , res, nid); + hugetlb_cma[nid] =NULL ;continue ; }
Signature · 1 line
2.54.0
PATCH v4 13/19 mm/hugetlb: Refactor early boot gigantic hugepage allocation
In reply to Muchun SongThe early boot gigantic hugepage allocation helpers currently mix allocation with huge_bootmem_page setup, and leave part of the initialization flow in architecture code.
Refactor the interface to return the allocated huge page pointer and move the huge_bootmem_page setup into the generic hugetlb code. This makes the architecture-specific paths focus only on finding memory, while the common code handles node placement and early page metadata setup in one place.
This also lets powerpc benefit from memblock_reserved_mark_noinit(), which it did not enable before.
In addition, upcoming cross-zone validation for boot-time gigantic hugetlb reservation is common logic. With this refactoring, that logic can stay in the generic code instead of being duplicated in architecture-specific paths.
--- arch/powerpc/mm/hugetlbpage.c | 13 ++--- include/linux/hugetlb.h | 18 ++----- mm/hugetlb.c | 95 ++++++++++++++--------------------- mm/hugetlb_cma.c | 13 ++--- mm/hugetlb_cma.h | 8 ++- mm/internal.h | 9 ++++ 6 files changed, 64 insertions(+), 92 deletions(-)
arch/powerpc/mm/hugetlbpage.c +5 -8
@@ -104,17 +104,14 @@ void __init pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_p } } -static int __init pseries_alloc_bootmem_huge_page (struct hstate *hstate) +static __init void *pseries_alloc_bootmem_huge_page (struct hstate *hstate) { -struct huge_bootmem_page *m; +void *m;if (nr_gpages ==0 ) -return 0 ; +return NULL ; m =phys_to_virt (gpage_freearray[--nr_gpages]); gpage_freearray[nr_gpages] =0 ; -list_add (&m->list, &huge_boot_pages[0 ]); - m->hstate = hstate; - m->flags =0 ; -return 1 ; +return m; }bool __init hugetlb_node_alloc_supported (void )
@@ -124,7 +121,7 @@ bool __init hugetlb_node_alloc_supported(void)#endif -int __init alloc_bootmem_huge_page (struct hstate *h,int nid) +void *__init arch_alloc_bootmem_huge_page (struct hstate *h,int nid) {#ifdef CONFIG_PPC_BOOK3S_64
include/linux/hugetlb.h +5 -13
@@ -674,19 +674,11 @@ struct hstate {char name[HSTATE_NAME_LEN ]; }; -struct cma ; - -struct huge_bootmem_page { -struct list_head list; -struct hstate *hstate; -unsigned long flags; -struct cma *cma; -}; -#define HUGE_BOOTMEM_HVO 0x0001#define HUGE_BOOTMEM_ZONES_VALID 0x0002#define HUGE_BOOTMEM_CMA 0x0004 +struct huge_bootmem_page ;bool hugetlb_bootmem_page_zones_valid (int nid,struct huge_bootmem_page *m);int isolate_or_dissolve_huge_folio (struct folio *folio,struct list_head *list);
@@ -706,8 +698,8 @@ void restore_reserve_on_error(struct hstate *h, struct vm_area_struct *vma, unsigned long address, struct folio *folio);/* arch callback */ -int __init __alloc_bootmem_huge_page (struct hstate *h,int nid); -int __init alloc_bootmem_huge_page (struct hstate *h,int nid); +void *__init __alloc_bootmem_huge_page (struct hstate *h,int nid); +void *__init arch_alloc_bootmem_huge_page (struct hstate *h,int nid);bool __init hugetlb_node_alloc_supported (void );void __init hugetlb_add_hstate (unsigned order);
@@ -1138,9 +1130,9 @@ alloc_hugetlb_folio_nodemask(struct hstate *h, int preferred_nid,return NULL ; } -static inline int __alloc_bootmem_huge_page (struct hstate *h) +static inline void *__alloc_bootmem_huge_page (struct hstate *h,int nid) { -return 0 ; +return NULL ; }static inline struct hstate *hstate_file (struct file *f)
mm/hugetlb.c +37 -58
123 lines not on this page · raw message
mm/hugetlb_cma.c +5 -8
@@ -56,14 +56,13 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,return folio; } -struct huge_bootmem_page *__init -hugetlb_cma_alloc_bootmem (struct hstate *h,int *nid,bool node_exact) +void *__init hugetlb_cma_alloc_bootmem (struct hstate *h,int nid,bool node_exact) {struct cma *cma;struct huge_bootmem_page *m; -int node = *nid; +int node; - cma = hugetlb_cma[*nid]; + cma = hugetlb_cma[nid]; m =cma_reserve_early (cma,huge_page_size (h));if (!m) {if (node_exact)
@@ -71,13 +70,11 @@ hugetlb_cma_alloc_bootmem(struct hstate *h, int *nid, bool node_exact)for_each_node_mask (node, hugetlb_bootmem_nodes) { cma = hugetlb_cma[node]; -if (!cma || node == *nid) +if (!cma || node == nid)continue ; m =cma_reserve_early (cma,huge_page_size (h)); -if (m) { - *nid = node; +if (m)break ; - } } }
mm/hugetlb_cma.h +3 -5
@@ -6,8 +6,7 @@void hugetlb_cma_free_frozen_folio (struct folio *folio);struct folio *hugetlb_cma_alloc_frozen_folio (int order,gfp_t gfp_mask,int nid,nodemask_t *nodemask); -struct huge_bootmem_page *hugetlb_cma_alloc_bootmem (struct hstate *h,int *nid, -bool node_exact); +void *hugetlb_cma_alloc_bootmem (struct hstate *h,int nid,bool node_exact);bool hugetlb_cma_exclusive_alloc (void );unsigned long hugetlb_cma_total_size (void );void hugetlb_cma_validate_params (void );
@@ -23,9 +22,8 @@ static inline struct folio *hugetlb_cma_alloc_frozen_folio(int order,return NULL ; } -static inline -struct huge_bootmem_page *hugetlb_cma_alloc_bootmem (struct hstate *h,int *nid, -bool node_exact) +static inline void *hugetlb_cma_alloc_bootmem (struct hstate *h,int nid, +bool node_exact) {return NULL ; }
mm/internal.h +9 -0
@@ -23,6 +23,15 @@#include "vma.h" struct folio_batch ; +struct hstate ; +struct cma ; + +struct huge_bootmem_page { +struct list_head list; +struct hstate *hstate; +unsigned long flags; +struct cma *cma; +};/* * Maintains state across a page table move. The operation assumes both source
Signature · 1 line
2.54.0
PATCH v4 14/19 mm/hugetlb: Free cross-zone bootmem gigantic pages after allocation
In reply to Muchun SongNow that hugetlb reservation runs after zone initialization, bootmem gigantic page allocation can detect pages that span multiple zones.
Keep those cross-zone pages separate during allocation and free them after allocation completes, so later hugetlb initialization only sees zone-valid gigantic pages.
This chooses to free cross-zone gigantic pages directly instead of retrying allocation. In practice, such cross-zone cases are expected to be very rare, so adding retry logic does not seem justified at this point. Keeping the handling simple also preserves the previous behavior. If similar real-world reports show up later, retry support can be reconsidered then.
--- mm/hugetlb.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 11 deletions(-)
mm/hugetlb.c +64 -11
@@ -3060,12 +3060,15 @@ void *__init __alloc_bootmem_huge_page(struct hstate *h, int nid)static bool __init alloc_bootmem_huge_page (struct hstate *h,int nid) { +unsigned long pfn; +unsigned int nid_request = nid;struct huge_bootmem_page *m =arch_alloc_bootmem_huge_page (h, nid);if (!m)return false; - nid =early_pfn_to_nid (PHYS_PFN (__pa (m))); + pfn =PHYS_PFN (__pa (m)); + nid =early_pfn_to_nid (pfn);/* * Use the beginning of the huge page to store the huge_bootmem_page * struct (until gather_bootmem puts them into the mem_map).
@@ -3073,22 +3076,38 @@ static bool __init alloc_bootmem_huge_page(struct hstate *h, int nid)* Put them into a private list first because mem_map is not up yet. */ INIT_LIST_HEAD (&m->list); -list_add (&m->list, &huge_boot_pages[nid]); m->hstate = h;if (!hugetlb_early_cma (h)) { m->cma =NULL ; m->flags =0 ; } -/* -* Only initialize the head struct page in memmap_init_reserved_pages, -* rest of the struct pages will be initialized by the HugeTLB -* subsystem itself. -* The head struct page is used to get folio information by the HugeTLB -* subsystem like zone id and node id. -*/ -memblock_reserved_mark_noinit (__pa ((void *)m +PAGE_SIZE ), -huge_page_size (h) -PAGE_SIZE ); +/* CMA pages: zone-crossing is validated in hugetlb_cma_reserve(). */ +if (!hugetlb_early_cma (h) && +pfn_range_intersects_zones (nid, pfn,pages_per_huge_page (h))) { +/* +* If the allocated page is on a different node than requested +* (e.g., on PowerPC LPARs), put it on the requested node's list, +* because hugetlb_free_cross_zone_pages() only frees cross-zone +* pages belonging to the requested node. +*/ +if (WARN_ON_ONCE (nid_request !=NUMA_NO_NODE && nid != nid_request)) +list_add (&m->list, &huge_boot_pages[nid_request]); +else +list_add (&m->list, &huge_boot_pages[nid]); + }else { +list_add_tail (&m->list, &huge_boot_pages[nid]); + m->flags |=HUGE_BOOTMEM_ZONES_VALID ; +/* +* Only initialize the head struct page in memmap_init_reserved_pages, +* rest of the struct pages will be initialized by the HugeTLB +* subsystem itself. +* The head struct page is used to get folio information by the HugeTLB +* subsystem like zone id and node id. +*/ +memblock_reserved_mark_noinit (__pa ((void *)m +PAGE_SIZE ), +huge_page_size (h) -PAGE_SIZE ); + }return true; }
@@ -3373,6 +3392,34 @@ void __init hugetlb_bootmem_struct_page_init(void)padata_do_multithreaded (&job); } +static unsigned long __init hugetlb_free_cross_zone_pages (struct hstate *h,int nid) +{ +unsigned long freed =0 ; +struct huge_bootmem_page *m, *tmp; + +if (!hstate_is_gigantic (h)) +return freed; + +list_for_each_entry_safe (m, tmp, &huge_boot_pages[nid], list) { +if (m->flags &HUGE_BOOTMEM_ZONES_VALID ) +break ; + +list_del (&m->list); +memblock_free (m,huge_page_size (h)); + freed++; + } + +if (freed) { +char buf[32 ]; + +string_get_size (huge_page_size (h),1 ,STRING_UNITS_2 , buf,sizeof (buf)); +pr_warn ("HugeTLB: freed %lu cross-zone hugepages of size %s on node %d.\n" , + freed, buf, nid); + } + +return freed; +} +static void __init hugetlb_hstate_alloc_pages_onenode (struct hstate *h,int nid) {unsigned long i;
@@ -3403,6 +3450,8 @@ static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)cond_resched (); } + i -=hugetlb_free_cross_zone_pages (h, nid); +if (!list_empty (&folio_list))prep_and_add_allocated_folios (h, &folio_list);
@@ -3476,6 +3525,7 @@ static void __init hugetlb_pages_alloc_boot_node(unsigned long start, unsigned lstatic unsigned long __init hugetlb_gigantic_pages_alloc_boot (struct hstate *h) { +int nid;unsigned long i;for (i =0 ; i < h->max_huge_pages; ++i) {
@@ -3484,6 +3534,9 @@ static unsigned long __init hugetlb_gigantic_pages_alloc_boot(struct hstate *h)cond_resched (); } +for_each_node (nid) + i -=hugetlb_free_cross_zone_pages (h, nid); +return i; }
Signature · 1 line
2.54.0
Re: [PATCH v4 14/19] mm/hugetlb: Free cross-zone bootmem gigantic pages after allocation
Quoted 7 lines · Muchun Song
Now that hugetlb reservation runs after zone initialization, bootmem gigantic page allocation can detect pages that span multiple zones.
Keep those cross-zone pages separate during allocation and free them after allocation completes, so later hugetlb initialization only sees zone-valid gigantic pages.
Quoted 8 lines
This chooses to free cross-zone gigantic pages directly instead of retrying allocation. In practice, such cross-zone cases are expected to be very rare, so adding retry logic does not seem justified at this point. Keeping the handling simple also preserves the previous behavior. If similar real-world reports show up later, retry support can be reconsidered then.
Signed-off-by Muchun Song <[email protected]>
Quoted 3 lines
--- mm/hugetlb.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 11 deletions(-)
Signature · 2 lines
Sincerely yours, Mike.
PATCH v4 15/19 mm/hugetlb_vmemmap: Move bootmem HVO setup to early init
In reply to Muchun SongBootmem HugeTLB pages currently defer HVO setup to hugetlb_vmemmap_init_late(), because the optimization needs zone information.
Now that zone initialization is available earlier, the bootmem HVO setup can be done directly from hugetlb_vmemmap_init_early(). This lets gigantic HugeTLB pages apply HVO as soon as they are allocated.
Bootmem gigantic pages that span multiple zones are now filtered out when they are allocated, so the remaining bootmem gigantic pages seen by later hugetlb initialization are already zone-valid. As a result, hugetlb_vmemmap_init_late() no longer needs to handle bootmem HVO setup.
---v3->v4:
- Move pfn_to_zone() before hugetlb_vmemmap_init_early() instead of adding a forward declaration (suggested by Mike Rapoport)
- Collect Acked-by from Mike Rapoport and Usama Arif
--- mm/hugetlb_vmemmap.c | 93 ++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 68 deletions(-)
mm/hugetlb_vmemmap.c +25 -68
@@ -745,6 +745,20 @@ static bool vmemmap_should_optimize_bootmem_page(struct huge_bootmem_page *m)return true; } +static struct zone *pfn_to_zone (unsigned nid,unsigned long pfn) +{ +struct zone *zone; +enum zone_type zone_type; + +for (zone_type =0 ; zone_type <MAX_NR_ZONES ; zone_type++) { + zone = &NODE_DATA (nid)->node_zones[zone_type]; +if (zone_spans_pfn (zone, pfn)) +return zone; + } + +return NULL ; +} +/* * Initialize memmap section for a gigantic page, HVO-style. */
@@ -752,6 +766,7 @@ void __init hugetlb_vmemmap_init_early(int nid) {unsigned long psize, paddr, section_size;unsigned long ns, i, pnum, pfn, nr_pages; +unsigned long start, end;struct huge_bootmem_page *m =NULL ;void *map;
@@ -761,6 +776,8 @@ void __init hugetlb_vmemmap_init_early(int nid) section_size = (1UL <<PA_SECTION_SHIFT );list_for_each_entry (m, &huge_boot_pages[nid], list) { +struct zone *zone; +if (!vmemmap_should_optimize_bootmem_page (m))continue ;
@@ -769,6 +786,14 @@ void __init hugetlb_vmemmap_init_early(int nid) paddr =virt_to_phys (m); pfn =PHYS_PFN (paddr); map =pfn_to_page (pfn); + start = (unsigned long )map; + end = start +hugetlb_vmemmap_size (m->hstate); + zone =pfn_to_zone (nid, pfn); + +if (vmemmap_populate_hvo (start, end,huge_page_order (m->hstate), + zone,HUGETLB_VMEMMAP_RESERVE_SIZE )) +panic ("Failed to allocate memmap for HugeTLB page\n" ); +memmap_boot_pages_add (DIV_ROUND_UP (HUGETLB_VMEMMAP_RESERVE_SIZE ,PAGE_SIZE )); pnum =pfn_to_section_nr (pfn); ns = psize / section_size;
@@ -784,76 +809,8 @@ void __init hugetlb_vmemmap_init_early(int nid) } } -static struct zone *pfn_to_zone (unsigned nid,unsigned long pfn) -{ -struct zone *zone; -enum zone_type zone_type; - -for (zone_type =0 ; zone_type <MAX_NR_ZONES ; zone_type++) { - zone = &NODE_DATA (nid)->node_zones[zone_type]; -if (zone_spans_pfn (zone, pfn)) -return zone; - } - -return NULL ; -} -void __init hugetlb_vmemmap_init_late (int nid) { -struct huge_bootmem_page *m, *tm; -unsigned long phys, nr_pages, start, end; -unsigned long pfn, nr_mmap; -struct zone *zone =NULL ; -struct hstate *h; -void *map; - -if (!READ_ONCE (vmemmap_optimize_enabled)) -return ; - -list_for_each_entry_safe (m, tm, &huge_boot_pages[nid], list) { -if (!(m->flags &HUGE_BOOTMEM_HVO )) -continue ; - - phys =virt_to_phys (m); - h = m->hstate; - pfn =PHYS_PFN (phys); - nr_pages =pages_per_huge_page (h); - map =pfn_to_page (pfn); - start = (unsigned long )map; - end = start + nr_pages *sizeof (struct page ); - -if (!hugetlb_bootmem_page_zones_valid (nid, m)) { -/* -* Oops, the hugetlb page spans multiple zones. -* Remove it from the list, and populate it normally. -*/ -list_del (&m->list); - -vmemmap_populate (start, end, nid,NULL ); - nr_mmap = end - start; -memmap_boot_pages_add (DIV_ROUND_UP (nr_mmap,PAGE_SIZE )); - -memblock_phys_free (phys,huge_page_size (h)); -continue ; - } - -if (!zone || !zone_spans_pfn (zone, pfn)) - zone =pfn_to_zone (nid, pfn); -if (WARN_ON_ONCE (!zone)) -continue ; - -if (vmemmap_populate_hvo (start, end,huge_page_order (h), zone, -HUGETLB_VMEMMAP_RESERVE_SIZE ) <0 ) { -/* Fallback if HVO population fails */ -vmemmap_populate (start, end, nid,NULL ); - nr_mmap = end - start; - }else { - m->flags |=HUGE_BOOTMEM_ZONES_VALID ; - nr_mmap =HUGETLB_VMEMMAP_RESERVE_SIZE ; - } - -memmap_boot_pages_add (DIV_ROUND_UP (nr_mmap,PAGE_SIZE )); - } }#endif
Signature · 1 line
2.54.0
PATCH v4 16/19 mm/hugetlb: Remove obsolete bootmem cross-zone checks
In reply to Muchun SongBootmem gigantic HugeTLB pages used to be validated again during gather_bootmem_prealloc_node() and any cross-zone pages were discarded there.
That validation is no longer needed. Cross-zone bootmem gigantic pages are now detected during allocation and freed before they reach the later bootmem gathering path, so the remaining pages are already zone-valid.
Remove the obsolete cross-zone validation, invalid-page freeing, and the associated discarded-page accounting.
---v3->v4:
- Collect Acked-by from Mike Rapoport
--- include/linux/hugetlb.h | 3 -- mm/hugetlb.c | 70 ----------------------------------------- 2 files changed, 73 deletions(-)
include/linux/hugetlb.h +0 -3
@@ -678,9 +678,6 @@ struct hstate {#define HUGE_BOOTMEM_ZONES_VALID 0x0002#define HUGE_BOOTMEM_CMA 0x0004 -struct huge_bootmem_page ; -bool hugetlb_bootmem_page_zones_valid (int nid,struct huge_bootmem_page *m); -int isolate_or_dissolve_huge_folio (struct folio *folio,struct list_head *list);int replace_free_hugepage_folios (unsigned long start_pfn,unsigned long end_pfn);void wait_for_freed_hugetlb_folios (void );
mm/hugetlb.c +0 -70
101 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v4 17/19 mm/sparse-vmemmap: Remove sparse_vmemmap_init_nid_late()
In reply to Muchun Songhugetlb_vmemmap_init_late() no longer has any users, so the remaining late-init path in sparse_vmemmap_init_nid_late() is dead code.
Remove sparse_vmemmap_init_nid_late() and its declarations.
---v3->v4:
- Collect Acked-by from Mike Rapoport
--- include/linux/mmzone.h | 7 ------- mm/hugetlb_vmemmap.c | 4 ---- mm/hugetlb_vmemmap.h | 5 ----- mm/sparse-vmemmap.c | 11 ----------- mm/sparse.c | 1 - 5 files changed, 28 deletions(-)
include/linux/mmzone.h +0 -7
@@ -2156,8 +2156,6 @@ static inline int preinited_vmemmap_section(const struct mem_section *section) }void sparse_vmemmap_init_nid_early (int nid); -void sparse_vmemmap_init_nid_late (int nid); -#else static inline int preinited_vmemmap_section (const struct mem_section *section) {
@@ -2166,10 +2164,6 @@ static inline int preinited_vmemmap_section(const struct mem_section *section)static inline void sparse_vmemmap_init_nid_early (int nid) { } - -static inline void sparse_vmemmap_init_nid_late (int nid) -{ -}#endif static inline int online_section_nr (unsigned long nr)
@@ -2374,7 +2368,6 @@ static inline unsigned long next_present_section_nr(unsigned long section_nr)#else #define sparse_vmemmap_init_nid_early (_nid) do {} while (0) -#define sparse_vmemmap_init_nid_late (_nid) do {} while (0)#define pfn_in_present_section pfn_valid#endif /* CONFIG_SPARSEMEM */
mm/hugetlb_vmemmap.c +0 -4
@@ -808,10 +808,6 @@ void __init hugetlb_vmemmap_init_early(int nid) m->flags |= HUGE_BOOTMEM_HVO; } } - -void __init hugetlb_vmemmap_init_late (int nid) -{ -}#endif static const struct ctl_table hugetlb_vmemmap_sysctls[] = {
mm/hugetlb_vmemmap.h +0 -5
@@ -29,7 +29,6 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_lvoid hugetlb_vmemmap_optimize_bootmem_folios (struct hstate *h,struct list_head *folio_list);#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT void hugetlb_vmemmap_init_early (int nid); -void hugetlb_vmemmap_init_late (int nid);#endif
@@ -81,10 +80,6 @@ static inline void hugetlb_vmemmap_init_early(int nid) { } -static inline void hugetlb_vmemmap_init_late (int nid) -{ -} -static inline unsigned int hugetlb_vmemmap_optimizable_size (const struct hstate *h) {return 0 ;
mm/sparse-vmemmap.c +0 -11
@@ -579,17 +579,6 @@ void __init sparse_vmemmap_init_nid_early(int nid) {hugetlb_vmemmap_init_early (nid); } - -/* -* This is called just before the initialization of page structures -* through memmap_init. Zones are now initialized, so any work that -* needs to be done that needs zone information can be done from -* here. -*/ -void __init sparse_vmemmap_init_nid_late (int nid) -{ -hugetlb_vmemmap_init_late (nid); -}#endif static void subsection_mask_set(unsigned long *map, unsigned long pfn,
mm/sparse.c +0 -1
@@ -320,7 +320,6 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin, } }sparse_usage_fini (); -sparse_vmemmap_init_nid_late (nid); }/*
Signature · 1 line
2.54.0
PATCH v4 18/19 mm/hugetlb: Remove unused bootmem cma field
In reply to Muchun Songstruct huge_bootmem_page no longer needs to keep the CMA pointer. The bootmem path only needs to remember whether a huge page came from CMA, which is already encoded in the flags field.
Set HUGE_BOOTMEM_CMA when the page is allocated, drop the unused cma field together with the redundant assignments, and simplify the early CMA bootmem allocation fallback path now that the cma pointer no longer has to be stored in struct huge_bootmem_page.
---v3->v4:
- Clarify in the changelog that the patch also refactors the early CMA bootmem allocation path (suggested by Mike Rapoport)
--- mm/hugetlb.c | 5 +---- mm/hugetlb_cma.c | 29 +++++++++++------------------ mm/internal.h | 2 -- 3 files changed, 12 insertions(+), 24 deletions(-)
mm/hugetlb.c +1 -4
@@ -3076,10 +3076,7 @@ static bool __init alloc_bootmem_huge_page(struct hstate *h, int nid)*/ INIT_LIST_HEAD (&m->list); m->hstate = h; -if (!hugetlb_early_cma (h)) { - m->cma =NULL ; - m->flags =0 ; - } + m->flags =hugetlb_early_cma (h) ?HUGE_BOOTMEM_CMA :0 ;/* CMA pages: zone-crossing is validated in hugetlb_cma_reserve(). */ if (!hugetlb_early_cma(h) &&
mm/hugetlb_cma.c +11 -18
@@ -59,31 +59,24 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,void *__init hugetlb_cma_alloc_bootmem (struct hstate *h,int nid,bool node_exact) {struct cma *cma; -struct huge_bootmem_page *m; +void *m;int node; cma = hugetlb_cma[nid]; m =cma_reserve_early (cma,huge_page_size (h)); -if (!m) { -if (node_exact) -return NULL ; +if (m || node_exact) +return m; -for_each_node_mask (node, hugetlb_bootmem_nodes) { - cma = hugetlb_cma[node]; -if (!cma || node == nid) -continue ; - m =cma_reserve_early (cma,huge_page_size (h)); -if (m) -break ; - } - } - -if (m) { - m->flags =HUGE_BOOTMEM_CMA ; - m->cma = cma; +for_each_node_mask (node, hugetlb_bootmem_nodes) { + cma = hugetlb_cma[node]; +if (!cma || node == nid) +continue ; + m =cma_reserve_early (cma,huge_page_size (h)); +if (m) +return m; } -return m; +return NULL ; }static int __init cmdline_parse_hugetlb_cma (char *p)
mm/internal.h +0 -2
@@ -24,13 +24,11 @@struct folio_batch ;struct hstate ; -struct cma ;struct huge_bootmem_page {struct list_head list;struct hstate *hstate;unsigned long flags; -struct cma *cma; };/*
Signature · 1 line
2.54.0
PATCH v4 19/19 mm/mm_init: Fold __init_page_from_nid() into __init_deferred_page()
In reply to Muchun Song__init_page_from_nid() no longer has external users and is only used locally in mm/mm_init.c under CONFIG_DEFERRED_STRUCT_PAGE_INIT.
Fold it into its sole caller __init_deferred_page() and remove the separate helper declaration.
---v3->v4:
- Initialize pgdat at declaration line in __init_deferred_page() (suggested by Mike Rapoport)
- Collect Reviewed-by from Mike Rapoport
--- mm/internal.h | 1 - mm/mm_init.c | 42 ++++++++++++++++-------------------------- 2 files changed, 16 insertions(+), 27 deletions(-)
mm/internal.h +0 -1
@@ -1760,7 +1760,6 @@ static inline bool pte_needs_soft_dirty_wp(struct vm_area_struct *vma, pte_t ptevoid __meminit __init_single_page (struct page *page,unsigned long pfn,unsigned long zone,int nid); -void __meminit __init_page_from_nid (unsigned long pfn,int nid);/* shrinker related functions */ unsigned long shrink_slab(gfp_t gfp_mask, int nid, struct mem_cgroup *memcg,
mm/mm_init.c +16 -26
57 lines not on this page · raw message
Signature · 1 line
2.54.0
Muchun Song
maintainerQuoted 38 lines · Muchun Song
This series is split out from the earlier larger series "mm: Generalize HVO for HugeTLB and device DAX" [1]. It collects the first 19 patches of that series as a standalone set of fixes and preparatory cleanups around bootmem HugeTLB handling, sparse initialization ordering, and related vmemmap setup.
The first patches fix a few bugs found while reviewing the existing code, including incorrect bootmem HVO handling, wrong vmemmap registration arguments, a powerpc compound-vmemmap tracking bug, and too-late initialization of gigantic bootmem HugeTLB struct pages.
The rest of the series reorders early memory initialization so the relevant zone state is available before sparse and HugeTLB boot-time setup runs, then simplifies the remaining bootmem gigantic hugepage allocation path and removes code made obsolete by that rework.
At a high level:
- patches [1-4] fix boot-time and arch-specific bugs
- patches [5-12] reorder and simplify sparse/mm/hugetlb early init
- patches [13-19] refactor bootmem gigantic hugepage allocation and remove obsolete helpers and stateChanges since v3:
- patches 2-4: add Cc: [email protected] to the applicable fixes
- patch 3: collect Reviewed-by from Ritesh Harjani
- patch 15: movepfn_to_zone()beforehugetlb_vmemmap_init_early()instead of using a forward declaration
- patches 15-17: collect Acked-by from Mike Rapoport where applicable
- patch 15: collect Acked-by from Usama Arif
- patch 18: clarify that the changelog also covers the early CMA bootmem allocation fallback refactoring
- patch 19: initialize pgdat at declaration in__init_deferred_page()and collect Reviewed-by from Mike Rapoport
Quoted 1 line
Hi Andrew,
Would you mind picking these up for the mm-unstable branch? Almost all patches have received a Reviewed-by or Acked-by.
Regarding the Sashiko review comments, it reported 5 issues in total, and I've verified all of them:
- Patches 7 & 11: These are pre-existing issues and not introduced by this series.
- Patch 14: This is a false positive.
- Patches 8 & 9: These are valid observations but not a practical issue. The report mentioned:
"moving these reservations after free_area_init() artificially inflate
nr_kernel_pages and nr_all_pages, potentially causing an early boot OOM"
However, this aligns the 1GB HugeTLB allocation behavior with the existing 2MB HugeTLB allocation, which already behaves this way. So it won't introduce new risks.
Thanks for your time!
Best regards,
Muchun
Quoted 42 lines
Muchun Song (19):
mm/hugetlb: Fix boot panic withCONFIG_DEBUG_VMand HVO bootmem pagesmm/hugetlb_vmemmap: Fix__hugetlb_vmemmap_optimize_folios()powerpc/mm: Fix wrongaddr_pfntracking in compound vmemmap population mm/hugetlb: Initialize gigantic bootmem hugepage struct pages earliermm/mm_init: Simplifydeferred_free_pages()migratetype init mm/sparse: Panic on memmap and usemap allocation failure mm/sparse: Movesubsection_map_init()intosparse_init()mm/mm_init: Defersparse_init()until after zone initializationmm/mm_init: Defer hugetlb reservation until after zone initializationmm/mm_init: Removeset_pageblock_order()call fromsparse_init()mm/sparse: Movesparse_vmemmap_init_nid_late()intosparse_init_nid()mm/hugetlb_cma: Validate hugetlb CMA range by zone at reserve time mm/hugetlb: Refactor early boot gigantic hugepage allocation mm/hugetlb: Free cross-zone bootmem gigantic pages after allocationmm/hugetlb_vmemmap: Move bootmem HVO setup to early init mm/hugetlb: Remove obsolete bootmem cross-zone checksmm/sparse-vmemmap: Removesparse_vmemmap_init_nid_late()mm/hugetlb: Remove unused bootmem cma field
mm/mm_init: Fold__init_page_from_nid()into__init_deferred_page()arch/powerpc/mm/book3s64/radix_pgtable.c | 7 +- arch/powerpc/mm/hugetlbpage.c | 13 +- include/linux/hugetlb.h | 24 +-- include/linux/mmzone.h | 7 - mm/cma.c | 3 +- mm/hugetlb.c | 259 +++++++++++------------ mm/hugetlb_cma.c | 44 ++-- mm/hugetlb_cma.h | 8 +- mm/hugetlb_vmemmap.c | 120 +++-------- mm/hugetlb_vmemmap.h | 5 - mm/internal.h | 14 +- mm/mm_init.c | 86 +++----- mm/sparse-vmemmap.c | 26 ++- mm/sparse.c | 48 +---- 14 files changed, 251 insertions(+), 413 deletions(-)base-commit:
ec039126b7fac4e3af35ebccaa7c6f9b6875ba81Signature · 1 line
2.54.0
Andrew Morton
maintainerQuoted 11 lines · On Fri, 12 Jun 2026 11:58:44 +0800 Muchun Song <[email protected]> wrote:
This series is split out from the earlier larger series "mm: Generalize HVO for HugeTLB and device DAX" [1]. It collects the first 19 patches of that series as a standalone set of fixes and preparatory cleanups around bootmem HugeTLB handling, sparse initialization ordering, and related vmemmap setup.
The first patches fix a few bugs found while reviewing the existing code, including incorrect bootmem HVO handling, wrong vmemmap registration arguments, a powerpc compound-vmemmap tracking bug, and too-late initialization of gigantic bootmem HugeTLB struct pages.
Quoted 4 lines
The rest of the series reorders early memory initialization so the relevant zone state is available before sparse and HugeTLB boot-time setup runs, then simplifies the remaining bootmem gigantic hugepage allocation path and removes code made obsolete by that rework.
Thanks, I added this to mm.git's mm-new branch.
As requested, I dropped Michal's "mm/hugetlb: init tails before init_migratetype" from the mm-hotfixes-unstable branch. Michal, can you please review and perhaps test [01/19] "mm/hugetlb: fix boot panic with CONFIG_DEBUG_VM and HVO bootmem pages"
(https://lore.kernel.org/[email protected])?
Thanks.
I added cc:stable to [01/19], as CONFIG_DEBUG_VM is not uncommon.
I added this series as-sent. This means that the four cc:stable patches won't be offered to -stable maintainers for 2+ months. If you feel that some/all of them should be upstreamed earlier than this, please let me know and I'll make the necessary reorganizations.
And thanks for checking the Sashiko report. Dang thing ;)