vmemmap_should_optimize_folio() combines the global HVO policy, per-hstate eligibility, and the already-optimized state. The latter two do not need to gate the normal flow: generic folio and section helpers check order eligibility, while optimization entry points only receive freshly initialized folios. Thus, only the global enable policy must be checked at every entry point. Fold the global enable check into each caller. Retain the per-hstate check in hugetlb_vmemmap_optimize_folios() only as an optimization, so an ineligible hstate is rejected before iterating the entire folio list. The batch path now reads the enable policy only once at entry instead of once per folio in both passes. Signed-off-by: Muchun Song Reviewed-by: Lance Yang --- v2: - Mention that the batch path reads the enable policy once at entry (suggested by Lance Yang) - Collect Reviewed-by from Lance Yang --- mm/hugetlb_vmemmap.c | 38 ++++++++++---------------------------- 1 file changed, 10 insertions(+), 28 deletions(-) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 09b9fd83c198..71aa340cce55 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -488,21 +488,6 @@ long hugetlb_vmemmap_restore_folios(const struct hstate *h, return ret; } -/* Return true iff a HugeTLB whose vmemmap should and can be optimized. */ -static bool vmemmap_should_optimize_folio(const struct hstate *h, struct folio *folio) -{ - if (folio_test_hugetlb_vmemmap_optimized(folio)) - return false; - - if (!READ_ONCE(vmemmap_optimize_enabled)) - return false; - - if (!hugetlb_vmemmap_optimizable(h)) - return false; - - return true; -} - static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio, struct list_head *vmemmap_pages, @@ -512,9 +497,6 @@ static int __hugetlb_vmemmap_optimize_folio(const struct hstate *h, VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(folio), folio); - if (!vmemmap_should_optimize_folio(h, folio)) - return 0; - /* * Very Subtle * If VMEMMAP_DEFER_TLB_FLUSH is set, TLB flushing is not performed @@ -548,32 +530,32 @@ void hugetlb_vmemmap_optimize_folio(const struct hstate *h, struct folio *folio) { LIST_HEAD(vmemmap_pages); + if (!READ_ONCE(vmemmap_optimize_enabled)) + return; + guard(rwsem_read)(&vmemmap_remap_rwsem); __hugetlb_vmemmap_optimize_folio(h, folio, &vmemmap_pages, 0); free_vmemmap_page_list(&vmemmap_pages); } -static int hugetlb_vmemmap_pteify_folio(const struct hstate *h, struct folio *folio, - unsigned long flags) -{ - if (!vmemmap_should_optimize_folio(h, folio)) - return 0; - - return vmemmap_pteify_folio(folio, flags); -} - void hugetlb_vmemmap_optimize_folios(struct hstate *h, struct list_head *folio_list) { struct folio *folio; LIST_HEAD(vmemmap_pages); unsigned long flags = VMEMMAP_DEFER_TLB_FLUSH; + if (!READ_ONCE(vmemmap_optimize_enabled)) + return; + + if (!hugetlb_vmemmap_optimizable(h)) + return; + if (list_empty(folio_list)) return; down_write(&vmemmap_remap_rwsem); list_for_each_entry(folio, folio_list, lru) { - int ret = hugetlb_vmemmap_pteify_folio(h, folio, flags); + int ret = vmemmap_pteify_folio(folio, flags); /* * Splitting the PMD requires allocating a page, thus let's fail -- 2.54.0