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 prio = task->prio;
330 int waiter_prio = 0;
331
332 trace_android_vh_rtmutex_waiter_prio(task, &waiter_prio);
333 if (waiter_prio > 0)
334 return waiter_prio;
335
336 if (!rt_prio(prio))
337 return DEFAULT_PRIO;
338
339 return prio;
340 }
341
342 static __always_inline void
waiter_update_prio(struct rt_mutex_waiter * waiter,struct task_struct * task)343 waiter_update_prio(struct rt_mutex_waiter *waiter, struct task_struct *task)
344 {
345 waiter->prio = __waiter_prio(task);
346 waiter->deadline = task->dl.deadline;
347 }
348
349 /*
350 * Only use with rt_mutex_waiter_{less,equal}()
351 */
352 #define task_to_waiter(p) \
353 &(struct rt_mutex_waiter){ .prio = __waiter_prio(p), .deadline = (p)->dl.deadline }
354
rt_mutex_waiter_less(struct rt_mutex_waiter * left,struct rt_mutex_waiter * right)355 static __always_inline int rt_mutex_waiter_less(struct rt_mutex_waiter *left,
356 struct rt_mutex_waiter *right)
357 {
358 if (left->prio < right->prio)
359 return 1;
360
361 /*
362 * If both waiters have dl_prio(), we check the deadlines of the
363 * associated tasks.
364 * If left waiter has a dl_prio(), and we didn't return 1 above,
365 * then right waiter has a dl_prio() too.
366 */
367 if (dl_prio(left->prio))
368 return dl_time_before(left->deadline, right->deadline);
369
370 return 0;
371 }
372
rt_mutex_waiter_equal(struct rt_mutex_waiter * left,struct rt_mutex_waiter * right)373 static __always_inline int rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
374 struct rt_mutex_waiter *right)
375 {
376 if (left->prio != right->prio)
377 return 0;
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 0 above,
383 * then right waiter has a dl_prio() too.
384 */
385 if (dl_prio(left->prio))
386 return left->deadline == right->deadline;
387
388 return 1;
389 }
390
rt_mutex_steal(struct rt_mutex_waiter * waiter,struct rt_mutex_waiter * top_waiter)391 static inline bool rt_mutex_steal(struct rt_mutex_waiter *waiter,
392 struct rt_mutex_waiter *top_waiter)
393 {
394 bool ret = false;
395
396 if (rt_mutex_waiter_less(waiter, top_waiter))
397 return true;
398
399 trace_android_vh_rt_mutex_steal(waiter->prio, top_waiter->prio, &ret);
400 if (ret)
401 return true;
402
403 #ifdef RT_MUTEX_BUILD_SPINLOCKS
404 /*
405 * Note that RT tasks are excluded from same priority (lateral)
406 * steals to prevent the introduction of an unbounded latency.
407 */
408 if (rt_prio(waiter->prio) || dl_prio(waiter->prio))
409 return false;
410
411 return rt_mutex_waiter_equal(waiter, top_waiter);
412 #else
413 return false;
414 #endif
415 }
416
417 #define __node_2_waiter(node) \
418 rb_entry((node), struct rt_mutex_waiter, tree_entry)
419
__waiter_less(struct rb_node * a,const struct rb_node * b)420 static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_node *b)
421 {
422 struct rt_mutex_waiter *aw = __node_2_waiter(a);
423 struct rt_mutex_waiter *bw = __node_2_waiter(b);
424
425 if (rt_mutex_waiter_less(aw, bw))
426 return 1;
427
428 if (!build_ww_mutex())
429 return 0;
430
431 if (rt_mutex_waiter_less(bw, aw))
432 return 0;
433
434 /* NOTE: relies on waiter->ww_ctx being set before insertion */
435 if (aw->ww_ctx) {
436 if (!bw->ww_ctx)
437 return 1;
438
439 return (signed long)(aw->ww_ctx->stamp -
440 bw->ww_ctx->stamp) < 0;
441 }
442
443 return 0;
444 }
445
446 static __always_inline void
rt_mutex_enqueue(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)447 rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
448 {
449 rb_add_cached(&waiter->tree_entry, &lock->waiters, __waiter_less);
450 }
451
452 static __always_inline void
rt_mutex_dequeue(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)453 rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
454 {
455 if (RB_EMPTY_NODE(&waiter->tree_entry))
456 return;
457
458 rb_erase_cached(&waiter->tree_entry, &lock->waiters);
459 RB_CLEAR_NODE(&waiter->tree_entry);
460 }
461
462 #define __node_2_pi_waiter(node) \
463 rb_entry((node), struct rt_mutex_waiter, pi_tree_entry)
464
465 static __always_inline bool
__pi_waiter_less(struct rb_node * a,const struct rb_node * b)466 __pi_waiter_less(struct rb_node *a, const struct rb_node *b)
467 {
468 return rt_mutex_waiter_less(__node_2_pi_waiter(a), __node_2_pi_waiter(b));
469 }
470
471 static __always_inline void
rt_mutex_enqueue_pi(struct task_struct * task,struct rt_mutex_waiter * waiter)472 rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
473 {
474 rb_add_cached(&waiter->pi_tree_entry, &task->pi_waiters, __pi_waiter_less);
475 }
476
477 static __always_inline void
rt_mutex_dequeue_pi(struct task_struct * task,struct rt_mutex_waiter * waiter)478 rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
479 {
480 if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
481 return;
482
483 rb_erase_cached(&waiter->pi_tree_entry, &task->pi_waiters);
484 RB_CLEAR_NODE(&waiter->pi_tree_entry);
485 }
486
rt_mutex_adjust_prio(struct task_struct * p)487 static __always_inline void rt_mutex_adjust_prio(struct task_struct *p)
488 {
489 struct task_struct *pi_task = NULL;
490
491 lockdep_assert_held(&p->pi_lock);
492
493 if (task_has_pi_waiters(p))
494 pi_task = task_top_pi_waiter(p)->task;
495
496 rt_mutex_setprio(p, pi_task);
497 }
498
499 /* 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)500 static __always_inline void rt_mutex_wake_q_add_task(struct rt_wake_q_head *wqh,
501 struct task_struct *task,
502 unsigned int wake_state)
503 {
504 if (IS_ENABLED(CONFIG_PREEMPT_RT) && wake_state == TASK_RTLOCK_WAIT) {
505 if (IS_ENABLED(CONFIG_PROVE_LOCKING))
506 WARN_ON_ONCE(wqh->rtlock_task);
507 get_task_struct(task);
508 wqh->rtlock_task = task;
509 } else {
510 wake_q_add(&wqh->head, task);
511 }
512 }
513
rt_mutex_wake_q_add(struct rt_wake_q_head * wqh,struct rt_mutex_waiter * w)514 static __always_inline void rt_mutex_wake_q_add(struct rt_wake_q_head *wqh,
515 struct rt_mutex_waiter *w)
516 {
517 rt_mutex_wake_q_add_task(wqh, w->task, w->wake_state);
518 }
519
rt_mutex_wake_up_q(struct rt_wake_q_head * wqh)520 static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh)
521 {
522 if (IS_ENABLED(CONFIG_PREEMPT_RT) && wqh->rtlock_task) {
523 wake_up_state(wqh->rtlock_task, TASK_RTLOCK_WAIT);
524 put_task_struct(wqh->rtlock_task);
525 wqh->rtlock_task = NULL;
526 }
527
528 if (!wake_q_empty(&wqh->head))
529 wake_up_q(&wqh->head);
530
531 /* Pairs with preempt_disable() in mark_wakeup_next_waiter() */
532 preempt_enable();
533 }
534
535 /*
536 * Deadlock detection is conditional:
537 *
538 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
539 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
540 *
541 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
542 * conducted independent of the detect argument.
543 *
544 * If the waiter argument is NULL this indicates the deboost path and
545 * deadlock detection is disabled independent of the detect argument
546 * and the config settings.
547 */
548 static __always_inline bool
rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter * waiter,enum rtmutex_chainwalk chwalk)549 rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
550 enum rtmutex_chainwalk chwalk)
551 {
552 if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
553 return waiter != NULL;
554 return chwalk == RT_MUTEX_FULL_CHAINWALK;
555 }
556
task_blocked_on_lock(struct task_struct * p)557 static __always_inline struct rt_mutex_base *task_blocked_on_lock(struct task_struct *p)
558 {
559 return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
560 }
561
562 /*
563 * Adjust the priority chain. Also used for deadlock detection.
564 * Decreases task's usage by one - may thus free the task.
565 *
566 * @task: the task owning the mutex (owner) for which a chain walk is
567 * probably needed
568 * @chwalk: do we have to carry out deadlock detection?
569 * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
570 * things for a task that has just got its priority adjusted, and
571 * is waiting on a mutex)
572 * @next_lock: the mutex on which the owner of @orig_lock was blocked before
573 * we dropped its pi_lock. Is never dereferenced, only used for
574 * comparison to detect lock chain changes.
575 * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
576 * its priority to the mutex owner (can be NULL in the case
577 * depicted above or if the top waiter is gone away and we are
578 * actually deboosting the owner)
579 * @top_task: the current top waiter
580 *
581 * Returns 0 or -EDEADLK.
582 *
583 * Chain walk basics and protection scope
584 *
585 * [R] refcount on task
586 * [P] task->pi_lock held
587 * [L] rtmutex->wait_lock held
588 *
589 * Step Description Protected by
590 * function arguments:
591 * @task [R]
592 * @orig_lock if != NULL @top_task is blocked on it
593 * @next_lock Unprotected. Cannot be
594 * dereferenced. Only used for
595 * comparison.
596 * @orig_waiter if != NULL @top_task is blocked on it
597 * @top_task current, or in case of proxy
598 * locking protected by calling
599 * code
600 * again:
601 * loop_sanity_check();
602 * retry:
603 * [1] lock(task->pi_lock); [R] acquire [P]
604 * [2] waiter = task->pi_blocked_on; [P]
605 * [3] check_exit_conditions_1(); [P]
606 * [4] lock = waiter->lock; [P]
607 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
608 * unlock(task->pi_lock); release [P]
609 * goto retry;
610 * }
611 * [6] check_exit_conditions_2(); [P] + [L]
612 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
613 * [8] unlock(task->pi_lock); release [P]
614 * put_task_struct(task); release [R]
615 * [9] check_exit_conditions_3(); [L]
616 * [10] task = owner(lock); [L]
617 * get_task_struct(task); [L] acquire [R]
618 * lock(task->pi_lock); [L] acquire [P]
619 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
620 * [12] check_exit_conditions_4(); [P] + [L]
621 * [13] unlock(task->pi_lock); release [P]
622 * unlock(lock->wait_lock); release [L]
623 * goto again;
624 */
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)625 static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
626 enum rtmutex_chainwalk chwalk,
627 struct rt_mutex_base *orig_lock,
628 struct rt_mutex_base *next_lock,
629 struct rt_mutex_waiter *orig_waiter,
630 struct task_struct *top_task)
631 {
632 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
633 struct rt_mutex_waiter *prerequeue_top_waiter;
634 int ret = 0, depth = 0;
635 struct rt_mutex_base *lock;
636 bool detect_deadlock;
637 bool requeue = true;
638
639 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
640
641 /*
642 * The (de)boosting is a step by step approach with a lot of
643 * pitfalls. We want this to be preemptible and we want hold a
644 * maximum of two locks per step. So we have to check
645 * carefully whether things change under us.
646 */
647 again:
648 /*
649 * We limit the lock chain length for each invocation.
650 */
651 if (++depth > max_lock_depth) {
652 static int prev_max;
653
654 /*
655 * Print this only once. If the admin changes the limit,
656 * print a new message when reaching the limit again.
657 */
658 if (prev_max != max_lock_depth) {
659 prev_max = max_lock_depth;
660 printk(KERN_WARNING "Maximum lock depth %d reached "
661 "task: %s (%d)\n", max_lock_depth,
662 top_task->comm, task_pid_nr(top_task));
663 }
664 put_task_struct(task);
665
666 return -EDEADLK;
667 }
668
669 /*
670 * We are fully preemptible here and only hold the refcount on
671 * @task. So everything can have changed under us since the
672 * caller or our own code below (goto retry/again) dropped all
673 * locks.
674 */
675 retry:
676 /*
677 * [1] Task cannot go away as we did a get_task() before !
678 */
679 raw_spin_lock_irq(&task->pi_lock);
680
681 /*
682 * [2] Get the waiter on which @task is blocked on.
683 */
684 waiter = task->pi_blocked_on;
685
686 /*
687 * [3] check_exit_conditions_1() protected by task->pi_lock.
688 */
689
690 /*
691 * Check whether the end of the boosting chain has been
692 * reached or the state of the chain has changed while we
693 * dropped the locks.
694 */
695 if (!waiter)
696 goto out_unlock_pi;
697
698 /*
699 * Check the orig_waiter state. After we dropped the locks,
700 * the previous owner of the lock might have released the lock.
701 */
702 if (orig_waiter && !rt_mutex_owner(orig_lock))
703 goto out_unlock_pi;
704
705 /*
706 * We dropped all locks after taking a refcount on @task, so
707 * the task might have moved on in the lock chain or even left
708 * the chain completely and blocks now on an unrelated lock or
709 * on @orig_lock.
710 *
711 * We stored the lock on which @task was blocked in @next_lock,
712 * so we can detect the chain change.
713 */
714 if (next_lock != waiter->lock)
715 goto out_unlock_pi;
716
717 /*
718 * There could be 'spurious' loops in the lock graph due to ww_mutex,
719 * consider:
720 *
721 * P1: A, ww_A, ww_B
722 * P2: ww_B, ww_A
723 * P3: A
724 *
725 * P3 should not return -EDEADLK because it gets trapped in the cycle
726 * created by P1 and P2 (which will resolve -- and runs into
727 * max_lock_depth above). Therefore disable detect_deadlock such that
728 * the below termination condition can trigger once all relevant tasks
729 * are boosted.
730 *
731 * Even when we start with ww_mutex we can disable deadlock detection,
732 * since we would supress a ww_mutex induced deadlock at [6] anyway.
733 * Supressing it here however is not sufficient since we might still
734 * hit [6] due to adjustment driven iteration.
735 *
736 * NOTE: if someone were to create a deadlock between 2 ww_classes we'd
737 * utterly fail to report it; lockdep should.
738 */
739 if (IS_ENABLED(CONFIG_PREEMPT_RT) && waiter->ww_ctx && detect_deadlock)
740 detect_deadlock = false;
741
742 /*
743 * Drop out, when the task has no waiters. Note,
744 * top_waiter can be NULL, when we are in the deboosting
745 * mode!
746 */
747 if (top_waiter) {
748 if (!task_has_pi_waiters(task))
749 goto out_unlock_pi;
750 /*
751 * If deadlock detection is off, we stop here if we
752 * are not the top pi waiter of the task. If deadlock
753 * detection is enabled we continue, but stop the
754 * requeueing in the chain walk.
755 */
756 if (top_waiter != task_top_pi_waiter(task)) {
757 if (!detect_deadlock)
758 goto out_unlock_pi;
759 else
760 requeue = false;
761 }
762 }
763
764 /*
765 * If the waiter priority is the same as the task priority
766 * then there is no further priority adjustment necessary. If
767 * deadlock detection is off, we stop the chain walk. If its
768 * enabled we continue, but stop the requeueing in the chain
769 * walk.
770 */
771 if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
772 if (!detect_deadlock)
773 goto out_unlock_pi;
774 else
775 requeue = false;
776 }
777
778 /*
779 * [4] Get the next lock
780 */
781 lock = waiter->lock;
782 /*
783 * [5] We need to trylock here as we are holding task->pi_lock,
784 * which is the reverse lock order versus the other rtmutex
785 * operations.
786 */
787 if (!raw_spin_trylock(&lock->wait_lock)) {
788 raw_spin_unlock_irq(&task->pi_lock);
789 cpu_relax();
790 goto retry;
791 }
792
793 /*
794 * [6] check_exit_conditions_2() protected by task->pi_lock and
795 * lock->wait_lock.
796 *
797 * Deadlock detection. If the lock is the same as the original
798 * lock which caused us to walk the lock chain or if the
799 * current lock is owned by the task which initiated the chain
800 * walk, we detected a deadlock.
801 */
802 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
803 ret = -EDEADLK;
804
805 /*
806 * When the deadlock is due to ww_mutex; also see above. Don't
807 * report the deadlock and instead let the ww_mutex wound/die
808 * logic pick which of the contending threads gets -EDEADLK.
809 *
810 * NOTE: assumes the cycle only contains a single ww_class; any
811 * other configuration and we fail to report; also, see
812 * lockdep.
813 */
814 if (IS_ENABLED(CONFIG_PREEMPT_RT) && orig_waiter && orig_waiter->ww_ctx)
815 ret = 0;
816
817 raw_spin_unlock(&lock->wait_lock);
818 goto out_unlock_pi;
819 }
820
821 /*
822 * If we just follow the lock chain for deadlock detection, no
823 * need to do all the requeue operations. To avoid a truckload
824 * of conditionals around the various places below, just do the
825 * minimum chain walk checks.
826 */
827 if (!requeue) {
828 /*
829 * No requeue[7] here. Just release @task [8]
830 */
831 raw_spin_unlock(&task->pi_lock);
832 put_task_struct(task);
833
834 /*
835 * [9] check_exit_conditions_3 protected by lock->wait_lock.
836 * If there is no owner of the lock, end of chain.
837 */
838 if (!rt_mutex_owner(lock)) {
839 raw_spin_unlock_irq(&lock->wait_lock);
840 return 0;
841 }
842
843 /* [10] Grab the next task, i.e. owner of @lock */
844 task = get_task_struct(rt_mutex_owner(lock));
845 raw_spin_lock(&task->pi_lock);
846
847 /*
848 * No requeue [11] here. We just do deadlock detection.
849 *
850 * [12] Store whether owner is blocked
851 * itself. Decision is made after dropping the locks
852 */
853 next_lock = task_blocked_on_lock(task);
854 /*
855 * Get the top waiter for the next iteration
856 */
857 top_waiter = rt_mutex_top_waiter(lock);
858
859 /* [13] Drop locks */
860 raw_spin_unlock(&task->pi_lock);
861 raw_spin_unlock_irq(&lock->wait_lock);
862
863 /* If owner is not blocked, end of chain. */
864 if (!next_lock)
865 goto out_put_task;
866 goto again;
867 }
868
869 /*
870 * Store the current top waiter before doing the requeue
871 * operation on @lock. We need it for the boost/deboost
872 * decision below.
873 */
874 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
875
876 /* [7] Requeue the waiter in the lock waiter tree. */
877 rt_mutex_dequeue(lock, waiter);
878
879 /*
880 * Update the waiter prio fields now that we're dequeued.
881 *
882 * These values can have changed through either:
883 *
884 * sys_sched_set_scheduler() / sys_sched_setattr()
885 *
886 * or
887 *
888 * DL CBS enforcement advancing the effective deadline.
889 *
890 * Even though pi_waiters also uses these fields, and that tree is only
891 * updated in [11], we can do this here, since we hold [L], which
892 * serializes all pi_waiters access and rb_erase() does not care about
893 * the values of the node being removed.
894 */
895 waiter_update_prio(waiter, task);
896
897 rt_mutex_enqueue(lock, waiter);
898
899 /* [8] Release the task */
900 raw_spin_unlock(&task->pi_lock);
901 put_task_struct(task);
902
903 /*
904 * [9] check_exit_conditions_3 protected by lock->wait_lock.
905 *
906 * We must abort the chain walk if there is no lock owner even
907 * in the dead lock detection case, as we have nothing to
908 * follow here. This is the end of the chain we are walking.
909 */
910 if (!rt_mutex_owner(lock)) {
911 /*
912 * If the requeue [7] above changed the top waiter,
913 * then we need to wake the new top waiter up to try
914 * to get the lock.
915 */
916 top_waiter = rt_mutex_top_waiter(lock);
917 if (prerequeue_top_waiter != top_waiter)
918 wake_up_state(top_waiter->task, top_waiter->wake_state);
919 raw_spin_unlock_irq(&lock->wait_lock);
920 return 0;
921 }
922
923 /* [10] Grab the next task, i.e. the owner of @lock */
924 task = get_task_struct(rt_mutex_owner(lock));
925 raw_spin_lock(&task->pi_lock);
926
927 /* [11] requeue the pi waiters if necessary */
928 if (waiter == rt_mutex_top_waiter(lock)) {
929 /*
930 * The waiter became the new top (highest priority)
931 * waiter on the lock. Replace the previous top waiter
932 * in the owner tasks pi waiters tree with this waiter
933 * and adjust the priority of the owner.
934 */
935 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
936 rt_mutex_enqueue_pi(task, waiter);
937 rt_mutex_adjust_prio(task);
938
939 } else if (prerequeue_top_waiter == waiter) {
940 /*
941 * The waiter was the top waiter on the lock, but is
942 * no longer the top priority waiter. Replace waiter in
943 * the owner tasks pi waiters tree with the new top
944 * (highest priority) waiter and adjust the priority
945 * of the owner.
946 * The new top waiter is stored in @waiter so that
947 * @waiter == @top_waiter evaluates to true below and
948 * we continue to deboost the rest of the chain.
949 */
950 rt_mutex_dequeue_pi(task, waiter);
951 waiter = rt_mutex_top_waiter(lock);
952 rt_mutex_enqueue_pi(task, waiter);
953 rt_mutex_adjust_prio(task);
954 } else {
955 /*
956 * Nothing changed. No need to do any priority
957 * adjustment.
958 */
959 }
960
961 /*
962 * [12] check_exit_conditions_4() protected by task->pi_lock
963 * and lock->wait_lock. The actual decisions are made after we
964 * dropped the locks.
965 *
966 * Check whether the task which owns the current lock is pi
967 * blocked itself. If yes we store a pointer to the lock for
968 * the lock chain change detection above. After we dropped
969 * task->pi_lock next_lock cannot be dereferenced anymore.
970 */
971 next_lock = task_blocked_on_lock(task);
972 /*
973 * Store the top waiter of @lock for the end of chain walk
974 * decision below.
975 */
976 top_waiter = rt_mutex_top_waiter(lock);
977
978 /* [13] Drop the locks */
979 raw_spin_unlock(&task->pi_lock);
980 raw_spin_unlock_irq(&lock->wait_lock);
981
982 /*
983 * Make the actual exit decisions [12], based on the stored
984 * values.
985 *
986 * We reached the end of the lock chain. Stop right here. No
987 * point to go back just to figure that out.
988 */
989 if (!next_lock)
990 goto out_put_task;
991
992 /*
993 * If the current waiter is not the top waiter on the lock,
994 * then we can stop the chain walk here if we are not in full
995 * deadlock detection mode.
996 */
997 if (!detect_deadlock && waiter != top_waiter)
998 goto out_put_task;
999
1000 goto again;
1001
1002 out_unlock_pi:
1003 raw_spin_unlock_irq(&task->pi_lock);
1004 out_put_task:
1005 put_task_struct(task);
1006
1007 return ret;
1008 }
1009
1010 /*
1011 * Try to take an rt-mutex
1012 *
1013 * Must be called with lock->wait_lock held and interrupts disabled
1014 *
1015 * @lock: The lock to be acquired.
1016 * @task: The task which wants to acquire the lock
1017 * @waiter: The waiter that is queued to the lock's wait tree if the
1018 * callsite called task_blocked_on_lock(), otherwise NULL
1019 */
1020 static int __sched
try_to_take_rt_mutex(struct rt_mutex_base * lock,struct task_struct * task,struct rt_mutex_waiter * waiter)1021 try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
1022 struct rt_mutex_waiter *waiter)
1023 {
1024 lockdep_assert_held(&lock->wait_lock);
1025
1026 /*
1027 * Before testing whether we can acquire @lock, we set the
1028 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
1029 * other tasks which try to modify @lock into the slow path
1030 * and they serialize on @lock->wait_lock.
1031 *
1032 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
1033 * as explained at the top of this file if and only if:
1034 *
1035 * - There is a lock owner. The caller must fixup the
1036 * transient state if it does a trylock or leaves the lock
1037 * function due to a signal or timeout.
1038 *
1039 * - @task acquires the lock and there are no other
1040 * waiters. This is undone in rt_mutex_set_owner(@task) at
1041 * the end of this function.
1042 */
1043 mark_rt_mutex_waiters(lock);
1044
1045 /*
1046 * If @lock has an owner, give up.
1047 */
1048 if (rt_mutex_owner(lock))
1049 return 0;
1050
1051 /*
1052 * If @waiter != NULL, @task has already enqueued the waiter
1053 * into @lock waiter tree. If @waiter == NULL then this is a
1054 * trylock attempt.
1055 */
1056 if (waiter) {
1057 struct rt_mutex_waiter *top_waiter = rt_mutex_top_waiter(lock);
1058
1059 /*
1060 * If waiter is the highest priority waiter of @lock,
1061 * or allowed to steal it, take it over.
1062 */
1063 if (waiter == top_waiter || rt_mutex_steal(waiter, top_waiter)) {
1064 /*
1065 * We can acquire the lock. Remove the waiter from the
1066 * lock waiters tree.
1067 */
1068 rt_mutex_dequeue(lock, waiter);
1069 } else {
1070 return 0;
1071 }
1072 } else {
1073 /*
1074 * If the lock has waiters already we check whether @task is
1075 * eligible to take over the lock.
1076 *
1077 * If there are no other waiters, @task can acquire
1078 * the lock. @task->pi_blocked_on is NULL, so it does
1079 * not need to be dequeued.
1080 */
1081 if (rt_mutex_has_waiters(lock)) {
1082 /* Check whether the trylock can steal it. */
1083 if (!rt_mutex_steal(task_to_waiter(task),
1084 rt_mutex_top_waiter(lock)))
1085 return 0;
1086
1087 /*
1088 * The current top waiter stays enqueued. We
1089 * don't have to change anything in the lock
1090 * waiters order.
1091 */
1092 } else {
1093 /*
1094 * No waiters. Take the lock without the
1095 * pi_lock dance.@task->pi_blocked_on is NULL
1096 * and we have no waiters to enqueue in @task
1097 * pi waiters tree.
1098 */
1099 goto takeit;
1100 }
1101 }
1102
1103 /*
1104 * Clear @task->pi_blocked_on. Requires protection by
1105 * @task->pi_lock. Redundant operation for the @waiter == NULL
1106 * case, but conditionals are more expensive than a redundant
1107 * store.
1108 */
1109 raw_spin_lock(&task->pi_lock);
1110 task->pi_blocked_on = NULL;
1111 /*
1112 * Finish the lock acquisition. @task is the new owner. If
1113 * other waiters exist we have to insert the highest priority
1114 * waiter into @task->pi_waiters tree.
1115 */
1116 if (rt_mutex_has_waiters(lock))
1117 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
1118 raw_spin_unlock(&task->pi_lock);
1119
1120 takeit:
1121 /*
1122 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
1123 * are still waiters or clears it.
1124 */
1125 rt_mutex_set_owner(lock, task);
1126
1127 return 1;
1128 }
1129
1130 /*
1131 * Task blocks on lock.
1132 *
1133 * Prepare waiter and propagate pi chain
1134 *
1135 * This must be called with lock->wait_lock held and interrupts disabled
1136 */
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)1137 static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,
1138 struct rt_mutex_waiter *waiter,
1139 struct task_struct *task,
1140 struct ww_acquire_ctx *ww_ctx,
1141 enum rtmutex_chainwalk chwalk)
1142 {
1143 struct task_struct *owner = rt_mutex_owner(lock);
1144 struct rt_mutex_waiter *top_waiter = waiter;
1145 struct rt_mutex_base *next_lock;
1146 int chain_walk = 0, res;
1147
1148 lockdep_assert_held(&lock->wait_lock);
1149
1150 /*
1151 * Early deadlock detection. We really don't want the task to
1152 * enqueue on itself just to untangle the mess later. It's not
1153 * only an optimization. We drop the locks, so another waiter
1154 * can come in before the chain walk detects the deadlock. So
1155 * the other will detect the deadlock and return -EDEADLOCK,
1156 * which is wrong, as the other waiter is not in a deadlock
1157 * situation.
1158 *
1159 * Except for ww_mutex, in that case the chain walk must already deal
1160 * with spurious cycles, see the comments at [3] and [6].
1161 */
1162 if (owner == task && !(build_ww_mutex() && ww_ctx))
1163 return -EDEADLK;
1164
1165 trace_android_vh_task_blocks_on_rtmutex(lock, waiter, task, ww_ctx, &chwalk);
1166 raw_spin_lock(&task->pi_lock);
1167 waiter->task = task;
1168 waiter->lock = lock;
1169 waiter_update_prio(waiter, task);
1170
1171 /* Get the top priority waiter on the lock */
1172 if (rt_mutex_has_waiters(lock))
1173 top_waiter = rt_mutex_top_waiter(lock);
1174 rt_mutex_enqueue(lock, waiter);
1175
1176 task->pi_blocked_on = waiter;
1177
1178 raw_spin_unlock(&task->pi_lock);
1179
1180 if (build_ww_mutex() && ww_ctx) {
1181 struct rt_mutex *rtm;
1182
1183 /* Check whether the waiter should back out immediately */
1184 rtm = container_of(lock, struct rt_mutex, rtmutex);
1185 res = __ww_mutex_add_waiter(waiter, rtm, ww_ctx);
1186 if (res) {
1187 raw_spin_lock(&task->pi_lock);
1188 rt_mutex_dequeue(lock, waiter);
1189 task->pi_blocked_on = NULL;
1190 raw_spin_unlock(&task->pi_lock);
1191 return res;
1192 }
1193 }
1194
1195 if (!owner)
1196 return 0;
1197
1198 raw_spin_lock(&owner->pi_lock);
1199 if (waiter == rt_mutex_top_waiter(lock)) {
1200 rt_mutex_dequeue_pi(owner, top_waiter);
1201 rt_mutex_enqueue_pi(owner, waiter);
1202
1203 rt_mutex_adjust_prio(owner);
1204 if (owner->pi_blocked_on)
1205 chain_walk = 1;
1206 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
1207 chain_walk = 1;
1208 }
1209
1210 /* Store the lock on which owner is blocked or NULL */
1211 next_lock = task_blocked_on_lock(owner);
1212
1213 raw_spin_unlock(&owner->pi_lock);
1214 /*
1215 * Even if full deadlock detection is on, if the owner is not
1216 * blocked itself, we can avoid finding this out in the chain
1217 * walk.
1218 */
1219 if (!chain_walk || !next_lock)
1220 return 0;
1221
1222 /*
1223 * The owner can't disappear while holding a lock,
1224 * so the owner struct is protected by wait_lock.
1225 * Gets dropped in rt_mutex_adjust_prio_chain()!
1226 */
1227 get_task_struct(owner);
1228
1229 raw_spin_unlock_irq(&lock->wait_lock);
1230
1231 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
1232 next_lock, waiter, task);
1233
1234 raw_spin_lock_irq(&lock->wait_lock);
1235
1236 return res;
1237 }
1238
1239 /*
1240 * Remove the top waiter from the current tasks pi waiter tree and
1241 * queue it up.
1242 *
1243 * Called with lock->wait_lock held and interrupts disabled.
1244 */
mark_wakeup_next_waiter(struct rt_wake_q_head * wqh,struct rt_mutex_base * lock)1245 static void __sched mark_wakeup_next_waiter(struct rt_wake_q_head *wqh,
1246 struct rt_mutex_base *lock)
1247 {
1248 struct rt_mutex_waiter *waiter;
1249
1250 raw_spin_lock(¤t->pi_lock);
1251
1252 waiter = rt_mutex_top_waiter(lock);
1253
1254 /*
1255 * Remove it from current->pi_waiters and deboost.
1256 *
1257 * We must in fact deboost here in order to ensure we call
1258 * rt_mutex_setprio() to update p->pi_top_task before the
1259 * task unblocks.
1260 */
1261 rt_mutex_dequeue_pi(current, waiter);
1262 rt_mutex_adjust_prio(current);
1263
1264 /*
1265 * As we are waking up the top waiter, and the waiter stays
1266 * queued on the lock until it gets the lock, this lock
1267 * obviously has waiters. Just set the bit here and this has
1268 * the added benefit of forcing all new tasks into the
1269 * slow path making sure no task of lower priority than
1270 * the top waiter can steal this lock.
1271 */
1272 lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
1273
1274 /*
1275 * We deboosted before waking the top waiter task such that we don't
1276 * run two tasks with the 'same' priority (and ensure the
1277 * p->pi_top_task pointer points to a blocked task). This however can
1278 * lead to priority inversion if we would get preempted after the
1279 * deboost but before waking our donor task, hence the preempt_disable()
1280 * before unlock.
1281 *
1282 * Pairs with preempt_enable() in rt_mutex_wake_up_q();
1283 */
1284 preempt_disable();
1285 rt_mutex_wake_q_add(wqh, waiter);
1286 raw_spin_unlock(¤t->pi_lock);
1287 }
1288
__rt_mutex_slowtrylock(struct rt_mutex_base * lock)1289 static int __sched __rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1290 {
1291 int ret = try_to_take_rt_mutex(lock, current, NULL);
1292
1293 /*
1294 * try_to_take_rt_mutex() sets the lock waiters bit
1295 * unconditionally. Clean this up.
1296 */
1297 fixup_rt_mutex_waiters(lock, true);
1298
1299 return ret;
1300 }
1301
1302 /*
1303 * Slow path try-lock function:
1304 */
rt_mutex_slowtrylock(struct rt_mutex_base * lock)1305 static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock)
1306 {
1307 unsigned long flags;
1308 int ret;
1309
1310 /*
1311 * If the lock already has an owner we fail to get the lock.
1312 * This can be done without taking the @lock->wait_lock as
1313 * it is only being read, and this is a trylock anyway.
1314 */
1315 if (rt_mutex_owner(lock))
1316 return 0;
1317
1318 /*
1319 * The mutex has currently no owner. Lock the wait lock and try to
1320 * acquire the lock. We use irqsave here to support early boot calls.
1321 */
1322 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1323
1324 ret = __rt_mutex_slowtrylock(lock);
1325
1326 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1327
1328 return ret;
1329 }
1330
__rt_mutex_trylock(struct rt_mutex_base * lock)1331 static __always_inline int __rt_mutex_trylock(struct rt_mutex_base *lock)
1332 {
1333 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1334 return 1;
1335
1336 return rt_mutex_slowtrylock(lock);
1337 }
1338
1339 /*
1340 * Slow path to release a rt-mutex.
1341 */
rt_mutex_slowunlock(struct rt_mutex_base * lock)1342 static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
1343 {
1344 DEFINE_RT_WAKE_Q(wqh);
1345 unsigned long flags;
1346
1347 /* irqsave required to support early boot calls */
1348 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1349
1350 debug_rt_mutex_unlock(lock);
1351
1352 /*
1353 * We must be careful here if the fast path is enabled. If we
1354 * have no waiters queued we cannot set owner to NULL here
1355 * because of:
1356 *
1357 * foo->lock->owner = NULL;
1358 * rtmutex_lock(foo->lock); <- fast path
1359 * free = atomic_dec_and_test(foo->refcnt);
1360 * rtmutex_unlock(foo->lock); <- fast path
1361 * if (free)
1362 * kfree(foo);
1363 * raw_spin_unlock(foo->lock->wait_lock);
1364 *
1365 * So for the fastpath enabled kernel:
1366 *
1367 * Nothing can set the waiters bit as long as we hold
1368 * lock->wait_lock. So we do the following sequence:
1369 *
1370 * owner = rt_mutex_owner(lock);
1371 * clear_rt_mutex_waiters(lock);
1372 * raw_spin_unlock(&lock->wait_lock);
1373 * if (cmpxchg(&lock->owner, owner, 0) == owner)
1374 * return;
1375 * goto retry;
1376 *
1377 * The fastpath disabled variant is simple as all access to
1378 * lock->owner is serialized by lock->wait_lock:
1379 *
1380 * lock->owner = NULL;
1381 * raw_spin_unlock(&lock->wait_lock);
1382 */
1383 while (!rt_mutex_has_waiters(lock)) {
1384 /* Drops lock->wait_lock ! */
1385 if (unlock_rt_mutex_safe(lock, flags) == true)
1386 return;
1387 /* Relock the rtmutex and try again */
1388 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1389 }
1390
1391 /*
1392 * The wakeup next waiter path does not suffer from the above
1393 * race. See the comments there.
1394 *
1395 * Queue the next waiter for wakeup once we release the wait_lock.
1396 */
1397 mark_wakeup_next_waiter(&wqh, lock);
1398 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1399
1400 rt_mutex_wake_up_q(&wqh);
1401 }
1402
__rt_mutex_unlock(struct rt_mutex_base * lock)1403 static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
1404 {
1405 if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
1406 return;
1407
1408 rt_mutex_slowunlock(lock);
1409 }
1410
1411 #ifdef CONFIG_SMP
rtmutex_spin_on_owner(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * owner)1412 static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
1413 struct rt_mutex_waiter *waiter,
1414 struct task_struct *owner)
1415 {
1416 bool res = true;
1417
1418 rcu_read_lock();
1419 for (;;) {
1420 /* If owner changed, trylock again. */
1421 if (owner != rt_mutex_owner(lock))
1422 break;
1423 /*
1424 * Ensure that @owner is dereferenced after checking that
1425 * the lock owner still matches @owner. If that fails,
1426 * @owner might point to freed memory. If it still matches,
1427 * the rcu_read_lock() ensures the memory stays valid.
1428 */
1429 barrier();
1430 /*
1431 * Stop spinning when:
1432 * - the lock owner has been scheduled out
1433 * - current is not longer the top waiter
1434 * - current is requested to reschedule (redundant
1435 * for CONFIG_PREEMPT_RCU=y)
1436 * - the VCPU on which owner runs is preempted
1437 */
1438 if (!owner_on_cpu(owner) || need_resched() ||
1439 !rt_mutex_waiter_is_top_waiter(lock, waiter)) {
1440 res = false;
1441 break;
1442 }
1443 cpu_relax();
1444 }
1445 rcu_read_unlock();
1446 return res;
1447 }
1448 #else
rtmutex_spin_on_owner(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter,struct task_struct * owner)1449 static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
1450 struct rt_mutex_waiter *waiter,
1451 struct task_struct *owner)
1452 {
1453 return false;
1454 }
1455 #endif
1456
1457 #ifdef RT_MUTEX_BUILD_MUTEX
1458 /*
1459 * Functions required for:
1460 * - rtmutex, futex on all kernels
1461 * - mutex and rwsem substitutions on RT kernels
1462 */
1463
1464 /*
1465 * Remove a waiter from a lock and give up
1466 *
1467 * Must be called with lock->wait_lock held and interrupts disabled. It must
1468 * have just failed to try_to_take_rt_mutex().
1469 */
remove_waiter(struct rt_mutex_base * lock,struct rt_mutex_waiter * waiter)1470 static void __sched remove_waiter(struct rt_mutex_base *lock,
1471 struct rt_mutex_waiter *waiter)
1472 {
1473 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
1474 struct task_struct *owner = rt_mutex_owner(lock);
1475 struct rt_mutex_base *next_lock;
1476
1477 lockdep_assert_held(&lock->wait_lock);
1478
1479 raw_spin_lock(¤t->pi_lock);
1480 rt_mutex_dequeue(lock, waiter);
1481 current->pi_blocked_on = NULL;
1482 raw_spin_unlock(¤t->pi_lock);
1483
1484 /*
1485 * Only update priority if the waiter was the highest priority
1486 * waiter of the lock and there is an owner to update.
1487 */
1488 if (!owner || !is_top_waiter)
1489 return;
1490
1491 raw_spin_lock(&owner->pi_lock);
1492
1493 rt_mutex_dequeue_pi(owner, waiter);
1494
1495 if (rt_mutex_has_waiters(lock))
1496 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
1497
1498 rt_mutex_adjust_prio(owner);
1499
1500 /* Store the lock on which owner is blocked or NULL */
1501 next_lock = task_blocked_on_lock(owner);
1502
1503 raw_spin_unlock(&owner->pi_lock);
1504
1505 /*
1506 * Don't walk the chain, if the owner task is not blocked
1507 * itself.
1508 */
1509 if (!next_lock)
1510 return;
1511
1512 /* gets dropped in rt_mutex_adjust_prio_chain()! */
1513 get_task_struct(owner);
1514
1515 raw_spin_unlock_irq(&lock->wait_lock);
1516
1517 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1518 next_lock, NULL, current);
1519
1520 raw_spin_lock_irq(&lock->wait_lock);
1521 }
1522
1523 /**
1524 * rt_mutex_slowlock_block() - Perform the wait-wake-try-to-take loop
1525 * @lock: the rt_mutex to take
1526 * @ww_ctx: WW mutex context pointer
1527 * @state: the state the task should block in (TASK_INTERRUPTIBLE
1528 * or TASK_UNINTERRUPTIBLE)
1529 * @timeout: the pre-initialized and started timer, or NULL for none
1530 * @waiter: the pre-initialized rt_mutex_waiter
1531 *
1532 * Must be called with lock->wait_lock held and interrupts disabled
1533 */
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)1534 static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock,
1535 struct ww_acquire_ctx *ww_ctx,
1536 unsigned int state,
1537 struct hrtimer_sleeper *timeout,
1538 struct rt_mutex_waiter *waiter)
1539 {
1540 struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
1541 struct task_struct *owner;
1542 int ret = 0;
1543
1544 trace_android_vh_rtmutex_wait_start(lock);
1545 for (;;) {
1546 /* Try to acquire the lock: */
1547 if (try_to_take_rt_mutex(lock, current, waiter))
1548 break;
1549
1550 if (timeout && !timeout->task) {
1551 ret = -ETIMEDOUT;
1552 break;
1553 }
1554 if (signal_pending_state(state, current)) {
1555 ret = -EINTR;
1556 break;
1557 }
1558
1559 if (build_ww_mutex() && ww_ctx) {
1560 ret = __ww_mutex_check_kill(rtm, waiter, ww_ctx);
1561 if (ret)
1562 break;
1563 }
1564
1565 if (waiter == rt_mutex_top_waiter(lock))
1566 owner = rt_mutex_owner(lock);
1567 else
1568 owner = NULL;
1569 raw_spin_unlock_irq(&lock->wait_lock);
1570
1571 if (!owner || !rtmutex_spin_on_owner(lock, waiter, owner))
1572 schedule();
1573
1574 raw_spin_lock_irq(&lock->wait_lock);
1575 set_current_state(state);
1576 }
1577
1578 trace_android_vh_rtmutex_wait_finish(lock);
1579 __set_current_state(TASK_RUNNING);
1580 return ret;
1581 }
1582
rt_mutex_handle_deadlock(int res,int detect_deadlock,struct rt_mutex_waiter * w)1583 static void __sched rt_mutex_handle_deadlock(int res, int detect_deadlock,
1584 struct rt_mutex_waiter *w)
1585 {
1586 /*
1587 * If the result is not -EDEADLOCK or the caller requested
1588 * deadlock detection, nothing to do here.
1589 */
1590 if (res != -EDEADLOCK || detect_deadlock)
1591 return;
1592
1593 if (build_ww_mutex() && w->ww_ctx)
1594 return;
1595
1596 /*
1597 * Yell loudly and stop the task right here.
1598 */
1599 WARN(1, "rtmutex deadlock detected\n");
1600 while (1) {
1601 set_current_state(TASK_INTERRUPTIBLE);
1602 schedule();
1603 }
1604 }
1605
1606 /**
1607 * __rt_mutex_slowlock - Locking slowpath invoked with lock::wait_lock held
1608 * @lock: The rtmutex to block lock
1609 * @ww_ctx: WW mutex context pointer
1610 * @state: The task state for sleeping
1611 * @chwalk: Indicator whether full or partial chainwalk is requested
1612 * @waiter: Initializer waiter for blocking
1613 */
__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)1614 static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,
1615 struct ww_acquire_ctx *ww_ctx,
1616 unsigned int state,
1617 enum rtmutex_chainwalk chwalk,
1618 struct rt_mutex_waiter *waiter)
1619 {
1620 struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
1621 struct ww_mutex *ww = ww_container_of(rtm);
1622 int ret;
1623
1624 lockdep_assert_held(&lock->wait_lock);
1625
1626 /* Try to acquire the lock again: */
1627 if (try_to_take_rt_mutex(lock, current, NULL)) {
1628 if (build_ww_mutex() && ww_ctx) {
1629 __ww_mutex_check_waiters(rtm, ww_ctx);
1630 ww_mutex_lock_acquired(ww, ww_ctx);
1631 }
1632 return 0;
1633 }
1634
1635 set_current_state(state);
1636
1637 trace_contention_begin(lock, LCB_F_RT);
1638
1639 ret = task_blocks_on_rt_mutex(lock, waiter, current, ww_ctx, chwalk);
1640 if (likely(!ret))
1641 ret = rt_mutex_slowlock_block(lock, ww_ctx, state, NULL, waiter);
1642
1643 if (likely(!ret)) {
1644 /* acquired the lock */
1645 if (build_ww_mutex() && ww_ctx) {
1646 if (!ww_ctx->is_wait_die)
1647 __ww_mutex_check_waiters(rtm, ww_ctx);
1648 ww_mutex_lock_acquired(ww, ww_ctx);
1649 }
1650 } else {
1651 __set_current_state(TASK_RUNNING);
1652 remove_waiter(lock, waiter);
1653 rt_mutex_handle_deadlock(ret, chwalk, waiter);
1654 }
1655
1656 /*
1657 * try_to_take_rt_mutex() sets the waiter bit
1658 * unconditionally. We might have to fix that up.
1659 */
1660 fixup_rt_mutex_waiters(lock, true);
1661
1662 trace_contention_end(lock, ret);
1663
1664 return ret;
1665 }
1666
__rt_mutex_slowlock_locked(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state)1667 static inline int __rt_mutex_slowlock_locked(struct rt_mutex_base *lock,
1668 struct ww_acquire_ctx *ww_ctx,
1669 unsigned int state)
1670 {
1671 struct rt_mutex_waiter waiter;
1672 int ret;
1673
1674 rt_mutex_init_waiter(&waiter);
1675 waiter.ww_ctx = ww_ctx;
1676
1677 ret = __rt_mutex_slowlock(lock, ww_ctx, state, RT_MUTEX_MIN_CHAINWALK,
1678 &waiter);
1679
1680 debug_rt_mutex_free_waiter(&waiter);
1681 return ret;
1682 }
1683
1684 /*
1685 * rt_mutex_slowlock - Locking slowpath invoked when fast path fails
1686 * @lock: The rtmutex to block lock
1687 * @ww_ctx: WW mutex context pointer
1688 * @state: The task state for sleeping
1689 */
rt_mutex_slowlock(struct rt_mutex_base * lock,struct ww_acquire_ctx * ww_ctx,unsigned int state)1690 static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock,
1691 struct ww_acquire_ctx *ww_ctx,
1692 unsigned int state)
1693 {
1694 unsigned long flags;
1695 int ret;
1696
1697 /*
1698 * Technically we could use raw_spin_[un]lock_irq() here, but this can
1699 * be called in early boot if the cmpxchg() fast path is disabled
1700 * (debug, no architecture support). In this case we will acquire the
1701 * rtmutex with lock->wait_lock held. But we cannot unconditionally
1702 * enable interrupts in that early boot case. So we need to use the
1703 * irqsave/restore variants.
1704 */
1705 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1706 ret = __rt_mutex_slowlock_locked(lock, ww_ctx, state);
1707 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1708
1709 return ret;
1710 }
1711
__rt_mutex_lock(struct rt_mutex_base * lock,unsigned int state)1712 static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock,
1713 unsigned int state)
1714 {
1715 if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
1716 return 0;
1717
1718 return rt_mutex_slowlock(lock, NULL, state);
1719 }
1720 #endif /* RT_MUTEX_BUILD_MUTEX */
1721
1722 #ifdef RT_MUTEX_BUILD_SPINLOCKS
1723 /*
1724 * Functions required for spin/rw_lock substitution on RT kernels
1725 */
1726
1727 /**
1728 * rtlock_slowlock_locked - Slow path lock acquisition for RT locks
1729 * @lock: The underlying RT mutex
1730 */
rtlock_slowlock_locked(struct rt_mutex_base * lock)1731 static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock)
1732 {
1733 struct rt_mutex_waiter waiter;
1734 struct task_struct *owner;
1735
1736 lockdep_assert_held(&lock->wait_lock);
1737
1738 if (try_to_take_rt_mutex(lock, current, NULL))
1739 return;
1740
1741 rt_mutex_init_rtlock_waiter(&waiter);
1742
1743 /* Save current state and set state to TASK_RTLOCK_WAIT */
1744 current_save_and_set_rtlock_wait_state();
1745
1746 trace_contention_begin(lock, LCB_F_RT);
1747
1748 task_blocks_on_rt_mutex(lock, &waiter, current, NULL, RT_MUTEX_MIN_CHAINWALK);
1749
1750 for (;;) {
1751 /* Try to acquire the lock again */
1752 if (try_to_take_rt_mutex(lock, current, &waiter))
1753 break;
1754
1755 if (&waiter == rt_mutex_top_waiter(lock))
1756 owner = rt_mutex_owner(lock);
1757 else
1758 owner = NULL;
1759 raw_spin_unlock_irq(&lock->wait_lock);
1760
1761 if (!owner || !rtmutex_spin_on_owner(lock, &waiter, owner))
1762 schedule_rtlock();
1763
1764 raw_spin_lock_irq(&lock->wait_lock);
1765 set_current_state(TASK_RTLOCK_WAIT);
1766 }
1767
1768 /* Restore the task state */
1769 current_restore_rtlock_saved_state();
1770
1771 /*
1772 * try_to_take_rt_mutex() sets the waiter bit unconditionally.
1773 * We might have to fix that up:
1774 */
1775 fixup_rt_mutex_waiters(lock, true);
1776 debug_rt_mutex_free_waiter(&waiter);
1777
1778 trace_contention_end(lock, 0);
1779 }
1780
rtlock_slowlock(struct rt_mutex_base * lock)1781 static __always_inline void __sched rtlock_slowlock(struct rt_mutex_base *lock)
1782 {
1783 unsigned long flags;
1784
1785 raw_spin_lock_irqsave(&lock->wait_lock, flags);
1786 rtlock_slowlock_locked(lock);
1787 raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
1788 }
1789
1790 #endif /* RT_MUTEX_BUILD_SPINLOCKS */
1791