All of lore.kernel.org
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Hardware Monitoring <linux-hwmon@vger.kernel.org>
Cc: "Guenter Roeck" <linux@roeck-us.net>,
	"Davíð Steinn Geirsson" <david@dsg.is>
Subject: [PATCH] hwmon: Use per-device lockdep keys
Date: Sun, 15 Nov 2026 16:42:09 -0800	[thread overview]
Message-ID: <20261116004209.2570779-1-linux@roeck-us.net> (raw)

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


             reply	other threads:[~2026-11-16  0:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16  0:42 Guenter Roeck [this message]
2026-11-16  0:50 ` [PATCH] hwmon: Use per-device lockdep keys sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116004209.2570779-1-linux@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=david@dsg.is \
    --cc=linux-hwmon@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.