Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

locking/mutex: Introduce mutex_init_with_key()

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>
Link: https://lore.kernel.org/r/20240912223956.3554086-3-bvanassche@acm.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Bart Van Assche and committed by
Greg Kroah-Hartman
e837d833 96f80528

+11
+11
include/linux/mutex.h
··· 64 64 __mutex_init((mutex), #mutex, &__key); \ 65 65 } while (0) 66 66 67 + /** 68 + * mutex_init_with_key - initialize a mutex with a given lockdep key 69 + * @mutex: the mutex to be initialized 70 + * @key: the lockdep key to be associated with the mutex 71 + * 72 + * Initialize the mutex to the unlocked state. 73 + * 74 + * It is not allowed to initialize an already locked mutex. 75 + */ 76 + #define mutex_init_with_key(mutex, key) __mutex_init((mutex), #mutex, (key)) 77 + 67 78 #ifndef CONFIG_PREEMPT_RT 68 79 #define __MUTEX_INITIALIZER(lockname) \ 69 80 { .owner = ATOMIC_LONG_INIT(0) \