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:
7 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
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;
53 struct ww_acquire_ctx *ctx;
54 #ifdef CONFIG_DEBUG_MUTEXES
55 struct ww_class *ww_class;
59 #ifdef CONFIG_DEBUG_LOCK_ALLOC
60 # define __WW_CLASS_MUTEX_INITIALIZER(lockname, class) \
63 # define __WW_CLASS_MUTEX_INITIALIZER(lockname, class)
66 #define __WW_CLASS_INITIALIZER(ww_class, _is_wait_die) \
67 { .stamp = ATOMIC_LONG_INIT(0) \
68 , .acquire_name = #ww_class "_acquire" \
69 , .mutex_name = #ww_class "_mutex" \
70 , .is_wait_die = _is_wait_die }
72 #define __WW_MUTEX_INITIALIZER(lockname, class) \
73 { .base = __MUTEX_INITIALIZER(lockname.base) \
74 __WW_CLASS_MUTEX_INITIALIZER(lockname, class) }
76 #define DEFINE_WD_CLASS(classname) \
77 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 1)
79 #define DEFINE_WW_CLASS(classname) \
80 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 0)
82 #define DEFINE_WW_MUTEX(mutexname, ww_class) \
83 struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
86 * ww_mutex_init - initialize the w/w mutex
87 * @lock: the mutex to be initialized
88 * @ww_class: the w/w class the mutex should belong to
90 * Initialize the w/w mutex to unlocked state and associate it with the given
93 * It is not allowed to initialize an already locked mutex.
95 static inline void ww_mutex_init(struct ww_mutex *lock,
96 struct ww_class *ww_class)
98 __mutex_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
100 #ifdef CONFIG_DEBUG_MUTEXES
101 lock->ww_class = ww_class;
106 * ww_acquire_init - initialize a w/w acquire context
107 * @ctx: w/w acquire context to initialize
108 * @ww_class: w/w class of the context
110 * Initializes an context to acquire multiple mutexes of the given w/w class.
112 * Context-based w/w mutex acquiring can be done in any order whatsoever within
113 * a given lock class. Deadlocks will be detected and handled with the
116 * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
117 * result in undetected deadlocks and is so forbidden. Mixing different contexts
118 * for the same w/w class when acquiring mutexes can also result in undetected
119 * deadlocks, and is hence also forbidden. Both types of abuse will be caught by
120 * enabling CONFIG_PROVE_LOCKING.
122 * Nesting of acquire contexts for _different_ w/w classes is possible, subject
123 * to the usual locking rules between different lock classes.
125 * An acquire context must be released with ww_acquire_fini by the same task
126 * before the memory is freed. It is recommended to allocate the context itself
129 static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
130 struct ww_class *ww_class)
133 ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp);
135 ctx->wounded = false;
136 ctx->is_wait_die = ww_class->is_wait_die;
137 #ifdef CONFIG_DEBUG_MUTEXES
138 ctx->ww_class = ww_class;
139 ctx->done_acquire = 0;
140 ctx->contending_lock = NULL;
142 #ifdef CONFIG_DEBUG_LOCK_ALLOC
143 debug_check_no_locks_freed((void *)ctx, sizeof(*ctx));
144 lockdep_init_map(&ctx->dep_map, ww_class->acquire_name,
145 &ww_class->acquire_key, 0);
146 mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_);
148 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
149 ctx->deadlock_inject_interval = 1;
150 ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
155 * ww_acquire_done - marks the end of the acquire phase
156 * @ctx: the acquire context
158 * Marks the end of the acquire phase, any further w/w mutex lock calls using
159 * this context are forbidden.
161 * Calling this function is optional, it is just useful to document w/w mutex
162 * code and clearly designated the acquire phase from actually using the locked
165 static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
167 #ifdef CONFIG_DEBUG_MUTEXES
168 lockdep_assert_held(ctx);
170 DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
171 ctx->done_acquire = 1;
176 * ww_acquire_fini - releases a w/w acquire context
177 * @ctx: the acquire context to free
179 * Releases a w/w acquire context. This must be called _after_ all acquired w/w
180 * mutexes have been released with ww_mutex_unlock.
182 static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
184 #ifdef CONFIG_DEBUG_MUTEXES
185 mutex_release(&ctx->dep_map, 0, _THIS_IP_);
187 DEBUG_LOCKS_WARN_ON(ctx->acquired);
188 if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
190 * lockdep will normally handle this,
191 * but fail without anyway
193 ctx->done_acquire = 1;
195 if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC))
196 /* ensure ww_acquire_fini will still fail if called twice */
202 * ww_mutex_lock - acquire the w/w mutex
203 * @lock: the mutex to be acquired
204 * @ctx: w/w acquire context, or NULL to acquire only a single lock.
206 * Lock the w/w mutex exclusively for this task.
208 * Deadlocks within a given w/w class of locks are detected and handled with the
209 * wait/die algorithm. If the lock isn't immediately available this function
210 * will either sleep until it is (wait case). Or it selects the current context
211 * for backing off by returning -EDEADLK (die case). Trying to acquire the
212 * same lock with the same context twice is also detected and signalled by
213 * returning -EALREADY. Returns 0 if the mutex was successfully acquired.
215 * In the die case the caller must release all currently held w/w mutexes for
216 * the given context and then wait for this contending lock to be available by
217 * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
218 * lock and proceed with trying to acquire further w/w mutexes (e.g. when
219 * scanning through lru lists trying to free resources).
221 * The mutex must later on be released by the same task that
222 * acquired it. The task may not exit without first unlocking the mutex. Also,
223 * kernel memory where the mutex resides must not be freed with the mutex still
224 * locked. The mutex must first be initialized (or statically defined) before it
225 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
226 * of the same w/w lock class as was used to initialize the acquire context.
228 * A mutex acquired with this function must be released with ww_mutex_unlock.
230 extern int /* __must_check */ ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx);
233 * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
234 * @lock: the mutex to be acquired
235 * @ctx: w/w acquire context
237 * Lock the w/w mutex exclusively for this task.
239 * Deadlocks within a given w/w class of locks are detected and handled with the
240 * wait/die algorithm. If the lock isn't immediately available this function
241 * will either sleep until it is (wait case). Or it selects the current context
242 * for backing off by returning -EDEADLK (die case). Trying to acquire the
243 * same lock with the same context twice is also detected and signalled by
244 * returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a
245 * signal arrives while waiting for the lock then this function returns -EINTR.
247 * In the die case the caller must release all currently held w/w mutexes for
248 * the given context and then wait for this contending lock to be available by
249 * calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to
250 * not acquire this lock and proceed with trying to acquire further w/w mutexes
251 * (e.g. when scanning through lru lists trying to free resources).
253 * The mutex must later on be released by the same task that
254 * acquired it. The task may not exit without first unlocking the mutex. Also,
255 * kernel memory where the mutex resides must not be freed with the mutex still
256 * locked. The mutex must first be initialized (or statically defined) before it
257 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
258 * of the same w/w lock class as was used to initialize the acquire context.
260 * A mutex acquired with this function must be released with ww_mutex_unlock.
262 extern int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
263 struct ww_acquire_ctx *ctx);
266 * ww_mutex_lock_slow - slowpath acquiring of the w/w mutex
267 * @lock: the mutex to be acquired
268 * @ctx: w/w acquire context
270 * Acquires a w/w mutex with the given context after a die case. This function
271 * will sleep until the lock becomes available.
273 * The caller must have released all w/w mutexes already acquired with the
274 * context and then call this function on the contended lock.
276 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
277 * needs with ww_mutex_lock. Note that the -EALREADY return code from
278 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
280 * It is forbidden to call this function with any other w/w mutexes associated
281 * with the context held. It is forbidden to call this on anything else than the
284 * Note that the slowpath lock acquiring can also be done by calling
285 * ww_mutex_lock directly. This function here is simply to help w/w mutex
286 * locking code readability by clearly denoting the slowpath.
289 ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
292 #ifdef CONFIG_DEBUG_MUTEXES
293 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
295 ret = ww_mutex_lock(lock, ctx);
300 * ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex, interruptible
301 * @lock: the mutex to be acquired
302 * @ctx: w/w acquire context
304 * Acquires a w/w mutex with the given context after a die case. This function
305 * will sleep until the lock becomes available and returns 0 when the lock has
306 * been acquired. If a signal arrives while waiting for the lock then this
307 * function returns -EINTR.
309 * The caller must have released all w/w mutexes already acquired with the
310 * context and then call this function on the contended lock.
312 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
313 * needs with ww_mutex_lock. Note that the -EALREADY return code from
314 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
316 * It is forbidden to call this function with any other w/w mutexes associated
317 * with the given context held. It is forbidden to call this on anything else
318 * than the contending mutex.
320 * Note that the slowpath lock acquiring can also be done by calling
321 * ww_mutex_lock_interruptible directly. This function here is simply to help
322 * w/w mutex locking code readability by clearly denoting the slowpath.
324 static inline int __must_check
325 ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
326 struct ww_acquire_ctx *ctx)
328 #ifdef CONFIG_DEBUG_MUTEXES
329 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
331 return ww_mutex_lock_interruptible(lock, ctx);
334 extern void ww_mutex_unlock(struct ww_mutex *lock);
337 * ww_mutex_trylock - tries to acquire the w/w mutex without acquire context
338 * @lock: mutex to lock
340 * Trylocks a mutex without acquire context, so no deadlock detection is
341 * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
343 static inline int __must_check ww_mutex_trylock(struct ww_mutex *lock)
345 return mutex_trylock(&lock->base);
349 * ww_mutex_destroy - mark a w/w mutex unusable
350 * @lock: the mutex to be destroyed
352 * This function marks the mutex uninitialized, and any subsequent
353 * use of the mutex is forbidden. The mutex must not be locked when
354 * this function is called.
356 static inline void ww_mutex_destroy(struct ww_mutex *lock)
358 mutex_destroy(&lock->base);
362 * ww_mutex_is_locked - is the w/w mutex locked
363 * @lock: the mutex to be queried
365 * Returns 1 if the mutex is locked, 0 if unlocked.
367 static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
369 return mutex_is_locked(&lock->base);