Re: [PATCH v2] mm/memblock: Correct totalram_pages accounting with KMSAN

From: SeongJae Park
Date: Thu Sep 25 2025 - 08:39:12 EST


Hello,

On Wed, 24 Sep 2025 12:03:01 +0200 Alexander Potapenko <glider@xxxxxxxxxx> wrote:

> 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 <nogikh@xxxxxxxxxx>
> Fixes: 3c2065098260 ("init: kmsan: call KMSAN initialization routines")
> Signed-off-by: Alexander Potapenko <glider@xxxxxxxxxx>
> Reviewed-by: David Hildenbrand <david@xxxxxxxxxx>
[...]
> --- a/mm/mm_init.c
> +++ b/mm/mm_init.c
> @@ -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;
> }

I found this patch on mm-new tree is making my test machine (QEMU) reports much
less MemTotal even though KMSAN is disabled. And modifying the above part to
be considered as free success (returning '1UL << order') fixed my issue.
Because the commit message says the purpose of this change is only for
KMSAN-stashed memory, maybe the above behavior change is not really intended?

I'm not familiar with this code so I'm unsure if the workaround is the right
fix. But since I have no time to look this in deep for now, reporting first.

>
> if (!kmsan_memblock_free_pages(page, order)) {
> /* KMSAN will take care of these pages. */
> - return;
> + return 0;
> }

I understand this part is the intended change, of course.


Thanks,
SJ

[...]