* [PATCH v3 0/3] Improve a lockdep complaint suppression approach
@ 2024-09-12 22:39 Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 1/3] locking/mutex: Define mutex_init() once Bart Van Assche
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Bart Van Assche @ 2024-09-12 22:39 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: linux-usb, Thomas Gleixner, Peter Zijlstra, Ingo Molnar,
Andy Shevchenko, Amit Sunil Dhamne, Bart Van Assche
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(-)
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3 1/3] locking/mutex: Define mutex_init() once
2024-09-12 22:39 [PATCH v3 0/3] Improve a lockdep complaint suppression approach Bart Van Assche
@ 2024-09-12 22:39 ` Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 2/3] locking/mutex: Introduce mutex_init_with_key() Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 3/3] usb: roles: Improve the fix for a false positive recursive locking complaint Bart Van Assche
2 siblings, 0 replies; 4+ messages in thread
From: Bart Van Assche @ 2024-09-12 22:39 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: linux-usb, Thomas Gleixner, Peter Zijlstra, Ingo Molnar,
Andy Shevchenko, Amit Sunil Dhamne, Bart Van Assche
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").
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
include/linux/mutex.h | 8 +-------
1 file changed, 1 insertion(+), 7 deletions(-)
diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index a561c629d89f..ef617089db19 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -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
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v3 2/3] locking/mutex: Introduce mutex_init_with_key()
2024-09-12 22:39 [PATCH v3 0/3] Improve a lockdep complaint suppression approach Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 1/3] locking/mutex: Define mutex_init() once Bart Van Assche
@ 2024-09-12 22:39 ` Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 3/3] usb: roles: Improve the fix for a false positive recursive locking complaint Bart Van Assche
2 siblings, 0 replies; 4+ messages in thread
From: Bart Van Assche @ 2024-09-12 22:39 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: linux-usb, Thomas Gleixner, Peter Zijlstra, Ingo Molnar,
Andy Shevchenko, Amit Sunil Dhamne, 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.
Suggested-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@kernel.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
include/linux/mutex.h | 11 +++++++++++
1 file changed, 11 insertions(+)
diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index ef617089db19..2bf91b57591b 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -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) \
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v3 3/3] usb: roles: Improve the fix for a false positive recursive locking complaint
2024-09-12 22:39 [PATCH v3 0/3] Improve a lockdep complaint suppression approach Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 1/3] locking/mutex: Define mutex_init() once Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 2/3] locking/mutex: Introduce mutex_init_with_key() Bart Van Assche
@ 2024-09-12 22:39 ` Bart Van Assche
2 siblings, 0 replies; 4+ messages in thread
From: Bart Van Assche @ 2024-09-12 22:39 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: linux-usb, Thomas Gleixner, Peter Zijlstra, Ingo Molnar,
Andy Shevchenko, Amit Sunil Dhamne, Bart Van Assche,
Badhri Jagan Sridharan, Hans de Goede, Heikki Krogerus
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 <amitsd@google.com>
Cc: Badhri Jagan Sridharan <badhri@google.com>
Cc: Hans de Goede <hdegoede@redhat.com>
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
drivers/usb/roles/class.c | 14 +++++---------
1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
index 7aca1ef7f44c..c58a12c147f4 100644
--- a/drivers/usb/roles/class.c
+++ b/drivers/usb/roles/class.c
@@ -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);
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-09-12 22:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-12 22:39 [PATCH v3 0/3] Improve a lockdep complaint suppression approach Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 1/3] locking/mutex: Define mutex_init() once Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 2/3] locking/mutex: Introduce mutex_init_with_key() Bart Van Assche
2024-09-12 22:39 ` [PATCH v3 3/3] usb: roles: Improve the fix for a false positive recursive locking complaint Bart Van Assche
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).