Skip to content

Commit 7d01ef7

Browse files
bvanasschegregkh
authored andcommitted
usb: roles: Improve the fix for a false positive recursive locking complaint
Improve commit fc88bb1 ("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]> Signed-off-by: Bart Van Assche <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent e837d83 commit 7d01ef7

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

drivers/usb/roles/class.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static const struct class role_class = {
2222

2323
struct usb_role_switch {
2424
struct device dev;
25+
struct lock_class_key key;
2526
struct mutex lock; /* device lock*/
2627
struct module *module; /* the module this device depends on */
2728
enum usb_role role;
@@ -34,8 +35,6 @@ struct usb_role_switch {
3435
usb_role_switch_set_t set;
3536
usb_role_switch_get_t get;
3637
bool allow_userspace_control;
37-
38-
struct lock_class_key key;
3938
};
4039

4140
#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)
329328
{
330329
struct usb_role_switch *sw = to_role_switch(dev);
331330

331+
mutex_destroy(&sw->lock);
332+
lockdep_unregister_key(&sw->key);
332333
kfree(sw);
333334
}
334335

@@ -367,7 +368,8 @@ usb_role_switch_register(struct device *parent,
367368
if (!sw)
368369
return ERR_PTR(-ENOMEM);
369370

370-
mutex_init(&sw->lock);
371+
lockdep_register_key(&sw->key);
372+
mutex_init_with_key(&sw->lock, &sw->key);
371373

372374
sw->allow_userspace_control = desc->allow_userspace_control;
373375
sw->usb2_port = desc->usb2_port;
@@ -399,9 +401,6 @@ usb_role_switch_register(struct device *parent,
399401

400402
sw->registered = true;
401403

402-
lockdep_register_key(&sw->key);
403-
lockdep_set_class(&sw->lock, &sw->key);
404-
405404
/* TODO: Symlinks for the host port and the device controller. */
406405

407406
return sw;
@@ -418,9 +417,6 @@ void usb_role_switch_unregister(struct usb_role_switch *sw)
418417
{
419418
if (IS_ERR_OR_NULL(sw))
420419
return;
421-
422-
lockdep_unregister_key(&sw->key);
423-
424420
sw->registered = false;
425421
if (dev_fwnode(&sw->dev))
426422
component_del(&sw->dev, &connector_ops);

0 commit comments

Comments
 (0)