From: Marek Szyprowski <m.szyprowski@samsung.com>
To: Geert Uytterhoeven <geert+renesas@glider.be>,
Rob Herring <robh+dt@kernel.org>,
Frank Rowand <frowand.list@gmail.com>
Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/3] of: Fix truncation of memory sizes on 32-bit platforms
Date: Wed, 16 Jun 2021 11:36:42 +0200 [thread overview]
Message-ID: <0c2be5ec-8a22-d398-d455-847ddbcece86@samsung.com> (raw)
In-Reply-To: <4a1117e72d13d26126f57be034c20dac02f1e915.1623835273.git.geert+renesas@glider.be>
On 16.06.2021 11:27, Geert Uytterhoeven wrote:
> Variable "size" has type "phys_addr_t", which can be either 32-bit or
> 64-bit on 32-bit systems, while "unsigned long" is always 32-bit on
> 32-bit systems. Hence the cast in
>
> (unsigned long)size / SZ_1M
>
> may truncate a 64-bit size to 32-bit, as casts have a higher operator
> precedence than divisions.
>
> Fix this by inverting the order of the cast and division, which should
> be safe for memory blocks smaller than 4 PiB. Note that the division is
> actually a shift, as SZ_1M is a power-of-two constant, hence there is no
> need to use div_u64().
>
> While at it, use "%lu" to format "unsigned long".
>
> Fixes: e8d9d1f5485b52ec ("drivers: of: add initialization code for static reserved memory")
> Fixes: 3f0c8206644836e4 ("drivers: of: add initialization code for dynamic reserved memory")
> Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>
> ---
> drivers/of/fdt.c | 8 ++++----
> drivers/of/of_reserved_mem.c | 8 ++++----
> 2 files changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c
> index a03d43f95495d8e1..970fa8cdc9303195 100644
> --- a/drivers/of/fdt.c
> +++ b/drivers/of/fdt.c
> @@ -510,11 +510,11 @@ static int __init __reserved_mem_reserve_reg(unsigned long node,
>
> if (size &&
> early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
> - pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
> - uname, &base, (unsigned long)size / SZ_1M);
> + pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
> + uname, &base, (unsigned long)(size / SZ_1M));
> else
> - pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
> - uname, &base, (unsigned long)size / SZ_1M);
> + pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
> + uname, &base, (unsigned long)(size / SZ_1M));
>
> len -= t_len;
> if (first) {
> diff --git a/drivers/of/of_reserved_mem.c b/drivers/of/of_reserved_mem.c
> index 4592b71aba5cf4a1..333d33bad59d7888 100644
> --- a/drivers/of/of_reserved_mem.c
> +++ b/drivers/of/of_reserved_mem.c
> @@ -136,9 +136,9 @@ static int __init __reserved_mem_alloc_size(unsigned long node,
> ret = early_init_dt_alloc_reserved_memory_arch(size,
> align, start, end, nomap, &base);
> if (ret == 0) {
> - pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
> + pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
> uname, &base,
> - (unsigned long)size / SZ_1M);
> + (unsigned long)(size / SZ_1M));
> break;
> }
> len -= t_len;
> @@ -148,8 +148,8 @@ static int __init __reserved_mem_alloc_size(unsigned long node,
> ret = early_init_dt_alloc_reserved_memory_arch(size, align,
> 0, 0, nomap, &base);
> if (ret == 0)
> - pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
> - uname, &base, (unsigned long)size / SZ_1M);
> + pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
> + uname, &base, (unsigned long)(size / SZ_1M));
> }
>
> if (base == 0) {
Best regards
--
Marek Szyprowski, PhD
Samsung R&D Institute Poland
next prev parent reply other threads:[~2021-06-16 9:36 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-16 9:27 [PATCH 0/3] of: Miscellaneous fixes and cleanups Geert Uytterhoeven
2021-06-16 9:27 ` [PATCH 1/3] of: Fix truncation of memory sizes on 32-bit platforms Geert Uytterhoeven
2021-06-16 9:36 ` Marek Szyprowski [this message]
2021-06-16 18:05 ` Rob Herring
2021-06-16 9:27 ` [PATCH 2/3] of: Remove superfluous casts when printing u64 values Geert Uytterhoeven
2021-06-16 19:50 ` Rob Herring
2021-06-16 9:27 ` [PATCH 3/3] of: kexec: Always use FDT_PROP_INITRD_START and FDT_PROP_INITRD_END Geert Uytterhoeven
2021-06-16 17:14 ` Rob Herring
2021-06-16 19:36 ` Geert Uytterhoeven
2021-06-16 19:49 ` Rob Herring
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=0c2be5ec-8a22-d398-d455-847ddbcece86@samsung.com \
--to=m.szyprowski@samsung.com \
--cc=devicetree@vger.kernel.org \
--cc=frowand.list@gmail.com \
--cc=geert+renesas@glider.be \
--cc=linux-kernel@vger.kernel.org \
--cc=robh+dt@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;
as well as URLs for NNTP newsgroup(s).