[PATCH 01/11] of: reserved_mem: fix region count for nodes with multiple reg entries

Chen Wandun
2026-04-29T06:58:54Z

When a reserved-memory node contains multiple reg entries (e.g.,
reg = <base1 size1>, <base2 size2>), the count used for
total_reserved_mem_cnt is wrong in two places:

1) __reserved_mem_reserve_reg() returns 0 on success regardless of how
many regions it reserved in memblock. The caller in
fdt_scan_reserved_mem() then increments count by just 1.

2) fdt_scan_reserved_mem_late() uses of_flat_dt_get_addr_size() which
only reads the first reg entry. Subsequent entries are never
initialized via fdt_init_reserved_mem_node(), so their metadata is
lost.

Fix both issues:
- Make __reserved_mem_reserve_reg() return the actual number of
regions successfully reserved. Update the caller to accumulate
the returned count.
- Rewrite fdt_scan_reserved_mem_late() to use
of_flat_dt_get_addr_size_prop() and iterate all reg entries,
initializing each one via fdt_init_reserved_mem_node().

Fixes: 8a6e02d0c00e ("of: reserved_mem: Restructure how the reserved memory regions are processed")
Fixes: 00c9a452a235 ("of: reserved_mem: Add code to dynamically allocate reserved_mem array")
Signed-off-by: Chen Wandun <chenwandun@lixiang.com>
Tested-by: Zhao Meijing <zhaomeijing@lixiang.com>
---
drivers/of/of_reserved_mem.c | 37 +++++++++++++++++++++++-------------
1 file changed, 24 insertions(+), 13 deletions(-)

diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 8d5777cb5d1b..9d1b0193864c 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -129,6 +129,8 @@ static int __init early_init_dt_reserve_memory(phys_addr_t base,

/*
* __reserved_mem_reserve_reg() - reserve all memory described in 'reg' property
+ *
+ * Returns: number of regions successfully reserved, or negative error code
*/
static int __init __reserved_mem_reserve_reg(unsigned long node,
const char *uname)
@@ -137,6 +139,7 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
int i, len, err;
const __be32 *prop;
bool nomap;
+ int reserved_count = 0;

prop = of_flat_dt_get_addr_size_prop(node, "reg", &len);
if (!prop)
@@ -160,12 +163,13 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
fdt_fixup_reserved_mem_node(node, base, size);
pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
uname, &base, (unsigned long)(size / SZ_1M));
+ reserved_count++;
} else {
pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
uname, &base, (unsigned long)(size / SZ_1M));
}
}
- return 0;
+ return reserved_count;
}

/*
@@ -275,25 +279,32 @@ void __init fdt_scan_reserved_mem_late(void)

fdt_for_each_subnode(child, fdt, node) {
const char *uname;
- u64 b, s;
+ int i, len;
+ const __be32 *prop;
int ret;

if (!of_fdt_device_is_available(fdt, child))
continue;

- if (!of_flat_dt_get_addr_size(child, "reg", &b, &s))
+ prop = of_flat_dt_get_addr_size_prop(child, "reg", &len);
+ if (!prop)
continue;

ret = fdt_validate_reserved_mem_node(child, NULL);
if (ret && ret != -ENODEV)
continue;

- base = b;
- size = s;
+ uname = fdt_get_name(fdt, child, NULL);
+ for (i = 0; i < len; i++) {
+ u64 b, s;

- if (size) {
- uname = fdt_get_name(fdt, child, NULL);
- fdt_init_reserved_mem_node(child, uname, base, size);
+ of_flat_dt_read_addr_size(prop, i, &b, &s);
+
+ base = b;
+ size = s;
+
+ if (size)
+ fdt_init_reserved_mem_node(child, uname, base, size);
}
}

@@ -331,16 +342,16 @@ int __init fdt_scan_reserved_mem(void)

fdt_for_each_subnode(child, fdt, node) {
const char *uname;
- int err;
+ int ret;

if (!of_fdt_device_is_available(fdt, child))
continue;

uname = fdt_get_name(fdt, child, NULL);

- err = __reserved_mem_reserve_reg(child, uname);
- if (!err)
- count++;
+ ret = __reserved_mem_reserve_reg(child, uname);
+ if (ret > 0)
+ count += ret;
/*
* Save the nodes for the dynamically-placed regions
* into an array which will be used for allocation right
@@ -348,7 +359,7 @@ int __init fdt_scan_reserved_mem(void)
* or marked as no-map. This is done to avoid dynamically
* allocating from one of the statically-placed regions.
*/
- if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) {
+ if (ret == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) {
dynamic_nodes[dynamic_nodes_cnt] = child;
dynamic_nodes_cnt++;
}
--
2.43.0