Re: [PATCH v3 06/19] hugetlb: Optimize bootmem folios that were not pre-HVOed

From: James Houghton

Date: Wed Nov 25 2026 - 11:10:08 EST


On Wed, Nov 25, 2026 at 4:37 AM Muchun Song <muchun.song@xxxxxxxxx> wrote:
>
>
>
> > On Nov 25, 2026, at 00:58, James Houghton <jthoughton@xxxxxxxxxx> wrote:
> >
> > On Sun, Nov 22, 2026 at 10:11 PM Muchun Song <muchun.song@xxxxxxxxx> wrote:
> >>
> >>
> >>
> >>> On Nov 23, 2026, at 02:44, James Houghton <jthoughton@xxxxxxxxxx> wrote:
> >>>
> >>> Boot-time gigantic folios are expected to be pre-HVOed through the
> >>> section-based vmemmap optimization, so the bulk optimization pass for
> >>> bootmem folios was dropped.
> >>>
> >>> However, arch_support_vmemmap_remap() may not yet
> >>> return true when bootmem folios are allocated (e.g. on arm64, where
> >>> support depends on a system-wide CPU capability), while it does return
> >>> true by the time the bootmem folios are gathered. Such architectures
> >>> disable the section-based vmemmap optimization in their vmemmap
> >>> population path (see section_vmemmap_optimization_disable()), so the
> >>> folios are not pre-HVOed and would never be optimized.
> >>
> >> Yeah, when I removed support for this part of the code, I figured you
> >> might add a new patch in the future to add support back. It is fine.
> >>
> >>>
> >>> Optimize such folios when gathering bootmem folios. Their tail struct
> >>> pages have all been initialized by memmap_init_compound_page(),
> >>> as they were not pre-HVOed. Pre-HVOed folios are skipped by
> >>> vmemmap_should_optimize_folio().
> >>>
> >>> Signed-off-by: James Houghton <jthoughton@xxxxxxxxxx>
> >>> ---
> >>> mm/hugetlb.c | 11 +++++++++++
> >>> 1 file changed, 11 insertions(+)
> >>>
> >>> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> >>> index 72839e98f998..8756c0206922 100644
> >>> --- a/mm/hugetlb.c
> >>> +++ b/mm/hugetlb.c
> >>> @@ -3325,6 +3325,17 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h,
> >>> unsigned long flags;
> >>> struct folio *folio, *tmp_f;
> >>>
> >>> + /*
> >>> + * Bootmem folios are normally pre-HVOed through the section-based
> >>> + * vmemmap optimization. However, an architecture that can only
> >>> + * determine whether it supports HVO after bootmem allocation disables
> >>> + * the section-based optimization (see
> >>> + * section_vmemmap_optimization_disable()), in which case the folios
> >>> + * were not pre-HVOed and must be optimized now. Pre-HVOed folios are
> >>> + * skipped.
> >>> + */
> >>
> >> I'd like to condense these comments to something like:
> >>
> >> /*
> >> * Some architectures do not support section-based vmemmap optimization,
> >> * so their bootmem folios should be optimized here with folio-based
> >> * optimization.
> >> */
> >
> > Applied in v4, thanks!
> >
> > Also just so you don't miss it, would appreciate your thoughts on my
> > reply to Sashiko's comments on this patch[1]. Let me know if you would
> > like me to do something different.
>
> I think the diff below might be a bit simpler. You might think that when
> HVO is turned off, it could still cause some unnecessary TLB flushes. You
> don't need to worry about that, because I've added a global guard to make
> sure it doesn't happen. Take a look here [1].
>
> [1] https://lore.kernel.org/all/20261124110530.3004071-3-songmuchun@xxxxxxxxxxxxx/
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index b2c14f4f99b7..69a7d4ca8b1b 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -3345,6 +3345,7 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid)
> LIST_HEAD(folio_list);
> struct huge_bootmem_page *m, *tm;
> struct hstate *h = NULL, *prev_h = NULL;
> + unsigned long vmemmap_unoptimized = 0;
>
> list_for_each_entry_safe(m, tm, &huge_boot_pages[nid], list) {
> struct page *page = virt_to_page(m);
> @@ -3368,6 +3369,8 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid)
>
> if (vmemmap_optimizable_order(pfn_to_section_compound_order(pfn)))
> folio_set_hugetlb_vmemmap_optimized(folio);
> + else
> + vmemmap_unoptimized++;
> section_set_compound_order_range(pfn, nr_pages, 0);
>
> list_add(&folio->lru, &folio_list);
> @@ -3387,6 +3390,8 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid)
> cond_resched();
> }
>
> + if (vmemmap_unoptimized)
> + hugetlb_vmemmap_optimize_folios(h, &folio_list);

This hunk needs to also be added to the `h != prev_h` piece inside of
the loop, otherwise we could miss optimizing folios for some hstates.

I prefer the early return that I've re-applied. It's a lot more
straightforward for me. Let me know if you really want me to switch it
with your suggestion.

Thanks for all the feedback, Muchun! I really appreciate it.

> prep_and_add_bootmem_folios(h, &folio_list);
> }
>
>
>