Skip to contentSkip to conversation

usb: roles: add lockdep class key to struct usb_role_switch

PATCH Superseded Amit Sunil Dhamne 2 participants 3 messages Aug 13, 2024 – Aug 15, 2024 USB

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: roles: add lockdep class key to struct usb_role_switch

There can be multiple role switch devices running on a platform. Given that lockdep is not capable of differentiating between locks of different instances, false positive warnings for circular locking are reported. To prevent this, register unique lockdep key for each of the individual instances.

---
 drivers/usb/roles/class.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/usb/roles/class.c +15 -0
index d7aa913ceb8a..807a8f18ec20 100644
@@ -11,6 +11,7 @@
 #include <linux/usb/role.h>
 #include <linux/property.h>
 #include <linux/device.h>
+#include <linux/lockdep.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
@@ -33,6 +34,10 @@ struct usb_role_switch {
 	usb_role_switch_set_t set;
 	usb_role_switch_get_t get;
 	bool allow_userspace_control;
+
+#ifdef CONFIG_LOCKDEP
+	struct lock_class_key key;
+#endif
 };
 
 #define to_role_switch(d)	container_of(d, struct usb_role_switch, dev)
@@ -396,6 +401,11 @@ usb_role_switch_register(struct device *parent,
 
 	sw->registered = true;
 
+#ifdef CONFIG_LOCKDEP
+	lockdep_register_key(&sw->key);
+	lockdep_set_class(&sw->lock, &sw->key);
+#endif
+
 	/* TODO: Symlinks for the host port and the device controller. */
 
 	return sw;
@@ -412,6 +422,11 @@ void usb_role_switch_unregister(struct usb_role_switch *sw)
 {
 	if (IS_ERR_OR_NULL(sw))
 		return;
+
+#ifdef CONFIG_LOCKDEP
+	lockdep_unregister_key(&sw->key);
+#endif
+
 	sw->registered = false;
 	if (dev_fwnode(&sw->dev))
 		component_del(&sw->dev, &connector_ops);

base-commit: 38343be0bf9a7d7ef0d160da5f2db887a0e29b62

Signature · 1 line
2.46.0.76.ge559c4bf1a-goog
maintainer
#1 lore raw
Quoted 24 lines · Amit Sunil Dhamne

There can be multiple role switch devices running on a platform. Given that lockdep is not capable of differentiating between locks of different instances, false positive warnings for circular locking are reported. To prevent this, register unique lockdep key for each of the individual instances.

---
 drivers/usb/roles/class.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/usb/roles/class.c +1 -0
index d7aa913ceb8a..807a8f18ec20 100644
@@ -11,6 +11,7 @@
 #include <linux/usb/role.h>
 #include <linux/property.h>
 #include <linux/device.h>
+#include <linux/lockdep.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>

drivers/usb/roles/class.c struct usb_role_switch { · line 40

Quoted 7 lines
drivers/usb/roles/class.c +4 -0
@@ -33,6 +34,10 @@ struct usb_role_switch {
 	usb_role_switch_set_t set;
 	usb_role_switch_get_t get;
 	bool allow_userspace_control;
+
+#ifdef CONFIG_LOCKDEP
+	struct lock_class_key key;
+#endif

Please do not put #ifdef lines in .c files, they are not needed for this change to work properly, right?

checkpatch should have complained about this...

thanks,

greg k-h

#2 lore raw
Quoted 26 lines · Greg KH
Quoted 25 lines · Amit Sunil Dhamne

There can be multiple role switch devices running on a platform. Given that lockdep is not capable of differentiating between locks of different instances, false positive warnings for circular locking are reported. To prevent this, register unique lockdep key for each of the individual instances.

---
  drivers/usb/roles/class.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
drivers/usb/roles/class.c +1 -0
index d7aa913ceb8a..807a8f18ec20 100644
@@ -11,6 +11,7 @@
  #include <linux/usb/role.h>
  #include <linux/property.h>
  #include <linux/device.h>
+#include <linux/lockdep.h>
  #include <linux/module.h>
  #include <linux/mutex.h>
  #include <linux/slab.h>
@@ -33,6 +34,10 @@ struct usb_role_switch {
  	usb_role_switch_set_t set;
Quoted 8 lines

drivers/usb/roles/class.c struct usb_role_switch { · line 40

Quoted 6 lines
drivers/usb/roles/class.c +4 -0
  	usb_role_switch_get_t get;
  	bool allow_userspace_control;
+
+#ifdef CONFIG_LOCKDEP
+	struct lock_class_key key;
+#endif

Please do not put #ifdef lines in .c files, they are not needed for this change to work properly, right?

That's right. I will correct it in the next revision.

Quoted 2 lines

checkpatch should have complained about this...

Unfortunately it didn't complain.

Thanks,

Amit

Quoted 4 lines

thanks,

greg k-h