xref: /openbmc/linux/kernel/locking/mutex.c (revision 7f35e63d)
1 /*
2  * kernel/locking/mutex.c
3  *
4  * Mutexes: blocking mutual exclusion locks
5  *
6  * Started by Ingo Molnar:
7  *
8  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
9  *
10  * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
11  * David Howells for suggestions and improvements.
12  *
13  *  - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
14  *    from the -rt tree, where it was originally implemented for rtmutexes
15  *    by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
16  *    and Sven Dietrich.
17  *
18  * Also see Documentation/locking/mutex-design.txt.
19  */
20 #include <linux/mutex.h>
21 #include <linux/ww_mutex.h>
22 #include <linux/sched/signal.h>
23 #include <linux/sched/rt.h>
24 #include <linux/sched/wake_q.h>
25 #include <linux/sched/debug.h>
26 #include <linux/export.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/debug_locks.h>
30 #include <linux/osq_lock.h>
31 
32 #ifdef CONFIG_DEBUG_MUTEXES
33 # include "mutex-debug.h"
34 #else
35 # include "mutex.h"
36 #endif
37 
38 void
39 __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
40 {
41 	atomic_long_set(&lock->owner, 0);
42 	spin_lock_init(&lock->wait_lock);
43 	INIT_LIST_HEAD(&lock->wait_list);
44 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
45 	osq_lock_init(&lock->osq);
46 #endif
47 
48 	debug_mutex_init(lock, name, key);
49 }
50 EXPORT_SYMBOL(__mutex_init);
51 
52 /*
53  * @owner: contains: 'struct task_struct *' to the current lock owner,
54  * NULL means not owned. Since task_struct pointers are aligned at
55  * at least L1_CACHE_BYTES, we have low bits to store extra state.
56  *
57  * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
58  * Bit1 indicates unlock needs to hand the lock to the top-waiter
59  * Bit2 indicates handoff has been done and we're waiting for pickup.
60  */
61 #define MUTEX_FLAG_WAITERS	0x01
62 #define MUTEX_FLAG_HANDOFF	0x02
63 #define MUTEX_FLAG_PICKUP	0x04
64 
65 #define MUTEX_FLAGS		0x07
66 
67 static inline struct task_struct *__owner_task(unsigned long owner)
68 {
69 	return (struct task_struct *)(owner & ~MUTEX_FLAGS);
70 }
71 
72 static inline unsigned long __owner_flags(unsigned long owner)
73 {
74 	return owner & MUTEX_FLAGS;
75 }
76 
77 /*
78  * Trylock variant that retuns the owning task on failure.
79  */
80 static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
81 {
82 	unsigned long owner, curr = (unsigned long)current;
83 
84 	owner = atomic_long_read(&lock->owner);
85 	for (;;) { /* must loop, can race against a flag */
86 		unsigned long old, flags = __owner_flags(owner);
87 		unsigned long task = owner & ~MUTEX_FLAGS;
88 
89 		if (task) {
90 			if (likely(task != curr))
91 				break;
92 
93 			if (likely(!(flags & MUTEX_FLAG_PICKUP)))
94 				break;
95 
96 			flags &= ~MUTEX_FLAG_PICKUP;
97 		} else {
98 #ifdef CONFIG_DEBUG_MUTEXES
99 			DEBUG_LOCKS_WARN_ON(flags & MUTEX_FLAG_PICKUP);
100 #endif
101 		}
102 
103 		/*
104 		 * We set the HANDOFF bit, we must make sure it doesn't live
105 		 * past the point where we acquire it. This would be possible
106 		 * if we (accidentally) set the bit on an unlocked mutex.
107 		 */
108 		flags &= ~MUTEX_FLAG_HANDOFF;
109 
110 		old = atomic_long_cmpxchg_acquire(&lock->owner, owner, curr | flags);
111 		if (old == owner)
112 			return NULL;
113 
114 		owner = old;
115 	}
116 
117 	return __owner_task(owner);
118 }
119 
120 /*
121  * Actual trylock that will work on any unlocked state.
122  */
123 static inline bool __mutex_trylock(struct mutex *lock)
124 {
125 	return !__mutex_trylock_or_owner(lock);
126 }
127 
128 #ifndef CONFIG_DEBUG_LOCK_ALLOC
129 /*
130  * Lockdep annotations are contained to the slow paths for simplicity.
131  * There is nothing that would stop spreading the lockdep annotations outwards
132  * except more code.
133  */
134 
135 /*
136  * Optimistic trylock that only works in the uncontended case. Make sure to
137  * follow with a __mutex_trylock() before failing.
138  */
139 static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
140 {
141 	unsigned long curr = (unsigned long)current;
142 	unsigned long zero = 0UL;
143 
144 	if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
145 		return true;
146 
147 	return false;
148 }
149 
150 static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
151 {
152 	unsigned long curr = (unsigned long)current;
153 
154 	if (atomic_long_cmpxchg_release(&lock->owner, curr, 0UL) == curr)
155 		return true;
156 
157 	return false;
158 }
159 #endif
160 
161 static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
162 {
163 	atomic_long_or(flag, &lock->owner);
164 }
165 
166 static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
167 {
168 	atomic_long_andnot(flag, &lock->owner);
169 }
170 
171 static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter)
172 {
173 	return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter;
174 }
175 
176 /*
177  * Give up ownership to a specific task, when @task = NULL, this is equivalent
178  * to a regular unlock. Sets PICKUP on a handoff, clears HANDOF, preserves
179  * WAITERS. Provides RELEASE semantics like a regular unlock, the
180  * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
181  */
182 static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
183 {
184 	unsigned long owner = atomic_long_read(&lock->owner);
185 
186 	for (;;) {
187 		unsigned long old, new;
188 
189 #ifdef CONFIG_DEBUG_MUTEXES
190 		DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current);
191 		DEBUG_LOCKS_WARN_ON(owner & MUTEX_FLAG_PICKUP);
192 #endif
193 
194 		new = (owner & MUTEX_FLAG_WAITERS);
195 		new |= (unsigned long)task;
196 		if (task)
197 			new |= MUTEX_FLAG_PICKUP;
198 
199 		old = atomic_long_cmpxchg_release(&lock->owner, owner, new);
200 		if (old == owner)
201 			break;
202 
203 		owner = old;
204 	}
205 }
206 
207 #ifndef CONFIG_DEBUG_LOCK_ALLOC
208 /*
209  * We split the mutex lock/unlock logic into separate fastpath and
210  * slowpath functions, to reduce the register pressure on the fastpath.
211  * We also put the fastpath first in the kernel image, to make sure the
212  * branch is predicted by the CPU as default-untaken.
213  */
214 static void __sched __mutex_lock_slowpath(struct mutex *lock);
215 
216 /**
217  * mutex_lock - acquire the mutex
218  * @lock: the mutex to be acquired
219  *
220  * Lock the mutex exclusively for this task. If the mutex is not
221  * available right now, it will sleep until it can get it.
222  *
223  * The mutex must later on be released by the same task that
224  * acquired it. Recursive locking is not allowed. The task
225  * may not exit without first unlocking the mutex. Also, kernel
226  * memory where the mutex resides must not be freed with
227  * the mutex still locked. The mutex must first be initialized
228  * (or statically defined) before it can be locked. memset()-ing
229  * the mutex to 0 is not allowed.
230  *
231  * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging
232  * checks that will enforce the restrictions and will also do
233  * deadlock debugging)
234  *
235  * This function is similar to (but not equivalent to) down().
236  */
237 void __sched mutex_lock(struct mutex *lock)
238 {
239 	might_sleep();
240 
241 	if (!__mutex_trylock_fast(lock))
242 		__mutex_lock_slowpath(lock);
243 }
244 EXPORT_SYMBOL(mutex_lock);
245 #endif
246 
247 static __always_inline void
248 ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
249 {
250 #ifdef CONFIG_DEBUG_MUTEXES
251 	/*
252 	 * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
253 	 * but released with a normal mutex_unlock in this call.
254 	 *
255 	 * This should never happen, always use ww_mutex_unlock.
256 	 */
257 	DEBUG_LOCKS_WARN_ON(ww->ctx);
258 
259 	/*
260 	 * Not quite done after calling ww_acquire_done() ?
261 	 */
262 	DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
263 
264 	if (ww_ctx->contending_lock) {
265 		/*
266 		 * After -EDEADLK you tried to
267 		 * acquire a different ww_mutex? Bad!
268 		 */
269 		DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
270 
271 		/*
272 		 * You called ww_mutex_lock after receiving -EDEADLK,
273 		 * but 'forgot' to unlock everything else first?
274 		 */
275 		DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
276 		ww_ctx->contending_lock = NULL;
277 	}
278 
279 	/*
280 	 * Naughty, using a different class will lead to undefined behavior!
281 	 */
282 	DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
283 #endif
284 	ww_ctx->acquired++;
285 }
286 
287 static inline bool __sched
288 __ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
289 {
290 	return a->stamp - b->stamp <= LONG_MAX &&
291 	       (a->stamp != b->stamp || a > b);
292 }
293 
294 /*
295  * Wake up any waiters that may have to back off when the lock is held by the
296  * given context.
297  *
298  * Due to the invariants on the wait list, this can only affect the first
299  * waiter with a context.
300  *
301  * The current task must not be on the wait list.
302  */
303 static void __sched
304 __ww_mutex_wakeup_for_backoff(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
305 {
306 	struct mutex_waiter *cur;
307 
308 	lockdep_assert_held(&lock->wait_lock);
309 
310 	list_for_each_entry(cur, &lock->wait_list, list) {
311 		if (!cur->ww_ctx)
312 			continue;
313 
314 		if (cur->ww_ctx->acquired > 0 &&
315 		    __ww_ctx_stamp_after(cur->ww_ctx, ww_ctx)) {
316 			debug_mutex_wake_waiter(lock, cur);
317 			wake_up_process(cur->task);
318 		}
319 
320 		break;
321 	}
322 }
323 
324 /*
325  * After acquiring lock with fastpath or when we lost out in contested
326  * slowpath, set ctx and wake up any waiters so they can recheck.
327  */
328 static __always_inline void
329 ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
330 {
331 	ww_mutex_lock_acquired(lock, ctx);
332 
333 	lock->ctx = ctx;
334 
335 	/*
336 	 * The lock->ctx update should be visible on all cores before
337 	 * the atomic read is done, otherwise contended waiters might be
338 	 * missed. The contended waiters will either see ww_ctx == NULL
339 	 * and keep spinning, or it will acquire wait_lock, add itself
340 	 * to waiter list and sleep.
341 	 */
342 	smp_mb(); /* ^^^ */
343 
344 	/*
345 	 * Check if lock is contended, if not there is nobody to wake up
346 	 */
347 	if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS)))
348 		return;
349 
350 	/*
351 	 * Uh oh, we raced in fastpath, wake up everyone in this case,
352 	 * so they can see the new lock->ctx.
353 	 */
354 	spin_lock(&lock->base.wait_lock);
355 	__ww_mutex_wakeup_for_backoff(&lock->base, ctx);
356 	spin_unlock(&lock->base.wait_lock);
357 }
358 
359 /*
360  * After acquiring lock in the slowpath set ctx.
361  *
362  * Unlike for the fast path, the caller ensures that waiters are woken up where
363  * necessary.
364  *
365  * Callers must hold the mutex wait_lock.
366  */
367 static __always_inline void
368 ww_mutex_set_context_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
369 {
370 	ww_mutex_lock_acquired(lock, ctx);
371 	lock->ctx = ctx;
372 }
373 
374 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
375 
376 static inline
377 bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
378 			    struct mutex_waiter *waiter)
379 {
380 	struct ww_mutex *ww;
381 
382 	ww = container_of(lock, struct ww_mutex, base);
383 
384 	/*
385 	 * If ww->ctx is set the contents are undefined, only
386 	 * by acquiring wait_lock there is a guarantee that
387 	 * they are not invalid when reading.
388 	 *
389 	 * As such, when deadlock detection needs to be
390 	 * performed the optimistic spinning cannot be done.
391 	 *
392 	 * Check this in every inner iteration because we may
393 	 * be racing against another thread's ww_mutex_lock.
394 	 */
395 	if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
396 		return false;
397 
398 	/*
399 	 * If we aren't on the wait list yet, cancel the spin
400 	 * if there are waiters. We want  to avoid stealing the
401 	 * lock from a waiter with an earlier stamp, since the
402 	 * other thread may already own a lock that we also
403 	 * need.
404 	 */
405 	if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
406 		return false;
407 
408 	/*
409 	 * Similarly, stop spinning if we are no longer the
410 	 * first waiter.
411 	 */
412 	if (waiter && !__mutex_waiter_is_first(lock, waiter))
413 		return false;
414 
415 	return true;
416 }
417 
418 /*
419  * Look out! "owner" is an entirely speculative pointer access and not
420  * reliable.
421  *
422  * "noinline" so that this function shows up on perf profiles.
423  */
424 static noinline
425 bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
426 			 struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
427 {
428 	bool ret = true;
429 
430 	rcu_read_lock();
431 	while (__mutex_owner(lock) == owner) {
432 		/*
433 		 * Ensure we emit the owner->on_cpu, dereference _after_
434 		 * checking lock->owner still matches owner. If that fails,
435 		 * owner might point to freed memory. If it still matches,
436 		 * the rcu_read_lock() ensures the memory stays valid.
437 		 */
438 		barrier();
439 
440 		/*
441 		 * Use vcpu_is_preempted to detect lock holder preemption issue.
442 		 */
443 		if (!owner->on_cpu || need_resched() ||
444 				vcpu_is_preempted(task_cpu(owner))) {
445 			ret = false;
446 			break;
447 		}
448 
449 		if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
450 			ret = false;
451 			break;
452 		}
453 
454 		cpu_relax();
455 	}
456 	rcu_read_unlock();
457 
458 	return ret;
459 }
460 
461 /*
462  * Initial check for entering the mutex spinning loop
463  */
464 static inline int mutex_can_spin_on_owner(struct mutex *lock)
465 {
466 	struct task_struct *owner;
467 	int retval = 1;
468 
469 	if (need_resched())
470 		return 0;
471 
472 	rcu_read_lock();
473 	owner = __mutex_owner(lock);
474 
475 	/*
476 	 * As lock holder preemption issue, we both skip spinning if task is not
477 	 * on cpu or its cpu is preempted
478 	 */
479 	if (owner)
480 		retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
481 	rcu_read_unlock();
482 
483 	/*
484 	 * If lock->owner is not set, the mutex has been released. Return true
485 	 * such that we'll trylock in the spin path, which is a faster option
486 	 * than the blocking slow path.
487 	 */
488 	return retval;
489 }
490 
491 /*
492  * Optimistic spinning.
493  *
494  * We try to spin for acquisition when we find that the lock owner
495  * is currently running on a (different) CPU and while we don't
496  * need to reschedule. The rationale is that if the lock owner is
497  * running, it is likely to release the lock soon.
498  *
499  * The mutex spinners are queued up using MCS lock so that only one
500  * spinner can compete for the mutex. However, if mutex spinning isn't
501  * going to happen, there is no point in going through the lock/unlock
502  * overhead.
503  *
504  * Returns true when the lock was taken, otherwise false, indicating
505  * that we need to jump to the slowpath and sleep.
506  *
507  * The waiter flag is set to true if the spinner is a waiter in the wait
508  * queue. The waiter-spinner will spin on the lock directly and concurrently
509  * with the spinner at the head of the OSQ, if present, until the owner is
510  * changed to itself.
511  */
512 static __always_inline bool
513 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
514 		      const bool use_ww_ctx, struct mutex_waiter *waiter)
515 {
516 	if (!waiter) {
517 		/*
518 		 * The purpose of the mutex_can_spin_on_owner() function is
519 		 * to eliminate the overhead of osq_lock() and osq_unlock()
520 		 * in case spinning isn't possible. As a waiter-spinner
521 		 * is not going to take OSQ lock anyway, there is no need
522 		 * to call mutex_can_spin_on_owner().
523 		 */
524 		if (!mutex_can_spin_on_owner(lock))
525 			goto fail;
526 
527 		/*
528 		 * In order to avoid a stampede of mutex spinners trying to
529 		 * acquire the mutex all at once, the spinners need to take a
530 		 * MCS (queued) lock first before spinning on the owner field.
531 		 */
532 		if (!osq_lock(&lock->osq))
533 			goto fail;
534 	}
535 
536 	for (;;) {
537 		struct task_struct *owner;
538 
539 		/* Try to acquire the mutex... */
540 		owner = __mutex_trylock_or_owner(lock);
541 		if (!owner)
542 			break;
543 
544 		/*
545 		 * There's an owner, wait for it to either
546 		 * release the lock or go to sleep.
547 		 */
548 		if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
549 			goto fail_unlock;
550 
551 		/*
552 		 * The cpu_relax() call is a compiler barrier which forces
553 		 * everything in this loop to be re-loaded. We don't need
554 		 * memory barriers as we'll eventually observe the right
555 		 * values at the cost of a few extra spins.
556 		 */
557 		cpu_relax();
558 	}
559 
560 	if (!waiter)
561 		osq_unlock(&lock->osq);
562 
563 	return true;
564 
565 
566 fail_unlock:
567 	if (!waiter)
568 		osq_unlock(&lock->osq);
569 
570 fail:
571 	/*
572 	 * If we fell out of the spin path because of need_resched(),
573 	 * reschedule now, before we try-lock the mutex. This avoids getting
574 	 * scheduled out right after we obtained the mutex.
575 	 */
576 	if (need_resched()) {
577 		/*
578 		 * We _should_ have TASK_RUNNING here, but just in case
579 		 * we do not, make it so, otherwise we might get stuck.
580 		 */
581 		__set_current_state(TASK_RUNNING);
582 		schedule_preempt_disabled();
583 	}
584 
585 	return false;
586 }
587 #else
588 static __always_inline bool
589 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
590 		      const bool use_ww_ctx, struct mutex_waiter *waiter)
591 {
592 	return false;
593 }
594 #endif
595 
596 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
597 
598 /**
599  * mutex_unlock - release the mutex
600  * @lock: the mutex to be released
601  *
602  * Unlock a mutex that has been locked by this task previously.
603  *
604  * This function must not be used in interrupt context. Unlocking
605  * of a not locked mutex is not allowed.
606  *
607  * This function is similar to (but not equivalent to) up().
608  */
609 void __sched mutex_unlock(struct mutex *lock)
610 {
611 #ifndef CONFIG_DEBUG_LOCK_ALLOC
612 	if (__mutex_unlock_fast(lock))
613 		return;
614 #endif
615 	__mutex_unlock_slowpath(lock, _RET_IP_);
616 }
617 EXPORT_SYMBOL(mutex_unlock);
618 
619 /**
620  * ww_mutex_unlock - release the w/w mutex
621  * @lock: the mutex to be released
622  *
623  * Unlock a mutex that has been locked by this task previously with any of the
624  * ww_mutex_lock* functions (with or without an acquire context). It is
625  * forbidden to release the locks after releasing the acquire context.
626  *
627  * This function must not be used in interrupt context. Unlocking
628  * of a unlocked mutex is not allowed.
629  */
630 void __sched ww_mutex_unlock(struct ww_mutex *lock)
631 {
632 	/*
633 	 * The unlocking fastpath is the 0->1 transition from 'locked'
634 	 * into 'unlocked' state:
635 	 */
636 	if (lock->ctx) {
637 #ifdef CONFIG_DEBUG_MUTEXES
638 		DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
639 #endif
640 		if (lock->ctx->acquired > 0)
641 			lock->ctx->acquired--;
642 		lock->ctx = NULL;
643 	}
644 
645 	mutex_unlock(&lock->base);
646 }
647 EXPORT_SYMBOL(ww_mutex_unlock);
648 
649 static inline int __sched
650 __ww_mutex_lock_check_stamp(struct mutex *lock, struct mutex_waiter *waiter,
651 			    struct ww_acquire_ctx *ctx)
652 {
653 	struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
654 	struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);
655 	struct mutex_waiter *cur;
656 
657 	if (hold_ctx && __ww_ctx_stamp_after(ctx, hold_ctx))
658 		goto deadlock;
659 
660 	/*
661 	 * If there is a waiter in front of us that has a context, then its
662 	 * stamp is earlier than ours and we must back off.
663 	 */
664 	cur = waiter;
665 	list_for_each_entry_continue_reverse(cur, &lock->wait_list, list) {
666 		if (cur->ww_ctx)
667 			goto deadlock;
668 	}
669 
670 	return 0;
671 
672 deadlock:
673 #ifdef CONFIG_DEBUG_MUTEXES
674 	DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
675 	ctx->contending_lock = ww;
676 #endif
677 	return -EDEADLK;
678 }
679 
680 static inline int __sched
681 __ww_mutex_add_waiter(struct mutex_waiter *waiter,
682 		      struct mutex *lock,
683 		      struct ww_acquire_ctx *ww_ctx)
684 {
685 	struct mutex_waiter *cur;
686 	struct list_head *pos;
687 
688 	if (!ww_ctx) {
689 		list_add_tail(&waiter->list, &lock->wait_list);
690 		return 0;
691 	}
692 
693 	/*
694 	 * Add the waiter before the first waiter with a higher stamp.
695 	 * Waiters without a context are skipped to avoid starving
696 	 * them.
697 	 */
698 	pos = &lock->wait_list;
699 	list_for_each_entry_reverse(cur, &lock->wait_list, list) {
700 		if (!cur->ww_ctx)
701 			continue;
702 
703 		if (__ww_ctx_stamp_after(ww_ctx, cur->ww_ctx)) {
704 			/* Back off immediately if necessary. */
705 			if (ww_ctx->acquired > 0) {
706 #ifdef CONFIG_DEBUG_MUTEXES
707 				struct ww_mutex *ww;
708 
709 				ww = container_of(lock, struct ww_mutex, base);
710 				DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock);
711 				ww_ctx->contending_lock = ww;
712 #endif
713 				return -EDEADLK;
714 			}
715 
716 			break;
717 		}
718 
719 		pos = &cur->list;
720 
721 		/*
722 		 * Wake up the waiter so that it gets a chance to back
723 		 * off.
724 		 */
725 		if (cur->ww_ctx->acquired > 0) {
726 			debug_mutex_wake_waiter(lock, cur);
727 			wake_up_process(cur->task);
728 		}
729 	}
730 
731 	list_add_tail(&waiter->list, pos);
732 	return 0;
733 }
734 
735 /*
736  * Lock a mutex (possibly interruptible), slowpath:
737  */
738 static __always_inline int __sched
739 __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
740 		    struct lockdep_map *nest_lock, unsigned long ip,
741 		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
742 {
743 	struct mutex_waiter waiter;
744 	bool first = false;
745 	struct ww_mutex *ww;
746 	int ret;
747 
748 	might_sleep();
749 
750 	ww = container_of(lock, struct ww_mutex, base);
751 	if (use_ww_ctx && ww_ctx) {
752 		if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
753 			return -EALREADY;
754 	}
755 
756 	preempt_disable();
757 	mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
758 
759 	if (__mutex_trylock(lock) ||
760 	    mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, NULL)) {
761 		/* got the lock, yay! */
762 		lock_acquired(&lock->dep_map, ip);
763 		if (use_ww_ctx && ww_ctx)
764 			ww_mutex_set_context_fastpath(ww, ww_ctx);
765 		preempt_enable();
766 		return 0;
767 	}
768 
769 	spin_lock(&lock->wait_lock);
770 	/*
771 	 * After waiting to acquire the wait_lock, try again.
772 	 */
773 	if (__mutex_trylock(lock)) {
774 		if (use_ww_ctx && ww_ctx)
775 			__ww_mutex_wakeup_for_backoff(lock, ww_ctx);
776 
777 		goto skip_wait;
778 	}
779 
780 	debug_mutex_lock_common(lock, &waiter);
781 	debug_mutex_add_waiter(lock, &waiter, current);
782 
783 	lock_contended(&lock->dep_map, ip);
784 
785 	if (!use_ww_ctx) {
786 		/* add waiting tasks to the end of the waitqueue (FIFO): */
787 		list_add_tail(&waiter.list, &lock->wait_list);
788 
789 #ifdef CONFIG_DEBUG_MUTEXES
790 		waiter.ww_ctx = MUTEX_POISON_WW_CTX;
791 #endif
792 	} else {
793 		/* Add in stamp order, waking up waiters that must back off. */
794 		ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
795 		if (ret)
796 			goto err_early_backoff;
797 
798 		waiter.ww_ctx = ww_ctx;
799 	}
800 
801 	waiter.task = current;
802 
803 	if (__mutex_waiter_is_first(lock, &waiter))
804 		__mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
805 
806 	set_current_state(state);
807 	for (;;) {
808 		/*
809 		 * Once we hold wait_lock, we're serialized against
810 		 * mutex_unlock() handing the lock off to us, do a trylock
811 		 * before testing the error conditions to make sure we pick up
812 		 * the handoff.
813 		 */
814 		if (__mutex_trylock(lock))
815 			goto acquired;
816 
817 		/*
818 		 * Check for signals and wound conditions while holding
819 		 * wait_lock. This ensures the lock cancellation is ordered
820 		 * against mutex_unlock() and wake-ups do not go missing.
821 		 */
822 		if (unlikely(signal_pending_state(state, current))) {
823 			ret = -EINTR;
824 			goto err;
825 		}
826 
827 		if (use_ww_ctx && ww_ctx && ww_ctx->acquired > 0) {
828 			ret = __ww_mutex_lock_check_stamp(lock, &waiter, ww_ctx);
829 			if (ret)
830 				goto err;
831 		}
832 
833 		spin_unlock(&lock->wait_lock);
834 		schedule_preempt_disabled();
835 
836 		/*
837 		 * ww_mutex needs to always recheck its position since its waiter
838 		 * list is not FIFO ordered.
839 		 */
840 		if ((use_ww_ctx && ww_ctx) || !first) {
841 			first = __mutex_waiter_is_first(lock, &waiter);
842 			if (first)
843 				__mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
844 		}
845 
846 		set_current_state(state);
847 		/*
848 		 * Here we order against unlock; we must either see it change
849 		 * state back to RUNNING and fall through the next schedule(),
850 		 * or we must see its unlock and acquire.
851 		 */
852 		if (__mutex_trylock(lock) ||
853 		    (first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, &waiter)))
854 			break;
855 
856 		spin_lock(&lock->wait_lock);
857 	}
858 	spin_lock(&lock->wait_lock);
859 acquired:
860 	__set_current_state(TASK_RUNNING);
861 
862 	mutex_remove_waiter(lock, &waiter, current);
863 	if (likely(list_empty(&lock->wait_list)))
864 		__mutex_clear_flag(lock, MUTEX_FLAGS);
865 
866 	debug_mutex_free_waiter(&waiter);
867 
868 skip_wait:
869 	/* got the lock - cleanup and rejoice! */
870 	lock_acquired(&lock->dep_map, ip);
871 
872 	if (use_ww_ctx && ww_ctx)
873 		ww_mutex_set_context_slowpath(ww, ww_ctx);
874 
875 	spin_unlock(&lock->wait_lock);
876 	preempt_enable();
877 	return 0;
878 
879 err:
880 	__set_current_state(TASK_RUNNING);
881 	mutex_remove_waiter(lock, &waiter, current);
882 err_early_backoff:
883 	spin_unlock(&lock->wait_lock);
884 	debug_mutex_free_waiter(&waiter);
885 	mutex_release(&lock->dep_map, 1, ip);
886 	preempt_enable();
887 	return ret;
888 }
889 
890 static int __sched
891 __mutex_lock(struct mutex *lock, long state, unsigned int subclass,
892 	     struct lockdep_map *nest_lock, unsigned long ip)
893 {
894 	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
895 }
896 
897 static int __sched
898 __ww_mutex_lock(struct mutex *lock, long state, unsigned int subclass,
899 		struct lockdep_map *nest_lock, unsigned long ip,
900 		struct ww_acquire_ctx *ww_ctx)
901 {
902 	return __mutex_lock_common(lock, state, subclass, nest_lock, ip, ww_ctx, true);
903 }
904 
905 #ifdef CONFIG_DEBUG_LOCK_ALLOC
906 void __sched
907 mutex_lock_nested(struct mutex *lock, unsigned int subclass)
908 {
909 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
910 }
911 
912 EXPORT_SYMBOL_GPL(mutex_lock_nested);
913 
914 void __sched
915 _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
916 {
917 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
918 }
919 EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
920 
921 int __sched
922 mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
923 {
924 	return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
925 }
926 EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
927 
928 int __sched
929 mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
930 {
931 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
932 }
933 EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
934 
935 void __sched
936 mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
937 {
938 	int token;
939 
940 	might_sleep();
941 
942 	token = io_schedule_prepare();
943 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
944 			    subclass, NULL, _RET_IP_, NULL, 0);
945 	io_schedule_finish(token);
946 }
947 EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
948 
949 static inline int
950 ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
951 {
952 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
953 	unsigned tmp;
954 
955 	if (ctx->deadlock_inject_countdown-- == 0) {
956 		tmp = ctx->deadlock_inject_interval;
957 		if (tmp > UINT_MAX/4)
958 			tmp = UINT_MAX;
959 		else
960 			tmp = tmp*2 + tmp + tmp/2;
961 
962 		ctx->deadlock_inject_interval = tmp;
963 		ctx->deadlock_inject_countdown = tmp;
964 		ctx->contending_lock = lock;
965 
966 		ww_mutex_unlock(lock);
967 
968 		return -EDEADLK;
969 	}
970 #endif
971 
972 	return 0;
973 }
974 
975 int __sched
976 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
977 {
978 	int ret;
979 
980 	might_sleep();
981 	ret =  __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
982 			       0, ctx ? &ctx->dep_map : NULL, _RET_IP_,
983 			       ctx);
984 	if (!ret && ctx && ctx->acquired > 1)
985 		return ww_mutex_deadlock_injection(lock, ctx);
986 
987 	return ret;
988 }
989 EXPORT_SYMBOL_GPL(ww_mutex_lock);
990 
991 int __sched
992 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
993 {
994 	int ret;
995 
996 	might_sleep();
997 	ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
998 			      0, ctx ? &ctx->dep_map : NULL, _RET_IP_,
999 			      ctx);
1000 
1001 	if (!ret && ctx && ctx->acquired > 1)
1002 		return ww_mutex_deadlock_injection(lock, ctx);
1003 
1004 	return ret;
1005 }
1006 EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
1007 
1008 #endif
1009 
1010 /*
1011  * Release the lock, slowpath:
1012  */
1013 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
1014 {
1015 	struct task_struct *next = NULL;
1016 	DEFINE_WAKE_Q(wake_q);
1017 	unsigned long owner;
1018 
1019 	mutex_release(&lock->dep_map, 1, ip);
1020 
1021 	/*
1022 	 * Release the lock before (potentially) taking the spinlock such that
1023 	 * other contenders can get on with things ASAP.
1024 	 *
1025 	 * Except when HANDOFF, in that case we must not clear the owner field,
1026 	 * but instead set it to the top waiter.
1027 	 */
1028 	owner = atomic_long_read(&lock->owner);
1029 	for (;;) {
1030 		unsigned long old;
1031 
1032 #ifdef CONFIG_DEBUG_MUTEXES
1033 		DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current);
1034 		DEBUG_LOCKS_WARN_ON(owner & MUTEX_FLAG_PICKUP);
1035 #endif
1036 
1037 		if (owner & MUTEX_FLAG_HANDOFF)
1038 			break;
1039 
1040 		old = atomic_long_cmpxchg_release(&lock->owner, owner,
1041 						  __owner_flags(owner));
1042 		if (old == owner) {
1043 			if (owner & MUTEX_FLAG_WAITERS)
1044 				break;
1045 
1046 			return;
1047 		}
1048 
1049 		owner = old;
1050 	}
1051 
1052 	spin_lock(&lock->wait_lock);
1053 	debug_mutex_unlock(lock);
1054 	if (!list_empty(&lock->wait_list)) {
1055 		/* get the first entry from the wait-list: */
1056 		struct mutex_waiter *waiter =
1057 			list_first_entry(&lock->wait_list,
1058 					 struct mutex_waiter, list);
1059 
1060 		next = waiter->task;
1061 
1062 		debug_mutex_wake_waiter(lock, waiter);
1063 		wake_q_add(&wake_q, next);
1064 	}
1065 
1066 	if (owner & MUTEX_FLAG_HANDOFF)
1067 		__mutex_handoff(lock, next);
1068 
1069 	spin_unlock(&lock->wait_lock);
1070 
1071 	wake_up_q(&wake_q);
1072 }
1073 
1074 #ifndef CONFIG_DEBUG_LOCK_ALLOC
1075 /*
1076  * Here come the less common (and hence less performance-critical) APIs:
1077  * mutex_lock_interruptible() and mutex_trylock().
1078  */
1079 static noinline int __sched
1080 __mutex_lock_killable_slowpath(struct mutex *lock);
1081 
1082 static noinline int __sched
1083 __mutex_lock_interruptible_slowpath(struct mutex *lock);
1084 
1085 /**
1086  * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
1087  * @lock: The mutex to be acquired.
1088  *
1089  * Lock the mutex like mutex_lock().  If a signal is delivered while the
1090  * process is sleeping, this function will return without acquiring the
1091  * mutex.
1092  *
1093  * Context: Process context.
1094  * Return: 0 if the lock was successfully acquired or %-EINTR if a
1095  * signal arrived.
1096  */
1097 int __sched mutex_lock_interruptible(struct mutex *lock)
1098 {
1099 	might_sleep();
1100 
1101 	if (__mutex_trylock_fast(lock))
1102 		return 0;
1103 
1104 	return __mutex_lock_interruptible_slowpath(lock);
1105 }
1106 
1107 EXPORT_SYMBOL(mutex_lock_interruptible);
1108 
1109 /**
1110  * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
1111  * @lock: The mutex to be acquired.
1112  *
1113  * Lock the mutex like mutex_lock().  If a signal which will be fatal to
1114  * the current process is delivered while the process is sleeping, this
1115  * function will return without acquiring the mutex.
1116  *
1117  * Context: Process context.
1118  * Return: 0 if the lock was successfully acquired or %-EINTR if a
1119  * fatal signal arrived.
1120  */
1121 int __sched mutex_lock_killable(struct mutex *lock)
1122 {
1123 	might_sleep();
1124 
1125 	if (__mutex_trylock_fast(lock))
1126 		return 0;
1127 
1128 	return __mutex_lock_killable_slowpath(lock);
1129 }
1130 EXPORT_SYMBOL(mutex_lock_killable);
1131 
1132 /**
1133  * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
1134  * @lock: The mutex to be acquired.
1135  *
1136  * Lock the mutex like mutex_lock().  While the task is waiting for this
1137  * mutex, it will be accounted as being in the IO wait state by the
1138  * scheduler.
1139  *
1140  * Context: Process context.
1141  */
1142 void __sched mutex_lock_io(struct mutex *lock)
1143 {
1144 	int token;
1145 
1146 	token = io_schedule_prepare();
1147 	mutex_lock(lock);
1148 	io_schedule_finish(token);
1149 }
1150 EXPORT_SYMBOL_GPL(mutex_lock_io);
1151 
1152 static noinline void __sched
1153 __mutex_lock_slowpath(struct mutex *lock)
1154 {
1155 	__mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
1156 }
1157 
1158 static noinline int __sched
1159 __mutex_lock_killable_slowpath(struct mutex *lock)
1160 {
1161 	return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
1162 }
1163 
1164 static noinline int __sched
1165 __mutex_lock_interruptible_slowpath(struct mutex *lock)
1166 {
1167 	return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
1168 }
1169 
1170 static noinline int __sched
1171 __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1172 {
1173 	return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0, NULL,
1174 			       _RET_IP_, ctx);
1175 }
1176 
1177 static noinline int __sched
1178 __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
1179 					    struct ww_acquire_ctx *ctx)
1180 {
1181 	return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0, NULL,
1182 			       _RET_IP_, ctx);
1183 }
1184 
1185 #endif
1186 
1187 /**
1188  * mutex_trylock - try to acquire the mutex, without waiting
1189  * @lock: the mutex to be acquired
1190  *
1191  * Try to acquire the mutex atomically. Returns 1 if the mutex
1192  * has been acquired successfully, and 0 on contention.
1193  *
1194  * NOTE: this function follows the spin_trylock() convention, so
1195  * it is negated from the down_trylock() return values! Be careful
1196  * about this when converting semaphore users to mutexes.
1197  *
1198  * This function must not be used in interrupt context. The
1199  * mutex must be released by the same task that acquired it.
1200  */
1201 int __sched mutex_trylock(struct mutex *lock)
1202 {
1203 	bool locked = __mutex_trylock(lock);
1204 
1205 	if (locked)
1206 		mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1207 
1208 	return locked;
1209 }
1210 EXPORT_SYMBOL(mutex_trylock);
1211 
1212 #ifndef CONFIG_DEBUG_LOCK_ALLOC
1213 int __sched
1214 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1215 {
1216 	might_sleep();
1217 
1218 	if (__mutex_trylock_fast(&lock->base)) {
1219 		if (ctx)
1220 			ww_mutex_set_context_fastpath(lock, ctx);
1221 		return 0;
1222 	}
1223 
1224 	return __ww_mutex_lock_slowpath(lock, ctx);
1225 }
1226 EXPORT_SYMBOL(ww_mutex_lock);
1227 
1228 int __sched
1229 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1230 {
1231 	might_sleep();
1232 
1233 	if (__mutex_trylock_fast(&lock->base)) {
1234 		if (ctx)
1235 			ww_mutex_set_context_fastpath(lock, ctx);
1236 		return 0;
1237 	}
1238 
1239 	return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
1240 }
1241 EXPORT_SYMBOL(ww_mutex_lock_interruptible);
1242 
1243 #endif
1244 
1245 /**
1246  * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
1247  * @cnt: the atomic which we are to dec
1248  * @lock: the mutex to return holding if we dec to 0
1249  *
1250  * return true and hold lock if we dec to 0, return false otherwise
1251  */
1252 int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
1253 {
1254 	/* dec if we can't possibly hit 0 */
1255 	if (atomic_add_unless(cnt, -1, 1))
1256 		return 0;
1257 	/* we might hit 0, so take the lock */
1258 	mutex_lock(lock);
1259 	if (!atomic_dec_and_test(cnt)) {
1260 		/* when we actually did the dec, we didn't hit 0 */
1261 		mutex_unlock(lock);
1262 		return 0;
1263 	}
1264 	/* we hit 0, and we hold the lock */
1265 	return 1;
1266 }
1267 EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
1268