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