locking/atomic, kref: Avoid more abuse
[linux-block.git] / include / linux / ww_mutex.h
CommitLineData
1b375dc3
ML
1/*
2 * Wound/Wait Mutexes: blocking mutual exclusion locks with deadlock avoidance
3 *
4 * Original mutex implementation started by Ingo Molnar:
5 *
6 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7 *
8 * Wound/wait implementation:
9 * Copyright (C) 2013 Canonical Ltd.
10 *
11 * This file contains the main data structure and API definitions.
12 */
13
14#ifndef __LINUX_WW_MUTEX_H
15#define __LINUX_WW_MUTEX_H
16
17#include <linux/mutex.h>
18
19struct ww_class {
20 atomic_long_t stamp;
21 struct lock_class_key acquire_key;
22 struct lock_class_key mutex_key;
23 const char *acquire_name;
24 const char *mutex_name;
25};
26
27struct ww_acquire_ctx {
28 struct task_struct *task;
29 unsigned long stamp;
30 unsigned acquired;
31#ifdef CONFIG_DEBUG_MUTEXES
32 unsigned done_acquire;
33 struct ww_class *ww_class;
34 struct ww_mutex *contending_lock;
35#endif
36#ifdef CONFIG_DEBUG_LOCK_ALLOC
37 struct lockdep_map dep_map;
38#endif
39#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
40 unsigned deadlock_inject_interval;
41 unsigned deadlock_inject_countdown;
42#endif
43};
44
45struct ww_mutex {
46 struct mutex base;
47 struct ww_acquire_ctx *ctx;
48#ifdef CONFIG_DEBUG_MUTEXES
49 struct ww_class *ww_class;
50#endif
51};
52
53#ifdef CONFIG_DEBUG_LOCK_ALLOC
af2e859e
CW
54# define __WW_CLASS_MUTEX_INITIALIZER(lockname, class) \
55 , .ww_class = class
1b375dc3 56#else
af2e859e 57# define __WW_CLASS_MUTEX_INITIALIZER(lockname, class)
1b375dc3
ML
58#endif
59
60#define __WW_CLASS_INITIALIZER(ww_class) \
61 { .stamp = ATOMIC_LONG_INIT(0) \
62 , .acquire_name = #ww_class "_acquire" \
63 , .mutex_name = #ww_class "_mutex" }
64
65#define __WW_MUTEX_INITIALIZER(lockname, class) \
af2e859e 66 { .base = __MUTEX_INITIALIZER(lockname.base) \
1b375dc3
ML
67 __WW_CLASS_MUTEX_INITIALIZER(lockname, class) }
68
69#define DEFINE_WW_CLASS(classname) \
70 struct ww_class classname = __WW_CLASS_INITIALIZER(classname)
71
72#define DEFINE_WW_MUTEX(mutexname, ww_class) \
73 struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
74
75/**
76 * ww_mutex_init - initialize the w/w mutex
77 * @lock: the mutex to be initialized
78 * @ww_class: the w/w class the mutex should belong to
79 *
80 * Initialize the w/w mutex to unlocked state and associate it with the given
81 * class.
82 *
83 * It is not allowed to initialize an already locked mutex.
84 */
85static inline void ww_mutex_init(struct ww_mutex *lock,
86 struct ww_class *ww_class)
87{
88 __mutex_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
89 lock->ctx = NULL;
90#ifdef CONFIG_DEBUG_MUTEXES
91 lock->ww_class = ww_class;
92#endif
93}
94
95/**
96 * ww_acquire_init - initialize a w/w acquire context
97 * @ctx: w/w acquire context to initialize
98 * @ww_class: w/w class of the context
99 *
100 * Initializes an context to acquire multiple mutexes of the given w/w class.
101 *
102 * Context-based w/w mutex acquiring can be done in any order whatsoever within
103 * a given lock class. Deadlocks will be detected and handled with the
104 * wait/wound logic.
105 *
106 * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
107 * result in undetected deadlocks and is so forbidden. Mixing different contexts
108 * for the same w/w class when acquiring mutexes can also result in undetected
109 * deadlocks, and is hence also forbidden. Both types of abuse will be caught by
110 * enabling CONFIG_PROVE_LOCKING.
111 *
112 * Nesting of acquire contexts for _different_ w/w classes is possible, subject
113 * to the usual locking rules between different lock classes.
114 *
115 * An acquire context must be released with ww_acquire_fini by the same task
116 * before the memory is freed. It is recommended to allocate the context itself
117 * on the stack.
118 */
119static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
120 struct ww_class *ww_class)
121{
122 ctx->task = current;
f4ec57b6 123 ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp);
1b375dc3
ML
124 ctx->acquired = 0;
125#ifdef CONFIG_DEBUG_MUTEXES
126 ctx->ww_class = ww_class;
127 ctx->done_acquire = 0;
128 ctx->contending_lock = NULL;
129#endif
130#ifdef CONFIG_DEBUG_LOCK_ALLOC
131 debug_check_no_locks_freed((void *)ctx, sizeof(*ctx));
132 lockdep_init_map(&ctx->dep_map, ww_class->acquire_name,
133 &ww_class->acquire_key, 0);
134 mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_);
135#endif
136#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
137 ctx->deadlock_inject_interval = 1;
138 ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
139#endif
140}
141
142/**
143 * ww_acquire_done - marks the end of the acquire phase
144 * @ctx: the acquire context
145 *
146 * Marks the end of the acquire phase, any further w/w mutex lock calls using
147 * this context are forbidden.
148 *
149 * Calling this function is optional, it is just useful to document w/w mutex
150 * code and clearly designated the acquire phase from actually using the locked
151 * data structures.
152 */
153static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
154{
155#ifdef CONFIG_DEBUG_MUTEXES
156 lockdep_assert_held(ctx);
157
158 DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
159 ctx->done_acquire = 1;
160#endif
161}
162
163/**
164 * ww_acquire_fini - releases a w/w acquire context
165 * @ctx: the acquire context to free
166 *
167 * Releases a w/w acquire context. This must be called _after_ all acquired w/w
168 * mutexes have been released with ww_mutex_unlock.
169 */
170static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
171{
172#ifdef CONFIG_DEBUG_MUTEXES
173 mutex_release(&ctx->dep_map, 0, _THIS_IP_);
174
175 DEBUG_LOCKS_WARN_ON(ctx->acquired);
97f2645f 176 if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
1b375dc3
ML
177 /*
178 * lockdep will normally handle this,
179 * but fail without anyway
180 */
181 ctx->done_acquire = 1;
182
97f2645f 183 if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC))
1b375dc3
ML
184 /* ensure ww_acquire_fini will still fail if called twice */
185 ctx->acquired = ~0U;
186#endif
187}
188
1b375dc3
ML
189/**
190 * ww_mutex_lock - acquire the w/w mutex
191 * @lock: the mutex to be acquired
192 * @ctx: w/w acquire context, or NULL to acquire only a single lock.
193 *
194 * Lock the w/w mutex exclusively for this task.
195 *
196 * Deadlocks within a given w/w class of locks are detected and handled with the
197 * wait/wound algorithm. If the lock isn't immediately avaiable this function
198 * will either sleep until it is (wait case). Or it selects the current context
199 * for backing off by returning -EDEADLK (wound case). Trying to acquire the
200 * same lock with the same context twice is also detected and signalled by
201 * returning -EALREADY. Returns 0 if the mutex was successfully acquired.
202 *
203 * In the wound case the caller must release all currently held w/w mutexes for
204 * the given context and then wait for this contending lock to be available by
205 * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
206 * lock and proceed with trying to acquire further w/w mutexes (e.g. when
207 * scanning through lru lists trying to free resources).
208 *
209 * The mutex must later on be released by the same task that
210 * acquired it. The task may not exit without first unlocking the mutex. Also,
211 * kernel memory where the mutex resides must not be freed with the mutex still
212 * locked. The mutex must first be initialized (or statically defined) before it
213 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
214 * of the same w/w lock class as was used to initialize the acquire context.
215 *
216 * A mutex acquired with this function must be released with ww_mutex_unlock.
217 */
c5470b22
NH
218extern int __must_check ww_mutex_lock(struct ww_mutex *lock,
219 struct ww_acquire_ctx *ctx);
1b375dc3
ML
220
221/**
222 * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
223 * @lock: the mutex to be acquired
224 * @ctx: w/w acquire context
225 *
226 * Lock the w/w mutex exclusively for this task.
227 *
228 * Deadlocks within a given w/w class of locks are detected and handled with the
229 * wait/wound algorithm. If the lock isn't immediately avaiable this function
230 * will either sleep until it is (wait case). Or it selects the current context
231 * for backing off by returning -EDEADLK (wound case). Trying to acquire the
232 * same lock with the same context twice is also detected and signalled by
233 * returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a
234 * signal arrives while waiting for the lock then this function returns -EINTR.
235 *
236 * In the wound case the caller must release all currently held w/w mutexes for
237 * the given context and then wait for this contending lock to be available by
238 * calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to
239 * not acquire this lock and proceed with trying to acquire further w/w mutexes
240 * (e.g. when scanning through lru lists trying to free resources).
241 *
242 * The mutex must later on be released by the same task that
243 * acquired it. The task may not exit without first unlocking the mutex. Also,
244 * kernel memory where the mutex resides must not be freed with the mutex still
245 * locked. The mutex must first be initialized (or statically defined) before it
246 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
247 * of the same w/w lock class as was used to initialize the acquire context.
248 *
249 * A mutex acquired with this function must be released with ww_mutex_unlock.
250 */
c5470b22
NH
251extern int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
252 struct ww_acquire_ctx *ctx);
1b375dc3
ML
253
254/**
255 * ww_mutex_lock_slow - slowpath acquiring of the w/w mutex
256 * @lock: the mutex to be acquired
257 * @ctx: w/w acquire context
258 *
259 * Acquires a w/w mutex with the given context after a wound case. This function
260 * will sleep until the lock becomes available.
261 *
262 * The caller must have released all w/w mutexes already acquired with the
263 * context and then call this function on the contended lock.
264 *
265 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
266 * needs with ww_mutex_lock. Note that the -EALREADY return code from
267 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
268 *
269 * It is forbidden to call this function with any other w/w mutexes associated
270 * with the context held. It is forbidden to call this on anything else than the
271 * contending mutex.
272 *
273 * Note that the slowpath lock acquiring can also be done by calling
274 * ww_mutex_lock directly. This function here is simply to help w/w mutex
275 * locking code readability by clearly denoting the slowpath.
276 */
277static inline void
278ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
279{
280 int ret;
281#ifdef CONFIG_DEBUG_MUTEXES
282 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
283#endif
284 ret = ww_mutex_lock(lock, ctx);
285 (void)ret;
286}
287
288/**
289 * ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex, interruptible
290 * @lock: the mutex to be acquired
291 * @ctx: w/w acquire context
292 *
293 * Acquires a w/w mutex with the given context after a wound case. This function
294 * will sleep until the lock becomes available and returns 0 when the lock has
295 * been acquired. If a signal arrives while waiting for the lock then this
296 * function returns -EINTR.
297 *
298 * The caller must have released all w/w mutexes already acquired with the
299 * context and then call this function on the contended lock.
300 *
301 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
302 * needs with ww_mutex_lock. Note that the -EALREADY return code from
303 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
304 *
305 * It is forbidden to call this function with any other w/w mutexes associated
306 * with the given context held. It is forbidden to call this on anything else
307 * than the contending mutex.
308 *
309 * Note that the slowpath lock acquiring can also be done by calling
310 * ww_mutex_lock_interruptible directly. This function here is simply to help
311 * w/w mutex locking code readability by clearly denoting the slowpath.
312 */
313static inline int __must_check
314ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
315 struct ww_acquire_ctx *ctx)
316{
317#ifdef CONFIG_DEBUG_MUTEXES
318 DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
319#endif
320 return ww_mutex_lock_interruptible(lock, ctx);
321}
322
323extern void ww_mutex_unlock(struct ww_mutex *lock);
324
325/**
326 * ww_mutex_trylock - tries to acquire the w/w mutex without acquire context
327 * @lock: mutex to lock
328 *
329 * Trylocks a mutex without acquire context, so no deadlock detection is
330 * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
331 */
332static inline int __must_check ww_mutex_trylock(struct ww_mutex *lock)
333{
334 return mutex_trylock(&lock->base);
335}
336
337/***
338 * ww_mutex_destroy - mark a w/w mutex unusable
339 * @lock: the mutex to be destroyed
340 *
341 * This function marks the mutex uninitialized, and any subsequent
342 * use of the mutex is forbidden. The mutex must not be locked when
343 * this function is called.
344 */
345static inline void ww_mutex_destroy(struct ww_mutex *lock)
346{
347 mutex_destroy(&lock->base);
348}
349
350/**
351 * ww_mutex_is_locked - is the w/w mutex locked
352 * @lock: the mutex to be queried
353 *
354 * Returns 1 if the mutex is locked, 0 if unlocked.
355 */
356static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
357{
358 return mutex_is_locked(&lock->base);
359}
360
361#endif