From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 809AC606A684 for ; Thu, 3 Dec 2026 02:20:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=MK0XNdD0; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fGwBfO9a DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Reply-To:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Content-Transfer-Encoding:Content-Type:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Content-ID:Content-Description:Resent-Date :Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=dbaujvX2caIJtLB2VcAovYHwE71P0jG0FlyUdYgAGSU=; b=MK0XNdD0nu3KK1nldww1Pq63zl O7qgskHMomOo/SkxsGq6ncM8xxkD9jsEPctGOghRHTBBkHS0/6LQ3lvHPbiqQwIa+sgMiR9oIeW6N LdVDJR+xWgHWv8pK7iiNPEi3IifuJQiG0d+yH1Shyv5tnE6QSyhYpO3d5CwGwgnRVDeIHon3iLJpv fDbrmt4Qc/d2qvMSuqXhVc9x4reeYhJqq9XbZZ06EiMocgphq2g74n07cELtQPy6qmu9UUQ18I5rL ZtejGy0ijZzXp3wmzPEG2usEiL1xziUBEs+yxdwhdkI4ifiPWP5qATK4ibSQwuJzwdvUiIsGqXTFL UO4tgvSg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYwQS-0000000DD7m-46Jt; Thu, 03 Dec 2026 02:20:01 +0000 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYwQR-0000000DD6U-23T1; Thu, 03 Dec 2026 02:19:59 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 4250F42AEF; Thu, 3 Dec 2026 02:19:59 +0000 (UTC) Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796264399; b=AR8ehmCEPdgcWo9EWtKjR4jLoPgMEJPsr1GXgyPMD0/eDgNnA7lIg3UInYnaFJa5CLCI vU0XK1bx6PzH4Y5Nk7Jaa3pZujvBtXXVxLDhtpGnwbIYizg6zAjIqPoiaLjVWk/xvBq/t EgnvXtt3VX0/vpKhQR7bJhhONItdxLt+JxqwxFBXd2knSP2TpCbMlRDPMBjbKKUbDm+LD 6ezjrYos0Y1MPmhPms/Gwi08VU0druC/Is9jE2l8xwoZJaKQbgD+iyzsd16JjA+HlP3to eHLoZDpz/cU/22XOemrHWiyhvseM5WpzGRAWlipC5C3MB7WK49K9GaKp97Mke/zhjpg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796264399; h=DKIM-Signature:Received:From:To:Cc:Subject:Date:Message-Id: In-Reply-To:References:MIME-Version:Content-Type:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=dbaujvX2caIJtLB2VcAovYHwE71P0jG0FlyUdYgAGSU=; b=v/81Ei2Y2ADwnsP+pjwL1pT5Of5QFcF44ZpbPl2u1QbHEKmknB48aHX8uNVAeyquxz4A zuwB9HcjESrRtuPaE66irGzbWcITGIvhX5nMCDo0N3JddQtTh2ukiauRPrraz7LsUpvON ByDdXbv6IUCdbOabmk0pxlpGbZGtUSay+7W5k8W9hy4ZspIJAPAgwUIKoKDoebZUZhSno YkttGGCsoJYauUVR/Eg4bqd3m2ThgPNVfky9JGYpaZtd1X1/WRBa8j9CuUo2IjUbbFUgw 5RIEBWElSUpc/+lsQCoCMoN2cb10TU93xaYoldiRimcimuj9L72UWQd/J8s7w1xfYpA== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 445721F00898; Thu, 3 Dec 2026 02:19:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796264399; bh=dbaujvX2caIJtLB2VcAovYHwE71P0jG0FlyUdYgAGSU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:Reply-To; b=fGwBfO9aQCorX1fymLeKNRz0J9mwx0EpqINvRyAtvkzMTbvdM4Ea/d8dxNdrB4Wph j5qWmtZVKTsZe6slP2o+ef4Q8ButoWSFIczw93LWGBOfwchU7BdIiKrQqm5IXqxdNY yIv7evGcrqf9oczeskAIxsiHTaZtjBzHNwExTOjWWv0Nqy+wOtDMjo4LdtaWSGqLYw 96IB7Dkl1b4TfywQicwT/T80uhD5cQKIw01DtJkquJN7HCLh346cmZl9E6Yq8IbTZ8 ok1IstRr2gU56yXmqUEdn558kHdpkfb893Hn1nwVvODGiCcvLTsn0B4GGrhXUgYctW Ntj3qrJLiyB/Q== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id C45194F750; Thu, 3 Dec 2026 02:19:57 +0000 (UTC) From: Ilia Snezhkov via B4 Relay To: daniel.lezcano@kernel.org, rafael@kernel.org, rui.zhang@intel.com, lukasz.luba@arm.com Cc: matthias.bgg@gmail.com, angelogioacchino.delregno@collabora.com, bchihi@baylibre.com, nfraprado@collabora.com, linux-pm@vger.kernel.org, linux-mediatek@lists.infradead.org, linux-arm-kernel@lists.infradead.org, phone-devel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/5] thermal/drivers/mediatek/lvts: fix 32-bit overflow in temp_to_raw Date: Thu, 03 Dec 2026 02:19:58 +0000 Message-Id: <20261203021956.43607-3-iasnezhkov@gmail.com> In-Reply-To: <20261203021956.43607-1-iasnezhkov@gmail.com> References: <20261203021956.43607-1-iasnezhkov@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1796264396; l=2274; i=iasnezhkov@gmail.com; s=mindone2026; h=from:subject:message-id; bh=MgoOnkrVOsQiUj3x3VRf2zqUQ8kcBe3phsqYU5LvLbg=; b=DVUNEwJ+eRcXzU1V3+XdSqlyCczKw6OphXJGE6Frk0ZYnCr3Nq9MaySIni4hgeF8W3Jtl+fk4 F2eC9UHbUHND1H3OtbgvjcRzcxgmSu3IcsY0ii5J8jHl5wZiGWByDqc X-Developer-Key: i=iasnezhkov@gmail.com; a=ed25519; pk=hQgXfiWi4ukk2jh4BHu1vRVhg9BUivo//AFp50AKMag= X-Endpoint-Received: by B4 Relay for iasnezhkov@gmail.com/mindone2026 with auth_id=1643 X-Original-From: Ilia Snezhkov Content-Transfer-Encoding: 7bit X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: iasnezhkov@gmail.com Sender: "Linux-mediatek" X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Ilia Snezhkov lvts_temp_to_raw_mt7988() subtracts two 32-bit ints before casting to s64, shifts left by 14 bits, and truncates the shifted numerator to u32 before calling div_s64(). Whenever golden_temp_offset - temperature >= 262144, the shifted value exceeds U32_MAX and wraps modulo 2^32, producing a small raw value that represents a much higher temperature than requested. With LVTS_COEFF_B_MT8195 (250460), this happens for any temperature <= 13316 mC at the default golden_temp of 50 (golden_temp_offset = 275460) and up to 18816 mC at golden_temp = 61 (just below LVTS_GOLDEN_TEMP_MAX = 62). In addition, temperatures at or above golden_temp_offset produce a negative difference, which causes a negative left shift and unsigned wraparound. Promote golden_temp_offset to s64 before subtracting, return 0 early when the difference is non-positive, and pass the s64 shifted value directly to div_s64(). Fixes: f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver") Signed-off-by: Ilia Snezhkov --- Changes in v2: - Narrowed to lvts_temp_to_raw_mt7988() only (INT_MAX trip handling moved to patch 4/5). - Return 0 early when golden_temp_offset - temperature <= 0 to avoid left-shifting a negative s64. - Corrected the golden_temp boundary example (LVTS_GOLDEN_TEMP_MAX=62). Found and drafted with an AI coding assistant. drivers/thermal/mediatek/lvts_thermal.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c index e034c6c..6d39025 100644 --- a/drivers/thermal/mediatek/lvts_thermal.c +++ b/drivers/thermal/mediatek/lvts_thermal.c @@ -322,11 +322,12 @@ static int lvts_raw_to_temp_mt7988(u32 raw_temp, int temp_factor) static u32 lvts_temp_to_raw_mt7988(int temperature, int temp_factor) { - u32 raw_temp = ((s64)(golden_temp_offset - temperature)) << 14; + s64 diff = (s64)golden_temp_offset - temperature; - raw_temp = div_s64(raw_temp, -temp_factor); + if (diff <= 0) + return 0; - return raw_temp; + return div_s64(diff << 14, -temp_factor); } static u32 lvts_temp_to_raw_mt8196(int temperature, int temp_factor) -- 2.54.0 (Apple Git-157)