blob: 39fda195bf788001448398275cf24545c20731c3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +02002/*
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 *
9 * Wound/wait implementation:
10 * Copyright (C) 2013 Canonical Ltd.
11 *
12 * This file contains the main data structure and API definitions.
13 */
14
15#ifndef __LINUX_WW_MUTEX_H
16#define __LINUX_WW_MUTEX_H
17
18#include <linux/mutex.h>
19
20struct ww_class {
21 atomic_long_t stamp;
22 struct lock_class_key acquire_key;
23 struct lock_class_key mutex_key;
24 const char *acquire_name;
25 const char *mutex_name;
26};
27
28struct ww_acquire_ctx {
29 struct task_struct *task;
30 unsigned long stamp;
31 unsigned acquired;
32#ifdef CONFIG_DEBUG_MUTEXES
33 unsigned done_acquire;
34 struct ww_class *ww_class;
35 struct ww_mutex *contending_lock;
36#endif
37#ifdef CONFIG_DEBUG_LOCK_ALLOC
38 struct lockdep_map dep_map;
39#endif
40#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
41 unsigned deadlock_inject_interval;
42 unsigned deadlock_inject_countdown;
43#endif
44};
45
46struct ww_mutex {
47 struct mutex base;
48 struct ww_acquire_ctx *ctx;
49#ifdef CONFIG_DEBUG_MUTEXES
50 struct ww_class *ww_class;
51#endif
52};
53
54#ifdef CONFIG_DEBUG_LOCK_ALLOC
Chris Wilsonaf2e859e2016-12-01 11:47:04 +000055# define __WW_CLASS_MUTEX_INITIALIZER(lockname, class) \
56 , .ww_class = class
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +020057#else
Chris Wilsonaf2e859e2016-12-01 11:47:04 +000058# define __WW_CLASS_MUTEX_INITIALIZER(lockname, class)
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +020059#endif
60
61#define __WW_CLASS_INITIALIZER(ww_class) \
62 { .stamp = ATOMIC_LONG_INIT(0) \
63 , .acquire_name = #ww_class "_acquire" \
64 , .mutex_name = #ww_class "_mutex" }
65
66#define __WW_MUTEX_INITIALIZER(lockname, class) \
Chris Wilsonaf2e859e2016-12-01 11:47:04 +000067 { .base = __MUTEX_INITIALIZER(lockname.base) \
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +020068 __WW_CLASS_MUTEX_INITIALIZER(lockname, class) }
69
70#define DEFINE_WW_CLASS(classname) \
71 struct ww_class classname = __WW_CLASS_INITIALIZER(classname)
72
73#define DEFINE_WW_MUTEX(mutexname, ww_class) \
74 struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
75
76/**
77 * ww_mutex_init - initialize the w/w mutex
78 * @lock: the mutex to be initialized
79 * @ww_class: the w/w class the mutex should belong to
80 *
81 * Initialize the w/w mutex to unlocked state and associate it with the given
82 * class.
83 *
84 * It is not allowed to initialize an already locked mutex.
85 */
86static inline void ww_mutex_init(struct ww_mutex *lock,
87 struct ww_class *ww_class)
88{
89 __mutex_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
90 lock->ctx = NULL;
91#ifdef CONFIG_DEBUG_MUTEXES
92 lock->ww_class = ww_class;
93#endif
94}
95
96/**
97 * ww_acquire_init - initialize a w/w acquire context
98 * @ctx: w/w acquire context to initialize
99 * @ww_class: w/w class of the context
100 *
101 * Initializes an context to acquire multiple mutexes of the given w/w class.
102 *
103 * Context-based w/w mutex acquiring can be done in any order whatsoever within
104 * a given lock class. Deadlocks will be detected and handled with the
105 * wait/wound logic.
106 *
107 * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
108 * result in undetected deadlocks and is so forbidden. Mixing different contexts
109 * for the same w/w class when acquiring mutexes can also result in undetected
110 * deadlocks, and is hence also forbidden. Both types of abuse will be caught by
111 * enabling CONFIG_PROVE_LOCKING.
112 *
113 * Nesting of acquire contexts for _different_ w/w classes is possible, subject
114 * to the usual locking rules between different lock classes.
115 *
116 * An acquire context must be released with ww_acquire_fini by the same task
117 * before the memory is freed. It is recommended to allocate the context itself
118 * on the stack.
119 */
120static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
121 struct ww_class *ww_class)
122{
123 ctx->task = current;
Peter Zijlstraf4ec57b2016-11-24 12:46:26 +0100124 ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp);
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +0200125 ctx->acquired = 0;
126#ifdef CONFIG_DEBUG_MUTEXES
127 ctx->ww_class = ww_class;
128 ctx->done_acquire = 0;
129 ctx->contending_lock = NULL;
130#endif
131#ifdef CONFIG_DEBUG_LOCK_ALLOC
132 debug_check_no_locks_freed((void *)ctx, sizeof(*ctx));
133 lockdep_init_map(&ctx->dep_map, ww_class->acquire_name,
134 &ww_class->acquire_key, 0);
135 mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_);
136#endif
137#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
138 ctx->deadlock_inject_interval = 1;
139 ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
140#endif
141}
142
143/**
144 * ww_acquire_done - marks the end of the acquire phase
145 * @ctx: the acquire context
146 *
147 * Marks the end of the acquire phase, any further w/w mutex lock calls using
148 * this context are forbidden.
149 *
150 * Calling this function is optional, it is just useful to document w/w mutex
151 * code and clearly designated the acquire phase from actually using the locked
152 * data structures.
153 */
154static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
155{
156#ifdef CONFIG_DEBUG_MUTEXES
157 lockdep_assert_held(ctx);
158
159 DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
160 ctx->done_acquire = 1;
161#endif
162}
163
164/**
165 * ww_acquire_fini - releases a w/w acquire context
166 * @ctx: the acquire context to free
167 *
168 * Releases a w/w acquire context. This must be called _after_ all acquired w/w
169 * mutexes have been released with ww_mutex_unlock.
170 */
171static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
172{
173#ifdef CONFIG_DEBUG_MUTEXES
174 mutex_release(&ctx->dep_map, 0, _THIS_IP_);
175
176 DEBUG_LOCKS_WARN_ON(ctx->acquired);
Masahiro Yamada97f26452016-08-03 13:45:50 -0700177 if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +0200178 /*
179 * lockdep will normally handle this,
180 * but fail without anyway
181 */
182 ctx->done_acquire = 1;
183
Masahiro Yamada97f26452016-08-03 13:45:50 -0700184 if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC))
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +0200185 /* ensure ww_acquire_fini will still fail if called twice */
186 ctx->acquired = ~0U;
187#endif
188}
189
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +0200190/**
191 * ww_mutex_lock - acquire the w/w mutex
192 * @lock: the mutex to be acquired
193 * @ctx: w/w acquire context, or NULL to acquire only a single lock.
194 *
195 * Lock the w/w mutex exclusively for this task.
196 *
197 * Deadlocks within a given w/w class of locks are detected and handled with the
198 * wait/wound algorithm. If the lock isn't immediately avaiable this function
199 * will either sleep until it is (wait case). Or it selects the current context
200 * for backing off by returning -EDEADLK (wound case). Trying to acquire the
201 * same lock with the same context twice is also detected and signalled by
202 * returning -EALREADY. Returns 0 if the mutex was successfully acquired.
203 *
204 * In the wound case the caller must release all currently held w/w mutexes for
205 * the given context and then wait for this contending lock to be available by
206 * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
207 * lock and proceed with trying to acquire further w/w mutexes (e.g. when
208 * scanning through lru lists trying to free resources).
209 *
210 * The mutex must later on be released by the same task that
211 * acquired it. The task may not exit without first unlocking the mutex. Also,
212 * kernel memory where the mutex resides must not be freed with the mutex still
213 * locked. The mutex must first be initialized (or statically defined) before it
214 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
215 * of the same w/w lock class as was used to initialize the acquire context.
216 *
217 * A mutex acquired with this function must be released with ww_mutex_unlock.
218 */
Ingo Molnar23b19ec2017-01-14 12:11:59 +0100219extern int /* __must_check */ ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx);
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +0200220
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 */
Nicolai Hähnlec5470b22016-12-21 19:46:33 +0100251extern int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
252 struct ww_acquire_ctx *ctx);
Maarten Lankhorst1b375dc2013-07-05 09:29:32 +0200253
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