]> git.baikalelectronics.ru Git - kernel.git/commit
of: Fix truncation of memory sizes on 32-bit platforms
authorGeert Uytterhoeven <geert+renesas@glider.be>
Wed, 16 Jun 2021 09:27:44 +0000 (11:27 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 14 Jul 2021 14:53:45 +0000 (16:53 +0200)
commitb1234464227492effa01d7743f2dfa210954d5ab
tree4c946ee7c8fe0407915ab3ef45dae44819e158f5
parent41db3e368003218ea7ac85bd2de982a3cbdc6de5
of: Fix truncation of memory sizes on 32-bit platforms

[ Upstream commit 2892d8a00d23d511a0591ac4b2ff3f050ae1f004 ]

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: 65b26b7e7294a66c ("drivers: of: add initialization code for static reserved memory")
Fixes: 4a0076a8ca889690 ("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>
Link: https://lore.kernel.org/r/4a1117e72d13d26126f57be034c20dac02f1e915.1623835273.git.geert+renesas@glider.be
Signed-off-by: Rob Herring <robh@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/of/fdt.c
drivers/of/of_reserved_mem.c