Skip to contentSkip to conversation

drivers/hwmon/adm9240: fix data race in adm9240_fan_read

PATCH Superseded Li Zhong 2 participants 4 messages Sep 22, 2022 Hardware monitoring

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li Zhong

author
#0 lore raw

PATCH 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 | 2 ++
 1 file changed, 2 insertions(+)
drivers/hwmon/adm9240.c +2 -0
index 483cd757abd3..d93ae3147994 100644
@@ -501,6 +501,7 @@ 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)
 			return err;
@@ -511,6 +512,7 @@ static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val
 			if (err)
 				return err;
 		}
+		mutex_unlock(&data->update_lock);
 		*val = FAN_FROM_REG(regval, BIT(data->fan_div[channel]));
 		break;
 	case hwmon_fan_div:
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 19 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.

---
  drivers/hwmon/adm9240.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/hwmon/adm9240.c +0 -0
index 483cd757abd3..d93ae3147994 100644

drivers/hwmon/adm9240.c static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val · line 507

Quoted 7 lines
drivers/hwmon/adm9240.c +1 -0
@@ -501,6 +501,7 @@ 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)
  			return err;

The mutex needs to be released here.

Quoted 8 lines
drivers/hwmon/adm9240.c +1 -0
@@ -511,6 +512,7 @@ static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val
  			if (err)
  				return err;
  		}
+		mutex_unlock(&data->update_lock);
  		*val = FAN_FROM_REG(regval, BIT(data->fan_div[channel]));
  		break;
  	case hwmon_fan_div:
author
#2 lore raw
Quoted 31 lines · Guenter Roeck
Quoted 19 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.

---
  drivers/hwmon/adm9240.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/hwmon/adm9240.c +0 -0
index 483cd757abd3..d93ae3147994 100644

drivers/hwmon/adm9240.c static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val · line 507

Quoted 7 lines
drivers/hwmon/adm9240.c +1 -0
@@ -501,6 +501,7 @@ 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)
                      return err;

The mutex needs to be released here.

Quoted 9 lines

drivers/hwmon/adm9240.c static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val · line 518

Quoted 8 lines
drivers/hwmon/adm9240.c +1 -0
@@ -511,6 +512,7 @@ static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val
                      if (err)
                              return err;
              }
+             mutex_unlock(&data->update_lock);
              *val = FAN_FROM_REG(regval, BIT(data->fan_div[channel]));
              break;
      case hwmon_fan_div:

Thanks. Fixed in v2 patch.

maintainer
#3 lore raw
In reply to Li Zhong
Quoted 27 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.

---
  drivers/hwmon/adm9240.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/hwmon/adm9240.c +1 -0
index 483cd757abd3..d93ae3147994 100644
@@ -501,6 +501,7 @@ 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)
  			return err;

drivers/hwmon/adm9240.c static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val · line 513

Quoted 2 lines
drivers/hwmon/adm9240.c +0 -0
@@ -511,6 +512,7 @@ static int adm9240_fan_read(struct device *dev, u32 attr, int channel, long *val
  			if (err)
  				return err;

The mutex also needs to be released here (in the error handling path).

Quoted 5 lines
drivers/hwmon/adm9240.c +1 -0
  		}
+		mutex_unlock(&data->update_lock);
  		*val = FAN_FROM_REG(regval, BIT(data->fan_div[channel]));
  		break;
  	case hwmon_fan_div: