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 * Wound/wait implementation:
10 * Copyright (C) 2013 Canonical Ltd.
12 * This file contains the main data structure and API definitions.
15 #ifndef __LINUX_WW_MUTEX_H
16 #define __LINUX_WW_MUTEX_H
18 #include <linux/mutex.h>
22 struct lock_class_key acquire_key
;
23 struct lock_class_key mutex_key
;
24 const char *acquire_name
;
25 const char *mutex_name
;
28 struct ww_acquire_ctx
{
29 struct task_struct
*task
;
32 #ifdef CONFIG_DEBUG_MUTEXES
33 unsigned done_acquire
;
34 struct ww_class
*ww_class
;
35 struct ww_mutex
*contending_lock
;
37 #ifdef CONFIG_DEBUG_LOCK_ALLOC
38 struct lockdep_map dep_map
;
40 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
41 unsigned deadlock_inject_interval
;
42 unsigned deadlock_inject_countdown
;
48 struct ww_acquire_ctx
*ctx
;
49 #ifdef CONFIG_DEBUG_MUTEXES
50 struct ww_class
*ww_class
;
54 #ifdef CONFIG_DEBUG_LOCK_ALLOC
55 # define __WW_CLASS_MUTEX_INITIALIZER(lockname, class) \
58 # define __WW_CLASS_MUTEX_INITIALIZER(lockname, class)
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" }
66 #define __WW_MUTEX_INITIALIZER(lockname, class) \
67 { .base = __MUTEX_INITIALIZER(lockname.base) \
68 __WW_CLASS_MUTEX_INITIALIZER(lockname, class) }
70 #define DEFINE_WW_CLASS(classname) \
71 struct ww_class classname = __WW_CLASS_INITIALIZER(classname)
73 #define DEFINE_WW_MUTEX(mutexname, ww_class) \
74 struct ww_mutex mutexname = __WW_MUTEX_INITIALIZER(mutexname, ww_class)
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
81 * Initialize the w/w mutex to unlocked state and associate it with the given
84 * It is not allowed to initialize an already locked mutex.
86 static inline void ww_mutex_init(struct ww_mutex
*lock
,
87 struct ww_class
*ww_class
)
89 __mutex_init(&lock
->base
, ww_class
->mutex_name
, &ww_class
->mutex_key
);
91 #ifdef CONFIG_DEBUG_MUTEXES
92 lock
->ww_class
= ww_class
;
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
101 * Initializes an context to acquire multiple mutexes of the given w/w class.
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
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.
113 * Nesting of acquire contexts for _different_ w/w classes is possible, subject
114 * to the usual locking rules between different lock classes.
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
120 static inline void ww_acquire_init(struct ww_acquire_ctx
*ctx
,
121 struct ww_class
*ww_class
)
124 ctx
->stamp
= atomic_long_inc_return_relaxed(&ww_class
->stamp
);
126 #ifdef CONFIG_DEBUG_MUTEXES
127 ctx
->ww_class
= ww_class
;
128 ctx
->done_acquire
= 0;
129 ctx
->contending_lock
= NULL
;
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_
);
137 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
138 ctx
->deadlock_inject_interval
= 1;
139 ctx
->deadlock_inject_countdown
= ctx
->stamp
& 0xf;
144 * ww_acquire_done - marks the end of the acquire phase
145 * @ctx: the acquire context
147 * Marks the end of the acquire phase, any further w/w mutex lock calls using
148 * this context are forbidden.
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
154 static inline void ww_acquire_done(struct ww_acquire_ctx
*ctx
)
156 #ifdef CONFIG_DEBUG_MUTEXES
157 lockdep_assert_held(ctx
);
159 DEBUG_LOCKS_WARN_ON(ctx
->done_acquire
);
160 ctx
->done_acquire
= 1;
165 * ww_acquire_fini - releases a w/w acquire context
166 * @ctx: the acquire context to free
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.
171 static inline void ww_acquire_fini(struct ww_acquire_ctx
*ctx
)
173 #ifdef CONFIG_DEBUG_MUTEXES
174 mutex_release(&ctx
->dep_map
, 0, _THIS_IP_
);
176 DEBUG_LOCKS_WARN_ON(ctx
->acquired
);
177 if (!IS_ENABLED(CONFIG_PROVE_LOCKING
))
179 * lockdep will normally handle this,
180 * but fail without anyway
182 ctx
->done_acquire
= 1;
184 if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC
))
185 /* ensure ww_acquire_fini will still fail if called twice */
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.
195 * Lock the w/w mutex exclusively for this task.
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.
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).
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.
217 * A mutex acquired with this function must be released with ww_mutex_unlock.
219 extern int /* __must_check */ ww_mutex_lock(struct ww_mutex
*lock
, struct ww_acquire_ctx
*ctx
);
222 * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
223 * @lock: the mutex to be acquired
224 * @ctx: w/w acquire context
226 * Lock the w/w mutex exclusively for this task.
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.
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).
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.
249 * A mutex acquired with this function must be released with ww_mutex_unlock.
251 extern int __must_check
ww_mutex_lock_interruptible(struct ww_mutex
*lock
,
252 struct ww_acquire_ctx
*ctx
);
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
259 * Acquires a w/w mutex with the given context after a wound case. This function
260 * will sleep until the lock becomes available.
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.
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.
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
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.
278 ww_mutex_lock_slow(struct ww_mutex
*lock
, struct ww_acquire_ctx
*ctx
)
281 #ifdef CONFIG_DEBUG_MUTEXES
282 DEBUG_LOCKS_WARN_ON(!ctx
->contending_lock
);
284 ret
= ww_mutex_lock(lock
, ctx
);
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
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.
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.
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.
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.
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.
313 static inline int __must_check
314 ww_mutex_lock_slow_interruptible(struct ww_mutex
*lock
,
315 struct ww_acquire_ctx
*ctx
)
317 #ifdef CONFIG_DEBUG_MUTEXES
318 DEBUG_LOCKS_WARN_ON(!ctx
->contending_lock
);
320 return ww_mutex_lock_interruptible(lock
, ctx
);
323 extern void ww_mutex_unlock(struct ww_mutex
*lock
);
326 * ww_mutex_trylock - tries to acquire the w/w mutex without acquire context
327 * @lock: mutex to lock
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.
332 static inline int __must_check
ww_mutex_trylock(struct ww_mutex
*lock
)
334 return mutex_trylock(&lock
->base
);
338 * ww_mutex_destroy - mark a w/w mutex unusable
339 * @lock: the mutex to be destroyed
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.
345 static inline void ww_mutex_destroy(struct ww_mutex
*lock
)
347 mutex_destroy(&lock
->base
);
351 * ww_mutex_is_locked - is the w/w mutex locked
352 * @lock: the mutex to be queried
354 * Returns 1 if the mutex is locked, 0 if unlocked.
356 static inline bool ww_mutex_is_locked(struct ww_mutex
*lock
)
358 return mutex_is_locked(&lock
->base
);