xref: /linux/include/linux/ww_mutex.h (revision 7cb1b466315004af98f6ba6c2546bb713ca3c237)
1 /* SPDX-License-Identifier: GPL-2.0 */
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  *
9  * Wait/Die implementation:
10  *  Copyright (C) 2013 Canonical Ltd.
11  * Choice of algorithm:
12  *  Copyright (C) 2018 WMWare Inc.
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 #include <linux/rtmutex.h>
22 
23 #if defined(CONFIG_DEBUG_MUTEXES) || \
24    (defined(CONFIG_PREEMPT_RT) && defined(CONFIG_DEBUG_RT_MUTEXES))
25 #define DEBUG_WW_MUTEXES
26 #endif
27 
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))
32 #else
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)
36 #endif
37 
38 struct ww_class {
39 	atomic_long_t stamp;
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;
45 };
46 
47 struct ww_mutex {
48 	struct WW_MUTEX_BASE base;
49 	struct ww_acquire_ctx *ctx;
50 #ifdef DEBUG_WW_MUTEXES
51 	struct ww_class *ww_class;
52 #endif
53 };
54 
55 struct ww_acquire_ctx {
56 	struct task_struct *task;
57 	unsigned long stamp;
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;
65 #endif
66 #ifdef CONFIG_DEBUG_LOCK_ALLOC
67 	struct lockdep_map dep_map;
68 	/**
69 	 * @first_lock_dep_map: fake lockdep_map for first locked ww_mutex.
70 	 *
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
75 	 * ww_acquire_fini().
76 	 */
77 	struct lockdep_map first_lock_dep_map;
78 #endif
79 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
80 	unsigned int deadlock_inject_interval;
81 	unsigned int deadlock_inject_countdown;
82 #endif
83 };
84 
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 }
90 
91 #define DEFINE_WD_CLASS(classname) \
92 	struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 1)
93 
94 #define DEFINE_WW_CLASS(classname) \
95 	struct ww_class classname = __WW_CLASS_INITIALIZER(classname, 0)
96 
97 /**
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
101  *
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.
105  *
106  * It is not allowed to initialize an already locked mutex.
107  */
ww_mutex_init(struct ww_mutex * lock,struct ww_class * ww_class)108 static inline void ww_mutex_init(struct ww_mutex *lock,
109 				 struct ww_class *ww_class)
110 {
111 	ww_mutex_base_init(&lock->base, ww_class->mutex_name, &ww_class->mutex_key);
112 	lock->ctx = NULL;
113 #ifdef DEBUG_WW_MUTEXES
114 	lock->ww_class = ww_class;
115 #endif
116 }
117 
118 /**
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
122  *
123  * Initializes an context to acquire multiple mutexes of the given w/w class.
124  *
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
127  * wait/die logic.
128  *
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.
134  *
135  * Nesting of acquire contexts for _different_ w/w classes is possible, subject
136  * to the usual locking rules between different lock classes.
137  *
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
140  * on the stack.
141  */
ww_acquire_init(struct ww_acquire_ctx * ctx,struct ww_class * ww_class)142 static inline void ww_acquire_init(struct ww_acquire_ctx *ctx,
143 				   struct ww_class *ww_class)
144 {
145 	ctx->task = current;
146 	ctx->stamp = atomic_long_inc_return_relaxed(&ww_class->stamp);
147 	ctx->acquired = 0;
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;
154 #endif
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_wait(&ctx->first_lock_dep_map, ww_class->mutex_name,
160 			      &ww_class->mutex_key, 0, LD_WAIT_SLEEP);
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_);
163 #endif
164 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
165 	ctx->deadlock_inject_interval = 1;
166 	ctx->deadlock_inject_countdown = ctx->stamp & 0xf;
167 #endif
168 }
169 
170 /**
171  * ww_acquire_done - marks the end of the acquire phase
172  * @ctx: the acquire context
173  *
174  * Marks the end of the acquire phase, any further w/w mutex lock calls using
175  * this context are forbidden.
176  *
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
179  * data structures.
180  */
ww_acquire_done(struct ww_acquire_ctx * ctx)181 static inline void ww_acquire_done(struct ww_acquire_ctx *ctx)
182 {
183 #ifdef DEBUG_WW_MUTEXES
184 	lockdep_assert_held(ctx);
185 
186 	DEBUG_LOCKS_WARN_ON(ctx->done_acquire);
187 	ctx->done_acquire = 1;
188 #endif
189 }
190 
191 /**
192  * ww_acquire_fini - releases a w/w acquire context
193  * @ctx: the acquire context to free
194  *
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.
197  */
ww_acquire_fini(struct ww_acquire_ctx * ctx)198 static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx)
199 {
200 #ifdef CONFIG_DEBUG_LOCK_ALLOC
201 	mutex_release(&ctx->first_lock_dep_map, _THIS_IP_);
202 	mutex_release(&ctx->dep_map, _THIS_IP_);
203 #endif
204 #ifdef DEBUG_WW_MUTEXES
205 	DEBUG_LOCKS_WARN_ON(ctx->acquired);
206 	if (!IS_ENABLED(CONFIG_PROVE_LOCKING))
207 		/*
208 		 * lockdep will normally handle this,
209 		 * but fail without anyway
210 		 */
211 		ctx->done_acquire = 1;
212 
213 	if (!IS_ENABLED(CONFIG_DEBUG_LOCK_ALLOC))
214 		/* ensure ww_acquire_fini will still fail if called twice */
215 		ctx->acquired = ~0U;
216 #endif
217 }
218 
219 /**
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.
223  *
224  * Lock the w/w mutex exclusively for this task.
225  *
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.
232  *
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).
238  *
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.
245  *
246  * A mutex acquired with this function must be released with ww_mutex_unlock.
247  */
248 extern int /* __must_check */ ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx);
249 
250 /**
251  * ww_mutex_lock_interruptible - acquire the w/w mutex, interruptible
252  * @lock: the mutex to be acquired
253  * @ctx: w/w acquire context
254  *
255  * Lock the w/w mutex exclusively for this task.
256  *
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.
264  *
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).
270  *
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.
277  *
278  * A mutex acquired with this function must be released with ww_mutex_unlock.
279  */
280 extern int __must_check ww_mutex_lock_interruptible(struct ww_mutex *lock,
281 						    struct ww_acquire_ctx *ctx);
282 
283 /**
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
287  *
288  * Acquires a w/w mutex with the given context after a die case. This function
289  * will sleep until the lock becomes available.
290  *
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.
293  *
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.
297  *
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
300  * contending mutex.
301  *
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.
305  */
306 static inline void
ww_mutex_lock_slow(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)307 ww_mutex_lock_slow(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
308 {
309 	int ret;
310 #ifdef DEBUG_WW_MUTEXES
311 	DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
312 #endif
313 	ret = ww_mutex_lock(lock, ctx);
314 	(void)ret;
315 }
316 
317 /**
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
321  *
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.
326  *
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.
329  *
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.
333  *
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.
337  *
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.
341  */
342 static inline int __must_check
ww_mutex_lock_slow_interruptible(struct ww_mutex * lock,struct ww_acquire_ctx * ctx)343 ww_mutex_lock_slow_interruptible(struct ww_mutex *lock,
344 				 struct ww_acquire_ctx *ctx)
345 {
346 #ifdef DEBUG_WW_MUTEXES
347 	DEBUG_LOCKS_WARN_ON(!ctx->contending_lock);
348 #endif
349 	return ww_mutex_lock_interruptible(lock, ctx);
350 }
351 
352 extern void ww_mutex_unlock(struct ww_mutex *lock);
353 
354 extern int __must_check ww_mutex_trylock(struct ww_mutex *lock,
355 					 struct ww_acquire_ctx *ctx);
356 
357 /***
358  * ww_mutex_destroy - mark a w/w mutex unusable
359  * @lock: the mutex to be destroyed
360  *
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.
364  */
ww_mutex_destroy(struct ww_mutex * lock)365 static inline void ww_mutex_destroy(struct ww_mutex *lock)
366 {
367 #ifndef CONFIG_PREEMPT_RT
368 	mutex_destroy(&lock->base);
369 #endif
370 }
371 
372 /**
373  * ww_mutex_is_locked - is the w/w mutex locked
374  * @lock: the mutex to be queried
375  *
376  * Returns 1 if the mutex is locked, 0 if unlocked.
377  */
ww_mutex_is_locked(struct ww_mutex * lock)378 static inline bool ww_mutex_is_locked(struct ww_mutex *lock)
379 {
380 	return ww_mutex_base_is_locked(&lock->base);
381 }
382 
383 #endif
384