1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Wound/Wait Mutexes: blocking mutual exclusion locks with deadlock avoidance
5 * Original mutex implementation started by Ingo Molnar:
9 * Wait/Die implementation:
10 * Copyright (C) 2013 Canonical Ltd.
11 * Choice of algorithm:
12 * Copyright (C) 2018 WMWare Inc.
14 * This file contains the main data structure and API definitions.
17 #ifndef __LINUX_WW_MUTEX_H
18 #define __LINUX_WW_MUTEX_H
20 #include <linux/mutex.h>
24 struct lock_class_key acquire_key;
25 struct lock_class_key mutex_key;
26 const char *acquire_name;
27 const char *mutex_name;
28 unsigned int is_wait_die;
31 struct ww_acquire_ctx {
32 struct task_struct *task;
34 unsigned int acquired;
35 unsigned short wounded;
36 unsigned short is_wait_die;
37 #ifdef CONFIG_DEBUG_MUTEXES
38 unsigned int done_acquire;
39 struct ww_class *ww_class;
40 struct ww_mutex *contending_lock;
42 #ifdef CONFIG_DEBUG_LOCK_ALLOC
43 struct lockdep_map dep_map;
45 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
46 unsigned int deadlock_inject_interval;
47 unsigned int deadlock_inject_countdown;
51 #define __WW_CLASS_INITIALIZER(ww_class, _is_wait_die) \
52 { .stamp = ATOMIC_LONG_INIT(0) \
53 , .acquire_name = #ww_class "_acquire" \
54 , .mutex_name = #ww_class "_mutex" \
55 , .is_wait_die = _is_wait_die }
57 #define DEFINE_WD_CLASS(classname) \
58 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 1)
60 #define DEFINE_WW_CLASS(classname) \
61 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 0)
64 * ww_mutex_init - initialize the w/w mutex
65 * @lock: the mutex to be initialized
66 * @ww_class: the w/w class the mutex should belong to
68 * Initialize the w/w mutex to unlocked state and associate it with the given
69 * class. Static define macro for w/w mutex is not provided and this function
70 * is the only way to properly initialize the w/w mutex.
72 * It is not allowed to initialize an already locked mutex.
74 static inline void ww_mutex_init(struct ww_mutex *lock,
75 struct ww_class *ww_class)
77 __mutex_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
79 #ifdef CONFIG_DEBUG_MUTEXES
80 lock->ww_class = ww_class;
85 * ww_acquire_init - initialize a w/w acquire context
86 * @ctx: w/w acquire context to initialize
87 * @ww_class: w/w class of the context
89 * Initializes an context to acquire multiple mutexes of the given w/w class.
91 * Context-based w/w mutex acquiring can be done in any order whatsoever within
92 * a given lock class. Deadlocks will be detected and handled with the
95 * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
96 * result in undetected deadlocks and is so forbidden. Mixing different contexts
97 * for the same w/w class when acquiring mutexes can also result in undetected
98 * deadlocks, and is hence also forbidden. Both types of abuse will be caught by
99 * enabling CONFIG_PROVE_LOCKING.
101 * Nesting of acquire contexts for _different_ w/w classes is possible, subject
102 * to the usual locking rules between different lock classes.
104 * An acquire context must be released with ww_acquire_fini by the same task
105 * before the memory is freed. It is recommended to allocate the context itself
108 static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
109 struct ww_class *ww_class)
112 ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp);
114 ctx->wounded = false;
115 ctx->is_wait_die = ww_class->is_wait_die;
116 #ifdef CONFIG_DEBUG_MUTEXES
117 ctx->ww_class = ww_class;
118 ctx->done_acquire = 0;
119 ctx->contending_lock = NULL;
121 #ifdef CONFIG_DEBUG_LOCK_ALLOC
122 debug_check_no_locks_freed((void *)ctx, sizeof(*ctx));
123 lockdep_init_map(&ctx->dep_map, ww_class->acquire_name,
124 &ww_class->acquire_key, 0);
125 mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_);
127 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
128 ctx->deadlock_inject_interval = 1;
129 ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
134 * ww_acquire_done - marks the end of the acquire phase
135 * @ctx: the acquire context
137 * Marks the end of the acquire phase, any further w/w mutex lock calls using
138 * this context are forbidden.
140 * Calling this function is optional, it is just useful to document w/w mutex
141 * code and clearly designated the acquire phase from actually using the locked
144 static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
146 #ifdef CONFIG_DEBUG_MUTEXES
147 lockdep_assert_held(ctx);
149 DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
150 ctx->done_acquire = 1;
155 * ww_acquire_fini - releases a w/w acquire context
156 * @ctx: the acquire context to free
158 * Releases a w/w acquire context. This must be called _after_ all acquired w/w
159 * mutexes have been released with ww_mutex_unlock.
161 static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
163 #ifdef CONFIG_DEBUG_LOCK_ALLOC
164 mutex_release(&ctx->dep_map, _THIS_IP_);
166 #ifdef CONFIG_DEBUG_MUTEXES
167 DEBUG_LOCKS_WARN_ON(ctx->acquired);
168 if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
170 * lockdep will normally handle this,
171 * but fail without anyway
173 ctx->done_acquire = 1;
175 if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC))
176 /* ensure ww_acquire_fini will still fail if called twice */
182 * ww_mutex_lock - acquire the w/w mutex
183 * @lock: the mutex to be acquired
184 * @ctx: w/w acquire context, or NULL to acquire only a single lock.
186 * Lock the w/w mutex exclusively for this task.
188 * Deadlocks within a given w/w class of locks are detected and handled with the
189 * wait/die algorithm. If the lock isn't immediately available this function
190 * will either sleep until it is (wait case). Or it selects the current context
191 * for backing off by returning -EDEADLK (die case). Trying to acquire the
192 * same lock with the same context twice is also detected and signalled by
193 * returning -EALREADY. Returns 0 if the mutex was successfully acquired.
195 * In the die case the caller must release all currently held w/w mutexes for
196 * the given context and then wait for this contending lock to be available by
197 * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
198 * lock and proceed with trying to acquire further w/w mutexes (e.g. when
199 * scanning through lru lists trying to free resources).
201 * The mutex must later on be released by the same task that
202 * acquired it. The task may not exit without first unlocking the mutex. Also,
203 * kernel memory where the mutex resides must not be freed with the mutex still
204 * locked. The mutex must first be initialized (or statically defined) before it
205 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
206 * of the same w/w lock class as was used to initialize the acquire context.
208 * A mutex acquired with this function must be released with ww_mutex_unlock.
210 extern int /* __must_check */ ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx);
213 * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
214 * @lock: the mutex to be acquired
215 * @ctx: w/w acquire context
217 * Lock the w/w mutex exclusively for this task.
219 * Deadlocks within a given w/w class of locks are detected and handled with the
220 * wait/die algorithm. If the lock isn't immediately available this function
221 * will either sleep until it is (wait case). Or it selects the current context
222 * for backing off by returning -EDEADLK (die case). Trying to acquire the
223 * same lock with the same context twice is also detected and signalled by
224 * returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a
225 * signal arrives while waiting for the lock then this function returns -EINTR.
227 * In the die case the caller must release all currently held w/w mutexes for
228 * the given context and then wait for this contending lock to be available by
229 * calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to
230 * not acquire this lock and proceed with trying to acquire further w/w mutexes
231 * (e.g. when scanning through lru lists trying to free resources).
233 * The mutex must later on be released by the same task that
234 * acquired it. The task may not exit without first unlocking the mutex. Also,
235 * kernel memory where the mutex resides must not be freed with the mutex still
236 * locked. The mutex must first be initialized (or statically defined) before it
237 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
238 * of the same w/w lock class as was used to initialize the acquire context.
240 * A mutex acquired with this function must be released with ww_mutex_unlock.
242 extern int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
243 struct ww_acquire_ctx *ctx);
246 * ww_mutex_lock_slow - slowpath acquiring of the w/w mutex
247 * @lock: the mutex to be acquired
248 * @ctx: w/w acquire context
250 * Acquires a w/w mutex with the given context after a die case. This function
251 * will sleep until the lock becomes available.
253 * The caller must have released all w/w mutexes already acquired with the
254 * context and then call this function on the contended lock.
256 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
257 * needs with ww_mutex_lock. Note that the -EALREADY return code from
258 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
260 * It is forbidden to call this function with any other w/w mutexes associated
261 * with the context held. It is forbidden to call this on anything else than the
264 * Note that the slowpath lock acquiring can also be done by calling
265 * ww_mutex_lock directly. This function here is simply to help w/w mutex
266 * locking code readability by clearly denoting the slowpath.
269 ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
272 #ifdef CONFIG_DEBUG_MUTEXES
273 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
275 ret = ww_mutex_lock(lock, ctx);
280 * ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex, interruptible
281 * @lock: the mutex to be acquired
282 * @ctx: w/w acquire context
284 * Acquires a w/w mutex with the given context after a die case. This function
285 * will sleep until the lock becomes available and returns 0 when the lock has
286 * been acquired. If a signal arrives while waiting for the lock then this
287 * function returns -EINTR.
289 * The caller must have released all w/w mutexes already acquired with the
290 * context and then call this function on the contended lock.
292 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
293 * needs with ww_mutex_lock. Note that the -EALREADY return code from
294 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
296 * It is forbidden to call this function with any other w/w mutexes associated
297 * with the given context held. It is forbidden to call this on anything else
298 * than the contending mutex.
300 * Note that the slowpath lock acquiring can also be done by calling
301 * ww_mutex_lock_interruptible directly. This function here is simply to help
302 * w/w mutex locking code readability by clearly denoting the slowpath.
304 static inline int __must_check
305 ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
306 struct ww_acquire_ctx *ctx)
308 #ifdef CONFIG_DEBUG_MUTEXES
309 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
311 return ww_mutex_lock_interruptible(lock, ctx);
314 extern void ww_mutex_unlock(struct ww_mutex *lock);
317 * ww_mutex_trylock - tries to acquire the w/w mutex without acquire context
318 * @lock: mutex to lock
320 * Trylocks a mutex without acquire context, so no deadlock detection is
321 * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
323 static inline int __must_check ww_mutex_trylock(struct ww_mutex *lock)
325 return mutex_trylock(&lock->base);
329 * ww_mutex_destroy - mark a w/w mutex unusable
330 * @lock: the mutex to be destroyed
332 * This function marks the mutex uninitialized, and any subsequent
333 * use of the mutex is forbidden. The mutex must not be locked when
334 * this function is called.
336 static inline void ww_mutex_destroy(struct ww_mutex *lock)
338 mutex_destroy(&lock->base);
342 * ww_mutex_is_locked - is the w/w mutex locked
343 * @lock: the mutex to be queried
345 * Returns 1 if the mutex is locked, 0 if unlocked.
347 static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
349 return mutex_is_locked(&lock->base);