From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796264399; b=ojyo9fIYO5ar5Z11HRA0xiCihh342iB8Lcf9CqLsI/pagB/FSBZqSnL4OD2vlcqDLFPm nqWm8UUjoh+vLHAZsB32DiHEYGjo4NDIGRP70jXdlB34+r4Ogw3KQzhvEqeOEfAk2NXoO TUcrOPhebQPhogQh4W3psVB2c8WV8KxQpiCOE0k1C2lc7otelhrB8eFVh8VhuY+ewBN37 2JSqWjwZTyrdUAoYNUso/sui0wCnTrmpksuZxxStpf8NG81tdI7rMkIcKKYk80xNxuD6w W/dNlESKovaxoiM+dsBA4+EZEAbH9mPWXCLK33gJSHC7wMRU00lTIkC2OAvxSqOZKiw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796264399; h=Received: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=PTlLnn50mvmJusoNaihtYgFmorkhzUP1Z/tZr+onxp/YlIMYvo3ZQ+WA06zw6LS6sXo5 iVJVr0uRb60G3uQ3B3r3VEDKvM9lPouBRBN0or+Ph3CpgFMuS/KC5TZPHp2jJPmqPMgS8 hTqPhTQkbR/v5/r5B4IfubBLNzcVUVhqJJI4l1KFoixCzSihNhqmVvzB+2kaNxy0VOilC N72LQU4ResWMPVWodDff2QBSw/ZREwcXQsXMh5ok1I5KqBK7Pnur1m7HPwzwg1ZD7JhEQ 3TLWnFKqft/F5nhyv7crgQUyPpA+lQhHwUsCYpnwtEfjXhkTLLiVYMh6Dr61QcR0ULw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=fGwBfO9a; arc=pass header.oldest-pass=0 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 6DABE606A32C; Thu, 3 Dec 2026 02:19:59 +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=fGwBfO9a 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-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-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 Reply-To: iasnezhkov@gmail.com Content-Transfer-Encoding: 7bit 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)