HPG_vmemmap_optimized duplicates the state recorded in the shared vmemmap backing page and must be updated on every mapping transition. Derive the state with folio_vmemmap_optimized() instead and remove the flag and its explicit updates to simplify the code. Signed-off-by: Muchun Song --- include/linux/hugetlb.h | 3 --- mm/hugetlb.c | 15 ++++++-------- mm/hugetlb_vmemmap.c | 43 +++++++---------------------------------- mm/memory-failure.c | 4 ++-- 4 files changed, 15 insertions(+), 50 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 14af4396de7e..98338392789f 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -612,7 +612,6 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr, * modifications require hugetlb_lock. * HPG_freed - Set when page is on the free lists. * Synchronization: hugetlb_lock held for examination and modification. - * HPG_vmemmap_optimized - Set when the vmemmap pages of the page are freed. * HPG_raw_hwp_unreliable - Set when the hugetlb page has a hwpoison sub-page * that is not tracked by raw_hwp_page list. */ @@ -621,7 +620,6 @@ enum hugetlb_page_flags { HPG_migratable, HPG_temporary, HPG_freed, - HPG_vmemmap_optimized, HPG_raw_hwp_unreliable, HPG_cma, __NR_HPAGEFLAGS, @@ -681,7 +679,6 @@ HPAGEFLAG(RestoreReserve, restore_reserve) HPAGEFLAG(Migratable, migratable) HPAGEFLAG(Temporary, temporary) HPAGEFLAG(Freed, freed) -HPAGEFLAG(VmemmapOptimized, vmemmap_optimized) HPAGEFLAG(RawHwpUnreliable, raw_hwp_unreliable) HPAGEFLAG(Cma, cma) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index a0f25fbb1a74..a664bcb50005 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1510,7 +1510,7 @@ void remove_hugetlb_folio(struct hstate *h, struct folio *folio, * pages. Otherwise, someone (memory error handling) may try to write * to tail struct pages. */ - if (!folio_test_hugetlb_vmemmap_optimized(folio)) + if (!folio_vmemmap_optimized(folio)) __folio_clear_hugetlb(folio); h->nr_huge_pages--; @@ -1522,14 +1522,13 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio, { int nid = folio_nid(folio); - VM_BUG_ON_FOLIO(!folio_test_hugetlb_vmemmap_optimized(folio), folio); + VM_BUG_ON_FOLIO(!folio_vmemmap_optimized(folio), folio); lockdep_assert_held(&hugetlb_lock); /* Callers must clear transient flags before returning to the pool. */ VM_WARN_ON_ONCE_FOLIO((unsigned long)folio->private & - ~(BIT(HPG_vmemmap_optimized) | - BIT(HPG_raw_hwp_unreliable) | + ~(BIT(HPG_raw_hwp_unreliable) | BIT(HPG_cma)), folio); INIT_LIST_HEAD(&folio->lru); @@ -1550,7 +1549,7 @@ void add_hugetlb_folio(struct hstate *h, struct folio *folio, static void __update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio) { - bool clear_flag = folio_test_hugetlb_vmemmap_optimized(folio); + bool clear_flag = folio_vmemmap_optimized(folio); if (hstate_is_gigantic_no_runtime(h)) { /* Callers must filter gigantic_no_runtime upstream. */ @@ -1658,7 +1657,7 @@ static inline void flush_free_hpage_work(struct hstate *h) static void update_and_free_hugetlb_folio(struct hstate *h, struct folio *folio, bool atomic) { - if (!folio_test_hugetlb_vmemmap_optimized(folio) || !atomic) { + if (!folio_vmemmap_optimized(folio) || !atomic) { __update_and_free_hugetlb_folio(h, folio); return; } @@ -3481,8 +3480,6 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid) memmap_init_compound_page(page, pfn, zone, huge_page_order(h)); init_new_hugetlb_folio(folio); - if (vmemmap_optimizable_order(pfn_to_section_compound_order(pfn))) - folio_set_hugetlb_vmemmap_optimized(folio); section_set_compound_order_range(pfn, nr_pages, 0); list_add(&folio->lru, &folio_list); @@ -4115,7 +4112,7 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst, int i; bool cma; - if (folio_test_hugetlb_vmemmap_optimized(folio)) + if (folio_vmemmap_optimized(folio)) continue; cma = folio_test_hugetlb_cma(folio); diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 96203d3eba91..199b157b8f2b 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -393,6 +393,9 @@ static int vmemmap_restore_folio(const struct folio *folio, unsigned long flags) int ret, nid = folio_nid(folio); LIST_HEAD(vmemmap_pages); + if (!folio_vmemmap_optimized(folio)) + return 0; + start += VMEMMAP_OPTIMIZATION_SIZE; ret = alloc_vmemmap_page_list(&vmemmap_pages, (end - start) / PAGE_SIZE, nid, GFP_KERNEL); @@ -412,18 +415,8 @@ early_param("hugetlb_free_vmemmap", hugetlb_vmemmap_optimize_param); static int __hugetlb_vmemmap_restore_folio(struct folio *folio, unsigned long flags) { - int ret; - VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(folio), folio); - - if (!folio_test_hugetlb_vmemmap_optimized(folio)) - return 0; - - ret = vmemmap_restore_folio(folio, flags); - if (!ret) - folio_clear_hugetlb_vmemmap_optimized(folio); - - return ret; + return vmemmap_restore_folio(folio, flags); } /** @@ -461,7 +454,7 @@ long hugetlb_vmemmap_restore_folios(struct list_head *folio_list, unsigned long flags = VMEMMAP_DEFER_TLB_FLUSH; list_for_each_entry_safe(folio, t_folio, folio_list, lru) { - if (folio_test_hugetlb_vmemmap_optimized(folio)) { + if (folio_vmemmap_optimized(folio)) { ret = __hugetlb_vmemmap_restore_folio(folio, flags); if (ret) break; @@ -483,37 +476,15 @@ static int __hugetlb_vmemmap_optimize_folio(struct folio *folio, struct list_head *vmemmap_pages, unsigned long flags) { - int ret; - VM_WARN_ON_ONCE_FOLIO(!folio_test_hugetlb(folio), folio); - - /* - * Very Subtle - * If VMEMMAP_DEFER_TLB_FLUSH is set, TLB flushing is not performed - * immediately after remapping. As a result, subsequent accesses - * and modifications to struct pages associated with the hugetlb - * page could be to the OLD struct pages. Set the vmemmap optimized - * flag here so that it is copied to the new head page. This keeps - * the old and new struct pages in sync. - * If there is an error during optimization, we will immediately FLUSH - * the TLB and clear the flag below. - */ - folio_set_hugetlb_vmemmap_optimized(folio); - ret = vmemmap_optimize_folio(folio, vmemmap_pages, flags); - if (ret) - folio_clear_hugetlb_vmemmap_optimized(folio); - - return ret; + return vmemmap_optimize_folio(folio, vmemmap_pages, flags); } /** * hugetlb_vmemmap_optimize_folio - optimize @folio's vmemmap pages. * @folio: the folio whose vmemmap pages will be optimized. * - * This function only tries to optimize @folio's vmemmap pages and does not - * guarantee that the optimization will succeed after it returns. The caller - * can use folio_test_hugetlb_vmemmap_optimized(@folio) to detect if @folio's - * vmemmap pages have been optimized. + * On success, folio_vmemmap_optimized(@folio) returns true. */ void hugetlb_vmemmap_optimize_folio(struct folio *folio) { diff --git a/mm/memory-failure.c b/mm/memory-failure.c index a45e0cfcb9e9..2be5b4d4057a 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -2028,7 +2028,7 @@ static unsigned long folio_free_raw_hwp(struct folio *folio, bool move_flag) * hugetlb_vmemmap_optimized hugepages can't be freed because struct * pages for tail pages are required but they don't exist. */ - if (move_flag && folio_test_hugetlb_vmemmap_optimized(folio)) + if (move_flag && folio_vmemmap_optimized(folio)) return 0; /* @@ -2045,7 +2045,7 @@ void folio_clear_hugetlb_hwpoison(struct folio *folio) { if (folio_test_hugetlb_raw_hwp_unreliable(folio)) return; - if (folio_test_hugetlb_vmemmap_optimized(folio)) + if (folio_vmemmap_optimized(folio)) return; folio_clear_hwpoison(folio); folio_free_raw_hwp(folio, true); -- 2.54.0