Skip to contentSkip to conversation

hwmon: (adm9240) Fix overflows seen when writing into limit attributes

PATCH Superseded Guenter Roeck 2 participants 48 messages Dec 5, 2016 – Dec 13, 2016

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/17 hwmon: (adm9240) Fix overflows seen when writing into limit attributes

Module test reports:

in0_min: Suspected overflow: [3320 vs. 0]
in0_max: Suspected overflow: [3320 vs. 0]
in4_min: Suspected overflow: [15938 vs. 0]
in4_max: Suspected overflow: [15938 vs. 0]
temp1_max: Suspected overflow: [127000 vs. 0]
temp1_max_hyst: Suspected overflow: [127000 vs. 0]
aout_output: Suspected overflow: [1250 vs. 0]

Code analysis reveals that the overflows are caused by conversions from unsigned long to long to int, combined with multiplications on passed values.

---
 drivers/hwmon/adm9240.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/hwmon/adm9240.c +3 -0
index 2fe1828bd10b..347afacedcf5 100644
@@ -98,12 +98,14 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n)
 
 static inline u8 IN_TO_REG(unsigned long val, int n)
 {
+	val = clamp_val(val, 0, INT_MAX / 192 - 12000);
 	return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
 }
 
 /* temperature range: -40..125, 127 disables temperature alarm */
 static inline s8 TEMP_TO_REG(long val)
 {
+	val = clamp_val(val, INT_MIN + 1000, INT_MAX - 1000);
 	return clamp_val(SCALE(val, 1, 1000), -40, 127);
 }
 
@@ -122,6 +124,7 @@ static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
 /* analog out 0..1250mV */
 static inline u8 AOUT_TO_REG(unsigned long val)
 {
+	val = clamp_val(val, 0, INT_MAX / 255 - 1250);
 	return clamp_val(SCALE(val, 255, 1250), 0, 255);
 }
 
Signature · 1 line
2.5.0
author maintainer
#1 lore raw

PATCH 2/17 hwmon: (ds620) Fix overflows seen when writing temperature limits

Module test reports:

temp1_max: Suspected overflow: [160000 vs. 0]
temp1_min: Suspected overflow: [160000 vs. 0]

This is seen because the values passed when writing temperature limits are unbound.

---
 drivers/hwmon/ds620.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/ds620.c +1 -1
index edf550fc4eef..0043a4c02b85 100644
@@ -166,7 +166,7 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da,
 	if (res)
 		return res;
 
-	val = (val * 10 / 625) * 8;
+	val = (clamp_val(val, -128000, 128000) * 10 / 625) * 8;
 
 	mutex_lock(&data->update_lock);
 	data->temp[attr->index] = val;
Signature · 1 line
2.5.0
Reviewed-by on v1 2/17
#2 lore raw

Re: [PATCH 02/17] hwmon: (ds620) Fix overflows seen when writing temperature limits

Quoted 18 lines · Guenter Roeck

Module test reports:

temp1_max: Suspected overflow: [160000 vs. 0]
temp1_min: Suspected overflow: [160000 vs. 0]

This is seen because the values passed when writing temperature limits are unbound.

---
 drivers/hwmon/ds620.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/ds620.c +0 -0
index edf550fc4eef..0043a4c02b85 100644

drivers/hwmon/ds620.c static ssize_t set_temp(struct device *dev, struct device_attribute *da, · line 172

Quoted 8 lines
drivers/hwmon/ds620.c +1 -1
@@ -166,7 +166,7 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *da,
 	if (res)
 		return res;
 
-	val = (val * 10 / 625) * 8;
+	val = (clamp_val(val, -128000, 128000) * 10 / 625) * 8;
 
 	mutex_lock(&data->update_lock);
 	data->temp[attr->index] = val;
Fixes 6099469805c2 ("hwmon: Support for Dallas Semiconductor DS620")
Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#3 lore raw

PATCH 3/17 hwmon: (lm93) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Module test shows a large number of overflows, caused by missing clamps as well as various conversions between variable types.

Also fix temperature calculations for hysteresis and offset registers. For those, temperature calculations were a mix of millisecond and second based, causing reported and accepted hysteresis and offset temperatures to be widely off target.

This also changes the offset and base temperature attributes to be officially reported and set in milli-degrees C. This was already the case for the base temperature attribute, even though it was documented to be reported and set in degrees C.

---
 Documentation/hwmon/lm93 | 26 ++++++++++++-------------
 drivers/hwmon/lm93.c     | 49 +++++++++++++++++++++++++-----------------------
 2 files changed, 39 insertions(+), 36 deletions(-)
Documentation/hwmon/lm93 +13 -13
index f3b2ad2ceb01..7bda7f0291e4 100644
@@ -174,25 +174,25 @@ a "0" disables it. The h/w default is 0x0f (all temperatures bound).
 
 The function y = f(x) takes a source temperature x to a PWM output y.  This
 function of the LM93 is derived from a base temperature and a table of 12
-temperature offsets.  The base temperature is expressed in degrees C in the
-sysfs files temp<n>_auto_base.  The offsets are expressed in cumulative
-degrees C, with the value of offset <i> for temperature value <n> being
+temperature offsets.  The base temperature is expressed in milli-degrees C in
+the sysfs files temp<n>_auto_base.  The offsets are expressed in cumulative
+milli-degrees C, with the value of offset <i> for temperature value <n> being
 contained in the file temp<n>_auto_offset<i>.  E.g. if the base temperature
 is 40C:
 
      offset #	temp<n>_auto_offset<i>	range		pwm
 	 1		0		-		 25.00%
 	 2		0		-		 28.57%
-	 3		1		40C - 41C	 32.14%
-	 4		1		41C - 42C	 35.71%
-	 5		2		42C - 44C	 39.29%
-	 6		2		44C - 46C	 42.86%
-	 7		2		48C - 50C	 46.43%
-	 8		2		50C - 52C	 50.00%
-	 9		2		52C - 54C	 53.57%
-	10		2		54C - 56C	 57.14%
-	11		2		56C - 58C	 71.43%
-	12		2		58C - 60C	 85.71%
+	 3		500		40C - 41C	 32.14%
+	 4		500		41C - 42C	 35.71%
+	 5		1000		42C - 44C	 39.29%
+	 6		1000		44C - 46C	 42.86%
+	 7		1000		48C - 50C	 46.43%
+	 8		1000		50C - 52C	 50.00%
+	 9		1000		52C - 54C	 53.57%
+	10		1000		54C - 56C	 57.14%
+	11		1000		56C - 58C	 71.43%
+	12		1000		58C - 60C	 85.71%
 					> 60C		100.00%
 
 Valid offsets are in the range 0C <= x <= 7.5C in 0.5C increments.
drivers/hwmon/lm93.c +26 -23

118 lines not on this page · raw message

Signature · 1 line
2.5.0
Reviewed-by on v1 3/17
#4 lore raw

Re: [PATCH 03/17] hwmon: (lm93) Fix overflows seen when writing into limit attributes

Hi Guetner,

I kept the more tasty one for last ;-)

Quoted 50 lines · Guenter Roeck

Module test shows a large number of overflows, caused by missing clamps as well as various conversions between variable types.

Also fix temperature calculations for hysteresis and offset registers. For those, temperature calculations were a mix of millisecond and second based, causing reported and accepted hysteresis and offset temperatures to be widely off target.

This also changes the offset and base temperature attributes to be officially reported and set in milli-degrees C. This was already the case for the base temperature attribute, even though it was documented to be reported and set in degrees C.

---
 Documentation/hwmon/lm93 | 26 ++++++++++++-------------
 drivers/hwmon/lm93.c     | 49 +++++++++++++++++++++++++-----------------------
 2 files changed, 39 insertions(+), 36 deletions(-)
Documentation/hwmon/lm93 +4 -13
index f3b2ad2ceb01..7bda7f0291e4 100644
@@ -174,25 +174,25 @@ a "0" disables it. The h/w default is 0x0f (all temperatures bound).
 
 The function y = f(x) takes a source temperature x to a PWM output y.  This
 function of the LM93 is derived from a base temperature and a table of 12
-temperature offsets.  The base temperature is expressed in degrees C in the
-sysfs files temp<n>_auto_base.  The offsets are expressed in cumulative
-degrees C, with the value of offset <i> for temperature value <n> being
+temperature offsets.  The base temperature is expressed in milli-degrees C in
+the sysfs files temp<n>_auto_base.  The offsets are expressed in cumulative
+milli-degrees C, with the value of offset <i> for temperature value <n> being
 contained in the file temp<n>_auto_offset<i>.  E.g. if the base temperature
 is 40C:
 
      offset #	temp<n>_auto_offset<i>	range		pwm
 	 1		0		-		 25.00%
 	 2		0		-		 28.57%
-	 3		1		40C - 41C	 32.14%
-	 4		1		41C - 42C	 35.71%
-	 5		2		42C - 44C	 39.29%
-	 6		2		44C - 46C	 42.86%
-	 7		2		48C - 50C	 46.43%
-	 8		2		50C - 52C	 50.00%
-	 9		2		52C - 54C	 53.57%
-	10		2		54C - 56C	 57.14%
-	11		2		56C - 58C	 71.43%
-	12		2		58C - 60C	 85.71%
+	 3		500		40C - 41C	 32.14%

Documentation/hwmon/lm93 a "0" disables it. The h/w default is 0x0f (all temperatures bound). · line 196

Quoted 10 lines
Documentation/hwmon/lm93 +9 -0
+	 4		500		41C - 42C	 35.71%
+	 5		1000		42C - 44C	 39.29%
+	 6		1000		44C - 46C	 42.86%
+	 7		1000		48C - 50C	 46.43%
+	 8		1000		50C - 52C	 50.00%
+	 9		1000		52C - 54C	 53.57%
+	10		1000		54C - 56C	 57.14%
+	11		1000		56C - 58C	 71.43%
+	12		1000		58C - 60C	 85.71%
 					> 60C		100.00%

I'm a bit confused, I would have expected 1000 and 2000 for temp<n>_auto_offset<i>, not 500 and 1000? Otherwise I can't see how you get the announced ranges.

drivers/hwmon/lm93.c

Quoted 7 lines
Documentation/hwmon/lm93 +0 -0
 Valid offsets are in the range 0C <= x <= 7.5C in 0.5C increments.
drivers/hwmon/lm93.c +0 -0
index 90bb04858117..7b3152368e3b 100644
(...)

Code changes all look good to me, good job. Hairy code :-/

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#5 lore raw

Re: [PATCH 03/17] hwmon: (lm93) Fix overflows seen when writing into limit attributes

Quoted 61 lines · Jean Delvare

Hi Guetner,

I kept the more tasty one for last ;-)

Quoted 56 lines · Guenter Roeck

Module test shows a large number of overflows, caused by missing clamps as well as various conversions between variable types.

Also fix temperature calculations for hysteresis and offset registers. For those, temperature calculations were a mix of millisecond and second based, causing reported and accepted hysteresis and offset temperatures to be widely off target.

This also changes the offset and base temperature attributes to be officially reported and set in milli-degrees C. This was already the case for the base temperature attribute, even though it was documented to be reported and set in degrees C.

---
 Documentation/hwmon/lm93 | 26 ++++++++++++-------------
 drivers/hwmon/lm93.c     | 49 +++++++++++++++++++++++++-----------------------
 2 files changed, 39 insertions(+), 36 deletions(-)
Documentation/hwmon/lm93 +10 -13
index f3b2ad2ceb01..7bda7f0291e4 100644
@@ -174,25 +174,25 @@ a "0" disables it. The h/w default is 0x0f (all temperatures bound).
 
 The function y = f(x) takes a source temperature x to a PWM output y.  This
 function of the LM93 is derived from a base temperature and a table of 12
-temperature offsets.  The base temperature is expressed in degrees C in the
-sysfs files temp<n>_auto_base.  The offsets are expressed in cumulative
-degrees C, with the value of offset <i> for temperature value <n> being
+temperature offsets.  The base temperature is expressed in milli-degrees C in
+the sysfs files temp<n>_auto_base.  The offsets are expressed in cumulative
+milli-degrees C, with the value of offset <i> for temperature value <n> being
 contained in the file temp<n>_auto_offset<i>.  E.g. if the base temperature
 is 40C:
 
      offset #	temp<n>_auto_offset<i>	range		pwm
 	 1		0		-		 25.00%
 	 2		0		-		 28.57%
-	 3		1		40C - 41C	 32.14%
-	 4		1		41C - 42C	 35.71%
-	 5		2		42C - 44C	 39.29%
-	 6		2		44C - 46C	 42.86%
-	 7		2		48C - 50C	 46.43%
-	 8		2		50C - 52C	 50.00%
-	 9		2		52C - 54C	 53.57%
-	10		2		54C - 56C	 57.14%
-	11		2		56C - 58C	 71.43%
-	12		2		58C - 60C	 85.71%
+	 3		500		40C - 41C	 32.14%
+	 4		500		41C - 42C	 35.71%
+	 5		1000		42C - 44C	 39.29%
+	 6		1000		44C - 46C	 42.86%
+	 7		1000		48C - 50C	 46.43%
+	 8		1000		50C - 52C	 50.00%
+	 9		1000		52C - 54C	 53.57%
Quoted 9 lines

Documentation/hwmon/lm93 a "0" disables it. The h/w default is 0x0f (all temperatures bound). · line 196

Quoted 4 lines
Documentation/hwmon/lm93 +3 -0
+	10		1000		54C - 56C	 57.14%
+	11		1000		56C - 58C	 71.43%
+	12		1000		58C - 60C	 85.71%
 					> 60C		100.00%

I'm a bit confused, I would have expected 1000 and 2000 for temp<n>_auto_offset<i>, not 500 and 1000? Otherwise I can't see how you get the announced ranges.

Let me look into it again. The driver has some other issues with voltage limit ranges which I'll need to address as well. I'll hold this patch until after the rc - no need to hurry.

Quoted 10 lines

drivers/hwmon/lm93.c

Quoted 7 lines
Documentation/hwmon/lm93 +0 -0
 Valid offsets are in the range 0C <= x <= 7.5C in 0.5C increments.
drivers/hwmon/lm93.c +0 -0
index 90bb04858117..7b3152368e3b 100644
(...)

Code changes all look good to me, good job. Hairy code :-/

It most definitely is. Thanks!

Guenter

Quoted 2 lines
author maintainer
#6 lore raw

PATCH 4/17 hwmon: (smsc47m192) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Module test reports overflows when writing into temperature and voltage limit attributes

temp1_min: Suspected overflow: [127000 vs. 0]
temp1_max: Suspected overflow: [127000 vs. 0]
temp1_offset: Suspected overflow: [127000 vs. 0]
temp2_min: Suspected overflow: [127000 vs. 0]
temp2_max: Suspected overflow: [127000 vs. 0]
temp2_offset: Suspected overflow: [127000 vs. 0]
temp3_min: Suspected overflow: [127000 vs. 0]
temp3_max: Suspected overflow: [127000 vs. 0]
temp3_offset: Suspected overflow: [127000 vs. 0]
in0_min: Suspected overflow: [3320 vs. 0]
in0_max: Suspected overflow: [3320 vs. 0]
in4_min: Suspected overflow: [15938 vs. 0]
in4_max: Suspected overflow: [15938 vs. 0]
in6_min: Suspected overflow: [1992 vs. 0]
in6_max: Suspected overflow: [1992 vs. 0]
in7_min: Suspected overflow: [2391 vs. 0]
in7_max: Suspected overflow: [2391 vs. 0]

The problem is caused by conversions from unsigned long to long and from long to int.

---
 drivers/hwmon/smsc47m192.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/smsc47m192.c +2 -1
index 6ac7cda72d4c..202e167d6a59 100644
@@ -77,6 +77,7 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n)
 
 static inline u8 IN_TO_REG(unsigned long val, int n)
 {
+	val = clamp_val(val, 0, 65535);
 	return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
 }
 
@@ -84,7 +85,7 @@ static inline u8 IN_TO_REG(unsigned long val, int n)
  * TEMP: 0.001 degC units (-128C to +127C)
  * REG: 1C/bit, two's complement
  */
-static inline s8 TEMP_TO_REG(int val)
+static inline s8 TEMP_TO_REG(long val)
 {
 	return SCALE(clamp_val(val, -128000, 127000), 1, 1000);
 }
Signature · 1 line
2.5.0
Reviewed-by on v1 4/17
#7 lore raw

Re: [PATCH 04/17] hwmon: (smsc47m192) Fix overflows seen when writing into limit attributes

Quoted 34 lines · Guenter Roeck

Module test reports overflows when writing into temperature and voltage limit attributes

temp1_min: Suspected overflow: [127000 vs. 0]
temp1_max: Suspected overflow: [127000 vs. 0]
temp1_offset: Suspected overflow: [127000 vs. 0]
temp2_min: Suspected overflow: [127000 vs. 0]
temp2_max: Suspected overflow: [127000 vs. 0]
temp2_offset: Suspected overflow: [127000 vs. 0]
temp3_min: Suspected overflow: [127000 vs. 0]
temp3_max: Suspected overflow: [127000 vs. 0]
temp3_offset: Suspected overflow: [127000 vs. 0]
in0_min: Suspected overflow: [3320 vs. 0]
in0_max: Suspected overflow: [3320 vs. 0]
in4_min: Suspected overflow: [15938 vs. 0]
in4_max: Suspected overflow: [15938 vs. 0]
in6_min: Suspected overflow: [1992 vs. 0]
in6_max: Suspected overflow: [1992 vs. 0]
in7_min: Suspected overflow: [2391 vs. 0]
in7_max: Suspected overflow: [2391 vs. 0]

The problem is caused by conversions from unsigned long to long and from long to int.

---
 drivers/hwmon/smsc47m192.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/smsc47m192.c +0 -0
index 6ac7cda72d4c..202e167d6a59 100644

drivers/hwmon/smsc47m192.c static inline unsigned int IN_FROM_REG(u8 reg, int n) · line 82

Quoted 6 lines
drivers/hwmon/smsc47m192.c +1 -0
@@ -77,6 +77,7 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n)
 
 static inline u8 IN_TO_REG(unsigned long val, int n)
 {
+	val = clamp_val(val, 0, 65535);
 	return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
 }

Same as for adm9240, I would suggest to remove the second clamp_val():

static inline u8 IN_TO_REG(unsigned long val, int n)

{
	val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
	return SCALE(val, 192, nom_mv[n]);
}

drivers/hwmon/smsc47m192.c static inline u8 IN_TO_REG(unsigned long val, int n) · line 91

Quoted 10 lines
drivers/hwmon/smsc47m192.c +1 -1
@@ -84,7 +85,7 @@ static inline u8 IN_TO_REG(unsigned long val, int n)
  * TEMP: 0.001 degC units (-128C to +127C)
  * REG: 1C/bit, two's complement
  */
-static inline s8 TEMP_TO_REG(int val)
+static inline s8 TEMP_TO_REG(long val)
 {
 	return SCALE(clamp_val(val, -128000, 127000), 1, 1000);
 }

Other than this:

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#8 lore raw

Re: [PATCH 04/17] hwmon: (smsc47m192) Fix overflows seen when writing into limit attributes

Quoted 44 lines · Jean Delvare
Quoted 34 lines · Guenter Roeck

Module test reports overflows when writing into temperature and voltage limit attributes

temp1_min: Suspected overflow: [127000 vs. 0]
temp1_max: Suspected overflow: [127000 vs. 0]
temp1_offset: Suspected overflow: [127000 vs. 0]
temp2_min: Suspected overflow: [127000 vs. 0]
temp2_max: Suspected overflow: [127000 vs. 0]
temp2_offset: Suspected overflow: [127000 vs. 0]
temp3_min: Suspected overflow: [127000 vs. 0]
temp3_max: Suspected overflow: [127000 vs. 0]
temp3_offset: Suspected overflow: [127000 vs. 0]
in0_min: Suspected overflow: [3320 vs. 0]
in0_max: Suspected overflow: [3320 vs. 0]
in4_min: Suspected overflow: [15938 vs. 0]
in4_max: Suspected overflow: [15938 vs. 0]
in6_min: Suspected overflow: [1992 vs. 0]
in6_max: Suspected overflow: [1992 vs. 0]
in7_min: Suspected overflow: [2391 vs. 0]
in7_max: Suspected overflow: [2391 vs. 0]

The problem is caused by conversions from unsigned long to long and from long to int.

---
 drivers/hwmon/smsc47m192.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/smsc47m192.c +0 -0
index 6ac7cda72d4c..202e167d6a59 100644

drivers/hwmon/smsc47m192.c static inline unsigned int IN_FROM_REG(u8 reg, int n) · line 82

Quoted 6 lines
drivers/hwmon/smsc47m192.c +1 -0
@@ -77,6 +77,7 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n)
 
 static inline u8 IN_TO_REG(unsigned long val, int n)
 {
+	val = clamp_val(val, 0, 65535);
 	return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
 }

Same as for adm9240, I would suggest to remove the second clamp_val():

Quoted 6 lines

static inline u8 IN_TO_REG(unsigned long val, int n)

{
	val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
	return SCALE(val, 192, nom_mv[n]);
}

I updated the patch accordingly.

Thanks,
Guenter

Quoted 22 lines

drivers/hwmon/smsc47m192.c static inline u8 IN_TO_REG(unsigned long val, int n) · line 91

Quoted 10 lines
drivers/hwmon/smsc47m192.c +1 -1
@@ -84,7 +85,7 @@ static inline u8 IN_TO_REG(unsigned long val, int n)
  * TEMP: 0.001 degC units (-128C to +127C)
  * REG: 1C/bit, two's complement
  */
-static inline s8 TEMP_TO_REG(int val)
+static inline s8 TEMP_TO_REG(long val)
 {
 	return SCALE(clamp_val(val, -128000, 127000), 1, 1000);
 }

Other than this:

Signature · 6 lines
Jean Delvare
SUSE L3 Support
--
To unsubscribe from this list: send the line "unsubscribe linux-hwmon" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author maintainer
#9 lore raw

PATCH 5/17 hwmon: (adm1025) Fix overflows seen when writing voltage limits

In reply to Guenter Roeck

Writes into voltage limit attributes can overflow due to an unbound multiplication.

---
 drivers/hwmon/adm1025.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/adm1025.c +1 -1
index d6c767ace916..1abb4609b412 100644
@@ -93,7 +93,7 @@ static const int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
 
 #define IN_FROM_REG(reg, scale)	(((reg) * (scale) + 96) / 192)
 #define IN_TO_REG(val, scale)	((val) <= 0 ? 0 : \
-				 (val) * 192 >= (scale) * 255 ? 255 : \
+				 (val) >= (scale) * 255 / 192 ? 255 : \
 				 ((val) * 192 + (scale) / 2) / (scale))
 
 #define TEMP_FROM_REG(reg)	((reg) * 1000)
Signature · 1 line
2.5.0
Reviewed-by on v1 5/17
#10 lore raw

Re: [PATCH 05/17] hwmon: (adm1025) Fix overflows seen when writing voltage limits

Quoted 13 lines · Guenter Roeck

Writes into voltage limit attributes can overflow due to an unbound multiplication.

---
 drivers/hwmon/adm1025.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/adm1025.c +0 -0
index d6c767ace916..1abb4609b412 100644

drivers/hwmon/adm1025.c static const int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 }; · line 99

Quoted 8 lines
drivers/hwmon/adm1025.c +1 -1
@@ -93,7 +93,7 @@ static const int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
 
 #define IN_FROM_REG(reg, scale)	(((reg) * (scale) + 96) / 192)
 #define IN_TO_REG(val, scale)	((val) <= 0 ? 0 : \
-				 (val) * 192 >= (scale) * 255 ? 255 : \
+				 (val) >= (scale) * 255 / 192 ? 255 : \
 				 ((val) * 192 + (scale) / 2) / (scale))
 
 #define TEMP_FROM_REG(reg)	((reg) * 1000)
Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#11 lore raw

PATCH 6/17 hwmon: (adm1026) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Fix overflows seen when writing large values into voltage limit, temperature limit, temperature offset, and DAC attributes.

Overflows are seen due to unbound multiplications and additions.

---
 drivers/hwmon/adm1026.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/hwmon/adm1026.c +17 -9
index e67b9a50ac7c..b2a5d9e5c590 100644
@@ -197,8 +197,9 @@ static int adm1026_scaling[] = { /* .001 Volts */
 	};
 #define NEG12_OFFSET  16000
 #define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
-#define INS_TO_REG(n, val)  (clamp_val(SCALE(val, adm1026_scaling[n], 192),\
-	0, 255))
+#define INS_TO_REG(n, val)	\
+		SCALE(clamp_val(val, 0, 255 * adm1026_scaling[n] / 192), \
+		      adm1026_scaling[n], 192)
 #define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
 
 /*
@@ -215,11 +216,11 @@ static int adm1026_scaling[] = { /* .001 Volts */
 #define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
 
 /* Temperature is reported in 1 degC increments */
-#define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
-					/ 1000, -127, 127))
+#define TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
+					   1000)
 #define TEMP_FROM_REG(val) ((val) * 1000)
-#define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
-					  / 1000, -127, 127))
+#define OFFSET_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
+					     1000)
 #define OFFSET_FROM_REG(val) ((val) * 1000)
 
 #define PWM_TO_REG(val) (clamp_val(val, 0, 255))
@@ -233,7 +234,8 @@ static int adm1026_scaling[] = { /* .001 Volts */
  *   indicates that the DAC could be used to drive the fans, but in our
  *   example board (Arima HDAMA) it isn't connected to the fans at all.
  */
-#define DAC_TO_REG(val) (clamp_val(((((val) * 255) + 500) / 2500), 0, 255))
+#define DAC_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, 0, 2500) * 255, \
+					  2500)
 #define DAC_FROM_REG(val) (((val) * 2500) / 255)
 
 /*
@@ -593,7 +595,10 @@ static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
 		return err;
 
 	mutex_lock(&data->update_lock);
-	data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
+	data->in_min[16] = INS_TO_REG(16,
+				      clamp_val(val, INT_MIN,
+						INT_MAX - NEG12_OFFSET) +
+				      NEG12_OFFSET);
 	adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
 	mutex_unlock(&data->update_lock);
 	return count;
@@ -618,7 +623,10 @@ static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
 		return err;
 
 	mutex_lock(&data->update_lock);
-	data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
+	data->in_max[16] = INS_TO_REG(16,
+				      clamp_val(val, INT_MIN,
+						INT_MAX - NEG12_OFFSET) +
+				      NEG12_OFFSET);
 	adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
 	mutex_unlock(&data->update_lock);
 	return count;
Signature · 1 line
2.5.0
#12 lore raw

Re: [PATCH 06/17] hwmon: (adm1026) Fix overflows seen when writing into limit attributes

Quoted 29 lines · Guenter Roeck

Fix overflows seen when writing large values into voltage limit, temperature limit, temperature offset, and DAC attributes.

Overflows are seen due to unbound multiplications and additions.

---
 drivers/hwmon/adm1026.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/hwmon/adm1026.c +3 -2
index e67b9a50ac7c..b2a5d9e5c590 100644
@@ -197,8 +197,9 @@ static int adm1026_scaling[] = { /* .001 Volts */
 	};
 #define NEG12_OFFSET  16000
 #define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
-#define INS_TO_REG(n, val)  (clamp_val(SCALE(val, adm1026_scaling[n], 192),\
-	0, 255))
+#define INS_TO_REG(n, val)	\
+		SCALE(clamp_val(val, 0, 255 * adm1026_scaling[n] / 192), \
+		      adm1026_scaling[n], 192)
 #define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
 
 /*
@@ -215,11 +216,11 @@ static int adm1026_scaling[] = { /* .001 Volts */
 #define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
 

drivers/hwmon/adm1026.c static int adm1026_scaling[] = { /* .001 Volts */ · line 223

Quoted 10 lines
drivers/hwmon/adm1026.c +4 -4
 /* Temperature is reported in 1 degC increments */
-#define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
-					/ 1000, -127, 127))
+#define TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
+					   1000)
 #define TEMP_FROM_REG(val) ((val) * 1000)
-#define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
-					  / 1000, -127, 127))
+#define OFFSET_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
+					     1000)

Sorry for nitpicking but the original code had -127 °C as the negative limit. You are changing it to -128 °C without a justification. If it matters, it should be at least documented in the commit message. If not, it should be left as it was.

Quoted 27 lines
drivers/hwmon/adm1026.c +6 -2
 #define OFFSET_FROM_REG(val) ((val) * 1000)
 
 #define PWM_TO_REG(val) (clamp_val(val, 0, 255))
@@ -233,7 +234,8 @@ static int adm1026_scaling[] = { /* .001 Volts */
  *   indicates that the DAC could be used to drive the fans, but in our
  *   example board (Arima HDAMA) it isn't connected to the fans at all.
  */
-#define DAC_TO_REG(val) (clamp_val(((((val) * 255) + 500) / 2500), 0, 255))
+#define DAC_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, 0, 2500) * 255, \
+					  2500)
 #define DAC_FROM_REG(val) (((val) * 2500) / 255)
 
 /*
@@ -593,7 +595,10 @@ static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
 		return err;
 
 	mutex_lock(&data->update_lock);
-	data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
+	data->in_min[16] = INS_TO_REG(16,
+				      clamp_val(val, INT_MIN,
+						INT_MAX - NEG12_OFFSET) +
+				      NEG12_OFFSET);
 	adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
 	mutex_unlock(&data->update_lock);
 	return count;
@@ -618,7 +623,10 @@ static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
 		return err;

drivers/hwmon/adm1026.c static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, · line 632

Quoted 10 lines
drivers/hwmon/adm1026.c +4 -1
 
 	mutex_lock(&data->update_lock);
-	data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
+	data->in_max[16] = INS_TO_REG(16,
+				      clamp_val(val, INT_MIN,
+						INT_MAX - NEG12_OFFSET) +
+				      NEG12_OFFSET);
 	adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
 	mutex_unlock(&data->update_lock);
 	return count;

On these code paths, you end up calling clamp_val() twice. This could certainly be avoided, but I'm too lazy to do the math ;-)

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#13 lore raw

Re: [PATCH 06/17] hwmon: (adm1026) Fix overflows seen when writing into limit attributes

Hi Jean,

Quoted 30 lines · Jean Delvare
Quoted 29 lines · Guenter Roeck

Fix overflows seen when writing large values into voltage limit, temperature limit, temperature offset, and DAC attributes.

Overflows are seen due to unbound multiplications and additions.

---
 drivers/hwmon/adm1026.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/hwmon/adm1026.c +3 -2
index e67b9a50ac7c..b2a5d9e5c590 100644
@@ -197,8 +197,9 @@ static int adm1026_scaling[] = { /* .001 Volts */
 	};
 #define NEG12_OFFSET  16000
 #define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
-#define INS_TO_REG(n, val)  (clamp_val(SCALE(val, adm1026_scaling[n], 192),\
-	0, 255))
+#define INS_TO_REG(n, val)	\
+		SCALE(clamp_val(val, 0, 255 * adm1026_scaling[n] / 192), \
+		      adm1026_scaling[n], 192)
 #define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
 
 /*
@@ -215,11 +216,11 @@ static int adm1026_scaling[] = { /* .001 Volts */
 #define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
 
Quoted 16 lines

drivers/hwmon/adm1026.c static int adm1026_scaling[] = { /* .001 Volts */ · line 223

Quoted 10 lines
drivers/hwmon/adm1026.c +4 -4
 /* Temperature is reported in 1 degC increments */
-#define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
-					/ 1000, -127, 127))
+#define TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
+					   1000)
 #define TEMP_FROM_REG(val) ((val) * 1000)
-#define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
-					  / 1000, -127, 127))
+#define OFFSET_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
+					     1000)

Sorry for nitpicking but the original code had -127 °C as the negative limit. You are changing it to -128 °C without a justification. If it matters, it should be at least documented in the commit message. If not, it should be left as it was.

I had seen -128 as value reported by the chip with one of my register dumps, which messes up my module tests because it tries to rewrite the value it read into the attribute, and that fails. Also, the datasheet lists -128 degrees C as a valid register value.

This is one of those philosophical questions, for which I don't have a really good answer. Should we clamp to the register limits or to the chip specification ? I tend to clamp to register limits, because I think that it should always be possible to write back what was read, but we are highly inconsistent in the various drivers. Any opinion ?

Quoted 28 lines
Quoted 28 lines
drivers/hwmon/adm1026.c +6 -2
 #define OFFSET_FROM_REG(val) ((val) * 1000)
 
 #define PWM_TO_REG(val) (clamp_val(val, 0, 255))
@@ -233,7 +234,8 @@ static int adm1026_scaling[] = { /* .001 Volts */
  *   indicates that the DAC could be used to drive the fans, but in our
  *   example board (Arima HDAMA) it isn't connected to the fans at all.
  */
-#define DAC_TO_REG(val) (clamp_val(((((val) * 255) + 500) / 2500), 0, 255))
+#define DAC_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, 0, 2500) * 255, \
+					  2500)
 #define DAC_FROM_REG(val) (((val) * 2500) / 255)
 
 /*
@@ -593,7 +595,10 @@ static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
 		return err;
 
 	mutex_lock(&data->update_lock);
-	data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
+	data->in_min[16] = INS_TO_REG(16,
+				      clamp_val(val, INT_MIN,
+						INT_MAX - NEG12_OFFSET) +
+				      NEG12_OFFSET);
 	adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
 	mutex_unlock(&data->update_lock);
 	return count;
@@ -618,7 +623,10 @@ static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
 		return err;
 
Quoted 13 lines

drivers/hwmon/adm1026.c static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, · line 632

Quoted 9 lines
drivers/hwmon/adm1026.c +4 -1
 	mutex_lock(&data->update_lock);
-	data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
+	data->in_max[16] = INS_TO_REG(16,
+				      clamp_val(val, INT_MIN,
+						INT_MAX - NEG12_OFFSET) +
+				      NEG12_OFFSET);
 	adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
 	mutex_unlock(&data->update_lock);
 	return count;

On these code paths, you end up calling clamp_val() twice. This could certainly be avoided, but I'm too lazy to do the math ;-)

I know. Problem here is that there are two overflows, one in the calling code (since it does arithmetic on the input value), and another one in INS_TO_REG(). When I wrote this, I didn't have a good idea how to avoid the first overflow without a second clamp.

One possibility would be to define INS_TO_REG_NOCLAMP(). Would that be worth it ?

Thanks,
Guenter

#14 lore raw

Re: [PATCH 06/17] hwmon: (adm1026) Fix overflows seen when writing into limit attributes

Hi Guenter,

Quoted 21 lines · Guenter Roeck
Quoted 4 lines · Jean Delvare
Quoted 3 lines · Guenter Roeck
drivers/hwmon/adm1026.c +0 -0
@@ -215,11 +216,11 @@ static int adm1026_scaling[] = { /* .001 Volts */
 #define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
 
Quoted 15 lines

drivers/hwmon/adm1026.c static int adm1026_scaling[] = { /* .001 Volts */ · line 223

Quoted 10 lines
drivers/hwmon/adm1026.c +4 -4
 /* Temperature is reported in 1 degC increments */
-#define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
-					/ 1000, -127, 127))
+#define TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
+					   1000)
 #define TEMP_FROM_REG(val) ((val) * 1000)
-#define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
-					  / 1000, -127, 127))
+#define OFFSET_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
+					     1000)

Sorry for nitpicking but the original code had -127 °C as the negative limit. You are changing it to -128 °C without a justification. If it matters, it should be at least documented in the commit message. If not, it should be left as it was.

Quoted 4 lines

I had seen -128 as value reported by the chip with one of my register dumps, which messes up my module tests because it tries to rewrite the value it read into the attribute, and that fails. Also, the datasheet lists -128 degrees C as a valid register value.

OK, I understand.

Quoted 5 lines

This is one of those philosophical questions, for which I don't have a really good answer. Should we clamp to the register limits or to the chip specification ? I tend to clamp to register limits, because I think that it should always be possible to write back what was read, but we are highly inconsistent in the various drivers. Any opinion ?

I have noticed that inconsistency too. Given that we do not have attributes to expose the limits to user-space, I consider it a nice feature to clamp the requested limits to what the chip is actually specified for. But I don't have a strong opinion on it either, and am not going to spend time "fixing" all drivers not doing it. And you have a point with being able to write back what was read, especially if the power-on value isn't withing the specified limits.

Quoted 29 lines
Quoted 16 lines
Quoted 16 lines
drivers/hwmon/adm1026.c +4 -1
(...)
@@ -593,7 +595,10 @@ static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
 		return err;
 
 	mutex_lock(&data->update_lock);
-	data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
+	data->in_min[16] = INS_TO_REG(16,
+				      clamp_val(val, INT_MIN,
+						INT_MAX - NEG12_OFFSET) +
+				      NEG12_OFFSET);
 	adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
 	mutex_unlock(&data->update_lock);
 	return count;
@@ -618,7 +623,10 @@ static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
 		return err;
 
Quoted 13 lines

drivers/hwmon/adm1026.c static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, · line 632

Quoted 9 lines
drivers/hwmon/adm1026.c +4 -1
 	mutex_lock(&data->update_lock);
-	data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
+	data->in_max[16] = INS_TO_REG(16,
+				      clamp_val(val, INT_MIN,
+						INT_MAX - NEG12_OFFSET) +
+				      NEG12_OFFSET);
 	adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
 	mutex_unlock(&data->update_lock);
 	return count;

On these code paths, you end up calling clamp_val() twice. This could certainly be avoided, but I'm too lazy to do the math ;-)

Quoted 6 lines

I know. Problem here is that there are two overflows, one in the calling code (since it does arithmetic on the input value), and another one in INS_TO_REG(). When I wrote this, I didn't have a good idea how to avoid the first overflow without a second clamp.

One possibility would be to define INS_TO_REG_NOCLAMP(). Would that be worth it ?

No, don't bother. God only knows if there's any user left for this driver ;-)

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#15 lore raw

PATCH 7/17 hwmon: (adt7462) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Fix overflows seen when writing large values into temperature limit, voltage limit, and pwm hysteresis attributes.

The input parameter to DIV_ROUND_CLOSEST() needs to be clamped to avoid such overflows.

---
 drivers/hwmon/adt7462.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/hwmon/adt7462.c +6 -8
index 5929e126da63..0e9c8d3e8f5c 100644
@@ -810,8 +810,7 @@ static ssize_t set_temp_min(struct device *dev,
 	if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
-	temp = clamp_val(temp, 0, 255);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -64000, 191000), 1000) + 64;
 
 	mutex_lock(&data->lock);
 	data->temp_min[attr->index] = temp;
@@ -848,8 +847,7 @@ static ssize_t set_temp_max(struct device *dev,
 	if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
-	temp = clamp_val(temp, 0, 255);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -64000, 191000), 1000) + 64;
 
 	mutex_lock(&data->lock);
 	data->temp_max[attr->index] = temp;
@@ -912,6 +910,7 @@ static ssize_t set_volt_max(struct device *dev,
 	if (kstrtol(buf, 10, &temp) || !x)
 		return -EINVAL;
 
+	temp = clamp_val(temp, 0, INT_MAX / 1000 - x);
 	temp *= 1000; /* convert mV to uV */
 	temp = DIV_ROUND_CLOSEST(temp, x);
 	temp = clamp_val(temp, 0, 255);
@@ -954,6 +953,7 @@ static ssize_t set_volt_min(struct device *dev,
 	if (kstrtol(buf, 10, &temp) || !x)
 		return -EINVAL;
 
+	temp = clamp_val(temp, 0, INT_MAX / 1000 - x);
 	temp *= 1000; /* convert mV to uV */
 	temp = DIV_ROUND_CLOSEST(temp, x);
 	temp = clamp_val(temp, 0, 255);
@@ -1220,8 +1220,7 @@ static ssize_t set_pwm_hyst(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, 0, 15);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, 0, 15000), 1000);
 
 	/* package things up */
 	temp &= ADT7462_PWM_HYST_MASK;
@@ -1306,8 +1305,7 @@ static ssize_t set_pwm_tmin(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
-	temp = clamp_val(temp, 0, 255);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -64000, 191000), 1000) + 64;
 
 	mutex_lock(&data->lock);
 	data->pwm_tmin[attr->index] = temp;
Signature · 1 line
2.5.0
#16 lore raw

Re: [PATCH 07/17] hwmon: (adt7462) Fix overflows seen when writing into limit attributes

Quoted 26 lines · Guenter Roeck

Fix overflows seen when writing large values into temperature limit, voltage limit, and pwm hysteresis attributes.

The input parameter to DIV_ROUND_CLOSEST() needs to be clamped to avoid such overflows.

---
 drivers/hwmon/adt7462.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)
drivers/hwmon/adt7462.c +1 -2
index 5929e126da63..0e9c8d3e8f5c 100644
@@ -810,8 +810,7 @@ static ssize_t set_temp_min(struct device *dev,
 	if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
-	temp = clamp_val(temp, 0, 255);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -64000, 191000), 1000) + 64;
 
 	mutex_lock(&data->lock);
 	data->temp_min[attr->index] = temp;

drivers/hwmon/adt7462.c static ssize_t set_temp_max(struct device *dev, · line 853

Quoted 9 lines
drivers/hwmon/adt7462.c +1 -2
@@ -848,8 +847,7 @@ static ssize_t set_temp_max(struct device *dev,
 	if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
-	temp = clamp_val(temp, 0, 255);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -64000, 191000), 1000) + 64;
 
 	mutex_lock(&data->lock);
 	data->temp_max[attr->index] = temp;

The original code had the division and the clamping on separate lines, and I believe it makes the code more readable. It would also make the patch smaller and more readable as well.

Quoted 9 lines
drivers/hwmon/adt7462.c +1 -0
@@ -912,6 +910,7 @@ static ssize_t set_volt_max(struct device *dev,
 	if (kstrtol(buf, 10, &temp) || !x)
 		return -EINVAL;
 
+	temp = clamp_val(temp, 0, INT_MAX / 1000 - x);
 	temp *= 1000; /* convert mV to uV */
 	temp = DIV_ROUND_CLOSEST(temp, x);
 	temp = clamp_val(temp, 0, 255);

drivers/hwmon/adt7462.c static ssize_t set_volt_min(struct device *dev, · line 959

Quoted 7 lines
drivers/hwmon/adt7462.c +1 -0
@@ -954,6 +953,7 @@ static ssize_t set_volt_min(struct device *dev,
 	if (kstrtol(buf, 10, &temp) || !x)
 		return -EINVAL;
 
+	temp = clamp_val(temp, 0, INT_MAX / 1000 - x);
 	temp *= 1000; /* convert mV to uV */
 	temp = DIV_ROUND_CLOSEST(temp, x);
 	temp = clamp_val(temp, 0, 255);

Any chance to get the new clamp_val()s such that the second ones are no longer needed?

Quoted 11 lines
drivers/hwmon/adt7462.c +1 -2
@@ -1220,8 +1220,7 @@ static ssize_t set_pwm_hyst(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, 0, 15);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, 0, 15000), 1000);
 
 	/* package things up */
 	temp &= ADT7462_PWM_HYST_MASK;

drivers/hwmon/adt7462.c static ssize_t set_pwm_tmin(struct device *dev, · line 1311

Quoted 9 lines
drivers/hwmon/adt7462.c +1 -2
@@ -1306,8 +1305,7 @@ static ssize_t set_pwm_tmin(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
-	temp = clamp_val(temp, 0, 255);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -64000, 191000), 1000) + 64;
 
 	mutex_lock(&data->lock);
 	data->pwm_tmin[attr->index] = temp;

Same comment as first 2.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#17 lore raw

PATCH 8/17 hwmon: (adt7470) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Fix overflows seen when writing large values into various temperature limit attributes.

The input value passed to DIC_ROUND_CLOSEST() needs to be clamped to avoid such overflows.

---
 drivers/hwmon/adt7470.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/hwmon/adt7470.c +3 -6
index 6e60ca53406e..8996120b8170 100644
@@ -483,8 +483,7 @@ static ssize_t set_temp_min(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, -128, 127);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -128000, 127000), 1000);
 
 	mutex_lock(&data->lock);
 	data->temp_min[attr->index] = temp;
@@ -517,8 +516,7 @@ static ssize_t set_temp_max(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, -128, 127);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -128000, 127000), 1000);
 
 	mutex_lock(&data->lock);
 	data->temp_max[attr->index] = temp;
@@ -880,8 +878,7 @@ static ssize_t set_pwm_tmin(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, -128, 127);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -128000, 127000), 1000);
 
 	mutex_lock(&data->lock);
 	data->pwm_tmin[attr->index] = temp;
Signature · 1 line
2.5.0
Reviewed-by on v1 8/17
#18 lore raw

Re: [PATCH 08/17] hwmon: (adt7470) Fix overflows seen when writing into limit attributes

Quoted 36 lines · Guenter Roeck

Fix overflows seen when writing large values into various temperature limit attributes.

The input value passed to DIC_ROUND_CLOSEST() needs to be clamped to avoid such overflows.

---
 drivers/hwmon/adt7470.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/hwmon/adt7470.c +2 -4
index 6e60ca53406e..8996120b8170 100644
@@ -483,8 +483,7 @@ static ssize_t set_temp_min(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, -128, 127);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -128000, 127000), 1000);
 
 	mutex_lock(&data->lock);
 	data->temp_min[attr->index] = temp;
@@ -517,8 +516,7 @@ static ssize_t set_temp_max(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, -128, 127);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -128000, 127000), 1000);
 
 	mutex_lock(&data->lock);
 	data->temp_max[attr->index] = temp;

drivers/hwmon/adt7470.c static ssize_t set_pwm_tmin(struct device *dev, · line 884

Quoted 9 lines
drivers/hwmon/adt7470.c +1 -2
@@ -880,8 +878,7 @@ static ssize_t set_pwm_tmin(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, -128, 127);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -128000, 127000), 1000);
 
 	mutex_lock(&data->lock);
 	data->pwm_tmin[attr->index] = temp;

Seems more readable on 2 lines, but other than this:

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#19 lore raw

Re: [PATCH 08/17] hwmon: (adt7470) Fix overflows seen when writing into limit attributes

Quoted 40 lines · Jean Delvare
Quoted 39 lines · Guenter Roeck

Fix overflows seen when writing large values into various temperature limit attributes.

The input value passed to DIC_ROUND_CLOSEST() needs to be clamped to avoid such overflows.

---
 drivers/hwmon/adt7470.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
drivers/hwmon/adt7470.c +2 -4
index 6e60ca53406e..8996120b8170 100644
@@ -483,8 +483,7 @@ static ssize_t set_temp_min(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, -128, 127);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -128000, 127000), 1000);
 
 	mutex_lock(&data->lock);
 	data->temp_min[attr->index] = temp;
@@ -517,8 +516,7 @@ static ssize_t set_temp_max(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, -128, 127);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -128000, 127000), 1000);
 
 	mutex_lock(&data->lock);
 	data->temp_max[attr->index] = temp;
@@ -880,8 +878,7 @@ static ssize_t set_pwm_tmin(struct device *dev,
 	if (kstrtol(buf, 10, &temp))
 		return -EINVAL;
 
Quoted 9 lines

drivers/hwmon/adt7470.c static ssize_t set_pwm_tmin(struct device *dev, · line 884

Quoted 6 lines
drivers/hwmon/adt7470.c +1 -2
-	temp = DIV_ROUND_CLOSEST(temp, 1000);
-	temp = clamp_val(temp, -128, 127);
+	temp = DIV_ROUND_CLOSEST(clamp_val(temp, -128000, 127000), 1000);
 
 	mutex_lock(&data->lock);
 	data->pwm_tmin[attr->index] = temp;

Seems more readable on 2 lines, but other than this:

You are right. Split into two lines.

Quoted 2 lines

Thanks a lot for the review!

Guenter

author maintainer
#20 lore raw

PATCH 9/17 hwmon: (nct7802) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Fix overflows seen when writing voltage and temperature limit attributes.

The value passed to DIV_ROUND_CLOSEST() needs to be clamped.

---
 drivers/hwmon/nct7802.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/hwmon/nct7802.c +4 -3
index 3ce33d244cc0..6fe71cfe0320 100644
@@ -326,8 +326,9 @@ static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
 	int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
 	int err;
 
-	voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
-	voltage = clamp_val(voltage, 0, 0x3ff);
+	voltage = DIV_ROUND_CLOSEST(clamp_val(voltage, 0,
+					      0x3ff * nct7802_vmul[nr]),
+				    nct7802_vmul[nr]);
 
 	mutex_lock(&data->access_lock);
 	err = regmap_write(data->regmap,
@@ -402,7 +403,7 @@ static ssize_t store_temp(struct device *dev, struct device_attribute *attr,
 	if (err < 0)
 		return err;
 
-	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -127000, 127000), 1000);
 
 	err = regmap_write(data->regmap, nr, val & 0xff);
 	return err ? : count;
Signature · 1 line
2.5.0
#21 lore raw

Re: [PATCH 09/17] hwmon: (nct7802) Fix overflows seen when writing into limit attributes

Hi Guenter,

Quoted 5 lines · Guenter Roeck

Fix overflows seen when writing voltage and temperature limit attributes.

The value passed to DIV_ROUND_CLOSEST() needs to be clamped.

I think you can also add:

Fixes 3434f3783580 ("hwmon: Driver for Nuvoton NCT7802Y")
Quoted 9 lines
---
 drivers/hwmon/nct7802.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/hwmon/nct7802.c +0 -0
index 3ce33d244cc0..6fe71cfe0320 100644

drivers/hwmon/nct7802.c static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index, · line 331

Quoted 8 lines
drivers/hwmon/nct7802.c +3 -2
@@ -326,8 +326,9 @@ static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
 	int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
 	int err;
 
-	voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
-	voltage = clamp_val(voltage, 0, 0x3ff);
+	voltage = DIV_ROUND_CLOSEST(clamp_val(voltage, 0,
+					      0x3ff * nct7802_vmul[nr]),
+				    nct7802_vmul[nr]);

As for previous patches, I think separate lines make the code more readable.

drivers/hwmon/nct7802.c static ssize_t store_temp(struct device *dev, struct device_attribute *attr, · line 406

Quoted 9 lines
drivers/hwmon/nct7802.c +1 -1
 	mutex_lock(&data->access_lock);
 	err = regmap_write(data->regmap,
@@ -402,7 +403,7 @@ static ssize_t store_temp(struct device *dev, struct device_attribute *attr,
 	if (err < 0)
 		return err;
 
-	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -127000, 127000), 1000);

You change the low limit from -128 to -127 without justification.

drivers/hwmon/nct7802.c static ssize_t store_temp(struct device *dev, struct device_attribute *attr, · line 409

Quoted 3 lines
drivers/hwmon/nct7802.c +0 -0
 	err = regmap_write(data->regmap, nr, val & 0xff);
 	return err ? : count;

Looking at function nct7802_write_fan_min() I think an overflow can happen here too, as DIV_ROUND_CLOSEST() is called before clamp_val(). Any reason why you did not fix that one?

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#22 lore raw

Re: [PATCH 09/17] hwmon: (nct7802) Fix overflows seen when writing into limit attributes

Hi Jean,

Quoted 25 lines · Jean Delvare

Hi Guenter,

Quoted 5 lines · Guenter Roeck

Fix overflows seen when writing voltage and temperature limit attributes.

The value passed to DIV_ROUND_CLOSEST() needs to be clamped.

I think you can also add:

Fixes 3434f3783580 ("hwmon: Driver for Nuvoton NCT7802Y")
Quoted 12 lines
---
 drivers/hwmon/nct7802.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/hwmon/nct7802.c +0 -0
index 3ce33d244cc0..6fe71cfe0320 100644
@@ -326,8 +326,9 @@ static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
 	int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
 	int err;
 
Quoted 9 lines

drivers/hwmon/nct7802.c static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index, · line 331

Quoted 5 lines
drivers/hwmon/nct7802.c +3 -2
-	voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
-	voltage = clamp_val(voltage, 0, 0x3ff);
+	voltage = DIV_ROUND_CLOSEST(clamp_val(voltage, 0,
+					      0x3ff * nct7802_vmul[nr]),
+				    nct7802_vmul[nr]);

As for previous patches, I think separate lines make the code more readable.

Ok

Quoted 7 lines
Quoted 7 lines
drivers/hwmon/nct7802.c +0 -0
 	mutex_lock(&data->access_lock);
 	err = regmap_write(data->regmap,
@@ -402,7 +403,7 @@ static ssize_t store_temp(struct device *dev, struct device_attribute *attr,
 	if (err < 0)
 		return err;
 
Quoted 5 lines

drivers/hwmon/nct7802.c static ssize_t store_temp(struct device *dev, struct device_attribute *attr, · line 406

Quoted 2 lines
drivers/hwmon/nct7802.c +1 -1
-	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -127000, 127000), 1000);

You change the low limit from -128 to -127 without justification.

There is none. This is a bug.

Quoted 8 lines

drivers/hwmon/nct7802.c static ssize_t store_temp(struct device *dev, struct device_attribute *attr, · line 409

Quoted 3 lines
drivers/hwmon/nct7802.c +0 -0
 	err = regmap_write(data->regmap, nr, val & 0xff);
 	return err ? : count;

Looking at function nct7802_write_fan_min() I think an overflow can happen here too, as DIV_ROUND_CLOSEST() is called before clamp_val(). Any reason why you did not fix that one?

Not really. The call is

	DIV_ROUND_CLOSEST(1350000U, limit);

and thus won't overflow.

Thanks,
Guenter

#23 lore raw

Re: [PATCH 09/17] hwmon: (nct7802) Fix overflows seen when writing into limit attributes

Quoted 8 lines · Guenter Roeck
Quoted 4 lines · Jean Delvare

Looking at function nct7802_write_fan_min() I think an overflow can happen here too, as DIV_ROUND_CLOSEST() is called before clamp_val(). Any reason why you did not fix that one?

Not really. The call is

	DIV_ROUND_CLOSEST(1350000U, limit);

and thus won't overflow.

Limit is originally parsed by kstrtoul into an unsigned long, however the nct7802_write_fan_min function parameter is an unsigned int, so it is implicitly cast to an unsigned int. On a 32-bit system, that may not fit?

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#24 lore raw

Re: [PATCH 09/17] hwmon: (nct7802) Fix overflows seen when writing into limit attributes

Quoted 6 lines · Jean Delvare
Quoted 5 lines · Guenter Roeck
Quoted 4 lines · Jean Delvare

Looking at function nct7802_write_fan_min() I think an overflow can happen here too, as DIV_ROUND_CLOSEST() is called before clamp_val(). Any reason why you did not fix that one?

Quoted 9 lines
Quoted 3 lines

Not really. The call is

	DIV_ROUND_CLOSEST(1350000U, limit);

and thus won't overflow.

Limit is originally parsed by kstrtoul into an unsigned long, however the nct7802_write_fan_min function parameter is an unsigned int, so it is implicitly cast to an unsigned int. On a 32-bit system, that may not fit?

Good point. My module test doesn't catch that. Let me have a look if I can somehow trigger that failure.

Thanks,
Guenter

author maintainer
#25 lore raw

PATCH 10/17 hwmon: (lm87) Fix overflow seen when writing voltage limit attributes

In reply to Guenter Roeck

Writes into voltage limit attributes can overflow due to an unbound multiplication.

---
 drivers/hwmon/lm87.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/lm87.c +1 -1
index a5e295826aea..24f971b75e46 100644
@@ -121,7 +121,7 @@ static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
 
 #define IN_FROM_REG(reg, scale)	(((reg) * (scale) + 96) / 192)
 #define IN_TO_REG(val, scale)	((val) <= 0 ? 0 : \
-				 (val) * 192 >= (scale) * 255 ? 255 : \
+				 (val) >= (scale) * 255 / 192 ? 255 : \
 				 ((val) * 192 + (scale) / 2) / (scale))
 
 #define TEMP_FROM_REG(reg)	((reg) * 1000)
Signature · 1 line
2.5.0
Reviewed-by on v1 10/17
#26 lore raw

Re: [PATCH 10/17] hwmon: (lm87) Fix overflow seen when writing voltage limit attributes

Quoted 13 lines · Guenter Roeck

Writes into voltage limit attributes can overflow due to an unbound multiplication.

---
 drivers/hwmon/lm87.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/lm87.c +0 -0
index a5e295826aea..24f971b75e46 100644

drivers/hwmon/lm87.c static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C }; · line 127

Quoted 8 lines
drivers/hwmon/lm87.c +1 -1
@@ -121,7 +121,7 @@ static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
 
 #define IN_FROM_REG(reg, scale)	(((reg) * (scale) + 96) / 192)
 #define IN_TO_REG(val, scale)	((val) <= 0 ? 0 : \
-				 (val) * 192 >= (scale) * 255 ? 255 : \
+				 (val) >= (scale) * 255 / 192 ? 255 : \
 				 ((val) * 192 + (scale) / 2) / (scale))
 
 #define TEMP_FROM_REG(reg)	((reg) * 1000)
Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#27 lore raw

PATCH 11/17 hwmon: (lm85) Fix overflows seen when writing voltage limit attributes

In reply to Guenter Roeck

Writes into voltage limit attributes can overflow due to an unbound multiplication.

---
 drivers/hwmon/lm85.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/lm85.c +2 -1
index 6ff773fcaefb..29c8136ce9c5 100644
@@ -136,7 +136,8 @@ static const int lm85_scaling[] = {  /* .001 Volts */
 #define SCALE(val, from, to)	(((val) * (to) + ((from) / 2)) / (from))
 
 #define INS_TO_REG(n, val)	\
-		clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255)
+		SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \
+		      lm85_scaling[n], 192)
 
 #define INSEXT_FROM_REG(n, val, ext)	\
 		SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Signature · 1 line
2.5.0
Reviewed-by on v1 11/17
#28 lore raw

Re: [PATCH 11/17] hwmon: (lm85) Fix overflows seen when writing voltage limit attributes

Quoted 13 lines · Guenter Roeck

Writes into voltage limit attributes can overflow due to an unbound multiplication.

---
 drivers/hwmon/lm85.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hwmon/lm85.c +0 -0
index 6ff773fcaefb..29c8136ce9c5 100644

drivers/hwmon/lm85.c static const int lm85_scaling[] = { /* .001 Volts */ · line 143

Quoted 9 lines
drivers/hwmon/lm85.c +2 -1
@@ -136,7 +136,8 @@ static const int lm85_scaling[] = {  /* .001 Volts */
 #define SCALE(val, from, to)	(((val) * (to) + ((from) / 2)) / (from))
 
 #define INS_TO_REG(n, val)	\
-		clamp_val(SCALE(val, lm85_scaling[n], 192), 0, 255)
+		SCALE(clamp_val(val, 0, 255 * lm85_scaling[n] / 192), \
+		      lm85_scaling[n], 192)
 
 #define INSEXT_FROM_REG(n, val, ext)	\
 		SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#29 lore raw

PATCH 12/17 hwmon: (dme1737) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Writes into voltage limit, temperature limit, and temperature zone attributes can overflow due to unchecked parameters to multiplications and additions.

Cc Juerg Haefliger <[email protected]>
---
 drivers/hwmon/dme1737.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/hwmon/dme1737.c +5 -2
index 8763c4a8280c..29d082c12c74 100644
@@ -279,7 +279,8 @@ static inline int IN_FROM_REG(int reg, int nominal, int res)
 
 static inline int IN_TO_REG(long val, int nominal)
 {
-	return clamp_val((val * 192 + nominal / 2) / nominal, 0, 255);
+	return DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * nominal / 192) * 192,
+				 nominal);
 }
 
 /*
@@ -295,7 +296,7 @@ static inline int TEMP_FROM_REG(int reg, int res)
 
 static inline int TEMP_TO_REG(long val)
 {
-	return clamp_val((val < 0 ? val - 500 : val + 500) / 1000, -128, 127);
+	return DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
 }
 
 /* Temperature range */
@@ -1028,6 +1029,8 @@ static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
 	if (err)
 		return err;
 
+	val = clamp_val(val, -256000, 255000);
+
 	mutex_lock(&data->update_lock);
 	switch (fn) {
 	case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
Signature · 1 line
2.5.0
#30 lore raw

Re: [PATCH 12/17] hwmon: (dme1737) Fix overflows seen when writing into limit attributes

Hi Guenter,

Quoted 34 lines · Guenter Roeck

Writes into voltage limit, temperature limit, and temperature zone attributes can overflow due to unchecked parameters to multiplications and additions.

Cc Juerg Haefliger <[email protected]>
---
 drivers/hwmon/dme1737.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/hwmon/dme1737.c +3 -2
index 8763c4a8280c..29d082c12c74 100644
@@ -279,7 +279,8 @@ static inline int IN_FROM_REG(int reg, int nominal, int res)
 
 static inline int IN_TO_REG(long val, int nominal)
 {
-	return clamp_val((val * 192 + nominal / 2) / nominal, 0, 255);
+	return DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * nominal / 192) * 192,
+				 nominal);
 }
 
 /*
@@ -295,7 +296,7 @@ static inline int TEMP_FROM_REG(int reg, int res)
 
 static inline int TEMP_TO_REG(long val)
 {
-	return clamp_val((val < 0 ? val - 500 : val + 500) / 1000, -128, 127);
+	return DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
 }
 
 /* Temperature range */

drivers/hwmon/dme1737.c static ssize_t set_zone(struct device *dev, struct device_attribute *attr, · line 1032

Quoted 4 lines
drivers/hwmon/dme1737.c +1 -0
@@ -1028,6 +1029,8 @@ static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
 	if (err)
 		return err;
 
+	val = clamp_val(val, -256000, 255000);

Where do these values come from? I would have naively expected the auto-pwm temperature values to have the same range as the temperature channels themselves.

Also in the case of SYS_ZONE_AUTO_POINT1_TEMP and SYS_ZONE_AUTO_POINT3_TEMP, TEMP_TO_REG() is called, which already performs the clamping, so it is redundant. So maybe it would be better to have a "dedicated" clamp for the SYS_ZONE_AUTO_POINT1_TEMP_HYST and SYS_ZONE_AUTO_POINT2_TEMP cases?

Quoted 4 lines
drivers/hwmon/dme1737.c +1 -0
+
 	mutex_lock(&data->update_lock);
 	switch (fn) {
 	case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#31 lore raw

Re: [PATCH 12/17] hwmon: (dme1737) Fix overflows seen when writing into limit attributes

Quoted 40 lines · Jean Delvare

Hi Guenter,

Quoted 37 lines · Guenter Roeck

Writes into voltage limit, temperature limit, and temperature zone attributes can overflow due to unchecked parameters to multiplications and additions.

Cc Juerg Haefliger <[email protected]>
---
 drivers/hwmon/dme1737.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/hwmon/dme1737.c +3 -2
index 8763c4a8280c..29d082c12c74 100644
@@ -279,7 +279,8 @@ static inline int IN_FROM_REG(int reg, int nominal, int res)
 
 static inline int IN_TO_REG(long val, int nominal)
 {
-	return clamp_val((val * 192 + nominal / 2) / nominal, 0, 255);
+	return DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * nominal / 192) * 192,
+				 nominal);
 }
 
 /*
@@ -295,7 +296,7 @@ static inline int TEMP_FROM_REG(int reg, int res)
 
 static inline int TEMP_TO_REG(long val)
 {
-	return clamp_val((val < 0 ? val - 500 : val + 500) / 1000, -128, 127);
+	return DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
 }
 
 /* Temperature range */
@@ -1028,6 +1029,8 @@ static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
 	if (err)
 		return err;
 
Quoted 5 lines

drivers/hwmon/dme1737.c static ssize_t set_zone(struct device *dev, struct device_attribute *attr, · line 1032

Quoted 1 line
drivers/hwmon/dme1737.c +1 -0
+	val = clamp_val(val, -256000, 255000);

Where do these values come from? I would have naively expected the auto-pwm temperature values to have the same range as the temperature channels themselves.

Not related to real limits, just a "basic" clamp since the real clamp happens later anyway.

Quoted 7 lines

Also in the case of SYS_ZONE_AUTO_POINT1_TEMP and SYS_ZONE_AUTO_POINT3_TEMP, TEMP_TO_REG() is called, which already performs the clamping, so it is redundant. So maybe it would be better to have a "dedicated" clamp for the SYS_ZONE_AUTO_POINT1_TEMP_HYST and SYS_ZONE_AUTO_POINT2_TEMP cases?

Yrd, I noticed. Makes sense. Let me think about it.

Thanks,
Guenter

Quoted 6 lines
Quoted 4 lines
drivers/hwmon/dme1737.c +1 -0
+
 	mutex_lock(&data->update_lock);
 	switch (fn) {
 	case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
author maintainer
#32 lore raw

PATCH 13/17 hwmon: (emc2103) Fix overflows seen when temperature limit attributes

In reply to Guenter Roeck

Writes into temperature limit attributes can overflow due to unbound values passed to DIV_ROUND_CLOSEST().

Cc Steve Glendinning <[email protected]>
---
 drivers/hwmon/emc2103.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/emc2103.c +2 -2
index 24e395c5907d..4b870ee9b0d3 100644
@@ -251,7 +251,7 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *da,
 	if (result < 0)
 		return result;
 
-	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
 
 	mutex_lock(&data->update_lock);
 	data->temp_min[nr] = val;
@@ -273,7 +273,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
 	if (result < 0)
 		return result;
 
-	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
 
 	mutex_lock(&data->update_lock);
 	data->temp_max[nr] = val;
Signature · 1 line
2.5.0
Reviewed-by on v1 13/17
#33 lore raw

Re: [PATCH 13/17] hwmon: (emc2103) Fix overflows seen when temperature limit attributes

Quoted 23 lines · Guenter Roeck

Writes into temperature limit attributes can overflow due to unbound values passed to DIV_ROUND_CLOSEST().

Cc Steve Glendinning <[email protected]>
---
 drivers/hwmon/emc2103.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/emc2103.c +1 -1
index 24e395c5907d..4b870ee9b0d3 100644
@@ -251,7 +251,7 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *da,
 	if (result < 0)
 		return result;
 
-	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
 
 	mutex_lock(&data->update_lock);
 	data->temp_min[nr] = val;

drivers/hwmon/emc2103.c static ssize_t set_temp_max(struct device *dev, struct device_attribute *da, · line 279

Quoted 8 lines
drivers/hwmon/emc2103.c +1 -1
@@ -273,7 +273,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
 	if (result < 0)
 		return result;
 
-	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
 
 	mutex_lock(&data->update_lock);
 	data->temp_max[nr] = val;
Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#34 lore raw

PATCH 14/17 hwmon: (emcw201) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Writes into temperature and voltage limit attributes can overflow due to multiplications with unchecked parameters. Also, the input parameter to DIV_ROUND_CLOSEST() needis to be range checked.

---
 drivers/hwmon/emc6w201.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hwmon/emc6w201.c +5 -4
index f37fe2011640..f8c776c49df9 100644
@@ -215,12 +215,13 @@ static ssize_t set_in(struct device *dev, struct device_attribute *devattr,
 	if (err < 0)
 		return err;
 
-	val = DIV_ROUND_CLOSEST(val * 0xC0, nominal_mv[nr]);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * nominal_mv[nr] / 192)
+				* 192, nominal_mv[nr]);
 	reg = (sf == min) ? EMC6W201_REG_IN_LOW(nr)
 			  : EMC6W201_REG_IN_HIGH(nr);
 
 	mutex_lock(&data->update_lock);
-	data->in[sf][nr] = clamp_val(val, 0, 255);
+	data->in[sf][nr] = val;
 	err = emc6w201_write8(client, reg, data->in[sf][nr]);
 	mutex_unlock(&data->update_lock);
 
@@ -252,12 +253,12 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
 	if (err < 0)
 		return err;
 
-	val = DIV_ROUND_CLOSEST(val, 1000);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -127000, 127000), 1000);
 	reg = (sf == min) ? EMC6W201_REG_TEMP_LOW(nr)
 			  : EMC6W201_REG_TEMP_HIGH(nr);
 
 	mutex_lock(&data->update_lock);
-	data->temp[sf][nr] = clamp_val(val, -127, 127);
+	data->temp[sf][nr] = val;
 	err = emc6w201_write8(client, reg, data->temp[sf][nr]);
 	mutex_unlock(&data->update_lock);
 
Signature · 1 line
2.5.0
Reviewed-by on v1 14/17
#35 lore raw

Re: [PATCH 14/17] hwmon: (emcw201) Fix overflows seen when writing into limit attributes

Quoted 34 lines · Guenter Roeck

Writes into temperature and voltage limit attributes can overflow due to multiplications with unchecked parameters. Also, the input parameter to DIV_ROUND_CLOSEST() needis to be range checked.

---
 drivers/hwmon/emc6w201.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hwmon/emc6w201.c +3 -3
index f37fe2011640..f8c776c49df9 100644
@@ -215,12 +215,13 @@ static ssize_t set_in(struct device *dev, struct device_attribute *devattr,
 	if (err < 0)
 		return err;
 
-	val = DIV_ROUND_CLOSEST(val * 0xC0, nominal_mv[nr]);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * nominal_mv[nr] / 192)
+				* 192, nominal_mv[nr]);
 	reg = (sf == min) ? EMC6W201_REG_IN_LOW(nr)
 			  : EMC6W201_REG_IN_HIGH(nr);
 
 	mutex_lock(&data->update_lock);
-	data->in[sf][nr] = clamp_val(val, 0, 255);
+	data->in[sf][nr] = val;
 	err = emc6w201_write8(client, reg, data->in[sf][nr]);
 	mutex_unlock(&data->update_lock);
 
@@ -252,12 +253,12 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
 	if (err < 0)
 		return err;
 
-	val = DIV_ROUND_CLOSEST(val, 1000);

drivers/hwmon/emc6w201.c static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, · line 264

Quoted 10 lines
drivers/hwmon/emc6w201.c +2 -1
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -127000, 127000), 1000);
 	reg = (sf == min) ? EMC6W201_REG_TEMP_LOW(nr)
 			  : EMC6W201_REG_TEMP_HIGH(nr);
 
 	mutex_lock(&data->update_lock);
-	data->temp[sf][nr] = clamp_val(val, -127, 127);
+	data->temp[sf][nr] = val;
 	err = emc6w201_write8(client, reg, data->temp[sf][nr]);
 	mutex_unlock(&data->update_lock);
 

Keep on separate lines? Other than this, looks good:

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#36 lore raw

Re: [PATCH 14/17] hwmon: (emcw201) Fix overflows seen when writing into limit attributes

Quoted 39 lines · Jean Delvare
Quoted 38 lines · Guenter Roeck

Writes into temperature and voltage limit attributes can overflow due to multiplications with unchecked parameters. Also, the input parameter to DIV_ROUND_CLOSEST() needis to be range checked.

---
 drivers/hwmon/emc6w201.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hwmon/emc6w201.c +4 -3
index f37fe2011640..f8c776c49df9 100644
@@ -215,12 +215,13 @@ static ssize_t set_in(struct device *dev, struct device_attribute *devattr,
 	if (err < 0)
 		return err;
 
-	val = DIV_ROUND_CLOSEST(val * 0xC0, nominal_mv[nr]);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * nominal_mv[nr] / 192)
+				* 192, nominal_mv[nr]);
 	reg = (sf == min) ? EMC6W201_REG_IN_LOW(nr)
 			  : EMC6W201_REG_IN_HIGH(nr);
 
 	mutex_lock(&data->update_lock);
-	data->in[sf][nr] = clamp_val(val, 0, 255);
+	data->in[sf][nr] = val;
 	err = emc6w201_write8(client, reg, data->in[sf][nr]);
 	mutex_unlock(&data->update_lock);
 
@@ -252,12 +253,12 @@ static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
 	if (err < 0)
 		return err;
 
-	val = DIV_ROUND_CLOSEST(val, 1000);
+	val = DIV_ROUND_CLOSEST(clamp_val(val, -127000, 127000), 1000);
 	reg = (sf == min) ? EMC6W201_REG_TEMP_LOW(nr)
 			  : EMC6W201_REG_TEMP_HIGH(nr);
 
Quoted 9 lines

drivers/hwmon/emc6w201.c static ssize_t set_temp(struct device *dev, struct device_attribute *devattr, · line 264

Quoted 6 lines
drivers/hwmon/emc6w201.c +1 -1
 	mutex_lock(&data->update_lock);
-	data->temp[sf][nr] = clamp_val(val, -127, 127);
+	data->temp[sf][nr] = val;
 	err = emc6w201_write8(client, reg, data->temp[sf][nr]);
 	mutex_unlock(&data->update_lock);
 

Keep on separate lines? Other than this, looks good:

Yes, makes sense. Done.

Thanks,
Guenter

Quoted 2 lines
author maintainer
#37 lore raw

PATCH 15/17 hwmln: (g762) Fix overflows and crash seen when writing limit attributes

In reply to Guenter Roeck

Fix overflows seen when writing into fan speed limit attributes. Also fix crash due to division by zero, seen when certain very large values (such as 4611686018427387904, or 0x4000000000000000) are written into fan speed limit attributes.

Fixes 594fbe713bf60 ("Add support for GMT G762/G763 PWM fan controllers")
---
 drivers/hwmon/g762.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/hwmon/g762.c +3 -2
index b96a2a9e4df7..584f9f755a92 100644
@@ -193,12 +193,13 @@ static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
  * Convert fan RPM value from sysfs into count value for fan controller
  * register (FAN_SET_CNT).
  */
-static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p,
+static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p,
 					 u8 clk_div, u8 gear_mult)
 {
-	if (!rpm)         /* to stop the fan, set cnt to 255 */
+	if (!rpm || !p || !clk_div)	/* to stop the fan, set cnt to 255 */
 		return 0xff;
 
+	rpm = clamp_val(rpm, 1, ULONG_MAX / (p * clk_div));
 	return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)),
 			 0, 255);
 }
Signature · 1 line
2.5.0
Reviewed-by on v1 15/17
#38 lore raw

Re: [PATCH 15/17] hwmln: (g762) Fix overflows and crash seen when writing limit attributes

Hi Guenter,

There's a typo in the subject (hwmln -> hwmon.)

Quoted 16 lines · Guenter Roeck

Fix overflows seen when writing into fan speed limit attributes. Also fix crash due to division by zero, seen when certain very large values (such as 4611686018427387904, or 0x4000000000000000) are written into fan speed limit attributes.

Fixes 594fbe713bf60 ("Add support for GMT G762/G763 PWM fan controllers")
---
 drivers/hwmon/g762.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/hwmon/g762.c +0 -0
index b96a2a9e4df7..584f9f755a92 100644

drivers/hwmon/g762.c static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p, · line 200

Quoted 10 lines
drivers/hwmon/g762.c +2 -2
@@ -193,12 +193,13 @@ static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
  * Convert fan RPM value from sysfs into count value for fan controller
  * register (FAN_SET_CNT).
  */
-static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p,
+static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p,
 					 u8 clk_div, u8 gear_mult)
 {
-	if (!rpm)         /* to stop the fan, set cnt to 255 */
+	if (!rpm || !p || !clk_div)	/* to stop the fan, set cnt to 255 */
 		return 0xff;

Looking at the definitions of G762_PULSE_FROM_REG and G762_CLKDIV_FROM_REG, I can't see how either p or clk_div would be 0. So this change seems unneeded to me. The division by zero you have seen would be the consequence of an overflow (fixed below.)

drivers/hwmon/g762.c static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p, · line 205

Quoted 5 lines
drivers/hwmon/g762.c +1 -0
+	rpm = clamp_val(rpm, 1, ULONG_MAX / (p * clk_div));
 	return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)),
 			 0, 255);
 }

If my math is right, this can be simplified to:

	rpm = clamp_val(rpm, 1, (clk_freq * 30 * gear_mult) / (255  * p * clk_div));
	return (clk_freq * 30 * gear_mult) / (rpm * p * clk_div);

which may or may not actually be more simple ;-) as it avoids the double clamping only at the cost of extra arithmetic. Probably only worth it if we store the result of clk_freq * 30 * gear_mult and p * clk_div locally so we don't have to compute them again.

Probably doesn't matter much in the end, so it's your call.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#39 lore raw

Re: [PATCH 15/17] hwmln: (g762) Fix overflows and crash seen when writing limit attributes

Hi Jean,

Quoted 28 lines · Jean Delvare

Hi Guenter,

There's a typo in the subject (hwmln -> hwmon.)

Quoted 23 lines · Guenter Roeck

Fix overflows seen when writing into fan speed limit attributes. Also fix crash due to division by zero, seen when certain very large values (such as 4611686018427387904, or 0x4000000000000000) are written into fan speed limit attributes.

Fixes 594fbe713bf60 ("Add support for GMT G762/G763 PWM fan controllers")
---
 drivers/hwmon/g762.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/hwmon/g762.c +1 -1
index b96a2a9e4df7..584f9f755a92 100644
@@ -193,12 +193,13 @@ static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p,
  * Convert fan RPM value from sysfs into count value for fan controller
  * register (FAN_SET_CNT).
  */
-static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p,
+static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p,
 					 u8 clk_div, u8 gear_mult)
 {
Quoted 9 lines

drivers/hwmon/g762.c static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p, · line 200

Quoted 3 lines
drivers/hwmon/g762.c +1 -1
-	if (!rpm)         /* to stop the fan, set cnt to 255 */
+	if (!rpm || !p || !clk_div)	/* to stop the fan, set cnt to 255 */
 		return 0xff;

Looking at the definitions of G762_PULSE_FROM_REG and G762_CLKDIV_FROM_REG, I can't see how either p or clk_div would be 0. So this change seems unneeded to me. The division by zero you have seen would be the consequence of an overflow (fixed below.)

Yes, you are right. I dropped that part.

Quoted 8 lines

drivers/hwmon/g762.c static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p, · line 205

Quoted 5 lines
drivers/hwmon/g762.c +1 -0
+	rpm = clamp_val(rpm, 1, ULONG_MAX / (p * clk_div));
 	return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)),
 			 0, 255);
 }

If my math is right, this can be simplified to:

I wrote test code to compare the old calculation and the new one for the complete value range (I didn't trust my math skills :-), and it reports the same results. So it should be correct.

Quoted 8 lines
	rpm = clamp_val(rpm, 1, (clk_freq * 30 * gear_mult) / (255  * p * clk_div));
	return (clk_freq * 30 * gear_mult) / (rpm * p * clk_div);

which may or may not actually be more simple ;-) as it avoids the double clamping only at the cost of extra arithmetic. Probably only worth it if we store the result of clk_freq * 30 * gear_mult and p * clk_div locally so we don't have to compute them again.

Excellent idea. I'll make that change.

Thanks,
Guenter

Quoted 4 lines

Probably doesn't matter much in the end, so it's your call.

author maintainer
#40 lore raw

PATCH 16/17 hwmon: (gl518sm) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Writes into limit attributes can overflow due to additions and multiplications with unchecked parameters.

---
 drivers/hwmon/gl518sm.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/hwmon/gl518sm.c +5 -5
index 0212c8317bca..0a11a550c2a2 100644
@@ -86,9 +86,8 @@ enum chips { gl518sm_r00, gl518sm_r80 };
 #define BOOL_FROM_REG(val)	((val) ? 0 : 1)
 #define BOOL_TO_REG(val)	((val) ? 0 : 1)
 
-#define TEMP_TO_REG(val)	clamp_val(((((val) < 0 ? \
-				(val) - 500 : \
-				(val) + 500) / 1000) + 119), 0, 255)
+#define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val(val, -119000, 136000), \
+					    1000) + 119)
 #define TEMP_FROM_REG(val)	(((val) - 119) * 1000)
 
 static inline u8 FAN_TO_REG(long rpm, int div)
@@ -101,10 +100,11 @@ static inline u8 FAN_TO_REG(long rpm, int div)
 }
 #define FAN_FROM_REG(val, div)	((val) == 0 ? 0 : (480000 / ((val) * (div))))
 
-#define IN_TO_REG(val)		clamp_val((((val) + 9) / 19), 0, 255)
+#define IN_TO_REG(val)	DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 19), 19)
 #define IN_FROM_REG(val)	((val) * 19)
 
-#define VDD_TO_REG(val)		clamp_val((((val) * 4 + 47) / 95), 0, 255)
+#define VDD_TO_REG(val) \
+	DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 95 / 4) * 4, 95)
 #define VDD_FROM_REG(val)	(((val) * 95 + 2) / 4)
 
 #define DIV_FROM_REG(val)	(1 << (val))
Signature · 1 line
2.5.0
Reviewed-by on v1 16/17
#41 lore raw

Re: [PATCH 16/17] hwmon: (gl518sm) Fix overflows seen when writing into limit attributes

Hi Guenter,

Quoted 28 lines · Guenter Roeck

Writes into limit attributes can overflow due to additions and multiplications with unchecked parameters.

---
 drivers/hwmon/gl518sm.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/hwmon/gl518sm.c +2 -3
index 0212c8317bca..0a11a550c2a2 100644
@@ -86,9 +86,8 @@ enum chips { gl518sm_r00, gl518sm_r80 };
 #define BOOL_FROM_REG(val)	((val) ? 0 : 1)
 #define BOOL_TO_REG(val)	((val) ? 0 : 1)
 
-#define TEMP_TO_REG(val)	clamp_val(((((val) < 0 ? \
-				(val) - 500 : \
-				(val) + 500) / 1000) + 119), 0, 255)
+#define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val(val, -119000, 136000), \
+					    1000) + 119)
 #define TEMP_FROM_REG(val)	(((val) - 119) * 1000)
 
 static inline u8 FAN_TO_REG(long rpm, int div)
@@ -101,10 +100,11 @@ static inline u8 FAN_TO_REG(long rpm, int div)
 }
 #define FAN_FROM_REG(val, div)	((val) == 0 ? 0 : (480000 / ((val) * (div))))
 

drivers/hwmon/gl518sm.c static inline u8 FAN_TO_REG(long rpm, int div) · line 110

Quoted 10 lines
drivers/hwmon/gl518sm.c +3 -2
-#define IN_TO_REG(val)		clamp_val((((val) + 9) / 19), 0, 255)
+#define IN_TO_REG(val)	DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 19), 19)
 #define IN_FROM_REG(val)	((val) * 19)
 
-#define VDD_TO_REG(val)		clamp_val((((val) * 4 + 47) / 95), 0, 255)
+#define VDD_TO_REG(val) \
+	DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 95 / 4) * 4, 95)
 #define VDD_FROM_REG(val)	(((val) * 95 + 2) / 4)
 
 #define DIV_FROM_REG(val)	(1 << (val))

Code looks good. Alignment is a bit clumsy though. Also it feels strange now that you are using DIV_ROUND_CLOSEST for VDD_TO_REG but not VDD_FROM_REG.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#42 lore raw

Re: [PATCH 16/17] hwmon: (gl518sm) Fix overflows seen when writing into limit attributes

Hi Jean,

Quoted 40 lines · Jean Delvare

Hi Guenter,

Quoted 37 lines · Guenter Roeck

Writes into limit attributes can overflow due to additions and multiplications with unchecked parameters.

---
 drivers/hwmon/gl518sm.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/hwmon/gl518sm.c +5 -5
index 0212c8317bca..0a11a550c2a2 100644
@@ -86,9 +86,8 @@ enum chips { gl518sm_r00, gl518sm_r80 };
 #define BOOL_FROM_REG(val)	((val) ? 0 : 1)
 #define BOOL_TO_REG(val)	((val) ? 0 : 1)
 
-#define TEMP_TO_REG(val)	clamp_val(((((val) < 0 ? \
-				(val) - 500 : \
-				(val) + 500) / 1000) + 119), 0, 255)
+#define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val(val, -119000, 136000), \
+					    1000) + 119)
 #define TEMP_FROM_REG(val)	(((val) - 119) * 1000)
 
 static inline u8 FAN_TO_REG(long rpm, int div)
@@ -101,10 +100,11 @@ static inline u8 FAN_TO_REG(long rpm, int div)
 }
 #define FAN_FROM_REG(val, div)	((val) == 0 ? 0 : (480000 / ((val) * (div))))
 
-#define IN_TO_REG(val)		clamp_val((((val) + 9) / 19), 0, 255)
+#define IN_TO_REG(val)	DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 19), 19)
 #define IN_FROM_REG(val)	((val) * 19)
 
-#define VDD_TO_REG(val)		clamp_val((((val) * 4 + 47) / 95), 0, 255)
+#define VDD_TO_REG(val) \
+	DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 95 / 4) * 4, 95)
 #define VDD_FROM_REG(val)	(((val) * 95 + 2) / 4)
 
Quoted 6 lines

drivers/hwmon/gl518sm.c static inline u8 FAN_TO_REG(long rpm, int div) · line 110

Quoted 1 line
drivers/hwmon/gl518sm.c +0 -0
 #define DIV_FROM_REG(val)	(1 << (val))

Code looks good. Alignment is a bit clumsy though. Also it feels strange now that you are using DIV_ROUND_CLOSEST for VDD_TO_REG but not VDD_FROM_REG.

I cleaned up the alignment, and added DIV_ROUND_CLOSEST() to VDD_TO_REG(). I'll resend an updated version.

Thanks,
Guenter

Quoted 5 lines
Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#43 lore raw

PATCH 17/17 hwmon: (gl520sm) Fix overflows seen when writing into limit attributes

In reply to Guenter Roeck

Writes into limit attributes can overflow due to multplications and additions with unbound input values.

---
 drivers/hwmon/gl520sm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hwmon/gl520sm.c +5 -4
index dee93ec87d02..4bb37d7234b1 100644
@@ -209,10 +209,11 @@ static ssize_t get_cpu_vid(struct device *dev, struct device_attribute *attr,
 static DEVICE_ATTR(cpu0_vid, S_IRUGO, get_cpu_vid, NULL);
 
 #define VDD_FROM_REG(val) (((val) * 95 + 2) / 4)
-#define VDD_TO_REG(val) clamp_val((((val) * 4 + 47) / 95), 0, 255)
+#define VDD_TO_REG(val) \
+	DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 95 / 4) * 4, 95)
 
 #define IN_FROM_REG(val) ((val) * 19)
-#define IN_TO_REG(val) clamp_val((((val) + 9) / 19), 0, 255)
+#define IN_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 19), 19)
 
 static ssize_t get_in_input(struct device *dev, struct device_attribute *attr,
 			    char *buf)
@@ -514,8 +515,8 @@ static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR,
 		get_fan_off, set_fan_off);
 
 #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
-#define TEMP_TO_REG(val) clamp_val(((((val) < 0 ? \
-			(val) - 500 : (val) + 500) / 1000) + 130), 0, 255)
+#define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val(val, -130000, 125000), \
+					    1000) + 130)
 
 static ssize_t get_temp_input(struct device *dev, struct device_attribute *attr,
 			      char *buf)
Signature · 1 line
2.5.0
Reviewed-by on v1 17/17
#44 lore raw

Re: [PATCH 17/17] hwmon: (gl520sm) Fix overflows seen when writing into limit attributes

Quoted 27 lines · Guenter Roeck

Writes into limit attributes can overflow due to multplications and additions with unbound input values.

---
 drivers/hwmon/gl520sm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hwmon/gl520sm.c +3 -2
index dee93ec87d02..4bb37d7234b1 100644
@@ -209,10 +209,11 @@ static ssize_t get_cpu_vid(struct device *dev, struct device_attribute *attr,
 static DEVICE_ATTR(cpu0_vid, S_IRUGO, get_cpu_vid, NULL);
 
 #define VDD_FROM_REG(val) (((val) * 95 + 2) / 4)
-#define VDD_TO_REG(val) clamp_val((((val) * 4 + 47) / 95), 0, 255)
+#define VDD_TO_REG(val) \
+	DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 95 / 4) * 4, 95)
 
 #define IN_FROM_REG(val) ((val) * 19)
-#define IN_TO_REG(val) clamp_val((((val) + 9) / 19), 0, 255)
+#define IN_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 19), 19)
 
 static ssize_t get_in_input(struct device *dev, struct device_attribute *attr,
 			    char *buf)

drivers/hwmon/gl520sm.c static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR, · line 522

Quoted 10 lines
drivers/hwmon/gl520sm.c +2 -2
@@ -514,8 +515,8 @@ static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR,
 		get_fan_off, set_fan_off);
 
 #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
-#define TEMP_TO_REG(val) clamp_val(((((val) < 0 ? \
-			(val) - 500 : (val) + 500) / 1000) + 130), 0, 255)
+#define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val(val, -130000, 125000), \
+					    1000) + 130)
 
 static ssize_t get_temp_input(struct device *dev, struct device_attribute *attr,
 			      char *buf)

But I think FAN_TO_REG can overflow too? Input value is left-shifted without a prior check.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#45 lore raw

Re: [PATCH 17/17] hwmon: (gl520sm) Fix overflows seen when writing into limit attributes

Quoted 36 lines · Jean Delvare
Quoted 35 lines · Guenter Roeck

Writes into limit attributes can overflow due to multplications and additions with unbound input values.

---
 drivers/hwmon/gl520sm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/hwmon/gl520sm.c +5 -4
index dee93ec87d02..4bb37d7234b1 100644
@@ -209,10 +209,11 @@ static ssize_t get_cpu_vid(struct device *dev, struct device_attribute *attr,
 static DEVICE_ATTR(cpu0_vid, S_IRUGO, get_cpu_vid, NULL);
 
 #define VDD_FROM_REG(val) (((val) * 95 + 2) / 4)
-#define VDD_TO_REG(val) clamp_val((((val) * 4 + 47) / 95), 0, 255)
+#define VDD_TO_REG(val) \
+	DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 95 / 4) * 4, 95)
 
 #define IN_FROM_REG(val) ((val) * 19)
-#define IN_TO_REG(val) clamp_val((((val) + 9) / 19), 0, 255)
+#define IN_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, 0, 255 * 19), 19)
 
 static ssize_t get_in_input(struct device *dev, struct device_attribute *attr,
 			    char *buf)
@@ -514,8 +515,8 @@ static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR,
 		get_fan_off, set_fan_off);
 
 #define TEMP_FROM_REG(val) (((val) - 130) * 1000)
-#define TEMP_TO_REG(val) clamp_val(((((val) < 0 ? \
-			(val) - 500 : (val) + 500) / 1000) + 130), 0, 255)
+#define TEMP_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val(val, -130000, 125000), \
+					    1000) + 130)
 
Quoted 8 lines

drivers/hwmon/gl520sm.c static DEVICE_ATTR(fan1_off, S_IRUGO | S_IWUSR, · line 522

Quoted 2 lines
drivers/hwmon/gl520sm.c +0 -0
 static ssize_t get_temp_input(struct device *dev, struct device_attribute *attr,
 			      char *buf)

But I think FAN_TO_REG can overflow too? Input value is left-shifted without a prior check.

You are right. My older script didn't detect that because the overflow happens with a very low value, and the script just concluded that the value range was [0,0].

After improving my test script, the driver generates KASAN bad memory reports. Outch. I'll have to look into that.

Thanks,
Guenter

#46 lore raw
In reply to Guenter Roeck

Hi Guenter,

Quoted 24 lines · Guenter Roeck

Module test reports:

in0_min: Suspected overflow: [3320 vs. 0]
in0_max: Suspected overflow: [3320 vs. 0]
in4_min: Suspected overflow: [15938 vs. 0]
in4_max: Suspected overflow: [15938 vs. 0]
temp1_max: Suspected overflow: [127000 vs. 0]
temp1_max_hyst: Suspected overflow: [127000 vs. 0]
aout_output: Suspected overflow: [1250 vs. 0]

Code analysis reveals that the overflows are caused by conversions from unsigned long to long to int, combined with multiplications on passed values.

---
 drivers/hwmon/adm9240.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/hwmon/adm9240.c +0 -0
index 2fe1828bd10b..347afacedcf5 100644

drivers/hwmon/adm9240.c static inline unsigned int IN_FROM_REG(u8 reg, int n) · line 103

Quoted 6 lines
drivers/hwmon/adm9240.c +1 -0
@@ -98,12 +98,14 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n)
 
 static inline u8 IN_TO_REG(unsigned long val, int n)
 {
+	val = clamp_val(val, 0, INT_MAX / 192 - 12000);
 	return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
 }

I understand the idea of clamping before the conversion to avoid the overflow. However I would have hoped that clamping the input makes clamping the output unneeded. Clamping is full of tests, which aren't cheap as they break the CPU instruction prediction, so we should not abuse it.

Would the following work?

static inline u8 IN_TO_REG(unsigned long val, int n)

{
	val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
	return SCALE(val, 192, nom_mv[n]);
}

This should be more compact and faster.

Quoted 9 lines
drivers/hwmon/adm9240.c +1 -0
 /* temperature range: -40..125, 127 disables temperature alarm */
 static inline s8 TEMP_TO_REG(long val)
 {
+	val = clamp_val(val, INT_MIN + 1000, INT_MAX - 1000);
 	return clamp_val(SCALE(val, 1, 1000), -40, 127);
 }
 

drivers/hwmon/adm9240.c static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) · line 130

Quoted 7 lines
drivers/hwmon/adm9240.c +1 -0
@@ -122,6 +124,7 @@ static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
 /* analog out 0..1250mV */
 static inline u8 AOUT_TO_REG(unsigned long val)
 {
+	val = clamp_val(val, 0, INT_MAX / 255 - 1250);
 	return clamp_val(SCALE(val, 255, 1250), 0, 255);
 }
 

Same comment and same suggested solution for these two functions:

/* temperature range: -40..125, 127 disables temperature alarm */ static inline s8 TEMP_TO_REG(long val)

{
	val = clamp_val(val, -40000, 127000);
	return SCALE(val, 1, 1000);
}

/* analog out 0..1250mV */
static inline u8 AOUT_TO_REG(unsigned long val)

{
	val = clamp_val(val, 0, 1250);
	return SCALE(val, 255, 1250);
}
Signature · 2 lines
Jean Delvare
SUSE L3 Support
author maintainer
#47 lore raw
Quoted 28 lines · Jean Delvare

Hi Guenter,

Quoted 25 lines · Guenter Roeck

Module test reports:

in0_min: Suspected overflow: [3320 vs. 0]
in0_max: Suspected overflow: [3320 vs. 0]
in4_min: Suspected overflow: [15938 vs. 0]
in4_max: Suspected overflow: [15938 vs. 0]
temp1_max: Suspected overflow: [127000 vs. 0]
temp1_max_hyst: Suspected overflow: [127000 vs. 0]
aout_output: Suspected overflow: [1250 vs. 0]

Code analysis reveals that the overflows are caused by conversions from unsigned long to long to int, combined with multiplications on passed values.

---
 drivers/hwmon/adm9240.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/hwmon/adm9240.c +0 -0
index 2fe1828bd10b..347afacedcf5 100644
@@ -98,12 +98,14 @@ static inline unsigned int IN_FROM_REG(u8 reg, int n)
 
Quoted 12 lines

drivers/hwmon/adm9240.c static inline unsigned int IN_FROM_REG(u8 reg, int n) · line 103

Quoted 5 lines
drivers/hwmon/adm9240.c +1 -0
 static inline u8 IN_TO_REG(unsigned long val, int n)
 {
+	val = clamp_val(val, 0, INT_MAX / 192 - 12000);
 	return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
 }

I understand the idea of clamping before the conversion to avoid the overflow. However I would have hoped that clamping the input makes clamping the output unneeded. Clamping is full of tests, which aren't cheap as they break the CPU instruction prediction, so we should not abuse it.

I am not that much concerned about this here, since the limits are not usually set continuously. I agree though, since it is always better to keep the code as simple as possible.

Quoted 36 lines

Would the following work?

static inline u8 IN_TO_REG(unsigned long val, int n)

{
	val = clamp_val(val, 0, nom_mv[n] * 255 / 192);
	return SCALE(val, 192, nom_mv[n]);
}

This should be more compact and faster.

Quoted 9 lines
drivers/hwmon/adm9240.c +1 -0
 /* temperature range: -40..125, 127 disables temperature alarm */
 static inline s8 TEMP_TO_REG(long val)
 {
+	val = clamp_val(val, INT_MIN + 1000, INT_MAX - 1000);
 	return clamp_val(SCALE(val, 1, 1000), -40, 127);
 }
 

drivers/hwmon/adm9240.c static inline unsigned int FAN_FROM_REG(u8 reg, u8 div) · line 130

Quoted 7 lines
drivers/hwmon/adm9240.c +1 -0
@@ -122,6 +124,7 @@ static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
 /* analog out 0..1250mV */
 static inline u8 AOUT_TO_REG(unsigned long val)
 {
+	val = clamp_val(val, 0, INT_MAX / 255 - 1250);
 	return clamp_val(SCALE(val, 255, 1250), 0, 255);
 }
 

Same comment and same suggested solution for these two functions:

/* temperature range: -40..125, 127 disables temperature alarm */ static inline s8 TEMP_TO_REG(long val)

{
	val = clamp_val(val, -40000, 127000);
	return SCALE(val, 1, 1000);
}
Quoted 7 lines

/* analog out 0..1250mV */
static inline u8 AOUT_TO_REG(unsigned long val)

{
	val = clamp_val(val, 0, 1250);
	return SCALE(val, 255, 1250);
}

Should work. I'll give it a try.

Guenter