Linux-mediatek Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Ilia Snezhkov via B4 Relay <devnull+iasnezhkov.gmail.com@kernel.org>
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	[thread overview]
Message-ID: <20261203021956.43607-3-iasnezhkov@gmail.com> (raw)
In-Reply-To: <20261203021956.43607-1-iasnezhkov@gmail.com>

From: Ilia Snezhkov <iasnezhkov@gmail.com>

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 <iasnezhkov@gmail.com>
---
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)





  parent reply	other threads:[~2026-12-03  2:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03  2:19 [PATCH v2 0/5] thermal/drivers/mediatek/lvts: CALSCALE, overflow, trip, and IRQ fixes Ilia Snezhkov via B4 Relay
2026-12-03  2:19 ` [PATCH v2 1/5] thermal/drivers/mediatek/lvts: write LVTS_CALSCALE in configure Ilia Snezhkov via B4 Relay
2026-12-03  2:19 ` Ilia Snezhkov via B4 Relay [this message]
2026-12-03  2:19 ` [PATCH v2 3/5] thermal/drivers/mediatek/lvts: serialize lvts_set_trips() per ctrl Ilia Snezhkov via B4 Relay
2026-12-03  2:20 ` [PATCH v2 4/5] thermal/drivers/mediatek/lvts: recompute shared trips and mask first Ilia Snezhkov via B4 Relay
2026-12-03  2:20 ` [PATCH v2 5/5] thermal/drivers/mediatek/lvts: skip sensors without tz in IRQ Ilia Snezhkov via B4 Relay

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=20261203021956.43607-3-iasnezhkov@gmail.com \
    --to=devnull+iasnezhkov.gmail.com@kernel.org \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=bchihi@baylibre.com \
    --cc=daniel.lezcano@kernel.org \
    --cc=iasnezhkov@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=lukasz.luba@arm.com \
    --cc=matthias.bgg@gmail.com \
    --cc=nfraprado@collabora.com \
    --cc=phone-devel@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=rui.zhang@intel.com \
    /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