of/fdt: Fix the len check in early_init_dt_check_for_usable_mem_range()

The len value is in bytes, while `dt_root_addr_cells + dt_root_size_cells`
is in cells (4 bytes per cell). Modulo calculation between them is
incorrect, the units must be converted first.

Use helper functions to simplify the code and fix this issue.

Fixes: fb319e77a0 ("of: fdt: Add memory for devices by DT property "linux,usable-memory-range"")
Fixes: 2af2b50acf ("of: fdt: Add generic support for handling usable memory range property")
Fixes: 8f579b1c4e ("arm64: limit memory regions based on DT property, usable-memory-range")
Signed-off-by: Yuntao Wang <yuntao.wang@linux.dev>
Link: https://patch.msgid.link/20251115134753.179931-4-yuntao.wang@linux.dev
Signed-off-by: Rob Herring (Arm) <robh@kernel.org>
This commit is contained in:
Yuntao Wang 2025-11-15 21:47:48 +08:00 committed by Rob Herring (Arm)
parent bec5f6092b
commit 463942de13
1 changed files with 11 additions and 7 deletions

View File

@ -884,8 +884,9 @@ static unsigned long chosen_node_offset = -FDT_ERR_NOTFOUND;
void __init early_init_dt_check_for_usable_mem_range(void) void __init early_init_dt_check_for_usable_mem_range(void)
{ {
struct memblock_region rgn[MAX_USABLE_RANGES] = {0}; struct memblock_region rgn[MAX_USABLE_RANGES] = {0};
const __be32 *prop, *endp; const __be32 *prop;
int len, i; int len, i;
u64 base, size;
unsigned long node = chosen_node_offset; unsigned long node = chosen_node_offset;
if ((long)node < 0) if ((long)node < 0)
@ -893,14 +894,17 @@ void __init early_init_dt_check_for_usable_mem_range(void)
pr_debug("Looking for usable-memory-range property... "); pr_debug("Looking for usable-memory-range property... ");
prop = of_get_flat_dt_prop(node, "linux,usable-memory-range", &len); prop = of_flat_dt_get_addr_size_prop(node, "linux,usable-memory-range",
if (!prop || (len % (dt_root_addr_cells + dt_root_size_cells))) &len);
if (!prop)
return; return;
endp = prop + (len / sizeof(__be32)); len = min(len, MAX_USABLE_RANGES);
for (i = 0; i < MAX_USABLE_RANGES && prop < endp; i++) {
rgn[i].base = dt_mem_next_cell(dt_root_addr_cells, &prop); for (i = 0; i < len; i++) {
rgn[i].size = dt_mem_next_cell(dt_root_size_cells, &prop); of_flat_dt_read_addr_size(prop, i, &base, &size);
rgn[i].base = base;
rgn[i].size = size;
pr_debug("cap_mem_regions[%d]: base=%pa, size=%pa\n", pr_debug("cap_mem_regions[%d]: base=%pa, size=%pa\n",
i, &rgn[i].base, &rgn[i].size); i, &rgn[i].base, &rgn[i].size);