All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Sebastián Peyrott" <speyrott@gmail.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] hwmon: (minisforum-um780xtx) Avoid overflow when rounding threshold input
Date: Thu, 15 Oct 2026 00:38:41 -0300	[thread overview]
Message-ID: <20261015033841.390615-1-speyrott@gmail.com> (raw)

The SYS fan threshold store path rounds the user-supplied millidegree value
with DIV_ROUND_CLOSEST() before clamping it. DIV_ROUND_CLOSEST() adds or
subtracts half the divisor first, so inputs within 500 of LONG_MAX or
LONG_MIN overflow and change sign. Writing LONG_MAX then selects the lower
limit instead of the upper one, and LONG_MIN the upper limit instead of the
lower one.

Clamp the input to the representable 0..255000 range before rounding. The
result is unchanged for all other inputs, because the subsequent clamp
already limits the threshold to 0..254 degrees.

Fixes: 9d980da93bbc ("hwmon: Add Minisforum UM780 XTX EC monitoring and fan control")
Reported-by: Sashiko AI review <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/r/sashiko-outbox-171941@kernel.org
Assisted-by: LLM
Signed-off-by: Sebastián Peyrott <speyrott@gmail.com>
---
This applies on top of the GCC 11 build fix:
https://lore.kernel.org/r/20261014214522.243253-1-speyrott@gmail.com

A userspace comparison using the kernel's DIV_ROUND_CLOSEST() with
-fno-strict-overflow shows identical results for every input from -300000
to 300000 across several limit pairs; only inputs near LONG_MAX/LONG_MIN
change, and they now clamp correctly. Builds without warnings with W=1
using GCC 11 (module), GCC 15 (built-in, sparse clean), and GCC 15/16
allmodconfig on x86_64 plus GCC 16 on i386.

Tested on the UM780 XTX: writing LONG_MAX to pwm2_auto_point2_temp sets
51 C with the current driver and 99 C (threshold 3 - 1) with this change;
LONG_MIN sets the lower limit. All other rounding and clamping results are
unchanged, every value was verified against the EC registers, and the
original thresholds were restored.

An LLM-based coding assistant helped analyse the report and prepare this
patch. I reviewed the change and the test results.

 drivers/hwmon/minisforum-um780xtx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/hwmon/minisforum-um780xtx.c b/drivers/hwmon/minisforum-um780xtx.c
index 962ebf41ffb7..bbd77a0ce25d 100644
--- a/drivers/hwmon/minisforum-um780xtx.c
+++ b/drivers/hwmon/minisforum-um780xtx.c
@@ -205,7 +205,7 @@ static ssize_t um780xtx_sys_point_temp_store(struct device *dev,
 		lo = points[0] + 1;
 		hi = data->sys_point3 - 1;
 	}
-	value = DIV_ROUND_CLOSEST(value, 1000);
+	value = DIV_ROUND_CLOSEST(clamp_val(value, 0, 255000), 1000);
 	points[sattr->index] = clamp_val(value, lo, hi);
 
 	ret = ec_write(um780xtx_sys_point_offsets[sattr->index],

base-commit: 4781ca52761e666cf18b591e6bb0478396c90320
prerequisite-patch-id: 817e6d58067db84010e9c7f4ba73d312275021fb
-- 
2.54.0


             reply	other threads:[~2026-10-15  3:38 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-15  3:38 Sebastián Peyrott [this message]
2026-10-15  3:46 ` [PATCH] hwmon: (minisforum-um780xtx) Avoid overflow when rounding threshold input sashiko-bot

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=20261015033841.390615-1-speyrott@gmail.com \
    --to=speyrott@gmail.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /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 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.