Skip to content

Commit 8e34841

Browse files
sumanthkorikkaropsiff
authored andcommitted
mm: fix accounting of memmap pages
[ Upstream commit c3576889d87b603cb66b417e08844a53c1077a37 ] 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/[email protected] Fixes: 15995a3 ("mm: report per-page metadata information") Signed-off-by: Sumanth Korikkar <[email protected]> Suggested-by: David Hildenbrand <[email protected]> Reviewed-by: Wei Yang <[email protected]> Cc: Alexander Gordeev <[email protected]> Cc: Gerald Schaefer <[email protected]> Cc: Heiko Carstens <[email protected]> Cc: Vasily Gorbik <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 1ee0e14814b88ff1771c212a94226a624486637a)
1 parent abe02a8 commit 8e34841

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
@@ -474,10 +474,5 @@ struct page * __meminit __populate_section_memmap(unsigned long pfn,
474474
if (r < 0)
475475
return NULL;
476476

477-
if (system_state == SYSTEM_BOOTING)
478-
memmap_boot_pages_add(DIV_ROUND_UP(end - start, PAGE_SIZE));
479-
else
480-
memmap_pages_add(DIV_ROUND_UP(end - start, PAGE_SIZE));
481-
482477
return pfn_to_page(pfn);
483478
}

mm/sparse.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,6 @@ static void __init sparse_buffer_init(unsigned long size, int nid)
462462
*/
463463
sparsemap_buf = memmap_alloc(size, section_map_size(), addr, nid, true);
464464
sparsemap_buf_end = sparsemap_buf + size;
465-
#ifndef CONFIG_SPARSEMEM_VMEMMAP
466-
memmap_boot_pages_add(DIV_ROUND_UP(size, PAGE_SIZE));
467-
#endif
468465
}
469466

470467
static void __init sparse_buffer_fini(void)
@@ -532,6 +529,8 @@ static void __init sparse_init_nid(int nid, unsigned long pnum_begin,
532529
sparse_buffer_fini();
533530
goto failed;
534531
}
532+
memmap_boot_pages_add(DIV_ROUND_UP(PAGES_PER_SECTION * sizeof(struct page),
533+
PAGE_SIZE));
535534
check_usemap_section_nr(nid, usage);
536535
sparse_init_one_section(__nr_to_section(pnum), pnum, map, usage,
537536
SECTION_IS_EARLY);
@@ -643,7 +642,6 @@ static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
643642
unsigned long start = (unsigned long) pfn_to_page(pfn);
644643
unsigned long end = start + nr_pages * sizeof(struct page);
645644

646-
memmap_pages_add(-1L * (DIV_ROUND_UP(end - start, PAGE_SIZE)));
647645
vmemmap_free(start, end, altmap);
648646
}
649647
static void free_map_bootmem(struct page *memmap)
@@ -819,10 +817,14 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
819817
* The memmap of early sections is always fully populated. See
820818
* section_activate() and pfn_valid() .
821819
*/
822-
if (!section_is_early)
820+
if (!section_is_early) {
821+
memmap_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)));
823822
depopulate_section_memmap(pfn, nr_pages, altmap);
824-
else if (memmap)
823+
} else if (memmap) {
824+
memmap_boot_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page),
825+
PAGE_SIZE)));
825826
free_map_bootmem(memmap);
827+
}
826828

827829
if (empty)
828830
ms->section_mem_map = (unsigned long)NULL;
@@ -867,6 +869,7 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn,
867869
section_deactivate(pfn, nr_pages, altmap);
868870
return ERR_PTR(-ENOMEM);
869871
}
872+
memmap_pages_add(DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE));
870873

871874
return memmap;
872875
}

0 commit comments

Comments
 (0)