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
23 struct ww_acquire_ctx;
24
25 /*
26 * Simple, straightforward mutexes with strict semantics:
27 *
28 * - only one task can hold the mutex at a time
29 * - only the owner can unlock the mutex
30 * - multiple unlocks are not permitted
31 * - recursive locking is not permitted
32 * - a mutex object must be initialized via the API
33 * - a mutex object must not be initialized via memset or copying
34 * - task may not exit with mutex held
35 * - memory areas where held locks reside must not be freed
36 * - held mutexes must not be reinitialized
37 * - mutexes may not be used in hardware or software interrupt
38 * contexts such as tasklets and timers
39 *
40 * These semantics are fully enforced when DEBUG_MUTEXES is
41 * enabled. Furthermore, besides enforcing the above rules, the mutex
42 * debugging code also implements a number of additional features
43 * that make lock debugging easier and faster:
44 *
45 * - uses symbolic names of mutexes, whenever they are printed in debug output
46 * - point-of-acquire tracking, symbolic lookup of function names
47 * - list of all locks held in the system, printout of them
48 * - owner tracking
49 * - detects self-recursing locks and prints out all relevant info
50 * - detects multi-task circular deadlocks and prints out all affected
51 * locks and tasks (and only those tasks)
52 */
53 struct mutex {
54 atomic_long_t owner;
55 spinlock_t wait_lock;
56 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
57 struct optimistic_spin_queue osq; /* Spinner MCS lock */
58 #endif
59 struct list_head wait_list;
60 #ifdef CONFIG_DEBUG_MUTEXES
61 void *magic;
62 #endif
63 #ifdef CONFIG_DEBUG_LOCK_ALLOC
64 struct lockdep_map dep_map;
65 #endif
66 };
67
68 /*
69 * Internal helper function; C doesn't allow us to hide it :/
70 *
71 * DO NOT USE (outside of mutex code).
72 */
__mutex_owner(struct mutex * lock)73 static inline struct task_struct *__mutex_owner(struct mutex *lock)
74 {
75 return (struct task_struct *)(atomic_long_read(&lock->owner) & ~0x07);
76 }
77
78 /*
79 * This is the control structure for tasks blocked on mutex,
80 * which resides on the blocked task's kernel stack:
81 */
82 struct mutex_waiter {
83 struct list_head list;
84 struct task_struct *task;
85 struct ww_acquire_ctx *ww_ctx;
86 #ifdef CONFIG_DEBUG_MUTEXES
87 void *magic;
88 #endif
89 };
90
91 #ifdef CONFIG_DEBUG_MUTEXES
92
93 #define __DEBUG_MUTEX_INITIALIZER(lockname) \
94 , .magic = &lockname
95
96 extern void mutex_destroy(struct mutex *lock);
97
98 #else
99
100 # define __DEBUG_MUTEX_INITIALIZER(lockname)
101
mutex_destroy(struct mutex * lock)102 static inline void mutex_destroy(struct mutex *lock) {}
103
104 #endif
105
106 /**
107 * mutex_init - initialize the mutex
108 * @mutex: the mutex to be initialized
109 *
110 * Initialize the mutex to unlocked state.
111 *
112 * It is not allowed to initialize an already locked mutex.
113 */
114 #define mutex_init(mutex) \
115 do { \
116 static struct lock_class_key __key; \
117 \
118 __mutex_init((mutex), #mutex, &__key); \
119 } while (0)
120
121 #ifdef CONFIG_DEBUG_LOCK_ALLOC
122 # define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
123 , .dep_map = { .name = #lockname }
124 #else
125 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
126 #endif
127
128 #define __MUTEX_INITIALIZER(lockname) \
129 { .owner = ATOMIC_LONG_INIT(0) \
130 , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
131 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
132 __DEBUG_MUTEX_INITIALIZER(lockname) \
133 __DEP_MAP_MUTEX_INITIALIZER(lockname) }
134
135 #define DEFINE_MUTEX(mutexname) \
136 struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
137
138 extern void __mutex_init(struct mutex *lock, const char *name,
139 struct lock_class_key *key);
140
141 /**
142 * mutex_is_locked - is the mutex locked
143 * @lock: the mutex to be queried
144 *
145 * Returns true if the mutex is locked, false if unlocked.
146 */
mutex_is_locked(struct mutex * lock)147 static inline bool mutex_is_locked(struct mutex *lock)
148 {
149 return __mutex_owner(lock) != NULL;
150 }
151
152 /*
153 * See kernel/locking/mutex.c for detailed documentation of these APIs.
154 * Also see Documentation/locking/mutex-design.txt.
155 */
156 #ifdef CONFIG_DEBUG_LOCK_ALLOC
157 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
158 extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
159
160 extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
161 unsigned int subclass);
162 extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
163 unsigned int subclass);
164 extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
165
166 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
167 #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
168 #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
169 #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
170
171 #define mutex_lock_nest_lock(lock, nest_lock) \
172 do { \
173 typecheck(struct lockdep_map *, &(nest_lock)->dep_map); \
174 _mutex_lock_nest_lock(lock, &(nest_lock)->dep_map); \
175 } while (0)
176
177 #else
178 extern void mutex_lock(struct mutex *lock);
179 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
180 extern int __must_check mutex_lock_killable(struct mutex *lock);
181 extern void mutex_lock_io(struct mutex *lock);
182
183 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
184 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
185 # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
186 # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
187 # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
188 #endif
189
190 /*
191 * NOTE: mutex_trylock() follows the spin_trylock() convention,
192 * not the down_trylock() convention!
193 *
194 * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
195 */
196 extern int mutex_trylock(struct mutex *lock);
197 extern void mutex_unlock(struct mutex *lock);
198
199 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
200
201 /*
202 * These values are chosen such that FAIL and SUCCESS match the
203 * values of the regular mutex_trylock().
204 */
205 enum mutex_trylock_recursive_enum {
206 MUTEX_TRYLOCK_FAILED = 0,
207 MUTEX_TRYLOCK_SUCCESS = 1,
208 MUTEX_TRYLOCK_RECURSIVE,
209 };
210
211 /**
212 * mutex_trylock_recursive - trylock variant that allows recursive locking
213 * @lock: mutex to be locked
214 *
215 * This function should not be used, _ever_. It is purely for hysterical GEM
216 * raisins, and once those are gone this will be removed.
217 *
218 * Returns:
219 * - MUTEX_TRYLOCK_FAILED - trylock failed,
220 * - MUTEX_TRYLOCK_SUCCESS - lock acquired,
221 * - MUTEX_TRYLOCK_RECURSIVE - we already owned the lock.
222 */
223 static inline /* __deprecated */ __must_check enum mutex_trylock_recursive_enum
mutex_trylock_recursive(struct mutex * lock)224 mutex_trylock_recursive(struct mutex *lock)
225 {
226 if (unlikely(__mutex_owner(lock) == current))
227 return MUTEX_TRYLOCK_RECURSIVE;
228
229 return mutex_trylock(lock);
230 }
231
232 #endif /* __LINUX_MUTEX_H */
233