Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-block.git] / include / linux / ww_mutex.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1b375dc3
ML
2/*
3 * Wound/Wait Mutexes: blocking mutual exclusion locks with deadlock avoidance
4 *
5 * Original mutex implementation started by Ingo Molnar:
6 *
7 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 *
55f036ca 9 * Wait/Die implementation:
1b375dc3 10 * Copyright (C) 2013 Canonical Ltd.
08295b3b
TH
11 * Choice of algorithm:
12 * Copyright (C) 2018 WMWare Inc.
1b375dc3
ML
13 *
14 * This file contains the main data structure and API definitions.
15 */
16
17#ifndef __LINUX_WW_MUTEX_H
18#define __LINUX_WW_MUTEX_H
19
20#include <linux/mutex.h>
21
22struct ww_class {
23 atomic_long_t stamp;
24 struct lock_class_key acquire_key;
25 struct lock_class_key mutex_key;
26 const char *acquire_name;
27 const char *mutex_name;
08295b3b 28 unsigned int is_wait_die;
1b375dc3
ML
29};
30
31struct ww_acquire_ctx {
32 struct task_struct *task;
33 unsigned long stamp;
55f036ca 34 unsigned int acquired;
08295b3b
TH
35 unsigned short wounded;
36 unsigned short is_wait_die;
1b375dc3 37#ifdef CONFIG_DEBUG_MUTEXES
55f036ca 38 unsigned int done_acquire;
1b375dc3
ML
39 struct ww_class *ww_class;
40 struct ww_mutex *contending_lock;
41#endif
42#ifdef CONFIG_DEBUG_LOCK_ALLOC
43 struct lockdep_map dep_map;
44#endif
45#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
55f036ca
PZ
46 unsigned int deadlock_inject_interval;
47 unsigned int deadlock_inject_countdown;
1b375dc3
ML
48#endif
49};
50
1b375dc3 51#ifdef CONFIG_DEBUG_LOCK_ALLOC
af2e859e
CW
52# define __WW_CLASS_MUTEX_INITIALIZER(lockname, class) \
53 , .ww_class = class
1b375dc3 54#else
af2e859e 55# define __WW_CLASS_MUTEX_INITIALIZER(lockname, class)
1b375dc3
ML
56#endif
57
08295b3b 58#define __WW_CLASS_INITIALIZER(ww_class, _is_wait_die) \
1b375dc3
ML
59 { .stamp = ATOMIC_LONG_INIT(0) \
60 , .acquire_name = #ww_class "_acquire" \
08295b3b
TH
61 , .mutex_name = #ww_class "_mutex" \
62 , .is_wait_die = _is_wait_die }
1b375dc3
ML
63
64#define __WW_MUTEX_INITIALIZER(lockname, class) \
af2e859e 65 { .base = __MUTEX_INITIALIZER(lockname.base) \
1b375dc3
ML
66 __WW_CLASS_MUTEX_INITIALIZER(lockname, class) }
67
08295b3b
TH
68#define DEFINE_WD_CLASS(classname) \
69 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 1)
70
1b375dc3 71#define DEFINE_WW_CLASS(classname) \
08295b3b 72 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 0)
1b375dc3
ML
73
74#define DEFINE_WW_MUTEX(mutexname, ww_class) \
75 struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
76
77/**
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
81 *
82 * Initialize the w/w mutex to unlocked state and associate it with the given
83 * class.
84 *
85 * It is not allowed to initialize an already locked mutex.
86 */
87static inline void ww_mutex_init(struct ww_mutex *lock,
88 struct ww_class *ww_class)
89{
90 __mutex_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
91 lock->ctx = NULL;
92#ifdef CONFIG_DEBUG_MUTEXES
93 lock->ww_class = ww_class;
94#endif
95}
96
97/**
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
101 *
102 * Initializes an context to acquire multiple mutexes of the given w/w class.
103 *
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
55f036ca 106 * wait/die logic.
1b375dc3
ML
107 *
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.
113 *
114 * Nesting of acquire contexts for _different_ w/w classes is possible, subject
115 * to the usual locking rules between different lock classes.
116 *
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
119 * on the stack.
120 */
121static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
122 struct ww_class *ww_class)
123{
124 ctx->task = current;
f4ec57b6 125 ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp);
1b375dc3 126 ctx->acquired = 0;
08295b3b
TH
127 ctx->wounded = false;
128 ctx->is_wait_die = ww_class->is_wait_die;
1b375dc3
ML
129#ifdef CONFIG_DEBUG_MUTEXES
130 ctx->ww_class = ww_class;
131 ctx->done_acquire = 0;
132 ctx->contending_lock = NULL;
133#endif
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_);
139#endif
140#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
141 ctx->deadlock_inject_interval = 1;
142 ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
143#endif
144}
145
146/**
147 * ww_acquire_done - marks the end of the acquire phase
148 * @ctx: the acquire context
149 *
150 * Marks the end of the acquire phase, any further w/w mutex lock calls using
151 * this context are forbidden.
152 *
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
155 * data structures.
156 */
157static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
158{
159#ifdef CONFIG_DEBUG_MUTEXES
160 lockdep_assert_held(ctx);
161
162 DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
163 ctx->done_acquire = 1;
164#endif
165}
166
167/**
168 * ww_acquire_fini - releases a w/w acquire context
169 * @ctx: the acquire context to free
170 *
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.
173 */
174static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
175{
176#ifdef CONFIG_DEBUG_MUTEXES
5facae4f 177 mutex_release(&ctx->dep_map, _THIS_IP_);
1b375dc3
ML
178
179 DEBUG_LOCKS_WARN_ON(ctx->acquired);
97f2645f 180 if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
1b375dc3
ML
181 /*
182 * lockdep will normally handle this,
183 * but fail without anyway
184 */
185 ctx->done_acquire = 1;
186
97f2645f 187 if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC))
1b375dc3
ML
188 /* ensure ww_acquire_fini will still fail if called twice */
189 ctx->acquired = ~0U;
190#endif
191}
192
1b375dc3
ML
193/**
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.
197 *
198 * Lock the w/w mutex exclusively for this task.
199 *
200 * Deadlocks within a given w/w class of locks are detected and handled with the
55f036ca 201 * wait/die algorithm. If the lock isn't immediately available this function
1b375dc3 202 * will either sleep until it is (wait case). Or it selects the current context
55f036ca 203 * for backing off by returning -EDEADLK (die case). Trying to acquire the
1b375dc3
ML
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.
206 *
55f036ca 207 * In the die case the caller must release all currently held w/w mutexes for
1b375dc3
ML
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).
212 *
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.
219 *
220 * A mutex acquired with this function must be released with ww_mutex_unlock.
221 */
23b19ec3 222extern int /* __must_check */ ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx);
1b375dc3
ML
223
224/**
225 * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
226 * @lock: the mutex to be acquired
227 * @ctx: w/w acquire context
228 *
229 * Lock the w/w mutex exclusively for this task.
230 *
231 * Deadlocks within a given w/w class of locks are detected and handled with the
55f036ca 232 * wait/die algorithm. If the lock isn't immediately available this function
1b375dc3 233 * will either sleep until it is (wait case). Or it selects the current context
55f036ca 234 * for backing off by returning -EDEADLK (die case). Trying to acquire the
1b375dc3
ML
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.
238 *
55f036ca 239 * In the die case the caller must release all currently held w/w mutexes for
1b375dc3
ML
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).
244 *
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.
251 *
252 * A mutex acquired with this function must be released with ww_mutex_unlock.
253 */
c5470b22
NH
254extern int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
255 struct ww_acquire_ctx *ctx);
1b375dc3
ML
256
257/**
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
261 *
55f036ca 262 * Acquires a w/w mutex with the given context after a die case. This function
1b375dc3
ML
263 * will sleep until the lock becomes available.
264 *
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.
267 *
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.
271 *
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
274 * contending mutex.
275 *
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.
279 */
280static inline void
281ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
282{
283 int ret;
284#ifdef CONFIG_DEBUG_MUTEXES
285 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
286#endif
287 ret = ww_mutex_lock(lock, ctx);
288 (void)ret;
289}
290
291/**
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
295 *
55f036ca 296 * Acquires a w/w mutex with the given context after a die case. This function
1b375dc3
ML
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.
300 *
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.
303 *
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.
307 *
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.
311 *
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.
315 */
316static inline int __must_check
317ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
318 struct ww_acquire_ctx *ctx)
319{
320#ifdef CONFIG_DEBUG_MUTEXES
321 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
322#endif
323 return ww_mutex_lock_interruptible(lock, ctx);
324}
325
326extern void ww_mutex_unlock(struct ww_mutex *lock);
327
328/**
329 * ww_mutex_trylock - tries to acquire the w/w mutex without acquire context
330 * @lock: mutex to lock
331 *
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.
334 */
335static inline int __must_check ww_mutex_trylock(struct ww_mutex *lock)
336{
337 return mutex_trylock(&lock->base);
338}
339
340/***
341 * ww_mutex_destroy - mark a w/w mutex unusable
342 * @lock: the mutex to be destroyed
343 *
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.
347 */
348static inline void ww_mutex_destroy(struct ww_mutex *lock)
349{
350 mutex_destroy(&lock->base);
351}
352
353/**
354 * ww_mutex_is_locked - is the w/w mutex locked
355 * @lock: the mutex to be queried
356 *
357 * Returns 1 if the mutex is locked, 0 if unlocked.
358 */
359static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
360{
361 return mutex_is_locked(&lock->base);
362}
363
364#endif