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;
51 #ifdef CONFIG_DEBUG_LOCK_ALLOC
52 # define __WW_CLASS_MUTEX_INITIALIZER(lockname, class) \
55 # define __WW_CLASS_MUTEX_INITIALIZER(lockname, class)
58 #define __WW_CLASS_INITIALIZER(ww_class, _is_wait_die) \
59 { .stamp = ATOMIC_LONG_INIT(0) \
60 , .acquire_name = #ww_class "_acquire" \
61 , .mutex_name = #ww_class "_mutex" \
62 , .is_wait_die = _is_wait_die }
64 #define __WW_MUTEX_INITIALIZER(lockname, class) \
65 { .base = __MUTEX_INITIALIZER(lockname.base) \
66 __WW_CLASS_MUTEX_INITIALIZER(lockname, class) }
68 #define DEFINE_WD_CLASS(classname) \
69 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 1)
71 #define DEFINE_WW_CLASS(classname) \
72 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 0)
74 #define DEFINE_WW_MUTEX(mutexname, ww_class) \
75 struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
78 * ww_mutex_init - initialize the w/w mutex
79 * @lock: the mutex to be initialized
80 * @ww_class: the w/w class the mutex should belong to
82 * Initialize the w/w mutex to unlocked state and associate it with the given
85 * It is not allowed to initialize an already locked mutex.
87 static inline void ww_mutex_init(struct ww_mutex *lock,
88 struct ww_class *ww_class)
90 __mutex_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
92 #ifdef CONFIG_DEBUG_MUTEXES
93 lock->ww_class = ww_class;
98 * ww_acquire_init - initialize a w/w acquire context
99 * @ctx: w/w acquire context to initialize
100 * @ww_class: w/w class of the context
102 * Initializes an context to acquire multiple mutexes of the given w/w class.
104 * Context-based w/w mutex acquiring can be done in any order whatsoever within
105 * a given lock class. Deadlocks will be detected and handled with the
108 * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
109 * result in undetected deadlocks and is so forbidden. Mixing different contexts
110 * for the same w/w class when acquiring mutexes can also result in undetected
111 * deadlocks, and is hence also forbidden. Both types of abuse will be caught by
112 * enabling CONFIG_PROVE_LOCKING.
114 * Nesting of acquire contexts for _different_ w/w classes is possible, subject
115 * to the usual locking rules between different lock classes.
117 * An acquire context must be released with ww_acquire_fini by the same task
118 * before the memory is freed. It is recommended to allocate the context itself
121 static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
122 struct ww_class *ww_class)
125 ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp);
127 ctx->wounded = false;
128 ctx->is_wait_die = ww_class->is_wait_die;
129 #ifdef CONFIG_DEBUG_MUTEXES
130 ctx->ww_class = ww_class;
131 ctx->done_acquire = 0;
132 ctx->contending_lock = NULL;
134 #ifdef CONFIG_DEBUG_LOCK_ALLOC
135 debug_check_no_locks_freed((void *)ctx, sizeof(*ctx));
136 lockdep_init_map(&ctx->dep_map, ww_class->acquire_name,
137 &ww_class->acquire_key, 0);
138 mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_);
140 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
141 ctx->deadlock_inject_interval = 1;
142 ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
147 * ww_acquire_done - marks the end of the acquire phase
148 * @ctx: the acquire context
150 * Marks the end of the acquire phase, any further w/w mutex lock calls using
151 * this context are forbidden.
153 * Calling this function is optional, it is just useful to document w/w mutex
154 * code and clearly designated the acquire phase from actually using the locked
157 static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
159 #ifdef CONFIG_DEBUG_MUTEXES
160 lockdep_assert_held(ctx);
162 DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
163 ctx->done_acquire = 1;
168 * ww_acquire_fini - releases a w/w acquire context
169 * @ctx: the acquire context to free
171 * Releases a w/w acquire context. This must be called _after_ all acquired w/w
172 * mutexes have been released with ww_mutex_unlock.
174 static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
176 #ifdef CONFIG_DEBUG_MUTEXES
177 mutex_release(&ctx->dep_map, _THIS_IP_);
179 DEBUG_LOCKS_WARN_ON(ctx->acquired);
180 if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
182 * lockdep will normally handle this,
183 * but fail without anyway
185 ctx->done_acquire = 1;
187 if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC))
188 /* ensure ww_acquire_fini will still fail if called twice */
194 * ww_mutex_lock - acquire the w/w mutex
195 * @lock: the mutex to be acquired
196 * @ctx: w/w acquire context, or NULL to acquire only a single lock.
198 * Lock the w/w mutex exclusively for this task.
200 * Deadlocks within a given w/w class of locks are detected and handled with the
201 * wait/die algorithm. If the lock isn't immediately available this function
202 * will either sleep until it is (wait case). Or it selects the current context
203 * for backing off by returning -EDEADLK (die case). Trying to acquire the
204 * same lock with the same context twice is also detected and signalled by
205 * returning -EALREADY. Returns 0 if the mutex was successfully acquired.
207 * In the die case the caller must release all currently held w/w mutexes for
208 * the given context and then wait for this contending lock to be available by
209 * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
210 * lock and proceed with trying to acquire further w/w mutexes (e.g. when
211 * scanning through lru lists trying to free resources).
213 * The mutex must later on be released by the same task that
214 * acquired it. The task may not exit without first unlocking the mutex. Also,
215 * kernel memory where the mutex resides must not be freed with the mutex still
216 * locked. The mutex must first be initialized (or statically defined) before it
217 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
218 * of the same w/w lock class as was used to initialize the acquire context.
220 * A mutex acquired with this function must be released with ww_mutex_unlock.
222 extern int /* __must_check */ ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx);
225 * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
226 * @lock: the mutex to be acquired
227 * @ctx: w/w acquire context
229 * Lock the w/w mutex exclusively for this task.
231 * Deadlocks within a given w/w class of locks are detected and handled with the
232 * wait/die algorithm. If the lock isn't immediately available this function
233 * will either sleep until it is (wait case). Or it selects the current context
234 * for backing off by returning -EDEADLK (die case). Trying to acquire the
235 * same lock with the same context twice is also detected and signalled by
236 * returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a
237 * signal arrives while waiting for the lock then this function returns -EINTR.
239 * In the die case the caller must release all currently held w/w mutexes for
240 * the given context and then wait for this contending lock to be available by
241 * calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to
242 * not acquire this lock and proceed with trying to acquire further w/w mutexes
243 * (e.g. when scanning through lru lists trying to free resources).
245 * The mutex must later on be released by the same task that
246 * acquired it. The task may not exit without first unlocking the mutex. Also,
247 * kernel memory where the mutex resides must not be freed with the mutex still
248 * locked. The mutex must first be initialized (or statically defined) before it
249 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
250 * of the same w/w lock class as was used to initialize the acquire context.
252 * A mutex acquired with this function must be released with ww_mutex_unlock.
254 extern int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
255 struct ww_acquire_ctx *ctx);
258 * ww_mutex_lock_slow - slowpath acquiring of the w/w mutex
259 * @lock: the mutex to be acquired
260 * @ctx: w/w acquire context
262 * Acquires a w/w mutex with the given context after a die case. This function
263 * will sleep until the lock becomes available.
265 * The caller must have released all w/w mutexes already acquired with the
266 * context and then call this function on the contended lock.
268 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
269 * needs with ww_mutex_lock. Note that the -EALREADY return code from
270 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
272 * It is forbidden to call this function with any other w/w mutexes associated
273 * with the context held. It is forbidden to call this on anything else than the
276 * Note that the slowpath lock acquiring can also be done by calling
277 * ww_mutex_lock directly. This function here is simply to help w/w mutex
278 * locking code readability by clearly denoting the slowpath.
281 ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
284 #ifdef CONFIG_DEBUG_MUTEXES
285 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
287 ret = ww_mutex_lock(lock, ctx);
292 * ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex, interruptible
293 * @lock: the mutex to be acquired
294 * @ctx: w/w acquire context
296 * Acquires a w/w mutex with the given context after a die case. This function
297 * will sleep until the lock becomes available and returns 0 when the lock has
298 * been acquired. If a signal arrives while waiting for the lock then this
299 * function returns -EINTR.
301 * The caller must have released all w/w mutexes already acquired with the
302 * context and then call this function on the contended lock.
304 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
305 * needs with ww_mutex_lock. Note that the -EALREADY return code from
306 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
308 * It is forbidden to call this function with any other w/w mutexes associated
309 * with the given context held. It is forbidden to call this on anything else
310 * than the contending mutex.
312 * Note that the slowpath lock acquiring can also be done by calling
313 * ww_mutex_lock_interruptible directly. This function here is simply to help
314 * w/w mutex locking code readability by clearly denoting the slowpath.
316 static inline int __must_check
317 ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
318 struct ww_acquire_ctx *ctx)
320 #ifdef CONFIG_DEBUG_MUTEXES
321 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
323 return ww_mutex_lock_interruptible(lock, ctx);
326 extern void ww_mutex_unlock(struct ww_mutex *lock);
329 * ww_mutex_trylock - tries to acquire the w/w mutex without acquire context
330 * @lock: mutex to lock
332 * Trylocks a mutex without acquire context, so no deadlock detection is
333 * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
335 static inline int __must_check ww_mutex_trylock(struct ww_mutex *lock)
337 return mutex_trylock(&lock->base);
341 * ww_mutex_destroy - mark a w/w mutex unusable
342 * @lock: the mutex to be destroyed
344 * This function marks the mutex uninitialized, and any subsequent
345 * use of the mutex is forbidden. The mutex must not be locked when
346 * this function is called.
348 static inline void ww_mutex_destroy(struct ww_mutex *lock)
350 mutex_destroy(&lock->base);
354 * ww_mutex_is_locked - is the w/w mutex locked
355 * @lock: the mutex to be queried
357 * Returns 1 if the mutex is locked, 0 if unlocked.
359 static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
361 return mutex_is_locked(&lock->base);