1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Mutexes: blocking mutual exclusion locks
4 *
5 * started by Ingo Molnar:
6 *
7 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 *
9 * This file contains the main data structure and API definitions.
10 */
11 #ifndef __LINUX_MUTEX_H
12 #define __LINUX_MUTEX_H
13
14 #include <asm/current.h>
15 #include <linux/list.h>
16 #include <linux/spinlock_types.h>
17 #include <linux/lockdep.h>
18 #include <linux/atomic.h>
19 #include <asm/processor.h>
20 #include <linux/osq_lock.h>
21 #include <linux/debug_locks.h>
22 #include <linux/cleanup.h>
23 #include <linux/android_vendor.h>
24
25 struct device;
26
27 #ifdef CONFIG_DEBUG_LOCK_ALLOC
28 # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
29 , .dep_map = { \
30 .name = #lockname, \
31 .wait_type_inner = LD_WAIT_SLEEP, \
32 }
33 #else
34 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
35 #endif
36
37 #ifndef CONFIG_PREEMPT_RT
38
39 /*
40 * Simple, straightforward mutexes with strict semantics:
41 *
42 * - only one task can hold the mutex at a time
43 * - only the owner can unlock the mutex
44 * - multiple unlocks are not permitted
45 * - recursive locking is not permitted
46 * - a mutex object must be initialized via the API
47 * - a mutex object must not be initialized via memset or copying
48 * - task may not exit with mutex held
49 * - memory areas where held locks reside must not be freed
50 * - held mutexes must not be reinitialized
51 * - mutexes may not be used in hardware or software interrupt
52 * contexts such as tasklets and timers
53 *
54 * These semantics are fully enforced when DEBUG_MUTEXES is
55 * enabled. Furthermore, besides enforcing the above rules, the mutex
56 * debugging code also implements a number of additional features
57 * that make lock debugging easier and faster:
58 *
59 * - uses symbolic names of mutexes, whenever they are printed in debug output
60 * - point-of-acquire tracking, symbolic lookup of function names
61 * - list of all locks held in the system, printout of them
62 * - owner tracking
63 * - detects self-recursing locks and prints out all relevant info
64 * - detects multi-task circular deadlocks and prints out all affected
65 * locks and tasks (and only those tasks)
66 */
67 struct mutex {
68 atomic_long_t owner;
69 raw_spinlock_t wait_lock;
70 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
71 struct optimistic_spin_queue osq; /* Spinner MCS lock */
72 #endif
73 struct list_head wait_list;
74 #ifdef CONFIG_DEBUG_MUTEXES
75 void *magic;
76 #endif
77 #ifdef CONFIG_DEBUG_LOCK_ALLOC
78 struct lockdep_map dep_map;
79 #endif
80 ANDROID_OEM_DATA_ARRAY(1, 2);
81 };
82
83 #ifdef CONFIG_DEBUG_MUTEXES
84
85 #define __DEBUG_MUTEX_INITIALIZER(lockname) \
86 , .magic = &lockname
87
88 extern void mutex_destroy(struct mutex *lock);
89
90 #else
91
92 # define __DEBUG_MUTEX_INITIALIZER(lockname)
93
mutex_destroy(struct mutex * lock)94 static inline void mutex_destroy(struct mutex *lock) {}
95
96 #endif
97
98 /**
99 * mutex_init - initialize the mutex
100 * @mutex: the mutex to be initialized
101 *
102 * Initialize the mutex to unlocked state.
103 *
104 * It is not allowed to initialize an already locked mutex.
105 */
106 #define mutex_init(mutex) \
107 do { \
108 static struct lock_class_key __key; \
109 \
110 __mutex_init((mutex), #mutex, &__key); \
111 } while (0)
112
113 #define __MUTEX_INITIALIZER(lockname) \
114 { .owner = ATOMIC_LONG_INIT(0) \
115 , .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
116 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
117 __DEBUG_MUTEX_INITIALIZER(lockname) \
118 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
119
120 #define DEFINE_MUTEX(mutexname) \
121 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
122
123 extern void __mutex_init(struct mutex *lock, const char *name,
124 struct lock_class_key *key);
125
126 /**
127 * mutex_is_locked - is the mutex locked
128 * @lock: the mutex to be queried
129 *
130 * Returns true if the mutex is locked, false if unlocked.
131 */
132 extern bool mutex_is_locked(struct mutex *lock);
133
134 #else /* !CONFIG_PREEMPT_RT */
135 /*
136 * Preempt-RT variant based on rtmutexes.
137 */
138 #include <linux/rtmutex.h>
139
140 struct mutex {
141 struct rt_mutex_base rtmutex;
142 #ifdef CONFIG_DEBUG_LOCK_ALLOC
143 struct lockdep_map dep_map;
144 #endif
145 };
146
147 #define __MUTEX_INITIALIZER(mutexname) \
148 { \
149 .rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex) \
150 __DEP_MAP_MUTEX_INITIALIZER(mutexname) \
151 }
152
153 #define DEFINE_MUTEX(mutexname) \
154 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
155
156 extern void __mutex_rt_init(struct mutex *lock, const char *name,
157 struct lock_class_key *key);
158 extern int mutex_trylock(struct mutex *lock);
159
mutex_destroy(struct mutex * lock)160 static inline void mutex_destroy(struct mutex *lock) { }
161
162 #define mutex_is_locked(l) rt_mutex_base_is_locked(&(l)->rtmutex)
163
164 #define __mutex_init(mutex, name, key) \
165 do { \
166 rt_mutex_base_init(&(mutex)->rtmutex); \
167 __mutex_rt_init((mutex), name, key); \
168 } while (0)
169
170 #define mutex_init(mutex) \
171 do { \
172 static struct lock_class_key __key; \
173 \
174 __mutex_init((mutex), #mutex, &__key); \
175 } while (0)
176 #endif /* CONFIG_PREEMPT_RT */
177
178 #ifdef CONFIG_DEBUG_MUTEXES
179
180 int __devm_mutex_init(struct device *dev, struct mutex *lock);
181
182 #else
183
__devm_mutex_init(struct device * dev,struct mutex * lock)184 static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
185 {
186 /*
187 * When CONFIG_DEBUG_MUTEXES is off mutex_destroy() is just a nop so
188 * no really need to register it in the devm subsystem.
189 */
190 return 0;
191 }
192
193 #endif
194
195 #define devm_mutex_init(dev, mutex) \
196 ({ \
197 typeof(mutex) mutex_ = (mutex); \
198 \
199 mutex_init(mutex_); \
200 __devm_mutex_init(dev, mutex_); \
201 })
202
203 /*
204 * See kernel/locking/mutex.c for detailed documentation of these APIs.
205 * Also see Documentation/locking/mutex-design.rst.
206 */
207 #ifdef CONFIG_DEBUG_LOCK_ALLOC
208 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
209 extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
210
211 extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
212 unsigned int subclass);
213 extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
214 unsigned int subclass);
215 extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
216
217 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
218 #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
219 #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
220 #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
221
222 #define mutex_lock_nest_lock(lock, nest_lock) \
223 do { \
224 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
225 _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
226 } while (0)
227
228 #else
229 extern void mutex_lock(struct mutex *lock);
230 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
231 extern int __must_check mutex_lock_killable(struct mutex *lock);
232 extern void mutex_lock_io(struct mutex *lock);
233
234 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
235 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
236 # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
237 # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
238 # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
239 #endif
240
241 /*
242 * NOTE: mutex_trylock() follows the spin_trylock() convention,
243 * not the down_trylock() convention!
244 *
245 * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
246 */
247 extern int mutex_trylock(struct mutex *lock);
248 extern void mutex_unlock(struct mutex *lock);
249
250 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
251
252 DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
253 DEFINE_FREE(mutex, struct mutex *, if (_T) mutex_unlock(_T))
254
255 #endif /* __LINUX_MUTEX_H */
256