From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796204130; b=mbZjcMRuhuHAg/2i6sV0/duDgNvDxvmB6ElhGq31mveHYDBIwcU9Xn/dof8JNn9voW4Y rc+zNZnZQtzZ0MmkUIfkOXFV5lRdjb2GkhPxGswbsnYPbt8OfuGZ13DR09itYFoOoFQHu ft6AoGNwSwTfis0fXnEp39V6U/6W9573VCg67/J+uXIQg+D56ga2v5Wqvclph5Mfcb6iL xOHmfxCg93kAsQzUyD+G9s5M0QOaUyyjqfCK6sHUeXOWTzuQmulj1B+ebpd4fuWo6fBxh 5sUW+APAf9qD7Shp2cPheG8PozGo24wghyR4HHlqF4Mh6ortJTKpGs1tWCjPdHcmpIg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796204130; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=dUR46Ii1YTkqRoAr+HPzpQFUnVpOOGLVmGvcm1zRqto=; b=uSy4riGOjt/HiR6oqi+4BoVmKsn9k3J4Ob0fcdP2Y2YI/v3JzcMcewJPct0DKvdyVOYS 1zOARbYDdH1rs8K9r28NJOzEOIN6Hla4/NK4KO5+KNpB4hinYxIoryQxH+MlXBfs1V2q/ obpnOeT//+UrL5ON7l1CfE7rcY5wkuhqzEgv5LerzEujMC03+XR783pc5avyliYn77pdO 5oz72xZVHQk31L7vXACYDDizXTA7pB75rnP1qYTzGaB5f2Iix/Jyvbl8bXH9uckU5CD8b DMccMOurDmchW3WMTnAORhJjzHOZn9xUil8ObqIJ1ULlOgKwDoJ/P3acmcaR6DtW+IA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="W/snhsoJ"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 732D5606A31A; Wed, 2 Dec 2026 09:35:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=W/snhsoJ Received: by smtp.kernel.org (Postfix) with ESMTPSA id 643E31F0089C; Wed, 2 Dec 2026 09:35:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796204129; bh=dUR46Ii1YTkqRoAr+HPzpQFUnVpOOGLVmGvcm1zRqto=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=W/snhsoJluyNy++x+uYsgFxhnvFos3LBJ5JtYhIrv6stb0giC4qcZmMz66BkC85Pb ZhhoM0lvGUPk6MqZ/4m0+lrG4wcuWk2jzbtxpDL58FECBFxdZG3zCHqrqIU9ld6AOb eMxk4EHX3i0IaGICaQ/cEZ+Eht5P12sVjLegj/tj1RHbVwRzXKy0I6nrB87h6QSDJF QXLkI5rv+OjIUNIdD466bs89BznqLTFaSwf+ns0n5/LIihhq99wfDLboOM/lJ4gBwz 3diXj1GbqrbzsIG6KiON4bQb4mzqe/eBO+ObEOS0XvhVsaPeeEvlFBsKrB09H8Pi0B nHjgFbdRJIPbw== From: Lee Jones To: lee@kernel.org, Santosh Shilimkar , Krzysztof Kozlowski , Aneesh V , Greg Kroah-Hartman , Grant Likely , Benoit Cousson , 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 Message-ID: <144a9050516939f090b0e7e176b24e0e9ededaad.1796203994.git.lee@kernel.org> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 #include #include +#include #include #include #include @@ -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