Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Marek Szyprowski" <m.szyprowski@samsung.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 2/2] of: reserved_mem: allocate and map the reserved_mem array early
Date: Thu, 08 Oct 2026 09:59:51 +0000	[thread overview]
Message-ID: <sashiko-outbox-163975@kernel.org> (raw)
In-Reply-To: <20261008094125.3197971-3-m.szyprowski@samsung.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] of: reserved_mem array allocated before static regions are reserved
--

commit cee7768ed6d5a3ae8d8c64404da579769becebbc
Author: Marek Szyprowski <m.szyprowski@samsung.com>

of: reserved_mem: allocate and map the reserved_mem array early

This commit replaces the statically-sized reserved_mem array with a
dynamically allocated array using memblock_alloc_raw(). It accesses the
array through a temporary early mapping so that all regions can be processed
in a single early boot scan.

Link: https://lore.kernel.org/all/asc6brs_g0ky0wOM@kernel.org/

> diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
> --- a/drivers/of/of_reserved_mem.c
> +++ b/drivers/of/of_reserved_mem.c

[ ... ]

> @@ -381,143 +376,89 @@ static void __init __rmem_check_for_overlap(void)

[ ... ]

>  int __init fdt_scan_reserved_mem(void)
>  {
> -	int node, child;
> -	int dynamic_nodes_cnt = 0, count = 0;
> -	int dynamic_nodes[MAX_RESERVED_REGIONS];
> +	struct reserved_mem *array, *map;
> +	size_t array_bytes;
> +	int node, child, cnt;
>  	const void *fdt = initial_boot_params;
>  
>  	node = fdt_path_offset(fdt, "/reserved-memory");
> -	if (node < 0) {
> -		total_reserved_mem_cnt = 0;
> +	if (node < 0)
>  		return -ENODEV;
> -	}
>  
>  	if (__reserved_mem_check_root(node) != 0) {
>  		pr_err("Reserved memory: unsupported node format, ignoring\n");
> -		total_reserved_mem_cnt = 0;
>  		return -EINVAL;
>  	}
>  
> -	fdt_for_each_subnode(child, fdt, node) {
> -		const char *uname;
> -		int err;
> +	/* First pass: count the entries needed for the reserved_mem array */
> +	cnt = count_reserved_mem_nodes(fdt, node);
> +	if (!cnt)
> +		return 0;
>  
> -		if (!of_fdt_device_is_available(fdt, child))
> -			continue;
> +	array_bytes = array_size(cnt, sizeof(*array));
> +	array = memblock_alloc_raw(array_bytes, SMP_CACHE_BYTES);

[Severity: High]
Could this allocate the array inside one of the statically-placed reserved
memory regions?

At the time of this memblock_alloc_raw() call, statically-placed reserved
memory regions (defined by the reg property) have not yet been marked as
reserved in memblock. The allocator considers the physical memory intended
for these static regions as available.

> +	if (!array) {
> +		pr_err("Failed to allocate memory for reserved_mem array\n");
> +		return -ENOMEM;
> +	}

[ ... ]

> +	reserved_mem = map;
> +	total_reserved_mem_cnt = cnt;
>  
> -		uname = fdt_get_name(fdt, child, NULL);
> +	/* Second pass: reserve and initialize the static regions */
> +	fdt_for_each_subnode(child, fdt, node) {
> +		if (!of_fdt_device_is_available(fdt, child))
> +			continue;
>  
> -		err = __reserved_mem_reserve_reg(child, uname);
> -		if (!err)
> -			count++;
> +		__reserved_mem_reserve_reg(child,
> +					   fdt_get_name(fdt, child, NULL));

[Severity: High]
If the array was allocated inside a static region above, won't this late
reservation either overlap and fail, or silently succeed?

If it succeeds, it places the kernel's reserved_mem array in physical
memory expected to be exclusively owned by a hardware device or firmware.
When the device later uses this memory, it might overwrite the array,
potentially leading to memory corruption and boot failure.

> +	}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261008094125.3197971-1-m.szyprowski@samsung.com?part=2

  reply	other threads:[~2026-10-08  9:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20261008094129eucas1p2c89032b43a7491f200a703da1a224c2c@eucas1p2.samsung.com>
2026-10-08  9:41 ` [PATCH 0/2] of: reserved_mem: use dynamically allocated reserved_mem array Marek Szyprowski
2026-10-08  9:41   ` [PATCH 1/2] of: reserved_mem: group allocation helpers together Marek Szyprowski
2026-10-08  9:41   ` [PATCH 2/2] of: reserved_mem: allocate and map the reserved_mem array early Marek Szyprowski
2026-10-08  9:59     ` sashiko-bot [this message]
2026-10-08 14:05     ` Rob Herring
2026-10-08 15:10       ` Marek Szyprowski
2026-10-10 10:01       ` Mike Rapoport

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=sashiko-outbox-163975@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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