All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] hwmon: (cros_ec) Avoid threshold temperature conversion overflows
@ 2026-07-30 15:12 Thomas Weißschuh
  2026-07-30 15:28 ` sashiko-bot
  2026-07-30 16:30 ` Guenter Roeck
  0 siblings, 2 replies; 3+ messages in thread
From: Thomas Weißschuh @ 2026-07-30 15:12 UTC (permalink / raw)
  To: Guenter Roeck, Benson Leung
  Cc: Guenter Roeck, chrome-platform, linux-hwmon, linux-kernel,
	Thomas Weißschuh

If the EC returns non-sensical values the temperature conversions might
overflow on 32-bit systems.

As these values wouldn't make sense, clamp them to 255 degrees celsius.
The machine would die before reaching that limit anyways.

Suggested-by: Guenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/lkml/0fbf7f69-bb90-4209-b9d5-258759711496@roeck-us.net/
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Changes in v3:
- Drop the overflow handling and instead clamp the value sent by the EC
- Link to v2: https://patch.msgid.link/20260728-cros_ec-hwmon-overflow-v2-1-d24e24d0792b@weissschuh.net

Changes in v2:
- Drop already applied patch 1.
- Also handle overflow of u32 -> long.
- Clarify commit message wrt compiler optimizations.
- Use __always_inline over __flatten to allow the compiler to optimize
  away more unnecessary overflow checks.
- Link to v1: https://patch.msgid.link/20260630-cros_ec-hwmon-overflow-v1-0-3d2ecd3eb0f2@weissschuh.net
---
 drivers/hwmon/cros_ec_hwmon.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/hwmon/cros_ec_hwmon.c b/drivers/hwmon/cros_ec_hwmon.c
index 1337b646e022..27af13f0941c 100644
--- a/drivers/hwmon/cros_ec_hwmon.c
+++ b/drivers/hwmon/cros_ec_hwmon.c
@@ -236,8 +236,13 @@ static int cros_ec_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
 			ret = cros_ec_hwmon_read_temp_threshold(priv->cros_ec, channel,
 								cros_ec_hwmon_attr_to_thres(attr),
 								&threshold);
-			if (ret == 0)
-				*val = cros_ec_hwmon_kelvin_to_millicelsius(threshold);
+			if (ret == 0) {
+				/* Limit to sensible, non-overflowing values. */
+				if (threshold > 255 + 273)
+					*val = 255000;
+				else
+					*val = cros_ec_hwmon_kelvin_to_millicelsius(threshold);
+			}
 		}
 	}
 

---
base-commit: 8a98254c65f629dcdc50a1046f58c5c87c72ef67
change-id: 20260630-cros_ec-hwmon-overflow-0381c8509df3

Best regards,
--  
Thomas Weißschuh <linux@weissschuh.net>


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

* Re: [PATCH v3] hwmon: (cros_ec) Avoid threshold temperature conversion overflows
  2026-07-30 15:12 [PATCH v3] hwmon: (cros_ec) Avoid threshold temperature conversion overflows Thomas Weißschuh
@ 2026-07-30 15:28 ` sashiko-bot
  2026-07-30 16:30 ` Guenter Roeck
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-07-30 15:28 UTC (permalink / raw)
  To: Thomas Weißschuh; +Cc: linux-hwmon

> If the EC returns non-sensical values the temperature conversions might
> overflow on 32-bit systems.
> 
> As these values wouldn't make sense, clamp them to 255 degrees celsius.
> The machine would die before reaching that limit anyways.
> 
> Suggested-by: Guenter Roeck <linux@roeck-us.net>
> Link: https://lore.kernel.org/lkml/0fbf7f69-bb90-4209-b9d5-258759711496@roeck-us.net/
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Sashiko has reviewed this patch and found no issues. It looks great!

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260730-cros_ec-hwmon-overflow-v3-1-c7198ba034ad@weissschuh.net?part=1


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

* Re: [PATCH v3] hwmon: (cros_ec) Avoid threshold temperature conversion overflows
  2026-07-30 15:12 [PATCH v3] hwmon: (cros_ec) Avoid threshold temperature conversion overflows Thomas Weißschuh
  2026-07-30 15:28 ` sashiko-bot
@ 2026-07-30 16:30 ` Guenter Roeck
  1 sibling, 0 replies; 3+ messages in thread
From: Guenter Roeck @ 2026-07-30 16:30 UTC (permalink / raw)
  To: Thomas Weißschuh
  Cc: Benson Leung, Guenter Roeck, chrome-platform, linux-hwmon,
	linux-kernel

On Thu, Jul 30, 2026 at 05:12:26PM +0200, Thomas Weißschuh wrote:
> If the EC returns non-sensical values the temperature conversions might
> overflow on 32-bit systems.
> 
> As these values wouldn't make sense, clamp them to 255 degrees celsius.
> The machine would die before reaching that limit anyways.
> 
> Suggested-by: Guenter Roeck <linux@roeck-us.net>
> Link: https://lore.kernel.org/lkml/0fbf7f69-bb90-4209-b9d5-258759711496@roeck-us.net/
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Applied.

Thanks,
Guenter

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

end of thread, other threads:[~2026-07-30 16:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-30 15:12 [PATCH v3] hwmon: (cros_ec) Avoid threshold temperature conversion overflows Thomas Weißschuh
2026-07-30 15:28 ` sashiko-bot
2026-07-30 16:30 ` Guenter Roeck

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.