[PATCH 3/5] kexec_file: Honor the segment alignment for CMA allocations

From: Han Gao via B4 Relay

Date: Fri Oct 23 2026 - 03:39:46 EST


From: Han Gao <gaohan@xxxxxxxxxxx>

dma_alloc_from_contiguous() limits the requested alignment to
CONFIG_CMA_ALIGNMENT without reporting it. kexec_alloc_contig() can
therefore place a segment at a less aligned address than buf_align. On
RISC-V with 4 KiB pages, the default 1 MiB limit is below the 2 MiB
kernel alignment, and the new kernel stops at the setup_vm() BUG_ON for
a misaligned physical address.

Release such a block and fall back to the regular memory hole search,
which honors the alignment.

Fixes: 07d24902977e ("kexec: enable CMA based contiguous allocation")
Signed-off-by: Han Gao <gaohan@xxxxxxxxxxx>
---
kernel/kexec_file.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c
index 59fb9d71e9d8..6ce10e01414e 100644
--- a/kernel/kexec_file.c
+++ b/kernel/kexec_file.c
@@ -690,6 +690,13 @@ static int kexec_alloc_contig(struct kexec_buf *kbuf)

mem = page_to_boot_pfn(p) << PAGE_SHIFT;

+ /* dma_alloc_from_contiguous() caps the alignment at CONFIG_CMA_ALIGNMENT. */
+ if (!IS_ALIGNED(mem, kbuf->buf_align)) {
+ pr_debug("CMA block 0x%lx is not aligned to 0x%lx\n", mem, kbuf->buf_align);
+ dma_release_from_contiguous(NULL, p, nr_pages);
+ return -EINVAL;
+ }
+
if (kimage_is_destination_range(kbuf->image, mem, mem + kbuf->memsz)) {
/* Our region is already in use by a statically defined one. Bail out. */
pr_debug("CMA overlaps existing mem: 0x%lx+0x%lx\n", mem, kbuf->memsz);

--
2.55.0