hwmon: emc2103: use min_t() for explicit type in fan target clamp
Subsystems (2)
PATCH 1/1 hwmon: emc2103: use min_t() for explicit type in fan target clamp
Using min() with an explicit cast on one operand is fragile. Replace with min_t(u16, ...) to make the intended comparison type explicit and avoid implicit type conversion.
--- drivers/hwmon/emc2103.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/emc2103.c +1 -1
@@ -348,7 +348,7 @@ static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,/* update fan target if high byte is not disabled */ if ((data->fan_target &0x1fe0 ) !=0x1fe0 ) {u16 new_target = (data->fan_target * old_div) / new_div; - data->fan_target =min (new_target, (u16 )0x1fff ); + data->fan_target =min_t (u16, new_target,0x1fff );write_fan_target_to_i2c (client, data->fan_target); }
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Clamping the calculated or user-provided fan target to 0x1fff overlaps with the 'disabled' state, inadvertently disabling fan RPM control for slow RPMs.
- [High] Lockless read of data->fan_multiplier leads to incorrect fan target scaling in fan1_div_store.
--
commit 78e3c12646374da73de8e3b7d0409be33a0d722f
Author: Animesh Rai <[email protected]>
hwmon: emc2103: use min_t() for explicit type in fan target clamp
This commit replaces min() with min_t() when clamping the fan target in fan1_div_store() to explicitly specify the comparison type and avoid implicit type conversions.
drivers/hwmon/emc2103.c static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da, · line 350
Quoted 8 lines
drivers/hwmon/emc2103.c +0
-0@@ -348,7 +348,7 @@ static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,/* update fan target if high byte is not disabled */ if ((data->fan_target &0x1fe0 ) !=0x1fe0 ) {u16 new_target = (data->fan_target * old_div) / new_div;
[Severity: High]
This is a pre-existing issue, but couldn't reading data->fan_multiplier locklessly to calculate old_div lead to an incorrect new_target calculation?
In fan1_div_store(), old_div is calculated as 8 / data->fan_multiplier before acquiring data->update_lock. If two threads write to the fan1_div sysfs attribute concurrently, won't the thread that acquires the lock second use a stale old_div value, corrupting the hardware fan target RPM state here?
drivers/hwmon/emc2103.c static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da, · line 351
Quoted 2 lines
drivers/hwmon/emc2103.c +1
-1- data->fan_target =min (new_target, (u16 )0x1fff ); + data->fan_target =min_t (u16, new_target,0x1fff );
[Severity: High]
This is a pre-existing issue, but does clamping to 0x1fff inadvertently disable fan RPM control for slow RPMs?
Since values >= 0x1fe0 result in a high byte of 0xff, this overlaps with the "disabled" state. When scaling the target beyond the 0x1fdf threshold, or when a user writes a very low RPM value in fan1_target_store(), clamping to 0x1fff instead of 0x1fdf seems to cause the fan to fall back to an uncontrolled state rather than its minimum speed.
Quoted 3 lines
drivers/hwmon/emc2103.c +0
-0write_fan_target_to_i2c (client, data->fan_target); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Guenter Roeck
maintainerQuoted 4 lines · Animesh Rai
Using
min()with an explicit cast on one operand is fragile. Replace withmin_t(u16, ...)to make the intended comparison type explicit and avoid implicit type conversion.
Sorry, I have no idea what you are trying to fix here. Both parameters are u16. There is nothing "fragile" about that.
Guenter
Quoted 18 lines
Signed-off-by Animesh Rai <[email protected]>--- drivers/hwmon/emc2103.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/hwmon/emc2103.c +1
-1@@ -348,7 +348,7 @@ static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,/* update fan target if high byte is not disabled */ if ((data->fan_target &0x1fe0 ) !=0x1fe0 ) {u16 new_target = (data->fan_target * old_div) / new_div; - data->fan_target =min (new_target, (u16 )0x1fff ); + data->fan_target =min_t (u16, new_target,0x1fff );write_fan_target_to_i2c (client, data->fan_target); }
Quoted 3 lines · On Mon, 6 Jul 2026 21:55:19 +0530 Animesh Rai <[email protected]> wrote:
Using
min()with an explicit cast on one operand is fragile. Replace withmin_t(u16, ...)to make the intended comparison type explicit and avoid implicit type conversion.
min_t() is worse than having a cast on the argument to min(). It just casts both arguments to the specified type.
If you'd tried you's have found you could have just deleted the cast. But why is new_target u16, it could just be 'unsigned int'. That saves a load of masking instructions.
Were old_div 9 and new_div 1 the rescale could overflow 16 bits, overflowing 32 is much less likely.
(The surrounding code may make the overflow impossible...)
If the code even right?
It ignores values 0x1fe0 to 0x1fff (assuming the high bits can't be set) so they must be 'special' in some way, but doesn't stop the same 'special' values being generated when rescaled.
David
Quoted 19 lines
Signed-off-by Animesh Rai <[email protected]>--- drivers/hwmon/emc2103.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/hwmon/emc2103.c +1
-1@@ -348,7 +348,7 @@ static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,/* update fan target if high byte is not disabled */ if ((data->fan_target &0x1fe0 ) !=0x1fe0 ) {u16 new_target = (data->fan_target * old_div) / new_div; - data->fan_target =min (new_target, (u16 )0x1fff ); + data->fan_target =min_t (u16, new_target,0x1fff );write_fan_target_to_i2c (client, data->fan_target); }
Sorry for the noise, I misread the types, the operand was already u16 and the cast was unnecessary. I once again apologize for wasting your time and disturbing you.
Quoted 44 lines · David Laight
Quoted 3 lines · On Mon, 6 Jul 2026 21:55:19 +0530 Animesh Rai <[email protected]> wrote:
Using
min()with an explicit cast on one operand is fragile. Replace withmin_t(u16, ...)to make the intended comparison type explicit and avoid implicit type conversion.
min_t()is worse than having a cast on the argument tomin(). It just casts both arguments to the specified type.
If you'd tried you's have found you could have just deleted the cast. But why isnew_targetu16, it could just be 'unsigned int'. That saves a load of masking instructions.
Wereold_div9 andnew_div1 the rescale could overflow 16 bits, overflowing 32 is much less likely.
(The surrounding code may make the overflow impossible...)If the code even right?
It ignores values0x1fe0to0x1fff(assuming the high bits can't be set) so they must be 'special' in some way, but doesn't stop the same 'special' values being generated when rescaled.David
Quoted 19 lines
Signed-off-by Animesh Rai <[email protected]>--- drivers/hwmon/emc2103.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/hwmon/emc2103.c +1
-1@@ -348,7 +348,7 @@ static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,/* update fan target if high byte is not disabled */ if ((data->fan_target &0x1fe0 ) !=0x1fe0 ) {u16 new_target = (data->fan_target * old_div) / new_div; - data->fan_target =min (new_target, (u16 )0x1fff ); + data->fan_target =min_t (u16, new_target,0x1fff );write_fan_target_to_i2c (client, data->fan_target); }
Guenter Roeck
maintainerQuoted 16 lines · David Laight
Quoted 3 lines · On Mon, 6 Jul 2026 21:55:19 +0530 Animesh Rai <[email protected]> wrote:
Using
min()with an explicit cast on one operand is fragile. Replace withmin_t(u16, ...)to make the intended comparison type explicit and avoid implicit type conversion.
min_t()is worse than having a cast on the argument tomin(). It just casts both arguments to the specified type.
If you'd tried you's have found you could have just deleted the cast. But why isnew_targetu16, it could just be 'unsigned int'. That saves a load of masking instructions.
Wereold_div9 andnew_div1 the rescale could overflow 16 bits, overflowing 32 is much less likely.
(The surrounding code may make the overflow impossible...)
Quoted 5 lines
If the code even right?
It ignores values0x1fe0to0x1fff(assuming the high bits can't be set) so they must be 'special' in some way, but doesn't stop the same 'special' values being generated when rescaled.
No, it isn't, as reported by Sashiko. So the patch does not improve anything while at the same time ignoring the real problems in the driver.
Guenter