• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * RT-Mutexes: simple blocking mutual exclusion locks with PI support
4  *
5  * started by Ingo Molnar and Thomas Gleixner.
6  *
7  *  Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8  *  Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
9  *  Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
10  *  Copyright (C) 2006 Esben Nielsen
11  * Adaptive Spinlocks:
12  *  Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
13  *				     and Peter Morreale,
14  * Adaptive Spinlocks simplification:
15  *  Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
16  *
17  *  See Documentation/locking/rt-mutex-design.rst for details.
18  */
19 #include <linux/sched.h>
20 #include <linux/sched/debug.h>
21 #include <linux/sched/deadline.h>
22 #include <linux/sched/signal.h>
23 #include <linux/sched/rt.h>
24 #include <linux/sched/wake_q.h>
25 #include <linux/ww_mutex.h>
26 
27 #include <trace/events/lock.h>
28 #include <trace/hooks/dtask.h>
29 
30 #include "rtmutex_common.h"
31 
32 #ifndef WW_RT
33 # define build_ww_mutex()	(false)
34 # define ww_container_of(rtm)	NULL
35 
__ww_mutex_add_waiter(struct rt_mutex_waiter * waiter,struct rt_mutex * lock,struct ww_acquire_ctx * ww_ctx)36 static inline int __ww_mutex_add_waiter(struct rt_mutex_waiter *waiter,
37 					struct rt_mutex *lock,
38 					struct ww_acquire_ctx *ww_ctx)
39 {
40 	return 0;
41 }
42 
__ww_mutex_check_waiters(struct rt_mutex * lock,struct ww_acquire_ctx * ww_ctx)43 static inline void __ww_mutex_check_waiters(struct rt_mutex *lock,
44 					    struct ww_acquire_ctx *ww_ctx)
45 {
46 }
47 
ww_mutex_lock_acquired(struct ww_mutex * lock,struct ww_acquire_ctx * ww_ctx)48 static inline void ww_mutex_lock_acquired(struct ww_mutex *lock,
49 					  struct ww_acquire_ctx *ww_ctx)
50 {
51 }
52 
__ww_mutex_check_kill(struct rt_mutex * lock,struct rt_mutex_waiter * waiter,struct ww_acquire_ctx * ww_ctx)53 static inline int __ww_mutex_check_kill(struct rt_mutex *lock,
54 					struct rt_mutex_waiter *waiter,
55 					struct ww_acquire_ctx *ww_ctx)
56 {
57 	return 0;
58 }
59 
60 #else
61 # define build_ww_mutex()	(true)
62 # define ww_container_of(rtm)	container_of(rtm, struct ww_mutex, base)
63 # include "ww_mutex.h"
64 #endif
65 
66 /*
67  * lock->owner state tracking:
68  *
69  * lock->owner holds the task_struct pointer of the owner. Bit 0
70  * is used to keep track of the "lock has waiters" state.
71  *
72  * owner	bit0
73  * NULL		0	lock is free (fast acquire possible)
74  * NULL		1	lock is free and has waiters and the top waiter
75  *				is going to take the lock*
76  * taskpointer	0	lock is held (fast release possible)
77  * taskpointer	1	lock is held and has waiters**
78  *
79  * The fast atomic compare exchange based acquire and release is only
80  * possible when bit 0 of lock->owner is 0.
81  *
82  * (*) It also can be a transitional state when grabbing the lock
83  * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
84  * we need to set the bit0 before looking at the lock, and the owner may be
85  * NULL in this small time, hence this can be a transitional state.
86  *
87  * (**) There is a small time when bit 0 is set but there are no
88  * waiters. This can happen when grabbing the lock in the slow path.
89  * To prevent a cmpxchg of the owner releasing the lock, we need to
90  * set this bit before looking at the lock.
91  */
92 
93 static __always_inline struct task_struct *
rt_mutex_owner_encode(struct rt_mutex_base * lock,struct task_struct * owner)94 rt_mutex_owner_encode(struct rt_mutex_base *lock, struct task_struct *owner)
95 {
96 	unsigned long val = (unsigned long)owner;
97 
98 	if (rt_mutex_has_waiters(lock))
99 		val |= RT_MUTEX_HAS_WAITERS;
100 
101 	return (struct task_struct *)val;
102 }
103 
104 static __always_inline void
rt_mutex_set_owner(struct rt_mutex_base * lock,struct task_struct * owner)105 rt_mutex_set_owner(struct rt_mutex_base *lock, struct task_struct *owner)
106 {
107 	/*
108 	 * lock->wait_lock is held but explicit acquire semantics are needed
109 	 * for a new lock owner so WRITE_ONCE is insufficient.
110 	 */
111 	xchg_acquire(&lock->owner, rt_mutex_owner_encode(lock, owner));
112 }
113 
rt_mutex_clear_owner(struct rt_mutex_base * lock)114 static __always_inline void rt_mutex_clear_owner(struct rt_mutex_base *lock)
115 {
116 	/* lock->wait_lock is held so the unlock provides release semantics. */
117 	WRITE_ONCE(lock->owner, rt_mutex_owner_encode(lock, NULL));
118 }
119 
clear_rt_mutex_waiters(struct rt_mutex_base * lock)120 static __always_inline void clear_rt_mutex_waiters(struct rt_mutex_base *lock)
121 {
122 	lock->owner = (struct task_struct *)
123 			((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
124 }
125 
126 static __always_inline void
fixup_rt_mutex_waiters(struct rt_mutex_base * lock,bool acquire_lock)127 fixup_rt_mutex_waiters(struct rt_mutex_base *lock, bool acquire_lock)
128 {
129 	unsigned long owner, *p = (unsigned long *) &lock->owner;
130 
131 	if (rt_mutex_has_waiters(lock))
132 		return;
133 
134 	/*
135 	 * The rbtree has no waiters enqueued, now make sure that the
136 	 * lock->owner still has the waiters bit set, otherwise the
137 	 * following can happen:
138 	 *
139 	 * CPU 0	CPU 1		CPU2
140 	 * l->owner=T1
141 	 *		rt_mutex_lock(l)
142 	 *		lock(l->lock)
143 	 *		l->owner = T1 | HAS_WAITERS;
144 	 *		enqueue(T2)
145 	 *		boost()
146 	 *		  unlock(l->lock)
147 	 *		block()
148 	 *
149 	 *				rt_mutex_lock(l)
150 	 *				lock(l->lock)
151 	 *				l->owner = T1 | HAS_WAITERS;
152 	 *				enqueue(T3)
153 	 *				boost()
154 	 *				  unlock(l->lock)
155 	 *				block()
156 	 *		signal(->T2)	signal(->T3)
157 	 *		lock(l->lock)
158 	 *		dequeue(T2)
159 	 *		deboost()
160 	 *		  unlock(l->lock)
161 	 *				lock(l->lock)
162 	 *				dequeue(T3)
163 	 *				 ==> wait list is empty
164 	 *				deboost()
165 	 *				 unlock(l->lock)
166 	 *		lock(l->lock)
167 	 *		fixup_rt_mutex_waiters()
168 	 *		  if (wait_list_empty(l) {
169 	 *		    l->owner = owner
170 	 *		    owner = l->owner & ~HAS_WAITERS;
171 	 *		      ==> l->owner = T1
172 	 *		  }
173 	 *				lock(l->lock)
174 	 * rt_mutex_unlock(l)		fixup_rt_mutex_waiters()
175 	 *				  if (wait_list_empty(l) {
176 	 *				    owner = l->owner & ~HAS_WAITERS;
177 	 * cmpxchg(l->owner, T1, NULL)
178 	 *  ===> Success (l->owner = NULL)
179 	 *
180 	 *				    l->owner = owner
181 	 *				      ==> l->owner = T1
182 	 *				  }
183 	 *
184 	 * With the check for the waiter bit in place T3 on CPU2 will not
185 	 * overwrite. All tasks fiddling with the waiters bit are
186 	 * serialized by l->lock, so nothing else can modify the waiters
187 	 * bit. If the bit is set then nothing can change l->owner either
188 	 * so the simple RMW is safe. The cmpxchg() will simply fail if it
189 	 * happens in the middle of the RMW because the waiters bit is
190 	 * still set.
191 	 */
192 	owner = READ_ONCE(*p);
193 	if (owner & RT_MUTEX_HAS_WAITERS) {
194 		/*
195 		 * See rt_mutex_set_owner() and rt_mutex_clear_owner() on
196 		 * why xchg_acquire() is used for updating owner for
197 		 * locking and WRITE_ONCE() for unlocking.
198 		 *
199 		 * WRITE_ONCE() would work for the acquire case too, but
200 		 * in case that the lock acquisition failed it might
201 		 * force other lockers into the slow path unnecessarily.
202 		 */
203 		if (acquire_lock)
204 			xchg_acquire(p, owner & ~RT_MUTEX_HAS_WAITERS);
205 		else
206 			WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
207 	}
208 }
209 
210 /*
211  * We can speed up the acquire/release, if there's no debugging state to be
212  * set up.
213  */
214 #ifndef CONFIG_DEBUG_RT_MUTEXES
rt_mutex_cmpxchg_acquire(struct rt_mutex_base * lock,struct task_struct * old,struct task_struct * new)215 static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
216 						     struct task_struct *old,
217 						     struct task_struct *new)
218 {
219 	return try_cmpxchg_acquire(&lock->owner, &old, new);
220 }
221 
rt_mutex_cmpxchg_release(struct rt_mutex_base * lock,struct task_struct * old,struct task_struct * new)222 static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
223 						     struct task_struct *old,
224 						     struct task_struct *new)
225 {
226 	return try_cmpxchg_release(&lock->owner, &old, new);
227 }
228 
229 /*
230  * Callers must hold the ->wait_lock -- which is the whole purpose as we force
231  * all future threads that attempt to [Rmw] the lock to the slowpath. As such
232  * relaxed semantics suffice.
233  */
mark_rt_mutex_waiters(struct rt_mutex_base * lock)234 static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
235 {
236 	unsigned long owner, *p = (unsigned long *) &lock->owner;
237 
238 	do {
239 		owner = *p;
240 	} while (cmpxchg_relaxed(p, owner,
241 				 owner | RT_MUTEX_HAS_WAITERS) != owner);
242 
243 	/*
244 	 * The cmpxchg loop above is relaxed to avoid back-to-back ACQUIRE
245 	 * operations in the event of contention. Ensure the successful
246 	 * cmpxchg is visible.
247 	 */
248 	smp_mb__after_atomic();
249 }
250 
251 /*
252  * Safe fastpath aware unlock:
253  * 1) Clear the waiters bit
254  * 2) Drop lock->wait_lock
255  * 3) Try to unlock the lock with cmpxchg
256  */
unlock_rt_mutex_safe(struct rt_mutex_base * lock,unsigned long flags)257 static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
258 						 unsigned long flags)
259 	__releases(lock->wait_lock)
260 {
261 	struct task_struct *owner = rt_mutex_owner(lock);
262 
263 	clear_rt_mutex_waiters(lock);
264 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
265 	/*
266 	 * If a new waiter comes in between the unlock and the cmpxchg
267 	 * we have two situations:
268 	 *
269 	 * unlock(wait_lock);
270 	 *					lock(wait_lock);
271 	 * cmpxchg(p, owner, 0) == owner
272 	 *					mark_rt_mutex_waiters(lock);
273 	 *					acquire(lock);
274 	 * or:
275 	 *
276 	 * unlock(wait_lock);
277 	 *					lock(wait_lock);
278 	 *					mark_rt_mutex_waiters(lock);
279 	 *
280 	 * cmpxchg(p, owner, 0) != owner
281 	 *					enqueue_waiter();
282 	 *					unlock(wait_lock);
283 	 * lock(wait_lock);
284 	 * wake waiter();
285 	 * unlock(wait_lock);
286 	 *					lock(wait_lock);
287 	 *					acquire(lock);
288 	 */
289 	return rt_mutex_cmpxchg_release(lock, owner, NULL);
290 }
291 
292 #else
rt_mutex_cmpxchg_acquire(struct rt_mutex_base * lock,struct task_struct * old,struct task_struct * new)293 static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
294 						     struct task_struct *old,
295 						     struct task_struct *new)
296 {
297 	return false;
298 
299 }
300 
rt_mutex_cmpxchg_release(struct rt_mutex_base * lock,struct task_struct * old,struct task_struct * new)301 static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
302 						     struct task_struct *old,
303 						     struct task_struct *new)
304 {
305 	return false;
306 }
307 
mark_rt_mutex_waiters(struct rt_mutex_base * lock)308 static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
309 {
310 	lock->owner = (struct task_struct *)
311 			((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
312 }
313 
314 /*
315  * Simple slow path only version: lock->owner is protected by lock->wait_lock.
316  */
unlock_rt_mutex_safe(struct rt_mutex_base * lock,unsigned long flags)317 static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
318 						 unsigned long flags)
319 	__releases(lock->wait_lock)
320 {
321 	lock->owner = NULL;
322 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
323 	return true;
324 }
325 #endif
326 
__waiter_prio(struct task_struct * task)327 static __always_inline int __waiter_prio(struct task_struct *task)
328 {
329 	int waiter_prio = 0;
330 
331 	trace_android_vh_rtmutex_waiter_prio(task, &waiter_prio);
332 	if (waiter_prio > 0)
333 		return waiter_prio;
334 
335 	return task->prio;
336 }
337 
338 /*
339  * Update the waiter->tree copy of the sort keys.
340  */
341 static __always_inline void
waiter_update_prio(struct rt_mutex_waiter * waiter,struct task_struct * task)342 waiter_update_prio(struct rt_mutex_waiter *waiter, struct task_struct *task)
343 {
344 	lockdep_assert_held(&waiter->lock->wait_lock);
345 	lockdep_assert(RB_EMPTY_NODE(&waiter->tree.entry));
346 
347 	waiter->tree.prio = __waiter_prio(task);
348 	waiter->tree.deadline = task->dl.deadline;
349 }
350 
351 /*
352  * Update the waiter->pi_tree copy of the sort keys (from the tree copy).
353  */
354 static __always_inline void
waiter_clone_prio(struct rt_mutex_waiter * waiter,struct task_struct * task)355 waiter_clone_prio(struct rt_mutex_waiter *waiter, struct task_struct *task)
356 {
357 	lockdep_assert_held(&waiter->lock->wait_lock);
358 	lockdep_assert_held(&task->pi_lock);
359 	lockdep_assert(RB_EMPTY_NODE(&waiter->pi_tree.entry));
360 
361 	waiter->pi_tree.prio = waiter->tree.prio;
362 	waiter->pi_tree.deadline = waiter->tree.deadline;
363 }
364 
365 /*
366  * Only use with rt_waiter_node_{less,equal}()
367  */
368 #define task_to_waiter_node(p)	\
369 	&(struct rt_waiter_node){ .prio = __waiter_prio(p), .deadline = (p)->dl.deadline }
370 #define task_to_waiter(p)	\
371 	&(struct rt_mutex_waiter){ .tree = *task_to_waiter_node(p) }
372 
rt_waiter_node_less(struct rt_waiter_node * left,struct rt_waiter_node * right)373 static __always_inline int rt_waiter_node_less(struct rt_waiter_node *left,
374 					       struct rt_waiter_node *right)
375 {
376 	if (left->prio < right->prio)
377 		return 1;
378 
379 	/*
380 	 * If both waiters have dl_prio(), we check the deadlines of the
381 	 * associated tasks.
382 	 * If left waiter has a dl_prio(), and we didn't return 1 above,
383 	 * then right waiter has a dl_prio() too.
384 	 */
385 	if (dl_prio(left->prio))
386 		return dl_time_before(left->deadline, right->deadline);
387 
388 	return 0;
389 }
390 
rt_waiter_node_equal(struct rt_waiter_node * left,struct rt_waiter_node * right)391 static __always_inline int rt_waiter_node_equal(struct rt_waiter_node *left,
392 						 struct rt_waiter_node *right)
393 {
394 	if (left->prio != right->prio)
395 		return 0;
396 
397 	/*
398 	 * If both waiters have dl_prio(), we check the deadlines of the
399 	 * associated tasks.
400 	 * If left waiter has a dl_prio(), and we didn't return 0 above,
401 	 * then right waiter has a dl_prio() too.
402 	 */
403 	if (dl_prio(left->prio))
404 		return left->deadline == right->deadline;
405 
406 	return 1;
407 }
408 
rt_mutex_steal(struct rt_mutex_waiter * waiter,struct rt_mutex_waiter * top_waiter)409 static inline bool rt_mutex_steal(struct rt_mutex_waiter *waiter,
410 				  struct rt_mutex_waiter *top_waiter)
411 {
412 	bool ret = false;
413 
414 	if (rt_waiter_node_less(&waiter->tree, &top_waiter->tree))
415 		return true;
416 
417 	trace_android_vh_rt_mutex_steal(waiter->tree.prio, top_waiter->tree.prio, &ret);
418 	if (ret)
419 		return true;
420 
421 #ifdef RT_MUTEX_BUILD_SPINLOCKS
422 	/*
423 	 * Note that RT tasks are excluded from same priority (lateral)
424 	 * steals to prevent the introduction of an unbounded latency.
425 	 */
426 	if (rt_prio(waiter->tree.prio) || dl_prio(waiter->tree.prio))
427 		return false;
428 
429 	return rt_waiter_node_equal(&waiter->tree, &top_waiter->tree);
430 #else
431 	return false;
432 #endif
433 }
434 
435 #define __node_2_waiter(node) \
436 	rb_entry((node), struct rt_mutex_waiter, tree.entry)
437 
__waiter_less(struct rb_node * a,const struct rb_node * b)438 static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_node *b)
439 {
440 	struct rt_mutex_waiter *aw = __node_2_waiter(a);
441 	struct rt_mutex_waiter *bw = __node_2_waiter(b);
442 
443 	if (rt_waiter_node_less(&aw->tree, &bw->tree))
444 		return 1;
445 
446 	if (!build_ww_mutex())
447 		return 0;
448 
449 	if (rt_waiter_node_less(&bw->tree, &aw->tree))
450 		return 0;
451 
452 	/* NOTE: relies on waiter->ww_ctx being set before insertion */
453 	if (aw->ww_ctx) {
454 		if (!bw->ww_ctx)
455 			return 1;
456 
457 		return (signed long)(aw->ww_ctx->stamp -
458 				     bw->ww_ctx->stamp) < 0;
459 	}
460 
461 	return 0;
462 }
463 
464 static __always_inline void
rt_mutex_enqueue(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)465 rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
466 {
467 	lockdep_assert_held(&lock->wait_lock);
468 
469 	rb_add_cached(&waiter->tree.entry, &lock->waiters, __waiter_less);
470 }
471 
472 static __always_inline void
rt_mutex_dequeue(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)473 rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
474 {
475 	lockdep_assert_held(&lock->wait_lock);
476 
477 	if (RB_EMPTY_NODE(&waiter->tree.entry))
478 		return;
479 
480 	rb_erase_cached(&waiter->tree.entry, &lock->waiters);
481 	RB_CLEAR_NODE(&waiter->tree.entry);
482 }
483 
484 #define __node_2_rt_node(node) \
485 	rb_entry((node), struct rt_waiter_node, entry)
486 
__pi_waiter_less(struct rb_node * a,const struct rb_node * b)487 static __always_inline bool __pi_waiter_less(struct rb_node *a, const struct rb_node *b)
488 {
489 	return rt_waiter_node_less(__node_2_rt_node(a), __node_2_rt_node(b));
490 }
491 
492 static __always_inline void
rt_mutex_enqueue_pi(struct task_struct * task,struct rt_mutex_waiter * waiter)493 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
494 {
495 	lockdep_assert_held(&task->pi_lock);
496 
497 	rb_add_cached(&waiter->pi_tree.entry, &task->pi_waiters, __pi_waiter_less);
498 }
499 
500 static __always_inline void
rt_mutex_dequeue_pi(struct task_struct * task,struct rt_mutex_waiter * waiter)501 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
502 {
503 	lockdep_assert_held(&task->pi_lock);
504 
505 	if (RB_EMPTY_NODE(&waiter->pi_tree.entry))
506 		return;
507 
508 	rb_erase_cached(&waiter->pi_tree.entry, &task->pi_waiters);
509 	RB_CLEAR_NODE(&waiter->pi_tree.entry);
510 }
511 
rt_mutex_adjust_prio(struct rt_mutex_base * lock,struct task_struct * p)512 static __always_inline void rt_mutex_adjust_prio(struct rt_mutex_base *lock,
513 						 struct task_struct *p)
514 {
515 	struct task_struct *pi_task = NULL;
516 
517 	lockdep_assert_held(&lock->wait_lock);
518 	lockdep_assert(rt_mutex_owner(lock) == p);
519 	lockdep_assert_held(&p->pi_lock);
520 
521 	if (task_has_pi_waiters(p))
522 		pi_task = task_top_pi_waiter(p)->task;
523 
524 	rt_mutex_setprio(p, pi_task);
525 }
526 
527 /* RT mutex specific wake_q wrappers */
rt_mutex_wake_q_add_task(struct rt_wake_q_head * wqh,struct task_struct * task,unsigned int wake_state)528 static __always_inline void rt_mutex_wake_q_add_task(struct rt_wake_q_head *wqh,
529 						     struct task_struct *task,
530 						     unsigned int wake_state)
531 {
532 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && wake_state == TASK_RTLOCK_WAIT) {
533 		if (IS_ENABLED(CONFIG_PROVE_LOCKING))
534 			WARN_ON_ONCE(wqh->rtlock_task);
535 		get_task_struct(task);
536 		wqh->rtlock_task = task;
537 	} else {
538 		wake_q_add(&wqh->head, task);
539 	}
540 }
541 
rt_mutex_wake_q_add(struct rt_wake_q_head * wqh,struct rt_mutex_waiter * w)542 static __always_inline void rt_mutex_wake_q_add(struct rt_wake_q_head *wqh,
543 						struct rt_mutex_waiter *w)
544 {
545 	rt_mutex_wake_q_add_task(wqh, w->task, w->wake_state);
546 }
547 
rt_mutex_wake_up_q(struct rt_wake_q_head * wqh)548 static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh)
549 {
550 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && wqh->rtlock_task) {
551 		wake_up_state(wqh->rtlock_task, TASK_RTLOCK_WAIT);
552 		put_task_struct(wqh->rtlock_task);
553 		wqh->rtlock_task = NULL;
554 	}
555 
556 	if (!wake_q_empty(&wqh->head))
557 		wake_up_q(&wqh->head);
558 
559 	/* Pairs with preempt_disable() in mark_wakeup_next_waiter() */
560 	preempt_enable();
561 }
562 
563 /*
564  * Deadlock detection is conditional:
565  *
566  * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
567  * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
568  *
569  * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
570  * conducted independent of the detect argument.
571  *
572  * If the waiter argument is NULL this indicates the deboost path and
573  * deadlock detection is disabled independent of the detect argument
574  * and the config settings.
575  */
576 static __always_inline bool
rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter * waiter,enum rtmutex_chainwalk chwalk)577 rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
578 			      enum rtmutex_chainwalk chwalk)
579 {
580 	if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
581 		return waiter != NULL;
582 	return chwalk == RT_MUTEX_FULL_CHAINWALK;
583 }
584 
task_blocked_on_lock(struct task_struct * p)585 static __always_inline struct rt_mutex_base *task_blocked_on_lock(struct task_struct *p)
586 {
587 	return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
588 }
589 
590 /*
591  * Adjust the priority chain. Also used for deadlock detection.
592  * Decreases task's usage by one - may thus free the task.
593  *
594  * @task:	the task owning the mutex (owner) for which a chain walk is
595  *		probably needed
596  * @chwalk:	do we have to carry out deadlock detection?
597  * @orig_lock:	the mutex (can be NULL if we are walking the chain to recheck
598  *		things for a task that has just got its priority adjusted, and
599  *		is waiting on a mutex)
600  * @next_lock:	the mutex on which the owner of @orig_lock was blocked before
601  *		we dropped its pi_lock. Is never dereferenced, only used for
602  *		comparison to detect lock chain changes.
603  * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
604  *		its priority to the mutex owner (can be NULL in the case
605  *		depicted above or if the top waiter is gone away and we are
606  *		actually deboosting the owner)
607  * @top_task:	the current top waiter
608  *
609  * Returns 0 or -EDEADLK.
610  *
611  * Chain walk basics and protection scope
612  *
613  * [R] refcount on task
614  * [Pn] task->pi_lock held
615  * [L] rtmutex->wait_lock held
616  *
617  * Normal locking order:
618  *
619  *   rtmutex->wait_lock
620  *     task->pi_lock
621  *
622  * Step	Description				Protected by
623  *	function arguments:
624  *	@task					[R]
625  *	@orig_lock if != NULL			@top_task is blocked on it
626  *	@next_lock				Unprotected. Cannot be
627  *						dereferenced. Only used for
628  *						comparison.
629  *	@orig_waiter if != NULL			@top_task is blocked on it
630  *	@top_task				current, or in case of proxy
631  *						locking protected by calling
632  *						code
633  *	again:
634  *	  loop_sanity_check();
635  *	retry:
636  * [1]	  lock(task->pi_lock);			[R] acquire [P1]
637  * [2]	  waiter = task->pi_blocked_on;		[P1]
638  * [3]	  check_exit_conditions_1();		[P1]
639  * [4]	  lock = waiter->lock;			[P1]
640  * [5]	  if (!try_lock(lock->wait_lock)) {	[P1] try to acquire [L]
641  *	    unlock(task->pi_lock);		release [P1]
642  *	    goto retry;
643  *	  }
644  * [6]	  check_exit_conditions_2();		[P1] + [L]
645  * [7]	  requeue_lock_waiter(lock, waiter);	[P1] + [L]
646  * [8]	  unlock(task->pi_lock);		release [P1]
647  *	  put_task_struct(task);		release [R]
648  * [9]	  check_exit_conditions_3();		[L]
649  * [10]	  task = owner(lock);			[L]
650  *	  get_task_struct(task);		[L] acquire [R]
651  *	  lock(task->pi_lock);			[L] acquire [P2]
652  * [11]	  requeue_pi_waiter(tsk, waiters(lock));[P2] + [L]
653  * [12]	  check_exit_conditions_4();		[P2] + [L]
654  * [13]	  unlock(task->pi_lock);		release [P2]
655  *	  unlock(lock->wait_lock);		release [L]
656  *	  goto again;
657  *
658  * Where P1 is the blocking task and P2 is the lock owner; going up one step
659  * the owner becomes the next blocked task etc..
660  *
661 *
662  */
rt_mutex_adjust_prio_chain(struct task_struct * task,enum rtmutex_chainwalk chwalk,struct rt_mutex_base * orig_lock,struct rt_mutex_base * next_lock,struct rt_mutex_waiter * orig_waiter,struct task_struct * top_task)663 static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
664 					      enum rtmutex_chainwalk chwalk,
665 					      struct rt_mutex_base *orig_lock,
666 					      struct rt_mutex_base *next_lock,
667 					      struct rt_mutex_waiter *orig_waiter,
668 					      struct task_struct *top_task)
669 {
670 	struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
671 	struct rt_mutex_waiter *prerequeue_top_waiter;
672 	int ret = 0, depth = 0;
673 	struct rt_mutex_base *lock;
674 	bool detect_deadlock;
675 	bool requeue = true;
676 
677 	detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
678 
679 	/*
680 	 * The (de)boosting is a step by step approach with a lot of
681 	 * pitfalls. We want this to be preemptible and we want hold a
682 	 * maximum of two locks per step. So we have to check
683 	 * carefully whether things change under us.
684 	 */
685  again:
686 	/*
687 	 * We limit the lock chain length for each invocation.
688 	 */
689 	if (++depth > max_lock_depth) {
690 		static int prev_max;
691 
692 		/*
693 		 * Print this only once. If the admin changes the limit,
694 		 * print a new message when reaching the limit again.
695 		 */
696 		if (prev_max != max_lock_depth) {
697 			prev_max = max_lock_depth;
698 			printk(KERN_WARNING "Maximum lock depth %d reached "
699 			       "task: %s (%d)\n", max_lock_depth,
700 			       top_task->comm, task_pid_nr(top_task));
701 		}
702 		put_task_struct(task);
703 
704 		return -EDEADLK;
705 	}
706 
707 	/*
708 	 * We are fully preemptible here and only hold the refcount on
709 	 * @task. So everything can have changed under us since the
710 	 * caller or our own code below (goto retry/again) dropped all
711 	 * locks.
712 	 */
713  retry:
714 	/*
715 	 * [1] Task cannot go away as we did a get_task() before !
716 	 */
717 	raw_spin_lock_irq(&task->pi_lock);
718 
719 	/*
720 	 * [2] Get the waiter on which @task is blocked on.
721 	 */
722 	waiter = task->pi_blocked_on;
723 
724 	/*
725 	 * [3] check_exit_conditions_1() protected by task->pi_lock.
726 	 */
727 
728 	/*
729 	 * Check whether the end of the boosting chain has been
730 	 * reached or the state of the chain has changed while we
731 	 * dropped the locks.
732 	 */
733 	if (!waiter)
734 		goto out_unlock_pi;
735 
736 	/*
737 	 * Check the orig_waiter state. After we dropped the locks,
738 	 * the previous owner of the lock might have released the lock.
739 	 */
740 	if (orig_waiter && !rt_mutex_owner(orig_lock))
741 		goto out_unlock_pi;
742 
743 	/*
744 	 * We dropped all locks after taking a refcount on @task, so
745 	 * the task might have moved on in the lock chain or even left
746 	 * the chain completely and blocks now on an unrelated lock or
747 	 * on @orig_lock.
748 	 *
749 	 * We stored the lock on which @task was blocked in @next_lock,
750 	 * so we can detect the chain change.
751 	 */
752 	if (next_lock != waiter->lock)
753 		goto out_unlock_pi;
754 
755 	/*
756 	 * There could be 'spurious' loops in the lock graph due to ww_mutex,
757 	 * consider:
758 	 *
759 	 *   P1: A, ww_A, ww_B
760 	 *   P2: ww_B, ww_A
761 	 *   P3: A
762 	 *
763 	 * P3 should not return -EDEADLK because it gets trapped in the cycle
764 	 * created by P1 and P2 (which will resolve -- and runs into
765 	 * max_lock_depth above). Therefore disable detect_deadlock such that
766 	 * the below termination condition can trigger once all relevant tasks
767 	 * are boosted.
768 	 *
769 	 * Even when we start with ww_mutex we can disable deadlock detection,
770 	 * since we would supress a ww_mutex induced deadlock at [6] anyway.
771 	 * Supressing it here however is not sufficient since we might still
772 	 * hit [6] due to adjustment driven iteration.
773 	 *
774 	 * NOTE: if someone were to create a deadlock between 2 ww_classes we'd
775 	 * utterly fail to report it; lockdep should.
776 	 */
777 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && waiter->ww_ctx && detect_deadlock)
778 		detect_deadlock = false;
779 
780 	/*
781 	 * Drop out, when the task has no waiters. Note,
782 	 * top_waiter can be NULL, when we are in the deboosting
783 	 * mode!
784 	 */
785 	if (top_waiter) {
786 		if (!task_has_pi_waiters(task))
787 			goto out_unlock_pi;
788 		/*
789 		 * If deadlock detection is off, we stop here if we
790 		 * are not the top pi waiter of the task. If deadlock
791 		 * detection is enabled we continue, but stop the
792 		 * requeueing in the chain walk.
793 		 */
794 		if (top_waiter != task_top_pi_waiter(task)) {
795 			if (!detect_deadlock)
796 				goto out_unlock_pi;
797 			else
798 				requeue = false;
799 		}
800 	}
801 
802 	/*
803 	 * If the waiter priority is the same as the task priority
804 	 * then there is no further priority adjustment necessary.  If
805 	 * deadlock detection is off, we stop the chain walk. If its
806 	 * enabled we continue, but stop the requeueing in the chain
807 	 * walk.
808 	 */
809 	if (rt_waiter_node_equal(&waiter->tree, task_to_waiter_node(task))) {
810 		if (!detect_deadlock)
811 			goto out_unlock_pi;
812 		else
813 			requeue = false;
814 	}
815 
816 	/*
817 	 * [4] Get the next lock; per holding task->pi_lock we can't unblock
818 	 * and guarantee @lock's existence.
819 	 */
820 	lock = waiter->lock;
821 	/*
822 	 * [5] We need to trylock here as we are holding task->pi_lock,
823 	 * which is the reverse lock order versus the other rtmutex
824 	 * operations.
825 	 *
826 	 * Per the above, holding task->pi_lock guarantees lock exists, so
827 	 * inverting this lock order is infeasible from a life-time
828 	 * perspective.
829 	 */
830 	if (!raw_spin_trylock(&lock->wait_lock)) {
831 		raw_spin_unlock_irq(&task->pi_lock);
832 		cpu_relax();
833 		goto retry;
834 	}
835 
836 	/*
837 	 * [6] check_exit_conditions_2() protected by task->pi_lock and
838 	 * lock->wait_lock.
839 	 *
840 	 * Deadlock detection. If the lock is the same as the original
841 	 * lock which caused us to walk the lock chain or if the
842 	 * current lock is owned by the task which initiated the chain
843 	 * walk, we detected a deadlock.
844 	 */
845 	if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
846 		ret = -EDEADLK;
847 
848 		/*
849 		 * When the deadlock is due to ww_mutex; also see above. Don't
850 		 * report the deadlock and instead let the ww_mutex wound/die
851 		 * logic pick which of the contending threads gets -EDEADLK.
852 		 *
853 		 * NOTE: assumes the cycle only contains a single ww_class; any
854 		 * other configuration and we fail to report; also, see
855 		 * lockdep.
856 		 */
857 		if (IS_ENABLED(CONFIG_PREEMPT_RT) && orig_waiter && orig_waiter->ww_ctx)
858 			ret = 0;
859 
860 		raw_spin_unlock(&lock->wait_lock);
861 		goto out_unlock_pi;
862 	}
863 
864 	/*
865 	 * If we just follow the lock chain for deadlock detection, no
866 	 * need to do all the requeue operations. To avoid a truckload
867 	 * of conditionals around the various places below, just do the
868 	 * minimum chain walk checks.
869 	 */
870 	if (!requeue) {
871 		/*
872 		 * No requeue[7] here. Just release @task [8]
873 		 */
874 		raw_spin_unlock(&task->pi_lock);
875 		put_task_struct(task);
876 
877 		/*
878 		 * [9] check_exit_conditions_3 protected by lock->wait_lock.
879 		 * If there is no owner of the lock, end of chain.
880 		 */
881 		if (!rt_mutex_owner(lock)) {
882 			raw_spin_unlock_irq(&lock->wait_lock);
883 			return 0;
884 		}
885 
886 		/* [10] Grab the next task, i.e. owner of @lock */
887 		task = get_task_struct(rt_mutex_owner(lock));
888 		raw_spin_lock(&task->pi_lock);
889 
890 		/*
891 		 * No requeue [11] here. We just do deadlock detection.
892 		 *
893 		 * [12] Store whether owner is blocked
894 		 * itself. Decision is made after dropping the locks
895 		 */
896 		next_lock = task_blocked_on_lock(task);
897 		/*
898 		 * Get the top waiter for the next iteration
899 		 */
900 		top_waiter = rt_mutex_top_waiter(lock);
901 
902 		/* [13] Drop locks */
903 		raw_spin_unlock(&task->pi_lock);
904 		raw_spin_unlock_irq(&lock->wait_lock);
905 
906 		/* If owner is not blocked, end of chain. */
907 		if (!next_lock)
908 			goto out_put_task;
909 		goto again;
910 	}
911 
912 	/*
913 	 * Store the current top waiter before doing the requeue
914 	 * operation on @lock. We need it for the boost/deboost
915 	 * decision below.
916 	 */
917 	prerequeue_top_waiter = rt_mutex_top_waiter(lock);
918 
919 	/* [7] Requeue the waiter in the lock waiter tree. */
920 	rt_mutex_dequeue(lock, waiter);
921 
922 	/*
923 	 * Update the waiter prio fields now that we're dequeued.
924 	 *
925 	 * These values can have changed through either:
926 	 *
927 	 *   sys_sched_set_scheduler() / sys_sched_setattr()
928 	 *
929 	 * or
930 	 *
931 	 *   DL CBS enforcement advancing the effective deadline.
932 	 */
933 	waiter_update_prio(waiter, task);
934 
935 	rt_mutex_enqueue(lock, waiter);
936 
937 	/*
938 	 * [8] Release the (blocking) task in preparation for
939 	 * taking the owner task in [10].
940 	 *
941 	 * Since we hold lock->waiter_lock, task cannot unblock, even if we
942 	 * release task->pi_lock.
943 	 */
944 	raw_spin_unlock(&task->pi_lock);
945 	put_task_struct(task);
946 
947 	/*
948 	 * [9] check_exit_conditions_3 protected by lock->wait_lock.
949 	 *
950 	 * We must abort the chain walk if there is no lock owner even
951 	 * in the dead lock detection case, as we have nothing to
952 	 * follow here. This is the end of the chain we are walking.
953 	 */
954 	if (!rt_mutex_owner(lock)) {
955 		/*
956 		 * If the requeue [7] above changed the top waiter,
957 		 * then we need to wake the new top waiter up to try
958 		 * to get the lock.
959 		 */
960 		top_waiter = rt_mutex_top_waiter(lock);
961 		if (prerequeue_top_waiter != top_waiter)
962 			wake_up_state(top_waiter->task, top_waiter->wake_state);
963 		raw_spin_unlock_irq(&lock->wait_lock);
964 		return 0;
965 	}
966 
967 	/*
968 	 * [10] Grab the next task, i.e. the owner of @lock
969 	 *
970 	 * Per holding lock->wait_lock and checking for !owner above, there
971 	 * must be an owner and it cannot go away.
972 	 */
973 	task = get_task_struct(rt_mutex_owner(lock));
974 	raw_spin_lock(&task->pi_lock);
975 
976 	/* [11] requeue the pi waiters if necessary */
977 	if (waiter == rt_mutex_top_waiter(lock)) {
978 		/*
979 		 * The waiter became the new top (highest priority)
980 		 * waiter on the lock. Replace the previous top waiter
981 		 * in the owner tasks pi waiters tree with this waiter
982 		 * and adjust the priority of the owner.
983 		 */
984 		rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
985 		waiter_clone_prio(waiter, task);
986 		rt_mutex_enqueue_pi(task, waiter);
987 		rt_mutex_adjust_prio(lock, task);
988 
989 	} else if (prerequeue_top_waiter == waiter) {
990 		/*
991 		 * The waiter was the top waiter on the lock, but is
992 		 * no longer the top priority waiter. Replace waiter in
993 		 * the owner tasks pi waiters tree with the new top
994 		 * (highest priority) waiter and adjust the priority
995 		 * of the owner.
996 		 * The new top waiter is stored in @waiter so that
997 		 * @waiter == @top_waiter evaluates to true below and
998 		 * we continue to deboost the rest of the chain.
999 		 */
1000 		rt_mutex_dequeue_pi(task, waiter);
1001 		waiter = rt_mutex_top_waiter(lock);
1002 		waiter_clone_prio(waiter, task);
1003 		rt_mutex_enqueue_pi(task, waiter);
1004 		rt_mutex_adjust_prio(lock, task);
1005 	} else {
1006 		/*
1007 		 * Nothing changed. No need to do any priority
1008 		 * adjustment.
1009 		 */
1010 	}
1011 
1012 	/*
1013 	 * [12] check_exit_conditions_4() protected by task->pi_lock
1014 	 * and lock->wait_lock. The actual decisions are made after we
1015 	 * dropped the locks.
1016 	 *
1017 	 * Check whether the task which owns the current lock is pi
1018 	 * blocked itself. If yes we store a pointer to the lock for
1019 	 * the lock chain change detection above. After we dropped
1020 	 * task->pi_lock next_lock cannot be dereferenced anymore.
1021 	 */
1022 	next_lock = task_blocked_on_lock(task);
1023 	/*
1024 	 * Store the top waiter of @lock for the end of chain walk
1025 	 * decision below.
1026 	 */
1027 	top_waiter = rt_mutex_top_waiter(lock);
1028 
1029 	/* [13] Drop the locks */
1030 	raw_spin_unlock(&task->pi_lock);
1031 	raw_spin_unlock_irq(&lock->wait_lock);
1032 
1033 	/*
1034 	 * Make the actual exit decisions [12], based on the stored
1035 	 * values.
1036 	 *
1037 	 * We reached the end of the lock chain. Stop right here. No
1038 	 * point to go back just to figure that out.
1039 	 */
1040 	if (!next_lock)
1041 		goto out_put_task;
1042 
1043 	/*
1044 	 * If the current waiter is not the top waiter on the lock,
1045 	 * then we can stop the chain walk here if we are not in full
1046 	 * deadlock detection mode.
1047 	 */
1048 	if (!detect_deadlock && waiter != top_waiter)
1049 		goto out_put_task;
1050 
1051 	goto again;
1052 
1053  out_unlock_pi:
1054 	raw_spin_unlock_irq(&task->pi_lock);
1055  out_put_task:
1056 	put_task_struct(task);
1057 
1058 	return ret;
1059 }
1060 
1061 /*
1062  * Try to take an rt-mutex
1063  *
1064  * Must be called with lock->wait_lock held and interrupts disabled
1065  *
1066  * @lock:   The lock to be acquired.
1067  * @task:   The task which wants to acquire the lock
1068  * @waiter: The waiter that is queued to the lock's wait tree if the
1069  *	    callsite called task_blocked_on_lock(), otherwise NULL
1070  */
1071 static int __sched
try_to_take_rt_mutex(struct rt_mutex_base * lock,struct task_struct * task,struct rt_mutex_waiter * waiter)1072 try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
1073 		     struct rt_mutex_waiter *waiter)
1074 {
1075 	lockdep_assert_held(&lock->wait_lock);
1076 
1077 	/*
1078 	 * Before testing whether we can acquire @lock, we set the
1079 	 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
1080 	 * other tasks which try to modify @lock into the slow path
1081 	 * and they serialize on @lock->wait_lock.
1082 	 *
1083 	 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
1084 	 * as explained at the top of this file if and only if:
1085 	 *
1086 	 * - There is a lock owner. The caller must fixup the
1087 	 *   transient state if it does a trylock or leaves the lock
1088 	 *   function due to a signal or timeout.
1089 	 *
1090 	 * - @task acquires the lock and there are no other
1091 	 *   waiters. This is undone in rt_mutex_set_owner(@task) at
1092 	 *   the end of this function.
1093 	 */
1094 	mark_rt_mutex_waiters(lock);
1095 
1096 	/*
1097 	 * If @lock has an owner, give up.
1098 	 */
1099 	if (rt_mutex_owner(lock))
1100 		return 0;
1101 
1102 	/*
1103 	 * If @waiter != NULL, @task has already enqueued the waiter
1104 	 * into @lock waiter tree. If @waiter == NULL then this is a
1105 	 * trylock attempt.
1106 	 */
1107 	if (waiter) {
1108 		struct rt_mutex_waiter *top_waiter = rt_mutex_top_waiter(lock);
1109 
1110 		/*
1111 		 * If waiter is the highest priority waiter of @lock,
1112 		 * or allowed to steal it, take it over.
1113 		 */
1114 		if (waiter == top_waiter || rt_mutex_steal(waiter, top_waiter)) {
1115 			/*
1116 			 * We can acquire the lock. Remove the waiter from the
1117 			 * lock waiters tree.
1118 			 */
1119 			rt_mutex_dequeue(lock, waiter);
1120 		} else {
1121 			return 0;
1122 		}
1123 	} else {
1124 		/*
1125 		 * If the lock has waiters already we check whether @task is
1126 		 * eligible to take over the lock.
1127 		 *
1128 		 * If there are no other waiters, @task can acquire
1129 		 * the lock.  @task->pi_blocked_on is NULL, so it does
1130 		 * not need to be dequeued.
1131 		 */
1132 		if (rt_mutex_has_waiters(lock)) {
1133 			/* Check whether the trylock can steal it. */
1134 			if (!rt_mutex_steal(task_to_waiter(task),
1135 					    rt_mutex_top_waiter(lock)))
1136 				return 0;
1137 
1138 			/*
1139 			 * The current top waiter stays enqueued. We
1140 			 * don't have to change anything in the lock
1141 			 * waiters order.
1142 			 */
1143 		} else {
1144 			/*
1145 			 * No waiters. Take the lock without the
1146 			 * pi_lock dance.@task->pi_blocked_on is NULL
1147 			 * and we have no waiters to enqueue in @task
1148 			 * pi waiters tree.
1149 			 */
1150 			goto takeit;
1151 		}
1152 	}
1153 
1154 	/*
1155 	 * Clear @task->pi_blocked_on. Requires protection by
1156 	 * @task->pi_lock. Redundant operation for the @waiter == NULL
1157 	 * case, but conditionals are more expensive than a redundant
1158 	 * store.
1159 	 */
1160 	raw_spin_lock(&task->pi_lock);
1161 	task->pi_blocked_on = NULL;
1162 	/*
1163 	 * Finish the lock acquisition. @task is the new owner. If
1164 	 * other waiters exist we have to insert the highest priority
1165 	 * waiter into @task->pi_waiters tree.
1166 	 */
1167 	if (rt_mutex_has_waiters(lock))
1168 		rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
1169 	raw_spin_unlock(&task->pi_lock);
1170 
1171 takeit:
1172 	/*
1173 	 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
1174 	 * are still waiters or clears it.
1175 	 */
1176 	rt_mutex_set_owner(lock, task);
1177 
1178 	return 1;
1179 }
1180 
1181 /*
1182  * Task blocks on lock.
1183  *
1184  * Prepare waiter and propagate pi chain
1185  *
1186  * This must be called with lock->wait_lock held and interrupts disabled
1187  */
task_blocks_on_rt_mutex(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * task,struct ww_acquire_ctx * ww_ctx,enum rtmutex_chainwalk chwalk)1188 static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,
1189 					   struct rt_mutex_waiter *waiter,
1190 					   struct task_struct *task,
1191 					   struct ww_acquire_ctx *ww_ctx,
1192 					   enum rtmutex_chainwalk chwalk)
1193 {
1194 	struct task_struct *owner = rt_mutex_owner(lock);
1195 	struct rt_mutex_waiter *top_waiter = waiter;
1196 	struct rt_mutex_base *next_lock;
1197 	int chain_walk = 0, res;
1198 
1199 	lockdep_assert_held(&lock->wait_lock);
1200 
1201 	/*
1202 	 * Early deadlock detection. We really don't want the task to
1203 	 * enqueue on itself just to untangle the mess later. It's not
1204 	 * only an optimization. We drop the locks, so another waiter
1205 	 * can come in before the chain walk detects the deadlock. So
1206 	 * the other will detect the deadlock and return -EDEADLOCK,
1207 	 * which is wrong, as the other waiter is not in a deadlock
1208 	 * situation.
1209 	 *
1210 	 * Except for ww_mutex, in that case the chain walk must already deal
1211 	 * with spurious cycles, see the comments at [3] and [6].
1212 	 */
1213 	if (owner == task && !(build_ww_mutex() && ww_ctx))
1214 		return -EDEADLK;
1215 
1216 	trace_android_vh_task_blocks_on_rtmutex(lock, waiter, task, ww_ctx, &chwalk);
1217 	raw_spin_lock(&task->pi_lock);
1218 	waiter->task = task;
1219 	waiter->lock = lock;
1220 	waiter_update_prio(waiter, task);
1221 	waiter_clone_prio(waiter, task);
1222 
1223 	/* Get the top priority waiter on the lock */
1224 	if (rt_mutex_has_waiters(lock))
1225 		top_waiter = rt_mutex_top_waiter(lock);
1226 	rt_mutex_enqueue(lock, waiter);
1227 
1228 	task->pi_blocked_on = waiter;
1229 
1230 	raw_spin_unlock(&task->pi_lock);
1231 
1232 	if (build_ww_mutex() && ww_ctx) {
1233 		struct rt_mutex *rtm;
1234 
1235 		/* Check whether the waiter should back out immediately */
1236 		rtm = container_of(lock, struct rt_mutex, rtmutex);
1237 		res = __ww_mutex_add_waiter(waiter, rtm, ww_ctx);
1238 		if (res) {
1239 			raw_spin_lock(&task->pi_lock);
1240 			rt_mutex_dequeue(lock, waiter);
1241 			task->pi_blocked_on = NULL;
1242 			raw_spin_unlock(&task->pi_lock);
1243 			return res;
1244 		}
1245 	}
1246 
1247 	if (!owner)
1248 		return 0;
1249 
1250 	raw_spin_lock(&owner->pi_lock);
1251 	if (waiter == rt_mutex_top_waiter(lock)) {
1252 		rt_mutex_dequeue_pi(owner, top_waiter);
1253 		rt_mutex_enqueue_pi(owner, waiter);
1254 
1255 		rt_mutex_adjust_prio(lock, owner);
1256 		if (owner->pi_blocked_on)
1257 			chain_walk = 1;
1258 	} else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
1259 		chain_walk = 1;
1260 	}
1261 
1262 	/* Store the lock on which owner is blocked or NULL */
1263 	next_lock = task_blocked_on_lock(owner);
1264 
1265 	raw_spin_unlock(&owner->pi_lock);
1266 	/*
1267 	 * Even if full deadlock detection is on, if the owner is not
1268 	 * blocked itself, we can avoid finding this out in the chain
1269 	 * walk.
1270 	 */
1271 	if (!chain_walk || !next_lock)
1272 		return 0;
1273 
1274 	/*
1275 	 * The owner can't disappear while holding a lock,
1276 	 * so the owner struct is protected by wait_lock.
1277 	 * Gets dropped in rt_mutex_adjust_prio_chain()!
1278 	 */
1279 	get_task_struct(owner);
1280 
1281 	raw_spin_unlock_irq(&lock->wait_lock);
1282 
1283 	res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
1284 					 next_lock, waiter, task);
1285 
1286 	raw_spin_lock_irq(&lock->wait_lock);
1287 
1288 	return res;
1289 }
1290 
1291 /*
1292  * Remove the top waiter from the current tasks pi waiter tree and
1293  * queue it up.
1294  *
1295  * Called with lock->wait_lock held and interrupts disabled.
1296  */
mark_wakeup_next_waiter(struct rt_wake_q_head * wqh,struct rt_mutex_base * lock)1297 static void __sched mark_wakeup_next_waiter(struct rt_wake_q_head *wqh,
1298 					    struct rt_mutex_base *lock)
1299 {
1300 	struct rt_mutex_waiter *waiter;
1301 
1302 	lockdep_assert_held(&lock->wait_lock);
1303 
1304 	raw_spin_lock(&current->pi_lock);
1305 
1306 	waiter = rt_mutex_top_waiter(lock);
1307 
1308 	/*
1309 	 * Remove it from current->pi_waiters and deboost.
1310 	 *
1311 	 * We must in fact deboost here in order to ensure we call
1312 	 * rt_mutex_setprio() to update p->pi_top_task before the
1313 	 * task unblocks.
1314 	 */
1315 	rt_mutex_dequeue_pi(current, waiter);
1316 	rt_mutex_adjust_prio(lock, current);
1317 
1318 	/*
1319 	 * As we are waking up the top waiter, and the waiter stays
1320 	 * queued on the lock until it gets the lock, this lock
1321 	 * obviously has waiters. Just set the bit here and this has
1322 	 * the added benefit of forcing all new tasks into the
1323 	 * slow path making sure no task of lower priority than
1324 	 * the top waiter can steal this lock.
1325 	 */
1326 	lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
1327 
1328 	/*
1329 	 * We deboosted before waking the top waiter task such that we don't
1330 	 * run two tasks with the 'same' priority (and ensure the
1331 	 * p->pi_top_task pointer points to a blocked task). This however can
1332 	 * lead to priority inversion if we would get preempted after the
1333 	 * deboost but before waking our donor task, hence the preempt_disable()
1334 	 * before unlock.
1335 	 *
1336 	 * Pairs with preempt_enable() in rt_mutex_wake_up_q();
1337 	 */
1338 	preempt_disable();
1339 	rt_mutex_wake_q_add(wqh, waiter);
1340 	raw_spin_unlock(&current->pi_lock);
1341 }
1342 
__rt_mutex_slowtrylock(struct rt_mutex_base * lock)1343 static int __sched __rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1344 {
1345 	int ret = try_to_take_rt_mutex(lock, current, NULL);
1346 
1347 	/*
1348 	 * try_to_take_rt_mutex() sets the lock waiters bit
1349 	 * unconditionally. Clean this up.
1350 	 */
1351 	fixup_rt_mutex_waiters(lock, true);
1352 
1353 	return ret;
1354 }
1355 
1356 /*
1357  * Slow path try-lock function:
1358  */
rt_mutex_slowtrylock(struct rt_mutex_base * lock)1359 static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1360 {
1361 	unsigned long flags;
1362 	int ret;
1363 
1364 	/*
1365 	 * If the lock already has an owner we fail to get the lock.
1366 	 * This can be done without taking the @lock->wait_lock as
1367 	 * it is only being read, and this is a trylock anyway.
1368 	 */
1369 	if (rt_mutex_owner(lock))
1370 		return 0;
1371 
1372 	/*
1373 	 * The mutex has currently no owner. Lock the wait lock and try to
1374 	 * acquire the lock. We use irqsave here to support early boot calls.
1375 	 */
1376 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1377 
1378 	ret = __rt_mutex_slowtrylock(lock);
1379 
1380 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1381 
1382 	return ret;
1383 }
1384 
__rt_mutex_trylock(struct rt_mutex_base * lock)1385 static __always_inline int __rt_mutex_trylock(struct rt_mutex_base *lock)
1386 {
1387 	if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1388 		return 1;
1389 
1390 	return rt_mutex_slowtrylock(lock);
1391 }
1392 
1393 /*
1394  * Slow path to release a rt-mutex.
1395  */
rt_mutex_slowunlock(struct rt_mutex_base * lock)1396 static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
1397 {
1398 	DEFINE_RT_WAKE_Q(wqh);
1399 	unsigned long flags;
1400 
1401 	/* irqsave required to support early boot calls */
1402 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1403 
1404 	debug_rt_mutex_unlock(lock);
1405 
1406 	/*
1407 	 * We must be careful here if the fast path is enabled. If we
1408 	 * have no waiters queued we cannot set owner to NULL here
1409 	 * because of:
1410 	 *
1411 	 * foo->lock->owner = NULL;
1412 	 *			rtmutex_lock(foo->lock);   <- fast path
1413 	 *			free = atomic_dec_and_test(foo->refcnt);
1414 	 *			rtmutex_unlock(foo->lock); <- fast path
1415 	 *			if (free)
1416 	 *				kfree(foo);
1417 	 * raw_spin_unlock(foo->lock->wait_lock);
1418 	 *
1419 	 * So for the fastpath enabled kernel:
1420 	 *
1421 	 * Nothing can set the waiters bit as long as we hold
1422 	 * lock->wait_lock. So we do the following sequence:
1423 	 *
1424 	 *	owner = rt_mutex_owner(lock);
1425 	 *	clear_rt_mutex_waiters(lock);
1426 	 *	raw_spin_unlock(&lock->wait_lock);
1427 	 *	if (cmpxchg(&lock->owner, owner, 0) == owner)
1428 	 *		return;
1429 	 *	goto retry;
1430 	 *
1431 	 * The fastpath disabled variant is simple as all access to
1432 	 * lock->owner is serialized by lock->wait_lock:
1433 	 *
1434 	 *	lock->owner = NULL;
1435 	 *	raw_spin_unlock(&lock->wait_lock);
1436 	 */
1437 	while (!rt_mutex_has_waiters(lock)) {
1438 		/* Drops lock->wait_lock ! */
1439 		if (unlock_rt_mutex_safe(lock, flags) == true)
1440 			return;
1441 		/* Relock the rtmutex and try again */
1442 		raw_spin_lock_irqsave(&lock->wait_lock, flags);
1443 	}
1444 
1445 	/*
1446 	 * The wakeup next waiter path does not suffer from the above
1447 	 * race. See the comments there.
1448 	 *
1449 	 * Queue the next waiter for wakeup once we release the wait_lock.
1450 	 */
1451 	mark_wakeup_next_waiter(&wqh, lock);
1452 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1453 
1454 	rt_mutex_wake_up_q(&wqh);
1455 }
1456 
__rt_mutex_unlock(struct rt_mutex_base * lock)1457 static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
1458 {
1459 	if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1460 		return;
1461 
1462 	rt_mutex_slowunlock(lock);
1463 }
1464 
1465 #ifdef CONFIG_SMP
rtmutex_spin_on_owner(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * owner)1466 static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
1467 				  struct rt_mutex_waiter *waiter,
1468 				  struct task_struct *owner)
1469 {
1470 	bool res = true;
1471 
1472 	rcu_read_lock();
1473 	for (;;) {
1474 		/* If owner changed, trylock again. */
1475 		if (owner != rt_mutex_owner(lock))
1476 			break;
1477 		/*
1478 		 * Ensure that @owner is dereferenced after checking that
1479 		 * the lock owner still matches @owner. If that fails,
1480 		 * @owner might point to freed memory. If it still matches,
1481 		 * the rcu_read_lock() ensures the memory stays valid.
1482 		 */
1483 		barrier();
1484 		/*
1485 		 * Stop spinning when:
1486 		 *  - the lock owner has been scheduled out
1487 		 *  - current is not longer the top waiter
1488 		 *  - current is requested to reschedule (redundant
1489 		 *    for CONFIG_PREEMPT_RCU=y)
1490 		 *  - the VCPU on which owner runs is preempted
1491 		 */
1492 		if (!owner_on_cpu(owner) || need_resched() ||
1493 		    !rt_mutex_waiter_is_top_waiter(lock, waiter)) {
1494 			res = false;
1495 			break;
1496 		}
1497 		cpu_relax();
1498 	}
1499 	rcu_read_unlock();
1500 	return res;
1501 }
1502 #else
rtmutex_spin_on_owner(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * owner)1503 static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
1504 				  struct rt_mutex_waiter *waiter,
1505 				  struct task_struct *owner)
1506 {
1507 	return false;
1508 }
1509 #endif
1510 
1511 #ifdef RT_MUTEX_BUILD_MUTEX
1512 /*
1513  * Functions required for:
1514  *	- rtmutex, futex on all kernels
1515  *	- mutex and rwsem substitutions on RT kernels
1516  */
1517 
1518 /*
1519  * Remove a waiter from a lock and give up
1520  *
1521  * Must be called with lock->wait_lock held and interrupts disabled. It must
1522  * have just failed to try_to_take_rt_mutex().
1523  */
remove_waiter(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)1524 static void __sched remove_waiter(struct rt_mutex_base *lock,
1525 				  struct rt_mutex_waiter *waiter)
1526 {
1527 	bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
1528 	struct task_struct *owner = rt_mutex_owner(lock);
1529 	struct rt_mutex_base *next_lock;
1530 
1531 	lockdep_assert_held(&lock->wait_lock);
1532 
1533 	raw_spin_lock(&current->pi_lock);
1534 	rt_mutex_dequeue(lock, waiter);
1535 	current->pi_blocked_on = NULL;
1536 	raw_spin_unlock(&current->pi_lock);
1537 
1538 	/*
1539 	 * Only update priority if the waiter was the highest priority
1540 	 * waiter of the lock and there is an owner to update.
1541 	 */
1542 	if (!owner || !is_top_waiter)
1543 		return;
1544 
1545 	raw_spin_lock(&owner->pi_lock);
1546 
1547 	rt_mutex_dequeue_pi(owner, waiter);
1548 
1549 	if (rt_mutex_has_waiters(lock))
1550 		rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
1551 
1552 	rt_mutex_adjust_prio(lock, owner);
1553 
1554 	/* Store the lock on which owner is blocked or NULL */
1555 	next_lock = task_blocked_on_lock(owner);
1556 
1557 	raw_spin_unlock(&owner->pi_lock);
1558 
1559 	/*
1560 	 * Don't walk the chain, if the owner task is not blocked
1561 	 * itself.
1562 	 */
1563 	if (!next_lock)
1564 		return;
1565 
1566 	/* gets dropped in rt_mutex_adjust_prio_chain()! */
1567 	get_task_struct(owner);
1568 
1569 	raw_spin_unlock_irq(&lock->wait_lock);
1570 
1571 	rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1572 				   next_lock, NULL, current);
1573 
1574 	raw_spin_lock_irq(&lock->wait_lock);
1575 }
1576 
1577 /**
1578  * rt_mutex_slowlock_block() - Perform the wait-wake-try-to-take loop
1579  * @lock:		 the rt_mutex to take
1580  * @ww_ctx:		 WW mutex context pointer
1581  * @state:		 the state the task should block in (TASK_INTERRUPTIBLE
1582  *			 or TASK_UNINTERRUPTIBLE)
1583  * @timeout:		 the pre-initialized and started timer, or NULL for none
1584  * @waiter:		 the pre-initialized rt_mutex_waiter
1585  *
1586  * Must be called with lock->wait_lock held and interrupts disabled
1587  */
rt_mutex_slowlock_block(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state,struct hrtimer_sleeper * timeout,struct rt_mutex_waiter * waiter)1588 static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock,
1589 					   struct ww_acquire_ctx *ww_ctx,
1590 					   unsigned int state,
1591 					   struct hrtimer_sleeper *timeout,
1592 					   struct rt_mutex_waiter *waiter)
1593 {
1594 	struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
1595 	struct task_struct *owner;
1596 	int ret = 0;
1597 
1598 	trace_android_vh_rtmutex_wait_start(lock);
1599 	for (;;) {
1600 		/* Try to acquire the lock: */
1601 		if (try_to_take_rt_mutex(lock, current, waiter))
1602 			break;
1603 
1604 		if (timeout && !timeout->task) {
1605 			ret = -ETIMEDOUT;
1606 			break;
1607 		}
1608 		if (signal_pending_state(state, current)) {
1609 			ret = -EINTR;
1610 			break;
1611 		}
1612 
1613 		if (build_ww_mutex() && ww_ctx) {
1614 			ret = __ww_mutex_check_kill(rtm, waiter, ww_ctx);
1615 			if (ret)
1616 				break;
1617 		}
1618 
1619 		if (waiter == rt_mutex_top_waiter(lock))
1620 			owner = rt_mutex_owner(lock);
1621 		else
1622 			owner = NULL;
1623 		raw_spin_unlock_irq(&lock->wait_lock);
1624 
1625 		if (!owner || !rtmutex_spin_on_owner(lock, waiter, owner))
1626 			schedule();
1627 
1628 		raw_spin_lock_irq(&lock->wait_lock);
1629 		set_current_state(state);
1630 	}
1631 
1632 	trace_android_vh_rtmutex_wait_finish(lock);
1633 	__set_current_state(TASK_RUNNING);
1634 	return ret;
1635 }
1636 
rt_mutex_handle_deadlock(int res,int detect_deadlock,struct rt_mutex_waiter * w)1637 static void __sched rt_mutex_handle_deadlock(int res, int detect_deadlock,
1638 					     struct rt_mutex_waiter *w)
1639 {
1640 	/*
1641 	 * If the result is not -EDEADLOCK or the caller requested
1642 	 * deadlock detection, nothing to do here.
1643 	 */
1644 	if (res != -EDEADLOCK || detect_deadlock)
1645 		return;
1646 
1647 	if (build_ww_mutex() && w->ww_ctx)
1648 		return;
1649 
1650 	/*
1651 	 * Yell loudly and stop the task right here.
1652 	 */
1653 	WARN(1, "rtmutex deadlock detected\n");
1654 	while (1) {
1655 		set_current_state(TASK_INTERRUPTIBLE);
1656 		schedule();
1657 	}
1658 }
1659 
1660 /**
1661  * __rt_mutex_slowlock - Locking slowpath invoked with lock::wait_lock held
1662  * @lock:	The rtmutex to block lock
1663  * @ww_ctx:	WW mutex context pointer
1664  * @state:	The task state for sleeping
1665  * @chwalk:	Indicator whether full or partial chainwalk is requested
1666  * @waiter:	Initializer waiter for blocking
1667  */
__rt_mutex_slowlock(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state,enum rtmutex_chainwalk chwalk,struct rt_mutex_waiter * waiter)1668 static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,
1669 				       struct ww_acquire_ctx *ww_ctx,
1670 				       unsigned int state,
1671 				       enum rtmutex_chainwalk chwalk,
1672 				       struct rt_mutex_waiter *waiter)
1673 {
1674 	struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
1675 	struct ww_mutex *ww = ww_container_of(rtm);
1676 	int ret;
1677 
1678 	lockdep_assert_held(&lock->wait_lock);
1679 
1680 	/* Try to acquire the lock again: */
1681 	if (try_to_take_rt_mutex(lock, current, NULL)) {
1682 		if (build_ww_mutex() && ww_ctx) {
1683 			__ww_mutex_check_waiters(rtm, ww_ctx);
1684 			ww_mutex_lock_acquired(ww, ww_ctx);
1685 		}
1686 		return 0;
1687 	}
1688 
1689 	set_current_state(state);
1690 
1691 	trace_contention_begin(lock, LCB_F_RT);
1692 
1693 	ret = task_blocks_on_rt_mutex(lock, waiter, current, ww_ctx, chwalk);
1694 	if (likely(!ret))
1695 		ret = rt_mutex_slowlock_block(lock, ww_ctx, state, NULL, waiter);
1696 
1697 	if (likely(!ret)) {
1698 		/* acquired the lock */
1699 		if (build_ww_mutex() && ww_ctx) {
1700 			if (!ww_ctx->is_wait_die)
1701 				__ww_mutex_check_waiters(rtm, ww_ctx);
1702 			ww_mutex_lock_acquired(ww, ww_ctx);
1703 		}
1704 	} else {
1705 		__set_current_state(TASK_RUNNING);
1706 		remove_waiter(lock, waiter);
1707 		rt_mutex_handle_deadlock(ret, chwalk, waiter);
1708 	}
1709 
1710 	/*
1711 	 * try_to_take_rt_mutex() sets the waiter bit
1712 	 * unconditionally. We might have to fix that up.
1713 	 */
1714 	fixup_rt_mutex_waiters(lock, true);
1715 
1716 	trace_contention_end(lock, ret);
1717 
1718 	return ret;
1719 }
1720 
__rt_mutex_slowlock_locked(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state)1721 static inline int __rt_mutex_slowlock_locked(struct rt_mutex_base *lock,
1722 					     struct ww_acquire_ctx *ww_ctx,
1723 					     unsigned int state)
1724 {
1725 	struct rt_mutex_waiter waiter;
1726 	int ret;
1727 
1728 	rt_mutex_init_waiter(&waiter);
1729 	waiter.ww_ctx = ww_ctx;
1730 
1731 	ret = __rt_mutex_slowlock(lock, ww_ctx, state, RT_MUTEX_MIN_CHAINWALK,
1732 				  &waiter);
1733 
1734 	debug_rt_mutex_free_waiter(&waiter);
1735 	return ret;
1736 }
1737 
1738 /*
1739  * rt_mutex_slowlock - Locking slowpath invoked when fast path fails
1740  * @lock:	The rtmutex to block lock
1741  * @ww_ctx:	WW mutex context pointer
1742  * @state:	The task state for sleeping
1743  */
rt_mutex_slowlock(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state)1744 static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock,
1745 				     struct ww_acquire_ctx *ww_ctx,
1746 				     unsigned int state)
1747 {
1748 	unsigned long flags;
1749 	int ret;
1750 
1751 	/*
1752 	 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1753 	 * be called in early boot if the cmpxchg() fast path is disabled
1754 	 * (debug, no architecture support). In this case we will acquire the
1755 	 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1756 	 * enable interrupts in that early boot case. So we need to use the
1757 	 * irqsave/restore variants.
1758 	 */
1759 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1760 	ret = __rt_mutex_slowlock_locked(lock, ww_ctx, state);
1761 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1762 
1763 	return ret;
1764 }
1765 
__rt_mutex_lock(struct rt_mutex_base * lock,unsigned int state)1766 static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock,
1767 					   unsigned int state)
1768 {
1769 	if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1770 		return 0;
1771 
1772 	return rt_mutex_slowlock(lock, NULL, state);
1773 }
1774 #endif /* RT_MUTEX_BUILD_MUTEX */
1775 
1776 #ifdef RT_MUTEX_BUILD_SPINLOCKS
1777 /*
1778  * Functions required for spin/rw_lock substitution on RT kernels
1779  */
1780 
1781 /**
1782  * rtlock_slowlock_locked - Slow path lock acquisition for RT locks
1783  * @lock:	The underlying RT mutex
1784  */
rtlock_slowlock_locked(struct rt_mutex_base * lock)1785 static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock)
1786 {
1787 	struct rt_mutex_waiter waiter;
1788 	struct task_struct *owner;
1789 
1790 	lockdep_assert_held(&lock->wait_lock);
1791 
1792 	if (try_to_take_rt_mutex(lock, current, NULL))
1793 		return;
1794 
1795 	rt_mutex_init_rtlock_waiter(&waiter);
1796 
1797 	/* Save current state and set state to TASK_RTLOCK_WAIT */
1798 	current_save_and_set_rtlock_wait_state();
1799 
1800 	trace_contention_begin(lock, LCB_F_RT);
1801 
1802 	task_blocks_on_rt_mutex(lock, &waiter, current, NULL, RT_MUTEX_MIN_CHAINWALK);
1803 
1804 	for (;;) {
1805 		/* Try to acquire the lock again */
1806 		if (try_to_take_rt_mutex(lock, current, &waiter))
1807 			break;
1808 
1809 		if (&waiter == rt_mutex_top_waiter(lock))
1810 			owner = rt_mutex_owner(lock);
1811 		else
1812 			owner = NULL;
1813 		raw_spin_unlock_irq(&lock->wait_lock);
1814 
1815 		if (!owner || !rtmutex_spin_on_owner(lock, &waiter, owner))
1816 			schedule_rtlock();
1817 
1818 		raw_spin_lock_irq(&lock->wait_lock);
1819 		set_current_state(TASK_RTLOCK_WAIT);
1820 	}
1821 
1822 	/* Restore the task state */
1823 	current_restore_rtlock_saved_state();
1824 
1825 	/*
1826 	 * try_to_take_rt_mutex() sets the waiter bit unconditionally.
1827 	 * We might have to fix that up:
1828 	 */
1829 	fixup_rt_mutex_waiters(lock, true);
1830 	debug_rt_mutex_free_waiter(&waiter);
1831 
1832 	trace_contention_end(lock, 0);
1833 }
1834 
rtlock_slowlock(struct rt_mutex_base * lock)1835 static __always_inline void __sched rtlock_slowlock(struct rt_mutex_base *lock)
1836 {
1837 	unsigned long flags;
1838 
1839 	raw_spin_lock_irqsave(&lock->wait_lock, flags);
1840 	rtlock_slowlock_locked(lock);
1841 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1842 }
1843 
1844 #endif /* RT_MUTEX_BUILD_SPINLOCKS */
1845