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_supports_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. 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(). Also restore the early return in hugetlb_vmemmap_optimize_folios() when no folio needs to be optimized, so that systems where all bootmem folios were optimized through the section-based optimization (or where HVO is disabled) do not take vmemmap_remap_rwsem for write and flush the TLB twice for nothing. Signed-off-by: James Houghton --- mm/hugetlb.c | 7 +++++++ mm/hugetlb_vmemmap.c | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 6a4af46ddded..54c1dd955a29 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -3364,6 +3364,13 @@ static void __init prep_and_add_bootmem_folios(struct hstate *h, unsigned long flags; struct folio *folio, *tmp_f; + /* + * Some architectures do not support section-based vmemmap optimization, + * so their bootmem folios should be optimized here with folio-based + * optimization. + */ + hugetlb_vmemmap_optimize_folios(h, folio_list); + list_for_each_entry_safe(folio, tmp_f, folio_list, lru) { /* Subdivide locks to achieve better parallel performance */ spin_lock_irqsave(&hugetlb_lock, flags); diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 8ec80f450123..df145776aa6b 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -610,6 +610,7 @@ static int hugetlb_vmemmap_pteify_folio(const struct hstate *h, struct folio *fo void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list) { struct folio *folio; + unsigned long nr_to_optimize = 0; LIST_HEAD(vmemmap_pages); unsigned long flags = VMEMMAP_DEFER_TLB_FLUSH; @@ -618,7 +619,14 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l down_write(&vmemmap_remap_rwsem); list_for_each_entry(folio, folio_list, lru) { - int ret = hugetlb_vmemmap_pteify_folio(h, folio, flags); + int ret; + + if (!vmemmap_should_optimize_folio(h, folio)) + continue; + + nr_to_optimize++; + + ret = hugetlb_vmemmap_pteify_folio(h, folio, flags); /* * Splitting the PMD requires allocating a page, thus let's fail @@ -630,6 +638,11 @@ void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_l break; } + if (!nr_to_optimize) { + up_write(&vmemmap_remap_rwsem); + return; + } + flush_tlb_all(); downgrade_write(&vmemmap_remap_rwsem); -- 2.56.1.839.g22a8290db9-goog