Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Marek Szyprowski <m.szyprowski@samsung.com>
To: Wandun Chen <chenwandun1@gmail.com>,
	robh@kernel.org, saravanak@kernel.org, rppt@kernel.org,
	devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org
Cc: akpm@linux-foundation.org
Subject: Re: [PATCH v2 1/5] of: reserved_mem: skip init for regions whose early reservation failed
Date: Wed, 26 Aug 2026 15:14:44 +0200	[thread overview]
Message-ID: <72074a49-2963-4f96-b939-79bf95a829cb@samsung.com> (raw)
In-Reply-To: <20260818092420.2859026-2-chenwandun1@gmail.com>

On 18.08.2026 11:24, Wandun Chen wrote:
> From: Wandun Chen <chenwandun@lixiang.com>
>
> __reserved_mem_reserve_reg() discards the error from
> early_init_dt_reserve_memory() and returns 0 unconditionally, so the
> caller counts the node in total_reserved_mem_cnt and the late scan
> initializes it without checking whether the early reservation actually
> succeeded. A region whose reservation failed is then handed to a
> device assuming the memory is protected.
>
> Propagate the error so failed reservations are no longer counted, and
> record the failed nodes so fdt_scan_reserved_mem_late() can skip them.
>
> Recording the failed nodes explicitly is necessary because
> fdt_scan_reserved_mem_late() rescans the DT independently. It cannot
> tell from memblock whether early reservation succeeded.
>
> The failed-node array is bounded by MAX_RESERVED_REGIONS, the number
> of static regions is not bounded by it, so on overflow the extra nodes
> fall back to being initialized, which is the current behavior.

I'm not very keen on such partial solution. Indeed we have no place to

store the result of the early init call, but we can check if the given

region has been earlier marked in memblock as reserved or no-map in

fdt_scan_reserved_mem_late(). If those attributes don't match the

region can be simply skipped then.


> Fixes: 8a6e02d0c00e ("of: reserved_mem: Restructure how the reserved memory regions are processed")
> Signed-off-by: Wandun Chen <chenwandun@lixiang.com>
> ---
>  drivers/of/of_reserved_mem.c | 65 ++++++++++++++++++++++++++++++------
>  1 file changed, 54 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
> index 8c9d6395d6a3..c6e73d710ee1 100644
> --- a/drivers/of/of_reserved_mem.c
> +++ b/drivers/of/of_reserved_mem.c
> @@ -32,6 +32,30 @@ static struct reserved_mem *reserved_mem __refdata = reserved_mem_array;
>  static int total_reserved_mem_cnt = MAX_RESERVED_REGIONS;
>  static int reserved_mem_count;
>  
> +static int reserve_failed_nodes[MAX_RESERVED_REGIONS] __initdata;
> +static int reserve_failed_nodes_cnt __initdata;
> +
> +static bool __init reserved_mem_node_reserve_failed(int node)
> +{
> +	int i;
> +
> +	for (i = 0; i < reserve_failed_nodes_cnt; i++)
> +		if (reserve_failed_nodes[i] == node)
> +			return true;
> +	return false;
> +}
> +
> +static bool __init record_reserve_failed_node(int node, const char *uname)
> +{
> +	if (reserve_failed_nodes_cnt == MAX_RESERVED_REGIONS) {
> +		pr_err("too many failed regions, '%s' reservation failed\n", uname);
> +		return false;
> +	}
> +
> +	reserve_failed_nodes[reserve_failed_nodes_cnt++] = node;
> +	return true;
> +}
> +
>  static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
>  	phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
>  	phys_addr_t *res_base)
> @@ -141,7 +165,8 @@ static int __init early_init_dt_reserve_memory(phys_addr_t base,
>   * first entry in 'reg' property
>   */
>  static int __init __reserved_mem_reserve_reg(unsigned long node,
> -					     const char *uname)
> +					     const char *uname,
> +					     bool *should_record_failed_node)
>  {
>  	phys_addr_t base, size;
>  	int len, err;
> @@ -149,6 +174,8 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>  	bool nomap;
>  	u64 b, s;
>  
> +	*should_record_failed_node = false;
> +
>  	prop = of_flat_dt_get_addr_size_prop(node, "reg", &len);
>  	if (!prop || !len)
>  		return -ENOENT;
> @@ -167,14 +194,20 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>  	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 {
> +	if (!size)
> +		return -EINVAL;
> +
> +	err = early_init_dt_reserve_memory(base, size, nomap);
> +	if (err) {
> +		*should_record_failed_node = true;
>  		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 err;
>  	}
> +
> +	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));
>  	return 0;
>  }
>  
> @@ -306,10 +339,14 @@ void __init fdt_scan_reserved_mem_late(void)
>  		base = b;
>  		size = s;
>  
> -		if (size) {
> -			uname = fdt_get_name(fdt, child, NULL);
> -			fdt_init_reserved_mem_node(child, uname, base, size);
> -		}
> +		if (!size)
> +			continue;
> +
> +		if (reserved_mem_node_reserve_failed(child))
> +			continue;
> +
> +		uname = fdt_get_name(fdt, child, NULL);
> +		fdt_init_reserved_mem_node(child, uname, base, size);
>  	}
>  
>  	/* check for overlapping reserved regions */
> @@ -349,6 +386,7 @@ int __init fdt_scan_reserved_mem(void)
>  
>  	fdt_for_each_subnode(child, fdt, node) {
>  		const char *uname;
> +		bool should_record_failed_node;
>  		int err;
>  
>  		if (!of_fdt_device_is_available(fdt, child))
> @@ -356,9 +394,14 @@ int __init fdt_scan_reserved_mem(void)
>  
>  		uname = fdt_get_name(fdt, child, NULL);
>  
> -		err = __reserved_mem_reserve_reg(child, uname);
> +		err = __reserved_mem_reserve_reg(child, uname,
> +						 &should_record_failed_node);
>  		if (!err)
>  			count++;
> +		else if (should_record_failed_node &&
> +			 !record_reserve_failed_node(child, uname))
> +			/* Keep a slot for the untracked node's late initialization. */
> +			count++;
>  
>  		/*
>  		 * Save the nodes for the dynamically-placed regions

Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland



  reply	other threads:[~2026-08-26 13:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-18  9:24 [PATCH v2 0/5] of: reserved_mem: several fixes about reserved memory Wandun Chen
2026-08-18  9:24 ` [PATCH v2 1/5] of: reserved_mem: skip init for regions whose early reservation failed Wandun Chen
2026-08-26 13:14   ` Marek Szyprowski [this message]
2026-08-18  9:24 ` [PATCH v2 2/5] of: reserved_mem: reject static regions overlapping no-map memory Wandun Chen
2026-08-26 13:16   ` Marek Szyprowski
2026-08-18  9:24 ` [PATCH v2 3/5] of: reserved_mem: reject statically placed regions overlapping existing reservations Wandun Chen
2026-08-26 13:16   ` Marek Szyprowski
2026-08-18  9:24 ` [PATCH v2 4/5] of: reserved_mem: release dynamically allocated no-map region on init failure Wandun Chen
2026-08-26 13:17   ` Marek Szyprowski
2026-08-18  9:24 ` [PATCH v2 5/5] of: reserved_mem: retain static no-map memory " Wandun Chen
2026-08-26 13:18   ` Marek Szyprowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=72074a49-2963-4f96-b939-79bf95a829cb@samsung.com \
    --to=m.szyprowski@samsung.com \
    --cc=akpm@linux-foundation.org \
    --cc=chenwandun1@gmail.com \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=robh@kernel.org \
    --cc=rppt@kernel.org \
    --cc=saravanak@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox