usb: roles: Fix a false positive recursive locking complaint
Subsystems (2)
PATCH 1/1 usb: roles: Fix a false positive recursive locking complaint
Suppress the following lockdep complaint:
INFO: trying to register non-static key.
The code is fine but needs lockdep annotation, or maybe you didn't initialize this object before use? turning off the locking correctness validator.
--- drivers/usb/roles/class.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/usb/roles/class.c +5 -1
@@ -21,6 +21,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;
@@ -326,6 +327,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); }
@@ -364,7 +367,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 (&sw->lock,"usb_role_switch_desc::lock" , &sw->key); sw->allow_userspace_control = desc->allow_userspace_control; sw->usb2_port = desc->usb2_port;
HI Bart,
Quoted 41 lines · Bart Van Assche
Suppress the following lockdep complaint:
INFO: trying to register non-static key.
The code is fine but needs lockdep annotation, or maybe you didn't initialize this object before use? turning off the locking correctness validator.Cc Hans de Goede <[email protected]>Cc Andy Shevchenko <[email protected]>Cc Heikki Krogerus <[email protected]>Cc Greg Kroah-Hartman <[email protected]>Fixes fde0aa6c175a ("usb: common: Small class for USB role switches")Signed-off-by Bart Van Assche <[email protected]>--- drivers/usb/roles/class.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)drivers/usb/roles/class.c +3
-0@@ -21,6 +21,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;@@ -326,6 +327,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); }
drivers/usb/roles/class.c usb_role_switch_register(struct device *parent, · line 371
Quoted 6 lines
drivers/usb/roles/class.c +2
-1@@ -364,7 +367,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 (&sw->lock,"usb_role_switch_desc::lock" , &sw->key);
https://lore.kernel.org/all/ZsiYRAJST%[email protected]/ was already accepted and is perhaps better than what you are suggesting as it does not use the internal methods of mutex_init(). CCing Amit as well so that his patch can be submitted to stable trees as well.
drivers/usb/roles/class.c usb_role_switch_register(struct device *parent,
Quoted 4 lines
drivers/usb/roles/class.c +0
-0sw->allow_userspace_control = desc->allow_userspace_control; sw->usb2_port = desc->usb2_port;
Thanks,
Badhri
Quoted 2 lines · Badhri Jagan Sridharan
https://lore.kernel.org/all/ZsiYRAJST%[email protected]/ was already accepted
Thanks, I hadn't noticed this yet.
Quoted 2 lines
and is perhaps better than what you are suggesting as it does not use the internal methods of
mutex_init().
Although I do not have a strong opinion about which patch is sent to Linus, I think my patch has multiple advantages compared to the patch mentioned above:
- Cleaner. lockdep_set_class() is not used. Hence, it is not possible that the wrong lockdep key is used (the one assigned by mutex_init()).
- The lock_class_key declaration occurs close to the sw->lock declaration.
- The lockdep_register_key() call occurs close to __mutex_init() call that uses the registered key.
- Needs less memory in debug kernels. The advantage of __mutex_init() compared to mutex_init() is that it does not allocate (static) memory for a lockdep key.
Thanks,
Bart.
Hi Bart,
Quoted 9 lines · Bart Van Assche
Quoted 2 lines · Badhri Jagan Sridharan
https://lore.kernel.org/all/ZsiYRAJST%[email protected]/ was already accepted
Thanks, I hadn't noticed this yet.
Quoted 2 lines
and is perhaps better than what you are suggesting as it does not use the internal methods of
mutex_init().
Quoted 14 lines
Although I do not have a strong opinion about which patch is sent to Linus, I think my patch has multiple advantages compared to the patch mentioned above:
- Cleaner.lockdep_set_class()is not used. Hence, it is not possible that the wrong lockdep key is used (the one assigned bymutex_init()).
- Thelock_class_keydeclaration occurs close to thesw->lockdeclaration.
- Thelockdep_register_key()call occurs close to__mutex_init()call that uses the registered key.
- Needs less memory in debug kernels. The advantage of__mutex_init()compared tomutex_init()is that it does not allocate (static) memory for a lockdep key.
Thanks for the patch.
While I agree on (1) & (4), *may* be a good reason to reconsider. However, I have seen almost 30+ instances of the prior
method (https://lore.kernel.org/all/[email protected]/)
of registering lockdep key, which is what I followed.
However, if that's is not the right way, it brings into question the purpose of lockdep_set_class() considering I would always and unconditionally use __mutex_init() if I want to manage the lockdep class keys myself or mutex_init() if I didn't.
Thanks,
Amit
Quoted 4 lines
Thanks,
Bart.
Quoted 3 lines · Amit Sunil Dhamne
However, I have seen almost 30+ instances of the prior
method (https://lore.kernel.org/all/[email protected]/)
of registering lockdep key, which is what I followed.
Many of these examples are for spinlocks. It would be good to have a variant of spin_lock_init() that does not instantiate a struct lock_class_key and instead accepts a lock_class_key pointer as argument.
Quoted 4 lines
However, if that's is not the right way, it brings into question the purpose of
lockdep_set_class()considering I would always and unconditionally use __mutex_init() if I want to manage the lockdep class keys myself ormutex_init()if I didn't.
What I'm proposing is not a new pattern. There are multiple examples in the kernel tree of lockdep_register_key() calls followed by a __mutex_init() call:
$ git grep -wB3 __mutex_init | grep lockdep_register_key | wc -l 5
Thanks,
Bart.
Quoted 15 lines · Bart Van Assche
Quoted 4 lines · Amit Sunil Dhamne
However, I have seen almost 30+ instances of the prior method
(https://lore.kernel.org/all/[email protected]/)
of registering lockdep key, which is what I followed.Many of these examples are for spinlocks. It would be good to have a variant of
spin_lock_init()that does not instantiate a structlock_class_keyand instead accepts alock_class_keypointer as argument.Quoted 5 lines
However, if that's is not the right way, it brings into question the purpose
oflockdep_set_class()considering I would always and unconditionally use__mutex_init()if I want to manage the lockdep class keys myself ormutex_init()if I didn't.
Quoted 6 lines
What I'm proposing is not a new pattern. There are multiple examples in the kernel tree of
lockdep_register_key()calls followed by a__mutex_init()call:$ git grep -wB3 __mutex_init | grep lockdep_register_key | wc -l 5
I see pros and cons for both approaches, but I take Bart's as the simpler one. However, since it might be confusing, what I would suggest is to add a respective wrapper to mutex.h and have a non-__ named macro for this case.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 20 lines · Andy Shevchenko
Quoted 19 lines · Bart Van Assche
Quoted 4 lines · Amit Sunil Dhamne
However, I have seen almost 30+ instances of the prior method
(https://lore.kernel.org/all/[email protected]/)
of registering lockdep key, which is what I followed.Many of these examples are for spinlocks. It would be good to have a variant of
spin_lock_init()that does not instantiate a structlock_class_keyand instead accepts alock_class_keypointer as argument.Quoted 5 lines
However, if that's is not the right way, it brings into question the purpose
oflockdep_set_class()considering I would always and unconditionally use__mutex_init()if I want to manage the lockdep class keys myself ormutex_init()if I didn't.What I'm proposing is not a new pattern. There are multiple examples in the kernel tree of
lockdep_register_key()calls followed by a__mutex_init()call:
Quoted 7 lines
Quoted 2 lines
$ git grep -wB3 __mutex_init | grep lockdep_register_key | wc -l 5I see pros and cons for both approaches, but I take Bart's as the simpler one. However, since it might be confusing, what I would suggest is to add a respective wrapper to
mutex.hand have a non-__ named macro for this case.
To be clear, something like
#define mutex_init_with_lockdep(...)
do {
...
__mutex_init();
} while (0)
in the mutex.h.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 9 lines · Andy Shevchenko
To be clear, something like
#define
mutex_init_with_lockdep(...)do { ... __mutex_init(); } while (0)in the
mutex.h.
How about using the name "mutex_init_with_key()" since the name "lockdep" refers to the lock dependency infrastructure and the additional argument will have type struct lock_class_key *?
Amit, do you want me to add your Signed-off-by to my patch since your patch was posted first on the linux-usb mailing list?
Thanks,
Bart.
Hi,
Quoted 9 lines · Bart Van Assche
Quoted 8 lines · Andy Shevchenko
To be clear, something like
#define
mutex_init_with_lockdep(...)
do {
...
__mutex_init();
} while (0)
Quoted 9 lines
Quoted 1 line
in the
mutex.h.How about using the name "
mutex_init_with_key()" since the name "lockdep" refers to the lock dependency infrastructure and the additional argument will have type structlock_class_key*?Amit, do you want me to add your Signed-off-by to my patch since your patch was posted first on the linux-usb mailing list?
Yes, thank you :) .
Also, thanks for the clarification on my previous questions Bart and for your suggestions Andy!
-
Amit
Quoted 4 lines
Thanks,
Bart.
Quoted 9 lines · Bart Van Assche
Quoted 8 lines · Andy Shevchenko
To be clear, something like
#define
mutex_init_with_lockdep(...)do { ... __mutex_init(); } while (0)
Quoted 5 lines
Quoted 1 line
in the
mutex.h.How about using the name "
mutex_init_with_key()" since the name "lockdep" refers to the lock dependency infrastructure and the additional argument will have type structlock_class_key*?
LGTM, go for it!
Signature · 2 lines
With Best Regards, Andy Shevchenko