Skip to contentSkip to conversation

hwmon: Support guard() and scoped_guard for subsystem locks

PATCH New Guenter Roeck 2 participants 7 messages May 13 – May 14

Subsystems (3)
lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

cover hwmon: Support guard() and scoped_guard for subsystem locks

Add support for guard() and scoped_guard() for the hwmon subsystem lock to simplify its use, and convert existing drivers to use the new functions.

----------------------------------------------------------------
Guenter Roeck (4):
hwmon: Support guard() and scoped_guard for subsystem locks
hwmon: (lm90) Use guard() to acquire subsystem lock
hwmon: (ina2xx) Use scoped_guard() to acquire the subsystem lock
hwmon: (adt7411) Use scoped_guard() to acquire the subsystem lock

 Documentation/hwmon/hwmon-kernel-api.rst |  7 ++++---
 drivers/hwmon/adt7411.c                  | 10 +++++-----
 drivers/hwmon/ina2xx.c                   | 10 +++++-----
 drivers/hwmon/lm90.c                     |  9 ++-------
 include/linux/hwmon.h                    |  2 ++
 5 files changed, 18 insertions(+), 20 deletions(-)
author maintainer
#1 lore raw

PATCH 1/4 hwmon: Support guard() and scoped_guard for subsystem locks

Add support for guard() and scoped_guard() for the hwmon subsystem lock to simplify its use.

---
 Documentation/hwmon/hwmon-kernel-api.rst | 7 ++++---
 include/linux/hwmon.h                    | 2 ++
 2 files changed, 6 insertions(+), 3 deletions(-)
Documentation/hwmon/hwmon-kernel-api.rst +4 -3
index 1d7f1397a827..9fcde32a140d 100644
@@ -85,9 +85,10 @@ removal.
 When using ``[devm_]hwmon_device_register_with_info()`` to register the
 hardware monitoring device, accesses using the associated access functions
 are serialised by the hardware monitoring core. If a driver needs locking
-for other functions such as interrupt handlers or for attributes which are
-fully implemented in the driver, hwmon_lock() and hwmon_unlock() can be used
-to ensure that calls to those functions are serialized.
+for other functions such as interrupt handlers, attributes which are fully
+implemented in the driver, or debugfs functions, hwmon_lock() and hwmon_unlock()
+can be used to ensure that calls to those functions are serialized. Those
+functions also support guard() and scoped_guard() variants.
 
 Using devm_hwmon_device_register_with_info()
 --------------------------------------------
include/linux/hwmon.h +2 -0
index 301a83afbd66..04959e044fd0 100644
@@ -495,6 +495,8 @@ char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
 void hwmon_lock(struct device *dev);
 void hwmon_unlock(struct device *dev);
 
+DEFINE_GUARD(hwmon_lock, struct device *, hwmon_lock(_T), hwmon_unlock(_T))
+
 /**
  * hwmon_is_bad_char - Is the char invalid in a hwmon name
  * @ch: the char to be considered
Signature · 1 line
2.45.2
#2 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The header include/linux/hwmon.h uses the DEFINE_GUARD macro but fails to explicitly include…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The header include/linux/hwmon.h uses the DEFINE_GUARD macro but fails to explicitly include <linux/cleanup.h>. --

commit 14ebb15eae07a29dd197db88caa15eb8cc2e93a1
Author: Guenter Roeck <[email protected]>

hwmon: Support guard() and scoped_guard for subsystem locks

This commit adds support for guard() and scoped_guard() macros to the hwmon subsystem lock, aiming to simplify lock management.

Quoted 4 lines
include/linux/hwmon.h +0 -0
index 301a83afbd663..04959e044fd0f 100644

[ ... ]

include/linux/hwmon.h char *devm_hwmon_sanitize_name(struct device *dev, const char *name); · line 498

Quoted 5 lines
include/linux/hwmon.h +1 -0
@@ -495,6 +495,8 @@ char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
 void hwmon_lock(struct device *dev);
 void hwmon_unlock(struct device *dev);
 
+DEFINE_GUARD(hwmon_lock, struct device *, hwmon_lock(_T), hwmon_unlock(_T))

Does this header need to include <linux/cleanup.h>?

The DEFINE_GUARD macro is provided by <linux/cleanup.h>. While many files might implicitly pull it in through <linux/device.h> or <linux/mutex.h>, could this introduce a build regression for any compilation units that include <linux/hwmon.h> without an implicit include path to <linux/cleanup.h>?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH 2/4 hwmon: (lm90) Use guard() to acquire subsystem lock

In reply to Guenter Roeck

Use guard() instead of hwmon_lock() / hwmon_unlock() to acquire and release the hardware monitoring subsystem lock.

---
 drivers/hwmon/lm90.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/hwmon/lm90.c +2 -7
index 3c10a5066b53..9ca49abc1a93 100644
@@ -1222,13 +1222,8 @@ static int lm90_update_alarms_locked(struct lm90_data *data, bool force)
 
 static int lm90_update_alarms(struct lm90_data *data, bool force)
 {
-	int err;
-
-	hwmon_lock(data->hwmon_dev);
-	err = lm90_update_alarms_locked(data, force);
-	hwmon_unlock(data->hwmon_dev);
-
-	return err;
+	guard(hwmon_lock)(data->hwmon_dev);
+	return lm90_update_alarms_locked(data, force);
 }
 
 static void lm90_alert_work(struct work_struct *__work)
Signature · 1 line
2.45.2
#4 lore raw

Re: [PATCH 2/4] hwmon: (lm90) Use guard() to acquire subsystem lock

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Devm registration ordering causes a Use-After-Free of `data->hwmon_dev` if delayed works run…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Devm registration ordering causes a Use-After-Free of data->hwmon_dev if delayed works run during driver removal.
- [High] Modifying data->config in lm90_alert() without holding hwmon_lock creates a data race with sysfs operations. --

commit 7118ba7da109d5ac53da9e4739a437d57be7a2f6
Author: Guenter Roeck <[email protected]>
Subject: hwmon: (lm90) Use guard() to acquire subsystem lock

This commit converts the lm90 driver to use the scope-based guard() macro for acquiring and releasing the hardware monitoring subsystem lock, replacing the explicit hwmon_lock() and hwmon_unlock() calls.

Quoted 6 lines
drivers/hwmon/lm90.c +0 -0
index 3c10a5066b53d..9ca49abc1a93a 100644
@@ -1222,13 +1222,8 @@ static int lm90_update_alarms_locked(struct lm90_data *data, bool force)
 

drivers/hwmon/lm90.c static int lm90_update_alarms_locked(struct lm90_data *data, bool force) · line 1225

Quoted 10 lines
drivers/hwmon/lm90.c +1 -7
 static int lm90_update_alarms(struct lm90_data *data, bool force)
 {
-	int err;
-
-	hwmon_lock(data->hwmon_dev);
-	err = lm90_update_alarms_locked(data, force);
-	hwmon_unlock(data->hwmon_dev);
-
-	return err;
+	guard(hwmon_lock)(data->hwmon_dev);

Can this cause a use-after-free of data->hwmon_dev if the delayed works run during driver removal?

This is a pre-existing issue, but looking at lm90_probe(), the devm action to cancel the alert_work and report_work (lm90_restore_conf) is registered in lm90_init_client() before devm_hwmon_device_register_with_info() is called.

Because devm executes cleanup actions in reverse order during module unbind or probe failure, the hwmon device is unregistered and freed first.

If lm90_alert_work() or lm90_report_alarms() runs in the window between the hwmon device being freed and the delayed works being cancelled, lm90_update_alarms() will dereference the freed data->hwmon_dev here.

Is there also a data race when modifying data->config in lm90_alert() without holding the subsystem lock?

This is another pre-existing issue. lm90_alert() executes in the smbus alert context and calls lm90_update_confreg() to disable the hardware alert line, without acquiring hwmon_lock.

Concurrently, sysfs write operations (such as lm90_write_convrate) hold the hwmon_lock, temporarily modify data->config, and then restore it.

If an alert interrupt occurs concurrently with a sysfs write, the sysfs path will overwrite the alert handler's modifications to data->config and the hardware register.

This unintentionally re-enables the hardware alert line while the alarm is still active, causing an interrupt storm.

Quoted 4 lines
drivers/hwmon/lm90.c +1 -0
+	return lm90_update_alarms_locked(data, force);
 }
 
 static void lm90_alert_work(struct work_struct *__work)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author maintainer
#5 lore raw

PATCH 3/4 hwmon: (ina2xx) Use scoped_guard() to acquire the subsystem lock

In reply to Guenter Roeck

Use scoped_guard() instead of hwmon_lock() / hwmon_unlock() to acquire and release the hardware monitoring subsystem lock.

---
 drivers/hwmon/ina2xx.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/hwmon/ina2xx.c +5 -5
index 613ffb622b7c..3765c8542a75 100644
@@ -875,11 +875,11 @@ static ssize_t shunt_resistor_store(struct device *dev,
 	if (status < 0)
 		return status;
 
-	hwmon_lock(dev);
-	status = ina2xx_set_shunt(data, val);
-	hwmon_unlock(dev);
-	if (status < 0)
-		return status;
+	scoped_guard(hwmon_lock, dev) {
+		status = ina2xx_set_shunt(data, val);
+		if (status < 0)
+			return status;
+	}
 	return count;
 }
 
Signature · 1 line
2.45.2
author maintainer
#6 lore raw

PATCH 4/4 hwmon: (adt7411) Use scoped_guard() to acquire the subsystem lock

In reply to Guenter Roeck

Use scoped_guard() instead of hwmon_lock() / hwmon_unlock() to acquire and release the hardware monitoring subsystem lock.

---
 drivers/hwmon/adt7411.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/hwmon/adt7411.c +5 -5
index b9991a69e6c6..5bf3b3bd37da 100644
@@ -169,11 +169,11 @@ static ssize_t adt7411_set_bit(struct device *dev,
 	if (ret || flag > 1)
 		return -EINVAL;
 
-	hwmon_lock(dev);
-	ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
-	/* force update */
-	data->next_update = jiffies;
-	hwmon_unlock(dev);
+	scoped_guard(hwmon_lock, dev) {
+		ret = adt7411_modify_bit(client, s_attr2->index, s_attr2->nr, flag);
+		/* force update */
+		data->next_update = jiffies;
+	}
 
 	return ret < 0 ? ret : count;
 }
Signature · 1 line
2.45.2