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

Configure Feed

Select the types of activity you want to include in your feed.

locking/mutex: Redo __mutex_init() to reduce generated code size

mutex_init() invokes __mutex_init() providing the name of the lock and
a pointer to a the lock class. With LOCKDEP enabled this information is
useful but without LOCKDEP it not used at all. Passing the pointer
information of the lock class might be considered negligible but the
name of the lock is passed as well and the string is stored. This
information is wasting storage.

Split __mutex_init() into a _genereic() variant doing the initialisation
of the lock and a _lockdep() version which does _genereic() plus the
lockdep bits. Restrict the lockdep version to lockdep enabled builds
allowing the compiler to remove the unused parameter.

This results in the following size reduction:

text data bss dec filename
| 30237599 8161430 1176624 39575653 vmlinux.defconfig
| 30233269 8149142 1176560 39558971 vmlinux.defconfig.patched
-4.2KiB -12KiB

| 32455099 8471098 12934684 53860881 vmlinux.defconfig.lockdep
| 32455100 8471098 12934684 53860882 vmlinux.defconfig.patched.lockdep

| 27152407 7191822 2068040 36412269 vmlinux.defconfig.preempt_rt
| 27145937 7183630 2067976 36397543 vmlinux.defconfig.patched.preempt_rt
-6.3KiB -8KiB

| 29382020 7505742 13784608 50672370 vmlinux.defconfig.preempt_rt.lockdep
| 29376229 7505742 13784544 50666515 vmlinux.defconfig.patched.preempt_rt.lockdep
-5.6KiB

[peterz: folded fix from boqun]

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Waiman Long <longman@redhat.com>
Link: https://lkml.kernel.org/r/20251125145425.68319-1-boqun.feng@gmail.com
Link: https://patch.msgid.link/20251105142350.Tfeevs2N@linutronix.de

authored by

Sebastian Andrzej Siewior and committed by
Ingo Molnar
51d7a054 f74cf399

+75 -32
+35 -10
include/linux/mutex.h
··· 86 86 #define DEFINE_MUTEX(mutexname) \ 87 87 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname) 88 88 89 - extern void __mutex_init(struct mutex *lock, const char *name, 90 - struct lock_class_key *key); 89 + #ifdef CONFIG_DEBUG_LOCK_ALLOC 90 + void mutex_init_lockep(struct mutex *lock, const char *name, struct lock_class_key *key); 91 + 92 + static inline void __mutex_init(struct mutex *lock, const char *name, 93 + struct lock_class_key *key) 94 + { 95 + mutex_init_lockep(lock, name, key); 96 + } 97 + #else 98 + extern void mutex_init_generic(struct mutex *lock); 99 + 100 + static inline void __mutex_init(struct mutex *lock, const char *name, 101 + struct lock_class_key *key) 102 + { 103 + mutex_init_generic(lock); 104 + } 105 + #endif /* !CONFIG_DEBUG_LOCK_ALLOC */ 91 106 92 107 /** 93 108 * mutex_is_locked - is the mutex locked ··· 126 111 #define DEFINE_MUTEX(mutexname) \ 127 112 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname) 128 113 129 - extern void __mutex_rt_init(struct mutex *lock, const char *name, 130 - struct lock_class_key *key); 131 - 132 114 #define mutex_is_locked(l) rt_mutex_base_is_locked(&(l)->rtmutex) 133 115 134 - #define __mutex_init(mutex, name, key) \ 135 - do { \ 136 - rt_mutex_base_init(&(mutex)->rtmutex); \ 137 - __mutex_rt_init((mutex), name, key); \ 138 - } while (0) 116 + #ifdef CONFIG_DEBUG_LOCK_ALLOC 117 + extern void mutex_rt_init_lockdep(struct mutex *mutex, const char *name, 118 + struct lock_class_key *key); 139 119 120 + static inline void __mutex_init(struct mutex *lock, const char *name, 121 + struct lock_class_key *key) 122 + { 123 + mutex_rt_init_lockdep(lock, name, key); 124 + } 125 + 126 + #else 127 + extern void mutex_rt_init_generic(struct mutex *mutex); 128 + 129 + static inline void __mutex_init(struct mutex *lock, const char *name, 130 + struct lock_class_key *key) 131 + { 132 + mutex_rt_init_generic(lock); 133 + } 134 + #endif /* !CONFIG_LOCKDEP */ 140 135 #endif /* CONFIG_PREEMPT_RT */ 141 136 142 137 #ifdef CONFIG_DEBUG_MUTEXES
+1 -9
kernel/locking/mutex-debug.c
··· 78 78 } 79 79 } 80 80 81 - void debug_mutex_init(struct mutex *lock, const char *name, 82 - struct lock_class_key *key) 81 + void debug_mutex_init(struct mutex *lock) 83 82 { 84 - #ifdef CONFIG_DEBUG_LOCK_ALLOC 85 - /* 86 - * Make sure we are not reinitializing a held lock: 87 - */ 88 - debug_check_no_locks_freed((void *)lock, sizeof(*lock)); 89 - lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP); 90 - #endif 91 83 lock->magic = lock; 92 84 } 93 85
+22 -6
kernel/locking/mutex.c
··· 43 43 # define MUTEX_WARN_ON(cond) 44 44 #endif 45 45 46 - void 47 - __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) 46 + static void __mutex_init_generic(struct mutex *lock) 48 47 { 49 48 atomic_long_set(&lock->owner, 0); 50 49 raw_spin_lock_init(&lock->wait_lock); ··· 51 52 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 52 53 osq_lock_init(&lock->osq); 53 54 #endif 54 - 55 - debug_mutex_init(lock, name, key); 55 + debug_mutex_init(lock); 56 56 } 57 - EXPORT_SYMBOL(__mutex_init); 58 57 59 58 static inline struct task_struct *__owner_task(unsigned long owner) 60 59 { ··· 139 142 * There is nothing that would stop spreading the lockdep annotations outwards 140 143 * except more code. 141 144 */ 145 + void mutex_init_generic(struct mutex *lock) 146 + { 147 + __mutex_init_generic(lock); 148 + } 149 + EXPORT_SYMBOL(mutex_init_generic); 142 150 143 151 /* 144 152 * Optimistic trylock that only works in the uncontended case. Make sure to ··· 168 166 169 167 return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL); 170 168 } 171 - #endif 169 + 170 + #else /* !CONFIG_DEBUG_LOCK_ALLOC */ 171 + 172 + void mutex_init_lockep(struct mutex *lock, const char *name, struct lock_class_key *key) 173 + { 174 + __mutex_init_generic(lock); 175 + 176 + /* 177 + * Make sure we are not reinitializing a held lock: 178 + */ 179 + debug_check_no_locks_freed((void *)lock, sizeof(*lock)); 180 + lockdep_init_map_wait(&lock->dep_map, name, key, 0, LD_WAIT_SLEEP); 181 + } 182 + EXPORT_SYMBOL(mutex_init_lockep); 183 + #endif /* !CONFIG_DEBUG_LOCK_ALLOC */ 172 184 173 185 static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag) 174 186 {
+2 -3
kernel/locking/mutex.h
··· 59 59 extern void debug_mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter, 60 60 struct task_struct *task); 61 61 extern void debug_mutex_unlock(struct mutex *lock); 62 - extern void debug_mutex_init(struct mutex *lock, const char *name, 63 - struct lock_class_key *key); 62 + extern void debug_mutex_init(struct mutex *lock); 64 63 #else /* CONFIG_DEBUG_MUTEXES */ 65 64 # define debug_mutex_lock_common(lock, waiter) do { } while (0) 66 65 # define debug_mutex_wake_waiter(lock, waiter) do { } while (0) ··· 67 68 # define debug_mutex_add_waiter(lock, waiter, ti) do { } while (0) 68 69 # define debug_mutex_remove_waiter(lock, waiter, ti) do { } while (0) 69 70 # define debug_mutex_unlock(lock) do { } while (0) 70 - # define debug_mutex_init(lock, name, key) do { } while (0) 71 + # define debug_mutex_init(lock) do { } while (0) 71 72 #endif /* !CONFIG_DEBUG_MUTEXES */ 72 73 #endif /* CONFIG_PREEMPT_RT */
+15 -4
kernel/locking/rtmutex_api.c
··· 515 515 516 516 #ifdef CONFIG_PREEMPT_RT 517 517 /* Mutexes */ 518 - void __mutex_rt_init(struct mutex *mutex, const char *name, 519 - struct lock_class_key *key) 518 + static void __mutex_rt_init_generic(struct mutex *mutex) 520 519 { 520 + rt_mutex_base_init(&mutex->rtmutex); 521 521 debug_check_no_locks_freed((void *)mutex, sizeof(*mutex)); 522 - lockdep_init_map_wait(&mutex->dep_map, name, key, 0, LD_WAIT_SLEEP); 523 522 } 524 - EXPORT_SYMBOL(__mutex_rt_init); 525 523 526 524 static __always_inline int __mutex_lock_common(struct mutex *lock, 527 525 unsigned int state, ··· 540 542 } 541 543 542 544 #ifdef CONFIG_DEBUG_LOCK_ALLOC 545 + void mutex_rt_init_lockdep(struct mutex *mutex, const char *name, struct lock_class_key *key) 546 + { 547 + __mutex_rt_init_generic(mutex); 548 + lockdep_init_map_wait(&mutex->dep_map, name, key, 0, LD_WAIT_SLEEP); 549 + } 550 + EXPORT_SYMBOL(mutex_rt_init_lockdep); 551 + 543 552 void __sched mutex_lock_nested(struct mutex *lock, unsigned int subclass) 544 553 { 545 554 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_); ··· 602 597 } 603 598 EXPORT_SYMBOL_GPL(_mutex_trylock_nest_lock); 604 599 #else /* CONFIG_DEBUG_LOCK_ALLOC */ 600 + 601 + void mutex_rt_init_generic(struct mutex *mutex) 602 + { 603 + __mutex_rt_init_generic(mutex); 604 + } 605 + EXPORT_SYMBOL(mutex_rt_init_generic); 605 606 606 607 void __sched mutex_lock(struct mutex *lock) 607 608 {