Skip to content

Commit 463942d

Browse files
ytcooderobherring
authored andcommitted
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: fb319e7 ("of: fdt: Add memory for devices by DT property "linux,usable-memory-range"") Fixes: 2af2b50 ("of: fdt: Add generic support for handling usable memory range property") Fixes: 8f579b1 ("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>
1 parent bec5f60 commit 463942d

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

drivers/of/fdt.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -884,23 +884,27 @@ static unsigned long chosen_node_offset = -FDT_ERR_NOTFOUND;
884884
void __init early_init_dt_check_for_usable_mem_range(void)
885885
{
886886
struct memblock_region rgn[MAX_USABLE_RANGES] = {0};
887-
const __be32 *prop, *endp;
887+
const __be32 *prop;
888888
int len, i;
889+
u64 base, size;
889890
unsigned long node = chosen_node_offset;
890891

891892
if ((long)node < 0)
892893
return;
893894

894895
pr_debug("Looking for usable-memory-range property... ");
895896

896-
prop = of_get_flat_dt_prop(node, "linux,usable-memory-range", &len);
897-
if (!prop || (len % (dt_root_addr_cells + dt_root_size_cells)))
897+
prop = of_flat_dt_get_addr_size_prop(node, "linux,usable-memory-range",
898+
&len);
899+
if (!prop)
898900
return;
899901

900-
endp = prop + (len / sizeof(__be32));
901-
for (i = 0; i < MAX_USABLE_RANGES && prop < endp; i++) {
902-
rgn[i].base = dt_mem_next_cell(dt_root_addr_cells, &prop);
903-
rgn[i].size = dt_mem_next_cell(dt_root_size_cells, &prop);
902+
len = min(len, MAX_USABLE_RANGES);
903+
904+
for (i = 0; i < len; i++) {
905+
of_flat_dt_read_addr_size(prop, i, &base, &size);
906+
rgn[i].base = base;
907+
rgn[i].size = size;
904908

905909
pr_debug("cap_mem_regions[%d]: base=%pa, size=%pa\n",
906910
i, &rgn[i].base, &rgn[i].size);

0 commit comments

Comments
 (0)