* [PATCH 0/2] mm, kho: count KHO bootmem in CmaTotal and in zoneinfo's cma
@ 2026-11-29 5:01 Chris Down
2026-11-29 5:01 ` [PATCH 1/2] kho: count bootmem in zoneinfo's cma after a KHO kexec Chris Down
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Chris Down @ 2026-11-29 5:01 UTC (permalink / raw)
To: Mike Rapoport, Pasha Tatashin, Pratyush Yadav
Cc: Alexander Graf, kexec, linux-mm, linux-kernel, David Hildenbrand
KHO bootmem is handed to the buddy allocator in MIGRATE_CMA pageblocks,
so its free pages show up in CmaFree, but two of the CMA counters leave
it out. CmaTotal in /proc/meminfo never includes it, and the cma line in
/proc/zoneinfo includes it after a cold boot but goes to 0 after a KHO
kexec.
Patch 1 counts bootmem in zone->cma_pages after a KHO kexec, as
kho_init() already does on a cold boot. Patch 2 then adds bootmem to
totalcma_pages after both kinds of boot.
After a KHO kexec, part of bootmem is still held by early boot, like the
kernel image and initrd that kexec loaded there and some of the memmap.
Those pages are counted too, because they're still in MIGRATE_CMA
pageblocks and some of them come back as free CMA pages later on, such
as the init sections and memmap pages freed by hugetlb vmemmap
optimisation. Leaving them out would let CmaFree overtake CmaTotal
again.
The bootmem added to both counters comes from kho_init(), which runs long
after mem_init_print_info(), so the "Memory:" boot line doesn't change.
This came up in the thread about making bootmem visible to monitoring
[0].
[0]: https://lore.kernel.org/r/2vxz4id3wgu7.fsf@kernel.org
Chris Down (2):
kho: count bootmem in zoneinfo's cma after a KHO kexec
kho: count bootmem in CmaTotal
kernel/liveupdate/kexec_handover.c | 35 ++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
base-commit: a8d435799f3c87231bca0c245fd9e60f31e45135
--
2.56.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/2] kho: count bootmem in zoneinfo's cma after a KHO kexec
2026-11-29 5:01 [PATCH 0/2] mm, kho: count KHO bootmem in CmaTotal and in zoneinfo's cma Chris Down
@ 2026-11-29 5:01 ` Chris Down
2026-11-29 19:48 ` Mike Rapoport
2026-11-29 5:01 ` [PATCH 2/2] kho: count bootmem in CmaTotal Chris Down
2026-11-29 5:11 ` [PATCH 0/2] mm, kho: count KHO bootmem in CmaTotal and in zoneinfo's cma sashiko-bot
2 siblings, 1 reply; 6+ messages in thread
From: Chris Down @ 2026-11-29 5:01 UTC (permalink / raw)
To: Mike Rapoport, Pasha Tatashin, Pratyush Yadav
Cc: Alexander Graf, kexec, linux-mm, linux-kernel, David Hildenbrand
On a cold boot, kho_init() releases bootmem to the buddy allocator with
init_cma_reserved_pageblock(), which adds each pageblock to
zone->cma_pages.
A KHO boot takes a different path, because bootmem isn't reserved in
memblock:
1. kho_bootmem_migratetype() makes its pageblocks MIGRATE_CMA during
memmap init.
2. memblock_free_all() releases the free pages.
3. kho_init() returns before it gets to the bootmem loop.
Nothing adds bootmem to zone->cma_pages, so the cma line in
/proc/zoneinfo goes to 0 after a KHO kexec, even though the memory is
still MIGRATE_CMA and its free pages are still counted in CmaFree. For
example, with 4 GiB over two nodes and kho_bootmem=32M,512M,256M, in
pages:
cold boot after KHO kexec
Node 0, DMA32 65536 0
Node 1, DMA32 8192 0
Node 1, Normal 196608 0
zone->cma_pages is also what the auto-movable memory onlining policy
treats as movable memory, so after a KHO kexec it counts bootmem as
kernel memory instead.
Let's count the MIGRATE_CMA pageblocks in bootmem from kho_init() on a
KHO boot.
You might wonder why we don't count pageblocks as they are made
MIGRATE_CMA in pageblock_migratetype_init_range() and
__init_deferred_page(). The reason is that, with deferred struct page
init, __init_deferred_page() makes bootmem MIGRATE_CMA on a cold boot
too, where init_cma_reserved_pageblock() then counts it again.
deferred_free_pages() also runs in several padata threads at once, which
would race on the counter.
Fixes: c609c144b0e8 ("kexec: add KHO parsing support")
Signed-off-by: Chris Down <chris@chrisdown.name>
---
kernel/liveupdate/kexec_handover.c | 33 ++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c
index 38e65d210cb9..7cc87338df24 100644
--- a/kernel/liveupdate/kexec_handover.c
+++ b/kernel/liveupdate/kexec_handover.c
@@ -1860,12 +1860,45 @@ static int __init kho_kexec_metadata_init(const void *fdt)
return err;
}
+static int __init kho_cma_area_intersects(struct cma *cma, void *data)
+{
+ phys_addr_t start = *(phys_addr_t *)data;
+
+ return cma_intersects(cma, start,
+ start + pageblock_nr_pages * PAGE_SIZE - 1);
+}
+
+static void __init kho_account_incoming_bootmem(void)
+{
+ for (int i = 0; i < kho_bootmem_cnt; i++) {
+ unsigned long base_pfn = PHYS_PFN(kho_bootmem[i].addr);
+ unsigned long count = kho_bootmem[i].size >> PAGE_SHIFT;
+ unsigned long pfn;
+
+ for (pfn = base_pfn; pfn < base_pfn + count;
+ pfn += pageblock_nr_pages) {
+ struct page *page = pfn_to_page(pfn);
+ phys_addr_t start = PFN_PHYS(pfn);
+
+ if (!is_migrate_cma_page(page))
+ continue;
+ if (cma_for_each_area(kho_cma_area_intersects, &start))
+ continue;
+
+ page_zone(page)->cma_pages += pageblock_nr_pages;
+ }
+ }
+}
+
static __init int kho_init(void)
{
struct kho_radix_tree *tree = &kho_out.radix_tree;
const void *fdt = kho_get_fdt();
int err = 0;
+ if (is_kho_boot())
+ kho_account_incoming_bootmem();
+
if (!kho_enable)
return 0;
--
2.56.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/2] kho: count bootmem in CmaTotal
2026-11-29 5:01 [PATCH 0/2] mm, kho: count KHO bootmem in CmaTotal and in zoneinfo's cma Chris Down
2026-11-29 5:01 ` [PATCH 1/2] kho: count bootmem in zoneinfo's cma after a KHO kexec Chris Down
@ 2026-11-29 5:01 ` Chris Down
2026-11-29 19:50 ` Mike Rapoport
2026-11-29 5:11 ` [PATCH 0/2] mm, kho: count KHO bootmem in CmaTotal and in zoneinfo's cma sashiko-bot
2 siblings, 1 reply; 6+ messages in thread
From: Chris Down @ 2026-11-29 5:01 UTC (permalink / raw)
To: Mike Rapoport, Pasha Tatashin, Pratyush Yadav
Cc: Alexander Graf, kexec, linux-mm, linux-kernel, David Hildenbrand
/proc/meminfo shows totalcma_pages as CmaTotal, but cma_new_area(),
cma_drop_area() and cma_activate_area() only update it for their own
areas. As such, bootmem is never in CmaTotal. Bootmem pageblocks are
MIGRATE_CMA though, so their free pages are in CmaFree, and without any
other CMA areas CmaFree ends up larger than CmaTotal. For example, with
4 GiB over two nodes and kho_bootmem=32M,512M,256M, which is 1056 MiB of
bootmem:
cold boot after KHO kexec
CmaTotal 0 kB 0 kB
CmaFree 1071076 kB 988188 kB
Let's add bootmem to totalcma_pages wherever kho_init() adds it to
zone->cma_pages, that is, after init_cma_reserved_pageblock() on a cold
boot, and in kho_account_incoming_bootmem() on a KHO boot. With this
applied, CmaTotal has bootmem accounted correctly after both boots.
This happens long after mem_init_print_info(), so the "Memory:" boot
line doesn't change.
Fixes: 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers")
Signed-off-by: Chris Down <chris@chrisdown.name>
---
kernel/liveupdate/kexec_handover.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c
index 7cc87338df24..8efef4a37991 100644
--- a/kernel/liveupdate/kexec_handover.c
+++ b/kernel/liveupdate/kexec_handover.c
@@ -1886,6 +1886,7 @@ static void __init kho_account_incoming_bootmem(void)
continue;
page_zone(page)->cma_pages += pageblock_nr_pages;
+ totalcma_pages += pageblock_nr_pages;
}
}
}
@@ -1950,6 +1951,7 @@ static __init int kho_init(void)
for (pfn = base_pfn; pfn < base_pfn + count;
pfn += pageblock_nr_pages)
init_cma_reserved_pageblock(pfn_to_page(pfn));
+ totalcma_pages += count;
}
WARN_ON_ONCE(kho_debugfs_blob_add(&kho_out.dbg, "fdt",
--
2.56.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 0/2] mm, kho: count KHO bootmem in CmaTotal and in zoneinfo's cma
2026-11-29 5:01 [PATCH 0/2] mm, kho: count KHO bootmem in CmaTotal and in zoneinfo's cma Chris Down
2026-11-29 5:01 ` [PATCH 1/2] kho: count bootmem in zoneinfo's cma after a KHO kexec Chris Down
2026-11-29 5:01 ` [PATCH 2/2] kho: count bootmem in CmaTotal Chris Down
@ 2026-11-29 5:11 ` sashiko-bot
2 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-29 5:11 UTC (permalink / raw)
To: Chris Down, Mike Rapoport, Pasha Tatashin, Pratyush Yadav
Cc: Alexander Graf, kexec, David Hildenbrand
Sashiko has reviewed this 2-patch series and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795928397.git.chris@chrisdown.name
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 1/2] kho: count bootmem in zoneinfo's cma after a KHO kexec
2026-11-29 5:01 ` [PATCH 1/2] kho: count bootmem in zoneinfo's cma after a KHO kexec Chris Down
@ 2026-11-29 19:48 ` Mike Rapoport
0 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport @ 2026-11-29 19:48 UTC (permalink / raw)
To: Chris Down
Cc: Pasha Tatashin, Pratyush Yadav, Alexander Graf, kexec, linux-mm,
linux-kernel, David Hildenbrand
Hi Chris,
Overall LGTM, just a few nits below.
On Sat, Nov 28, 2026 at 09:01:48PM -0800, Chris Down wrote:
> On a cold boot, kho_init() releases bootmem to the buddy allocator with
> init_cma_reserved_pageblock(), which adds each pageblock to
> zone->cma_pages.
>
> A KHO boot takes a different path, because bootmem isn't reserved in
> memblock:
>
> 1. kho_bootmem_migratetype() makes its pageblocks MIGRATE_CMA during
> memmap init.
> 2. memblock_free_all() releases the free pages.
> 3. kho_init() returns before it gets to the bootmem loop.
I'd say it bypasses bootmem initialization.
> Nothing adds bootmem to zone->cma_pages, so the cma line in
> /proc/zoneinfo goes to 0 after a KHO kexec, even though the memory is
> still MIGRATE_CMA and its free pages are still counted in CmaFree. For
> example, with 4 GiB over two nodes and kho_bootmem=32M,512M,256M, in
> pages:
>
> cold boot after KHO kexec
> Node 0, DMA32 65536 0
> Node 1, DMA32 8192 0
> Node 1, Normal 196608 0
It would be great to see "before" and "after" tables.
> zone->cma_pages is also what the auto-movable memory onlining policy
> treats as movable memory, so after a KHO kexec it counts bootmem as
> kernel memory instead.
>
> Let's count the MIGRATE_CMA pageblocks in bootmem from kho_init() on a
> KHO boot.
>
> You might wonder why we don't count pageblocks as they are made
> MIGRATE_CMA in pageblock_migratetype_init_range() and
> __init_deferred_page(). The reason is that, with deferred struct page
> init, __init_deferred_page() makes bootmem MIGRATE_CMA on a cold boot
> too, where init_cma_reserved_pageblock() then counts it again.
> deferred_free_pages() also runs in several padata threads at once, which
> would race on the counter.
>
> Fixes: c609c144b0e8 ("kexec: add KHO parsing support")
> Signed-off-by: Chris Down <chris@chrisdown.name>
> ---
> kernel/liveupdate/kexec_handover.c | 33 ++++++++++++++++++++++++++++++
> 1 file changed, 33 insertions(+)
>
> diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c
> index 38e65d210cb9..7cc87338df24 100644
> --- a/kernel/liveupdate/kexec_handover.c
> +++ b/kernel/liveupdate/kexec_handover.c
> @@ -1860,12 +1860,45 @@ static int __init kho_kexec_metadata_init(const void *fdt)
> return err;
> }
>
> +static int __init kho_cma_area_intersects(struct cma *cma, void *data)
> +{
> + phys_addr_t start = *(phys_addr_t *)data;
> +
> + return cma_intersects(cma, start,
> + start + pageblock_nr_pages * PAGE_SIZE - 1);
> +}
> +
> +static void __init kho_account_incoming_bootmem(void)
> +{
> + for (int i = 0; i < kho_bootmem_cnt; i++) {
> + unsigned long base_pfn = PHYS_PFN(kho_bootmem[i].addr);
> + unsigned long count = kho_bootmem[i].size >> PAGE_SHIFT;
> + unsigned long pfn;
> +
> + for (pfn = base_pfn; pfn < base_pfn + count;
> + pfn += pageblock_nr_pages) {
> + struct page *page = pfn_to_page(pfn);
> + phys_addr_t start = PFN_PHYS(pfn);
> +
> + if (!is_migrate_cma_page(page))
> + continue;
> + if (cma_for_each_area(kho_cma_area_intersects, &start))
> + continue;
A curious reader may also wonder why we need this check ;-)
A short comment would be nice.
> +
> + page_zone(page)->cma_pages += pageblock_nr_pages;
> + }
> + }
> +}
> +
> static __init int kho_init(void)
> {
> struct kho_radix_tree *tree = &kho_out.radix_tree;
> const void *fdt = kho_get_fdt();
> int err = 0;
>
> + if (is_kho_boot())
> + kho_account_incoming_bootmem();
> +
> if (!kho_enable)
> return 0;
>
> --
> 2.56.1
>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 2/2] kho: count bootmem in CmaTotal
2026-11-29 5:01 ` [PATCH 2/2] kho: count bootmem in CmaTotal Chris Down
@ 2026-11-29 19:50 ` Mike Rapoport
0 siblings, 0 replies; 6+ messages in thread
From: Mike Rapoport @ 2026-11-29 19:50 UTC (permalink / raw)
To: Chris Down
Cc: Pasha Tatashin, Pratyush Yadav, Alexander Graf, kexec, linux-mm,
linux-kernel, David Hildenbrand
Hi Chris,
On Sat, Nov 28, 2026 at 09:01:51PM -0800, Chris Down wrote:
> /proc/meminfo shows totalcma_pages as CmaTotal, but cma_new_area(),
> cma_drop_area() and cma_activate_area() only update it for their own
> areas. As such, bootmem is never in CmaTotal. Bootmem pageblocks are
> MIGRATE_CMA though, so their free pages are in CmaFree, and without any
> other CMA areas CmaFree ends up larger than CmaTotal. For example, with
> 4 GiB over two nodes and kho_bootmem=32M,512M,256M, which is 1056 MiB of
> bootmem:
>
> cold boot after KHO kexec
> CmaTotal 0 kB 0 kB
> CmaFree 1071076 kB 988188 kB
Can you please add a table with counts after the change?
> Let's add bootmem to totalcma_pages wherever kho_init() adds it to
> zone->cma_pages, that is, after init_cma_reserved_pageblock() on a cold
> boot, and in kho_account_incoming_bootmem() on a KHO boot. With this
> applied, CmaTotal has bootmem accounted correctly after both boots.
> This happens long after mem_init_print_info(), so the "Memory:" boot
> line doesn't change.
>
> Fixes: 3dc92c311498 ("kexec: add Kexec HandOver (KHO) generation helpers")
> Signed-off-by: Chris Down <chris@chrisdown.name>
> ---
> kernel/liveupdate/kexec_handover.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c
> index 7cc87338df24..8efef4a37991 100644
> --- a/kernel/liveupdate/kexec_handover.c
> +++ b/kernel/liveupdate/kexec_handover.c
> @@ -1886,6 +1886,7 @@ static void __init kho_account_incoming_bootmem(void)
> continue;
>
> page_zone(page)->cma_pages += pageblock_nr_pages;
> + totalcma_pages += pageblock_nr_pages;
> }
> }
> }
> @@ -1950,6 +1951,7 @@ static __init int kho_init(void)
> for (pfn = base_pfn; pfn < base_pfn + count;
> pfn += pageblock_nr_pages)
> init_cma_reserved_pageblock(pfn_to_page(pfn));
> + totalcma_pages += count;
> }
>
> WARN_ON_ONCE(kho_debugfs_blob_add(&kho_out.dbg, "fdt",
> --
> 2.56.1
>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-29 19:50 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 5:01 [PATCH 0/2] mm, kho: count KHO bootmem in CmaTotal and in zoneinfo's cma Chris Down
2026-11-29 5:01 ` [PATCH 1/2] kho: count bootmem in zoneinfo's cma after a KHO kexec Chris Down
2026-11-29 19:48 ` Mike Rapoport
2026-11-29 5:01 ` [PATCH 2/2] kho: count bootmem in CmaTotal Chris Down
2026-11-29 19:50 ` Mike Rapoport
2026-11-29 5:11 ` [PATCH 0/2] mm, kho: count KHO bootmem in CmaTotal and in zoneinfo's cma sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox