Re: [PATCH v3] cma: check for memory region overlapping

From: Robin Murphy
Date: Thu Sep 07 2023 - 14:21:57 EST


On 26/07/2023 3:28 pm, Binglei Wang wrote:
From: Binglei Wang <l3b2w1@xxxxxxxxx>

In the process of parsing the DTS, checks
whether the memory region specified by the DTS CMA node area
overlaps with the kernel text memory space reserved by memblock
before calling early_init_fdt_scan_reserved_mem.

This description bears no relation to what the code actually does. rmem_cma_setup() happens well after parsing the DTS, as it is that initial parsing process in fdt_scan_reserved_mem() which *makes* the memblock reservations in the first place. Thus, as the revert patch demonstrates, by the time we get here to start *using* the reserved region via fdt_init_reserved_mem(), it is always guaranteed to overlap a reserved region because it trivially overlaps with itself.

Furthermore, even if the bootloader does stupidly load a non-relocatable kernel into memory that it's described as reserved, the kernel text pages should not be available for CMA to allocate from - and if they were, that would be a far more fundamental bug elsewhere - so what is the exact problem you're trying to solve here?

Thanks,
Robin.

Maybe it's better to have some warning prompts printed.

Signed-off-by: Binglei Wang <l3b2w1@xxxxxxxxx>
---

Notes:
v3: fix compile error.
v2: delete the logic code for handling return -EBUSY.
v1: return -EBUSY when detect overlapping and handle the return case.

kernel/dma/contiguous.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/kernel/dma/contiguous.c b/kernel/dma/contiguous.c
index 6ea80ae42..dc6d2af1e 100644
--- a/kernel/dma/contiguous.c
+++ b/kernel/dma/contiguous.c
@@ -410,6 +410,11 @@ static int __init rmem_cma_setup(struct reserved_mem *rmem)
return -EBUSY;
}
+ if (memblock_is_region_reserved(rmem->base, rmem->size)) {
+ pr_info("Reserved memory: overlap with other memblock reserved region\n");
+ return -EBUSY;
+ }
+
if (!of_get_flat_dt_prop(node, "reusable", NULL) ||
of_get_flat_dt_prop(node, "no-map", NULL))
return -EINVAL;