mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/4] thermal/drivers/mediatek/lvts_thermal: CALSCALE, overflow, locking, and IRQ NULL fixes
@ 2026-11-26  0:53 Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH 1/4] thermal/drivers/mediatek/lvts_thermal: write LVTS_CALSCALE in lvts_ctrl_configure() Ilia Snezhkov via B4 Relay
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, linux-pm,
	linux-mediatek, linux-arm-kernel, linux-kernel

This four-patch series fixes four bugs in
drivers/thermal/mediatek/lvts_thermal.c:

1. Write LVTS_CALSCALE_CONF (0x300) to the LVTS_CALSCALE register instead
   of overwriting a local variable and discarding it.
2. Avoid 32-bit overflow and unsigned truncation in
   lvts_temp_to_raw_mt7988(), and skip converting high == INT_MAX in
   lvts_set_trips() while masking high-offset IRQ.
3. Serialize lvts_set_trips() with a per-controller spinlock_t in
   struct lvts_ctrl so concurrent updates across thermal zones on the
   same controller cannot corrupt shared thresholds or LVTS_MONINT via
   unsynchronized RMW.
4. Check lvts_ctrl->sensors[i].tz before calling
   thermal_zone_device_update() in lvts_ctrl_irq_handler() for disabled
   or DT-absent sensor slots.

Found and drafted with an AI coding assistant while bringing up MT6789.

Ilia Snezhkov (4):
  thermal/drivers/mediatek/lvts_thermal: write LVTS_CALSCALE in
    lvts_ctrl_configure()
  thermal/drivers/mediatek/lvts_thermal: avoid 32-bit overflow in
    lvts_temp_to_raw_mt7988() and INT_MAX trips
  thermal/drivers/mediatek/lvts_thermal: serialize lvts_set_trips() per
    controller
  thermal/drivers/mediatek/lvts_thermal: skip sensors without a thermal
    zone in IRQ

 drivers/thermal/mediatek/lvts_thermal.c | 30 ++++++++++++++++---------
 1 file changed, 20 insertions(+), 10 deletions(-)

-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] thermal/drivers/mediatek/lvts_thermal: write LVTS_CALSCALE in lvts_ctrl_configure()
  2026-11-26  0:53 [PATCH 0/4] thermal/drivers/mediatek/lvts_thermal: CALSCALE, overflow, locking, and IRQ NULL fixes Ilia Snezhkov via B4 Relay
@ 2026-11-26  0:53 ` Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH 2/4] thermal/drivers/mediatek/lvts_thermal: avoid 32-bit overflow in lvts_temp_to_raw_mt7988() and INT_MAX trips Ilia Snezhkov via B4 Relay
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, linux-pm,
	linux-mediatek, linux-arm-kernel, linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

lvts_ctrl_configure() assigns 0x300 and LVTS_CALSCALE_CONF (0x300) to
the local variable `value`, then immediately overwrites `value` with the
LVTS_MSRCTL0 filter setting without ever writing LVTS_CALSCALE (0x0048).

Remove the dead duplicate assignment and write LVTS_CALSCALE_CONF to
LVTS_CALSCALE(lvts_ctrl->base), matching the comment above it and
MediaTek's downstream soc_temp_lvts initialization sequence.

Found and drafted with an AI coding assistant.

Fixes: f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
 drivers/thermal/mediatek/lvts_thermal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index 350fffd..e034c6c 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -1197,8 +1197,8 @@ static int lvts_ctrl_configure(struct device *dev, struct lvts_ctrl *lvts_ctrl)
 	/*
 	 * LVTS_CALSCALE : ADC voltage round
 	 */
-	value = 0x300;
 	value = LVTS_CALSCALE_CONF;
+	writel(value, LVTS_CALSCALE(lvts_ctrl->base));
 
 	/*
 	 * LVTS_MSRCTL0 : Sensor filtering strategy
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/4] thermal/drivers/mediatek/lvts_thermal: avoid 32-bit overflow in lvts_temp_to_raw_mt7988() and INT_MAX trips
  2026-11-26  0:53 [PATCH 0/4] thermal/drivers/mediatek/lvts_thermal: CALSCALE, overflow, locking, and IRQ NULL fixes Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH 1/4] thermal/drivers/mediatek/lvts_thermal: write LVTS_CALSCALE in lvts_ctrl_configure() Ilia Snezhkov via B4 Relay
@ 2026-11-26  0:53 ` Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH 3/4] thermal/drivers/mediatek/lvts_thermal: serialize lvts_set_trips() per controller Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH 4/4] thermal/drivers/mediatek/lvts_thermal: skip sensors without a thermal zone in IRQ Ilia Snezhkov via B4 Relay
  3 siblings, 0 replies; 5+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, linux-pm,
	linux-mediatek, linux-arm-kernel, linux-kernel

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 immediately truncates the result to a
u32 before passing it to div_s64().

When golden_temp_offset - temperature >= 262144 (for example,
golden_temp = 60, temp_offset = 250460, temperature < 18316 mC, or even
LVTS_MINIMUM_THRESHOLD = 20000 mC when golden_temp >= 64), the shifted
value exceeds U32_MAX and silently wraps modulo 2^32, programming a
bogus low raw value (high temperature) into LVTS_OFFSETL/LVTS_OFFSETH.

In addition, lvts_set_trips() passes high == INT_MAX (used by the
thermal core when no upper trip exists) directly to lvts_temp_to_raw(),
which produces a negative quotient cast to u32 while leaving the sensor's
high-offset interrupt enabled.

Promote golden_temp_offset to s64 before subtracting, keep the shifted
numerator in s64, clamp negative results to 0, avoid converting
high == INT_MAX in lvts_set_trips(), and mask high_offset_inten when
high_thresh == INT_MAX.

Found and drafted with an AI coding assistant.

Fixes: f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver")
Fixes: 2bba1acf7a4c ("thermal/drivers/mediatek/lvts_thermal: Manage threshold between sensors")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
 drivers/thermal/mediatek/lvts_thermal.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index e034c6c..9b8b653 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -322,11 +322,11 @@ 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 raw_temp = ((s64)golden_temp_offset - temperature) << 14;
 
 	raw_temp = div_s64(raw_temp, -temp_factor);
 
-	return raw_temp;
+	return raw_temp < 0 ? 0 : (u32)raw_temp;
 }
 
 static u32 lvts_temp_to_raw_mt8196(int temperature, int temp_factor)
@@ -409,12 +409,15 @@ static void lvts_update_irq_mask(struct lvts_ctrl *lvts_ctrl)
 			 * don't actually want to generate interrupts when
 			 * crossing it.
 			 */
-			if (lvts_ctrl->low_thresh == -INT_MAX) {
+			if (lvts_ctrl->low_thresh == -INT_MAX)
 				value &= ~low_offset_inten_masks[i];
+			else
+				value |= low_offset_inten_masks[i];
+
+			if (lvts_ctrl->high_thresh == INT_MAX)
+				value &= ~high_offset_inten_masks[i];
+			else
 				value |= high_offset_inten_masks[i];
-			} else {
-				value |= low_offset_inten_masks[i] | high_offset_inten_masks[i];
-			}
 		} else {
 			value &= ~(low_offset_inten_masks[i] | high_offset_inten_masks[i]);
 		}
@@ -447,7 +450,7 @@ static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
 	void __iomem *base = lvts_sensor->base;
 	u32 raw_low = lvts_temp_to_raw(low != -INT_MAX ? low : LVTS_MINIMUM_THRESHOLD,
 				       lvts_data);
-	u32 raw_high = lvts_temp_to_raw(high, lvts_data);
+	u32 raw_high = high != INT_MAX ? lvts_temp_to_raw(high, lvts_data) : 0;
 	bool should_update_thresh;
 
 	lvts_sensor->low_thresh = low;
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/4] thermal/drivers/mediatek/lvts_thermal: serialize lvts_set_trips() per controller
  2026-11-26  0:53 [PATCH 0/4] thermal/drivers/mediatek/lvts_thermal: CALSCALE, overflow, locking, and IRQ NULL fixes Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH 1/4] thermal/drivers/mediatek/lvts_thermal: write LVTS_CALSCALE in lvts_ctrl_configure() Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH 2/4] thermal/drivers/mediatek/lvts_thermal: avoid 32-bit overflow in lvts_temp_to_raw_mt7988() and INT_MAX trips Ilia Snezhkov via B4 Relay
@ 2026-11-26  0:53 ` Ilia Snezhkov via B4 Relay
  2026-11-26  0:53 ` [PATCH 4/4] thermal/drivers/mediatek/lvts_thermal: skip sensors without a thermal zone in IRQ Ilia Snezhkov via B4 Relay
  3 siblings, 0 replies; 5+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, linux-pm,
	linux-mediatek, linux-arm-kernel, linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

Each struct lvts_ctrl manages up to four sensors, each registered as an
independent thermal_zone_device with its own tz->lock. Concurrent
lvts_set_trips() calls for sensors sharing a controller therefore run
without mutual exclusion while updating lvts_ctrl->{low,high}_thresh,
performing a read-modify-write on LVTS_MONINT in lvts_update_irq_mask(),
and writing LVTS_OFFSETL/LVTS_OFFSETH.

Two concurrent callers can lose each other's LVTS_MONINT enable bits or
leave the programmed threshold registers inconsistent with the stored
controller thresholds.

Add a per-controller spinlock_t to struct lvts_ctrl and serialize
lvts_set_trips() with guard(spinlock)(&lvts_ctrl->lock).

Found and drafted with an AI coding assistant.

Fixes: 2bba1acf7a4c ("thermal/drivers/mediatek/lvts_thermal: Manage threshold between sensors")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
 drivers/thermal/mediatek/lvts_thermal.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index 9b8b653..f0b7caa 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -4,6 +4,7 @@
  * Author: Balsam CHIHI <bchihi@baylibre.com>
  */
 
+#include <linux/cleanup.h>
 #include <linux/clk.h>
 #include <linux/clk-provider.h>
 #include <linux/delay.h>
@@ -16,6 +17,7 @@
 #include <linux/of.h>
 #include <linux/platform_device.h>
 #include <linux/reset.h>
+#include <linux/spinlock.h>
 #include <linux/thermal.h>
 #include <dt-bindings/thermal/mediatek,lvts-thermal.h>
 
@@ -181,6 +183,7 @@ struct lvts_ctrl {
 	void __iomem *base;
 	int low_thresh;
 	int high_thresh;
+	spinlock_t lock; /* protects thresholds and LVTS_MONINT */
 };
 
 struct lvts_domain {
@@ -453,6 +456,8 @@ static int lvts_set_trips(struct thermal_zone_device *tz, int low, int high)
 	u32 raw_high = high != INT_MAX ? lvts_temp_to_raw(high, lvts_data) : 0;
 	bool should_update_thresh;
 
+	guard(spinlock)(&lvts_ctrl->lock);
+
 	lvts_sensor->low_thresh = low;
 	lvts_sensor->high_thresh = high;
 
@@ -989,6 +994,7 @@ static int lvts_ctrl_init(struct device *dev, struct lvts_domain *lvts_td,
 
 		lvts_ctrl[i].low_thresh = INT_MIN;
 		lvts_ctrl[i].high_thresh = INT_MIN;
+		spin_lock_init(&lvts_ctrl[i].lock);
 	}
 
 	/*
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/4] thermal/drivers/mediatek/lvts_thermal: skip sensors without a thermal zone in IRQ
  2026-11-26  0:53 [PATCH 0/4] thermal/drivers/mediatek/lvts_thermal: CALSCALE, overflow, locking, and IRQ NULL fixes Ilia Snezhkov via B4 Relay
                   ` (2 preceding siblings ...)
  2026-11-26  0:53 ` [PATCH 3/4] thermal/drivers/mediatek/lvts_thermal: serialize lvts_set_trips() per controller Ilia Snezhkov via B4 Relay
@ 2026-11-26  0:53 ` Ilia Snezhkov via B4 Relay
  3 siblings, 0 replies; 5+ messages in thread
From: Ilia Snezhkov via B4 Relay @ 2026-11-26  0:53 UTC (permalink / raw)
  To: daniel.lezcano, rafael, rui.zhang, lukasz.luba
  Cc: matthias.bgg, angelogioacchino.delregno, linux-pm,
	linux-mediatek, linux-arm-kernel, linux-kernel

From: Ilia Snezhkov <iasnezhkov@gmail.com>

lvts_ctrl_start() skips sensors absent from valid_sensor_mask as well as
sensors whose thermal zone is not present in the device tree (-ENODEV),
leaving lvts_ctrl->sensors[i].tz as NULL.

lvts_ctrl_irq_handler(), however, loops over all four sensor slots and
passes lvts_ctrl->sensors[i].tz unconditionally to
thermal_zone_device_update(), which dereferences tz immediately if a
status bit for an unpopulated sensor slot is set in LVTS_MONINTSTS.

Check lvts_ctrl->sensors[i].tz before calling
thermal_zone_device_update() while still acknowledging the status bit
with W1C.

Found and drafted with an AI coding assistant.

Fixes: f5f633b18234 ("thermal/drivers/mediatek: Add the Low Voltage Thermal Sensor driver")
Signed-off-by: Ilia Snezhkov <iasnezhkov@gmail.com>
---
 drivers/thermal/mediatek/lvts_thermal.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/thermal/mediatek/lvts_thermal.c b/drivers/thermal/mediatek/lvts_thermal.c
index f0b7caa..5fc87a8 100644
--- a/drivers/thermal/mediatek/lvts_thermal.c
+++ b/drivers/thermal/mediatek/lvts_thermal.c
@@ -588,8 +588,9 @@ static irqreturn_t lvts_ctrl_irq_handler(struct lvts_ctrl *lvts_ctrl)
 		if (!(value & masks[i]))
 			continue;
 
-		thermal_zone_device_update(lvts_ctrl->sensors[i].tz,
-					   THERMAL_TRIP_VIOLATED);
+		if (lvts_ctrl->sensors[i].tz)
+			thermal_zone_device_update(lvts_ctrl->sensors[i].tz,
+						   THERMAL_TRIP_VIOLATED);
 		iret = IRQ_HANDLED;
 	}
 
-- 
2.54.0 (Apple Git-157)




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-26  0:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  0:53 [PATCH 0/4] thermal/drivers/mediatek/lvts_thermal: CALSCALE, overflow, locking, and IRQ NULL fixes Ilia Snezhkov via B4 Relay
2026-11-26  0:53 ` [PATCH 1/4] thermal/drivers/mediatek/lvts_thermal: write LVTS_CALSCALE in lvts_ctrl_configure() Ilia Snezhkov via B4 Relay
2026-11-26  0:53 ` [PATCH 2/4] thermal/drivers/mediatek/lvts_thermal: avoid 32-bit overflow in lvts_temp_to_raw_mt7988() and INT_MAX trips Ilia Snezhkov via B4 Relay
2026-11-26  0:53 ` [PATCH 3/4] thermal/drivers/mediatek/lvts_thermal: serialize lvts_set_trips() per controller Ilia Snezhkov via B4 Relay
2026-11-26  0:53 ` [PATCH 4/4] thermal/drivers/mediatek/lvts_thermal: skip sensors without a thermal zone in IRQ Ilia Snezhkov via B4 Relay

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®