[PATCH v2 2/2] mm/cma: do not count skipped ranges in cma_declare_contiguous_multi()
From: Ivy Lopez
Date: Thu Oct 15 2026 - 16:49:34 EST
cma_declare_contiguous_multi() adds the size of every candidate range
to sizesum before deciding whether to record it. Once CMA_MAX_RANGES
ranges are recorded, a candidate smaller than the last recorded one is
skipped, but its size stays in sizesum. The replacement path already
subtracts the evicted range, so sizesum is meant to track recorded
ranges only.
When more than CMA_MAX_RANGES ranges exist and one is skipped, sizesum
can reach total_size while the recorded ranges cannot. The sizesum <
total_size check then passes, the reserve loop ends with sizeleft
nonzero, and the function returns 0 with fewer pages reserved than
requested. cma->count, available_count and totalcma_pages still
reflect the full size, and the final message reports it as reserved.
For example, 8 free ranges of 1G followed by one of 512M with a
request of 8G + 256M reserves 8G and returns 0.
Only count a range once it is recorded.
Found by code inspection with the help of an LLM. No kernel-level
reproducer was produced and this was not tested on hardware or in a
booted kernel. The example above comes from a userspace copy of the
selection loop, written from the source.
Fixes: c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested")
Cc: <stable@xxxxxxxxxxxxxxx> # 6.15+
Assisted-by: LLM
Signed-off-by: Ivy Lopez <skunkolee@xxxxxxxxx>
---
Resend: the first attempt of this patch was blocked by Gmail and was
not delivered to any recipient.
mm/cma.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mm/cma.c b/mm/cma.c
index 527b2d894f3e..c2cdef953ee9 100644
--- a/mm/cma.c
+++ b/mm/cma.c
@@ -594,7 +594,6 @@ int __init cma_declare_contiguous_multi(phys_addr_t total_size,
size = ALIGN_DOWN(size, (PAGE_SIZE << order_per_bit));
if (!size)
continue;
- sizesum += size;
pr_debug("consider %016llx - %016llx\n", (u64)start, (u64)end);
@@ -620,6 +619,7 @@ int __init cma_declare_contiguous_multi(phys_addr_t total_size,
}
mrp->base = start;
mrp->size = size;
+ sizesum += size;
/*
* Now do a sorted insert.
--
2.56.0