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>
21 #include <linux/rtmutex.h>
23 #if defined(CONFIG_DEBUG_MUTEXES) || \
24 (defined(CONFIG_PREEMPT_RT) && defined(CONFIG_DEBUG_RT_MUTEXES))
25 #define DEBUG_WW_MUTEXES
28 #ifndef CONFIG_PREEMPT_RT
29 #define WW_MUTEX_BASE mutex
30 #define ww_mutex_base_init(l,n,k) __mutex_init(l,n,k)
31 #define ww_mutex_base_is_locked(b) mutex_is_locked((b))
33 #define WW_MUTEX_BASE rt_mutex
34 #define ww_mutex_base_init(l,n,k) __rt_mutex_init(l,n,k)
35 #define ww_mutex_base_is_locked(b) rt_mutex_base_is_locked(&(b)->rtmutex)
40 struct lock_class_key acquire_key
;
41 struct lock_class_key mutex_key
;
42 const char *acquire_name
;
43 const char *mutex_name
;
44 unsigned int is_wait_die
;
48 struct WW_MUTEX_BASE base
;
49 struct ww_acquire_ctx
*ctx
;
50 #ifdef DEBUG_WW_MUTEXES
51 struct ww_class
*ww_class
;
55 struct ww_acquire_ctx
{
56 struct task_struct
*task
;
58 unsigned int acquired
;
59 unsigned short wounded
;
60 unsigned short is_wait_die
;
61 #ifdef DEBUG_WW_MUTEXES
62 unsigned int done_acquire
;
63 struct ww_class
*ww_class
;
64 void *contending_lock
;
66 #ifdef CONFIG_DEBUG_LOCK_ALLOC
67 struct lockdep_map dep_map
;
69 * @first_lock_dep_map: fake lockdep_map for first locked ww_mutex.
71 * lockdep requires the lockdep_map for the first locked ww_mutex
72 * in a ww transaction to remain in memory until all ww_mutexes of
73 * the transaction have been unlocked. Ensure this by keeping a
74 * fake locked ww_mutex lockdep map between ww_acquire_init() and
77 struct lockdep_map first_lock_dep_map
;
79 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
80 unsigned int deadlock_inject_interval
;
81 unsigned int deadlock_inject_countdown
;
85 #define __WW_CLASS_INITIALIZER(ww_class, _is_wait_die) \
86 { .stamp = ATOMIC_LONG_INIT(0) \
87 , .acquire_name = #ww_class "_acquire" \
88 , .mutex_name = #ww_class "_mutex" \
89 , .is_wait_die = _is_wait_die }
91 #define DEFINE_WD_CLASS(classname) \
92 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 1)
94 #define DEFINE_WW_CLASS(classname) \
95 struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 0)
98 * ww_mutex_init - initialize the w/w mutex
99 * @lock: the mutex to be initialized
100 * @ww_class: the w/w class the mutex should belong to
102 * Initialize the w/w mutex to unlocked state and associate it with the given
103 * class. Static define macro for w/w mutex is not provided and this function
104 * is the only way to properly initialize the w/w mutex.
106 * It is not allowed to initialize an already locked mutex.
108 static inline void ww_mutex_init(struct ww_mutex
*lock
,
109 struct ww_class
*ww_class
)
111 ww_mutex_base_init(&lock
->base
, ww_class
->mutex_name
, &ww_class
->mutex_key
);
113 #ifdef DEBUG_WW_MUTEXES
114 lock
->ww_class
= ww_class
;
119 * ww_acquire_init - initialize a w/w acquire context
120 * @ctx: w/w acquire context to initialize
121 * @ww_class: w/w class of the context
123 * Initializes an context to acquire multiple mutexes of the given w/w class.
125 * Context-based w/w mutex acquiring can be done in any order whatsoever within
126 * a given lock class. Deadlocks will be detected and handled with the
129 * Mixing of context-based w/w mutex acquiring and single w/w mutex locking can
130 * result in undetected deadlocks and is so forbidden. Mixing different contexts
131 * for the same w/w class when acquiring mutexes can also result in undetected
132 * deadlocks, and is hence also forbidden. Both types of abuse will be caught by
133 * enabling CONFIG_PROVE_LOCKING.
135 * Nesting of acquire contexts for _different_ w/w classes is possible, subject
136 * to the usual locking rules between different lock classes.
138 * An acquire context must be released with ww_acquire_fini by the same task
139 * before the memory is freed. It is recommended to allocate the context itself
142 static inline void ww_acquire_init(struct ww_acquire_ctx
*ctx
,
143 struct ww_class
*ww_class
)
146 ctx
->stamp
= atomic_long_inc_return_relaxed(&ww_class
->stamp
);
148 ctx
->wounded
= false;
149 ctx
->is_wait_die
= ww_class
->is_wait_die
;
150 #ifdef DEBUG_WW_MUTEXES
151 ctx
->ww_class
= ww_class
;
152 ctx
->done_acquire
= 0;
153 ctx
->contending_lock
= NULL
;
155 #ifdef CONFIG_DEBUG_LOCK_ALLOC
156 debug_check_no_locks_freed((void *)ctx
, sizeof(*ctx
));
157 lockdep_init_map(&ctx
->dep_map
, ww_class
->acquire_name
,
158 &ww_class
->acquire_key
, 0);
159 lockdep_init_map(&ctx
->first_lock_dep_map
, ww_class
->mutex_name
,
160 &ww_class
->mutex_key
, 0);
161 mutex_acquire(&ctx
->dep_map
, 0, 0, _RET_IP_
);
162 mutex_acquire_nest(&ctx
->first_lock_dep_map
, 0, 0, &ctx
->dep_map
, _RET_IP_
);
164 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
165 ctx
->deadlock_inject_interval
= 1;
166 ctx
->deadlock_inject_countdown
= ctx
->stamp
& 0xf;
171 * ww_acquire_done - marks the end of the acquire phase
172 * @ctx: the acquire context
174 * Marks the end of the acquire phase, any further w/w mutex lock calls using
175 * this context are forbidden.
177 * Calling this function is optional, it is just useful to document w/w mutex
178 * code and clearly designated the acquire phase from actually using the locked
181 static inline void ww_acquire_done(struct ww_acquire_ctx
*ctx
)
183 #ifdef DEBUG_WW_MUTEXES
184 lockdep_assert_held(ctx
);
186 DEBUG_LOCKS_WARN_ON(ctx
->done_acquire
);
187 ctx
->done_acquire
= 1;
192 * ww_acquire_fini - releases a w/w acquire context
193 * @ctx: the acquire context to free
195 * Releases a w/w acquire context. This must be called _after_ all acquired w/w
196 * mutexes have been released with ww_mutex_unlock.
198 static inline void ww_acquire_fini(struct ww_acquire_ctx
*ctx
)
200 #ifdef CONFIG_DEBUG_LOCK_ALLOC
201 mutex_release(&ctx
->first_lock_dep_map
, _THIS_IP_
);
202 mutex_release(&ctx
->dep_map
, _THIS_IP_
);
204 #ifdef DEBUG_WW_MUTEXES
205 DEBUG_LOCKS_WARN_ON(ctx
->acquired
);
206 if (!IS_ENABLED(CONFIG_PROVE_LOCKING
))
208 * lockdep will normally handle this,
209 * but fail without anyway
211 ctx
->done_acquire
= 1;
213 if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC
))
214 /* ensure ww_acquire_fini will still fail if called twice */
220 * ww_mutex_lock - acquire the w/w mutex
221 * @lock: the mutex to be acquired
222 * @ctx: w/w acquire context, or NULL to acquire only a single lock.
224 * Lock the w/w mutex exclusively for this task.
226 * Deadlocks within a given w/w class of locks are detected and handled with the
227 * wait/die algorithm. If the lock isn't immediately available this function
228 * will either sleep until it is (wait case). Or it selects the current context
229 * for backing off by returning -EDEADLK (die case). Trying to acquire the
230 * same lock with the same context twice is also detected and signalled by
231 * returning -EALREADY. Returns 0 if the mutex was successfully acquired.
233 * In the die case the caller must release all currently held w/w mutexes for
234 * the given context and then wait for this contending lock to be available by
235 * calling ww_mutex_lock_slow. Alternatively callers can opt to not acquire this
236 * lock and proceed with trying to acquire further w/w mutexes (e.g. when
237 * scanning through lru lists trying to free resources).
239 * The mutex must later on be released by the same task that
240 * acquired it. The task may not exit without first unlocking the mutex. Also,
241 * kernel memory where the mutex resides must not be freed with the mutex still
242 * locked. The mutex must first be initialized (or statically defined) before it
243 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
244 * of the same w/w lock class as was used to initialize the acquire context.
246 * A mutex acquired with this function must be released with ww_mutex_unlock.
248 extern int /* __must_check */ ww_mutex_lock(struct ww_mutex
*lock
, struct ww_acquire_ctx
*ctx
);
251 * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
252 * @lock: the mutex to be acquired
253 * @ctx: w/w acquire context
255 * Lock the w/w mutex exclusively for this task.
257 * Deadlocks within a given w/w class of locks are detected and handled with the
258 * wait/die algorithm. If the lock isn't immediately available this function
259 * will either sleep until it is (wait case). Or it selects the current context
260 * for backing off by returning -EDEADLK (die case). Trying to acquire the
261 * same lock with the same context twice is also detected and signalled by
262 * returning -EALREADY. Returns 0 if the mutex was successfully acquired. If a
263 * signal arrives while waiting for the lock then this function returns -EINTR.
265 * In the die case the caller must release all currently held w/w mutexes for
266 * the given context and then wait for this contending lock to be available by
267 * calling ww_mutex_lock_slow_interruptible. Alternatively callers can opt to
268 * not acquire this lock and proceed with trying to acquire further w/w mutexes
269 * (e.g. when scanning through lru lists trying to free resources).
271 * The mutex must later on be released by the same task that
272 * acquired it. The task may not exit without first unlocking the mutex. Also,
273 * kernel memory where the mutex resides must not be freed with the mutex still
274 * locked. The mutex must first be initialized (or statically defined) before it
275 * can be locked. memset()-ing the mutex to 0 is not allowed. The mutex must be
276 * of the same w/w lock class as was used to initialize the acquire context.
278 * A mutex acquired with this function must be released with ww_mutex_unlock.
280 extern int __must_check
ww_mutex_lock_interruptible(struct ww_mutex
*lock
,
281 struct ww_acquire_ctx
*ctx
);
284 * ww_mutex_lock_slow - slowpath acquiring of the w/w mutex
285 * @lock: the mutex to be acquired
286 * @ctx: w/w acquire context
288 * Acquires a w/w mutex with the given context after a die case. This function
289 * will sleep until the lock becomes available.
291 * The caller must have released all w/w mutexes already acquired with the
292 * context and then call this function on the contended lock.
294 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
295 * needs with ww_mutex_lock. Note that the -EALREADY return code from
296 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
298 * It is forbidden to call this function with any other w/w mutexes associated
299 * with the context held. It is forbidden to call this on anything else than the
302 * Note that the slowpath lock acquiring can also be done by calling
303 * ww_mutex_lock directly. This function here is simply to help w/w mutex
304 * locking code readability by clearly denoting the slowpath.
307 ww_mutex_lock_slow(struct ww_mutex
*lock
, struct ww_acquire_ctx
*ctx
)
310 #ifdef DEBUG_WW_MUTEXES
311 DEBUG_LOCKS_WARN_ON(!ctx
->contending_lock
);
313 ret
= ww_mutex_lock(lock
, ctx
);
318 * ww_mutex_lock_slow_interruptible - slowpath acquiring of the w/w mutex, interruptible
319 * @lock: the mutex to be acquired
320 * @ctx: w/w acquire context
322 * Acquires a w/w mutex with the given context after a die case. This function
323 * will sleep until the lock becomes available and returns 0 when the lock has
324 * been acquired. If a signal arrives while waiting for the lock then this
325 * function returns -EINTR.
327 * The caller must have released all w/w mutexes already acquired with the
328 * context and then call this function on the contended lock.
330 * Afterwards the caller may continue to (re)acquire the other w/w mutexes it
331 * needs with ww_mutex_lock. Note that the -EALREADY return code from
332 * ww_mutex_lock can be used to avoid locking this contended mutex twice.
334 * It is forbidden to call this function with any other w/w mutexes associated
335 * with the given context held. It is forbidden to call this on anything else
336 * than the contending mutex.
338 * Note that the slowpath lock acquiring can also be done by calling
339 * ww_mutex_lock_interruptible directly. This function here is simply to help
340 * w/w mutex locking code readability by clearly denoting the slowpath.
342 static inline int __must_check
343 ww_mutex_lock_slow_interruptible(struct ww_mutex
*lock
,
344 struct ww_acquire_ctx
*ctx
)
346 #ifdef DEBUG_WW_MUTEXES
347 DEBUG_LOCKS_WARN_ON(!ctx
->contending_lock
);
349 return ww_mutex_lock_interruptible(lock
, ctx
);
352 extern void ww_mutex_unlock(struct ww_mutex
*lock
);
354 extern int __must_check
ww_mutex_trylock(struct ww_mutex
*lock
,
355 struct ww_acquire_ctx
*ctx
);
358 * ww_mutex_destroy - mark a w/w mutex unusable
359 * @lock: the mutex to be destroyed
361 * This function marks the mutex uninitialized, and any subsequent
362 * use of the mutex is forbidden. The mutex must not be locked when
363 * this function is called.
365 static inline void ww_mutex_destroy(struct ww_mutex
*lock
)
367 #ifndef CONFIG_PREEMPT_RT
368 mutex_destroy(&lock
->base
);
373 * ww_mutex_is_locked - is the w/w mutex locked
374 * @lock: the mutex to be queried
376 * Returns 1 if the mutex is locked, 0 if unlocked.
378 static inline bool ww_mutex_is_locked(struct ww_mutex
*lock
)
380 return ww_mutex_base_is_locked(&lock
->base
);