Skip to contentSkip to conversation

hwmon: (emc2103) Clamp limits instead of bailing out

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

Subsystems (2)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 hwmon: (emc2103) Clamp limits instead of bailing out

It is customary to clamp limits instead of bailing out with an error if a configured limit is out of the range supported by the driver. This simplifies limit configuration, since the user will not typically know chip and/or driver specific limits.

---
 drivers/hwmon/emc2103.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/hwmon/emc2103.c +3 -8
index fd892dd..0f38294 100644
@@ -250,9 +250,7 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *da,
 	if (result < 0)
 		return result;
 
-	val = DIV_ROUND_CLOSEST(val, 1000);
-	if ((val < -63) || (val > 127))
-		return -EINVAL;
+	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
 
 	mutex_lock(&data->update_lock);
 	data->temp_min[nr] = val;
@@ -274,9 +272,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
 	if (result < 0)
 		return result;
 
-	val = DIV_ROUND_CLOSEST(val, 1000);
-	if ((val < -63) || (val > 127))
-		return -EINVAL;
+	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
 
 	mutex_lock(&data->update_lock);
 	data->temp_max[nr] = val;
@@ -397,8 +393,7 @@ static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
 		return result;
 
 	/* Datasheet states 16384 as maximum RPM target (table 3.2) */
-	if ((rpm_target < 0) || (rpm_target > 16384))
-		return -EINVAL;
+	rpm_target = clamp_val(rpm_target, 0, 16384);
 
 	mutex_lock(&data->update_lock);
 
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 4 lines · Guenter Roeck

It is customary to clamp limits instead of bailing out with an error if a configured limit is out of the range supported by the driver. This simplifies limit configuration, since the user will not typically know chip and/or driver specific limits.

Agreed.

Quoted 21 lines
---
 drivers/hwmon/emc2103.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)
drivers/hwmon/emc2103.c +1 -3
index fd892dd..0f38294 100644
@@ -250,9 +250,7 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *da,
 	if (result < 0)
 		return result;
 
-	val = DIV_ROUND_CLOSEST(val, 1000);
-	if ((val < -63) || (val > 127))
-		return -EINVAL;
+	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
 
 	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 278

Quoted 10 lines
drivers/hwmon/emc2103.c +1 -3
@@ -274,9 +272,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
 	if (result < 0)
 		return result;
 
-	val = DIV_ROUND_CLOSEST(val, 1000);
-	if ((val < -63) || (val > 127))
-		return -EINVAL;
+	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
 
 	mutex_lock(&data->update_lock);
 	data->temp_max[nr] = val;

I fully agree with these two.

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

Quoted 10 lines
drivers/hwmon/emc2103.c +1 -2
@@ -397,8 +393,7 @@ static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
 		return result;
 
 	/* Datasheet states 16384 as maximum RPM target (table 3.2) */
-	if ((rpm_target < 0) || (rpm_target > 16384))
-		return -EINVAL;
+	rpm_target = clamp_val(rpm_target, 0, 16384);
 
 	mutex_lock(&data->update_lock);
 

Here however, < 0 is really invalid. There is no excuse for the user to ask for a negative fan speed (as opposed to speeds above 16384, where the limit is arbitrary and impossible to guess without reading the datasheet.)

IMHO the best way to handle that is to change rpm_target to unsigned long and set its value using kstrtoul (instead of kstrtol.)

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
Quoted 60 lines · Jean Delvare

Hi Guenter,

Quoted 4 lines · Guenter Roeck

It is customary to clamp limits instead of bailing out with an error if a configured limit is out of the range supported by the driver. This simplifies limit configuration, since the user will not typically know chip and/or driver specific limits.

Agreed.

Quoted 21 lines
---
  drivers/hwmon/emc2103.c | 11 +++--------
  1 file changed, 3 insertions(+), 8 deletions(-)
drivers/hwmon/emc2103.c +1 -3
index fd892dd..0f38294 100644
@@ -250,9 +250,7 @@ static ssize_t set_temp_min(struct device *dev, struct device_attribute *da,
  	if (result < 0)
  		return result;
 
-	val = DIV_ROUND_CLOSEST(val, 1000);
-	if ((val < -63) || (val > 127))
-		return -EINVAL;
+	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
 
  	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 278

Quoted 10 lines
drivers/hwmon/emc2103.c +1 -3
@@ -274,9 +272,7 @@ static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
  	if (result < 0)
  		return result;
 
-	val = DIV_ROUND_CLOSEST(val, 1000);
-	if ((val < -63) || (val > 127))
-		return -EINVAL;
+	val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
 
  	mutex_lock(&data->update_lock);
  	data->temp_max[nr] = val;

I fully agree with these two.

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

Quoted 10 lines
drivers/hwmon/emc2103.c +1 -2
@@ -397,8 +393,7 @@ static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
  		return result;
 
  	/* Datasheet states 16384 as maximum RPM target (table 3.2) */
-	if ((rpm_target < 0) || (rpm_target > 16384))
-		return -EINVAL;
+	rpm_target = clamp_val(rpm_target, 0, 16384);
 
  	mutex_lock(&data->update_lock);
 

Here however, < 0 is really invalid. There is no excuse for the user to ask for a negative fan speed (as opposed to speeds above 16384, where the limit is arbitrary and impossible to guess without reading the datasheet.)

Quoted 3 lines

IMHO the best way to handle that is to change rpm_target to unsigned long and set its value using kstrtoul (instead of kstrtol.)

Makes sense, I'll do that.

Quoted 2 lines

Thanks,
Guenter

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