Skip to contentSkip to conversation

mm: fix accounting of memmap pages for early sections

PATCH In review Sumanth Korikkar 2 participants 3 messages Aug 4, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: fix accounting of memmap pages for early sections

memmap pages can be allocated either from the memblock (boot) allocator during early boot or from the buddy allocator.

When these memmap pages are removed via arch_remove_memory(), the deallocation path depends on their source:

* For pages from the buddy allocator, depopulate_section_memmap() is called, which also decrements the count of nr_memmap_pages.

* For pages from the boot allocator, free_map_bootmem() is called. But it currently does not adjust the nr_memmap_boot_pages.

To fix this inconsistency, update free_map_bootmem() to also decrement the nr_memmap_boot_pages count by invoking memmap_boot_pages_add(), mirroring how free_vmemmap_page() handles this for boot-allocated pages.

This ensures correct tracking of memmap pages regardless of allocation source.

Fixes 15995a352474 ("mm: report per-page metadata information")
---
 mm/sparse.c | 1 +
 1 file changed, 1 insertion(+)
mm/sparse.c +1 -0
index 3c012cf83cc2..d7c128015397 100644
@@ -688,6 +688,7 @@ 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(end - start, PAGE_SIZE)));
 	vmemmap_free(start, end, NULL);
 }
 
Signature · 1 line
2.48.1
#1 lore raw
Quoted 31 lines · Sumanth Korikkar

memmap pages can be allocated either from the memblock (boot) allocator during early boot or from the buddy allocator.

When these memmap pages are removed via arch_remove_memory(), the deallocation path depends on their source:

* For pages from the buddy allocator, depopulate_section_memmap() is called, which also decrements the count of nr_memmap_pages.

* For pages from the boot allocator, free_map_bootmem() is called. But it currently does not adjust the nr_memmap_boot_pages.

To fix this inconsistency, update free_map_bootmem() to also decrement the nr_memmap_boot_pages count by invoking memmap_boot_pages_add(), mirroring how free_vmemmap_page() handles this for boot-allocated pages.

This ensures correct tracking of memmap pages regardless of allocation source.

Fixes 15995a352474 ("mm: report per-page metadata information")
---
  mm/sparse.c | 1 +
  1 file changed, 1 insertion(+)
mm/sparse.c +0 -0
index 3c012cf83cc2..d7c128015397 100644

mm/sparse.c static void free_map_bootmem(struct page *memmap) · line 694

Quoted 7 lines
mm/sparse.c +1 -0
@@ -688,6 +688,7 @@ 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(end - start, PAGE_SIZE)));
  	vmemmap_free(start, end, NULL);
  }
 

Looks good to me. But now I wonder about !CONFIG_SPARSEMEM_VMEMMAP, where neither depopulate_section_memmap() nor free_map_bootmem() adjust anything?

Which makes me wonder whether we should be moving that to section_deactivate().

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 41 lines · David Hildenbrand
Quoted 31 lines · Sumanth Korikkar

memmap pages can be allocated either from the memblock (boot) allocator during early boot or from the buddy allocator.

When these memmap pages are removed via arch_remove_memory(), the deallocation path depends on their source:

* For pages from the buddy allocator, depopulate_section_memmap() is called, which also decrements the count of nr_memmap_pages.

* For pages from the boot allocator, free_map_bootmem() is called. But it currently does not adjust the nr_memmap_boot_pages.

To fix this inconsistency, update free_map_bootmem() to also decrement the nr_memmap_boot_pages count by invoking memmap_boot_pages_add(), mirroring how free_vmemmap_page() handles this for boot-allocated pages.

This ensures correct tracking of memmap pages regardless of allocation source.

Fixes 15995a352474 ("mm: report per-page metadata information")
---
  mm/sparse.c | 1 +
  1 file changed, 1 insertion(+)
mm/sparse.c +0 -0
index 3c012cf83cc2..d7c128015397 100644

mm/sparse.c static void free_map_bootmem(struct page *memmap) · line 692

Quoted 5 lines
mm/sparse.c +1 -0
@@ -688,6 +688,7 @@ 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(end - start, PAGE_SIZE)));
  	vmemmap_free(start, end, NULL);
  }

Looks good to me. But now I wonder about !CONFIG_SPARSEMEM_VMEMMAP, where neither depopulate_section_memmap() nor free_map_bootmem() adjust anything?

Quoted 2 lines

Which makes me wonder whether we should be moving that to section_deactivate().

Agree. I will move accounting to section_deactivate() then.

Thanks