Skip to contentSkip to conversation

drivers/hwmon/adm9240: fix data race in adm9240_fan_read

PATCH Applied Li Zhong 2 participants 2 messages Sep 24, 2022 Hardware monitoring

lore
Share & tools
lore

Li Zhong

author
#0 lore raw

PATCH v3 1/1 drivers/hwmon/adm9240: fix data race in adm9240_fan_read

In
adm9240_read()
adm9240_fan_read()

    adm9240_write_fan_div(),

it assumes that the caller of adm9240_write_fan_div() must hold data->update_lock. Otherwise, it may cause data races when data is updated by other threads.

---
 drivers/hwmon/adm9240.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/hwmon/adm9240.c +8 -2
index 483cd757abd3..40e3558d3709 100644
@@ -501,17 +501,23 @@ static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val
 
 	switch (attr) {
 	case hwmon_fan_input:
+		mutex_lock(&data->update_lock);
 		err = regmap_read(data->regmap, ADM9240_REG_FAN(channel), &regval);
-		if (err < 0)
+		if (err < 0) {
+			mutex_unlock(&data->update_lock);
 			return err;
+		}
 		if (regval == 255 && data->fan_div[channel] < 3) {
 			/* adjust fan clock divider on overflow */
 			err = adm9240_write_fan_div(data, channel,
 						    ++data->fan_div[channel]);
-			if (err)
+			if (err) {
+				mutex_unlock(&data->update_lock);
 				return err;
+			}
 		}
 		*val = FAN_FROM_REG(regval, BIT(data->fan_div[channel]));
+		mutex_unlock(&data->update_lock);
 		break;
 	case hwmon_fan_div:
 		*val = BIT(data->fan_div[channel]);
Signature · 1 line
2.25.1
maintainer Applied
#1 lore raw
Quoted 10 lines · Li Zhong

In
adm9240_read()
adm9240_fan_read()

    adm9240_write_fan_div(),

it assumes that the caller of adm9240_write_fan_div() must hold data->update_lock. Otherwise, it may cause data races when data is updated by other threads.

Applied.

Thanks,
Guenter