diff mbox series

[v2,1/3] locking/mutex: Define mutex_init() once

Message ID 20240905204709.556577-2-bvanassche@acm.org (mailing list archive)
State Accepted
Commit 96f8052822e03c6f49b6b28fc1d6e5e0522ecbb9
Headers show
Series Fix a lockdep complaint related to USB role switching | expand

Commit Message

Bart Van Assche Sept. 5, 2024, 8:47 p.m. UTC
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").

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
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(-)

Comments

Andy Shevchenko Sept. 5, 2024, 8:51 p.m. UTC | #1
+ George Stark (IIRC you also tried to achieve something like this in the past)

On Thu, Sep 5, 2024 at 11:47 PM Bart Van Assche <bvanassche@acm.org> wrote:
>
> 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").
>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
> 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
diff mbox series

Patch

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