Skip to contentSkip to conversation

mm/memblock: Correct totalram_pages accounting with KMSAN

PATCH Superseded Alexander Potapenko 3 participants 4 messages Sep 17, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/memblock: Correct totalram_pages accounting with KMSAN

When KMSAN is enabled, kmsan_memblock_free_pages() can hold back pages for metadata instead of returning them to the early allocator. The callers, however, would unconditionally increment totalram_pages, assuming the pages were always freed. This resulted in an incorrect calculation of the total available RAM, causing the kernel to believe it had more memory than it actually did.

This patch refactors memblock_free_pages() to return the number of pages it successfully frees. If KMSAN stashes the pages, the function now returns 0; otherwise, it returns the number of pages in the block.

The callers in memblock.c have been updated to use this return value, ensuring that totalram_pages is incremented only by the number of pages actually returned to the allocator. This corrects the total RAM accounting when KMSAN is active.

Cc Aleksandr Nogikh <[email protected]>
Fixes 3c2065098260 ("init: kmsan: call KMSAN initialization routines")
---
 mm/internal.h |  4 ++--
 mm/memblock.c | 18 +++++++++---------
 mm/mm_init.c  |  9 +++++----
 3 files changed, 16 insertions(+), 15 deletions(-)
mm/internal.h +2 -2
index 45b725c3dc030..ae1ee6e02eff9 100644
@@ -742,8 +742,8 @@ static inline void clear_zone_contiguous(struct zone *zone)
 extern int __isolate_free_page(struct page *page, unsigned int order);
 extern void __putback_isolated_page(struct page *page, unsigned int order,
 				    int mt);
-extern void memblock_free_pages(struct page *page, unsigned long pfn,
-					unsigned int order);
+extern unsigned long memblock_free_pages(struct page *page, unsigned long pfn,
+					 unsigned int order);
 extern void __free_pages_core(struct page *page, unsigned int order,
 		enum meminit_context context);
 
mm/memblock.c +9 -9
index 117d963e677c9..de7ff644d8f4f 100644
@@ -1834,10 +1834,9 @@ void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
 	cursor = PFN_UP(base);
 	end = PFN_DOWN(base + size);
 
-	for (; cursor < end; cursor++) {
-		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
-		totalram_pages_inc();
-	}
+	for (; cursor < end; cursor++)
+		totalram_pages_add(
+			memblock_free_pages(pfn_to_page(cursor), cursor, 0));
 }
 
 /*
@@ -2259,9 +2258,11 @@ static void __init free_unused_memmap(void)
 #endif
 }
 
-static void __init __free_pages_memory(unsigned long start, unsigned long end)
+static unsigned long __init __free_pages_memory(unsigned long start,
+						unsigned long end)
 {
 	int order;
+	unsigned long freed = 0;
 
 	while (start < end) {
 		/*
@@ -2279,10 +2280,11 @@ static void __init __free_pages_memory(unsigned long start, unsigned long end)
 		while (start + (1UL << order) > end)
 			order--;
 
-		memblock_free_pages(pfn_to_page(start), start, order);
+		freed += memblock_free_pages(pfn_to_page(start), start, order);
 
 		start += (1UL << order);
 	}
+	return freed;
 }
 
 static unsigned long __init __free_memory_core(phys_addr_t start,
@@ -2297,9 +2299,7 @@ static unsigned long __init __free_memory_core(phys_addr_t start,
 	if (start_pfn >= end_pfn)
 		return 0;
 
-	__free_pages_memory(start_pfn, end_pfn);
-
-	return end_pfn - start_pfn;
+	return __free_pages_memory(start_pfn, end_pfn);
 }
 
 static void __init memmap_init_reserved_pages(void)
mm/mm_init.c +5 -4
index 5c21b3af216b2..9883612768511 100644
@@ -2548,24 +2548,25 @@ void *__init alloc_large_system_hash(const char *tablename,
 	return table;
 }
 
-void __init memblock_free_pages(struct page *page, unsigned long pfn,
-							unsigned int order)
+unsigned long __init memblock_free_pages(struct page *page, unsigned long pfn,
+					 unsigned int order)
 {
 	if (IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT)) {
 		int nid = early_pfn_to_nid(pfn);
 
 		if (!early_page_initialised(pfn, nid))
-			return;
+			return 0;
 	}
 
 	if (!kmsan_memblock_free_pages(page, order)) {
 		/* KMSAN will take care of these pages. */
-		return;
+		return 0;
 	}
 
 	/* pages were reserved and not allocated */
 	clear_page_tag_ref(page);
 	__free_pages_core(page, order, MEMINIT_EARLY);
+	return 1UL << order;
 }
 
 DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc);
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 46 lines · Alexander Potapenko

When KMSAN is enabled, kmsan_memblock_free_pages() can hold back pages for metadata instead of returning them to the early allocator. The callers, however, would unconditionally increment totalram_pages, assuming the pages were always freed. This resulted in an incorrect calculation of the total available RAM, causing the kernel to believe it had more memory than it actually did.

This patch refactors memblock_free_pages() to return the number of pages it successfully frees. If KMSAN stashes the pages, the function now returns 0; otherwise, it returns the number of pages in the block.

The callers in memblock.c have been updated to use this return value, ensuring that totalram_pages is incremented only by the number of pages actually returned to the allocator. This corrects the total RAM accounting when KMSAN is active.

Cc Aleksandr Nogikh <[email protected]>
Fixes 3c2065098260 ("init: kmsan: call KMSAN initialization routines")
---
  mm/internal.h |  4 ++--
  mm/memblock.c | 18 +++++++++---------
  mm/mm_init.c  |  9 +++++----
  3 files changed, 16 insertions(+), 15 deletions(-)
mm/internal.h +2 -2
index 45b725c3dc030..ae1ee6e02eff9 100644
@@ -742,8 +742,8 @@ static inline void clear_zone_contiguous(struct zone *zone)
  extern int __isolate_free_page(struct page *page, unsigned int order);
  extern void __putback_isolated_page(struct page *page, unsigned int order,
  				    int mt);
-extern void memblock_free_pages(struct page *page, unsigned long pfn,
-					unsigned int order);
+extern unsigned long memblock_free_pages(struct page *page, unsigned long pfn,
+					 unsigned int order);
  extern void __free_pages_core(struct page *page, unsigned int order,
  		enum meminit_context context);
 
mm/memblock.c +0 -0
index 117d963e677c9..de7ff644d8f4f 100644
@@ -1834,10 +1834,9 @@ void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
  	cursor = PFN_UP(base);

mm/memblock.c void __init memblock_free_late(phys_addr_t base, phys_addr_t size) · line 1840

Quoted 10 lines
mm/memblock.c +3 -4
  	end = PFN_DOWN(base + size);
 
-	for (; cursor < end; cursor++) {
-		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
-		totalram_pages_inc();
-	}
+	for (; cursor < end; cursor++)
+		totalram_pages_add(
+			memblock_free_pages(pfn_to_page(cursor), cursor, 0));
  }

That part is clear. But for readability we should probably just do

if (memblock_free_pages(pfn_to_page(cursor), cursor, 0))

	totalram_pages_inc();

Or use a temp variable as an alternative.

LGTM

Signature · 3 lines
Cheers

David / dhildenb
maintainer
#2 lore raw
Quoted 58 lines · David Hildenbrand
Quoted 44 lines · Alexander Potapenko

When KMSAN is enabled, kmsan_memblock_free_pages() can hold back pages for metadata instead of returning them to the early allocator. The callers, however, would unconditionally increment totalram_pages, assuming the pages were always freed. This resulted in an incorrect calculation of the total available RAM, causing the kernel to believe it had more memory than it actually did.

This patch refactors memblock_free_pages() to return the number of pages it successfully frees. If KMSAN stashes the pages, the function now returns 0; otherwise, it returns the number of pages in the block.

The callers in memblock.c have been updated to use this return value, ensuring that totalram_pages is incremented only by the number of pages actually returned to the allocator. This corrects the total RAM accounting when KMSAN is active.

Cc Aleksandr Nogikh <[email protected]>
Fixes 3c2065098260 ("init: kmsan: call KMSAN initialization routines")
---
  mm/internal.h |  4 ++--
  mm/memblock.c | 18 +++++++++---------
  mm/mm_init.c  |  9 +++++----
  3 files changed, 16 insertions(+), 15 deletions(-)
mm/internal.h +2 -2
index 45b725c3dc030..ae1ee6e02eff9 100644
@@ -742,8 +742,8 @@ static inline void clear_zone_contiguous(struct zone *zone)
  extern int __isolate_free_page(struct page *page, unsigned int order);
  extern void __putback_isolated_page(struct page *page, unsigned int order,
  				    int mt);
-extern void memblock_free_pages(struct page *page, unsigned long pfn,
-					unsigned int order);
+extern unsigned long memblock_free_pages(struct page *page, unsigned long pfn,
+					 unsigned int order);
  extern void __free_pages_core(struct page *page, unsigned int order,
  		enum meminit_context context);
mm/memblock.c +0 -0
index 117d963e677c9..de7ff644d8f4f 100644

mm/memblock.c void __init memblock_free_late(phys_addr_t base, phys_addr_t size) · line 1839

Quoted 10 lines
mm/memblock.c +3 -4
@@ -1834,10 +1834,9 @@ void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
  	cursor = PFN_UP(base);
  	end = PFN_DOWN(base + size);
-	for (; cursor < end; cursor++) {
-		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
-		totalram_pages_inc();
-	}
+	for (; cursor < end; cursor++)
+		totalram_pages_add(
+			memblock_free_pages(pfn_to_page(cursor), cursor, 0));
  }

That part is clear. But for readability we should probably just do

Quoted 4 lines

if (memblock_free_pages(pfn_to_page(cursor), cursor, 0))

	totalram_pages_inc();

Or use a temp variable as an alternative.

I prefer this one and totalram_pages_add() after the loop

Quoted 9 lines

LGTM

Signature · 3 lines
Cheers

David / dhildenb
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#3 lore raw
In reply to Alexander Potapenko
Quoted 30 lines · Alexander Potapenko

When KMSAN is enabled, kmsan_memblock_free_pages() can hold back pages for metadata instead of returning them to the early allocator. The callers, however, would unconditionally increment totalram_pages, assuming the pages were always freed. This resulted in an incorrect calculation of the total available RAM, causing the kernel to believe it had more memory than it actually did.

This patch refactors memblock_free_pages() to return the number of pages it successfully frees. If KMSAN stashes the pages, the function now returns 0; otherwise, it returns the number of pages in the block.

The callers in memblock.c have been updated to use this return value, ensuring that totalram_pages is incremented only by the number of pages actually returned to the allocator. This corrects the total RAM accounting when KMSAN is active.

Cc Aleksandr Nogikh <[email protected]>
Fixes 3c2065098260 ("init: kmsan: call KMSAN initialization routines")
---
 mm/internal.h |  4 ++--
 mm/memblock.c | 18 +++++++++---------
 mm/mm_init.c  |  9 +++++----
 3 files changed, 16 insertions(+), 15 deletions(-)
mm/internal.h +0 -0
index 45b725c3dc030..ae1ee6e02eff9 100644

mm/internal.h static inline void clear_zone_contiguous(struct zone *zone) · line 746

Quoted 7 lines
mm/internal.h +2 -2
@@ -742,8 +742,8 @@ static inline void clear_zone_contiguous(struct zone *zone)
 extern int __isolate_free_page(struct page *page, unsigned int order);
 extern void __putback_isolated_page(struct page *page, unsigned int order,
 				    int mt);
-extern void memblock_free_pages(struct page *page, unsigned long pfn,
-					unsigned int order);
+extern unsigned long memblock_free_pages(struct page *page, unsigned long pfn,
+					 unsigned int order);

No need for extern, the inconsistency is fine here.

Quoted 63 lines
mm/internal.h +0 -0
 extern void __free_pages_core(struct page *page, unsigned int order,
 		enum meminit_context context);
 
mm/memblock.c +9 -9
index 117d963e677c9..de7ff644d8f4f 100644
@@ -1834,10 +1834,9 @@ void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
 	cursor = PFN_UP(base);
 	end = PFN_DOWN(base + size);
 
-	for (; cursor < end; cursor++) {
-		memblock_free_pages(pfn_to_page(cursor), cursor, 0);
-		totalram_pages_inc();
-	}
+	for (; cursor < end; cursor++)
+		totalram_pages_add(
+			memblock_free_pages(pfn_to_page(cursor), cursor, 0));
 }
 
 /*
@@ -2259,9 +2258,11 @@ static void __init free_unused_memmap(void)
 #endif
 }
 
-static void __init __free_pages_memory(unsigned long start, unsigned long end)
+static unsigned long __init __free_pages_memory(unsigned long start,
+						unsigned long end)
 {
 	int order;
+	unsigned long freed = 0;
 
 	while (start < end) {
 		/*
@@ -2279,10 +2280,11 @@ static void __init __free_pages_memory(unsigned long start, unsigned long end)
 		while (start + (1UL << order) > end)
 			order--;
 
-		memblock_free_pages(pfn_to_page(start), start, order);
+		freed += memblock_free_pages(pfn_to_page(start), start, order);
 
 		start += (1UL << order);
 	}
+	return freed;
 }
 
 static unsigned long __init __free_memory_core(phys_addr_t start,
@@ -2297,9 +2299,7 @@ static unsigned long __init __free_memory_core(phys_addr_t start,
 	if (start_pfn >= end_pfn)
 		return 0;
 
-	__free_pages_memory(start_pfn, end_pfn);
-
-	return end_pfn - start_pfn;
+	return __free_pages_memory(start_pfn, end_pfn);
 }
 
 static void __init memmap_init_reserved_pages(void)
mm/mm_init.c +0 -0
index 5c21b3af216b2..9883612768511 100644

mm/mm_init.c void *__init alloc_large_system_hash(const char *tablename, · line 2552

Quoted 7 lines
mm/mm_init.c +2 -2
@@ -2548,24 +2548,25 @@ void *__init alloc_large_system_hash(const char *tablename,
 	return table;
 }
 
-void __init memblock_free_pages(struct page *page, unsigned long pfn,
-							unsigned int order)
+unsigned long __init memblock_free_pages(struct page *page, unsigned long pfn,
+					 unsigned int order)

Please either align this with 'struct' or drop spaces and keep only tabs.

Quoted 22 lines
mm/mm_init.c +3 -2
 {
 	if (IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT)) {
 		int nid = early_pfn_to_nid(pfn);
 
 		if (!early_page_initialised(pfn, nid))
-			return;
+			return 0;
 	}
 
 	if (!kmsan_memblock_free_pages(page, order)) {
 		/* KMSAN will take care of these pages. */
-		return;
+		return 0;
 	}
 
 	/* pages were reserved and not allocated */
 	clear_page_tag_ref(page);
 	__free_pages_core(page, order, MEMINIT_EARLY);
+	return 1UL << order;
 }
 
 DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc);
Signature · 2 lines
Sincerely yours,
Mike.