xref: /openbmc/linux/kernel/locking/rtmutex.c (revision 2c684d89)
1 /*
2  * RT-Mutexes: simple blocking mutual exclusion locks with PI support
3  *
4  * started by Ingo Molnar and Thomas Gleixner.
5  *
6  *  Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7  *  Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
8  *  Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
9  *  Copyright (C) 2006 Esben Nielsen
10  *
11  *  See Documentation/locking/rt-mutex-design.txt for details.
12  */
13 #include <linux/spinlock.h>
14 #include <linux/export.h>
15 #include <linux/sched.h>
16 #include <linux/sched/rt.h>
17 #include <linux/sched/deadline.h>
18 #include <linux/timer.h>
19 
20 #include "rtmutex_common.h"
21 
22 /*
23  * lock->owner state tracking:
24  *
25  * lock->owner holds the task_struct pointer of the owner. Bit 0
26  * is used to keep track of the "lock has waiters" state.
27  *
28  * owner	bit0
29  * NULL		0	lock is free (fast acquire possible)
30  * NULL		1	lock is free and has waiters and the top waiter
31  *				is going to take the lock*
32  * taskpointer	0	lock is held (fast release possible)
33  * taskpointer	1	lock is held and has waiters**
34  *
35  * The fast atomic compare exchange based acquire and release is only
36  * possible when bit 0 of lock->owner is 0.
37  *
38  * (*) It also can be a transitional state when grabbing the lock
39  * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
40  * we need to set the bit0 before looking at the lock, and the owner may be
41  * NULL in this small time, hence this can be a transitional state.
42  *
43  * (**) There is a small time when bit 0 is set but there are no
44  * waiters. This can happen when grabbing the lock in the slow path.
45  * To prevent a cmpxchg of the owner releasing the lock, we need to
46  * set this bit before looking at the lock.
47  */
48 
49 static void
50 rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
51 {
52 	unsigned long val = (unsigned long)owner;
53 
54 	if (rt_mutex_has_waiters(lock))
55 		val |= RT_MUTEX_HAS_WAITERS;
56 
57 	lock->owner = (struct task_struct *)val;
58 }
59 
60 static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
61 {
62 	lock->owner = (struct task_struct *)
63 			((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
64 }
65 
66 static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
67 {
68 	if (!rt_mutex_has_waiters(lock))
69 		clear_rt_mutex_waiters(lock);
70 }
71 
72 /*
73  * We can speed up the acquire/release, if there's no debugging state to be
74  * set up.
75  */
76 #ifndef CONFIG_DEBUG_RT_MUTEXES
77 # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
78 # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
79 # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
80 
81 /*
82  * Callers must hold the ->wait_lock -- which is the whole purpose as we force
83  * all future threads that attempt to [Rmw] the lock to the slowpath. As such
84  * relaxed semantics suffice.
85  */
86 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
87 {
88 	unsigned long owner, *p = (unsigned long *) &lock->owner;
89 
90 	do {
91 		owner = *p;
92 	} while (cmpxchg_relaxed(p, owner,
93 				 owner | RT_MUTEX_HAS_WAITERS) != owner);
94 }
95 
96 /*
97  * Safe fastpath aware unlock:
98  * 1) Clear the waiters bit
99  * 2) Drop lock->wait_lock
100  * 3) Try to unlock the lock with cmpxchg
101  */
102 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
103 	__releases(lock->wait_lock)
104 {
105 	struct task_struct *owner = rt_mutex_owner(lock);
106 
107 	clear_rt_mutex_waiters(lock);
108 	raw_spin_unlock(&lock->wait_lock);
109 	/*
110 	 * If a new waiter comes in between the unlock and the cmpxchg
111 	 * we have two situations:
112 	 *
113 	 * unlock(wait_lock);
114 	 *					lock(wait_lock);
115 	 * cmpxchg(p, owner, 0) == owner
116 	 *					mark_rt_mutex_waiters(lock);
117 	 *					acquire(lock);
118 	 * or:
119 	 *
120 	 * unlock(wait_lock);
121 	 *					lock(wait_lock);
122 	 *					mark_rt_mutex_waiters(lock);
123 	 *
124 	 * cmpxchg(p, owner, 0) != owner
125 	 *					enqueue_waiter();
126 	 *					unlock(wait_lock);
127 	 * lock(wait_lock);
128 	 * wake waiter();
129 	 * unlock(wait_lock);
130 	 *					lock(wait_lock);
131 	 *					acquire(lock);
132 	 */
133 	return rt_mutex_cmpxchg_release(lock, owner, NULL);
134 }
135 
136 #else
137 # define rt_mutex_cmpxchg_relaxed(l,c,n)	(0)
138 # define rt_mutex_cmpxchg_acquire(l,c,n)	(0)
139 # define rt_mutex_cmpxchg_release(l,c,n)	(0)
140 
141 static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
142 {
143 	lock->owner = (struct task_struct *)
144 			((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
145 }
146 
147 /*
148  * Simple slow path only version: lock->owner is protected by lock->wait_lock.
149  */
150 static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
151 	__releases(lock->wait_lock)
152 {
153 	lock->owner = NULL;
154 	raw_spin_unlock(&lock->wait_lock);
155 	return true;
156 }
157 #endif
158 
159 static inline int
160 rt_mutex_waiter_less(struct rt_mutex_waiter *left,
161 		     struct rt_mutex_waiter *right)
162 {
163 	if (left->prio < right->prio)
164 		return 1;
165 
166 	/*
167 	 * If both waiters have dl_prio(), we check the deadlines of the
168 	 * associated tasks.
169 	 * If left waiter has a dl_prio(), and we didn't return 1 above,
170 	 * then right waiter has a dl_prio() too.
171 	 */
172 	if (dl_prio(left->prio))
173 		return dl_time_before(left->task->dl.deadline,
174 				      right->task->dl.deadline);
175 
176 	return 0;
177 }
178 
179 static void
180 rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
181 {
182 	struct rb_node **link = &lock->waiters.rb_node;
183 	struct rb_node *parent = NULL;
184 	struct rt_mutex_waiter *entry;
185 	int leftmost = 1;
186 
187 	while (*link) {
188 		parent = *link;
189 		entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
190 		if (rt_mutex_waiter_less(waiter, entry)) {
191 			link = &parent->rb_left;
192 		} else {
193 			link = &parent->rb_right;
194 			leftmost = 0;
195 		}
196 	}
197 
198 	if (leftmost)
199 		lock->waiters_leftmost = &waiter->tree_entry;
200 
201 	rb_link_node(&waiter->tree_entry, parent, link);
202 	rb_insert_color(&waiter->tree_entry, &lock->waiters);
203 }
204 
205 static void
206 rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
207 {
208 	if (RB_EMPTY_NODE(&waiter->tree_entry))
209 		return;
210 
211 	if (lock->waiters_leftmost == &waiter->tree_entry)
212 		lock->waiters_leftmost = rb_next(&waiter->tree_entry);
213 
214 	rb_erase(&waiter->tree_entry, &lock->waiters);
215 	RB_CLEAR_NODE(&waiter->tree_entry);
216 }
217 
218 static void
219 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
220 {
221 	struct rb_node **link = &task->pi_waiters.rb_node;
222 	struct rb_node *parent = NULL;
223 	struct rt_mutex_waiter *entry;
224 	int leftmost = 1;
225 
226 	while (*link) {
227 		parent = *link;
228 		entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
229 		if (rt_mutex_waiter_less(waiter, entry)) {
230 			link = &parent->rb_left;
231 		} else {
232 			link = &parent->rb_right;
233 			leftmost = 0;
234 		}
235 	}
236 
237 	if (leftmost)
238 		task->pi_waiters_leftmost = &waiter->pi_tree_entry;
239 
240 	rb_link_node(&waiter->pi_tree_entry, parent, link);
241 	rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
242 }
243 
244 static void
245 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
246 {
247 	if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
248 		return;
249 
250 	if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
251 		task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
252 
253 	rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
254 	RB_CLEAR_NODE(&waiter->pi_tree_entry);
255 }
256 
257 /*
258  * Calculate task priority from the waiter tree priority
259  *
260  * Return task->normal_prio when the waiter tree is empty or when
261  * the waiter is not allowed to do priority boosting
262  */
263 int rt_mutex_getprio(struct task_struct *task)
264 {
265 	if (likely(!task_has_pi_waiters(task)))
266 		return task->normal_prio;
267 
268 	return min(task_top_pi_waiter(task)->prio,
269 		   task->normal_prio);
270 }
271 
272 struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
273 {
274 	if (likely(!task_has_pi_waiters(task)))
275 		return NULL;
276 
277 	return task_top_pi_waiter(task)->task;
278 }
279 
280 /*
281  * Called by sched_setscheduler() to get the priority which will be
282  * effective after the change.
283  */
284 int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
285 {
286 	if (!task_has_pi_waiters(task))
287 		return newprio;
288 
289 	if (task_top_pi_waiter(task)->task->prio <= newprio)
290 		return task_top_pi_waiter(task)->task->prio;
291 	return newprio;
292 }
293 
294 /*
295  * Adjust the priority of a task, after its pi_waiters got modified.
296  *
297  * This can be both boosting and unboosting. task->pi_lock must be held.
298  */
299 static void __rt_mutex_adjust_prio(struct task_struct *task)
300 {
301 	int prio = rt_mutex_getprio(task);
302 
303 	if (task->prio != prio || dl_prio(prio))
304 		rt_mutex_setprio(task, prio);
305 }
306 
307 /*
308  * Adjust task priority (undo boosting). Called from the exit path of
309  * rt_mutex_slowunlock() and rt_mutex_slowlock().
310  *
311  * (Note: We do this outside of the protection of lock->wait_lock to
312  * allow the lock to be taken while or before we readjust the priority
313  * of task. We do not use the spin_xx_mutex() variants here as we are
314  * outside of the debug path.)
315  */
316 void rt_mutex_adjust_prio(struct task_struct *task)
317 {
318 	unsigned long flags;
319 
320 	raw_spin_lock_irqsave(&task->pi_lock, flags);
321 	__rt_mutex_adjust_prio(task);
322 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
323 }
324 
325 /*
326  * Deadlock detection is conditional:
327  *
328  * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
329  * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
330  *
331  * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
332  * conducted independent of the detect argument.
333  *
334  * If the waiter argument is NULL this indicates the deboost path and
335  * deadlock detection is disabled independent of the detect argument
336  * and the config settings.
337  */
338 static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
339 					  enum rtmutex_chainwalk chwalk)
340 {
341 	/*
342 	 * This is just a wrapper function for the following call,
343 	 * because debug_rt_mutex_detect_deadlock() smells like a magic
344 	 * debug feature and I wanted to keep the cond function in the
345 	 * main source file along with the comments instead of having
346 	 * two of the same in the headers.
347 	 */
348 	return debug_rt_mutex_detect_deadlock(waiter, chwalk);
349 }
350 
351 /*
352  * Max number of times we'll walk the boosting chain:
353  */
354 int max_lock_depth = 1024;
355 
356 static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
357 {
358 	return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
359 }
360 
361 /*
362  * Adjust the priority chain. Also used for deadlock detection.
363  * Decreases task's usage by one - may thus free the task.
364  *
365  * @task:	the task owning the mutex (owner) for which a chain walk is
366  *		probably needed
367  * @chwalk:	do we have to carry out deadlock detection?
368  * @orig_lock:	the mutex (can be NULL if we are walking the chain to recheck
369  *		things for a task that has just got its priority adjusted, and
370  *		is waiting on a mutex)
371  * @next_lock:	the mutex on which the owner of @orig_lock was blocked before
372  *		we dropped its pi_lock. Is never dereferenced, only used for
373  *		comparison to detect lock chain changes.
374  * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
375  *		its priority to the mutex owner (can be NULL in the case
376  *		depicted above or if the top waiter is gone away and we are
377  *		actually deboosting the owner)
378  * @top_task:	the current top waiter
379  *
380  * Returns 0 or -EDEADLK.
381  *
382  * Chain walk basics and protection scope
383  *
384  * [R] refcount on task
385  * [P] task->pi_lock held
386  * [L] rtmutex->wait_lock held
387  *
388  * Step	Description				Protected by
389  *	function arguments:
390  *	@task					[R]
391  *	@orig_lock if != NULL			@top_task is blocked on it
392  *	@next_lock				Unprotected. Cannot be
393  *						dereferenced. Only used for
394  *						comparison.
395  *	@orig_waiter if != NULL			@top_task is blocked on it
396  *	@top_task				current, or in case of proxy
397  *						locking protected by calling
398  *						code
399  *	again:
400  *	  loop_sanity_check();
401  *	retry:
402  * [1]	  lock(task->pi_lock);			[R] acquire [P]
403  * [2]	  waiter = task->pi_blocked_on;		[P]
404  * [3]	  check_exit_conditions_1();		[P]
405  * [4]	  lock = waiter->lock;			[P]
406  * [5]	  if (!try_lock(lock->wait_lock)) {	[P] try to acquire [L]
407  *	    unlock(task->pi_lock);		release [P]
408  *	    goto retry;
409  *	  }
410  * [6]	  check_exit_conditions_2();		[P] + [L]
411  * [7]	  requeue_lock_waiter(lock, waiter);	[P] + [L]
412  * [8]	  unlock(task->pi_lock);		release [P]
413  *	  put_task_struct(task);		release [R]
414  * [9]	  check_exit_conditions_3();		[L]
415  * [10]	  task = owner(lock);			[L]
416  *	  get_task_struct(task);		[L] acquire [R]
417  *	  lock(task->pi_lock);			[L] acquire [P]
418  * [11]	  requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
419  * [12]	  check_exit_conditions_4();		[P] + [L]
420  * [13]	  unlock(task->pi_lock);		release [P]
421  *	  unlock(lock->wait_lock);		release [L]
422  *	  goto again;
423  */
424 static int rt_mutex_adjust_prio_chain(struct task_struct *task,
425 				      enum rtmutex_chainwalk chwalk,
426 				      struct rt_mutex *orig_lock,
427 				      struct rt_mutex *next_lock,
428 				      struct rt_mutex_waiter *orig_waiter,
429 				      struct task_struct *top_task)
430 {
431 	struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
432 	struct rt_mutex_waiter *prerequeue_top_waiter;
433 	int ret = 0, depth = 0;
434 	struct rt_mutex *lock;
435 	bool detect_deadlock;
436 	unsigned long flags;
437 	bool requeue = true;
438 
439 	detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
440 
441 	/*
442 	 * The (de)boosting is a step by step approach with a lot of
443 	 * pitfalls. We want this to be preemptible and we want hold a
444 	 * maximum of two locks per step. So we have to check
445 	 * carefully whether things change under us.
446 	 */
447  again:
448 	/*
449 	 * We limit the lock chain length for each invocation.
450 	 */
451 	if (++depth > max_lock_depth) {
452 		static int prev_max;
453 
454 		/*
455 		 * Print this only once. If the admin changes the limit,
456 		 * print a new message when reaching the limit again.
457 		 */
458 		if (prev_max != max_lock_depth) {
459 			prev_max = max_lock_depth;
460 			printk(KERN_WARNING "Maximum lock depth %d reached "
461 			       "task: %s (%d)\n", max_lock_depth,
462 			       top_task->comm, task_pid_nr(top_task));
463 		}
464 		put_task_struct(task);
465 
466 		return -EDEADLK;
467 	}
468 
469 	/*
470 	 * We are fully preemptible here and only hold the refcount on
471 	 * @task. So everything can have changed under us since the
472 	 * caller or our own code below (goto retry/again) dropped all
473 	 * locks.
474 	 */
475  retry:
476 	/*
477 	 * [1] Task cannot go away as we did a get_task() before !
478 	 */
479 	raw_spin_lock_irqsave(&task->pi_lock, flags);
480 
481 	/*
482 	 * [2] Get the waiter on which @task is blocked on.
483 	 */
484 	waiter = task->pi_blocked_on;
485 
486 	/*
487 	 * [3] check_exit_conditions_1() protected by task->pi_lock.
488 	 */
489 
490 	/*
491 	 * Check whether the end of the boosting chain has been
492 	 * reached or the state of the chain has changed while we
493 	 * dropped the locks.
494 	 */
495 	if (!waiter)
496 		goto out_unlock_pi;
497 
498 	/*
499 	 * Check the orig_waiter state. After we dropped the locks,
500 	 * the previous owner of the lock might have released the lock.
501 	 */
502 	if (orig_waiter && !rt_mutex_owner(orig_lock))
503 		goto out_unlock_pi;
504 
505 	/*
506 	 * We dropped all locks after taking a refcount on @task, so
507 	 * the task might have moved on in the lock chain or even left
508 	 * the chain completely and blocks now on an unrelated lock or
509 	 * on @orig_lock.
510 	 *
511 	 * We stored the lock on which @task was blocked in @next_lock,
512 	 * so we can detect the chain change.
513 	 */
514 	if (next_lock != waiter->lock)
515 		goto out_unlock_pi;
516 
517 	/*
518 	 * Drop out, when the task has no waiters. Note,
519 	 * top_waiter can be NULL, when we are in the deboosting
520 	 * mode!
521 	 */
522 	if (top_waiter) {
523 		if (!task_has_pi_waiters(task))
524 			goto out_unlock_pi;
525 		/*
526 		 * If deadlock detection is off, we stop here if we
527 		 * are not the top pi waiter of the task. If deadlock
528 		 * detection is enabled we continue, but stop the
529 		 * requeueing in the chain walk.
530 		 */
531 		if (top_waiter != task_top_pi_waiter(task)) {
532 			if (!detect_deadlock)
533 				goto out_unlock_pi;
534 			else
535 				requeue = false;
536 		}
537 	}
538 
539 	/*
540 	 * If the waiter priority is the same as the task priority
541 	 * then there is no further priority adjustment necessary.  If
542 	 * deadlock detection is off, we stop the chain walk. If its
543 	 * enabled we continue, but stop the requeueing in the chain
544 	 * walk.
545 	 */
546 	if (waiter->prio == task->prio) {
547 		if (!detect_deadlock)
548 			goto out_unlock_pi;
549 		else
550 			requeue = false;
551 	}
552 
553 	/*
554 	 * [4] Get the next lock
555 	 */
556 	lock = waiter->lock;
557 	/*
558 	 * [5] We need to trylock here as we are holding task->pi_lock,
559 	 * which is the reverse lock order versus the other rtmutex
560 	 * operations.
561 	 */
562 	if (!raw_spin_trylock(&lock->wait_lock)) {
563 		raw_spin_unlock_irqrestore(&task->pi_lock, flags);
564 		cpu_relax();
565 		goto retry;
566 	}
567 
568 	/*
569 	 * [6] check_exit_conditions_2() protected by task->pi_lock and
570 	 * lock->wait_lock.
571 	 *
572 	 * Deadlock detection. If the lock is the same as the original
573 	 * lock which caused us to walk the lock chain or if the
574 	 * current lock is owned by the task which initiated the chain
575 	 * walk, we detected a deadlock.
576 	 */
577 	if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
578 		debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
579 		raw_spin_unlock(&lock->wait_lock);
580 		ret = -EDEADLK;
581 		goto out_unlock_pi;
582 	}
583 
584 	/*
585 	 * If we just follow the lock chain for deadlock detection, no
586 	 * need to do all the requeue operations. To avoid a truckload
587 	 * of conditionals around the various places below, just do the
588 	 * minimum chain walk checks.
589 	 */
590 	if (!requeue) {
591 		/*
592 		 * No requeue[7] here. Just release @task [8]
593 		 */
594 		raw_spin_unlock_irqrestore(&task->pi_lock, flags);
595 		put_task_struct(task);
596 
597 		/*
598 		 * [9] check_exit_conditions_3 protected by lock->wait_lock.
599 		 * If there is no owner of the lock, end of chain.
600 		 */
601 		if (!rt_mutex_owner(lock)) {
602 			raw_spin_unlock(&lock->wait_lock);
603 			return 0;
604 		}
605 
606 		/* [10] Grab the next task, i.e. owner of @lock */
607 		task = rt_mutex_owner(lock);
608 		get_task_struct(task);
609 		raw_spin_lock_irqsave(&task->pi_lock, flags);
610 
611 		/*
612 		 * No requeue [11] here. We just do deadlock detection.
613 		 *
614 		 * [12] Store whether owner is blocked
615 		 * itself. Decision is made after dropping the locks
616 		 */
617 		next_lock = task_blocked_on_lock(task);
618 		/*
619 		 * Get the top waiter for the next iteration
620 		 */
621 		top_waiter = rt_mutex_top_waiter(lock);
622 
623 		/* [13] Drop locks */
624 		raw_spin_unlock_irqrestore(&task->pi_lock, flags);
625 		raw_spin_unlock(&lock->wait_lock);
626 
627 		/* If owner is not blocked, end of chain. */
628 		if (!next_lock)
629 			goto out_put_task;
630 		goto again;
631 	}
632 
633 	/*
634 	 * Store the current top waiter before doing the requeue
635 	 * operation on @lock. We need it for the boost/deboost
636 	 * decision below.
637 	 */
638 	prerequeue_top_waiter = rt_mutex_top_waiter(lock);
639 
640 	/* [7] Requeue the waiter in the lock waiter tree. */
641 	rt_mutex_dequeue(lock, waiter);
642 	waiter->prio = task->prio;
643 	rt_mutex_enqueue(lock, waiter);
644 
645 	/* [8] Release the task */
646 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
647 	put_task_struct(task);
648 
649 	/*
650 	 * [9] check_exit_conditions_3 protected by lock->wait_lock.
651 	 *
652 	 * We must abort the chain walk if there is no lock owner even
653 	 * in the dead lock detection case, as we have nothing to
654 	 * follow here. This is the end of the chain we are walking.
655 	 */
656 	if (!rt_mutex_owner(lock)) {
657 		/*
658 		 * If the requeue [7] above changed the top waiter,
659 		 * then we need to wake the new top waiter up to try
660 		 * to get the lock.
661 		 */
662 		if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
663 			wake_up_process(rt_mutex_top_waiter(lock)->task);
664 		raw_spin_unlock(&lock->wait_lock);
665 		return 0;
666 	}
667 
668 	/* [10] Grab the next task, i.e. the owner of @lock */
669 	task = rt_mutex_owner(lock);
670 	get_task_struct(task);
671 	raw_spin_lock_irqsave(&task->pi_lock, flags);
672 
673 	/* [11] requeue the pi waiters if necessary */
674 	if (waiter == rt_mutex_top_waiter(lock)) {
675 		/*
676 		 * The waiter became the new top (highest priority)
677 		 * waiter on the lock. Replace the previous top waiter
678 		 * in the owner tasks pi waiters tree with this waiter
679 		 * and adjust the priority of the owner.
680 		 */
681 		rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
682 		rt_mutex_enqueue_pi(task, waiter);
683 		__rt_mutex_adjust_prio(task);
684 
685 	} else if (prerequeue_top_waiter == waiter) {
686 		/*
687 		 * The waiter was the top waiter on the lock, but is
688 		 * no longer the top prority waiter. Replace waiter in
689 		 * the owner tasks pi waiters tree with the new top
690 		 * (highest priority) waiter and adjust the priority
691 		 * of the owner.
692 		 * The new top waiter is stored in @waiter so that
693 		 * @waiter == @top_waiter evaluates to true below and
694 		 * we continue to deboost the rest of the chain.
695 		 */
696 		rt_mutex_dequeue_pi(task, waiter);
697 		waiter = rt_mutex_top_waiter(lock);
698 		rt_mutex_enqueue_pi(task, waiter);
699 		__rt_mutex_adjust_prio(task);
700 	} else {
701 		/*
702 		 * Nothing changed. No need to do any priority
703 		 * adjustment.
704 		 */
705 	}
706 
707 	/*
708 	 * [12] check_exit_conditions_4() protected by task->pi_lock
709 	 * and lock->wait_lock. The actual decisions are made after we
710 	 * dropped the locks.
711 	 *
712 	 * Check whether the task which owns the current lock is pi
713 	 * blocked itself. If yes we store a pointer to the lock for
714 	 * the lock chain change detection above. After we dropped
715 	 * task->pi_lock next_lock cannot be dereferenced anymore.
716 	 */
717 	next_lock = task_blocked_on_lock(task);
718 	/*
719 	 * Store the top waiter of @lock for the end of chain walk
720 	 * decision below.
721 	 */
722 	top_waiter = rt_mutex_top_waiter(lock);
723 
724 	/* [13] Drop the locks */
725 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
726 	raw_spin_unlock(&lock->wait_lock);
727 
728 	/*
729 	 * Make the actual exit decisions [12], based on the stored
730 	 * values.
731 	 *
732 	 * We reached the end of the lock chain. Stop right here. No
733 	 * point to go back just to figure that out.
734 	 */
735 	if (!next_lock)
736 		goto out_put_task;
737 
738 	/*
739 	 * If the current waiter is not the top waiter on the lock,
740 	 * then we can stop the chain walk here if we are not in full
741 	 * deadlock detection mode.
742 	 */
743 	if (!detect_deadlock && waiter != top_waiter)
744 		goto out_put_task;
745 
746 	goto again;
747 
748  out_unlock_pi:
749 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
750  out_put_task:
751 	put_task_struct(task);
752 
753 	return ret;
754 }
755 
756 /*
757  * Try to take an rt-mutex
758  *
759  * Must be called with lock->wait_lock held.
760  *
761  * @lock:   The lock to be acquired.
762  * @task:   The task which wants to acquire the lock
763  * @waiter: The waiter that is queued to the lock's wait tree if the
764  *	    callsite called task_blocked_on_lock(), otherwise NULL
765  */
766 static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
767 				struct rt_mutex_waiter *waiter)
768 {
769 	unsigned long flags;
770 
771 	/*
772 	 * Before testing whether we can acquire @lock, we set the
773 	 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
774 	 * other tasks which try to modify @lock into the slow path
775 	 * and they serialize on @lock->wait_lock.
776 	 *
777 	 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
778 	 * as explained at the top of this file if and only if:
779 	 *
780 	 * - There is a lock owner. The caller must fixup the
781 	 *   transient state if it does a trylock or leaves the lock
782 	 *   function due to a signal or timeout.
783 	 *
784 	 * - @task acquires the lock and there are no other
785 	 *   waiters. This is undone in rt_mutex_set_owner(@task) at
786 	 *   the end of this function.
787 	 */
788 	mark_rt_mutex_waiters(lock);
789 
790 	/*
791 	 * If @lock has an owner, give up.
792 	 */
793 	if (rt_mutex_owner(lock))
794 		return 0;
795 
796 	/*
797 	 * If @waiter != NULL, @task has already enqueued the waiter
798 	 * into @lock waiter tree. If @waiter == NULL then this is a
799 	 * trylock attempt.
800 	 */
801 	if (waiter) {
802 		/*
803 		 * If waiter is not the highest priority waiter of
804 		 * @lock, give up.
805 		 */
806 		if (waiter != rt_mutex_top_waiter(lock))
807 			return 0;
808 
809 		/*
810 		 * We can acquire the lock. Remove the waiter from the
811 		 * lock waiters tree.
812 		 */
813 		rt_mutex_dequeue(lock, waiter);
814 
815 	} else {
816 		/*
817 		 * If the lock has waiters already we check whether @task is
818 		 * eligible to take over the lock.
819 		 *
820 		 * If there are no other waiters, @task can acquire
821 		 * the lock.  @task->pi_blocked_on is NULL, so it does
822 		 * not need to be dequeued.
823 		 */
824 		if (rt_mutex_has_waiters(lock)) {
825 			/*
826 			 * If @task->prio is greater than or equal to
827 			 * the top waiter priority (kernel view),
828 			 * @task lost.
829 			 */
830 			if (task->prio >= rt_mutex_top_waiter(lock)->prio)
831 				return 0;
832 
833 			/*
834 			 * The current top waiter stays enqueued. We
835 			 * don't have to change anything in the lock
836 			 * waiters order.
837 			 */
838 		} else {
839 			/*
840 			 * No waiters. Take the lock without the
841 			 * pi_lock dance.@task->pi_blocked_on is NULL
842 			 * and we have no waiters to enqueue in @task
843 			 * pi waiters tree.
844 			 */
845 			goto takeit;
846 		}
847 	}
848 
849 	/*
850 	 * Clear @task->pi_blocked_on. Requires protection by
851 	 * @task->pi_lock. Redundant operation for the @waiter == NULL
852 	 * case, but conditionals are more expensive than a redundant
853 	 * store.
854 	 */
855 	raw_spin_lock_irqsave(&task->pi_lock, flags);
856 	task->pi_blocked_on = NULL;
857 	/*
858 	 * Finish the lock acquisition. @task is the new owner. If
859 	 * other waiters exist we have to insert the highest priority
860 	 * waiter into @task->pi_waiters tree.
861 	 */
862 	if (rt_mutex_has_waiters(lock))
863 		rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
864 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
865 
866 takeit:
867 	/* We got the lock. */
868 	debug_rt_mutex_lock(lock);
869 
870 	/*
871 	 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
872 	 * are still waiters or clears it.
873 	 */
874 	rt_mutex_set_owner(lock, task);
875 
876 	rt_mutex_deadlock_account_lock(lock, task);
877 
878 	return 1;
879 }
880 
881 /*
882  * Task blocks on lock.
883  *
884  * Prepare waiter and propagate pi chain
885  *
886  * This must be called with lock->wait_lock held.
887  */
888 static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
889 				   struct rt_mutex_waiter *waiter,
890 				   struct task_struct *task,
891 				   enum rtmutex_chainwalk chwalk)
892 {
893 	struct task_struct *owner = rt_mutex_owner(lock);
894 	struct rt_mutex_waiter *top_waiter = waiter;
895 	struct rt_mutex *next_lock;
896 	int chain_walk = 0, res;
897 	unsigned long flags;
898 
899 	/*
900 	 * Early deadlock detection. We really don't want the task to
901 	 * enqueue on itself just to untangle the mess later. It's not
902 	 * only an optimization. We drop the locks, so another waiter
903 	 * can come in before the chain walk detects the deadlock. So
904 	 * the other will detect the deadlock and return -EDEADLOCK,
905 	 * which is wrong, as the other waiter is not in a deadlock
906 	 * situation.
907 	 */
908 	if (owner == task)
909 		return -EDEADLK;
910 
911 	raw_spin_lock_irqsave(&task->pi_lock, flags);
912 	__rt_mutex_adjust_prio(task);
913 	waiter->task = task;
914 	waiter->lock = lock;
915 	waiter->prio = task->prio;
916 
917 	/* Get the top priority waiter on the lock */
918 	if (rt_mutex_has_waiters(lock))
919 		top_waiter = rt_mutex_top_waiter(lock);
920 	rt_mutex_enqueue(lock, waiter);
921 
922 	task->pi_blocked_on = waiter;
923 
924 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
925 
926 	if (!owner)
927 		return 0;
928 
929 	raw_spin_lock_irqsave(&owner->pi_lock, flags);
930 	if (waiter == rt_mutex_top_waiter(lock)) {
931 		rt_mutex_dequeue_pi(owner, top_waiter);
932 		rt_mutex_enqueue_pi(owner, waiter);
933 
934 		__rt_mutex_adjust_prio(owner);
935 		if (owner->pi_blocked_on)
936 			chain_walk = 1;
937 	} else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
938 		chain_walk = 1;
939 	}
940 
941 	/* Store the lock on which owner is blocked or NULL */
942 	next_lock = task_blocked_on_lock(owner);
943 
944 	raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
945 	/*
946 	 * Even if full deadlock detection is on, if the owner is not
947 	 * blocked itself, we can avoid finding this out in the chain
948 	 * walk.
949 	 */
950 	if (!chain_walk || !next_lock)
951 		return 0;
952 
953 	/*
954 	 * The owner can't disappear while holding a lock,
955 	 * so the owner struct is protected by wait_lock.
956 	 * Gets dropped in rt_mutex_adjust_prio_chain()!
957 	 */
958 	get_task_struct(owner);
959 
960 	raw_spin_unlock(&lock->wait_lock);
961 
962 	res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
963 					 next_lock, waiter, task);
964 
965 	raw_spin_lock(&lock->wait_lock);
966 
967 	return res;
968 }
969 
970 /*
971  * Remove the top waiter from the current tasks pi waiter tree and
972  * queue it up.
973  *
974  * Called with lock->wait_lock held.
975  */
976 static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
977 				    struct rt_mutex *lock)
978 {
979 	struct rt_mutex_waiter *waiter;
980 	unsigned long flags;
981 
982 	raw_spin_lock_irqsave(&current->pi_lock, flags);
983 
984 	waiter = rt_mutex_top_waiter(lock);
985 
986 	/*
987 	 * Remove it from current->pi_waiters. We do not adjust a
988 	 * possible priority boost right now. We execute wakeup in the
989 	 * boosted mode and go back to normal after releasing
990 	 * lock->wait_lock.
991 	 */
992 	rt_mutex_dequeue_pi(current, waiter);
993 
994 	/*
995 	 * As we are waking up the top waiter, and the waiter stays
996 	 * queued on the lock until it gets the lock, this lock
997 	 * obviously has waiters. Just set the bit here and this has
998 	 * the added benefit of forcing all new tasks into the
999 	 * slow path making sure no task of lower priority than
1000 	 * the top waiter can steal this lock.
1001 	 */
1002 	lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
1003 
1004 	raw_spin_unlock_irqrestore(&current->pi_lock, flags);
1005 
1006 	wake_q_add(wake_q, waiter->task);
1007 }
1008 
1009 /*
1010  * Remove a waiter from a lock and give up
1011  *
1012  * Must be called with lock->wait_lock held and
1013  * have just failed to try_to_take_rt_mutex().
1014  */
1015 static void remove_waiter(struct rt_mutex *lock,
1016 			  struct rt_mutex_waiter *waiter)
1017 {
1018 	bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
1019 	struct task_struct *owner = rt_mutex_owner(lock);
1020 	struct rt_mutex *next_lock;
1021 	unsigned long flags;
1022 
1023 	raw_spin_lock_irqsave(&current->pi_lock, flags);
1024 	rt_mutex_dequeue(lock, waiter);
1025 	current->pi_blocked_on = NULL;
1026 	raw_spin_unlock_irqrestore(&current->pi_lock, flags);
1027 
1028 	/*
1029 	 * Only update priority if the waiter was the highest priority
1030 	 * waiter of the lock and there is an owner to update.
1031 	 */
1032 	if (!owner || !is_top_waiter)
1033 		return;
1034 
1035 	raw_spin_lock_irqsave(&owner->pi_lock, flags);
1036 
1037 	rt_mutex_dequeue_pi(owner, waiter);
1038 
1039 	if (rt_mutex_has_waiters(lock))
1040 		rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
1041 
1042 	__rt_mutex_adjust_prio(owner);
1043 
1044 	/* Store the lock on which owner is blocked or NULL */
1045 	next_lock = task_blocked_on_lock(owner);
1046 
1047 	raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
1048 
1049 	/*
1050 	 * Don't walk the chain, if the owner task is not blocked
1051 	 * itself.
1052 	 */
1053 	if (!next_lock)
1054 		return;
1055 
1056 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
1057 	get_task_struct(owner);
1058 
1059 	raw_spin_unlock(&lock->wait_lock);
1060 
1061 	rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1062 				   next_lock, NULL, current);
1063 
1064 	raw_spin_lock(&lock->wait_lock);
1065 }
1066 
1067 /*
1068  * Recheck the pi chain, in case we got a priority setting
1069  *
1070  * Called from sched_setscheduler
1071  */
1072 void rt_mutex_adjust_pi(struct task_struct *task)
1073 {
1074 	struct rt_mutex_waiter *waiter;
1075 	struct rt_mutex *next_lock;
1076 	unsigned long flags;
1077 
1078 	raw_spin_lock_irqsave(&task->pi_lock, flags);
1079 
1080 	waiter = task->pi_blocked_on;
1081 	if (!waiter || (waiter->prio == task->prio &&
1082 			!dl_prio(task->prio))) {
1083 		raw_spin_unlock_irqrestore(&task->pi_lock, flags);
1084 		return;
1085 	}
1086 	next_lock = waiter->lock;
1087 	raw_spin_unlock_irqrestore(&task->pi_lock, flags);
1088 
1089 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
1090 	get_task_struct(task);
1091 
1092 	rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1093 				   next_lock, NULL, task);
1094 }
1095 
1096 /**
1097  * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
1098  * @lock:		 the rt_mutex to take
1099  * @state:		 the state the task should block in (TASK_INTERRUPTIBLE
1100  * 			 or TASK_UNINTERRUPTIBLE)
1101  * @timeout:		 the pre-initialized and started timer, or NULL for none
1102  * @waiter:		 the pre-initialized rt_mutex_waiter
1103  *
1104  * lock->wait_lock must be held by the caller.
1105  */
1106 static int __sched
1107 __rt_mutex_slowlock(struct rt_mutex *lock, int state,
1108 		    struct hrtimer_sleeper *timeout,
1109 		    struct rt_mutex_waiter *waiter)
1110 {
1111 	int ret = 0;
1112 
1113 	for (;;) {
1114 		/* Try to acquire the lock: */
1115 		if (try_to_take_rt_mutex(lock, current, waiter))
1116 			break;
1117 
1118 		/*
1119 		 * TASK_INTERRUPTIBLE checks for signals and
1120 		 * timeout. Ignored otherwise.
1121 		 */
1122 		if (unlikely(state == TASK_INTERRUPTIBLE)) {
1123 			/* Signal pending? */
1124 			if (signal_pending(current))
1125 				ret = -EINTR;
1126 			if (timeout && !timeout->task)
1127 				ret = -ETIMEDOUT;
1128 			if (ret)
1129 				break;
1130 		}
1131 
1132 		raw_spin_unlock(&lock->wait_lock);
1133 
1134 		debug_rt_mutex_print_deadlock(waiter);
1135 
1136 		schedule();
1137 
1138 		raw_spin_lock(&lock->wait_lock);
1139 		set_current_state(state);
1140 	}
1141 
1142 	__set_current_state(TASK_RUNNING);
1143 	return ret;
1144 }
1145 
1146 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
1147 				     struct rt_mutex_waiter *w)
1148 {
1149 	/*
1150 	 * If the result is not -EDEADLOCK or the caller requested
1151 	 * deadlock detection, nothing to do here.
1152 	 */
1153 	if (res != -EDEADLOCK || detect_deadlock)
1154 		return;
1155 
1156 	/*
1157 	 * Yell lowdly and stop the task right here.
1158 	 */
1159 	rt_mutex_print_deadlock(w);
1160 	while (1) {
1161 		set_current_state(TASK_INTERRUPTIBLE);
1162 		schedule();
1163 	}
1164 }
1165 
1166 /*
1167  * Slow path lock function:
1168  */
1169 static int __sched
1170 rt_mutex_slowlock(struct rt_mutex *lock, int state,
1171 		  struct hrtimer_sleeper *timeout,
1172 		  enum rtmutex_chainwalk chwalk)
1173 {
1174 	struct rt_mutex_waiter waiter;
1175 	int ret = 0;
1176 
1177 	debug_rt_mutex_init_waiter(&waiter);
1178 	RB_CLEAR_NODE(&waiter.pi_tree_entry);
1179 	RB_CLEAR_NODE(&waiter.tree_entry);
1180 
1181 	raw_spin_lock(&lock->wait_lock);
1182 
1183 	/* Try to acquire the lock again: */
1184 	if (try_to_take_rt_mutex(lock, current, NULL)) {
1185 		raw_spin_unlock(&lock->wait_lock);
1186 		return 0;
1187 	}
1188 
1189 	set_current_state(state);
1190 
1191 	/* Setup the timer, when timeout != NULL */
1192 	if (unlikely(timeout))
1193 		hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
1194 
1195 	ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
1196 
1197 	if (likely(!ret))
1198 		/* sleep on the mutex */
1199 		ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
1200 
1201 	if (unlikely(ret)) {
1202 		__set_current_state(TASK_RUNNING);
1203 		if (rt_mutex_has_waiters(lock))
1204 			remove_waiter(lock, &waiter);
1205 		rt_mutex_handle_deadlock(ret, chwalk, &waiter);
1206 	}
1207 
1208 	/*
1209 	 * try_to_take_rt_mutex() sets the waiter bit
1210 	 * unconditionally. We might have to fix that up.
1211 	 */
1212 	fixup_rt_mutex_waiters(lock);
1213 
1214 	raw_spin_unlock(&lock->wait_lock);
1215 
1216 	/* Remove pending timer: */
1217 	if (unlikely(timeout))
1218 		hrtimer_cancel(&timeout->timer);
1219 
1220 	debug_rt_mutex_free_waiter(&waiter);
1221 
1222 	return ret;
1223 }
1224 
1225 /*
1226  * Slow path try-lock function:
1227  */
1228 static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
1229 {
1230 	int ret;
1231 
1232 	/*
1233 	 * If the lock already has an owner we fail to get the lock.
1234 	 * This can be done without taking the @lock->wait_lock as
1235 	 * it is only being read, and this is a trylock anyway.
1236 	 */
1237 	if (rt_mutex_owner(lock))
1238 		return 0;
1239 
1240 	/*
1241 	 * The mutex has currently no owner. Lock the wait lock and
1242 	 * try to acquire the lock.
1243 	 */
1244 	raw_spin_lock(&lock->wait_lock);
1245 
1246 	ret = try_to_take_rt_mutex(lock, current, NULL);
1247 
1248 	/*
1249 	 * try_to_take_rt_mutex() sets the lock waiters bit
1250 	 * unconditionally. Clean this up.
1251 	 */
1252 	fixup_rt_mutex_waiters(lock);
1253 
1254 	raw_spin_unlock(&lock->wait_lock);
1255 
1256 	return ret;
1257 }
1258 
1259 /*
1260  * Slow path to release a rt-mutex.
1261  * Return whether the current task needs to undo a potential priority boosting.
1262  */
1263 static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
1264 					struct wake_q_head *wake_q)
1265 {
1266 	raw_spin_lock(&lock->wait_lock);
1267 
1268 	debug_rt_mutex_unlock(lock);
1269 
1270 	rt_mutex_deadlock_account_unlock(current);
1271 
1272 	/*
1273 	 * We must be careful here if the fast path is enabled. If we
1274 	 * have no waiters queued we cannot set owner to NULL here
1275 	 * because of:
1276 	 *
1277 	 * foo->lock->owner = NULL;
1278 	 *			rtmutex_lock(foo->lock);   <- fast path
1279 	 *			free = atomic_dec_and_test(foo->refcnt);
1280 	 *			rtmutex_unlock(foo->lock); <- fast path
1281 	 *			if (free)
1282 	 *				kfree(foo);
1283 	 * raw_spin_unlock(foo->lock->wait_lock);
1284 	 *
1285 	 * So for the fastpath enabled kernel:
1286 	 *
1287 	 * Nothing can set the waiters bit as long as we hold
1288 	 * lock->wait_lock. So we do the following sequence:
1289 	 *
1290 	 *	owner = rt_mutex_owner(lock);
1291 	 *	clear_rt_mutex_waiters(lock);
1292 	 *	raw_spin_unlock(&lock->wait_lock);
1293 	 *	if (cmpxchg(&lock->owner, owner, 0) == owner)
1294 	 *		return;
1295 	 *	goto retry;
1296 	 *
1297 	 * The fastpath disabled variant is simple as all access to
1298 	 * lock->owner is serialized by lock->wait_lock:
1299 	 *
1300 	 *	lock->owner = NULL;
1301 	 *	raw_spin_unlock(&lock->wait_lock);
1302 	 */
1303 	while (!rt_mutex_has_waiters(lock)) {
1304 		/* Drops lock->wait_lock ! */
1305 		if (unlock_rt_mutex_safe(lock) == true)
1306 			return false;
1307 		/* Relock the rtmutex and try again */
1308 		raw_spin_lock(&lock->wait_lock);
1309 	}
1310 
1311 	/*
1312 	 * The wakeup next waiter path does not suffer from the above
1313 	 * race. See the comments there.
1314 	 *
1315 	 * Queue the next waiter for wakeup once we release the wait_lock.
1316 	 */
1317 	mark_wakeup_next_waiter(wake_q, lock);
1318 
1319 	raw_spin_unlock(&lock->wait_lock);
1320 
1321 	/* check PI boosting */
1322 	return true;
1323 }
1324 
1325 /*
1326  * debug aware fast / slowpath lock,trylock,unlock
1327  *
1328  * The atomic acquire/release ops are compiled away, when either the
1329  * architecture does not support cmpxchg or when debugging is enabled.
1330  */
1331 static inline int
1332 rt_mutex_fastlock(struct rt_mutex *lock, int state,
1333 		  int (*slowfn)(struct rt_mutex *lock, int state,
1334 				struct hrtimer_sleeper *timeout,
1335 				enum rtmutex_chainwalk chwalk))
1336 {
1337 	if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
1338 		rt_mutex_deadlock_account_lock(lock, current);
1339 		return 0;
1340 	} else
1341 		return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
1342 }
1343 
1344 static inline int
1345 rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1346 			struct hrtimer_sleeper *timeout,
1347 			enum rtmutex_chainwalk chwalk,
1348 			int (*slowfn)(struct rt_mutex *lock, int state,
1349 				      struct hrtimer_sleeper *timeout,
1350 				      enum rtmutex_chainwalk chwalk))
1351 {
1352 	if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1353 	    likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
1354 		rt_mutex_deadlock_account_lock(lock, current);
1355 		return 0;
1356 	} else
1357 		return slowfn(lock, state, timeout, chwalk);
1358 }
1359 
1360 static inline int
1361 rt_mutex_fasttrylock(struct rt_mutex *lock,
1362 		     int (*slowfn)(struct rt_mutex *lock))
1363 {
1364 	if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
1365 		rt_mutex_deadlock_account_lock(lock, current);
1366 		return 1;
1367 	}
1368 	return slowfn(lock);
1369 }
1370 
1371 static inline void
1372 rt_mutex_fastunlock(struct rt_mutex *lock,
1373 		    bool (*slowfn)(struct rt_mutex *lock,
1374 				   struct wake_q_head *wqh))
1375 {
1376 	WAKE_Q(wake_q);
1377 
1378 	if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
1379 		rt_mutex_deadlock_account_unlock(current);
1380 
1381 	} else {
1382 		bool deboost = slowfn(lock, &wake_q);
1383 
1384 		wake_up_q(&wake_q);
1385 
1386 		/* Undo pi boosting if necessary: */
1387 		if (deboost)
1388 			rt_mutex_adjust_prio(current);
1389 	}
1390 }
1391 
1392 /**
1393  * rt_mutex_lock - lock a rt_mutex
1394  *
1395  * @lock: the rt_mutex to be locked
1396  */
1397 void __sched rt_mutex_lock(struct rt_mutex *lock)
1398 {
1399 	might_sleep();
1400 
1401 	rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
1402 }
1403 EXPORT_SYMBOL_GPL(rt_mutex_lock);
1404 
1405 /**
1406  * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1407  *
1408  * @lock:		the rt_mutex to be locked
1409  *
1410  * Returns:
1411  *  0		on success
1412  * -EINTR	when interrupted by a signal
1413  */
1414 int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
1415 {
1416 	might_sleep();
1417 
1418 	return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1419 }
1420 EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1421 
1422 /*
1423  * Futex variant with full deadlock detection.
1424  */
1425 int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1426 			      struct hrtimer_sleeper *timeout)
1427 {
1428 	might_sleep();
1429 
1430 	return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1431 				       RT_MUTEX_FULL_CHAINWALK,
1432 				       rt_mutex_slowlock);
1433 }
1434 
1435 /**
1436  * rt_mutex_timed_lock - lock a rt_mutex interruptible
1437  *			the timeout structure is provided
1438  *			by the caller
1439  *
1440  * @lock:		the rt_mutex to be locked
1441  * @timeout:		timeout structure or NULL (no timeout)
1442  *
1443  * Returns:
1444  *  0		on success
1445  * -EINTR	when interrupted by a signal
1446  * -ETIMEDOUT	when the timeout expired
1447  */
1448 int
1449 rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
1450 {
1451 	might_sleep();
1452 
1453 	return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1454 				       RT_MUTEX_MIN_CHAINWALK,
1455 				       rt_mutex_slowlock);
1456 }
1457 EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1458 
1459 /**
1460  * rt_mutex_trylock - try to lock a rt_mutex
1461  *
1462  * @lock:	the rt_mutex to be locked
1463  *
1464  * This function can only be called in thread context. It's safe to
1465  * call it from atomic regions, but not from hard interrupt or soft
1466  * interrupt context.
1467  *
1468  * Returns 1 on success and 0 on contention
1469  */
1470 int __sched rt_mutex_trylock(struct rt_mutex *lock)
1471 {
1472 	if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
1473 		return 0;
1474 
1475 	return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
1476 }
1477 EXPORT_SYMBOL_GPL(rt_mutex_trylock);
1478 
1479 /**
1480  * rt_mutex_unlock - unlock a rt_mutex
1481  *
1482  * @lock: the rt_mutex to be unlocked
1483  */
1484 void __sched rt_mutex_unlock(struct rt_mutex *lock)
1485 {
1486 	rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
1487 }
1488 EXPORT_SYMBOL_GPL(rt_mutex_unlock);
1489 
1490 /**
1491  * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
1492  * @lock: the rt_mutex to be unlocked
1493  *
1494  * Returns: true/false indicating whether priority adjustment is
1495  * required or not.
1496  */
1497 bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
1498 				   struct wake_q_head *wqh)
1499 {
1500 	if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
1501 		rt_mutex_deadlock_account_unlock(current);
1502 		return false;
1503 	}
1504 	return rt_mutex_slowunlock(lock, wqh);
1505 }
1506 
1507 /**
1508  * rt_mutex_destroy - mark a mutex unusable
1509  * @lock: the mutex to be destroyed
1510  *
1511  * This function marks the mutex uninitialized, and any subsequent
1512  * use of the mutex is forbidden. The mutex must not be locked when
1513  * this function is called.
1514  */
1515 void rt_mutex_destroy(struct rt_mutex *lock)
1516 {
1517 	WARN_ON(rt_mutex_is_locked(lock));
1518 #ifdef CONFIG_DEBUG_RT_MUTEXES
1519 	lock->magic = NULL;
1520 #endif
1521 }
1522 
1523 EXPORT_SYMBOL_GPL(rt_mutex_destroy);
1524 
1525 /**
1526  * __rt_mutex_init - initialize the rt lock
1527  *
1528  * @lock: the rt lock to be initialized
1529  *
1530  * Initialize the rt lock to unlocked state.
1531  *
1532  * Initializing of a locked rt lock is not allowed
1533  */
1534 void __rt_mutex_init(struct rt_mutex *lock, const char *name)
1535 {
1536 	lock->owner = NULL;
1537 	raw_spin_lock_init(&lock->wait_lock);
1538 	lock->waiters = RB_ROOT;
1539 	lock->waiters_leftmost = NULL;
1540 
1541 	debug_rt_mutex_init(lock, name);
1542 }
1543 EXPORT_SYMBOL_GPL(__rt_mutex_init);
1544 
1545 /**
1546  * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
1547  *				proxy owner
1548  *
1549  * @lock: 	the rt_mutex to be locked
1550  * @proxy_owner:the task to set as owner
1551  *
1552  * No locking. Caller has to do serializing itself
1553  * Special API call for PI-futex support
1554  */
1555 void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
1556 				struct task_struct *proxy_owner)
1557 {
1558 	__rt_mutex_init(lock, NULL);
1559 	debug_rt_mutex_proxy_lock(lock, proxy_owner);
1560 	rt_mutex_set_owner(lock, proxy_owner);
1561 	rt_mutex_deadlock_account_lock(lock, proxy_owner);
1562 }
1563 
1564 /**
1565  * rt_mutex_proxy_unlock - release a lock on behalf of owner
1566  *
1567  * @lock: 	the rt_mutex to be locked
1568  *
1569  * No locking. Caller has to do serializing itself
1570  * Special API call for PI-futex support
1571  */
1572 void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1573 			   struct task_struct *proxy_owner)
1574 {
1575 	debug_rt_mutex_proxy_unlock(lock);
1576 	rt_mutex_set_owner(lock, NULL);
1577 	rt_mutex_deadlock_account_unlock(proxy_owner);
1578 }
1579 
1580 /**
1581  * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
1582  * @lock:		the rt_mutex to take
1583  * @waiter:		the pre-initialized rt_mutex_waiter
1584  * @task:		the task to prepare
1585  *
1586  * Returns:
1587  *  0 - task blocked on lock
1588  *  1 - acquired the lock for task, caller should wake it up
1589  * <0 - error
1590  *
1591  * Special API call for FUTEX_REQUEUE_PI support.
1592  */
1593 int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1594 			      struct rt_mutex_waiter *waiter,
1595 			      struct task_struct *task)
1596 {
1597 	int ret;
1598 
1599 	raw_spin_lock(&lock->wait_lock);
1600 
1601 	if (try_to_take_rt_mutex(lock, task, NULL)) {
1602 		raw_spin_unlock(&lock->wait_lock);
1603 		return 1;
1604 	}
1605 
1606 	/* We enforce deadlock detection for futexes */
1607 	ret = task_blocks_on_rt_mutex(lock, waiter, task,
1608 				      RT_MUTEX_FULL_CHAINWALK);
1609 
1610 	if (ret && !rt_mutex_owner(lock)) {
1611 		/*
1612 		 * Reset the return value. We might have
1613 		 * returned with -EDEADLK and the owner
1614 		 * released the lock while we were walking the
1615 		 * pi chain.  Let the waiter sort it out.
1616 		 */
1617 		ret = 0;
1618 	}
1619 
1620 	if (unlikely(ret))
1621 		remove_waiter(lock, waiter);
1622 
1623 	raw_spin_unlock(&lock->wait_lock);
1624 
1625 	debug_rt_mutex_print_deadlock(waiter);
1626 
1627 	return ret;
1628 }
1629 
1630 /**
1631  * rt_mutex_next_owner - return the next owner of the lock
1632  *
1633  * @lock: the rt lock query
1634  *
1635  * Returns the next owner of the lock or NULL
1636  *
1637  * Caller has to serialize against other accessors to the lock
1638  * itself.
1639  *
1640  * Special API call for PI-futex support
1641  */
1642 struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1643 {
1644 	if (!rt_mutex_has_waiters(lock))
1645 		return NULL;
1646 
1647 	return rt_mutex_top_waiter(lock)->task;
1648 }
1649 
1650 /**
1651  * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1652  * @lock:		the rt_mutex we were woken on
1653  * @to:			the timeout, null if none. hrtimer should already have
1654  *			been started.
1655  * @waiter:		the pre-initialized rt_mutex_waiter
1656  *
1657  * Complete the lock acquisition started our behalf by another thread.
1658  *
1659  * Returns:
1660  *  0 - success
1661  * <0 - error, one of -EINTR, -ETIMEDOUT
1662  *
1663  * Special API call for PI-futex requeue support
1664  */
1665 int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1666 			       struct hrtimer_sleeper *to,
1667 			       struct rt_mutex_waiter *waiter)
1668 {
1669 	int ret;
1670 
1671 	raw_spin_lock(&lock->wait_lock);
1672 
1673 	set_current_state(TASK_INTERRUPTIBLE);
1674 
1675 	/* sleep on the mutex */
1676 	ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
1677 
1678 	if (unlikely(ret))
1679 		remove_waiter(lock, waiter);
1680 
1681 	/*
1682 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
1683 	 * have to fix that up.
1684 	 */
1685 	fixup_rt_mutex_waiters(lock);
1686 
1687 	raw_spin_unlock(&lock->wait_lock);
1688 
1689 	return ret;
1690 }
1691