From: Yuntao Wang <yuntao.wang@linux.dev>
To: Rob Herring <robh@kernel.org>, Saravana Kannan <saravanak@google.com>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>,
Catalin Marinas <catalin.marinas@arm.com>,
James Morse <james.morse@arm.com>, Baoquan He <bhe@redhat.com>,
Zhen Lei <thunder.leizhen@huawei.com>,
Ard Biesheuvel <ardb@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Geoff Levand <geoff@infradead.org>,
Andrew Morton <akpm@linux-foundation.org>,
Changyuan Lyu <changyuanl@google.com>,
Alexander Graf <graf@amazon.com>,
"Mike Rapoport (Microsoft)" <rppt@kernel.org>,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
Yuntao Wang <yuntao.wang@linux.dev>
Subject: [PATCH v2 3/7] of/fdt: Fix the len check in early_init_dt_check_for_usable_mem_range()
Date: Thu, 13 Nov 2025 23:51:00 +0800 [thread overview]
Message-ID: <20251113155104.226617-4-yuntao.wang@linux.dev> (raw)
In-Reply-To: <20251113155104.226617-1-yuntao.wang@linux.dev>
The len value is in bytes, while `dt_root_addr_cells + dt_root_size_cells`
is in cells (4 bytes per cell). Modulo calculation between them is
incorrect, the units must be converted first.
Use helper functions to simplify the code and fix this issue.
Fixes: fb319e77a0e7 ("of: fdt: Add memory for devices by DT property "linux,usable-memory-range"")
Fixes: 2af2b50acf9b9c38 ("of: fdt: Add generic support for handling usable memory range property")
Fixes: 8f579b1c4e347b23 ("arm64: limit memory regions based on DT property, usable-memory-range")
Signed-off-by: Yuntao Wang <yuntao.wang@linux.dev>
---
drivers/of/fdt.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
index 80b7236efdc6..d0caaab42aa7 100644
--- a/drivers/of/fdt.c
+++ b/drivers/of/fdt.c
@@ -884,7 +884,7 @@ static unsigned long chosen_node_offset = -FDT_ERR_NOTFOUND;
void __init early_init_dt_check_for_usable_mem_range(void)
{
struct memblock_region rgn[MAX_USABLE_RANGES] = {0};
- const __be32 *prop, *endp;
+ const __be32 *prop;
int len, i;
unsigned long node = chosen_node_offset;
@@ -893,14 +893,14 @@ void __init early_init_dt_check_for_usable_mem_range(void)
pr_debug("Looking for usable-memory-range property... ");
- prop = of_get_flat_dt_prop(node, "linux,usable-memory-range", &len);
- if (!prop || (len % (dt_root_addr_cells + dt_root_size_cells)))
+ prop = of_fdt_get_addr_size_prop(node, "linux,usable-memory-range", &len);
+ if (!prop)
return;
- endp = prop + (len / sizeof(__be32));
- for (i = 0; i < MAX_USABLE_RANGES && prop < endp; i++) {
- rgn[i].base = dt_mem_next_cell(dt_root_addr_cells, &prop);
- rgn[i].size = dt_mem_next_cell(dt_root_size_cells, &prop);
+ len = min(len, MAX_USABLE_RANGES);
+
+ for (i = 0; i < len; i++) {
+ of_fdt_read_addr_size(prop, &rgn[i].base, &rgn[i].size);
pr_debug("cap_mem_regions[%d]: base=%pa, size=%pa\n",
i, &rgn[i].base, &rgn[i].size);
--
2.51.0
next prev parent reply other threads:[~2025-11-13 15:52 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-13 15:50 [PATCH v2 0/7] of/fdt: Some bug fixes and cleanups Yuntao Wang
2025-11-13 15:50 ` [PATCH v2 1/7] of/fdt: Consolidate duplicate code into helper functions Yuntao Wang
2025-11-13 22:38 ` Rob Herring
2025-11-14 3:09 ` Yuntao Wang
2025-11-14 14:55 ` Rob Herring
2025-11-13 15:50 ` [PATCH v2 2/7] of/fdt: Fix the len check in early_init_dt_check_for_elfcorehdr() Yuntao Wang
2025-11-13 15:51 ` Yuntao Wang [this message]
2025-11-13 18:43 ` [PATCH v2 3/7] of/fdt: Fix the len check in early_init_dt_check_for_usable_mem_range() kernel test robot
2025-11-13 19:26 ` kernel test robot
2025-11-13 15:51 ` [PATCH v2 4/7] of/fdt: Fix incorrect use of dt_root_addr_cells in early_init_dt_check_kho() Yuntao Wang
2025-11-13 15:51 ` [PATCH v2 5/7] of/fdt: Simplify the logic of early_init_dt_scan_memory() Yuntao Wang
2025-11-13 22:03 ` Rob Herring
2025-11-14 3:55 ` Yuntao Wang
2025-11-14 15:11 ` Rob Herring
2025-11-15 14:00 ` Yuntao Wang
2025-11-17 12:44 ` Geert Uytterhoeven
2025-11-13 15:51 ` [PATCH v2 6/7] of/reserved_mem: Simplify the logic of __reserved_mem_reserve_reg() Yuntao Wang
2025-11-13 19:37 ` kernel test robot
2025-11-13 15:51 ` [PATCH v2 7/7] of/reserved_mem: Simplify the logic of fdt_scan_reserved_mem_reg_nodes() Yuntao Wang
2025-11-14 8:04 ` [PATCH v2 0/7] of/fdt: Some bug fixes and cleanups Krzysztof Kozlowski
2025-11-15 14:07 ` Yuntao Wang
2025-11-17 7:02 ` Krzysztof Kozlowski
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=20251113155104.226617-4-yuntao.wang@linux.dev \
--to=yuntao.wang@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=ardb@kernel.org \
--cc=bhe@redhat.com \
--cc=catalin.marinas@arm.com \
--cc=changyuanl@google.com \
--cc=devicetree@vger.kernel.org \
--cc=geert+renesas@glider.be \
--cc=geoff@infradead.org \
--cc=graf@amazon.com \
--cc=james.morse@arm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=robh@kernel.org \
--cc=rppt@kernel.org \
--cc=saravanak@google.com \
--cc=thunder.leizhen@huawei.com \
/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;
as well as URLs for NNTP newsgroup(s).