Skip to contentSkip to conversation

Improve a lockdep complaint suppression approach

PATCH New Bart Van Assche 1 participant 4 messages Sep 12, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover Improve a lockdep complaint suppression approach

Hi Greg,

This patch series is intended as an improvement for commit fc88bb116179 ("usb:
roles: add lockdep class key to struct usb_role_switch"). Please consider this patch series for the next merge window.

Thanks,

Bart.

Changes compared to v2:
- Rebased this patch series on top of Greg KH's for-next branch.
- Left out the "Fixes:" and "Cc: stable" tags.

Changes compared to v1:
- Added two patches. One that combines the two mutex_init() definitions and another patch that introduces mutex_init_with_key().
- Changed patch 3/3 such that it uses mutex_init_with_key(). Added Amit's Signed-off-by.

Bart Van Assche (3):
locking/mutex: Define mutex_init() once
locking/mutex: Introduce mutex_init_with_key()
usb: roles: Improve the fix for a false positive recursive locking
complaint

 drivers/usb/roles/class.c | 14 +++++---------
 include/linux/mutex.h     | 19 ++++++++++++-------
 2 files changed, 17 insertions(+), 16 deletions(-)
#1 lore raw

PATCH v3 1/3 locking/mutex: Define mutex_init() once

With CONFIG_PREEMPT_RT disabled __mutex_init() is a function. With CONFIG_PREEMPT_RT enabled, __mutex_init() is a macro. I assume this is why mutex_init() is defined twice as exactly the same macro.

Prepare for introducing a new macro for mutex initialization by combining the two identical mutex_init() definitions into a single definition. This patch does not change any functionality because the C preprocessor expands macros when it encounters the macro name and not when a macro definition is encountered. See also commit bb630f9f7a7d ("locking/rtmutex: Add mutex variant for RT").

---
 include/linux/mutex.h | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
include/linux/mutex.h +1 -7
index a561c629d89f..ef617089db19 100644
@@ -49,7 +49,6 @@ static inline void mutex_destroy(struct mutex *lock) {}
 
 #endif
 
-#ifndef CONFIG_PREEMPT_RT
 /**
  * mutex_init - initialize the mutex
  * @mutex: the mutex to be initialized
@@ -65,6 +64,7 @@ do {									\
 	__mutex_init((mutex), #mutex, &__key);				\
 } while (0)
 
+#ifndef CONFIG_PREEMPT_RT
 #define __MUTEX_INITIALIZER(lockname) \
 		{ .owner = ATOMIC_LONG_INIT(0) \
 		, .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
@@ -111,12 +111,6 @@ do {							\
 	__mutex_rt_init((mutex), name, key);		\
 } while (0)
 
-#define mutex_init(mutex)				\
-do {							\
-	static struct lock_class_key __key;		\
-							\
-	__mutex_init((mutex), #mutex, &__key);		\
-} while (0)
 #endif /* CONFIG_PREEMPT_RT */
 
 #ifdef CONFIG_DEBUG_MUTEXES
#2 lore raw

PATCH v3 2/3 locking/mutex: Introduce mutex_init_with_key()

In reply to Bart Van Assche

The following pattern occurs 5 times in kernel drivers:

	lockdep_register_key(key);
	__mutex_init(mutex, name, key);

In several cases the 'name' argument matches #mutex. Hence, introduce the mutex_init_with_key() macro. This macro derives the 'name' argument from the 'mutex' argument.

---
 include/linux/mutex.h | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/linux/mutex.h +11 -0
index ef617089db19..2bf91b57591b 100644
@@ -64,6 +64,17 @@ do {									\
 	__mutex_init((mutex), #mutex, &__key);				\
 } while (0)
 
+/**
+ * mutex_init_with_key - initialize a mutex with a given lockdep key
+ * @mutex: the mutex to be initialized
+ * @key: the lockdep key to be associated with the mutex
+ *
+ * Initialize the mutex to the unlocked state.
+ *
+ * It is not allowed to initialize an already locked mutex.
+ */
+#define mutex_init_with_key(mutex, key) __mutex_init((mutex), #mutex, (key))
+
 #ifndef CONFIG_PREEMPT_RT
 #define __MUTEX_INITIALIZER(lockname) \
 		{ .owner = ATOMIC_LONG_INIT(0) \
#3 lore raw

PATCH v3 3/3 usb: roles: Improve the fix for a false positive recursive locking complaint

In reply to Bart Van Assche

Improve commit fc88bb116179 ("usb: roles: add lockdep class key to struct usb_role_switch") as follows:
* Move the lock class key declaration just above the mutex declaration such that the declaration order of these objects matches their initialization order.
* Destroy the mutex and lock class key just before these objects are freed. This makes it easier to verify that the destruction calls happen after the last use of these objects.
* Instead of switching the mutex key to the dynamic lock class key after initialization of the mutex has completed, initialize the mutex with the dynamic lock class key.

Cc Amit Sunil Dhamne <[email protected]>
Cc Badhri Jagan Sridharan <[email protected]>
Cc Hans de Goede <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Heikki Krogerus <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/usb/roles/class.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/usb/roles/class.c +5 -9
index 7aca1ef7f44c..c58a12c147f4 100644
@@ -22,6 +22,7 @@ static const struct class role_class = {
 
 struct usb_role_switch {
 	struct device dev;
+	struct lock_class_key key;
 	struct mutex lock; /* device lock*/
 	struct module *module; /* the module this device depends on */
 	enum usb_role role;
@@ -34,8 +35,6 @@ struct usb_role_switch {
 	usb_role_switch_set_t set;
 	usb_role_switch_get_t get;
 	bool allow_userspace_control;
-
-	struct lock_class_key key;
 };
 
 #define to_role_switch(d)	container_of(d, struct usb_role_switch, dev)
@@ -329,6 +328,8 @@ static void usb_role_switch_release(struct device *dev)
 {
 	struct usb_role_switch *sw = to_role_switch(dev);
 
+	mutex_destroy(&sw->lock);
+	lockdep_unregister_key(&sw->key);
 	kfree(sw);
 }
 
@@ -367,7 +368,8 @@ usb_role_switch_register(struct device *parent,
 	if (!sw)
 		return ERR_PTR(-ENOMEM);
 
-	mutex_init(&sw->lock);
+	lockdep_register_key(&sw->key);
+	mutex_init_with_key(&sw->lock, &sw->key);
 
 	sw->allow_userspace_control = desc->allow_userspace_control;
 	sw->usb2_port = desc->usb2_port;
@@ -399,9 +401,6 @@ usb_role_switch_register(struct device *parent,
 
 	sw->registered = true;
 
-	lockdep_register_key(&sw->key);
-	lockdep_set_class(&sw->lock, &sw->key);
-
 	/* TODO: Symlinks for the host port and the device controller. */
 
 	return sw;
@@ -418,9 +417,6 @@ void usb_role_switch_unregister(struct usb_role_switch *sw)
 {
 	if (IS_ERR_OR_NULL(sw))
 		return;
-
-	lockdep_unregister_key(&sw->key);
-
 	sw->registered = false;
 	if (dev_fwnode(&sw->dev))
 		component_del(&sw->dev, &connector_ops);