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

* Re: [PATCH] hwmon: Use per-device lockdep keys
  2026-11-16  0:42 [PATCH] hwmon: Use per-device lockdep keys Guenter Roeck
@ 2026-11-16  0:50 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-16  0:50 UTC (permalink / raw)
  To: Guenter Roeck, Hardware Monitoring

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 <linux@roeck-us.net>

> 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 <david@dsg.is>
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>

[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")

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261116004209.2570779-1-linux@roeck-us.net?part=1

^ permalink raw reply	[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