Skip to content

Commit cba402a

Browse files
sumanthkorikkargregkh
authored andcommitted
mm: fix accounting of memmap pages
commit c357688 upstream. For !CONFIG_SPARSEMEM_VMEMMAP, memmap page accounting is currently done upfront in sparse_buffer_init(). However, sparse_buffer_alloc() may return NULL in failure scenario. Also, memmap pages may be allocated either from the memblock allocator during early boot or from the buddy allocator. When removed via arch_remove_memory(), accounting of memmap pages must reflect the original allocation source. To ensure correctness: * Account memmap pages after successful allocation in sparse_init_nid() and section_activate(). * Account memmap pages in section_deactivate() based on allocation source. Link: https://lkml.kernel.org/r/20250807183545.1424509-1-sumanthk@linux.ibm.com Fixes: 15995a3 ("mm: report per-page metadata information") Signed-off-by: Sumanth Korikkar <sumanthk@linux.ibm.com> Suggested-by: David Hildenbrand <david@redhat.com> Reviewed-by: Wei Yang <richard.weiyang@gmail.com> Cc: Alexander Gordeev <agordeev@linux.ibm.com> Cc: Gerald Schaefer <gerald.schaefer@linux.ibm.com> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Vasily Gorbik <gor@linux.ibm.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent bd1ee62 commit cba402a

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

mm/sparse-vmemmap.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,6 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,
578578
if (r < 0)
579579
return NULL;
580580

581-
if (system_state == SYSTEM_BOOTING)
582-
memmap_boot_pages_add(DIV_ROUND_UP(end - start, PAGE_SIZE));
583-
else
584-
memmap_pages_add(DIV_ROUND_UP(end - start, PAGE_SIZE));
585-
586581
return pfn_to_page(pfn);
587582
}
588583

mm/sparse.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,6 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
454454
*/
455455
sparsemap_buf = memmap_alloc(size, section_map_size(), addr, nid, true);
456456
sparsemap_buf_end = sparsemap_buf + size;
457-
#ifndef CONFIG_SPARSEMEM_VMEMMAP
458-
memmap_boot_pages_add(DIV_ROUND_UP(size, PAGE_SIZE));
459-
#endif
460457
}
461458

462459
static void __init sparse_buffer_fini(void)
@@ -567,6 +564,8 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
567564
sparse_buffer_fini();
568565
goto failed;
569566
}
567+
memmap_boot_pages_add(DIV_ROUND_UP(PAGES_PER_SECTION * sizeof(struct page),
568+
PAGE_SIZE));
570569
sparse_init_early_section(nid, map, pnum, 0);
571570
}
572571
}
@@ -680,7 +679,6 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
680679
unsigned long start = (unsigned long) pfn_to_page(pfn);
681680
unsigned long end = start + nr_pages * sizeof(struct page);
682681

683-
memmap_pages_add(-1L * (DIV_ROUND_UP(end - start, PAGE_SIZE)));
684682
vmemmap_free(start, end, altmap);
685683
}
686684
static void free_map_bootmem(struct page *memmap)
@@ -856,10 +854,14 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
856854
* The memmap of early sections is always fully populated. See
857855
* section_activate() and pfn_valid() .
858856
*/
859-
if (!section_is_early)
857+
if (!section_is_early) {
858+
memmap_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)));
860859
depopulate_section_memmap(pfn, nr_pages, altmap);
861-
else if (memmap)
860+
} else if (memmap) {
861+
memmap_boot_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page),
862+
PAGE_SIZE)));
862863
free_map_bootmem(memmap);
864+
}
863865

864866
if (empty)
865867
ms->section_mem_map = (unsigned long)NULL;
@@ -904,6 +906,7 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,
904906
section_deactivate(pfn, nr_pages, altmap);
905907
return ERR_PTR(-ENOMEM);
906908
}
909+
memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE));
907910

908911
return memmap;
909912
}

0 commit comments

Comments
 (0)