Updating HPG_vmemmap_optimized in __hugetlb_vmemmap_optimize_folio() is subtle and counterintuitive. The flag cannot simply be set after optimization succeeds. When TLB flushing is deferred, accesses can continue using the old head page after remapping. The flag must therefore be set before remapping so that it is copied to the replacement head page, then cleared if optimization fails. This couples the flag update to vmemmap remapping and TLB behavior. A separate state transfer is required in gather_bootmem_prealloc_node(), which derives the optimization state from section metadata and copies it into HPG_vmemmap_optimized before the section state is cleared. This extra action is needed only to keep the two representations synchronized. Reuse PG_owner_priv_1 in the first struct page slot of each shared backing page to record the optimization state, and provide folio_vmemmap_optimized() as the common query. This lets consumers determine the state from the folio's vmemmap instead of maintaining and transferring a separate copy. Signed-off-by: Muchun Song --- v2: - Move PG_vmemmap_optimized to PG_owner_priv_1, since PG_owner_2 aliases PG_anon_exclusive on THP tail pages (reported by sashiko-bot) - Document that folio_vmemmap_optimized() returns true only after the TLB flush has completed (suggested by sashiko-bot) --- include/linux/page-flags.h | 6 ++++++ include/linux/vmemmap-optimization.h | 12 +++++++++++- mm/hugetlb_vmemmap.c | 4 +++- mm/sparse-vmemmap.c | 1 + 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 2b8582838a4e..377329a85151 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -132,6 +132,9 @@ enum pageflags { /* Some filesystems */ PG_checked = PG_owner_priv_1, + /* Set on the first struct page in a shared vmemmap backing page. */ + PG_vmemmap_optimized = PG_owner_priv_1, + /* * Depending on the way an anonymous folio can be mapped into a page * table (e.g., single PMD/PUD/CONT of the head page vs. PTE-mapped @@ -692,6 +695,9 @@ PAGEFLAG(VmemmapSelfHosted, vmemmap_self_hosted, PF_ANY) PAGEFLAG_FALSE(VmemmapSelfHosted, vmemmap_self_hosted) #endif +/* Only test this flag through folio_vmemmap_optimized(). */ +PAGEFLAG(VmemmapOptimized, vmemmap_optimized, PF_ANY) + /* * On an anonymous folio mapped into a user virtual memory area, * folio->mapping points to its anon_vma, not to a struct address_space; diff --git a/include/linux/vmemmap-optimization.h b/include/linux/vmemmap-optimization.h index b77a3161419e..68ee40cf389b 100644 --- a/include/linux/vmemmap-optimization.h +++ b/include/linux/vmemmap-optimization.h @@ -11,8 +11,8 @@ #include #include +#include #include -#include /* The number of retained vmemmap pages with HVO enabled. */ #define VMEMMAP_OPTIMIZATION_PAGES 1 @@ -106,4 +106,14 @@ static inline bool vmemmap_optimizable_pfn(unsigned long pfn, unsigned int order return (pfn & (nr_pages - 1)) >= VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES; } + +static inline bool folio_vmemmap_optimized(const struct folio *folio) +{ + const struct page *page = folio_page(folio, VMEMMAP_OPTIMIZATION_NR_STRUCT_PAGES); + + if (!vmemmap_optimizable_order(folio_order(folio))) + return false; + + return PageVmemmapOptimized(page); +} #endif /* _LINUX_VMEMMAP_OPTIMIZATION_H */ diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 336b1405cd6a..96203d3eba91 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -216,6 +216,7 @@ static struct page *vmemmap_dup_page_from_list(const struct page *src, list_del(&dst->lru); copy_page(page_address(dst), page_address(src)); + ClearPageVmemmapOptimized(page_address(dst)); /* Ensure updates to @dst page are visible before set_pte_at(). */ smp_wmb(); @@ -331,7 +332,8 @@ static int vmemmap_pteify_folio(const struct folio *folio, unsigned long flags) * Note: The caller must hold vmemmap_remap_rwsem. If TLB flushing is deferred, * the lock must remain held until the final TLB flush. * - * Return: %0 on success, negative error code otherwise. + * Return: %0 on success, with folio_vmemmap_optimized(@folio) returning true + * once the TLB flush has completed, or a negative error code otherwise. */ static int vmemmap_optimize_folio(const struct folio *folio, struct list_head *vmemmap_pages, unsigned long flags) diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 51fa77982ca5..d8246c6e7388 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -215,6 +215,7 @@ struct page __ref *vmemmap_shared_tail_page(unsigned int order, struct zone *zon memmap_init_page(page, zone_idx(zone), zone_to_nid(zone)); prep_compound_tail(page, NULL, order); } + SetPageVmemmapOptimized((struct page *)addr); page = virt_to_page(addr); if (cmpxchg(&pages[idx], NULL, page) != NULL) { -- 2.54.0