FAILED: patch "[PATCH] mm/sparse-vmemmap: fix vmemmap accounting underflow" failed to apply to 6.18-stable tree
Subsystems (3)
[email protected]
maintainerThe patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x c373f7f98e6ad591c85d40548cf8b6443be69311
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026072025-carving-copper-7ae4@gregkh' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From c373f7f98e6ad591c85d40548cf8b6443be69311 Mon Sep 17 00:00:00 2001
From: Muchun Song <[email protected]>
Date: Tue, 28 Apr 2026 16:18:50 +0800
Subject: [PATCH] mm/sparse-vmemmap: fix vmemmap accounting underflow
Patch series "mm: Fix vmemmap optimization accounting and initialization", v8.
The series fixes several bugs in vmemmap optimization, mainly around incorrect page accounting and memmap initialization in DAX and memory hotplug paths. It also fixes pageblock migratetype initialization and struct page initialization for ZONE_DEVICE compound pages.
Patches 1-4 fix vmemmap accounting issues. Patch 1 fixes an accounting underflow in the section activation failure path by moving vmemmap page accounting into the lower-level allocation and freeing helpers. Patch 2 fixes incorrect altmap passing in the memory hotplug error path. Patch 3 passes pgmap through memory deactivation paths so the teardown side can determine whether vmemmap optimization was in effect. Patch 4 uses that information to account the optimized DAX vmemmap size correctly.
Patches 5-6 fix initialization issues in mm/mm_init. One makes sure all pageblocks in ZONE_DEVICE compound pages get their migratetype initialized. The other fixes a case where DAX memory hotplug reuses an unoptimized early-section memmap while compound_nr_pages() still assumes vmemmap optimization, leaving tail struct pages uninitialized.
This patch (of 6):
In section_activate(), if populate_section_memmap() fails, the error handling path calls section_deactivate() to roll back the state. This causes a vmemmap accounting imbalance.
Since commit c3576889d87b ("mm: fix accounting of memmap pages"), memmap pages are accounted for only after populate_section_memmap() succeeds. However, the failure path unconditionally calls section_deactivate(), which decreases the vmemmap count. Consequently, a failure in populate_section_memmap() leads to an accounting underflow, incorrectly reducing the system's tracked vmemmap usage.
Fix this more thoroughly by moving all accounting calls into the lower level functions that actually perform the vmemmap allocation and freeing:
- populate_section_memmap() accounts for newly allocated vmemmap pages -depopulate_section_memmap() unaccounts when vmemmap is freed
This ensures proper accounting in all code paths, including error handling and early section cases.
mm/sparse-vmemmap.c +12 -8
@@ -651,7 +651,12 @@ static struct page * __meminit populate_section_memmap(unsigned long pfn, unsigned long nr_pages, int nid, struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { - return __populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap); + struct page *page = __populate_section_memmap(pfn, nr_pages, nid, altmap, + pgmap); + + memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)); + +return page; } static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
@@ -660,13 +665,17 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,unsigned long start = (unsigned long )pfn_to_page (pfn);unsigned long end = start + nr_pages *sizeof (struct page ); +memmap_pages_add (-1L * (DIV_ROUND_UP (nr_pages *sizeof (struct page ),PAGE_SIZE )));vmemmap_free (start, end, altmap); } +static void free_map_bootmem (struct page *memmap) {unsigned long start = (unsigned long )memmap;unsigned long end = (unsigned long )(memmap +PAGES_PER_SECTION ); +memmap_boot_pages_add (-1L * (DIV_ROUND_UP (PAGES_PER_SECTION *sizeof (struct page ), +PAGE_SIZE )));vmemmap_free (start, end,NULL ); }
@@ -769,14 +778,10 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,* The memmap of early sections is always fully populated. See * section_activate() and pfn_valid() . */ -if (!section_is_early) { -memmap_pages_add (-1L * (DIV_ROUND_UP (nr_pages *sizeof (struct page ),PAGE_SIZE ))); +if (!section_is_early)depopulate_section_memmap (pfn, nr_pages, altmap); - }else if (memmap) { -memmap_boot_pages_add (-1L * (DIV_ROUND_UP (nr_pages *sizeof (struct page ), -PAGE_SIZE ))); +else if (memmap)free_map_bootmem (memmap); - }if (empty) ms->section_mem_map = (unsigned long )NULL ;
@@ -821,7 +826,6 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,section_deactivate (pfn, nr_pages, altmap);return ERR_PTR (-ENOMEM ); } - memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE));return memmap; }
Sasha Levin
maintainerPATCH 1/1 mm/sparse-vmemmap: fix vmemmap accounting underflow
From: Muchun Song <[email protected]>
[ Upstream commit c373f7f98e6ad591c85d40548cf8b6443be69311 ]
Patch series "mm: Fix vmemmap optimization accounting and initialization", v8.
The series fixes several bugs in vmemmap optimization, mainly around incorrect page accounting and memmap initialization in DAX and memory hotplug paths. It also fixes pageblock migratetype initialization and struct page initialization for ZONE_DEVICE compound pages.
Patches 1-4 fix vmemmap accounting issues. Patch 1 fixes an accounting underflow in the section activation failure path by moving vmemmap page accounting into the lower-level allocation and freeing helpers. Patch 2 fixes incorrect altmap passing in the memory hotplug error path. Patch 3 passes pgmap through memory deactivation paths so the teardown side can determine whether vmemmap optimization was in effect. Patch 4 uses that information to account the optimized DAX vmemmap size correctly.
Patches 5-6 fix initialization issues in mm/mm_init. One makes sure all pageblocks in ZONE_DEVICE compound pages get their migratetype initialized. The other fixes a case where DAX memory hotplug reuses an unoptimized early-section memmap while compound_nr_pages() still assumes vmemmap optimization, leaving tail struct pages uninitialized.
This patch (of 6):
In section_activate(), if populate_section_memmap() fails, the error handling path calls section_deactivate() to roll back the state. This causes a vmemmap accounting imbalance.
Since commit c3576889d87b ("mm: fix accounting of memmap pages"), memmap pages are accounted for only after populate_section_memmap() succeeds. However, the failure path unconditionally calls section_deactivate(), which decreases the vmemmap count. Consequently, a failure in populate_section_memmap() leads to an accounting underflow, incorrectly reducing the system's tracked vmemmap usage.
Fix this more thoroughly by moving all accounting calls into the lower level functions that actually perform the vmemmap allocation and freeing:
- populate_section_memmap() accounts for newly allocated vmemmap pages -depopulate_section_memmap() unaccounts when vmemmap is freed
This ensures proper accounting in all code paths, including error handling and early section cases.
--- mm/sparse.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-)
mm/sparse.c +22 -10
@@ -670,7 +670,12 @@ static struct page * __meminit populate_section_memmap(unsigned long pfn, unsigned long nr_pages, int nid, struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { - return __populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap); + struct page *page = __populate_section_memmap(pfn, nr_pages, nid, altmap, + pgmap); + + memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)); + +return page; } static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
@@ -679,13 +684,17 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,unsigned long start = (unsigned long )pfn_to_page (pfn);unsigned long end = start + nr_pages *sizeof (struct page ); +memmap_pages_add (-1L * (DIV_ROUND_UP (nr_pages *sizeof (struct page ),PAGE_SIZE )));vmemmap_free (start, end, altmap); } +static void free_map_bootmem (struct page *memmap) {unsigned long start = (unsigned long )memmap;unsigned long end = (unsigned long )(memmap +PAGES_PER_SECTION ); +memmap_boot_pages_add (-1L * (DIV_ROUND_UP (PAGES_PER_SECTION *sizeof (struct page ), +PAGE_SIZE )));vmemmap_free (start, end,NULL ); }
@@ -742,13 +751,18 @@ static struct page * __meminit populate_section_memmap(unsigned long pfn, unsigned long nr_pages, int nid, struct vmem_altmap *altmap, struct dev_pagemap *pgmap) { - return kvmalloc_node(array_size(sizeof(struct page), -PAGES_PER_SECTION ),GFP_KERNEL ,nid ); + struct page *page = kvmalloc_node(array_size(sizeof(struct page), +PAGES_PER_SECTION ),GFP_KERNEL , nid); + + memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)); + +return page; }static void depopulate_section_memmap (unsigned long pfn,unsigned long nr_pages,struct vmem_altmap *altmap) { +memmap_pages_add (-1L * (DIV_ROUND_UP (nr_pages *sizeof (struct page ),PAGE_SIZE )));kvfree (pfn_to_page (pfn)); }
@@ -761,6 +775,9 @@ static void free_map_bootmem(struct page *memmap) nr_pages =PAGE_ALIGN (PAGES_PER_SECTION *sizeof (struct page )) >>PAGE_SHIFT ; +memmap_boot_pages_add (-1L * (DIV_ROUND_UP (PAGES_PER_SECTION *sizeof (struct page ), +PAGE_SIZE ))); +for (i =0 ; i < nr_pages; i++, page++) { type =bootmem_type (page);
@@ -854,14 +871,10 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,* The memmap of early sections is always fully populated. See * section_activate() and pfn_valid() . */ -if (!section_is_early) { -memmap_pages_add (-1L * (DIV_ROUND_UP (nr_pages *sizeof (struct page ),PAGE_SIZE ))); +if (!section_is_early)depopulate_section_memmap (pfn, nr_pages, altmap); - }else if (memmap) { -memmap_boot_pages_add (-1L * (DIV_ROUND_UP (nr_pages *sizeof (struct page ), -PAGE_SIZE ))); +else if (memmap)free_map_bootmem (memmap); - }if (empty) ms->section_mem_map = (unsigned long )NULL ;
@@ -906,7 +919,6 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,section_deactivate (pfn, nr_pages, altmap);return ERR_PTR (-ENOMEM ); } - memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE));return memmap; }
Signature · 1 line
2.53.0