From: Lee Jones <lee@kernel.org>
To: lee@kernel.org, Santosh Shilimkar <ssantosh@kernel.org>,
Krzysztof Kozlowski <krzk@kernel.org>, Aneesh V <aneesh@ti.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Grant Likely <grant.likely@secretlab.ca>,
Benoit Cousson <b-cousson@ti.com>,
linux-kernel@vger.kernel.org
Cc: stable@vger.kernel.org
Subject: [PATCH 7/8] memory: emif: Avoid __fls(0) and underflow in of_get_ddr_info()
Date: Wed, 2 Dec 2026 09:33:54 +0000 [thread overview]
Message-ID: <144a9050516939f090b0e7e176b24e0e9ededaad.1796203994.git.lee@kernel.org> (raw)
In-Reply-To: <cover.1796203994.git.lee@kernel.org>
of_get_ddr_info() uses `x & (x - 1)` to test whether the "density" and
"io-width" device tree properties are powers of two before calling
__fls(). When either property is missing or zero, `0 & -1` evaluates
to zero and __fls(0) is called, which has undefined behaviour. Small
power-of-two values (density < 64 or io-width < 4) also underflow when
subtracting 5 or 1 from the bit index.
Use is_power_of_2() and check the minimum valid values (64 Mb for
density and 4 bits for io-width) before calling __fls().
Fixes: e6b42eb6a66c ("memory: emif: add device tree support to emif driver")
Closes: https://sashiko.dev/#/bug/linux-c8a6e639-7c1a-4114-99be-f6df07f04fb4
Cc: stable@vger.kernel.org
Signed-off-by: Lee Jones <lee@kernel.org>
---
drivers/memory/emif.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/drivers/memory/emif.c b/drivers/memory/emif.c
index 886c2ca0dc19..cb2c49028d6c 100644
--- a/drivers/memory/emif.c
+++ b/drivers/memory/emif.c
@@ -10,6 +10,7 @@
#include <linux/cleanup.h>
#include <linux/err.h>
#include <linux/kernel.h>
+#include <linux/log2.h>
#include <linux/reboot.h>
#include <linux/platform_data/emif_plat.h>
#include <linux/io.h>
@@ -892,13 +893,13 @@ static void of_get_ddr_info(struct device_node *np_emif,
of_property_read_u32(np_ddr, "io-width", &io_width);
/* Convert from density in Mb to the density encoding in jedc_ddr.h */
- if (density & (density - 1))
+ if (!is_power_of_2(density) || density < 64)
dev_info->density = 0;
else
dev_info->density = __fls(density) - 5;
/* Convert from io_width in bits to io_width encoding in jedc_ddr.h */
- if (io_width & (io_width - 1))
+ if (!is_power_of_2(io_width) || io_width < 4)
dev_info->io_width = 0;
else
dev_info->io_width = __fls(io_width) - 1;
--
2.56.1.911.g883d8bcb20-goog
next prev parent reply other threads:[~2026-12-02 9:35 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 9:33 [PATCH 0/8] memory: Fix Critical and High issues reported by Sashiko Lee Jones
2026-12-02 9:33 ` [PATCH 1/8] memory: atmel-ebi: Use non-devm allocations for of_update_property() Lee Jones
2026-12-02 9:33 ` [PATCH 2/8] memory: tegra210-emc: Stop timers on suspend and prevent training re-arm Lee Jones
2026-12-02 9:33 ` [PATCH 3/8] memory: stm32_omm: Depopulate child devices before disabling clocks Lee Jones
2026-12-02 13:54 ` Patrice CHOTARD
2026-12-02 9:33 ` [PATCH 4/8] memory: stm32_omm: Fix double FIELD_PREP() and clamp for REQ2ACK Lee Jones
2026-12-02 14:08 ` Patrice CHOTARD
2026-12-02 9:33 ` [PATCH 5/8] memory: stm32_omm: Avoid division by zero in req2ack calculation Lee Jones
2026-12-02 14:17 ` Patrice CHOTARD
2026-12-02 9:33 ` [PATCH 6/8] memory: stm32_omm: Shift and mask AMCR value in stm32_omm_set_amcr() Lee Jones
2026-12-02 14:33 ` Patrice CHOTARD
2026-12-02 9:33 ` Lee Jones [this message]
2026-12-02 9:33 ` [PATCH 8/8] memory: emif: Clear invalid custom_configs in get_device_details() Lee Jones
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=144a9050516939f090b0e7e176b24e0e9ededaad.1796203994.git.lee@kernel.org \
--to=lee@kernel.org \
--cc=aneesh@ti.com \
--cc=b-cousson@ti.com \
--cc=grant.likely@secretlab.ca \
--cc=gregkh@linuxfoundation.org \
--cc=krzk@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ssantosh@kernel.org \
--cc=stable@vger.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
all inboxes | Powered by JetHome®