[PATCH v6 4/7] mm/sparse-vmemmap: Fix DAX vmemmap accounting with optimization
Muchun Song
muchun.song at linux.dev
Sat Apr 25 13:05:38 AEST 2026
> On Apr 24, 2026, at 15:33, David Hildenbrand (Arm) <david at kernel.org> wrote:
>
> On 4/24/26 04:55, Muchun Song wrote:
>> When vmemmap optimization is enabled for DAX, the nr_memmap_pages
>> counter in /proc/vmstat is incorrect. The current code always accounts
>> for the full, non-optimized vmemmap size, but vmemmap optimization
>> reduces the actual number of vmemmap pages by reusing tail pages. This
>> causes the system to overcount vmemmap usage, leading to inaccurate
>> page statistics in /proc/vmstat.
>>
>> Fix this by introducing section_vmemmap_pages(), which returns the exact
>> vmemmap page count for a given pfn range based on whether optimization
>> is in effect.
>>
>> Fixes: 15995a352474 ("mm: report per-page metadata information")
>> Cc: stable at vger.kernel.org
>> Signed-off-by: Muchun Song <songmuchun at bytedance.com>
>> Acked-by: Mike Rapoport (Microsoft) <rppt at kernel.org>
>> Acked-by: Oscar Salvador <osalvador at suse.de>
>> ---
>> mm/sparse-vmemmap.c | 31 +++++++++++++++++++++++++++----
>> 1 file changed, 27 insertions(+), 4 deletions(-)
>>
>> diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
>> index 3340f6d30b01..2e642c5ff3f2 100644
>> --- a/mm/sparse-vmemmap.c
>> +++ b/mm/sparse-vmemmap.c
>> @@ -652,6 +652,28 @@ 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 unsigned long pages_per_compound = 1UL << order;
>> +
>> + VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages,
>> + min(pages_per_compound, PAGES_PER_SECTION)));
>
> FWIW, I though the right thing to do here would be:
>
> VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, pages_per_compound);
> VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION);
>
> I don't really see how PAGES_PER_SECTION make sense given that
> PAGES_PER_SUBSECTION are the smallest granularity we allow adding/removing.
>
> Also, the "min()" implies that there is a connection between both properties,
> but there isn't to that degree.
>
> If order == 0, then you'd only ever check alignment for ... 1, not
> PAGES_PER_SUBSECTION, which already looks weird.
>
> So you really want to check "max(pages_per_compound, PAGES_PER_SUBSECTION)", but
> just having two statements is clearer.
>
> Or am I getting something very wrong here? :)
Hi David,
Sorry, I missed the 1GB hugepage scenario earlier. Given that sparse_add_section()
operates on a scale between PAGES_PER_SUBSECTION and PAGES_PER_SECTION, the pfn and
nr_pages parameters wouldn't be aligned with the hugepage size (pages_per_compound),
but rather with the PAGES_PER_SECTION boundary. Do you think this explanation makes
it clearer? In the interest of code clarity, do you think the modification below
makes it easier to follow?
diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c
index 2e642c5ff3f2..ce675c5fb94d 100644
--- a/mm/sparse-vmemmap.c
+++ b/mm/sparse-vmemmap.c
@@ -658,15 +658,18 @@ static int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long n
const unsigned int order = pgmap ? pgmap->vmemmap_shift : 0;
const unsigned long pages_per_compound = 1UL << order;
- VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages,
- min(pages_per_compound, PAGES_PER_SECTION)));
+ VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION));
VM_WARN_ON_ONCE(pfn_to_section_nr(pfn) != pfn_to_section_nr(pfn + nr_pages - 1));
if (!vmemmap_can_optimize(altmap, pgmap))
return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);
- if (order < PFN_SECTION_SHIFT)
+ 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;
+ }
+
+ VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION));
if (IS_ALIGNED(pfn, pages_per_compound))
return VMEMMAP_RESERVE_NR;
Thanks.
>
>
> --
> Cheers,
>
> David
More information about the Linuxppc-dev
mailing list