Skip to contentSkip to conversation

mm: fix accounting of memmap pages for early sections

PATCH Superseded Sumanth Korikkar 3 participants 5 messages Aug 4, 2025 – Aug 6, 2025

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 should decrement the count of nr_memmap_pages.

* For pages from the boot allocator, free_map_bootmem() is called, which should decrement the count of the nr_memmap_boot_pages.

Ensure correct tracking of memmap pages for both early sections and non early sections by adjusting the accounting in section_deactivate().

Fixes 15995a352474 ("mm: report per-page metadata information")

---
v2: consider accounting for !CONFIG_SPARSEMEM_VMEMMAP.

 mm/sparse.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mm/sparse.c +6 -3
index 3c012cf83cc2..b9cc9e548f80 100644
@@ -680,7 +680,6 @@ 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(end - start, PAGE_SIZE)));
 	vmemmap_free(start, end, altmap);
 }
 static void free_map_bootmem(struct page *memmap)
@@ -856,10 +855,14 @@ 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)
+	if (!section_is_early) {
+		memmap_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)));
 		depopulate_section_memmap(pfn, nr_pages, altmap);
-	else if (memmap)
+	} else if (memmap) {
+		memmap_boot_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page),
+				      PAGE_SIZE)));
 		free_map_bootmem(memmap);
+	}
 
 	if (empty)
 		ms->section_mem_map = (unsigned long)NULL;
Signature · 1 line
2.48.1
Acked-by on v2 1/1
#1 lore raw
Quoted 44 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 should decrement the count of nr_memmap_pages.

* For pages from the boot allocator, free_map_bootmem() is called, which should decrement the count of the nr_memmap_boot_pages.

Ensure correct tracking of memmap pages for both early sections and non early sections by adjusting the accounting in section_deactivate().

Fixes 15995a352474 ("mm: report per-page metadata information")

---
v2: consider accounting for !CONFIG_SPARSEMEM_VMEMMAP.

  mm/sparse.c | 9 ++++++---
  1 file changed, 6 insertions(+), 3 deletions(-)
mm/sparse.c +2 -2
index 3c012cf83cc2..b9cc9e548f80 100644
@@ -680,7 +680,6 @@ 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(end - start, PAGE_SIZE)));
  	vmemmap_free(start, end, altmap);
  }
  static void free_map_bootmem(struct page *memmap)
@@ -856,10 +855,14 @@ 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)
+	if (!section_is_early) {
+		memmap_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)));

mm/sparse.c static void section_deactivate(unsigned long pfn, unsigned long nr_pages, · line 868

Quoted 10 lines
mm/sparse.c +4 -1
  		depopulate_section_memmap(pfn, nr_pages, altmap);
-	else if (memmap)
+	} else if (memmap) {
+		memmap_boot_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page),
+				      PAGE_SIZE)));
  		free_map_bootmem(memmap);
+	}
 
  	if (empty)
  		ms->section_mem_map = (unsigned long)NULL;

Hopefully we're not missing anything important.

Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
In reply to Sumanth Korikkar
Quoted 41 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 should decrement the count of nr_memmap_pages.

* For pages from the boot allocator, free_map_bootmem() is called, which should decrement the count of the nr_memmap_boot_pages.

Ensure correct tracking of memmap pages for both early sections and non early sections by adjusting the accounting in section_deactivate().

Fixes 15995a352474 ("mm: report per-page metadata information")

---
v2: consider accounting for !CONFIG_SPARSEMEM_VMEMMAP.

mm/sparse.c | 9 ++++++---

1 file changed, 6 insertions(+), 3 deletions(-)
mm/sparse.c +0 -1
index 3c012cf83cc2..b9cc9e548f80 100644
@@ -680,7 +680,6 @@ 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(end - start, PAGE_SIZE)));
 	vmemmap_free(start, end, altmap);
 }
 static void free_map_bootmem(struct page *memmap)
@@ -856,10 +855,14 @@ 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() .
 	 */

mm/sparse.c static void section_deactivate(unsigned long pfn, unsigned long nr_pages, · line 865

Quoted 10 lines
mm/sparse.c +6 -2
-	if (!section_is_early)
+	if (!section_is_early) {
+		memmap_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE)));
 		depopulate_section_memmap(pfn, nr_pages, altmap);
-	else if (memmap)
+	} else if (memmap) {
+		memmap_boot_pages_add(-1L * (DIV_ROUND_UP(nr_pages * sizeof(struct page),
+				      PAGE_SIZE)));
 		free_map_bootmem(memmap);
+	}

The change here is reasonable. While maybe we still miss the counting at some other points.

For example:

a.

sparse_init_nid()
__populate_section_memmap()

If !CONFIG_SPARSEMEM_VMEMMAP, and sparse_buffer_alloc() return NULL, it allocate extra memory from bootmem, which looks not counted.

b.

section_activate()
populate_section_memmap()

If !CONFIG_SPARSEMEM_VMEMMAP, it just call kvmalloc_node(), which looks not counted.

Do I missed something?

Quoted 6 lines
mm/sparse.c +0 -0
 	if (empty)
 		ms->section_mem_map = (unsigned long)NULL;
Signature · 1 line
2.48.1
Signature · 2 lines
Wei Yang
Help you, Help me
#3 lore raw
Quoted 5 lines

The change here is reasonable. While maybe we still miss the counting at some other points.

For example:

Quoted 7 lines

a.

sparse_init_nid()
__populate_section_memmap()

If !CONFIG_SPARSEMEM_VMEMMAP, and sparse_buffer_alloc() return NULL, it allocate extra memory from bootmem, which looks not counted.

Currently, the accounting is done upfront in sparse_buffer_init(), where memmap_boot_pages_add() is called for !CONFIG_SPARSEMEM_VMEMMAP.

The function sparse_buffer_alloc() can return NULL in two scenarios:

* During sparse_buffer_init(), if memmap_alloc() fails, sparsemap_buf will be NULL.
* Inside sparse_buffer_alloc(), if ptr + size exceeds sparsemap_buf_end, then ptr is set to NULL.

Considering this, perhaps memmap_boot_pages_add() could be moved into __populate_section_memmap(), with the accounting done only if the operation is successful. What do you think?

Quoted 5 lines

section_activate()
populate_section_memmap()

If !CONFIG_SPARSEMEM_VMEMMAP, it just call kvmalloc_node(), which looks not counted.

Sounds right. This means nr_memmap_pages adjustment is needed for !CONFIG_SPARSEMEM_VMEMMAP here. I will recheck this.

Thank you

#4 lore raw
Quoted 18 lines · Sumanth Korikkar
Quoted 5 lines

The change here is reasonable. While maybe we still miss the counting at some other points.

For example:

Quoted 7 lines

a.

sparse_init_nid()
__populate_section_memmap()

If !CONFIG_SPARSEMEM_VMEMMAP, and sparse_buffer_alloc() return NULL, it allocate extra memory from bootmem, which looks not counted.

Currently, the accounting is done upfront in sparse_buffer_init(), where memmap_boot_pages_add() is called for !CONFIG_SPARSEMEM_VMEMMAP.

The function sparse_buffer_alloc() can return NULL in two scenarios:

Quoted 8 lines

* During sparse_buffer_init(), if memmap_alloc() fails, sparsemap_buf will be NULL.
* Inside sparse_buffer_alloc(), if ptr + size exceeds sparsemap_buf_end, then ptr is set to NULL.

Considering this, perhaps memmap_boot_pages_add() could be moved into __populate_section_memmap(), with the accounting done only if the operation is successful. What do you think?

Looks reasonable to me.

Quoted 10 lines
Quoted 5 lines

section_activate()
populate_section_memmap()

If !CONFIG_SPARSEMEM_VMEMMAP, it just call kvmalloc_node(), which looks not counted.

Sounds right. This means nr_memmap_pages adjustment is needed for !CONFIG_SPARSEMEM_VMEMMAP here. I will recheck this.

Thank you

Signature · 2 lines
Wei Yang
Help you, Help me