Teach section_nr_vmemmap_pages() to account for section-based vmemmap
optimization, so the helper can report the vmemmap page usage for a
memory section with or without shared tail vmemmap pages.

Signed-off-by: Muchun Song <[email protected]>
---
 include/linux/mmzone.h |  8 ++++++++
 mm/sparse-vmemmap.c    | 13 +++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index 5fc968bac1f7..0974205abd3d 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -2269,6 +2269,14 @@ static inline unsigned int pfn_to_section_order(unsigned 
long pfn)
        return section_order(__pfn_to_section(pfn));
 }
 
+static inline bool section_vmemmap_optimizable(const struct mem_section 
*section)
+{
+       if (!is_power_of_2(sizeof(struct page)))
+               return false;
+
+       return section_order(section) >= OPTIMIZABLE_FOLIO_MIN_ORDER;
+}
+
 void sparse_init_early_section(int nid, struct page *map, unsigned long pnum,
                               unsigned long flags);
 
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index 60d5330a8399..94964363d95c 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -629,24 +629,29 @@ void offline_mem_sections(unsigned long start_pfn, 
unsigned long end_pfn)
 static int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long 
nr_pages,
                struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
 {
-       const unsigned int order = pgmap ? pgmap->vmemmap_shift : 0;
+       const struct mem_section *ms = __pfn_to_section(pfn);
+       const unsigned int order = pgmap ? pgmap->vmemmap_shift : 
section_order(ms);
        const unsigned long pages_per_compound = 1UL << order;
+       unsigned int vmemmap_pages = OPTIMIZED_FOLIO_VMEMMAP_PAGES;
 
        VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION));
        VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION);
 
-       if (!vmemmap_can_optimize(altmap, pgmap))
+       if (vmemmap_can_optimize(altmap, pgmap))
+               vmemmap_pages = VMEMMAP_RESERVE_NR;
+
+       if (!vmemmap_can_optimize(altmap, pgmap) && 
!section_vmemmap_optimizable(ms))
                return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);
 
        if (order < PFN_SECTION_SHIFT) {
                VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, 
pages_per_compound));
-               return VMEMMAP_RESERVE_NR * nr_pages / pages_per_compound;
+               return vmemmap_pages * nr_pages / pages_per_compound;
        }
 
        VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION));
 
        if (IS_ALIGNED(pfn, pages_per_compound))
-               return VMEMMAP_RESERVE_NR;
+               return vmemmap_pages;
 
        return 0;
 }
-- 
2.54.0


Reply via email to