Linux Hardware Monitor development
 help / color / mirror / Atom feed
* [PATCH] hwmon: Use per-device lockdep keys
@ 2026-11-16  0:42 Guenter Roeck
  2026-11-16  0:50 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Guenter Roeck @ 2026-11-16  0:42 UTC (permalink / raw)
  To: Hardware Monitoring; +Cc: Guenter Roeck, Davíð Steinn Geirsson

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 <david@dsg.is>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 drivers/hwmon/hwmon.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
index 73a818806d3d..6cfcdb9f60b7 100644
--- a/drivers/hwmon/hwmon.c
+++ b/drivers/hwmon/hwmon.c
@@ -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);
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-16  0:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  0:42 [PATCH] hwmon: Use per-device lockdep keys Guenter Roeck
2026-11-16  0:50 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox