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