clamp() low limit 0 greater than high limit points[1] - 1
Subsystems (2)
Hello!
Recent -next trees have failed allmodconfig builds with the following build error:
drivers/hwmon/minisforum-um780xtx.c: In function ‘um780xtx_sys_point_temp_store’:
HDRTEST usr/include/linux/hash_info.h././include/linux/compiler_types.h:702:45: error: call to ‘__compiletime_assert_727’ declared with attribute error: clamp() low limit 0 greater than high limit points[1] - 1
702 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^././include/linux/compiler_types.h:683:25: note: in definition of macro ‘__compiletime_assert’
683 | prefix ## suffix(); \
| ^~~~~~
././include/linux/compiler_types.h:702:9: note: in expansion of macro ‘_compiletime_assert’
702 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
./include/linux/build_bug.h:40:37: note: in expansion of macro ‘compiletime_assert’
40 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
| ^~~~~~~~~~~~~~~~~~
./include/linux/minmax.h:188:9: note: in expansion of macro ‘BUILD_BUG_ON_MSG’
188 | BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \
| ^~~~~~~~~~~~~~~~
./include/linux/minmax.h:195:9: note: in expansion of macro ‘__clamp_once’
195 | __clamp_once(type, val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_))
| ^~~~~~~~~~~~
./include/linux/minmax.h:231:32: note: in expansion of macro ‘__careful_clamp’
231 | #define clamp_val(val, lo, hi) __careful_clamp(typeof(val), val, lo, hi)
| ^~~~~~~~~~~~~~~
drivers/hwmon/minisforum-um780xtx.c:203:25: note: in expansion of macro ‘clamp_val’
203 | value = clamp_val(value, 0, points[1] - 1);
| ^~~~~~~~~I must confess to being unsure what fix would be appropriate when points[1] is zero or negative.
Help?
Thanx, Paul
PATCH 1/1 hwmon: (minisforum-um780xtx) Fix clamp() build failure with GCC 11
GCC 11 fails to build the driver:
error: call to '__compiletime_assert_727' declared with attribute error:
clamp() low limit 0 greater than high limit points[1] - 1
The store function returns -EIO unless points[0] < points[1], so points[1] - 1 cannot be negative when the clamp is reached. GCC 11 jump threading, however, duplicates the clamp() limit check and does not prune the copy in which points[1] is zero, so the compile-time check in clamp() fires on an unreachable path. GCC 8 to 10 and 12 to 16 accept the code.
Select both limits first and use a single clamp_val(). This does not change behavior.
---
Reproduced on next-20261014 (x86_64) with GCC 11.5.0, both with allmodconfig and with defconfig plus this driver; GCC 8.5, 9.5, 10.5, 12.4, 13.4, 14.4, 15.3 and 16.1 build the unmodified driver. With this change, the driver builds without warnings with W=1 using GCC 11, 13, 14, 15 and 16 (x86_64 allmodconfig) and GCC 16 (i386 allmodconfig). A small standalone reproducer of the clamp() pattern fails only with GCC 11, and the patched form builds with GCC 8 through 16.
Tested on the UM780 XTX: the installed driver and this version produce identical rounding and clamping results for both system fan thresholds, with every value verified against the EC registers and the original thresholds restored afterwards. Module load and unload are also clean.
An LLM-based coding assistant helped reproduce and analyse the failure and prepare this patch. I reviewed the change and the test results.
drivers/hwmon/minisforum-um780xtx.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/hwmon/minisforum-um780xtx.c +9 -7
@@ -180,7 +180,7 @@ static ssize_t um780xtx_sys_point_temp_store(struct device *dev,struct sensor_device_attribute *sattr =to_sensor_dev_attr (attr);u8 points[2 ];u8 readback; -long value; +long value, lo, hi;int ret; ret =kstrtol (buf,10 , &value);
@@ -198,13 +198,15 @@ static ssize_t um780xtx_sys_point_temp_store(struct device *dev,if (points[0 ] >= points[1 ] || points[1 ] >= data->sys_point3)return -EIO ; +if (!sattr->index) { + lo =0 ; + hi = points[1 ] -1 ; + }else { + lo = points[0 ] +1 ; + hi = data->sys_point3 -1 ; + } value =DIV_ROUND_CLOSEST (value,1000 ); -if (!sattr->index) - value =clamp_val (value,0 , points[1 ] -1 ); -else - value =clamp_val (value, points[0 ] +1 , - data->sys_point3 -1 ); - points[sattr->index] = value; + points[sattr->index] =clamp_val (value, lo, hi); ret =ec_write (um780xtx_sys_point_offsets[sattr->index], points[sattr->index]);
base-commit: 4781ca52761e666cf18b591e6bb0478396c90320
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH] hwmon: (minisforum-um780xtx) Fix clamp() build failure with GCC 11
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] hwmon: minisforum-um780xtx: integer overflow in DIV_ROUND_CLOSEST --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] hwmon: minisforum-um780xtx: integer overflow in DIV_ROUND_CLOSEST --
commit 37b4941cfec26b0afeccdcd44aef6f32fe1e9d55
Author: Sebastián Peyrott <[email protected]>
hwmon: (minisforum-um780xtx) Fix clamp() build failure with GCC 11
This patch fixes a GCC 11 build failure by selecting clamping limits early and using a single clamp_val() execution. This prevents GCC jump threading from duplicating the limit check and firing an unreachable compile-time assertion.
Quoted 4 lines
drivers/hwmon/minisforum-um780xtx.c +0
-0
[ ... ]
Quoted 9 lines
drivers/hwmon/minisforum-um780xtx.c +5
-0@@ -198,13 +198,15 @@ static ssize_t um780xtx_sys_point_temp_store(struct device *dev,if (points[0 ] >= points[1 ] || points[1 ] >= data->sys_point3)return -EIO ; +if (!sattr->index) { + lo =0 ; + hi = points[1 ] -1 ; + }else { + lo = points[0 ] +1 ;
drivers/hwmon/minisforum-um780xtx.c static ssize_t um780xtx_sys_point_temp_store(struct device *dev, · line 209
Quoted 10 lines
drivers/hwmon/minisforum-um780xtx.c +3
-6+ hi = data->sys_point3 -1 ; + } value =DIV_ROUND_CLOSEST (value,1000 ); -if (!sattr->index) - value =clamp_val (value,0 , points[1 ] -1 ); -else - value =clamp_val (value, points[0 ] +1 , - data->sys_point3 -1 ); - points[sattr->index] = value; + points[sattr->index] =clamp_val (value, lo, hi);
[Severity: Low]
Can this arithmetic result in an integer overflow for extreme inputs?
When a user writes an extremely large value like LONG_MAX to the sysfs file in um780xtx_sys_point_temp_store(), the unbounded value is passed directly to DIV_ROUND_CLOSEST(value, 1000) before it is clamped.
Because DIV_ROUND_CLOSEST adds half the divisor before dividing, an extreme positive input can overflow and wrap around to a negative number. This would cause the value to incorrectly clamp to the minimum bound (lo) instead of the maximum bound (hi). Conversely, an extreme negative input could underflow and improperly clamp to hi.
Would it be safer to pre-clamp the value before calling DIV_ROUND_CLOSEST to prevent this overflow? Many other hwmon drivers clamp the input bounds first prior to performing the division to avoid this behavior.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH] hwmon: (minisforum-um780xtx) Fix clamp() build failure with GCC 11
In reply to Sebastián PeyrottQuoted 11 lines · Sebastián Peyrott
GCC 11 fails to build the driver:
error: call to '
__compiletime_assert_727' declared with attribute error:
clamp()low limit 0 greater than high limit points[1] - 1The store function returns
-EIOunless points[0] < points[1], so points[1] - 1 cannot be negative when the clamp is reached. GCC 11 jump threading, however, duplicates theclamp()limit check and does not prune the copy in which points[1] is zero, so the compile-time check inclamp()fires on an unreachable path. GCC 8 to 10 and 12 to 16 accept the code.
Quoted 8 lines
Select both limits first and use a single
clamp_val(). This does not change behavior.Fixes 9d980da93bbc ("hwmon: Add Minisforum UM780 XTX EC monitoring and fan control")Reported-by Paul E. McKenney <[email protected]>Assisted-by LLMSigned-off-by Sebastián Peyrott <[email protected]>
Thank you!!!
Though it would be *way* better to fold this back into the original commit to avoid breaking bisection.
Thanx, Paul
Quoted 60 lines
---
Reproduced on next-20261014 (x86_64) with GCC 11.5.0, both with allmodconfig and with defconfig plus this driver; GCC 8.5, 9.5, 10.5, 12.4, 13.4, 14.4, 15.3 and 16.1 build the unmodified driver. With this change, the driver builds without warnings with W=1 using GCC 11, 13, 14, 15 and 16 (x86_64allmodconfig) and GCC 16 (i386 allmodconfig). A small standalone reproducer of theclamp()pattern fails only with GCC 11, and the patched form builds with GCC 8 through 16.Tested on the UM780 XTX: the installed driver and this version produce identical rounding and clamping results for both system fan thresholds, with every value verified against the EC registers and the original thresholds restored afterwards. Module load and unload are also clean.
An LLM-based coding assistant helped reproduce and analyse the failure and prepare this patch. I reviewed the change and the test results.
drivers/hwmon/minisforum-um780xtx.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)drivers/hwmon/minisforum-um780xtx.c +9
-7@@ -180,7 +180,7 @@ static ssize_t um780xtx_sys_point_temp_store(struct device *dev,struct sensor_device_attribute *sattr =to_sensor_dev_attr (attr);u8 points[2 ];u8 readback; -long value; +long value, lo, hi;int ret; ret =kstrtol (buf,10 , &value);@@ -198,13 +198,15 @@ static ssize_t um780xtx_sys_point_temp_store(struct device *dev,if (points[0 ] >= points[1 ] || points[1 ] >= data->sys_point3)return -EIO ; +if (!sattr->index) { + lo =0 ; + hi = points[1 ] -1 ; + }else { + lo = points[0 ] +1 ; + hi = data->sys_point3 -1 ; + } value =DIV_ROUND_CLOSEST (value,1000 ); -if (!sattr->index) - value =clamp_val (value,0 , points[1 ] -1 ); -else - value =clamp_val (value, points[0 ] +1 , - data->sys_point3 -1 ); - points[sattr->index] = value; + points[sattr->index] =clamp_val (value, lo, hi); ret =ec_write (um780xtx_sys_point_offsets[sattr->index], points[sattr->index]);base-commit:
4781ca52761e666cf18b591e6bb0478396c90320Signature · 1 line
2.54.0
Guenter Roeck
maintainerRe: [PATCH] hwmon: (minisforum-um780xtx) Fix clamp() build failure with GCC 11
In reply to Sebastián PeyrottQuoted 14 lines · Sebastián Peyrott
GCC 11 fails to build the driver:
error: call to '
__compiletime_assert_727' declared with attribute error:
clamp()low limit 0 greater than high limit points[1] - 1The store function returns
-EIOunless points[0] < points[1], so points[1] - 1 cannot be negative when the clamp is reached. GCC 11 jump threading, however, duplicates theclamp()limit check and does not prune the copy in which points[1] is zero, so the compile-time check inclamp()fires on an unreachable path. GCC 8 to 10 and 12 to 16 accept the code.Select both limits first and use a single
clamp_val(). This does not change behavior.
Quoted 6 lines
Fixes 9d980da93bbc ("hwmon: Add Minisforum UM780 XTX EC monitoring and fan control")Reported-by Paul E. McKenney <[email protected]>Assisted-by LLMSigned-off-by Sebastián Peyrott <[email protected]>Tested-by Paul E. McKenney <[email protected]>
Folded into original patch, as suggested.
Thanks,
Guenter
Re: [PATCH] hwmon: (minisforum-um780xtx) Fix clamp() build failure with GCC 11
Quoted 15 lines · Guenter Roeck
Quoted 14 lines · Sebastián Peyrott
GCC 11 fails to build the driver:
error: call to '
__compiletime_assert_727' declared with attribute error:
clamp()low limit 0 greater than high limit points[1] - 1The store function returns
-EIOunless points[0] < points[1], so points[1] - 1 cannot be negative when the clamp is reached. GCC 11 jump threading, however, duplicates theclamp()limit check and does not prune the copy in which points[1] is zero, so the compile-time check inclamp()fires on an unreachable path. GCC 8 to 10 and 12 to 16 accept the code.Select both limits first and use a single
clamp_val(). This does not change behavior.
Quoted 8 lines
Quoted 6 lines
Fixes 9d980da93bbc ("hwmon: Add Minisforum UM780 XTX EC monitoring and fan control")Reported-by Paul E. McKenney <[email protected]>Assisted-by LLMSigned-off-by Sebastián Peyrott <[email protected]>Tested-by Paul E. McKenney <[email protected]>Folded into original patch, as suggested.
Thank you both!
Thanx, Paul