Skip to contentSkip to conversation

hwmon: (adm1031) Fix writes to limit registers

PATCH In review Guenter Roeck 2 participants 3 messages Jul 3, 2014 – Jul 7, 2014 Hardware monitoring

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 hwmon: (adm1031) Fix writes to limit registers

Upper limit for write operations to temperature limit registers was clamped to a fractional value. However, limit registers do not support fractional values. As a result, upper limits of 127.5 degrees C or higher resulted in a rounded limit of 128 degrees C. Since limit registers are signed, this was stored as -128 degrees C. Clamp limits to (-55, +127) degrees C to solve the problem.

Value on writes to auto_temp[12]_min and auto_temp[12]_max were not clamped at all, but masked. As a result, out-of-range writes resulted in a more or less arbitrary limit. Clamp those attributes to (0, 127) degrees C for more predictable results.

---
 drivers/hwmon/adm1031.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/hwmon/adm1031.c +5 -3
index a8a540c..9eaa235 100644
@@ -366,6 +366,7 @@ set_auto_temp_min(struct device *dev, struct device_attribute *attr,
 		return ret;
 
 	mutex_lock(&data->update_lock);
+	val = clamp_val(val, 0, 127000);
 	data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
 	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
 			    data->auto_temp[nr]);
@@ -395,6 +396,7 @@ set_auto_temp_max(struct device *dev, struct device_attribute *attr,
 		return ret;
 
 	mutex_lock(&data->update_lock);
+	val = clamp_val(val, 0, 127000);
 	data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
 						  data->pwm[nr]);
 	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
@@ -696,7 +698,7 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	val = clamp_val(val, -55000, nr = 0 ? 127750 : 127875);
+	val = clamp_val(val, -55000, 127000);
 	mutex_lock(&data->update_lock);
 	data->temp_min[nr] = TEMP_TO_REG(val);
 	adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
@@ -717,7 +719,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	val = clamp_val(val, -55000, nr = 0 ? 127750 : 127875);
+	val = clamp_val(val, -55000, 127000);
 	mutex_lock(&data->update_lock);
 	data->temp_max[nr] = TEMP_TO_REG(val);
 	adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
@@ -738,7 +740,7 @@ static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	val = clamp_val(val, -55000, nr = 0 ? 127750 : 127875);
+	val = clamp_val(val, -55000, 127000);
 	mutex_lock(&data->update_lock);
 	data->temp_crit[nr] = TEMP_TO_REG(val);
 	adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
Signature · 7 lines
1.9.1


_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Reviewed-by on v1 1/1
#1 lore raw

Hi Guenter,

Quoted 6 lines · Guenter Roeck

Upper limit for write operations to temperature limit registers was clamped to a fractional value. However, limit registers do not support fractional values. As a result, upper limits of 127.5 degrees C or higher resulted in a rounded limit of 128 degrees C. Since limit registers are signed, this was stored as -128 degrees C. Clamp limits to (-55, +127) degrees C to solve the problem.

Good catch, sorry for missing this bug during my original review.

Quoted 4 lines

Value on writes to auto_temp[12]_min and auto_temp[12]_max were not clamped at all, but masked. As a result, out-of-range writes resulted in a more or less arbitrary limit. Clamp those attributes to (0, 127) degrees C for more predictable results.

Ditto.

Quoted 11 lines
---
 drivers/hwmon/adm1031.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/hwmon/adm1031.c +0 -0
index a8a540c..9eaa235 100644

drivers/hwmon/adm1031.c set_auto_temp_min(struct device *dev, struct device_attribute *attr, · line 369

Quoted 4 lines
drivers/hwmon/adm1031.c +1 -0
@@ -366,6 +366,7 @@ set_auto_temp_min(struct device *dev, struct device_attribute *attr,
 		return ret;
 
 	mutex_lock(&data->update_lock);
+	val = clamp_val(val, 0, 127000);

For consistency and performance, please clamp the value before locking the mutex.

drivers/hwmon/adm1031.c set_auto_temp_max(struct device *dev, struct device_attribute *attr, · line 399

Quoted 8 lines
drivers/hwmon/adm1031.c +1 -0
 	data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
 	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
 			    data->auto_temp[nr]);
@@ -395,6 +396,7 @@ set_auto_temp_max(struct device *dev, struct device_attribute *attr,
 		return ret;
 
 	mutex_lock(&data->update_lock);
+	val = clamp_val(val, 0, 127000);

Same here.

Quoted 22 lines
drivers/hwmon/adm1031.c +2 -2
 	data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
 						  data->pwm[nr]);
 	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
@@ -696,7 +698,7 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	val = clamp_val(val, -55000, nr = 0 ? 127750 : 127875);
+	val = clamp_val(val, -55000, 127000);
 	mutex_lock(&data->update_lock);
 	data->temp_min[nr] = TEMP_TO_REG(val);
 	adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
@@ -717,7 +719,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	val = clamp_val(val, -55000, nr = 0 ? 127750 : 127875);
+	val = clamp_val(val, -55000, 127000);
 	mutex_lock(&data->update_lock);
 	data->temp_max[nr] = TEMP_TO_REG(val);
 	adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),

drivers/hwmon/adm1031.c static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, · line 746

Quoted 8 lines
drivers/hwmon/adm1031.c +1 -1
@@ -738,7 +740,7 @@ static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	val = clamp_val(val, -55000, nr = 0 ? 127750 : 127875);
+	val = clamp_val(val, -55000, 127000);
 	mutex_lock(&data->update_lock);
 	data->temp_crit[nr] = TEMP_TO_REG(val);
 	adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),
Signature · 7 lines
Jean Delvare
SUSE L3 Support

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
author maintainer
#2 lore raw
In reply to Guenter Roeck

Hi Jean,

Quoted 12 lines · Jean Delvare

Hi Guenter,

Quoted 6 lines · Guenter Roeck

Upper limit for write operations to temperature limit registers was clamped to a fractional value. However, limit registers do not support fractional values. As a result, upper limits of 127.5 degrees C or higher resulted in a rounded limit of 128 degrees C. Since limit registers are signed, this was stored as -128 degrees C. Clamp limits to (-55, +127) degrees C to solve the problem.

Good catch, sorry for missing this bug during my original review.

Easy to miss without explicit module test, really. I bet we have a lot of similar problems in other drivers ;-).

Quoted 32 lines
Quoted 4 lines

Value on writes to auto_temp[12]_min and auto_temp[12]_max were not clamped at all, but masked. As a result, out-of-range writes resulted in a more or less arbitrary limit. Clamp those attributes to (0, 127) degrees C for more predictable results.

Ditto.

Quoted 11 lines
---
  drivers/hwmon/adm1031.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
drivers/hwmon/adm1031.c +0 -0
index a8a540c..9eaa235 100644

drivers/hwmon/adm1031.c set_auto_temp_min(struct device *dev, struct device_attribute *attr, · line 369

Quoted 4 lines
drivers/hwmon/adm1031.c +1 -0
@@ -366,6 +366,7 @@ set_auto_temp_min(struct device *dev, struct device_attribute *attr,
  		return ret;
 
  	mutex_lock(&data->update_lock);
+	val = clamp_val(val, 0, 127000);

For consistency and performance, please clamp the value before locking the mutex.

Quoted 6 lines
drivers/hwmon/adm1031.c +0 -0
  	data->auto_temp[nr] = AUTO_TEMP_MIN_TO_REG(val, data->auto_temp[nr]);
  	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
  			    data->auto_temp[nr]);
@@ -395,6 +396,7 @@ set_auto_temp_max(struct device *dev, struct device_attribute *attr,
  		return ret;
 
Quoted 5 lines

drivers/hwmon/adm1031.c set_auto_temp_max(struct device *dev, struct device_attribute *attr, · line 399

Quoted 2 lines
drivers/hwmon/adm1031.c +1 -0
  	mutex_lock(&data->update_lock);
+	val = clamp_val(val, 0, 127000);

Same here.

Ok, done.

Quoted 25 lines
Quoted 25 lines
drivers/hwmon/adm1031.c +2 -2
  	data->temp_max[nr] = AUTO_TEMP_MAX_TO_REG(val, data->auto_temp[nr],
  						  data->pwm[nr]);
  	adm1031_write_value(client, ADM1031_REG_AUTO_TEMP(nr),
@@ -696,7 +698,7 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  	if (ret)
  		return ret;
 
-	val = clamp_val(val, -55000, nr = 0 ? 127750 : 127875);
+	val = clamp_val(val, -55000, 127000);
  	mutex_lock(&data->update_lock);
  	data->temp_min[nr] = TEMP_TO_REG(val);
  	adm1031_write_value(client, ADM1031_REG_TEMP_MIN(nr),
@@ -717,7 +719,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  	if (ret)
  		return ret;
 
-	val = clamp_val(val, -55000, nr = 0 ? 127750 : 127875);
+	val = clamp_val(val, -55000, 127000);
  	mutex_lock(&data->update_lock);
  	data->temp_max[nr] = TEMP_TO_REG(val);
  	adm1031_write_value(client, ADM1031_REG_TEMP_MAX(nr),
@@ -738,7 +740,7 @@ static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
  	if (ret)
  		return ret;
 
Quoted 8 lines

drivers/hwmon/adm1031.c static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr, · line 746

Quoted 5 lines
drivers/hwmon/adm1031.c +1 -1
-	val = clamp_val(val, -55000, nr = 0 ? 127750 : 127875);
+	val = clamp_val(val, -55000, 127000);
  	mutex_lock(&data->update_lock);
  	data->temp_crit[nr] = TEMP_TO_REG(val);
  	adm1031_write_value(client, ADM1031_REG_TEMP_CRIT(nr),

Thanks!

Guenter

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors