mm: Introduce section-based vmemmap optimization for HugeTLB
Subsystems (8)
v6 cover mm: Introduce section-based vmemmap optimization for HugeTLB
This series is split out from the earlier, larger series "mm: Generalize HVO for HugeTLB and device DAX" [1]. While the parent series generalizes vmemmap optimization across HugeTLB and device DAX, this subset addresses a single, self-contained step: making the generic sparse-vmemmap code section-based optimization aware and switching HugeTLB bootmem pages to this path.
HugeTLB vmemmap optimization currently has its own early boot setup path. It pre-populates optimized vmemmap mappings before the normal sparse-vmemmap population code runs, and sparsemem carries SPARSEMEM_VMEMMAP_PREINIT only to support that special case.
That makes the HugeTLB vmemmap optimization path harder to share with other users of sparse-vmemmap optimization and leaves a fair amount of HugeTLB-specific boot-time state in the generic memory initialization flow.
This series introduces section-based vmemmap optimization support in the sparse-vmemmap code and switches HugeTLB bootmem pages over to it. Instead of having HugeTLB pre-populate optimized vmemmap mappings itself, HugeTLB now records the compound page order in the corresponding memory sections. The generic sparse-vmemmap population path can then allocate or reuse shared tail vmemmap pages based on section metadata.
The patches are organized as follows:
- patches 1-2 prepare sparsemem and vmemmap optimization metadata
- patches 3-8 teach the common sparse-vmemmap paths to use that state
- patches 9-10 switch HugeTLB bootmem optimization to the section-based path
- patches 11-17 clean up sparsemem and HugeTLB bootmem code that is no longer needed after the conversion
This is intended to be the second smaller step toward the broader HVO generalization. The device DAX conversion and the wider HVO consolidation are left for follow-up series.
v6:
- Rename the section order field and helpers to use compound page order terminology (suggested by David Hildenbrand).
- Clarify when shared tail vmemmap pages are initialized and drop an inaccurate sparse_sections_init() comment (suggested by David Hildenbrand).
- Make the local vmemmap allocation size const (suggested by David Hildenbrand).
- Collect Reviewed-by/Acked-by tags from Mike Rapoport, Qi Zheng and David Hildenbrand.
v5:
- Add comments for pageblock migratetype helper callers and shared tail vmemmap skipping (suggested by Mike Rapoport).
- Move section_nr_vmemmap_pages() earlier under its own CONFIG_MEMORY_HOTPLUG block to avoid code movement in the later visibility change (suggested by Mike Rapoport).
- Add vmemmap_alloc_pte() to keep PTE allocation handling clearer (suggested by Mike Rapoport).
- Collect Reviewed-by/Acked-by from Mike Rapoport.
v4:
- Rename pfn_vmemmap_optimizable() to vmemmap_optimizable_pfn() for consistency with vmemmap_optimizable_order().
- Move section_nr_vmemmap_pages() outside CONFIG_MEMORY_HOTPLUG to fix CONFIG_MEMORY_HOTPLUG=n builds (reported by kernel test robot).
v3:
- Replaced the !SPARSEMEM __pfn_to_section() stub with pfn_to_section_order() so callers can keep mem_section access inside sparse helpers (suggested by Mike Rapoport).
- Added vmemmap_optimizable_order() for order-based vmemmap optimization checks and used it for HugeTLB bootmem decisions.
- Fixed the pfn_to_zone() commit message to name mm/mm_init.h instead of mm/internal.h.
- Collected Acked-by tags from Mike Rapoport.
v2:
- Kept struct mem_section aligned after relaxing lookup constraints (suggested by David Laight).
- Folded section order tracking into the first user instead of keeping a standalone API-only patch (suggested by Mike Rapoport).
- Renamed the HVO order macros with a VMEMMAP_OPTIMIZATION prefix (suggested by Mike Rapoport).
- Moved pfn_to_zone() before the sparse-vmemmap optimization changes (suggested by Mike Rapoport).
- Kept vmemmap accounting and population logic in sparse-vmemmap.c (suggested by Mike Rapoport).
- Renamed the early sparsemem helper to sparse_sections_init() (suggested by Mike Rapoport).
- Fixed the !SPARSEMEM stub name so SPARSEMEM=n builds compile (reported by Sashiko).
- Guarded section_order() with CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP so it returns 0 when HVO is disabled and lets the compiler optimize the code as much as possible (suggested by Mike Rapoport).
- Collected Acked-by tags from Mike Rapoport.
[1] https://lore.kernel.org/all/[email protected]/
Muchun Song (17):
mm/sparse: relax struct mem_section size constraints mm/sparse-vmemmap: rename HVO order macros
mm/mm_init: skip initializing shared vmemmap tail pages mm/sparse-vmemmap: initialize shared tail vmemmap pages on allocation mm/sparse-vmemmap: support section-based vmemmap accounting mm/mm_init: factor out pfn_to_zone()
mm/sparse-vmemmap: move helpers ahead of future callers mm/sparse-vmemmap: support section-based vmemmap optimization mm/sparse: initialize memory sections earlier
mm/hugetlb: switch HugeTLB to section-based vmemmap optimization mm/sparse-vmemmap: remove SPARSEMEM_VMEMMAP_PREINIT support mm/sparse: inline usemap allocation into sparse_init_nid() mm/sparse: remove section_map_size()
mm/hugetlb: remove HUGE_BOOTMEM_HVO
mm/hugetlb: remove HUGE_BOOTMEM_CMA
mm/hugetlb: localize struct huge_bootmem_page
mm/hugetlb: localize HUGE_BOOTMEM_ZONES_VALID
arch/x86/Kconfig | 1 - fs/Kconfig | 1 - include/linux/hugetlb.h | 5 - include/linux/mm.h | 4 - include/linux/mmzone.h | 66 ++++-------- mm/Kconfig | 5 - mm/hugetlb.c | 65 ++++-------- mm/hugetlb_vmemmap.c | 103 +----------------- mm/hugetlb_vmemmap.h | 13 +-- mm/internal.h | 7 -- mm/mm_init.c | 69 ++++++++----- mm/mm_init.h | 1 + mm/sparse-vmemmap.c | 224 +++++++++++++++++++--------------------- mm/sparse.c | 93 +++-------------- mm/sparse.h | 88 ++++++++++++++++ scripts/gdb/linux/mm.py | 6 +- 16 files changed, 302 insertions(+), 449 deletions(-)
base-commit: 219467624544a1dd72e2022a442741e66766e39a
Signature · 1 line
2.54.0
PATCH v6 1/17 mm/sparse: relax struct mem_section size constraints
struct mem_section is currently forced to a power-of-2 size so the section-to-root lookup can use a mask instead of a modulo.
That requirement makes future extensions harder than necessary: adding a small field can require configuration-dependent padding or layout checks just to preserve the lookup scheme. Keep the lookup correct for any struct mem_section size by using a plain modulo instead.
Do not leave the layout entirely unconstrained, though. Keep struct mem_section double-word aligned so modest size changes, such as adding another word-sized field on 64-bit systems, still keep a compact and efficient layout. If future fields grow the structure beyond that sweet spot, the lookup remains correct; only the exact layout efficiency changes.
---
v6:
- Collect Acked-by from David Hildenbrand
v2:
- Align struct mem_section to power-of-2 where possible, but support non-power-of-2 lookup (suggested by David Laight)
- Collect Acked-by from Mike Rapoport
--- include/linux/mmzone.h | 11 +++-------- mm/sparse.c | 2 -- scripts/gdb/linux/mm.py | 6 ++---- 3 files changed, 5 insertions(+), 14 deletions(-)
include/linux/mmzone.h +3 -8
@@ -2027,13 +2027,9 @@ struct mem_section {* section. (see page_ext.h about this.) */ struct page_ext *page_ext; -unsigned long pad;#endif -/* -* WARNING: mem_section must be a power-of-2 in size for the -* calculation and use of SECTION_ROOT_MASK to make sense. -*/ -}; +/* Sacrifice minor padding space for efficient lookup. */ +}__aligned(2 * sizeof(unsigned long)) ;#ifdef CONFIG_SPARSEMEM_EXTREME #define SECTIONS_PER_ROOT (PAGE_SIZE / sizeof (struct mem_section))
@@ -2043,7 +2039,6 @@ struct mem_section {#define SECTION_NR_TO_ROOT (sec) ((sec) / SECTIONS_PER_ROOT)#define NR_SECTION_ROOTS DIV_ROUND_UP(NR_MEM_SECTIONS, SECTIONS_PER_ROOT) -#define SECTION_ROOT_MASK (SECTIONS_PER_ROOT - 1)#ifdef CONFIG_SPARSEMEM_EXTREME extern struct mem_section **mem_section;
@@ -2067,7 +2062,7 @@ static inline struct mem_section *__nr_to_section(unsigned long nr)if (!mem_section || !mem_section[root])return NULL ;#endif -return &mem_section[root][nr &SECTION_ROOT_MASK ]; +return &mem_section[root][nr %SECTIONS_PER_ROOT ]; }/*
mm/sparse.c +0 -2
@@ -322,8 +322,6 @@ void __init sparse_init(void)unsigned long pnum_end, pnum_begin, map_count =1 ;int nid_begin; -/* see include/linux/mmzone.h 'struct mem_section' definition */ -BUILD_BUG_ON (!is_power_of_2 (sizeof (struct mem_section )));memblocks_present ();if (compound_info_has_mask ()) {
scripts/gdb/linux/mm.py +2 -4
@@ -70,7 +70,6 @@ class x86_page_ops(): self.SECTIONS_PER_ROOT = 1 self.NR_SECTION_ROOTS = DIV_ROUND_UP(self.NR_MEM_SECTIONS, self.SECTIONS_PER_ROOT) - self.SECTION_ROOT_MASK = self.SECTIONS_PER_ROOT - 1 try: self.SECTION_HAS_MEM_MAP = 1 << int(gdb.parse_and_eval('SECTION_HAS_MEM_MAP_BIT'))
@@ -100,7 +99,7 @@ class x86_page_ops(): def __nr_to_section(self, nr): root = self.SECTION_NR_TO_ROOT(nr) mem_section = gdb.parse_and_eval("mem_section") - return mem_section[root][nr & self.SECTION_ROOT_MASK] + return mem_section[root][nr % self.SECTIONS_PER_ROOT] def pfn_to_section_nr(self, pfn): return pfn >> self.PFN_SECTION_SHIFT
@@ -249,7 +248,6 @@ class aarch64_page_ops(): self.SECTIONS_PER_ROOT = 1 self.NR_SECTION_ROOTS = DIV_ROUND_UP(self.NR_MEM_SECTIONS, self.SECTIONS_PER_ROOT) - self.SECTION_ROOT_MASK = self.SECTIONS_PER_ROOT - 1 self.SUBSECTION_SHIFT = 21 self.SEBSECTION_SIZE = 1 << self.SUBSECTION_SHIFT self.PFN_SUBSECTION_SHIFT = self.SUBSECTION_SHIFT - self.PAGE_SHIFT
@@ -304,7 +302,7 @@ class aarch64_page_ops(): def __nr_to_section(self, nr): root = self.SECTION_NR_TO_ROOT(nr) mem_section = gdb.parse_and_eval("mem_section") - return mem_section[root][nr & self.SECTION_ROOT_MASK] + return mem_section[root][nr % self.SECTIONS_PER_ROOT] def pfn_to_section_nr(self, pfn): return pfn >> self.PFN_SECTION_SHIFT
Signature · 1 line
2.54.0
PATCH v6 2/17 mm/sparse-vmemmap: rename HVO order macros
In reply to Muchun SongThe macros VMEMMAP_TAIL_MIN_ORDER and NR_VMEMMAP_TAILS describe the order range where HVO can be applied, but their names tie that range to the tail-page cache implementation.
Rename them with a VMEMMAP_OPTIMIZATION prefix and use the new names in the HVO paths. This makes the code describe the optimization requirements rather than the tail-page cache implementation detail.
No functional change intended.
---
v6:
- Collect Acked-by from Qi Zheng
v3:
- Collect Acked-by from Mike Rapoport
v2:
- Rename the macros with a VMEMMAP_OPTIMIZATION prefix (suggested by Mike Rapoport)
- Drop intermediate optimized-folio size macros (suggested by Mike Rapoport)
--- include/linux/mmzone.h | 17 +++++++++-------- mm/hugetlb.c | 4 ++-- mm/hugetlb_vmemmap.c | 2 +- mm/sparse-vmemmap.c | 4 ++-- 4 files changed, 14 insertions(+), 13 deletions(-)
include/linux/mmzone.h +9 -8
@@ -107,13 +107,14 @@ 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 struct pages covered by the retained vmemmap pages with HVO enabled. */ +#define VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES (PAGE_SIZE / sizeof(struct page)) +#define VMEMMAP_OPTIMIZATION_MIN_ORDER (ilog2(VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES) + 1) + +#define __VMEMMAP_OPTIMIZATION_NR_ORDERS \ + (MAX_FOLIO_ORDER - VMEMMAP_OPTIMIZATION_MIN_ORDER + 1) +#define VMEMMAP_OPTIMIZATION_NR_ORDERS \ + (__VMEMMAP_OPTIMIZATION_NR_ORDERS > 0 ? __VMEMMAP_OPTIMIZATION_NR_ORDERS : 0)enum migratetype {MIGRATE_UNMOVABLE ,
@@ -1158,7 +1159,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[VMEMMAP_OPTIMIZATION_NR_ORDERS ];#endif }____cacheline_internodealigned_in_smp ;
mm/hugetlb.c +2 -2
@@ -3368,7 +3368,7 @@ void __init hugetlb_bootmem_struct_page_init(void)struct zone *zone;for_each_zone (zone) { -for (int i =0 ; i <NR_VMEMMAP_TAILS ; i++) { +for (int i =0 ; i <VMEMMAP_OPTIMIZATION_NR_ORDERS ; i++) {struct page *tail, *p;unsigned int order;
@@ -3376,7 +3376,7 @@ void __init hugetlb_bootmem_struct_page_init(void)if (!tail)continue ; - order = i +VMEMMAP_TAIL_MIN_ORDER ; + order = i +VMEMMAP_OPTIMIZATION_MIN_ORDER ; p =page_to_virt (tail);/* * prep_and_add_bootmem_folios() can access pageblock
mm/hugetlb_vmemmap.c +1 -1
@@ -494,7 +494,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 -VMEMMAP_OPTIMIZATION_MIN_ORDER ;struct page *tail, *p;int node =zone_to_nid (zone);
mm/sparse-vmemmap.c +2 -2
@@ -329,12 +329,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 <VMEMMAP_OPTIMIZATION_MIN_ORDER ))return NULL ;if (WARN_ON_ONCE (order >MAX_FOLIO_ORDER ))return NULL ; - idx = order -VMEMMAP_TAIL_MIN_ORDER ; + idx = order -VMEMMAP_OPTIMIZATION_MIN_ORDER ; tail = zone->vmemmap_tails[idx];if (tail)return tail;
Signature · 1 line
2.54.0
PATCH v6 3/17 mm/mm_init: skip initializing shared vmemmap tail pages
In reply to Muchun Songmemmap_init_range() initializes every struct page in the target range. For compound pages with vmemmap optimization, the tail struct pages are backed by a shared vmemmap page.
Initializing those tail struct pages would overwrite the shared vmemmap page contents, requiring users such as HugeTLB to restore the metadata afterwards.
Track the compound page order for HVO-backed sections and use that metadata to detect struct pages that fall into the shared tail vmemmap range. Skip those shared tail pages in memmap_init_range(), then initialize pageblock migratetypes for the processed range with a helper after the per-page initialization loop.
Keep direct mem_section access inside sparse helpers. Expose pfn_to_section_compound_order() for callers that only need the order associated with a PFN. This lets memmap_init_range() skip shared tail vmemmap pages without exposing __pfn_to_section() to !SPARSEMEM builds.
This is a preparatory change for consolidating handling across users of vmemmap optimization, and it also avoids redundant initialization of shared tail vmemmap pages during early boot. That early-boot benefit appears only once HugeTLB is switched to this common handling, since HugeTLB is the early-boot user that creates those shared tail vmemmap pages.
---
v6:
- Rename the section order field and helpers for compound page order (suggested by David Hildenbrand)
- Collect Reviewed-by from Mike Rapoport
- Collect Acked-by from Qi Zheng
v5:
- Add comments for pageblock migratetype helper callers and shared tail vmemmap skipping (suggested by Mike Rapoport)
v4:
- Rename pfn_vmemmap_optimizable() to vmemmap_optimizable_pfn() for consistency with vmemmap_optimizable_order()
v3:
- Replace the !SPARSEMEM __pfn_to_section() stub with pfn_to_section_order() (suggested by Mike Rapoport)
v2:
- Fold section order tracking into the first user instead of keeping a standalone API-only patch (suggested by Mike Rapoport)
- Rename page_vmemmap_optimizable() to pfn_vmemmap_optimizable() and pass a PFN directly (suggested by Mike Rapoport)
- Initialize pageblock migratetypes from a helper after the per-page loop (suggested by Mike Rapoport)
- Use a 1G PFN chunk for cond_resched() in the pageblock helper (suggested by Mike Rapoport)
- Guard section_order() with CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP so it returns 0 when HVO is disabled and lets the compiler optimize the code as much as possible (suggested by Mike Rapoport)
- Explain why the !SPARSEMEM __pfn_to_section() stub belongs here (suggested by Mike Rapoport)
--- include/linux/mmzone.h | 8 ++++++++ mm/mm_init.c | 40 +++++++++++++++++++++++----------------- mm/sparse.h | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 17 deletions(-)
include/linux/mmzone.h +8 -0
@@ -2022,6 +2022,14 @@ struct mem_section {unsigned long section_mem_map;struct mem_section_usage *usage; +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +/* +* Normally, sections hold regular (order-0) pages. However, for +* sections with HVO enabled, this tracks the compound page order +* to enable deduplication of redundant vmemmap pages. +*/ +unsigned int compound_page_order; +#endif #ifdef CONFIG_PAGE_EXTENSION /* * If SPARSEMEM, pgdat doesn't have page_ext pointer. We use
mm/mm_init.c +23 -17
@@ -29,6 +29,7 @@#include <linux/cma.h> #include <linux/crash_dump.h> #include <linux/execmem.h> +#include <linux/sizes.h> #include <linux/vmstat.h> #include <linux/kexec_handover.h> #include <linux/hugetlb.h>
@@ -677,21 +678,19 @@ static inline void fixup_hashdist(void)static inline void fixup_hashdist (void ) {}#endif /* CONFIG_NUMA */ -#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,bool atomic) +unsigned long nr_pages,int migratetype,bool isolate,bool atomic) {const unsigned long end = pfn + nr_pages;for (pfn =pageblock_align (pfn); pfn < end; pfn += pageblock_nr_pages) {enum migratetype mt =kho_scratch_migratetype (pfn, migratetype); -init_pageblock_migratetype (pfn_to_page (pfn), mt, false); -if (!atomic &&IS_ALIGNED (pfn,PAGES_PER_SECTION )) +init_pageblock_migratetype (pfn_to_page (pfn), mt, isolate); +if (!atomic &&IS_ALIGNED (pfn,PFN_DOWN (SZ_1G )))cond_resched (); } } -#endif #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT static inline void pgdat_set_deferred_range (pg_data_t *pgdat)
@@ -886,6 +885,17 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone } } +/* +* Vmemmap-optimizable PFNs are backed by shared tail struct pages, +* which have already been initialized during vmemmap population. +*/ +if (vmemmap_optimizable_pfn (pfn)) { +const unsigned int order =pfn_to_section_compound_order (pfn); + + pfn =min (ALIGN (pfn,1UL << order), end_pfn); +continue ; + } + page =pfn_to_page (pfn);__init_single_page (page, pfn, zone, nid);if (context ==MEMINIT_HOTPLUG ) {
@@ -897,19 +907,13 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone__SetPageOffline (page); } -/* -* Usually, we want to mark the pageblock MIGRATE_MOVABLE, -* such that unmovable allocations won't be scattered all -* over the place during system boot. -*/ -if (pageblock_aligned (pfn)) { -enum migratetype mt =kho_scratch_migratetype (pfn, migratetype); - -init_pageblock_migratetype (page, mt, isolate_pageblock); +if (pageblock_aligned (pfn))cond_resched (); - } pfn++; } + +pageblock_migratetype_init_range (start_pfn, pfn - start_pfn, migratetype, + isolate_pageblock,/* atomic */ false); } static void __init memmap_init_zone_range(struct zone *zone,
@@ -1112,7 +1116,8 @@ 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 , false); +pageblock_migratetype_init_range (start_pfn, nr_pages,MIGRATE_MOVABLE , +/* isolate */ false,/* atomic */ false);pr_debug ("%s initialised %lu pages in %ums\n" , __func__, nr_pages,jiffies_to_msecs (jiffies - start));
@@ -1921,7 +1926,8 @@ static void __init deferred_free_pages(unsigned long pfn,if (!nr_pages)return ; -pageblock_migratetype_init_range (pfn, nr_pages,MIGRATE_MOVABLE , true); +pageblock_migratetype_init_range (pfn, nr_pages,MIGRATE_MOVABLE , +/* isolate */ false,/* atomic */ true); page =pfn_to_page (pfn);
mm/sparse.h +33 -0
@@ -10,6 +10,39 @@#include <linux/mmzone.h> +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +static inline unsigned int section_compound_order (const struct mem_section *section) +{ +return section->compound_page_order; +} + +static inline unsigned int pfn_to_section_compound_order (unsigned long pfn) +{ +return section_compound_order (__pfn_to_section (pfn)); +} +#else +static inline unsigned int section_compound_order (const struct mem_section *section) +{ +return 0 ; +} + +static inline unsigned int pfn_to_section_compound_order (unsigned long pfn) +{ +return 0 ; +} +#endif + +static inline bool vmemmap_optimizable_pfn (unsigned long pfn) +{ +const unsigned int order =pfn_to_section_compound_order (pfn); +const unsigned long nr_pages =1UL << order; + +if (!is_power_of_2 (sizeof (struct page ))) +return false; + +return (pfn & (nr_pages -1 )) >=VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES ; +} +/* * mm/sparse.c */
Signature · 1 line
2.54.0
PATCH v6 4/17 mm/sparse-vmemmap: initialize shared tail vmemmap pages on allocation
In reply to Muchun SongThe shared tail vmemmap page allocated in vmemmap_get_tail() used to be left uninitialized, because memmap_init_range() would later overwrite it. That forced users such as HugeTLB to defer the initialization to their own setup paths.
Now that memmap_init_range() skips shared tail vmemmap pages, initialize them immediately in vmemmap_get_tail() with init_compound_tail() instead.
This adds initialization at the point where the shared tail page is allocated. The existing HugeTLB initialization remains necessary until the compound page order is set and memmap_init_range() starts skipping shared tails. It is removed when HugeTLB switches to the section-based mechanism.
---
v6:
- Clarify the timing of shared tail page initialization removal (suggested by David Hildenbrand)
- Collect Acked-by from Mike Rapoport and Qi Zheng
--- mm/sparse-vmemmap.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-)
mm/sparse-vmemmap.c +2 -10
@@ -338,19 +338,11 @@ static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone * tail = zone->vmemmap_tails[idx];if (tail)return tail; - -/* -* Only allocate the page, but do not initialize it. -* -* Any initialization done here will be overwritten by memmap_init(). -* -* hugetlb_bootmem_struct_page_init() will take care of initialization -* after memmap_init(). -*/ - p =vmemmap_alloc_block_zero (PAGE_SIZE , node);if (!p)return NULL ; +for (int i =0 ; i <PAGE_SIZE /sizeof (struct page ); i++) +init_compound_tail (p + i,NULL , order, zone); tail =virt_to_page (p); zone->vmemmap_tails[idx] = tail;
Signature · 1 line
2.54.0
PATCH v6 5/17 mm/sparse-vmemmap: support section-based vmemmap accounting
In reply to Muchun Songsection_nr_vmemmap_pages() can account ordinary sections and DAX sections, but section-based vmemmap optimization stores the compound page 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 stored compound page order and the HVO retained page count.
---
v6:
- Use the compound page order helper for vmemmap accounting (suggested by David Hildenbrand)
- Collect Acked-by from Qi Zheng
v5:
- Collect Acked-by from Mike Rapoport
v3:
- Add vmemmap_optimizable_order() for order-based optimization checks
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 | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-)
include/linux/mmzone.h +4 -2
@@ -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 \
mm/sparse-vmemmap.c +6 -4
@@ -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_compound_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 ; }
mm/sparse.h +16 -0
@@ -43,6 +43,17 @@ static inline bool vmemmap_optimizable_pfn(unsigned long pfn)return (pfn & (nr_pages -1 )) >=VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES ; } +static inline bool vmemmap_optimizable_order (unsigned int order) +{ +if (!IS_ENABLED (CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP )) +return false; + +if (!is_power_of_2 (sizeof (struct page ))) +return false; + +return order >=VMEMMAP_OPTIMIZATION_MIN_ORDER ; +} +/* * mm/sparse.c */
@@ -86,6 +97,11 @@ static inline size_t mem_section_usage_size(void) return struct_size_t(struct mem_section_usage, pageblock_flags, BITS_TO_LONGS(SECTION_BLOCKFLAGS_BITS)); } + +static inline bool section_vmemmap_optimizable (const struct mem_section *ms) +{ +return vmemmap_optimizable_order (section_compound_order (ms)); +}#else static inline void sparse_init (void ) {}#endif /* CONFIG_SPARSEMEM */
Signature · 1 line
2.54.0
PATCH v6 6/17 mm/mm_init: factor out pfn_to_zone()
In reply to Muchun Songpfn_to_zone() in hugetlb_vmemmap.c duplicates the zone lookup logic in __init_deferred_page().
Move it to mm_init.c, declare it in mm/mm_init.h, and reuse it from __init_deferred_page() and HugeTLB early vmemmap initialization instead of open-coding the zone walk there.
---
v6:
- Collect Acked-by from Qi Zheng
v3:
- Fix the commit message to name mm/mm_init.h instead of mm/internal.h
- Collect Acked-by from Mike Rapoport
v2:
- Move this preparatory patch before the sparse-vmemmap optimization changes (suggested by Mike Rapoport)
--- mm/hugetlb_vmemmap.c | 17 ++--------------- mm/mm_init.c | 28 ++++++++++++++++++---------- mm/mm_init.h | 1 + 3 files changed, 21 insertions(+), 25 deletions(-)
mm/hugetlb_vmemmap.c +2 -15
@@ -19,6 +19,7 @@#include <asm/tlbflush.h> #include "hugetlb_vmemmap.h" #include "internal.h" +#include "mm_init.h" /** * struct vmemmap_remap_walk - walk vmemmap page table
@@ -744,20 +745,6 @@ 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. */
@@ -787,7 +774,7 @@ void __init hugetlb_vmemmap_init_early(int nid) map =pfn_to_page (pfn); start = (unsigned long )map; end = start +hugetlb_vmemmap_size (m->hstate); - zone =pfn_to_zone (nid, pfn); + zone =pfn_to_zone (pfn, nid);if (vmemmap_populate_hvo (start, end,huge_page_order (m->hstate), zone,HUGETLB_VMEMMAP_RESERVE_SIZE ))
mm/mm_init.c +18 -10
@@ -692,6 +692,20 @@ static __meminit void pageblock_migratetype_init_range(unsigned long pfn, } } +struct zone __meminit *pfn_to_zone (unsigned long pfn,int nid) +{ +pg_data_t *pgdat =NODE_DATA (nid); + +for (enum zone_type zone_type =0 ; zone_type <MAX_NR_ZONES ; zone_type++) { +struct zone *zone = &pgdat->node_zones[zone_type]; + +if (zone_spans_pfn (zone, pfn)) +return zone; + } + +return NULL ; +} +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT static inline void pgdat_set_deferred_range (pg_data_t *pgdat) {
@@ -750,20 +764,14 @@ defer_init(int nid, unsigned long pfn, unsigned long end_pfn)static void __meminit __init_deferred_page (unsigned long pfn,int nid) { -pg_data_t *pgdat =NODE_DATA (nid); -int zid; +struct zone *zone;if (early_page_initialised (pfn, nid))return ; -for (zid =0 ; zid <MAX_NR_ZONES ; zid++) { -struct zone *zone = &pgdat->node_zones[zid]; - -if (zone_spans_pfn (zone, pfn)) -break ; - } -__init_single_page (pfn_to_page (pfn), pfn, zid, nid); - + zone =pfn_to_zone (pfn, nid); +__init_single_page (pfn_to_page (pfn), pfn, + zone ?zone_idx (zone) :MAX_NR_ZONES , nid);if (pageblock_aligned (pfn)) {enum migratetype mt =kho_scratch_migratetype (pfn,MIGRATE_MOVABLE );
mm/mm_init.h +1 -0
@@ -39,6 +39,7 @@ void memmap_init_range(unsigned long size, int nid, unsigned long zone,enum meminit_context context, struct vmem_altmap *altmap, int migratetype, bool isolate_pageblock); +struct zone *pfn_to_zone (unsigned long pfn,int nid);#if definedCONFIG_COMPACTION || definedCONFIG_CMA /* Free whole pageblock and set its migration type to MIGRATE_CMA. */
Signature · 1 line
2.54.0
PATCH v6 7/17 mm/sparse-vmemmap: move helpers ahead of future callers
In reply to Muchun SongPrepare for section-based vmemmap optimization by moving helpers that follow-up changes will use. vmemmap_get_tail() will be called from the PTE population path. section_nr_vmemmap_pages() will be made visible outside the memory hotplug code and called from sparse_init_nid().
Move vmemmap_alloc_block_zero() together with vmemmap_get_tail(), since the tail helper depends on it. Move section_nr_vmemmap_pages() earlier into its own CONFIG_MEMORY_HOTPLUG block. That lets the later patch change its visibility and callers without also moving the function body.
No functional change is intended.
---
v6:
- Keep the moved accounting helper on compound page order naming (suggested by David Hildenbrand)
- Collect Acked-by from Qi Zheng
v5:
- Move section_nr_vmemmap_pages() earlier under its own CONFIG_MEMORY_HOTPLUG block to avoid code movement in the later visibility change
- Reword the subject and commit message for the helper movement
- Collect Acked-by from Mike Rapoport
v2:
- Add this new patch to move vmemmap_get_tail() before PTE population (suggested by Mike Rapoport)
--- mm/sparse-vmemmap.c | 134 +++++++++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 65 deletions(-)
mm/sparse-vmemmap.c +69 -65
@@ -148,6 +148,75 @@ void __meminit vmemmap_verify(pte_t *pte, int node, start, end - 1); } +#ifdef CONFIG_MEMORY_HOTPLUG +static int __meminit section_nr_vmemmap_pages (unsigned long pfn,unsigned long nr_pages, +struct vmem_altmap *altmap,struct dev_pagemap *pgmap) +{ +const struct mem_section *ms =__pfn_to_section (pfn); +const int order = pgmap ? pgmap->vmemmap_shift :section_compound_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) && !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_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_pages; + +return 0 ; +} +#endif + +static void *__meminit vmemmap_alloc_block_zero (unsigned long size,int node) +{ +void *p =vmemmap_alloc_block (size, node); + +if (!p) +return NULL ; +memset (p,0 , size); + +return p; +} + +#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +static __meminit struct page *vmemmap_get_tail (unsigned int order,struct zone *zone) +{ +struct page *p, *tail; +unsigned int idx; +int node =zone_to_nid (zone); + +if (WARN_ON_ONCE (order <VMEMMAP_OPTIMIZATION_MIN_ORDER )) +return NULL ; +if (WARN_ON_ONCE (order >MAX_FOLIO_ORDER )) +return NULL ; + + idx = order -VMEMMAP_OPTIMIZATION_MIN_ORDER ; + tail = zone->vmemmap_tails[idx]; +if (tail) +return tail; + p =vmemmap_alloc_block_zero (PAGE_SIZE , node); +if (!p) +return NULL ; +for (int i =0 ; i <PAGE_SIZE /sizeof (struct page ); i++) +init_compound_tail (p + i,NULL , order, zone); + + tail =virt_to_page (p); + zone->vmemmap_tails[idx] = tail; + +return tail; +} +#endif +static pte_t *__meminit vmemmap_pte_populate (pmd_t *pmd,unsigned long addr,int node,struct vmem_altmap *altmap,unsigned long ptpfn,unsigned long flags)
@@ -181,17 +250,6 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, inreturn pte; } -static void *__meminit vmemmap_alloc_block_zero (unsigned long size,int node) -{ -void *p =vmemmap_alloc_block (size, node); - -if (!p) -return NULL ; -memset (p,0 , size); - -return p; -} -static pmd_t *__meminit vmemmap_pmd_populate (pud_t *pud,unsigned long addr,int node) {pmd_t *pmd =pmd_offset (pud, addr);
@@ -323,33 +381,6 @@ void vmemmap_wrprotect_hvo(unsigned long addr, unsigned long end, }#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP -static __meminit struct page *vmemmap_get_tail (unsigned int order,struct zone *zone) -{ -struct page *p, *tail; -unsigned int idx; -int node =zone_to_nid (zone); - -if (WARN_ON_ONCE (order <VMEMMAP_OPTIMIZATION_MIN_ORDER )) -return NULL ; -if (WARN_ON_ONCE (order >MAX_FOLIO_ORDER )) -return NULL ; - - idx = order -VMEMMAP_OPTIMIZATION_MIN_ORDER ; - tail = zone->vmemmap_tails[idx]; -if (tail) -return tail; - p =vmemmap_alloc_block_zero (PAGE_SIZE , node); -if (!p) -return NULL ; -for (int i =0 ; i <PAGE_SIZE /sizeof (struct page ); i++) -init_compound_tail (p + i,NULL , order, zone); - - tail =virt_to_page (p); - zone->vmemmap_tails[idx] = tail; - -return tail; -} -int __meminit vmemmap_populate_hvo (unsigned long addr,unsigned long end,unsigned int order,struct zone *zone,unsigned long headsize)
@@ -646,33 +677,6 @@ 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 struct mem_section *ms =__pfn_to_section (pfn); -const int order = pgmap ? pgmap->vmemmap_shift :section_compound_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) && !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_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_pages; - -return 0 ; -} -static struct page *__meminit populate_section_memmap (unsigned long pfn,unsigned long nr_pages,int nid,struct vmem_altmap *altmap,struct dev_pagemap *pgmap)
Signature · 1 line
2.54.0
PATCH v6 8/17 mm/sparse-vmemmap: support section-based vmemmap optimization
In reply to Muchun SongTeach sparse-vmemmap population code to use the compound page order when deciding whether a vmemmap page can be optimized.
With this information, the common sparse-vmemmap population path can allocate or reuse shared tail vmemmap pages directly instead of relying on HugeTLB-specific handling.
This centralizes vmemmap optimization logic in the sparse-vmemmap code, based on section metadata, and prepares for sharing the same mechanism across different users of vmemmap optimization, including HugeTLB and DAX.
---
v6:
- Use the compound page order helper in the population path (suggested by David Hildenbrand)
- Collect Acked-by from Mike Rapoport and Qi Zheng
v5:
- Add vmemmap_alloc_pte() to keep PTE allocation handling clearer (suggested by Mike Rapoport)
- Move preparatory section_nr_vmemmap_pages() changes to the previous patch (suggested by Mike Rapoport)
v4:
- Move section_nr_vmemmap_pages() outside CONFIG_MEMORY_HOTPLUG to fix CONFIG_MEMORY_HOTPLUG=n builds (reported by kernel test robot)
v2:
- Keep vmemmap accounting and population logic in sparse-vmemmap.c (suggested by Mike Rapoport)
- Move vmemmap_get_tail() before its first use instead of adding only a forward declaration in the previous patch (suggested by Mike Rapoport)
- Simplify the PMD path handling for HVO-covered sections
--- mm/sparse-vmemmap.c | 46 +++++++++++++++++++++++++++++++++++++-------- mm/sparse.c | 4 ++-- mm/sparse.h | 7 +++++++ 3 files changed, 47 insertions(+), 10 deletions(-)
mm/sparse-vmemmap.c +38 -8
@@ -148,8 +148,7 @@ void __meminit vmemmap_verify(pte_t *pte, int node, start, end - 1); } -#ifdef CONFIG_MEMORY_HOTPLUG -static int __meminit section_nr_vmemmap_pages (unsigned long pfn,unsigned long nr_pages, +int __meminit section_nr_vmemmap_pages (unsigned long pfn,unsigned long nr_pages,struct vmem_altmap *altmap,struct dev_pagemap *pgmap) {const struct mem_section *ms =__pfn_to_section (pfn);
@@ -175,7 +174,6 @@ static int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nreturn 0 ; } -#endif static void *__meminit vmemmap_alloc_block_zero (unsigned long size,int node) {
@@ -215,19 +213,44 @@ static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone *return tail; } +#else +static inline struct page *vmemmap_get_tail (unsigned int order,struct zone *zone) +{ +return NULL ; +}#endif +static __meminit void *vmemmap_alloc_pte (unsigned long pfn,int node, +struct vmem_altmap *altmap) +{ +struct zone *zone; +struct page *page; +const unsigned int order =pfn_to_section_compound_order (pfn); + +if (!vmemmap_optimizable_pfn (pfn)) +return vmemmap_alloc_block_buf (PAGE_SIZE , node, altmap); + + zone =pfn_to_zone (pfn, node); + page =vmemmap_get_tail (order, zone); +if (!page) +return NULL ; + +return page_address (page); +} +static pte_t *__meminit vmemmap_pte_populate (pmd_t *pmd,unsigned long addr,int node,struct vmem_altmap *altmap,unsigned long ptpfn,unsigned long flags) {pte_t *pte =pte_offset_kernel (pmd, addr); +unsigned long pfn =page_to_pfn ((struct page *)addr); +if (pte_none (ptep_get (pte))) {pte_t entry; -void *p;if (ptpfn == (unsigned long )-1 ) { - p =vmemmap_alloc_block_buf (PAGE_SIZE , node, altmap); +void *p =vmemmap_alloc_pte (pfn, node, altmap); +if (!p)return NULL ; ptpfn =PHYS_PFN (__pa (p));
@@ -246,7 +269,8 @@ static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, in } entry =pfn_pte (ptpfn,PAGE_KERNEL );set_pte_at (&init_mm, addr, pte, entry); - } + }else if (WARN_ON_ONCE (vmemmap_optimizable_pfn (pfn))) +return NULL ;return pte; }
@@ -435,6 +459,9 @@ int __meminit vmemmap_populate_hugepages(unsigned long start, unsigned long end,pmd_t *pmd;for (addr = start; addr < end; addr = next) { +unsigned long pfn =page_to_pfn ((struct page *)addr); +const struct mem_section *ms =__pfn_to_section (pfn); + next =pmd_addr_end (addr, end); pgd =vmemmap_pgd_populate (addr, node);
@@ -450,7 +477,7 @@ int __meminit vmemmap_populate_hugepages(unsigned long start, unsigned long end,return -ENOMEM ; pmd =pmd_offset (pud, addr); -if (pmd_none (pmdp_get (pmd))) { +if (pmd_none (pmdp_get (pmd)) && !section_vmemmap_optimizable (ms)) {void *p; p =vmemmap_alloc_block_buf (PMD_SIZE , node, altmap);
@@ -468,8 +495,11 @@ int __meminit vmemmap_populate_hugepages(unsigned long start, unsigned long end,*/ return -ENOMEM ; } - }else if (vmemmap_check_pmd (pmd, node, addr, next)) + }else if (vmemmap_check_pmd (pmd, node, addr, next)) { +if (WARN_ON_ONCE (section_vmemmap_optimizable (ms))) +return -EOPNOTSUPP ;continue ; + }if (vmemmap_populate_basepages (addr, next, node, altmap))return -ENOMEM ; }
mm/sparse.c +2 -2
@@ -305,8 +305,8 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin, 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 )); +memmap_boot_pages_add (section_nr_vmemmap_pages (pfn,PAGES_PER_SECTION , +NULL ,NULL ));sparse_init_early_section (nid, map, pnum,0 ); } }
mm/sparse.h +7 -0
@@ -111,8 +111,15 @@ static inline void sparse_init(void) {}*/ #ifdef CONFIG_SPARSEMEM_VMEMMAP void sparse_init_subsection_map (void ); +int section_nr_vmemmap_pages (unsigned long pfn,unsigned long nr_pages, +struct vmem_altmap *altmap,struct dev_pagemap *pgmap);#else static inline void sparse_init_subsection_map (void ) {} +static inline int section_nr_vmemmap_pages (unsigned long pfn,unsigned long nr_pages, +struct vmem_altmap *altmap,struct dev_pagemap *pgmap) +{ +return DIV_ROUND_UP (nr_pages *sizeof (struct page ),PAGE_SIZE ); +}#endif /* CONFIG_SPARSEMEM_VMEMMAP */ #endif /* __MM_SPARSE_H */
Signature · 1 line
2.54.0
PATCH v6 9/17 mm/sparse: initialize memory sections earlier
In reply to Muchun SongUpcoming 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.
---
v6:
- Drop the inaccurate sparse_sections_init() comment (suggested by David Hildenbrand)
- Collect Acked-by from Qi Zheng
v5:
- Collect Reviewed-by from Mike Rapoport
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 | 9 +-------- mm/sparse.h | 2 ++ 3 files changed, 4 insertions(+), 8 deletions(-)
mm/mm_init.c +1 -0
@@ -2642,6 +2642,7 @@ void __init mm_core_init_early(void) {kho_memory_init_early (); +sparse_sections_init ();free_area_init ();hugetlb_cma_reserve ();
mm/sparse.c +1 -8
@@ -191,12 +191,7 @@ 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 ) +void __init sparse_sections_init (void ) {unsigned long start, end;int i, nid;
@@ -322,8 +317,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 ));
mm/sparse.h +2 -0
@@ -59,6 +59,7 @@ static inline bool vmemmap_optimizable_order(unsigned int order)*/ #ifdef CONFIG_SPARSEMEM void sparse_init (void ); +void sparse_sections_init (void );int sparse_index_init (unsigned long section_nr,int nid); static inline void sparse_init_one_section(struct mem_section *ms,
@@ -104,6 +105,7 @@ static inline bool section_vmemmap_optimizable(const struct mem_section *ms) }#else static inline void sparse_init (void ) {} +static inline void sparse_sections_init (void ) {}#endif /* CONFIG_SPARSEMEM */ /*
Signature · 1 line
2.54.0
PATCH v6 10/17 mm/hugetlb: switch HugeTLB to section-based vmemmap optimization
In reply to Muchun SongHugeTLB bootmem vmemmap optimization still carries its own early setup path, including pre-populating optimized mappings before the generic sparse-vmemmap code runs.
Now that the section-based vmemmap optimization can derive HugeTLB vmemmap deduplication from section metadata, HugeTLB only needs to mark the bootmem huge page range with the appropriate order. The generic sparse-vmemmap population path can then allocate and map the shared tail vmemmap pages without any HugeTLB-specific early population code.
Do that by recording the compound page order when a bootmem huge page is allocated and dropping the dedicated pre-HVO helpers and related special-casing.
This removes duplicate early setup logic and switches HugeTLB to the section-based vmemmap optimization path.
---
v6:
- Rename section order setters for compound page order (suggested by David Hildenbrand)
- Collect Acked-by from Qi Zheng
v3:
- Use the order-based helper for the bootmem vmemmap-optimized check
v2:
- Collect Acked-by from Mike Rapoport
--- include/linux/hugetlb.h | 1 - include/linux/mm.h | 3 -- mm/hugetlb.c | 31 +++----------- mm/hugetlb_vmemmap.c | 91 ++++------------------------------------- mm/hugetlb_vmemmap.h | 14 +++---- mm/sparse-vmemmap.c | 31 -------------- mm/sparse.h | 30 ++++++++++++++ 7 files changed, 47 insertions(+), 154 deletions(-)
include/linux/hugetlb.h +0 -1
@@ -171,7 +171,6 @@ struct address_space *hugetlb_folio_mapping_lock_write(struct folio *folio);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 );
include/linux/mm.h +0 -3
@@ -5159,9 +5159,6 @@ int vmemmap_populate_hugepages(unsigned long start, unsigned long end, int node, struct vmem_altmap *altmap);int vmemmap_populate (unsigned long start,unsigned long end,int node,struct vmem_altmap *altmap); -int vmemmap_populate_hvo (unsigned long start,unsigned long end, -unsigned int order,struct zone *zone, -unsigned long headsize);void vmemmap_wrprotect_hvo (unsigned long start,unsigned long end,int node,unsigned long headsize);void vmemmap_populate_print_last (void );
mm/hugetlb.c +5 -26
@@ -52,6 +52,7 @@#include "hugetlb_cma.h" #include "hugetlb_internal.h" #include "mm_init.h" +#include "sparse.h" #include <linux/page-isolation.h> int hugetlb_max_hstate__read_mostly ;
@@ -59,7 +60,7 @@ unsigned int default_hstate_idx;struct hstate hstates[HUGE_MAX_HSTATE ];__initdata nodemask_t hugetlb_bootmem_nodes; -__initdata struct list_head huge_boot_pages[MAX_NUMNODES ]; +static struct list_head huge_boot_pages[MAX_NUMNODES ]__initdata ;/* * Due to ordering constraints across the init code for various
@@ -3161,6 +3162,7 @@ static bool __init alloc_bootmem_huge_page(struct hstate *h, int nid) }else {list_add_tail (&m->list, &huge_boot_pages[nid]); m->flags |=HUGE_BOOTMEM_ZONES_VALID ; +hugetlb_vmemmap_optimize_bootmem_page (m);/* * Only initialize the head struct page in memmap_init_reserved_pages, * rest of the struct pages will be initialized by the HugeTLB
@@ -3321,6 +3323,8 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid)* this folio. */ folio_set_hugetlb_vmemmap_optimized (folio); +section_set_compound_order_range (folio_pfn (folio), +folio_nr_pages (folio),0 );if (hugetlb_bootmem_page_earlycma (m))folio_set_hugetlb_cma (folio);
@@ -3364,31 +3368,6 @@ void __init hugetlb_bootmem_struct_page_init(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 <VMEMMAP_OPTIMIZATION_NR_ORDERS ; i++) { -struct page *tail, *p; -unsigned int order; - - tail = zone->vmemmap_tails[i]; -if (!tail) -continue ; - - order = i +VMEMMAP_OPTIMIZATION_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 +7 -84
110 lines not on this page · raw message
mm/hugetlb_vmemmap.h +5 -9
@@ -9,8 +9,7 @@#ifndef _LINUX_HUGETLB_VMEMMAP_H#define _LINUX_HUGETLB_VMEMMAP_H#include <linux/hugetlb.h> -#include <linux/io.h> -#include <linux/memblock.h> +#include "internal.h" /* * Reserve one vmemmap page, all vmemmap addresses are mapped to it. See
@@ -27,10 +26,7 @@ long hugetlb_vmemmap_restore_folios(const struct hstate *h,void hugetlb_vmemmap_optimize_folio (const struct hstate *h,struct folio *folio);void hugetlb_vmemmap_optimize_folios (struct hstate *h,struct list_head *folio_list);void 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); -#endif - +void hugetlb_vmemmap_optimize_bootmem_page (struct huge_bootmem_page *m);static inline unsigned int hugetlb_vmemmap_size (const struct hstate *h) {
@@ -76,13 +72,13 @@ static inline void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, { } -static inline void hugetlb_vmemmap_init_early (int nid) +static inline unsigned int hugetlb_vmemmap_optimizable_size (const struct hstate *h) { +return 0 ; } -static inline unsigned int hugetlb_vmemmap_optimizable_size (const struct hstate *h) +static inline void hugetlb_vmemmap_optimize_bootmem_page (struct huge_bootmem_page *m) { -return 0 ; }#endif /* CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP */
mm/sparse-vmemmap.c +0 -31
@@ -32,8 +32,6 @@#include <asm/dma.h> #include <asm/tlbflush.h> -#include "hugetlb_vmemmap.h" -/* * Flags for vmemmap_populate_range and friends. */
@@ -404,34 +402,6 @@ void vmemmap_wrprotect_hvo(unsigned long addr, unsigned long end, } } -#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP -int __meminit vmemmap_populate_hvo (unsigned long addr,unsigned long end, -unsigned int order,struct zone *zone, -unsigned long headsize) -{ -unsigned long maddr; -struct page *tail; -pte_t *pte; -int node =zone_to_nid (zone); - - tail =vmemmap_get_tail (order, zone); -if (!tail) -return -ENOMEM ; - -for (maddr = addr; maddr < addr + headsize; maddr +=PAGE_SIZE ) { - pte =vmemmap_populate_address (maddr, node,NULL ,-1 ,0 ); -if (!pte) -return -ENOMEM ; - } - -/* -* Reuse the last page struct page mapped above for the rest. -*/ -return vmemmap_populate_range (maddr, end, node,NULL , -page_to_pfn (tail),0 ); -} -#endif -void __weak __meminit vmemmap_set_pmd (pmd_t *pmd,void *p,int node,unsigned long addr,unsigned long next) {
@@ -634,7 +604,6 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,*/ void __init sparse_vmemmap_init_nid_early (int nid) { -hugetlb_vmemmap_init_early (nid); }#endif
mm/sparse.h +30 -0
@@ -16,6 +16,26 @@ static inline unsigned int section_compound_order(const struct mem_section *sectreturn section->compound_page_order; } +static inline void section_set_compound_order (struct mem_section *section, +unsigned int order) +{ +VM_WARN_ON (section_compound_order (section) && order && +section_compound_order (section) != order); + section->compound_page_order = order; +} + +static inline void section_set_compound_order_range (unsigned long pfn, +unsigned long nr_pages,unsigned int order) +{ +unsigned long section_nr =pfn_to_section_nr (pfn); + +if (!IS_ALIGNED (pfn | nr_pages,PAGES_PER_SECTION )) +return ; + +for (unsigned long i =0 ; i < nr_pages /PAGES_PER_SECTION ; i++) +section_set_compound_order (__nr_to_section (section_nr + i), order); +} +static inline unsigned int pfn_to_section_compound_order (unsigned long pfn) {return section_compound_order (__pfn_to_section (pfn));
@@ -26,6 +46,16 @@ static inline unsigned int section_compound_order(const struct mem_section *sectreturn 0 ; } +static inline void section_set_compound_order (struct mem_section *section, +unsigned int order) +{ +} + +static inline void section_set_compound_order_range (unsigned long pfn, +unsigned long nr_pages,unsigned int order) +{ +} +static inline unsigned int pfn_to_section_compound_order (unsigned long pfn) {return 0 ;
Signature · 1 line
2.54.0
PATCH v6 11/17 mm/sparse-vmemmap: remove SPARSEMEM_VMEMMAP_PREINIT support
In reply to Muchun SongSPARSEMEM_VMEMMAP_PREINIT existed only to support HugeTLB's early vmemmap optimization setup.
Now that HugeTLB bootmem vmemmap optimization uses the common section-based sparse-vmemmap path, the sparse initialization code no longer needs a separate pre-initialization mechanism for vmemmap population.
Remove the related Kconfig symbols, section flag, and empty early hook, so present sections always go through the normal sparse setup path.
---
v6:
- Collect Acked-by from Qi Zheng and David Hildenbrand
v2:
- Collect Acked-by from Mike Rapoport
--- arch/x86/Kconfig | 1 - fs/Kconfig | 1 - include/linux/mmzone.h | 25 ------------------------- mm/Kconfig | 5 ----- mm/sparse-vmemmap.c | 13 ------------- mm/sparse.c | 23 ++++++++--------------- 6 files changed, 8 insertions(+), 60 deletions(-)
arch/x86/Kconfig +0 -1
@@ -150,7 +150,6 @@ config X86 select ARCH_WANT_LD_ORPHAN_WARN select ARCH_WANT_OPTIMIZE_DAX_VMEMMAP if X86_64 select ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP if X86_64 - select ARCH_WANT_HUGETLB_VMEMMAP_PREINIT if X86_64 select ARCH_WANTS_THP_SWAP if X86_64 select ARCH_HAS_PARANOID_L1D_FLUSH select ARCH_WANT_IRQS_OFF_ACTIVATE_MM
fs/Kconfig +0 -1
@@ -278,7 +278,6 @@ config HUGETLB_PAGE_OPTIMIZE_VMEMMAP def_bool HUGETLB_PAGE depends on ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP depends on SPARSEMEM_VMEMMAP - select SPARSEMEM_VMEMMAP_PREINIT if ARCH_WANT_HUGETLB_VMEMMAP_PREINIT config HUGETLB_PMD_PAGE_TABLE_SHARING def_bool HUGETLB_PAGE
include/linux/mmzone.h +0 -25
@@ -2095,9 +2095,6 @@ enum { SECTION_IS_EARLY_BIT,#ifdef CONFIG_ZONE_DEVICE SECTION_TAINT_ZONE_DEVICE_BIT , -#endif -#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT -SECTION_IS_VMEMMAP_PREINIT_BIT , #endif SECTION_MAP_LAST_BIT, };
@@ -2109,9 +2106,6 @@ enum {#ifdef CONFIG_ZONE_DEVICE #define SECTION_TAINT_ZONE_DEVICE BIT(SECTION_TAINT_ZONE_DEVICE_BIT)#endif -#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT -#define SECTION_IS_VMEMMAP_PREINIT BIT(SECTION_IS_VMEMMAP_PREINIT_BIT) -#endif #define SECTION_MAP_MASK (~(BIT(SECTION_MAP_LAST_BIT) - 1))#define SECTION_NID_SHIFT SECTION_MAP_LAST_BIT
@@ -2166,24 +2160,6 @@ static inline int online_device_section(const struct mem_section *section) }#endif -#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT -static inline int preinited_vmemmap_section (const struct mem_section *section) -{ -return (section && - (section->section_mem_map &SECTION_IS_VMEMMAP_PREINIT )); -} - -void sparse_vmemmap_init_nid_early (int nid); -#else -static inline int preinited_vmemmap_section (const struct mem_section *section) -{ -return 0 ; -} -static inline void sparse_vmemmap_init_nid_early (int nid) -{ -} -#endif -static inline int online_section_nr (unsigned long nr) {return online_section (__nr_to_section (nr));
@@ -2385,7 +2361,6 @@ static inline unsigned long next_present_section_nr(unsigned long section_nr)#endif #else -#define sparse_vmemmap_init_nid_early (_nid) do {} while (0)#define pfn_in_present_section pfn_valid#endif /* CONFIG_SPARSEMEM */
mm/Kconfig +0 -5
@@ -461,8 +461,6 @@ config SPARSEMEM_VMEMMAP pfn_to_page and page_to_pfn operations. This is the most efficient option when sufficient kernel resources are available. -config SPARSEMEM_VMEMMAP_PREINIT - bool # # Select this config option from the architecture Kconfig, if it is preferred # to enable the feature of HugeTLB/dev_dax vmemmap optimization.
@@ -473,9 +471,6 @@ config ARCH_WANT_OPTIMIZE_DAX_VMEMMAP config ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP bool -config ARCH_WANT_HUGETLB_VMEMMAP_PREINIT - bool - config HAVE_MEMBLOCK_PHYS_MAP bool
mm/sparse-vmemmap.c +0 -13
@@ -594,19 +594,6 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,return pfn_to_page (pfn); } -#ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT -/* -* This is called just before initializing sections for a NUMA node. -* Any special initialization that needs to be done before the -* generic initialization can be done from here. Sections that -* are initialized in hooks called from here will be skipped by -* the generic initialization. -*/ -void __init sparse_vmemmap_init_nid_early (int nid) -{ -} -#endif -static void subsection_mask_set (unsigned long *map,unsigned long pfn,unsigned long nr_pages) {
mm/sparse.c +8 -15
@@ -283,27 +283,20 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin,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); +struct page *map;if (pnum >= pnum_end)break ; - 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) -panic ("Failed to allocate memmap for section %lu\n" , pnum); -memmap_boot_pages_add (section_nr_vmemmap_pages (pfn,PAGES_PER_SECTION , -NULL ,NULL )); -sparse_init_early_section (nid, map, pnum,0 ); - } + map =__populate_section_memmap (pfn,PAGES_PER_SECTION , + nid,NULL ,NULL ); +if (!map) +panic ("Failed to allocate memmap for section %lu\n" , pnum); +memmap_boot_pages_add (section_nr_vmemmap_pages (pfn,PAGES_PER_SECTION , +NULL ,NULL )); +sparse_init_early_section (nid, map, pnum,0 ); }sparse_usage_fini (); }
Signature · 1 line
2.54.0
PATCH v6 12/17 mm/sparse: inline usemap allocation into sparse_init_nid()
In reply to Muchun SongAfter removing SPARSEMEM_VMEMMAP_PREINIT, sparse_init_nid() no longer needs the transient sparse_usagebuf state and its helper wrappers.
Allocate the usemap buffer directly in sparse_init_nid(), pass it to sparse_init_one_section(), and drop sparse_usage_init(), sparse_usage_fini(), and sparse_init_early_section().
---
v6:
- Collect Acked-by from David Hildenbrand
v2:
- Collect Acked-by from Mike Rapoport
--- include/linux/mmzone.h | 3 --- mm/sparse.c | 46 +++++++----------------------------------- 2 files changed, 7 insertions(+), 42 deletions(-)
include/linux/mmzone.h +0 -3
@@ -2223,9 +2223,6 @@ static inline bool pfn_section_first_valid(struct mem_section *ms, unsigned long }#endif -void sparse_init_early_section (int nid,struct page *map,unsigned long pnum, -unsigned long flags); -#ifndef CONFIG_HAVE_ARCH_PFN_VALID /** * pfn_valid - check if there is a valid memory map entry for a PFN
mm/sparse.c +7 -39
66 lines not on this page · raw message
Signature · 1 line
2.54.0
PATCH v6 13/17 mm/sparse: remove section_map_size()
In reply to Muchun Songsection_map_size() no longer provides any shared logic.
After the sparse-vmemmap changes, its only remaining user is the !CONFIG_SPARSEMEM_VMEMMAP path in __populate_section_memmap(), which can compute the size inline with PAGE_ALIGN(sizeof(struct page) * PAGES_PER_SECTION).
Remove section_map_size() and inline the remaining calculation.
---
v6:
- Make the local vmemmap allocation size const
(suggested by David Hildenbrand)
- Collect Acked-by from David Hildenbrand
v2:
- Collect Acked-by from Mike Rapoport
--- include/linux/mm.h | 1 - mm/sparse.c | 15 ++------------- 2 files changed, 2 insertions(+), 14 deletions(-)
include/linux/mm.h +0 -1
@@ -5140,7 +5140,6 @@ static inline void print_vma_addr(char *prefix, unsigned long rip) }#endif -unsigned long section_map_size (void );struct page *__populate_section_memmap (unsigned long pfn,unsigned long nr_pages,int nid,struct vmem_altmap *altmap,struct dev_pagemap *pgmap);
mm/sparse.c +2 -13
@@ -208,23 +208,12 @@ void __init sparse_sections_init(void)memory_present (nid, start, end); } -#ifdef CONFIG_SPARSEMEM_VMEMMAP -unsigned long __init section_map_size (void ) -{ -return ALIGN (sizeof (struct page ) *PAGES_PER_SECTION ,PMD_SIZE ); -} - -#else -unsigned long __init section_map_size (void ) -{ -return PAGE_ALIGN (sizeof (struct page ) *PAGES_PER_SECTION ); -} - +#ifndef CONFIG_SPARSEMEM_VMEMMAP struct page __init *__populate_section_memmap (unsigned long pfn,unsigned long nr_pages,int nid,struct vmem_altmap *altmap,struct dev_pagemap *pgmap) { -unsigned long size =section_map_size (); +const unsigned long size =PAGE_ALIGN (sizeof (struct page ) *PAGES_PER_SECTION );return memmap_alloc (size, size,__pa (MAX_DMA_ADDRESS ), nid, false); }
Signature · 1 line
2.54.0
PATCH v6 14/17 mm/hugetlb: remove HUGE_BOOTMEM_HVO
In reply to Muchun SongThe HUGE_BOOTMEM_HVO flag tracked whether a bootmem huge page had already gone through the old early vmemmap optimization path.
Now that HugeTLB uses section-based vmemmap optimization, that state is already reflected in the compound page order stored in section metadata.
Remove HUGE_BOOTMEM_HVO and its helper, and use the section state directly when deciding whether to mark a folio as vmemmap-optimized.
---
v6:
- Use the compound page order helper names
(suggested by David Hildenbrand)
- Collect Acked-by from Qi Zheng
v3:
- Use the order-based helper for the bootmem vmemmap-optimized check
v2:
- Collect Acked-by from Mike Rapoport
--- include/linux/hugetlb.h | 5 ++--- mm/hugetlb.c | 16 +++------------- mm/hugetlb_vmemmap.c | 2 -- 3 files changed, 5 insertions(+), 18 deletions(-)
include/linux/hugetlb.h +2 -3
@@ -675,9 +675,8 @@ struct hstate {char name[HSTATE_NAME_LEN ]; }; -#define HUGE_BOOTMEM_HVO 0x0001 -#define HUGE_BOOTMEM_ZONES_VALID 0x0002 -#define HUGE_BOOTMEM_CMA 0x0004 +#define HUGE_BOOTMEM_ZONES_VALID BIT(0) +#define HUGE_BOOTMEM_CMA BIT(1)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);
mm/hugetlb.c +3 -13
@@ -3220,11 +3220,6 @@ static void __init hugetlb_folio_init_vmemmap(struct folio *folio,prep_compound_head (&folio->page,huge_page_order (h)); } -static bool __init hugetlb_bootmem_page_prehvo (struct huge_bootmem_page *m) -{ -return m->flags &HUGE_BOOTMEM_HVO ; -} -static bool __init hugetlb_bootmem_page_earlycma (struct huge_bootmem_page *m) {return m->flags &HUGE_BOOTMEM_CMA ;
@@ -3299,6 +3294,7 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid)list_for_each_entry_safe (m, tm, &huge_boot_pages[nid], list) {struct page *page =virt_to_page (m);struct folio *folio = (void *)page; +const unsigned long pfn =folio_pfn (folio); h = m->hstate;/*
@@ -3316,15 +3312,9 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid) HUGETLB_VMEMMAP_RESERVE_PAGES);init_new_hugetlb_folio (folio); -if (hugetlb_bootmem_page_prehvo (m)) -/* -* If pre-HVO was done, just set the -* flag, the HVO code will then skip -* this folio. -*/ +if (vmemmap_optimizable_order (pfn_to_section_compound_order (pfn)))folio_set_hugetlb_vmemmap_optimized (folio); -section_set_compound_order_range (folio_pfn (folio), -folio_nr_pages (folio),0 ); +section_set_compound_order_range (pfn,folio_nr_pages (folio),0 );if (hugetlb_bootmem_page_earlycma (m))folio_set_hugetlb_cma (folio);
mm/hugetlb_vmemmap.c +0 -2
@@ -715,8 +715,6 @@ void __init hugetlb_vmemmap_optimize_bootmem_page(struct huge_bootmem_page *m)section_set_compound_order_range (pfn,pages_per_huge_page (h),huge_page_order (h)); -if (vmemmap_optimizable_order (pfn_to_section_compound_order (pfn))) - m->flags |=HUGE_BOOTMEM_HVO ; }static const struct ctl_table hugetlb_vmemmap_sysctls[] = {
Signature · 1 line
2.54.0
PATCH v6 15/17 mm/hugetlb: remove HUGE_BOOTMEM_CMA
In reply to Muchun SongTrack early CMA hugetlb pages from the hstate instead of storing a redundant bootmem flag. This removes the unused helper and keeps the bootmem metadata minimal.
---
v6:
- Collect Acked-by from Qi Zheng
v2:
- Collect Acked-by from Mike Rapoport
--- include/linux/hugetlb.h | 1 - mm/hugetlb.c | 14 ++++---------- 2 files changed, 4 insertions(+), 11 deletions(-)
include/linux/hugetlb.h +0 -1
@@ -676,7 +676,6 @@ struct hstate { };#define HUGE_BOOTMEM_ZONES_VALID BIT(0) -#define HUGE_BOOTMEM_CMA BIT(1)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);
mm/hugetlb.c +4 -10
@@ -3144,7 +3144,7 @@ static bool __init alloc_bootmem_huge_page(struct hstate *h, int nid)*/ INIT_LIST_HEAD (&m->list); m->hstate = h; - m->flags =hugetlb_early_cma (h) ?HUGE_BOOTMEM_CMA :0 ; + m->flags =0 ;/* CMA pages: zone-crossing is validated in hugetlb_cma_reserve(). */ if (!hugetlb_early_cma(h) &&
@@ -3220,11 +3220,6 @@ static void __init hugetlb_folio_init_vmemmap(struct folio *folio,prep_compound_head (&folio->page,huge_page_order (h)); } -static bool __init hugetlb_bootmem_page_earlycma (struct huge_bootmem_page *m) -{ -return m->flags &HUGE_BOOTMEM_CMA ; -} -/* * memblock-allocated pageblocks might not have the migrate type set * if marked with the 'noinit' flag. Set it to the default (MIGRATE_MOVABLE)
@@ -3316,9 +3311,6 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid)folio_set_hugetlb_vmemmap_optimized (folio);section_set_compound_order_range (pfn,folio_nr_pages (folio),0 ); -if (hugetlb_bootmem_page_earlycma (m)) -folio_set_hugetlb_cma (folio); -list_add (&folio->lru, &folio_list);/*
@@ -3329,7 +3321,9 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid)* For CMA pages, this is done in init_cma_pageblock * (via hugetlb_bootmem_init_migratetype), so skip it here. */ -if (!folio_test_hugetlb_cma (folio)) +if (hugetlb_early_cma (h)) +folio_set_hugetlb_cma (folio); +else adjust_managed_page_count (page,pages_per_huge_page (h));cond_resched (); }
Signature · 1 line
2.54.0
PATCH v6 16/17 mm/hugetlb: localize struct huge_bootmem_page
In reply to Muchun Songstruct huge_bootmem_page is only used by hugetlb boot-time allocation code, but its definition currently lives in mm/internal.h because hugetlb_vmemmap_optimize_bootmem_page() takes it as an argument. This exposes a hugetlb-specific internal type more broadly than needed.
Change hugetlb_vmemmap_optimize_bootmem_page() to take the information it actually needs. With that interface, mm/hugetlb_vmemmap.h no longer needs to include mm/internal.h, and struct huge_bootmem_page can move into mm/hugetlb.c.
---
v6:
- Use the compound page order range setter (suggested by David Hildenbrand)
- Collect Acked-by from Qi Zheng
v2:
- Pass PFN and order to hugetlb_vmemmap_optimize_bootmem_page() instead of struct hstate and bootmem metadata to simplify the code further
- Collect Acked-by from Mike Rapoport
--- mm/hugetlb.c | 8 +++++++- mm/hugetlb_vmemmap.c | 9 +++------ mm/hugetlb_vmemmap.h | 5 ++--- mm/internal.h | 7 ------- 4 files changed, 12 insertions(+), 17 deletions(-)
mm/hugetlb.c +7 -1
@@ -55,6 +55,12 @@#include "sparse.h" #include <linux/page-isolation.h> +struct huge_bootmem_page { +struct list_head list; +struct hstate *hstate; +unsigned long flags; +}; +int hugetlb_max_hstate__read_mostly ;unsigned int default_hstate_idx;struct hstate hstates[HUGE_MAX_HSTATE ];
@@ -3162,7 +3168,7 @@ static bool __init alloc_bootmem_huge_page(struct hstate *h, int nid) }else {list_add_tail (&m->list, &huge_boot_pages[nid]); m->flags |=HUGE_BOOTMEM_ZONES_VALID ; -hugetlb_vmemmap_optimize_bootmem_page (m); +hugetlb_vmemmap_optimize_bootmem_page (pfn,huge_page_order (h));/* * Only initialize the head struct page in memmap_init_reserved_pages, * rest of the struct pages will be initialized by the HugeTLB
mm/hugetlb_vmemmap.c +3 -6
@@ -19,6 +19,7 @@#include <asm/tlbflush.h> #include "hugetlb_vmemmap.h" #include "sparse.h" +#include "internal.h" /** * struct vmemmap_remap_walk - walk vmemmap page table
@@ -705,16 +706,12 @@ void hugetlb_vmemmap_optimize_bootmem_folios(struct hstate *h, struct list_head__hugetlb_vmemmap_optimize_folios (h, folio_list, true); } -void __init hugetlb_vmemmap_optimize_bootmem_page (struct huge_bootmem_page *m) +void __init hugetlb_vmemmap_optimize_bootmem_page (unsigned long pfn,unsigned int order) { -struct hstate *h = m->hstate; -unsigned long pfn =PHYS_PFN (__pa (m)); -if (!READ_ONCE (vmemmap_optimize_enabled))return ; -section_set_compound_order_range (pfn,pages_per_huge_page (h), -huge_page_order (h)); +section_set_compound_order_range (pfn,1UL << order, order); }static const struct ctl_table hugetlb_vmemmap_sysctls[] = {
mm/hugetlb_vmemmap.h +2 -3
@@ -9,7 +9,6 @@#ifndef _LINUX_HUGETLB_VMEMMAP_H#define _LINUX_HUGETLB_VMEMMAP_H#include <linux/hugetlb.h> -#include "internal.h" /* * Reserve one vmemmap page, all vmemmap addresses are mapped to it. See
@@ -26,7 +25,7 @@ long hugetlb_vmemmap_restore_folios(const struct hstate *h,void hugetlb_vmemmap_optimize_folio (const struct hstate *h,struct folio *folio);void hugetlb_vmemmap_optimize_folios (struct hstate *h,struct list_head *folio_list);void hugetlb_vmemmap_optimize_bootmem_folios (struct hstate *h,struct list_head *folio_list); -void hugetlb_vmemmap_optimize_bootmem_page (struct huge_bootmem_page *m); +void hugetlb_vmemmap_optimize_bootmem_page (unsigned long pfn,unsigned int order);static inline unsigned int hugetlb_vmemmap_size (const struct hstate *h) {
@@ -77,7 +76,7 @@ static inline unsigned int hugetlb_vmemmap_optimizable_size(const struct hstate return 0; } -static inline void hugetlb_vmemmap_optimize_bootmem_page (struct huge_bootmem_page *m) +static inline void hugetlb_vmemmap_optimize_bootmem_page (unsigned long pfn,unsigned int order) { }#endif /* CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP */
mm/internal.h +0 -7
@@ -23,13 +23,6 @@#include "vma.h" struct folio_batch ; -struct hstate ; - -struct huge_bootmem_page { -struct list_head list; -struct hstate *hstate; -unsigned long flags; -};/* mm/workingset.c */ bool workingset_test_recent(void *shadow, bool file, bool *workingset,
Signature · 1 line
2.54.0
PATCH v6 17/17 mm/hugetlb: localize HUGE_BOOTMEM_ZONES_VALID
In reply to Muchun SongHUGE_BOOTMEM_ZONES_VALID is only used by the huge_bootmem_page flag handling in mm/hugetlb.c. Keep the definition next to that private data structure instead of exposing it through the public hugetlb header.
No functional change is intended.
---
v6:
- Collect Acked-by from Qi Zheng
v2:
- Collect Acked-by from Mike Rapoport
--- include/linux/hugetlb.h | 2 -- mm/hugetlb.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/hugetlb.h +0 -2
@@ -675,8 +675,6 @@ struct hstate {char name[HSTATE_NAME_LEN ]; }; -#define HUGE_BOOTMEM_ZONES_VALID BIT(0) -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 +2 -0
@@ -55,6 +55,8 @@#include "sparse.h" #include <linux/page-isolation.h> +#define HUGE_BOOTMEM_ZONES_VALID BIT(0) +struct huge_bootmem_page {struct list_head list;struct hstate *hstate;
Signature · 1 line
2.54.0
Andrew Morton
maintainerQuoted 17 lines · On Thu, 10 Sep 2026 14:32:39 +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]. While the parent series generalizes vmemmap optimization across HugeTLB and device DAX, this subset addresses a single, self-contained step: making the generic sparse-vmemmap code section-based optimization aware and switching HugeTLB bootmem pages to this path.
HugeTLB vmemmap optimization currently has its own early boot setup path. It pre-populates optimized vmemmap mappings before the normal sparse-vmemmap population code runs, and sparsemem carries
SPARSEMEM_VMEMMAP_PREINITonly to support that special case.That makes the HugeTLB vmemmap optimization path harder to share with other users of sparse-vmemmap optimization and leaves a fair amount of HugeTLB-specific boot-time state in the generic memory initialization flow.
Quoted 6 lines
This series introduces section-based vmemmap optimization support in the sparse-vmemmap code and switches HugeTLB bootmem pages over to it. Instead of having HugeTLB pre-populate optimized vmemmap mappings itself, HugeTLB now records the compound page order in the corresponding memory sections. The generic sparse-vmemmap population path can then allocate or reuse shared tail vmemmap pages based on section metadata.
Thanks, I updated mm-unstable with this series.