Skip to contentSkip to conversation

hwmon: Use per-device lockdep keys

PATCH New Guenter Roeck 2 participants 2 messages Nov 16 Hardware monitoring

lore
Share & tools
lore

Guenter Roeck

author maintainer
#0 lore raw

PATCH 1/1 hwmon: Use per-device lockdep keys

Currently, mutex_init(&hwdev->lock) in __hwmon_device_register() assigns a single static lockdep key to all hardware monitoring device locks. If one hardware monitoring device accesses another hardware monitoring device (for example, pwm-fan controlling a PWM output provided by adt7470), lockdep sees two instances of the same lock class acquired in sequence and reports a false-positive recursive locking warning.

Give each struct hwmon_device its own dynamic struct lock_class_key, registered via lockdep_register_key() and initialized with mutex_init_with_key() before any error path that calls hwmon_dev_release(), and cleaned up with mutex_destroy() and lockdep_unregister_key() in hwmon_dev_release(). This allows nested locking across distinct hwmon devices while preserving full lockdep deadlock and self-recursion detection.

Cc Davíð Steinn Geirsson <[email protected]>
---
 drivers/hwmon/hwmon.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/hwmon/hwmon.c +6 -1
index 73a818806d3d..6cfcdb9f60b7 100644
@@ -18,6 +18,7 @@
 #include <linux/idr.h>
 #include <linux/kstrtox.h>
 #include <linux/list.h>
+#include <linux/lockdep.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/pci.h>
@@ -37,6 +38,7 @@ struct hwmon_device {
 	const char *label;
 	struct device dev;
 	const struct hwmon_chip_info *chip;
+	struct lock_class_key lock_key;
 	struct mutex lock;
 	struct list_head tzdata;
 	struct attribute_group group;
@@ -136,6 +138,8 @@ static void hwmon_dev_release(struct device *dev)
 		hwmon_free_attrs(hwdev->group.attrs_const);
 	kfree(hwdev->groups);
 	kfree(hwdev->label);
+	mutex_destroy(&hwdev->lock);
+	lockdep_unregister_key(&hwdev->lock_key);
 	kfree(hwdev);
 }
 
@@ -939,6 +943,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 	}
 
 	hdev = &hwdev->dev;
+	lockdep_register_key(&hwdev->lock_key);
+	mutex_init_with_key(&hwdev->lock, &hwdev->lock_key);
 
 	if (chip) {
 		const struct attribute **attrs;
@@ -993,7 +999,6 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
 		tdev = tdev->parent;
 	hdev->of_node = tdev ? tdev->of_node : NULL;
 	hwdev->chip = chip;
-	mutex_init(&hwdev->lock);
 	dev_set_drvdata(hdev, drvdata);
 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
 	err = device_register(hdev);
Signature · 1 line
2.45.2
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] hwmon: missing Fixes: trailer for lockdep false-positive fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] hwmon: missing Fixes: trailer for lockdep false-positive fix --

commit 4d05c24302d1083670a8cf3177a400690315c5c3
Author: Guenter Roeck <[email protected]>

Quoted 17 lines

hwmon: Use per-device lockdep keys

Currently, mutex_init(&hwdev->lock) in __hwmon_device_register() assigns a single static lockdep key to all hardware monitoring device locks. If one hardware monitoring device accesses another hardware monitoring device (for example, pwm-fan controlling a PWM output provided by adt7470), lockdep sees two instances of the same lock class acquired in sequence and reports a false-positive recursive locking warning.

Give each struct hwmon_device its own dynamic struct lock_class_key, registered via lockdep_register_key() and initialized with mutex_init_with_key() before any error path that calls hwmon_dev_release(), and cleaned up with mutex_destroy() and lockdep_unregister_key() in hwmon_dev_release(). This allows nested locking across distinct hwmon devices while preserving full lockdep deadlock and self-recursion detection.

Quoted 2 lines
Cc Davíð Steinn Geirsson <[email protected]>

[Severity: Low]
Since this fixes the false-positive lockdep warning introduced with hwdev->lock, should it include a Fixes tag?

Fixes 3ad2a7b9b15d ("hwmon: Serialize accesses in hwmon core")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1