[PATCH v3 09/11] mm/cma: centralize CMA pageblock initialization

From: Muchun Song

Date: Fri Nov 20 2026 - 05:10:26 EST


Early CMA reservations currently require callers to maintain CMA state.
This exposes internal bookkeeping requirements and splits responsibility
for the same area between CMA and its users.

Make CMA fully responsible for the state of early reservations. Pages
already handed to a caller remain owned by that caller if activation
fails, so keep their managed-page accounting intact regardless of whether
CMA retains the rest of the area.

Signed-off-by: Muchun Song <songmuchun@xxxxxxxxxxxxx>
---
v3:
- Restore managed-page accounting for early-reserved pages regardless of
CMA_RESERVE_PAGES_ON_ERROR (reported by Sashiko)
---
mm/cma.c | 30 ++++++++++++++++++------------
mm/hugetlb.c | 8 +++-----
2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/mm/cma.c b/mm/cma.c
index 61fc59fa70f8..3e4366213b49 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -174,6 +174,10 @@ static void __init cma_activate_area(struct cma *cma)
count = early_pfn[r] - cmr->base_pfn;
bitmap_count = cma_bitmap_pages_to_bits(cma, count);
bitmap_set(cmr->bitmap, 0, bitmap_count);
+
+ for (pfn = cmr->base_pfn; pfn < early_pfn[r];
+ pfn += pageblock_nr_pages)
+ init_cma_pageblock(pfn_to_page(pfn));
}

WARN_ON_ONCE(!pfn_valid(cmr->base_pfn));
@@ -199,17 +203,20 @@ static void __init cma_activate_area(struct cma *cma)
for (r = 0; r < allocrange; r++)
bitmap_free(cma->ranges[r].bitmap);

- /* Expose all pages to the buddy, they are useless for CMA. */
- if (!test_bit(CMA_RESERVE_PAGES_ON_ERROR, &cma->flags)) {
- for (r = 0; r < cma->nranges; r++) {
- unsigned long start_pfn;
+ for (r = 0; r < cma->nranges; r++) {
+ cmr = &cma->ranges[r];
+ end_pfn = r <= allocrange ? early_pfn[r] : cmr->early_pfn;
+ /* Restore managed page accounting for early-reserved pages. */
+ for (pfn = cmr->base_pfn; pfn < end_pfn; pfn++)
+ adjust_managed_page_count(pfn_to_page(pfn), 1);

- cmr = &cma->ranges[r];
- start_pfn = r <= allocrange ? early_pfn[r] : cmr->early_pfn;
- end_pfn = cmr->base_pfn + cmr->count;
- for (pfn = start_pfn; pfn < end_pfn; pfn++)
- free_reserved_page(pfn_to_page(pfn));
- }
+ if (test_bit(CMA_RESERVE_PAGES_ON_ERROR, &cma->flags))
+ continue;
+
+ /* Expose the remaining pages to the buddy. */
+ end_pfn = cmr->base_pfn + cmr->count;
+ for (; pfn < end_pfn; pfn++)
+ free_reserved_page(pfn_to_page(pfn));
}
totalcma_pages -= cma->count;
cma->available_count = cma->count = 0;
@@ -1020,8 +1027,7 @@ bool cma_intersects(struct cma *cma, unsigned long start, unsigned long end)
*
* The caller is responsible for initializing the page structures
* in the area properly, since this just points to memblock-allocated
- * memory. The caller should subsequently use init_cma_pageblock to
- * set the migrate type and CMA stats the pageblocks that were reserved.
+ * memory.
*
* If the CMA area fails to activate later, memory obtained through
* this interface is not handed to the page allocator, this is
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 6a613d9c9332..7de65b540438 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -3337,9 +3337,7 @@ static void __init hugetlb_bootmem_init_migratetype(struct folio *folio,
WARN_ON_ONCE(!pageblock_aligned(folio_pfn(folio)));

for (i = 0; i < nr_pages; i += pageblock_nr_pages) {
- if (folio_test_hugetlb_cma(folio))
- init_cma_pageblock(folio_page(folio, i));
- else
+ if (!folio_test_hugetlb_cma(folio))
init_pageblock_migratetype(folio_page(folio, i),
MIGRATE_MOVABLE, false);
}
@@ -3402,8 +3400,8 @@ static void __init gather_bootmem_prealloc_node(unsigned long nid)
* in order to fix confusing memory reports from free(1) and
* other side-effects, like CommitLimit going negative.
*
- * For CMA pages, this is done in init_cma_pageblock
- * (via hugetlb_bootmem_init_migratetype), so skip it here.
+ * For CMA pages, this is done in cma_activate_area(), so skip
+ * it here.
*/
if (hugetlb_early_cma(h))
folio_set_hugetlb_cma(folio);
--
2.54.0