From: Wandun <chenwandun1@gmail.com>
To: sashiko-reviews@lists.linux.dev
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v2 5/5] of: reserved_mem: retain static no-map memory on init failure
Date: Wed, 19 Aug 2026 11:16:16 +0800 [thread overview]
Message-ID: <71b25c0f-9efd-4d8f-9bca-6d2d267d5ed7@gmail.com> (raw)
In-Reply-To: <20260818093450.AE0AB1F000E9@smtp.kernel.org>
On 8/18/26 17:34, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
>
> Pre-existing issues:
> - [High] In `__reserved_mem_alloc_size`, an allocation error when marking a region as `NOMAP` is ignored, leading to a use-after-free of memblock memory.
> --
>
> --- Patch [5]: [PATCH v2 5/5] of: reserved_mem: retain static no-map memory on init failure ---
> commit 35ad8d6e4b86dc63d754a8415950fc7ab54dfce4
> Author: Wandun Chen <chenwandun@lixiang.com>
>
> of: reserved_mem: retain static no-map memory on init failure
>
> Clear MEMBLOCK_NOMAP only for dynamically allocated regions on init failure.
> Static regions are initialized after paging_init() and should retain their
> memory states.
>
> Link: https://sashiko.dev/#/message/20260814090305.4C8741F00A3D%40smtp.kernel.org [1]
>
>> diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
>> index a0df075a9902e..0c5163ce601ea 100644
>> --- a/drivers/of/of_reserved_mem.c
>> +++ b/drivers/of/of_reserved_mem.c
>> @@ -708,7 +708,7 @@ static void __init fdt_init_reserved_mem_node(unsigned long node, const char *un
>> pr_info("node %s compatible matching fail\n", rmem->name);
>> rmem->name = NULL;
>>
>> - if (nomap)
>> + if (dynamic && nomap)
>> memblock_clear_nomap(rmem->base, rmem->size);
>>
>> if (dynamic || !nomap)
>
> [Severity: High]
> This is a pre-existing issue, but while looking at reserved memory error
> handling in this file, I noticed a potential memory corruption when dynamic
> allocation fails.
>
> In early_init_dt_alloc_reserved_memory_arch(), the base address is assigned
> before the nomap check:
>
> base = memblock_phys_alloc_range(size, align, start, end);
> ...
> *res_base = base;
> if (nomap) {
> err = memblock_mark_nomap(base, size);
> if (err)
> memblock_phys_free(base, size);
> }
>
> If memblock_mark_nomap() fails, the memory is freed, but *res_base still
> holds the freed address instead of being cleared.
>
> Then in __reserved_mem_alloc_size(), the code checks the base address instead
> of the return value ret:
>
> ret = early_init_dt_alloc_reserved_memory_arch(size, align,
> 0, 0, nomap, &base);
> if (ret == 0)
> pr_debug("...");
>
> if (base == 0) {
> pr_err("failed to allocate memory...");
> return -ENOMEM;
> }
Repeated questions;
The reason of memblock_mark_nomap fails only can be out of memory, and this would
trigger panic because memblock is not allowd resize before paging_init.
>
> Since base contains the freed address instead of 0, does this allow the
> freed memory to be registered and later handed out by the buddy allocator
> while still being incorrectly used as reserved memory?
>
prev parent reply other threads:[~2026-08-19 3:16 UTC|newest]
Thread overview: 14+ 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-18 9:24 ` [PATCH v2 2/5] of: reserved_mem: reject static regions overlapping no-map memory Wandun Chen
2026-08-18 9:40 ` sashiko-bot
2026-08-19 2:26 ` Wandun
2026-08-18 9:24 ` [PATCH v2 3/5] of: reserved_mem: reject statically placed regions overlapping existing reservations Wandun Chen
2026-08-18 9:41 ` sashiko-bot
2026-08-19 3:00 ` Wandun
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-18 9:38 ` sashiko-bot
2026-08-19 3:14 ` Wandun
2026-08-18 9:24 ` [PATCH v2 5/5] of: reserved_mem: retain static no-map memory " Wandun Chen
2026-08-18 9:34 ` sashiko-bot
2026-08-19 3:16 ` Wandun [this message]
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=71b25c0f-9efd-4d8f-9bca-6d2d267d5ed7@gmail.com \
--to=chenwandun1@gmail.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--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