Skip to contentSkip to conversation

hwmon: fix common race conditions, batch 1

lm-sensors Mark M. Hoffman 4 participants 10 messages Mar 4, 2008 – Mar 21, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi all:

Here is the first in a batch of patches which aim to fix a common class of race conditions in most hwmon drivers. Credit goes to Herbert Valerio Riedel for pointing this out, here:

http://lists.lm-sensors.org/pipermail/lm-sensors/2007-November/022014.html

To actually trip on any of these race conditions in practice would be *exceedingly* rare. In fact, I don't think we've ever seen a report for such a case. OTOH, one can see by looking at this patch that the fixes are not always completely trivial.

I will concentrate on cranking out the remaining patches as I have time. But of course I would like thorough reviews, especially from the driver author/maintainer if possible. To all potential reviewers: look not just at the patch itself; please also scan the entire driver to see if there are any races that I missed. Testing is also much appreciated.

Thanks & regards,

commit 2475ee13652b392d9720b39f274205c815979557

Author: Mark M. Hoffman <[email protected]>
Date:   Mon Mar 3 23:35:52 2008 -0500

hwmon: fix common race conditions, batch 1

Most hwmon drivers have one or more race conditions that could cause the driver to display incorrect data; in the absolute worst case, these races could allow divide-by-zero/OOPS. The most common of these involves a race between setting a fan divider and displaying the fan data (RPMs). This patch fixes these race conditions by taking the update lock (or equivalent) as necessary.

drivers/hwmon/adm1026.c +12 -8
index 904c6ce..92bed53 100644
@@ -838,20 +838,24 @@ static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_m
 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 		char *buf)
 {
-	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
-	int nr = sensor_attr->index;
+	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1026_data *data = adm1026_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
-		data->fan_div[nr]));
+	int val;
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan[nr], data->fan_div[nr]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
 		char *buf)
 {
-	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
-	int nr = sensor_attr->index;
+	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1026_data *data = adm1026_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
-		data->fan_div[nr]));
+	int val;
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan_min[nr], data->fan_div[nr]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
 		const char *buf, size_t count)
drivers/hwmon/adm1029.c +18 -12
index 2c6608d..9080372 100644
@@ -165,27 +165,33 @@ show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
 static ssize_t
 show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adm1029_data *data = adm1029_update_device(dev);
 	u16 val;
-	if (data->fan[attr->index] = 0 || data->fan_div[attr->index] = 0
-	    || data->fan[attr->index] = 255) {
-		return sprintf(buf, "0\n");
-	}
-
-	val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
-	    / data->fan[attr->index];
+	mutex_lock(&data->update_lock);
+	if (data->fan[index] = 0 || data->fan_div[index] = 0
+					|| data->fan[index] = 255)
+		val = 0;
+	else
+		val = 1880 * 120 / DIV_FROM_REG(data->fan_div[index])
+					/ data->fan[index];
+	mutex_unlock(&data->update_lock);
 	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t
 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adm1029_data *data = adm1029_update_device(dev);
-	if (data->fan_div[attr->index] = 0)
-		return sprintf(buf, "0\n");
-	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
+	int val;
+	mutex_lock(&data->update_lock);
+	if (data->fan_div[index] = 0)
+		val = 0;
+	else
+		val = DIV_FROM_REG(data->fan_div[index]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t set_fan_div(struct device *dev,
drivers/hwmon/adm1031.c +15 -5
index 2bffcab..c88187a 100644
@@ -466,8 +466,10 @@ static ssize_t show_fan(struct device *dev,
 	struct adm1031_data *data = adm1031_update_device(dev);
 	int value;
 
+	mutex_lock(&data->update_lock);
 	value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
 				 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
+	mutex_unlock(&data->update_lock);
 	return sprintf(buf, "%d\n", value);
 }
 
@@ -483,9 +485,13 @@ static ssize_t show_fan_min(struct device *dev,
 {
 	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1031_data *data = adm1031_update_device(dev);
-	return sprintf(buf, "%d\n",
-		       FAN_FROM_REG(data->fan_min[nr],
-				    FAN_DIV_FROM_REG(data->fan_div[nr])));
+	int value;
+
+	mutex_lock(&data->update_lock);
+	value = FAN_FROM_REG(data->fan_min[nr],
+				    FAN_DIV_FROM_REG(data->fan_div[nr]));
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", value);
 }
 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
 			   const char *buf, size_t count)
@@ -567,11 +573,15 @@ static ssize_t show_temp(struct device *dev,
 {
 	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1031_data *data = adm1031_update_device(dev);
-	int ext;
+	int ext, val;
+
+	mutex_lock(&data->update_lock);
 	ext = nr = 0 ?
 	    ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
 	    (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
-	return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
+	val = TEMP_FROM_REG_EXT(data->temp[nr], ext);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 static ssize_t show_temp_min(struct device *dev,
 			     struct device_attribute *attr, char *buf)
drivers/hwmon/adm9240.c +12 -6
index 149ef25..0023d44 100644
@@ -290,19 +290,25 @@ vin(5);
 static ssize_t show_fan(struct device *dev,
 		struct device_attribute *devattr, char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adm9240_data *data = adm9240_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
-				1 << data->fan_div[attr->index]));
+	int val;
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan[index], 1 << data->fan_div[index]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t show_fan_min(struct device *dev,
 		struct device_attribute *devattr, char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adm9240_data *data = adm9240_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
-				1 << data->fan_div[attr->index]));
+	int val;
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan_min[index], 1 << data->fan_div[index]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t show_fan_div(struct device *dev,
drivers/hwmon/adt7470.c +32 -25

88 lines not on this page · raw message

drivers/hwmon/adt7473.c +53 -31

142 lines not on this page · raw message

drivers/hwmon/asb100.c +12 -4
index fe2eea4..2aeac1a 100644
@@ -276,8 +276,12 @@ static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 {
 	int nr = to_sensor_dev_attr(attr)->index;
 	struct asb100_data *data = asb100_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
-		DIV_FROM_REG(data->fan_div[nr])));
+	int val;
+
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan[nr], DIV_FROM_REG(data->fan_div[nr]));
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
@@ -285,8 +289,12 @@ static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
 {
 	int nr = to_sensor_dev_attr(attr)->index;
 	struct asb100_data *data = asb100_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
-		DIV_FROM_REG(data->fan_div[nr])));
+	int val;
+
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
Signature · 8 lines
Mark M. Hoffman
[email protected]


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

Re: [lm-sensors] [RFC/PATCH] hwmon: fix common race conditions,

Hi Mark,

Quoted 27 lines · Mark M. Hoffman

Hi all:

Here is the first in a batch of patches which aim to fix a common class of race conditions in most hwmon drivers. Credit goes to Herbert Valerio Riedel for pointing this out, here:

http://lists.lm-sensors.org/pipermail/lm-sensors/2007-November/022014.html

To actually trip on any of these race conditions in practice would be *exceedingly* rare. In fact, I don't think we've ever seen a report for such a case. OTOH, one can see by looking at this patch that the fixes are not always completely trivial.

I will concentrate on cranking out the remaining patches as I have time. But of course I would like thorough reviews, especially from the driver author/maintainer if possible. To all potential reviewers: look not just at the patch itself; please also scan the entire driver to see if there are any races that I missed. Testing is also much appreciated.

Thanks & regards,

commit 2475ee13652b392d9720b39f274205c815979557

Author: Mark M. Hoffman <[email protected]>
Date:   Mon Mar 3 23:35:52 2008 -0500

hwmon: fix common race conditions, batch 1

Quoted 7 lines

Most hwmon drivers have one or more race conditions that could cause the driver to display incorrect data; in the absolute worst case, these races could allow divide-by-zero/OOPS. The most common of these involves a race between setting a fan divider and displaying the fan data (RPMs). This patch fixes these race conditions by taking the update lock (or equivalent) as necessary.

I would appreciate if your hwmon patches included a diffstat output. This would make it much easier for me to figure out what drivers are affected and whether I can help with testing and/or review.

Thanks,

Signature · 6 lines
Jean Delvare

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

Re: [lm-sensors] [RFC/PATCH] hwmon: fix common race conditions,

In reply to Mark M. Hoffman

On Tue, Mar 04, 2008 at 09:54:40AM -0500, Mark M. Hoffman wrote:

<snip>

Quoted 5 lines
drivers/hwmon/adt7473.c +0 -0
index 9587869..3807062 100644

drivers/hwmon/adt7473.c static ssize_t show_volt(struct device *dev, struct device_attribute *devattr, · line 427

Quoted 10 lines
drivers/hwmon/adt7473.c +2 -4
@@ -422,11 +422,9 @@ static ssize_t show_volt(struct device *dev, struct device_attribute *devattr,
  * number in the range -128 to 127, or as an unsigned number that must
  * be offset by 64.
  */
-static int decode_temp(struct adt7473_data *data, u8 raw)
+static int decode_temp(u8 twos_complement, u8 raw)
 {
-	if (data->temp_twos_complement)
-		return (s8)raw;
-	return raw - 64;
+	return twos_complement ? (s8)raw : raw - 64;

Since this patch is to fix a bunch of race conditions, I think this chunk would be better off as a separate cleanup patch. That said, you're the maintainer, so it's your call.... :)

I also suspect that ibmpex/i5k-amb will need similar cleanups. I can generate patches if you want, though if you're intent upon fixing all of the drivers as one big push, let me know.

For the adt7470/adt7473 bits,

--D

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

#3 lore raw

Re: [lm-sensors] [RFC/PATCH] hwmon: fix common race conditions,

In reply to Mark M. Hoffman

Hi Darrick:

Quoted 14 lines · Darrick J. Wong

On Tue, Mar 04, 2008 at 09:54:40AM -0500, Mark M. Hoffman wrote:

<snip>

Quoted 11 lines
drivers/hwmon/adt7473.c +1 -1
index 9587869..3807062 100644
@@ -422,11 +422,9 @@ static ssize_t show_volt(struct device *dev, struct device_attribute *devattr,
  * number in the range -128 to 127, or as an unsigned number that must
  * be offset by 64.
  */
-static int decode_temp(struct adt7473_data *data, u8 raw)
+static int decode_temp(u8 twos_complement, u8 raw)
 {
Quoted 8 lines

drivers/hwmon/adt7473.c static ssize_t show_volt(struct device *dev, struct device_attribute *devattr, · line 427

Quoted 4 lines
drivers/hwmon/adt7473.c +1 -3
-	if (data->temp_twos_complement)
-		return (s8)raw;
-	return raw - 64;
+	return twos_complement ? (s8)raw : raw - 64;

Since this patch is to fix a bunch of race conditions, I think this chunk would be better off as a separate cleanup patch. That said, you're the maintainer, so it's your call.... :)

I agree, and I'm glad you brought it up wrt adt7473. I refactored both encode_temp and decode_temp and then noticed that encode_temp actually has the same race condition, eg:

449 static ssize_t set_temp_min(struct device *dev,

 450                             struct device_attribute *devattr,
 451                             const char *buf,
 452                             size_t count)
 453 {
 454         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
 455         struct i2c_client *client = to_i2c_client(dev);
 456         struct adt7473_data *data = i2c_get_clientdata(client);
 457         int temp = simple_strtol(buf, NULL, 10) / 1000;
 458         temp = encode_temp(data, temp);
 459 
 460         mutex_lock(&data->lock);
 461         data->temp_min[attr->index] = temp;
 462         i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MIN(attr->index),

463 temp);
464 mutex_unlock(&data->lock);
465
466 return count;
467 }

Notice how encode_temp() above is outside of the locked section. It is instructive for potential reviewers of this patch series to see just how easy it is to miss these.

However...

REG_CFG5 (from which temp_twos_complement is updated) is never written. Do you really need to read that during every update? If you move that one read into the init routine, that will instantly kill all the race conditions associated with temp_twos_complement (assuming the hardware does not modify this register behind our backs). What do you think?

Quoted 3 lines

I also suspect that ibmpex/i5k-amb will need similar cleanups. I can generate patches if you want, though if you're intent upon fixing all of the drivers as one big push, let me know.

I am planning to just rip through them all myself. But I want to get them all done early enough to get good testing prior to 26-rc1... so I may ask for help later if it looks like I won't have time.

Of course, I won't tell anyone *not* to help. ;)

Quoted 2 lines

For the adt7470/adt7473 bits,

Thanks. Cleanup patch for adt7473 to follow...

Regards,

Signature · 8 lines
Mark M. Hoffman
[email protected]


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

Re: [lm-sensors] [RFC/PATCH] hwmon: fix common race conditions,

In reply to Mark M. Hoffman
Quoted 4 lines · Mark M. Hoffman

Hi Darrick:
Notice how encode_temp() above is outside of the locked section. It is instructive for potential reviewers of this patch series to see just how easy it is to miss these.

Yep...

Quoted 5 lines

REG_CFG5 (from which temp_twos_complement is updated) is never written. Do you really need to read that during every update? If you move that one read into the init routine, that will instantly kill all the race conditions associated with temp_twos_complement (assuming the hardware does not modify this register behind our backs). What do you think?

Correct, the hardware doesn't modify that register at all. I see your patch, so I'll have a look through that.

--D

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

#5 lore raw

Re: [lm-sensors] [RFC/PATCH] hwmon: fix common race conditions,

In reply to Mark M. Hoffman

Hi Mark,

Quoted 13 lines · Mark M. Hoffman

Hi all:

Here is the first in a batch of patches which aim to fix a common class of race conditions in most hwmon drivers. Credit goes to Herbert Valerio Riedel for pointing this out, here:

http://lists.lm-sensors.org/pipermail/lm-sensors/2007-November/022014.html

To actually trip on any of these race conditions in practice would be *exceedingly* rare. In fact, I don't think we've ever seen a report for such a case. OTOH, one can see by looking at this patch that the fixes are not always completely trivial.

Quoted 5 lines

I will concentrate on cranking out the remaining patches as I have time. But of course I would like thorough reviews, especially from the driver author/maintainer if possible. To all potential reviewers: look not just at the patch itself; please also scan the entire driver to see if there are any races that I missed. Testing is also much appreciated.

When looking for this kind of race, you should pay particular attention to macros and inline functions. Historically, some of our macros were evaluating their arguments more than once. I have tried to clean this up as we ported the driver to Linux 2.6 but I wouldn't swear that such macros are all gone; there could be a couple still lurking.

As for inline functions, I really don't know how gcc handles these. If it creates a temporary copy of the parameters on the stack, then we're safe. If not, then even inline functions evaluating their parameters more than once could hide the race.

<snip>

Quoted 10 lines
drivers/hwmon/adm1026.c +0 -2
index 904c6ce..92bed53 100644
@@ -838,20 +838,24 @@ static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_m
 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 		char *buf)
 {
-	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
-	int nr = sensor_attr->index;

drivers/hwmon/adm1026.c static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_m · line 848

Quoted 10 lines
drivers/hwmon/adm1026.c +6 -2
+	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1026_data *data = adm1026_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
-		data->fan_div[nr]));
+	int val;
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan[nr], data->fan_div[nr]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }

As you are making the functions bigger, I think it would be a good idea to leave a blank like between the variable declarations and the "real" code, as is common practice for non-trivial functions. Same goes for the rest of the patch, of course.

<snip>

Quoted 16 lines
drivers/hwmon/adt7470.c +1 -5
index 6b5325f..2c398d5 100644
(...)
@@ -678,14 +684,15 @@ static ssize_t show_pwm_auto_temp(struct device *dev,
 				  struct device_attribute *devattr,
 				  char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adt7470_data *data = adt7470_update_device(dev);
-	u8 ctrl = data->pwm_auto_temp[attr->index];
-
-	if (ctrl)
-		return sprintf(buf, "%d\n", 1 << (ctrl - 1));

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

Quoted 10 lines
drivers/hwmon/adt7470.c +7 -2
-	else
-		return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
+	u8 ctrl;
+	int val;
+	mutex_lock(&data->lock);
+	ctrl = data->pwm_auto_temp[index];
+	val = ctrl ?  (1 << (ctrl - 1)) : ADT7470_PWM_ALL_TEMPS;
+	mutex_unlock(&data->lock);
+	return sprintf(buf, "%d\n", val);
 }

Not sure why this change is needed? data->pwm_auto_temp[index] is only accessed once. Unless gcc optimizes the code to get rid of the ctrl local variable, of course. I really have no idea what gcc is allowed to do or not in this respect. Oh my, this is soooo tricky.

Signature · 6 lines
Jean Delvare

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

Re: [lm-sensors] [RFC/PATCH] hwmon: fix common race conditions,

In reply to Mark M. Hoffman

Hi Jean:

Thanks for the review...

Quoted 17 lines · Mark M. Hoffman
Quoted 16 lines

Here is the first in a batch of patches which aim to fix a common class of race conditions in most hwmon drivers. Credit goes to Herbert Valerio Riedel for pointing this out, here:

http://lists.lm-sensors.org/pipermail/lm-sensors/2007-November/022014.html

To actually trip on any of these race conditions in practice would be *exceedingly* rare. In fact, I don't think we've ever seen a report for such a case. OTOH, one can see by looking at this patch that the fixes are not always completely trivial.

I will concentrate on cranking out the remaining patches as I have time. But of course I would like thorough reviews, especially from the driver author/maintainer if possible. To all potential reviewers: look not just at the patch itself; please also scan the entire driver to see if there are any races that I missed. Testing is also much appreciated.

Quoted 10 lines · Jean Delvare

When looking for this kind of race, you should pay particular attention to macros and inline functions. Historically, some of our macros were evaluating their arguments more than once. I have tried to clean this up as we ported the driver to Linux 2.6 but I wouldn't swear that such macros are all gone; there could be a couple still lurking.

As for inline functions, I really don't know how gcc handles these. If it creates a temporary copy of the parameters on the stack, then we're safe. If not, then even inline functions evaluating their parameters more than once could hide the race.

No, inline functions must behave as regular functions in this regard. Their arguments are only evaluated once. This is almost the entire reason that inline functions are preferred over macros.

That said, careful attention is needed for *all* functions. E.g. passing two values from the driver data structure to a function and assuming that they're accessed coherently is still a race...

Quoted 12 lines
Quoted 12 lines
drivers/hwmon/adm1026.c +1 -2
index 904c6ce..92bed53 100644
@@ -838,20 +838,24 @@ static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_m
 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 		char *buf)
 {
-	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
-	int nr = sensor_attr->index;
+	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1026_data *data = adm1026_update_device(dev);
Quoted 8 lines

drivers/hwmon/adm1026.c static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_m · line 848

Quoted 8 lines
drivers/hwmon/adm1026.c +5 -2
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
-		data->fan_div[nr]));
+	int val;
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan[nr], data->fan_div[nr]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }

... and here is a good example too. In the snippet above, even if FAN_FROM_REG() were a regular (not inline) function, the lock/unlock would still be necessary to prevent the race.

Quoted 4 lines

As you are making the functions bigger, I think it would be a good idea to leave a blank like between the variable declarations and the "real" code, as is common practice for non-trivial functions. Same goes for the rest of the patch, of course.

Good point; fixed.

Quoted 23 lines
Quoted 23 lines
drivers/hwmon/adt7470.c +6 -7
index 6b5325f..2c398d5 100644
(...)
@@ -678,14 +684,15 @@ static ssize_t show_pwm_auto_temp(struct device *dev,
 				  struct device_attribute *devattr,
 				  char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adt7470_data *data = adt7470_update_device(dev);
-	u8 ctrl = data->pwm_auto_temp[attr->index];
-
-	if (ctrl)
-		return sprintf(buf, "%d\n", 1 << (ctrl - 1));
-	else
-		return sprintf(buf, "%d\n", ADT7470_PWM_ALL_TEMPS);
+	u8 ctrl;
+	int val;
+	mutex_lock(&data->lock);
+	ctrl = data->pwm_auto_temp[index];
+	val = ctrl ?  (1 << (ctrl - 1)) : ADT7470_PWM_ALL_TEMPS;
Quoted 8 lines

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

Quoted 3 lines
drivers/hwmon/adt7470.c +2 -0
+	mutex_unlock(&data->lock);
+	return sprintf(buf, "%d\n", val);
 }

Not sure why this change is needed? data->pwm_auto_temp[index] is only accessed once. Unless gcc optimizes the code to get rid of the ctrl local variable, of course. I really have no idea what gcc is allowed to do or not in this respect. Oh my, this is soooo tricky.

Oops, that's just a thinko on my part. There was no race there; fixed. The compiler is not allowed to "optimize" that way.

Updated patch to follow...

Thanks & regards,

Signature · 8 lines
Mark M. Hoffman
[email protected]


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

Re: [lm-sensors] [RFC/PATCH] hwmon: fix common race conditions,

In reply to Mark M. Hoffman

Hi all:

 adm1026.c |   22 ++++++++++++++--------
 adm1029.c |   30 +++++++++++++++++++-----------
 adm1031.c |   20 +++++++++++++++-----
 adm9240.c |   20 ++++++++++++++------
 adt7470.c |   40 ++++++++++++++++++++++++----------------
 adt7473.c |   54 +++++++++++++++++++++++++++++++-----------------------
 asb100.c  |   16 ++++++++++++----
 7 files changed, 129 insertions(+), 73 deletions(-)

commit 3c798b55145f42725c54d64ac7d69062a5d62bad

Author: Mark M. Hoffman <[email protected]>
Date:   Mon Mar 3 23:35:52 2008 -0500

hwmon: fix common race conditions, batch 1

Most hwmon drivers have one or more race conditions that could cause the driver to display incorrect data; in the absolute worst case, these races could allow divide-by-zero/OOPS. The most common of these involves a race between setting a fan divider and displaying the fan data (RPMs). This patch fixes these race conditions by taking the update lock (or equivalent) as necessary.

drivers/hwmon/adm1026.c +14 -8
index 904c6ce..ff1fded 100644
@@ -838,20 +838,26 @@ static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_m
 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 		char *buf)
 {
-	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
-	int nr = sensor_attr->index;
+	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1026_data *data = adm1026_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
-		data->fan_div[nr]));
+	int val;
+
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan[nr], data->fan_div[nr]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
 		char *buf)
 {
-	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
-	int nr = sensor_attr->index;
+	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1026_data *data = adm1026_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
-		data->fan_div[nr]));
+	int val;
+
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan_min[nr], data->fan_div[nr]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
 		const char *buf, size_t count)
drivers/hwmon/adm1029.c +19 -11
index 2c6608d..8cd6c5a 100644
@@ -165,27 +165,35 @@ show_temp(struct device *dev, struct device_attribute *devattr, char *buf)
 static ssize_t
 show_fan(struct device *dev, struct device_attribute *devattr, char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adm1029_data *data = adm1029_update_device(dev);
 	u16 val;
-	if (data->fan[attr->index] = 0 || data->fan_div[attr->index] = 0
-	    || data->fan[attr->index] = 255) {
-		return sprintf(buf, "0\n");
-	}
 
-	val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index])
-	    / data->fan[attr->index];
+	mutex_lock(&data->update_lock);
+	if (data->fan[index] = 0 || data->fan_div[index] = 0
+					|| data->fan[index] = 255)
+		val = 0;
+	else
+		val = 1880 * 120 / DIV_FROM_REG(data->fan_div[index])
+					/ data->fan[index];
+	mutex_unlock(&data->update_lock);
 	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t
 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adm1029_data *data = adm1029_update_device(dev);
-	if (data->fan_div[attr->index] = 0)
-		return sprintf(buf, "0\n");
-	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index]));
+	int val;
+
+	mutex_lock(&data->update_lock);
+	if (data->fan_div[index] = 0)
+		val = 0;
+	else
+		val = DIV_FROM_REG(data->fan_div[index]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t set_fan_div(struct device *dev,
drivers/hwmon/adm1031.c +15 -5
index 2bffcab..c88187a 100644
@@ -466,8 +466,10 @@ static ssize_t show_fan(struct device *dev,
 	struct adm1031_data *data = adm1031_update_device(dev);
 	int value;
 
+	mutex_lock(&data->update_lock);
 	value = trust_fan_readings(data, nr) ? FAN_FROM_REG(data->fan[nr],
 				 FAN_DIV_FROM_REG(data->fan_div[nr])) : 0;
+	mutex_unlock(&data->update_lock);
 	return sprintf(buf, "%d\n", value);
 }
 
@@ -483,9 +485,13 @@ static ssize_t show_fan_min(struct device *dev,
 {
 	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1031_data *data = adm1031_update_device(dev);
-	return sprintf(buf, "%d\n",
-		       FAN_FROM_REG(data->fan_min[nr],
-				    FAN_DIV_FROM_REG(data->fan_div[nr])));
+	int value;
+
+	mutex_lock(&data->update_lock);
+	value = FAN_FROM_REG(data->fan_min[nr],
+				    FAN_DIV_FROM_REG(data->fan_div[nr]));
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", value);
 }
 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
 			   const char *buf, size_t count)
@@ -567,11 +573,15 @@ static ssize_t show_temp(struct device *dev,
 {
 	int nr = to_sensor_dev_attr(attr)->index;
 	struct adm1031_data *data = adm1031_update_device(dev);
-	int ext;
+	int ext, val;
+
+	mutex_lock(&data->update_lock);
 	ext = nr = 0 ?
 	    ((data->ext_temp[nr] >> 6) & 0x3) * 2 :
 	    (((data->ext_temp[nr] >> ((nr - 1) * 3)) & 7));
-	return sprintf(buf, "%d\n", TEMP_FROM_REG_EXT(data->temp[nr], ext));
+	val = TEMP_FROM_REG_EXT(data->temp[nr], ext);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 static ssize_t show_temp_min(struct device *dev,
 			     struct device_attribute *attr, char *buf)
drivers/hwmon/adm9240.c +14 -6
index 149ef25..f43986d 100644
@@ -290,19 +290,27 @@ vin(5);
 static ssize_t show_fan(struct device *dev,
 		struct device_attribute *devattr, char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adm9240_data *data = adm9240_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
-				1 << data->fan_div[attr->index]));
+	int val;
+
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan[index], 1 << data->fan_div[index]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t show_fan_min(struct device *dev,
 		struct device_attribute *devattr, char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adm9240_data *data = adm9240_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
-				1 << data->fan_div[attr->index]));
+	int val;
+
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan_min[index], 1 << data->fan_div[index]);
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t show_fan_div(struct device *dev,
drivers/hwmon/adt7470.c +24 -16
index 6b5325f..4573855 100644
@@ -402,14 +402,17 @@ static ssize_t show_fan_max(struct device *dev,
 			    struct device_attribute *devattr,
 			    char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adt7470_data *data = adt7470_update_device(dev);
+	int val;
 
-	if (FAN_DATA_VALID(data->fan_max[attr->index]))
-		return sprintf(buf, "%d\n",
-			       FAN_PERIOD_TO_RPM(data->fan_max[attr->index]));
+	mutex_lock(&data->lock);
+	if (FAN_DATA_VALID(data->fan_max[index]))
+		val = FAN_PERIOD_TO_RPM(data->fan_max[index]);
 	else
-		return sprintf(buf, "0\n");
+		val = 0;
+	mutex_unlock(&data->lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t set_fan_max(struct device *dev,
@@ -437,14 +440,16 @@ static ssize_t show_fan_min(struct device *dev,
 			    struct device_attribute *devattr,
 			    char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adt7470_data *data = adt7470_update_device(dev);
-
-	if (FAN_DATA_VALID(data->fan_min[attr->index]))
-		return sprintf(buf, "%d\n",
-			       FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
+	int val;
+	mutex_lock(&data->lock);
+	if (FAN_DATA_VALID(data->fan_min[index]))
+		val = FAN_PERIOD_TO_RPM(data->fan_min[index]);
 	else
-		return sprintf(buf, "0\n");
+		val = 0;
+	mutex_unlock(&data->lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t set_fan_min(struct device *dev,
@@ -471,14 +476,17 @@ static ssize_t set_fan_min(struct device *dev,
 static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
 			char *buf)
 {
-	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
+	int index = to_sensor_dev_attr(devattr)->index;
 	struct adt7470_data *data = adt7470_update_device(dev);
+	int val;
 
-	if (FAN_DATA_VALID(data->fan[attr->index]))
-		return sprintf(buf, "%d\n",
-			       FAN_PERIOD_TO_RPM(data->fan[attr->index]));
+	mutex_lock(&data->lock);
+	if (FAN_DATA_VALID(data->fan[index]))
+		val = FAN_PERIOD_TO_RPM(data->fan[index]);
 	else
-		return sprintf(buf, "0\n");
+		val = 0;
+	mutex_unlock(&data->lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t show_force_pwm_max(struct device *dev,
drivers/hwmon/adt7473.c +31 -23

96 lines not on this page · raw message

drivers/hwmon/asb100.c +12 -4
index fe2eea4..2aeac1a 100644
@@ -276,8 +276,12 @@ static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
 {
 	int nr = to_sensor_dev_attr(attr)->index;
 	struct asb100_data *data = asb100_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
-		DIV_FROM_REG(data->fan_div[nr])));
+	int val;
+
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan[nr], DIV_FROM_REG(data->fan_div[nr]));
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
@@ -285,8 +289,12 @@ static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
 {
 	int nr = to_sensor_dev_attr(attr)->index;
 	struct asb100_data *data = asb100_update_device(dev);
-	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
-		DIV_FROM_REG(data->fan_div[nr])));
+	int val;
+
+	mutex_lock(&data->update_lock);
+	val = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
+	mutex_unlock(&data->update_lock);
+	return sprintf(buf, "%d\n", val);
 }
 
 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
Signature · 8 lines
Mark M. Hoffman
[email protected]


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

Re: [lm-sensors] [RFC/PATCH] hwmon: fix common race conditions,

In reply to Mark M. Hoffman
Quoted 17 lines · Mark M. Hoffman

Hi all:

adm1026.c |   22 ++++++++++++++--------
adm1029.c |   30 +++++++++++++++++++-----------
adm1031.c |   20 +++++++++++++++-----
adm9240.c |   20 ++++++++++++++------
adt7470.c |   40 ++++++++++++++++++++++++----------------
adt7473.c |   54 +++++++++++++++++++++++++++++++-----------------------
asb100.c  |   16 ++++++++++++----
7 files changed, 129 insertions(+), 73 deletions(-)

commit 3c798b55145f42725c54d64ac7d69062a5d62bad

Author: Mark M. Hoffman <[email protected]>
Date:   Mon Mar 3 23:35:52 2008 -0500

hwmon: fix common race conditions, batch 1

Quoted 7 lines

Most hwmon drivers have one or more race conditions that could cause the driver to display incorrect data; in the absolute worst case, these races could allow divide-by-zero/OOPS. The most common of these involves a race between setting a fan divider and displaying the fan data (RPMs). This patch fixes these race conditions by taking the update lock (or equivalent) as necessary.

adm9240 tested on 2.6.25-rc6 and lm-sensors 3.0.0, no problems noticed, no observed breakage ;) I doubt there's a possible div0/oops involved.

Grant.

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

#9 lore raw

Re: [lm-sensors] [RFC/PATCH] hwmon: fix common race conditions,

In reply to Mark M. Hoffman
Quoted 17 lines · Mark M. Hoffman

Hi all:

 adm1026.c |   22 ++++++++++++++--------
 adm1029.c |   30 +++++++++++++++++++-----------
 adm1031.c |   20 +++++++++++++++-----
 adm9240.c |   20 ++++++++++++++------
 adt7470.c |   40 ++++++++++++++++++++++++----------------
 adt7473.c |   54 +++++++++++++++++++++++++++++++-----------------------
 asb100.c  |   16 ++++++++++++----
 7 files changed, 129 insertions(+), 73 deletions(-)

commit 3c798b55145f42725c54d64ac7d69062a5d62bad

Author: Mark M. Hoffman <[email protected]>
Date:   Mon Mar 3 23:35:52 2008 -0500

hwmon: fix common race conditions, batch 1

Quoted 7 lines

Most hwmon drivers have one or more race conditions that could cause the driver to display incorrect data; in the absolute worst case, these races could allow divide-by-zero/OOPS. The most common of these involves a race between setting a fan divider and displaying the fan data (RPMs). This patch fixes these race conditions by taking the update lock (or equivalent) as necessary.

The adt7470/7473 bits compile and survive simple testing, so

--D

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