mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/3] of: reserved_mem: fixes and cleanups
@ 2026-05-19  8:24 Wandun Chen
  2026-05-19  8:24 ` [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails Wandun Chen
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Wandun Chen @ 2026-05-19  8:24 UTC (permalink / raw)
  To: devicetree, linux-kernel, zhaomeijing; +Cc: robh, saravanak

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/20260429065831.1510858-1-chenwandun@lixiang.com/
[2] https://lore.kernel.org/all/20260429065831.1510858-1-chenwandun@lixiang.com/T/#m29fa0f1c22c23e6343070e70f905c9482f930901
[3] https://lore.kernel.org/all/20260429065831.1510858-1-chenwandun@lixiang.com/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(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails
  2026-05-19  8:24 [PATCH v2 0/3] of: reserved_mem: fixes and cleanups Wandun Chen
@ 2026-05-19  8:24 ` Wandun Chen
  2026-05-20  7:44   ` Krzysztof Kozlowski
  2026-05-19  8:24 ` [PATCH v2 2/3] of: reserved_mem: clean up redundant alloc_reserved_mem_array() call Wandun Chen
  2026-05-19  8:24 ` [PATCH v2 3/3] of: reserved_mem: only support one <base size> entry in reg property Wandun Chen
  2 siblings, 1 reply; 8+ messages in thread
From: Wandun Chen @ 2026-05-19  8:24 UTC (permalink / raw)
  To: devicetree, linux-kernel, zhaomeijing; +Cc: robh, saravanak

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.

Signed-off-by: Wandun Chen <chenwandun@lixiang.com>
Tested-by: Meijing Zhao <zhaomeijing@lixiang.com>
---
 drivers/of/of_reserved_mem.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 8d5777cb5d1b..7856dc857d65 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -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;
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 2/3] of: reserved_mem: clean up redundant alloc_reserved_mem_array() call
  2026-05-19  8:24 [PATCH v2 0/3] of: reserved_mem: fixes and cleanups Wandun Chen
  2026-05-19  8:24 ` [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails Wandun Chen
@ 2026-05-19  8:24 ` Wandun Chen
  2026-05-19  8:24 ` [PATCH v2 3/3] of: reserved_mem: only support one <base size> entry in reg property Wandun Chen
  2 siblings, 0 replies; 8+ messages in thread
From: Wandun Chen @ 2026-05-19  8:24 UTC (permalink / raw)
  To: devicetree, linux-kernel, zhaomeijing; +Cc: robh, saravanak

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.

Signed-off-by: Wandun Chen <chenwandun@lixiang.com>
---
 drivers/of/of_reserved_mem.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 7856dc857d65..462e7c3078a3 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -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();
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2 3/3] of: reserved_mem: only support one <base size> entry in reg property
  2026-05-19  8:24 [PATCH v2 0/3] of: reserved_mem: fixes and cleanups Wandun Chen
  2026-05-19  8:24 ` [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails Wandun Chen
  2026-05-19  8:24 ` [PATCH v2 2/3] of: reserved_mem: clean up redundant alloc_reserved_mem_array() call Wandun Chen
@ 2026-05-19  8:24 ` Wandun Chen
  2 siblings, 0 replies; 8+ messages in thread
From: Wandun Chen @ 2026-05-19  8:24 UTC (permalink / raw)
  To: devicetree, linux-kernel, zhaomeijing; +Cc: robh, saravanak

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

Suggested-by: Rob Herring <robh@kernel.org>
Signed-off-by: Wandun Chen <chenwandun@lixiang.com>
Tested-by: Meijing Zhao <zhaomeijing@lixiang.com>
Link: https://lore.kernel.org/all/20260506014752.GA280279-robh@kernel.org/
---
 drivers/of/of_reserved_mem.c | 39 ++++++++++++++++++++----------------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
index 462e7c3078a3..0e91c4a71d89 100644
--- a/drivers/of/of_reserved_mem.c
+++ b/drivers/of/of_reserved_mem.c
@@ -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;
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails
  2026-05-19  8:24 ` [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails Wandun Chen
@ 2026-05-20  7:44   ` Krzysztof Kozlowski
  2026-05-20  8:16     ` Wandun
  0 siblings, 1 reply; 8+ messages in thread
From: Krzysztof Kozlowski @ 2026-05-20  7:44 UTC (permalink / raw)
  To: Wandun Chen; +Cc: devicetree, linux-kernel, zhaomeijing, robh, saravanak

On Tue, May 19, 2026 at 04:24:25PM +0800, Wandun Chen wrote:
> 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.
> 
> Signed-off-by: Wandun Chen <chenwandun@lixiang.com>

Authorship/DCO mismatch.

> Tested-by: Meijing Zhao <zhaomeijing@lixiang.com>

Best regards,
Krzysztof


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails
  2026-05-20  7:44   ` Krzysztof Kozlowski
@ 2026-05-20  8:16     ` Wandun
  2026-06-01 23:06       ` Rob Herring
  0 siblings, 1 reply; 8+ messages in thread
From: Wandun @ 2026-05-20  8:16 UTC (permalink / raw)
  To: Krzysztof Kozlowski
  Cc: devicetree, linux-kernel, zhaomeijing, robh, saravanak



On 5/20/26 15:44, Krzysztof Kozlowski wrote:
> On Tue, May 19, 2026 at 04:24:25PM +0800, Wandun Chen wrote:
>> 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.
>>
>> Signed-off-by: Wandun Chen <chenwandun@lixiang.com>
> 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
>> Tested-by: Meijing Zhao <zhaomeijing@lixiang.com>
> Best regards,
> Krzysztof
>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails
  2026-05-20  8:16     ` Wandun
@ 2026-06-01 23:06       ` Rob Herring
  2026-06-02  1:37         ` Wandun
  0 siblings, 1 reply; 8+ messages in thread
From: Rob Herring @ 2026-06-01 23:06 UTC (permalink / raw)
  To: Wandun
  Cc: Krzysztof Kozlowski, devicetree, linux-kernel, zhaomeijing, saravanak

On Wed, May 20, 2026 at 04:16:23PM +0800, Wandun wrote:
> 
> 
> On 5/20/26 15:44, Krzysztof Kozlowski wrote:
> > On Tue, May 19, 2026 at 04:24:25PM +0800, Wandun Chen wrote:
> > > 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.
> > > 
> > > Signed-off-by: Wandun Chen <chenwandun@lixiang.com>
> > 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails
  2026-06-01 23:06       ` Rob Herring
@ 2026-06-02  1:37         ` Wandun
  0 siblings, 0 replies; 8+ messages in thread
From: Wandun @ 2026-06-02  1:37 UTC (permalink / raw)
  To: Rob Herring
  Cc: Krzysztof Kozlowski, devicetree, linux-kernel, zhaomeijing, saravanak



On 6/2/26 07:06, Rob Herring wrote:
> On Wed, May 20, 2026 at 04:16:23PM +0800, Wandun wrote:
>>
>> On 5/20/26 15:44, Krzysztof Kozlowski wrote:
>>> On Tue, May 19, 2026 at 04:24:25PM +0800, Wandun Chen wrote:
>>>> 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.
>>>>
>>>> Signed-off-by: Wandun Chen <chenwandun@lixiang.com>
>>> 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
>
> Rob


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-06-02  1:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-19  8:24 [PATCH v2 0/3] of: reserved_mem: fixes and cleanups Wandun Chen
2026-05-19  8:24 ` [PATCH v2 1/3] of: reserved_mem: avoid unnecessary memory allocation when __reserved_mem_check_root() fails Wandun Chen
2026-05-20  7:44   ` Krzysztof Kozlowski
2026-05-20  8:16     ` Wandun
2026-06-01 23:06       ` Rob Herring
2026-06-02  1:37         ` Wandun
2026-05-19  8:24 ` [PATCH v2 2/3] of: reserved_mem: clean up redundant alloc_reserved_mem_array() call Wandun Chen
2026-05-19  8:24 ` [PATCH v2 3/3] of: reserved_mem: only support one <base size> entry in reg property Wandun Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome