Skip to contentSkip to conversation

of: reserved_mem: fixes and cleanups

PATCH In review Wandun Chen 4 participants 12 messages May 19 – Jun 2 Device tree

lore
Share & tools
lore
#0 lore raw

v2 cover of: reserved_mem: fixes and cleanups

This is v2 of the OF reserved memory fixes/cleanups series.

The original v1 [1] contains two parts of work:
- Bug fixes and small cleanups about reserved memory.
- A vmcore size optimization that exclude reserved memory out of vmcore.

For the convenience of review, I have split it into two independent patchsets. This patchset only contains bug fixes and cleanups content, while the content optimized for vmcore size will be in another patchset.

v1 --> v2:
1. Add new patch 1, to fix unnecessary memory allocation.
2. Support only one entry in reg property in patch 3, suggested by Rob Herring [2].
3. Per Rob's review on v1 2/11 [3], drop patch about "rejecting reserved memory outside physical memory range" due to potential abuse.
4. Drop patch about "of: reserved_mem: avoid unconditional save of reg entries in fdt_scan_reserved_mem_late()" due to a misunderstanding on my side.
5. Update commit message.

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/T/#m29fa0f1c22c23e6343070e70f905c9482f930901
[3] https://lore.kernel.org/all/[email protected]/T/#m5948f8679540fb361e7f432137babb4609ff154f

Wandun Chen (3):
of: reserved_mem: avoid unnecessary memory allocation when
__reserved_mem_check_root() fails
of: reserved_mem: clean up redundant alloc_reserved_mem_array() call
of: reserved_mem: only support one <base size> entry in reg property

 drivers/of/of_reserved_mem.c | 48 +++++++++++++++++++++---------------
 1 file changed, 28 insertions(+), 20 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails

total_reserved_mem_cnt will keep the init value (MAX_RESERVED_REGIONS) in fdt_scan_reserved_mem() if __reserved_mem_check_root() fails.

fdt_scan_reserved_mem_late() calls alloc_reserved_mem_array() to allocate memory according to total_reserved_mem_cnt before __reserved_mem_check_root(), so if __reserved_mem_check_root() fails, the allocated array has nowhere to be used, so no need to allocate memory.

Move alloc_reserved_mem_array() after __reserved_mem_check_root() to avoid potential memory waste.

---
 drivers/of/of_reserved_mem.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/of/of_reserved_mem.c +3 -3
index 8d5777cb5d1b..7856dc857d65 100644
@@ -265,14 +265,14 @@ void __init fdt_scan_reserved_mem_late(void)
 		return;
 	}
 
-	/* Attempt dynamic allocation of a new reserved_mem array */
-	alloc_reserved_mem_array();
-
 	if (__reserved_mem_check_root(node)) {
 		pr_err("Reserved memory: unsupported node format, ignoring\n");
 		return;
 	}
 
+	/* Attempt dynamic allocation of a new reserved_mem array */
+	alloc_reserved_mem_array();
+
 	fdt_for_each_subnode(child, fdt, node) {
 		const char *uname;
 		u64 b, s;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Error paths in alloc_reserved_mem_array() fail to reset total_reserved_mem_cnt, causing a…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Error paths in alloc_reserved_mem_array() fail to reset total_reserved_mem_cnt, causing a deterministic buffer overflow. --

commit b6927a2d1eca00b0e1f7c1ca6f640812391b20c8
Author: Wandun Chen <[email protected]>

of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails

This commit delays the dynamic allocation of the reserved memory array until after checking the root node format. It aims to prevent memory allocation and potential waste if the reserved memory root format is unsupported.

Quoted 4 lines
drivers/of/of_reserved_mem.c +0 -0
@@ -265,14 +265,14 @@ void __init fdt_scan_reserved_mem_late(void)

[ ... ]

drivers/of/of_reserved_mem.c void __init fdt_scan_reserved_mem_late(void) · line 274

Quoted 7 lines
drivers/of/of_reserved_mem.c +2 -0
 	if (__reserved_mem_check_root(node)) {
 		pr_err("Reserved memory: unsupported node format, ignoring\n");
 		return;
 	}
 
+	/* Attempt dynamic allocation of a new reserved_mem array */
+	alloc_reserved_mem_array();

This isn't a bug introduced by this commit, but while looking at the relocation of alloc_reserved_mem_array(), I noticed a pre-existing flaw in its error paths.

If total_reserved_mem_cnt exceeds MAX_RESERVED_REGIONS (64), we attempt to allocate a new array. If memblock_alloc() fails or alloc_size == SIZE_MAX, alloc_reserved_mem_array() logs an error and returns early without resetting total_reserved_mem_cnt back to MAX_RESERVED_REGIONS.

Since alloc_reserved_mem_array() returns void and doesn't propagate the error, fdt_scan_reserved_mem_late() continues iterating over the remaining nodes and calling fdt_init_reserved_mem_node().

Does this lead to an out-of-bounds write?

In fdt_init_reserved_mem_node(), the bounds check compares against the active total_reserved_mem_cnt:

drivers/of/of_reserved_mem.c:fdt_init_reserved_mem_node() {
    ...
    if (reserved_mem_count == total_reserved_mem_cnt) {
        pr_err("not enough space for all defined regions.\n");
        return -ENOSPC;
    }
    ...
}

If alloc_reserved_mem_array() failed, reserved_mem still points to the static 64-element array, but total_reserved_mem_cnt is still set to the larger requested size. This appears to allow reserved_mem_count to exceed 64, deterministically writing past the end of the static array.

Could the error paths in alloc_reserved_mem_array() be updated to always reset total_reserved_mem_cnt to MAX_RESERVED_REGIONS?

Quoted 4 lines
drivers/of/of_reserved_mem.c +1 -0
+
 	fdt_for_each_subnode(child, fdt, node) {
 		const char *uname;
 		u64 b, s;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails

In reply to Wandun Chen
Quoted 9 lines · Wandun Chen

total_reserved_mem_cnt will keep the init value (MAX_RESERVED_REGIONS) in fdt_scan_reserved_mem() if __reserved_mem_check_root() fails.

fdt_scan_reserved_mem_late() calls alloc_reserved_mem_array() to allocate memory according to total_reserved_mem_cnt before __reserved_mem_check_root(), so if __reserved_mem_check_root() fails, the allocated array has nowhere to be used, so no need to allocate memory.

Quoted 4 lines

Move alloc_reserved_mem_array() after __reserved_mem_check_root() to avoid potential memory waste.

Authorship/DCO mismatch.

Quoted 1 line

Best regards,
Krzysztof

author
#4 lore raw

Re: [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails

Quoted 10 lines · Krzysztof Kozlowski
Quoted 9 lines · Wandun Chen

total_reserved_mem_cnt will keep the init value (MAX_RESERVED_REGIONS) in fdt_scan_reserved_mem() if __reserved_mem_check_root() fails.

fdt_scan_reserved_mem_late() calls alloc_reserved_mem_array() to allocate memory according to total_reserved_mem_cnt before __reserved_mem_check_root(), so if __reserved_mem_check_root() fails, the allocated array has nowhere to be used, so no need to allocate memory.

Quoted 5 lines
Quoted 4 lines

Move alloc_reserved_mem_array() after __reserved_mem_check_root() to avoid potential memory waste.

Authorship/DCO mismatch.

Hi Krzysztof,

Thanks for your review.
Sorry for the mismatch. Dueto my company email server's issues with community mailing lists, I sent the patch via my personal email. I will add the correct 'From:' header and send a v3 patch shortly.

Best regards,
Wandun

Quoted 4 lines
Quoted 1 line

Best regards,
Krzysztof

maintainer
#5 lore raw

Re: [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails

Quoted 16 lines · Wandun
Quoted 13 lines · Krzysztof Kozlowski
Quoted 12 lines · Wandun Chen

total_reserved_mem_cnt will keep the init value (MAX_RESERVED_REGIONS) in fdt_scan_reserved_mem() if __reserved_mem_check_root() fails.

fdt_scan_reserved_mem_late() calls alloc_reserved_mem_array() to allocate memory according to total_reserved_mem_cnt before __reserved_mem_check_root(), so if __reserved_mem_check_root() fails, the allocated array has nowhere to be used, so no need to allocate memory.

Move alloc_reserved_mem_array() after __reserved_mem_check_root() to avoid potential memory waste.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

Authorship/DCO mismatch.

Hi Krzysztof,

Thanks for your review.
Sorry for the mismatch. Dueto my company email server's issues with community mailing lists, I sent the patch via my personal email. I will add the correct 'From:' header and send a v3 patch shortly.

There is also the 'b4 relay' that can be used for this kind of issue.

Rob

author
#6 lore raw

Re: [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails

Quoted 17 lines · Rob Herring
Quoted 16 lines · Wandun
Quoted 14 lines · Krzysztof Kozlowski
Quoted 13 lines · Wandun Chen

total_reserved_mem_cnt will keep the init value (MAX_RESERVED_REGIONS) in fdt_scan_reserved_mem() if __reserved_mem_check_root() fails.

fdt_scan_reserved_mem_late() calls alloc_reserved_mem_array() to allocate memory according to total_reserved_mem_cnt before __reserved_mem_check_root(), so if __reserved_mem_check_root() fails, the allocated array has nowhere to be used, so no need to allocate memory.

Move alloc_reserved_mem_array() after __reserved_mem_check_root() to avoid potential memory waste.

Quoted 8 lines
Quoted 7 lines
Quoted 1 line

Authorship/DCO mismatch.

Hi Krzysztof,

Thanks for your review.
Sorry for the mismatch. Dueto my company email server's issues with community mailing lists, I sent the patch via my personal email. I will add the correct 'From:' header and send a v3 patch shortly.

There is also the 'b4 relay' that can be used for this kind of issue.

I wasn't aware of 'b4 relay' before, this is really helpful. Thanks for the tip!

Best regards,
Wandun

Quoted 2 lines

Rob

#7 lore raw

PATCH v2 2/3 of: reserved_mem: clean up redundant alloc_reserved_mem_array() call

In reply to Wandun Chen

If total_reserved_mem_cnt is 0 after fdt_scan_reserved_mem(), there is no entry to save, alloc_reserved_mem_array() has nothing to do. Just skip it.

---
 drivers/of/of_reserved_mem.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/of/of_reserved_mem.c +3 -0
index 7856dc857d65..462e7c3078a3 100644
@@ -270,6 +270,9 @@ void __init fdt_scan_reserved_mem_late(void)
 		return;
 	}
 
+	if (!total_reserved_mem_cnt)
+		return;
+
 	/* Attempt dynamic allocation of a new reserved_mem array */
 	alloc_reserved_mem_array();
 
Signature · 1 line
2.43.0
#8 lore raw

Re: [PATCH v2 2/3] of: reserved_mem: clean up redundant alloc_reserved_mem_array() call

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Missing bounds check on `dynamic_nodes_cnt` in `fdt_scan_reserved_mem()` allows out-of-bounds…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Missing bounds check on dynamic_nodes_cnt in fdt_scan_reserved_mem() allows out-of-bounds writes to the stack array dynamic_nodes.
- [High] Error handling in alloc_reserved_mem_array() fails to reset total_reserved_mem_cnt on allocation failure, leading to a buffer overflow on the static reserved_mem_array. --

commit 7ce94f9c503bebcfe72a546d200327f9bde1957e
Author: Wandun Chen <[email protected]>

of: reserved_mem: clean up redundant alloc_reserved_mem_array() call

This commit skips calling alloc_reserved_mem_array() if total_reserved_mem_cnt is 0 after fdt_scan_reserved_mem(). This avoids executing dynamic allocation logic when there are no reserved memory regions to save.

drivers/of/of_reserved_mem.c void __init fdt_scan_reserved_mem_late(void) · line 277

Quoted 13 lines
drivers/of/of_reserved_mem.c +3 -0
index 7856dc857d650..462e7c3078a3a 100644
@@ -270,6 +270,9 @@ void __init fdt_scan_reserved_mem_late(void)
 		return;
 	}
 
+	if (!total_reserved_mem_cnt)
+		return;
+
 	/* Attempt dynamic allocation of a new reserved_mem array */
 	alloc_reserved_mem_array();

This problem wasn't introduced by this patch, but if alloc_reserved_mem_array() fails (e.g. memblock_alloc() fails or alloc_size == SIZE_MAX), it returns without resetting total_reserved_mem_cnt to MAX_RESERVED_REGIONS.

Because total_reserved_mem_cnt can exceed MAX_RESERVED_REGIONS, and reserved_mem would still point to the static reserved_mem_array, would the boundary check in fdt_init_reserved_mem_node() fail to stop insertions at MAX_RESERVED_REGIONS?

Can this result in an out-of-bounds write into the static array?

Also not introduced by this patch, there appears to be a missing bounds check in fdt_scan_reserved_mem():

drivers/of/of_reserved_mem.c:fdt_scan_reserved_mem() {
	int dynamic_nodes_cnt = 0, count = 0;
	int dynamic_nodes[MAX_RESERVED_REGIONS];
	...
		if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) {
			dynamic_nodes[dynamic_nodes_cnt] = child;
			dynamic_nodes_cnt++;
		}
	...
}

If a device tree contains more dynamically sized reserved memory nodes than MAX_RESERVED_REGIONS, could this overwrite the bounds of the dynamic_nodes stack array?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#9 lore raw

PATCH v2 3/3 of: reserved_mem: only support one <base size> entry in reg property

In reply to Wandun Chen

A /reserved-memory child node may have multiple <base size> tuples in 'reg' property, but multiple entries in 'reg' have never been fully functional:
- fdt_scan_reserved_mem() in the early pass loops over every tuple and reserves them all.

- fdt_scan_reserved_mem_late() reads 'reg' by of_flat_dt_get_addr_size(), which returns false if entries != 1. So 'reg' property with multiple <base size> entries will be skipped, no reserved_mem entry is created in reserved_mem[].

Supporting multiple <base size> tuples is not a good idea:
- It requires reserved_mem_ops->node_init support. Currently, CMA(rmem_cma_setup) and DMA(rmem_dma_setup) are not supported.

- of_reserved_mem_lookup() is name-based, only the first entry in multiple <base size> tuples will be found.

So change to support one <base size> entry in 'reg' property.

Also update dt binding:
https://github.com/devicetree-org/dt-schema/pull/197

---
 drivers/of/of_reserved_mem.c | 39 ++++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 17 deletions(-)
drivers/of/of_reserved_mem.c +22 -17
index 462e7c3078a3..0e91c4a71d89 100644
@@ -134,37 +134,38 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
 					     const char *uname)
 {
 	phys_addr_t base, size;
-	int i, len, err;
+	int len, err;
 	const __be32 *prop;
 	bool nomap;
+	u64 b, s;
 
 	prop = of_flat_dt_get_addr_size_prop(node, "reg", &len);
 	if (!prop)
 		return -ENOENT;
 
+	if (len > 1)
+		pr_warn("Reserved memory: node '%s' has %d <base size> entries, only the first is used\n",
+			uname, len);
+
 	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
 
 	err = fdt_validate_reserved_mem_node(node, NULL);
 	if (err && err != -ENODEV)
 		return err;
 
-	for (i = 0; i < len; i++) {
-		u64 b, s;
-
-		of_flat_dt_read_addr_size(prop, i, &b, &s);
-
-		base = b;
-		size = s;
+	of_flat_dt_read_addr_size(prop, 0, &b, &s);
+	base = b;
+	size = s;
 
-		if (size && early_init_dt_reserve_memory(base, size, nomap) == 0) {
-			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));
-		} 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));
-		}
+	if (size && early_init_dt_reserve_memory(base, size, nomap) == 0) {
+		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));
+	} 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;
 }
 
@@ -277,20 +278,24 @@ void __init fdt_scan_reserved_mem_late(void)
 	alloc_reserved_mem_array();
 
 	fdt_for_each_subnode(child, fdt, node) {
+		const __be32 *prop;
 		const char *uname;
 		u64 b, s;
 		int ret;
+		int len;
 
 		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;
 
+		of_flat_dt_read_addr_size(prop, 0, &b, &s);
 		base = b;
 		size = s;
 
Signature · 1 line
2.43.0
#10 lore raw

Re: [PATCH v2 3/3] of: reserved_mem: only support one <base size> entry in reg property

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Unconditional read of the first `reg` property entry without verifying that `len > 0` leads to an…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unconditional read of the first reg property entry without verifying that len > 0 leads to an out-of-bounds FDT read for empty reg properties. --

commit def0743ccda5f4a97f9b1275e989c8ea559363b4
Author: Wandun Chen <[email protected]>

of: reserved_mem: only support one <base size> entry in reg property

This commit simplifies reserved memory parsing by restricting support to a single base/size entry in the reg property. It removes the loop in the early pass and warns if multiple entries are present, unconditionally reading only the first entry.

Quoted 5 lines
drivers/of/of_reserved_mem.c +0 -0
index 462e7c3078a3a..0e91c4a71d89d 100644
@@ -134,37 +134,38 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,

[ ... ]

Quoted 12 lines
drivers/of/of_reserved_mem.c +4 -0
 	prop = of_flat_dt_get_addr_size_prop(node, "reg", &len);
 	if (!prop)
 		return -ENOENT;
 
+	if (len > 1)
+		pr_warn("Reserved memory: node '%s' has %d <base size> entries, only the first is used\n",
+			uname, len);
+
 	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
 
 	err = fdt_validate_reserved_mem_node(node, NULL);
 	if (err && err != -ENODEV)

drivers/of/of_reserved_mem.c static int __init __reserved_mem_reserve_reg(unsigned long node, · line 156

Quoted 10 lines
drivers/of/of_reserved_mem.c +1 -7
 		return err;
 
-	for (i = 0; i < len; i++) {
-		u64 b, s;
-
-		of_flat_dt_read_addr_size(prop, i, &b, &s);
-
-		base = b;
-		size = s;
+	of_flat_dt_read_addr_size(prop, 0, &b, &s);

If a device tree node contains an empty reg property, of_flat_dt_get_addr_size_prop() returns a valid pointer to the empty property data and sets len to 0.

Since the loop was removed, is there a risk of an out-of-bounds read here because the code unconditionally calls of_flat_dt_read_addr_size()?

This could fetch garbage values for the physical base and size. If the parsed garbage size happens to be non-zero, could the kernel attempt to reserve unintended physical memory regions via early_init_dt_reserve_memory()?

Would it be safer to verify len > 0 before reading the first entry?

Quoted 2 lines
drivers/of/of_reserved_mem.c +2 -0
+	base = b;
+	size = s;

[ ... ]

Quoted 5 lines
drivers/of/of_reserved_mem.c +1 -0
@@ -277,20 +278,24 @@ void __init fdt_scan_reserved_mem_late(void)
 	alloc_reserved_mem_array();
 
 	fdt_for_each_subnode(child, fdt, node) {
+		const __be32 *prop;

[ ... ]

drivers/of/of_reserved_mem.c void __init fdt_scan_reserved_mem_late(void) · line 298

Quoted 10 lines
drivers/of/of_reserved_mem.c +3 -1
-		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;
 
+		of_flat_dt_read_addr_size(prop, 0, &b, &s);

Does this have the same out-of-bounds read potential when len == 0?

The previous of_flat_dt_get_addr_size() wrapper explicitly checked that entries != 1, which inherently skipped empty reg properties.

Quoted 3 lines
drivers/of/of_reserved_mem.c +0 -0
 		base = b;
 		size = s;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#11 lore raw

Re: [PATCH v2 3/3] of: reserved_mem: only support one <base size> entry in reg property

Quoted 48 lines · On 5/19/26 17:24, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unconditional read of the first reg property entry without verifying that len > 0 leads to an out-of-bounds FDT read for empty reg properties. --

commit def0743ccda5f4a97f9b1275e989c8ea559363b4
Author: Wandun Chen <[email protected]>

of: reserved_mem: only support one <base size> entry in reg property

This commit simplifies reserved memory parsing by restricting support to a single base/size entry in the reg property. It removes the loop in the early pass and warns if multiple entries are present, unconditionally reading only the first entry.

Quoted 5 lines
drivers/of/of_reserved_mem.c +0 -0
index 462e7c3078a3a..0e91c4a71d89d 100644
@@ -134,37 +134,38 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,

[ ... ]

Quoted 12 lines
  	prop = of_flat_dt_get_addr_size_prop(node, "reg", &len);
  	if (!prop)
  		return -ENOENT;
drivers/of/of_reserved_mem.c +4 -0
+	if (len > 1)
+		pr_warn("Reserved memory: node '%s' has %d <base size> entries, only the first is used\n",
+			uname, len);
+
  	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
 
  	err = fdt_validate_reserved_mem_node(node, NULL);
  	if (err && err != -ENODEV)

drivers/of/of_reserved_mem.c static int __init __reserved_mem_reserve_reg(unsigned long node, · line 156

Quoted 10 lines
drivers/of/of_reserved_mem.c +1 -7
  		return err;
 
-	for (i = 0; i < len; i++) {
-		u64 b, s;
-
-		of_flat_dt_read_addr_size(prop, i, &b, &s);
-
-		base = b;
-		size = s;
+	of_flat_dt_read_addr_size(prop, 0, &b, &s);

If a device tree node contains an empty reg property, of_flat_dt_get_addr_size_prop() returns a valid pointer to the empty property data and sets len to 0.

Quoted 2 lines

Since the loop was removed, is there a risk of an out-of-bounds read here because the code unconditionally calls of_flat_dt_read_addr_size()?

Yes, there is indeed an out-of-bounds read issue here, which will be fixed in v3.

Quoted 35 lines

This could fetch garbage values for the physical base and size. If the parsed garbage size happens to be non-zero, could the kernel attempt to reserve unintended physical memory regions via early_init_dt_reserve_memory()?

Would it be safer to verify len > 0 before reading the first entry?

Quoted 2 lines
drivers/of/of_reserved_mem.c +2 -0
+	base = b;
+	size = s;

[ ... ]

Quoted 5 lines
drivers/of/of_reserved_mem.c +1 -0
@@ -277,20 +278,24 @@ void __init fdt_scan_reserved_mem_late(void)
  	alloc_reserved_mem_array();
 
  	fdt_for_each_subnode(child, fdt, node) {
+		const __be32 *prop;

[ ... ]

drivers/of/of_reserved_mem.c void __init fdt_scan_reserved_mem_late(void) · line 298

Quoted 10 lines
drivers/of/of_reserved_mem.c +3 -1
-		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;
 
+		of_flat_dt_read_addr_size(prop, 0, &b, &s);

Does this have the same out-of-bounds read potential when len == 0?

The previous of_flat_dt_get_addr_size() wrapper explicitly checked that entries != 1, which inherently skipped empty reg properties.

Quoted 3 lines
  		base = b;
  		size = s;