aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/locking/rtmutex.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/locking/rtmutex.c')
-rw-r--r--kernel/locking/rtmutex.c562
1 files changed, 417 insertions, 145 deletions
diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c
index fc605941b9b8..a0ea2a141b3b 100644
--- a/kernel/locking/rtmutex.c
+++ b/kernel/locking/rtmutex.c
@@ -308,6 +308,32 @@ static void rt_mutex_adjust_prio(struct task_struct *task)
308} 308}
309 309
310/* 310/*
311 * Deadlock detection is conditional:
312 *
313 * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
314 * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
315 *
316 * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
317 * conducted independent of the detect argument.
318 *
319 * If the waiter argument is NULL this indicates the deboost path and
320 * deadlock detection is disabled independent of the detect argument
321 * and the config settings.
322 */
323static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
324 enum rtmutex_chainwalk chwalk)
325{
326 /*
327 * This is just a wrapper function for the following call,
328 * because debug_rt_mutex_detect_deadlock() smells like a magic
329 * debug feature and I wanted to keep the cond function in the
330 * main source file along with the comments instead of having
331 * two of the same in the headers.
332 */
333 return debug_rt_mutex_detect_deadlock(waiter, chwalk);
334}
335
336/*
311 * Max number of times we'll walk the boosting chain: 337 * Max number of times we'll walk the boosting chain:
312 */ 338 */
313int max_lock_depth = 1024; 339int max_lock_depth = 1024;
@@ -337,21 +363,65 @@ static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
337 * @top_task: the current top waiter 363 * @top_task: the current top waiter
338 * 364 *
339 * Returns 0 or -EDEADLK. 365 * Returns 0 or -EDEADLK.
366 *
367 * Chain walk basics and protection scope
368 *
369 * [R] refcount on task
370 * [P] task->pi_lock held
371 * [L] rtmutex->wait_lock held
372 *
373 * Step Description Protected by
374 * function arguments:
375 * @task [R]
376 * @orig_lock if != NULL @top_task is blocked on it
377 * @next_lock Unprotected. Cannot be
378 * dereferenced. Only used for
379 * comparison.
380 * @orig_waiter if != NULL @top_task is blocked on it
381 * @top_task current, or in case of proxy
382 * locking protected by calling
383 * code
384 * again:
385 * loop_sanity_check();
386 * retry:
387 * [1] lock(task->pi_lock); [R] acquire [P]
388 * [2] waiter = task->pi_blocked_on; [P]
389 * [3] check_exit_conditions_1(); [P]
390 * [4] lock = waiter->lock; [P]
391 * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
392 * unlock(task->pi_lock); release [P]
393 * goto retry;
394 * }
395 * [6] check_exit_conditions_2(); [P] + [L]
396 * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
397 * [8] unlock(task->pi_lock); release [P]
398 * put_task_struct(task); release [R]
399 * [9] check_exit_conditions_3(); [L]
400 * [10] task = owner(lock); [L]
401 * get_task_struct(task); [L] acquire [R]
402 * lock(task->pi_lock); [L] acquire [P]
403 * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
404 * [12] check_exit_conditions_4(); [P] + [L]
405 * [13] unlock(task->pi_lock); release [P]
406 * unlock(lock->wait_lock); release [L]
407 * goto again;
340 */ 408 */
341static int rt_mutex_adjust_prio_chain(struct task_struct *task, 409static int rt_mutex_adjust_prio_chain(struct task_struct *task,
342 int deadlock_detect, 410 enum rtmutex_chainwalk chwalk,
343 struct rt_mutex *orig_lock, 411 struct rt_mutex *orig_lock,
344 struct rt_mutex *next_lock, 412 struct rt_mutex *next_lock,
345 struct rt_mutex_waiter *orig_waiter, 413 struct rt_mutex_waiter *orig_waiter,
346 struct task_struct *top_task) 414 struct task_struct *top_task)
347{ 415{
348 struct rt_mutex *lock;
349 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter; 416 struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
350 int detect_deadlock, ret = 0, depth = 0; 417 struct rt_mutex_waiter *prerequeue_top_waiter;
418 int ret = 0, depth = 0;
419 struct rt_mutex *lock;
420 bool detect_deadlock;
351 unsigned long flags; 421 unsigned long flags;
422 bool requeue = true;
352 423
353 detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter, 424 detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
354 deadlock_detect);
355 425
356 /* 426 /*
357 * The (de)boosting is a step by step approach with a lot of 427 * The (de)boosting is a step by step approach with a lot of
@@ -360,6 +430,9 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
360 * carefully whether things change under us. 430 * carefully whether things change under us.
361 */ 431 */
362 again: 432 again:
433 /*
434 * We limit the lock chain length for each invocation.
435 */
363 if (++depth > max_lock_depth) { 436 if (++depth > max_lock_depth) {
364 static int prev_max; 437 static int prev_max;
365 438
@@ -377,13 +450,28 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
377 450
378 return -EDEADLK; 451 return -EDEADLK;
379 } 452 }
453
454 /*
455 * We are fully preemptible here and only hold the refcount on
456 * @task. So everything can have changed under us since the
457 * caller or our own code below (goto retry/again) dropped all
458 * locks.
459 */
380 retry: 460 retry:
381 /* 461 /*
382 * Task can not go away as we did a get_task() before ! 462 * [1] Task cannot go away as we did a get_task() before !
383 */ 463 */
384 raw_spin_lock_irqsave(&task->pi_lock, flags); 464 raw_spin_lock_irqsave(&task->pi_lock, flags);
385 465
466 /*
467 * [2] Get the waiter on which @task is blocked on.
468 */
386 waiter = task->pi_blocked_on; 469 waiter = task->pi_blocked_on;
470
471 /*
472 * [3] check_exit_conditions_1() protected by task->pi_lock.
473 */
474
387 /* 475 /*
388 * Check whether the end of the boosting chain has been 476 * Check whether the end of the boosting chain has been
389 * reached or the state of the chain has changed while we 477 * reached or the state of the chain has changed while we
@@ -421,20 +509,41 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
421 goto out_unlock_pi; 509 goto out_unlock_pi;
422 /* 510 /*
423 * If deadlock detection is off, we stop here if we 511 * If deadlock detection is off, we stop here if we
424 * are not the top pi waiter of the task. 512 * are not the top pi waiter of the task. If deadlock
513 * detection is enabled we continue, but stop the
514 * requeueing in the chain walk.
425 */ 515 */
426 if (!detect_deadlock && top_waiter != task_top_pi_waiter(task)) 516 if (top_waiter != task_top_pi_waiter(task)) {
427 goto out_unlock_pi; 517 if (!detect_deadlock)
518 goto out_unlock_pi;
519 else
520 requeue = false;
521 }
428 } 522 }
429 523
430 /* 524 /*
431 * When deadlock detection is off then we check, if further 525 * If the waiter priority is the same as the task priority
432 * priority adjustment is necessary. 526 * then there is no further priority adjustment necessary. If
527 * deadlock detection is off, we stop the chain walk. If its
528 * enabled we continue, but stop the requeueing in the chain
529 * walk.
433 */ 530 */
434 if (!detect_deadlock && waiter->prio == task->prio) 531 if (waiter->prio == task->prio) {
435 goto out_unlock_pi; 532 if (!detect_deadlock)
533 goto out_unlock_pi;
534 else
535 requeue = false;
536 }
436 537
538 /*
539 * [4] Get the next lock
540 */
437 lock = waiter->lock; 541 lock = waiter->lock;
542 /*
543 * [5] We need to trylock here as we are holding task->pi_lock,
544 * which is the reverse lock order versus the other rtmutex
545 * operations.
546 */
438 if (!raw_spin_trylock(&lock->wait_lock)) { 547 if (!raw_spin_trylock(&lock->wait_lock)) {
439 raw_spin_unlock_irqrestore(&task->pi_lock, flags); 548 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
440 cpu_relax(); 549 cpu_relax();
@@ -442,79 +551,180 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
442 } 551 }
443 552
444 /* 553 /*
554 * [6] check_exit_conditions_2() protected by task->pi_lock and
555 * lock->wait_lock.
556 *
445 * Deadlock detection. If the lock is the same as the original 557 * Deadlock detection. If the lock is the same as the original
446 * lock which caused us to walk the lock chain or if the 558 * lock which caused us to walk the lock chain or if the
447 * current lock is owned by the task which initiated the chain 559 * current lock is owned by the task which initiated the chain
448 * walk, we detected a deadlock. 560 * walk, we detected a deadlock.
449 */ 561 */
450 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) { 562 if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
451 debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock); 563 debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
452 raw_spin_unlock(&lock->wait_lock); 564 raw_spin_unlock(&lock->wait_lock);
453 ret = -EDEADLK; 565 ret = -EDEADLK;
454 goto out_unlock_pi; 566 goto out_unlock_pi;
455 } 567 }
456 568
457 top_waiter = rt_mutex_top_waiter(lock); 569 /*
570 * If we just follow the lock chain for deadlock detection, no
571 * need to do all the requeue operations. To avoid a truckload
572 * of conditionals around the various places below, just do the
573 * minimum chain walk checks.
574 */
575 if (!requeue) {
576 /*
577 * No requeue[7] here. Just release @task [8]
578 */
579 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
580 put_task_struct(task);
581
582 /*
583 * [9] check_exit_conditions_3 protected by lock->wait_lock.
584 * If there is no owner of the lock, end of chain.
585 */
586 if (!rt_mutex_owner(lock)) {
587 raw_spin_unlock(&lock->wait_lock);
588 return 0;
589 }
590
591 /* [10] Grab the next task, i.e. owner of @lock */
592 task = rt_mutex_owner(lock);
593 get_task_struct(task);
594 raw_spin_lock_irqsave(&task->pi_lock, flags);
595
596 /*
597 * No requeue [11] here. We just do deadlock detection.
598 *
599 * [12] Store whether owner is blocked
600 * itself. Decision is made after dropping the locks
601 */
602 next_lock = task_blocked_on_lock(task);
603 /*
604 * Get the top waiter for the next iteration
605 */
606 top_waiter = rt_mutex_top_waiter(lock);
607
608 /* [13] Drop locks */
609 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
610 raw_spin_unlock(&lock->wait_lock);
611
612 /* If owner is not blocked, end of chain. */
613 if (!next_lock)
614 goto out_put_task;
615 goto again;
616 }
458 617
459 /* Requeue the waiter */ 618 /*
619 * Store the current top waiter before doing the requeue
620 * operation on @lock. We need it for the boost/deboost
621 * decision below.
622 */
623 prerequeue_top_waiter = rt_mutex_top_waiter(lock);
624
625 /* [7] Requeue the waiter in the lock waiter list. */
460 rt_mutex_dequeue(lock, waiter); 626 rt_mutex_dequeue(lock, waiter);
461 waiter->prio = task->prio; 627 waiter->prio = task->prio;
462 rt_mutex_enqueue(lock, waiter); 628 rt_mutex_enqueue(lock, waiter);
463 629
464 /* Release the task */ 630 /* [8] Release the task */
465 raw_spin_unlock_irqrestore(&task->pi_lock, flags); 631 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
632 put_task_struct(task);
633
634 /*
635 * [9] check_exit_conditions_3 protected by lock->wait_lock.
636 *
637 * We must abort the chain walk if there is no lock owner even
638 * in the dead lock detection case, as we have nothing to
639 * follow here. This is the end of the chain we are walking.
640 */
466 if (!rt_mutex_owner(lock)) { 641 if (!rt_mutex_owner(lock)) {
467 /* 642 /*
468 * If the requeue above changed the top waiter, then we need 643 * If the requeue [7] above changed the top waiter,
469 * to wake the new top waiter up to try to get the lock. 644 * then we need to wake the new top waiter up to try
645 * to get the lock.
470 */ 646 */
471 647 if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
472 if (top_waiter != rt_mutex_top_waiter(lock))
473 wake_up_process(rt_mutex_top_waiter(lock)->task); 648 wake_up_process(rt_mutex_top_waiter(lock)->task);
474 raw_spin_unlock(&lock->wait_lock); 649 raw_spin_unlock(&lock->wait_lock);
475 goto out_put_task; 650 return 0;
476 } 651 }
477 put_task_struct(task);
478 652
479 /* Grab the next task */ 653 /* [10] Grab the next task, i.e. the owner of @lock */
480 task = rt_mutex_owner(lock); 654 task = rt_mutex_owner(lock);
481 get_task_struct(task); 655 get_task_struct(task);
482 raw_spin_lock_irqsave(&task->pi_lock, flags); 656 raw_spin_lock_irqsave(&task->pi_lock, flags);
483 657
658 /* [11] requeue the pi waiters if necessary */
484 if (waiter == rt_mutex_top_waiter(lock)) { 659 if (waiter == rt_mutex_top_waiter(lock)) {
485 /* Boost the owner */ 660 /*
486 rt_mutex_dequeue_pi(task, top_waiter); 661 * The waiter became the new top (highest priority)
662 * waiter on the lock. Replace the previous top waiter
663 * in the owner tasks pi waiters list with this waiter
664 * and adjust the priority of the owner.
665 */
666 rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
487 rt_mutex_enqueue_pi(task, waiter); 667 rt_mutex_enqueue_pi(task, waiter);
488 __rt_mutex_adjust_prio(task); 668 __rt_mutex_adjust_prio(task);
489 669
490 } else if (top_waiter == waiter) { 670 } else if (prerequeue_top_waiter == waiter) {
491 /* Deboost the owner */ 671 /*
672 * The waiter was the top waiter on the lock, but is
673 * no longer the top prority waiter. Replace waiter in
674 * the owner tasks pi waiters list with the new top
675 * (highest priority) waiter and adjust the priority
676 * of the owner.
677 * The new top waiter is stored in @waiter so that
678 * @waiter == @top_waiter evaluates to true below and
679 * we continue to deboost the rest of the chain.
680 */
492 rt_mutex_dequeue_pi(task, waiter); 681 rt_mutex_dequeue_pi(task, waiter);
493 waiter = rt_mutex_top_waiter(lock); 682 waiter = rt_mutex_top_waiter(lock);
494 rt_mutex_enqueue_pi(task, waiter); 683 rt_mutex_enqueue_pi(task, waiter);
495 __rt_mutex_adjust_prio(task); 684 __rt_mutex_adjust_prio(task);
685 } else {
686 /*
687 * Nothing changed. No need to do any priority
688 * adjustment.
689 */
496 } 690 }
497 691
498 /* 692 /*
693 * [12] check_exit_conditions_4() protected by task->pi_lock
694 * and lock->wait_lock. The actual decisions are made after we
695 * dropped the locks.
696 *
499 * Check whether the task which owns the current lock is pi 697 * Check whether the task which owns the current lock is pi
500 * blocked itself. If yes we store a pointer to the lock for 698 * blocked itself. If yes we store a pointer to the lock for
501 * the lock chain change detection above. After we dropped 699 * the lock chain change detection above. After we dropped
502 * task->pi_lock next_lock cannot be dereferenced anymore. 700 * task->pi_lock next_lock cannot be dereferenced anymore.
503 */ 701 */
504 next_lock = task_blocked_on_lock(task); 702 next_lock = task_blocked_on_lock(task);
703 /*
704 * Store the top waiter of @lock for the end of chain walk
705 * decision below.
706 */
707 top_waiter = rt_mutex_top_waiter(lock);
505 708
709 /* [13] Drop the locks */
506 raw_spin_unlock_irqrestore(&task->pi_lock, flags); 710 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
507
508 top_waiter = rt_mutex_top_waiter(lock);
509 raw_spin_unlock(&lock->wait_lock); 711 raw_spin_unlock(&lock->wait_lock);
510 712
511 /* 713 /*
714 * Make the actual exit decisions [12], based on the stored
715 * values.
716 *
512 * We reached the end of the lock chain. Stop right here. No 717 * We reached the end of the lock chain. Stop right here. No
513 * point to go back just to figure that out. 718 * point to go back just to figure that out.
514 */ 719 */
515 if (!next_lock) 720 if (!next_lock)
516 goto out_put_task; 721 goto out_put_task;
517 722
723 /*
724 * If the current waiter is not the top waiter on the lock,
725 * then we can stop the chain walk here if we are not in full
726 * deadlock detection mode.
727 */
518 if (!detect_deadlock && waiter != top_waiter) 728 if (!detect_deadlock && waiter != top_waiter)
519 goto out_put_task; 729 goto out_put_task;
520 730
@@ -533,76 +743,119 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
533 * 743 *
534 * Must be called with lock->wait_lock held. 744 * Must be called with lock->wait_lock held.
535 * 745 *
536 * @lock: the lock to be acquired. 746 * @lock: The lock to be acquired.
537 * @task: the task which wants to acquire the lock 747 * @task: The task which wants to acquire the lock
538 * @waiter: the waiter that is queued to the lock's wait list. (could be NULL) 748 * @waiter: The waiter that is queued to the lock's wait list if the
749 * callsite called task_blocked_on_lock(), otherwise NULL
539 */ 750 */
540static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task, 751static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
541 struct rt_mutex_waiter *waiter) 752 struct rt_mutex_waiter *waiter)
542{ 753{
754 unsigned long flags;
755
543 /* 756 /*
544 * We have to be careful here if the atomic speedups are 757 * Before testing whether we can acquire @lock, we set the
545 * enabled, such that, when 758 * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
546 * - no other waiter is on the lock 759 * other tasks which try to modify @lock into the slow path
547 * - the lock has been released since we did the cmpxchg 760 * and they serialize on @lock->wait_lock.
548 * the lock can be released or taken while we are doing the 761 *
549 * checks and marking the lock with RT_MUTEX_HAS_WAITERS. 762 * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
763 * as explained at the top of this file if and only if:
550 * 764 *
551 * The atomic acquire/release aware variant of 765 * - There is a lock owner. The caller must fixup the
552 * mark_rt_mutex_waiters uses a cmpxchg loop. After setting 766 * transient state if it does a trylock or leaves the lock
553 * the WAITERS bit, the atomic release / acquire can not 767 * function due to a signal or timeout.
554 * happen anymore and lock->wait_lock protects us from the
555 * non-atomic case.
556 * 768 *
557 * Note, that this might set lock->owner = 769 * - @task acquires the lock and there are no other
558 * RT_MUTEX_HAS_WAITERS in the case the lock is not contended 770 * waiters. This is undone in rt_mutex_set_owner(@task) at
559 * any more. This is fixed up when we take the ownership. 771 * the end of this function.
560 * This is the transitional state explained at the top of this file.
561 */ 772 */
562 mark_rt_mutex_waiters(lock); 773 mark_rt_mutex_waiters(lock);
563 774
775 /*
776 * If @lock has an owner, give up.
777 */
564 if (rt_mutex_owner(lock)) 778 if (rt_mutex_owner(lock))
565 return 0; 779 return 0;
566 780
567 /* 781 /*
568 * It will get the lock because of one of these conditions: 782 * If @waiter != NULL, @task has already enqueued the waiter
569 * 1) there is no waiter 783 * into @lock waiter list. If @waiter == NULL then this is a
570 * 2) higher priority than waiters 784 * trylock attempt.
571 * 3) it is top waiter
572 */ 785 */
573 if (rt_mutex_has_waiters(lock)) { 786 if (waiter) {
574 if (task->prio >= rt_mutex_top_waiter(lock)->prio) { 787 /*
575 if (!waiter || waiter != rt_mutex_top_waiter(lock)) 788 * If waiter is not the highest priority waiter of
576 return 0; 789 * @lock, give up.
577 } 790 */
578 } 791 if (waiter != rt_mutex_top_waiter(lock))
579 792 return 0;
580 if (waiter || rt_mutex_has_waiters(lock)) {
581 unsigned long flags;
582 struct rt_mutex_waiter *top;
583
584 raw_spin_lock_irqsave(&task->pi_lock, flags);
585 793
586 /* remove the queued waiter. */ 794 /*
587 if (waiter) { 795 * We can acquire the lock. Remove the waiter from the
588 rt_mutex_dequeue(lock, waiter); 796 * lock waiters list.
589 task->pi_blocked_on = NULL; 797 */
590 } 798 rt_mutex_dequeue(lock, waiter);
591 799
800 } else {
592 /* 801 /*
593 * We have to enqueue the top waiter(if it exists) into 802 * If the lock has waiters already we check whether @task is
594 * task->pi_waiters list. 803 * eligible to take over the lock.
804 *
805 * If there are no other waiters, @task can acquire
806 * the lock. @task->pi_blocked_on is NULL, so it does
807 * not need to be dequeued.
595 */ 808 */
596 if (rt_mutex_has_waiters(lock)) { 809 if (rt_mutex_has_waiters(lock)) {
597 top = rt_mutex_top_waiter(lock); 810 /*
598 rt_mutex_enqueue_pi(task, top); 811 * If @task->prio is greater than or equal to
812 * the top waiter priority (kernel view),
813 * @task lost.
814 */
815 if (task->prio >= rt_mutex_top_waiter(lock)->prio)
816 return 0;
817
818 /*
819 * The current top waiter stays enqueued. We
820 * don't have to change anything in the lock
821 * waiters order.
822 */
823 } else {
824 /*
825 * No waiters. Take the lock without the
826 * pi_lock dance.@task->pi_blocked_on is NULL
827 * and we have no waiters to enqueue in @task
828 * pi waiters list.
829 */
830 goto takeit;
599 } 831 }
600 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
601 } 832 }
602 833
834 /*
835 * Clear @task->pi_blocked_on. Requires protection by
836 * @task->pi_lock. Redundant operation for the @waiter == NULL
837 * case, but conditionals are more expensive than a redundant
838 * store.
839 */
840 raw_spin_lock_irqsave(&task->pi_lock, flags);
841 task->pi_blocked_on = NULL;
842 /*
843 * Finish the lock acquisition. @task is the new owner. If
844 * other waiters exist we have to insert the highest priority
845 * waiter into @task->pi_waiters list.
846 */
847 if (rt_mutex_has_waiters(lock))
848 rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
849 raw_spin_unlock_irqrestore(&task->pi_lock, flags);
850
851takeit:
603 /* We got the lock. */ 852 /* We got the lock. */
604 debug_rt_mutex_lock(lock); 853 debug_rt_mutex_lock(lock);
605 854
855 /*
856 * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
857 * are still waiters or clears it.
858 */
606 rt_mutex_set_owner(lock, task); 859 rt_mutex_set_owner(lock, task);
607 860
608 rt_mutex_deadlock_account_lock(lock, task); 861 rt_mutex_deadlock_account_lock(lock, task);
@@ -620,7 +873,7 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
620static int task_blocks_on_rt_mutex(struct rt_mutex *lock, 873static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
621 struct rt_mutex_waiter *waiter, 874 struct rt_mutex_waiter *waiter,
622 struct task_struct *task, 875 struct task_struct *task,
623 int detect_deadlock) 876 enum rtmutex_chainwalk chwalk)
624{ 877{
625 struct task_struct *owner = rt_mutex_owner(lock); 878 struct task_struct *owner = rt_mutex_owner(lock);
626 struct rt_mutex_waiter *top_waiter = waiter; 879 struct rt_mutex_waiter *top_waiter = waiter;
@@ -666,7 +919,7 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
666 __rt_mutex_adjust_prio(owner); 919 __rt_mutex_adjust_prio(owner);
667 if (owner->pi_blocked_on) 920 if (owner->pi_blocked_on)
668 chain_walk = 1; 921 chain_walk = 1;
669 } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) { 922 } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
670 chain_walk = 1; 923 chain_walk = 1;
671 } 924 }
672 925
@@ -691,7 +944,7 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
691 944
692 raw_spin_unlock(&lock->wait_lock); 945 raw_spin_unlock(&lock->wait_lock);
693 946
694 res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, 947 res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
695 next_lock, waiter, task); 948 next_lock, waiter, task);
696 949
697 raw_spin_lock(&lock->wait_lock); 950 raw_spin_lock(&lock->wait_lock);
@@ -753,9 +1006,9 @@ static void wakeup_next_waiter(struct rt_mutex *lock)
753static void remove_waiter(struct rt_mutex *lock, 1006static void remove_waiter(struct rt_mutex *lock,
754 struct rt_mutex_waiter *waiter) 1007 struct rt_mutex_waiter *waiter)
755{ 1008{
756 int first = (waiter == rt_mutex_top_waiter(lock)); 1009 bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
757 struct task_struct *owner = rt_mutex_owner(lock); 1010 struct task_struct *owner = rt_mutex_owner(lock);
758 struct rt_mutex *next_lock = NULL; 1011 struct rt_mutex *next_lock;
759 unsigned long flags; 1012 unsigned long flags;
760 1013
761 raw_spin_lock_irqsave(&current->pi_lock, flags); 1014 raw_spin_lock_irqsave(&current->pi_lock, flags);
@@ -763,29 +1016,31 @@ static void remove_waiter(struct rt_mutex *lock,
763 current->pi_blocked_on = NULL; 1016 current->pi_blocked_on = NULL;
764 raw_spin_unlock_irqrestore(&current->pi_lock, flags); 1017 raw_spin_unlock_irqrestore(&current->pi_lock, flags);
765 1018
766 if (!owner) 1019 /*
1020 * Only update priority if the waiter was the highest priority
1021 * waiter of the lock and there is an owner to update.
1022 */
1023 if (!owner || !is_top_waiter)
767 return; 1024 return;
768 1025
769 if (first) { 1026 raw_spin_lock_irqsave(&owner->pi_lock, flags);
770
771 raw_spin_lock_irqsave(&owner->pi_lock, flags);
772 1027
773 rt_mutex_dequeue_pi(owner, waiter); 1028 rt_mutex_dequeue_pi(owner, waiter);
774 1029
775 if (rt_mutex_has_waiters(lock)) { 1030 if (rt_mutex_has_waiters(lock))
776 struct rt_mutex_waiter *next; 1031 rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
777 1032
778 next = rt_mutex_top_waiter(lock); 1033 __rt_mutex_adjust_prio(owner);
779 rt_mutex_enqueue_pi(owner, next);
780 }
781 __rt_mutex_adjust_prio(owner);
782 1034
783 /* Store the lock on which owner is blocked or NULL */ 1035 /* Store the lock on which owner is blocked or NULL */
784 next_lock = task_blocked_on_lock(owner); 1036 next_lock = task_blocked_on_lock(owner);
785 1037
786 raw_spin_unlock_irqrestore(&owner->pi_lock, flags); 1038 raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
787 }
788 1039
1040 /*
1041 * Don't walk the chain, if the owner task is not blocked
1042 * itself.
1043 */
789 if (!next_lock) 1044 if (!next_lock)
790 return; 1045 return;
791 1046
@@ -794,7 +1049,8 @@ static void remove_waiter(struct rt_mutex *lock,
794 1049
795 raw_spin_unlock(&lock->wait_lock); 1050 raw_spin_unlock(&lock->wait_lock);
796 1051
797 rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current); 1052 rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
1053 next_lock, NULL, current);
798 1054
799 raw_spin_lock(&lock->wait_lock); 1055 raw_spin_lock(&lock->wait_lock);
800} 1056}
@@ -824,7 +1080,8 @@ void rt_mutex_adjust_pi(struct task_struct *task)
824 /* gets dropped in rt_mutex_adjust_prio_chain()! */ 1080 /* gets dropped in rt_mutex_adjust_prio_chain()! */
825 get_task_struct(task); 1081 get_task_struct(task);
826 1082
827 rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task); 1083 rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
1084 next_lock, NULL, task);
828} 1085}
829 1086
830/** 1087/**
@@ -902,7 +1159,7 @@ static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
902static int __sched 1159static int __sched
903rt_mutex_slowlock(struct rt_mutex *lock, int state, 1160rt_mutex_slowlock(struct rt_mutex *lock, int state,
904 struct hrtimer_sleeper *timeout, 1161 struct hrtimer_sleeper *timeout,
905 int detect_deadlock) 1162 enum rtmutex_chainwalk chwalk)
906{ 1163{
907 struct rt_mutex_waiter waiter; 1164 struct rt_mutex_waiter waiter;
908 int ret = 0; 1165 int ret = 0;
@@ -928,7 +1185,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
928 timeout->task = NULL; 1185 timeout->task = NULL;
929 } 1186 }
930 1187
931 ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock); 1188 ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
932 1189
933 if (likely(!ret)) 1190 if (likely(!ret))
934 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter); 1191 ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
@@ -937,7 +1194,7 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
937 1194
938 if (unlikely(ret)) { 1195 if (unlikely(ret)) {
939 remove_waiter(lock, &waiter); 1196 remove_waiter(lock, &waiter);
940 rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter); 1197 rt_mutex_handle_deadlock(ret, chwalk, &waiter);
941 } 1198 }
942 1199
943 /* 1200 /*
@@ -960,22 +1217,31 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
960/* 1217/*
961 * Slow path try-lock function: 1218 * Slow path try-lock function:
962 */ 1219 */
963static inline int 1220static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
964rt_mutex_slowtrylock(struct rt_mutex *lock)
965{ 1221{
966 int ret = 0; 1222 int ret;
1223
1224 /*
1225 * If the lock already has an owner we fail to get the lock.
1226 * This can be done without taking the @lock->wait_lock as
1227 * it is only being read, and this is a trylock anyway.
1228 */
1229 if (rt_mutex_owner(lock))
1230 return 0;
967 1231
1232 /*
1233 * The mutex has currently no owner. Lock the wait lock and
1234 * try to acquire the lock.
1235 */
968 raw_spin_lock(&lock->wait_lock); 1236 raw_spin_lock(&lock->wait_lock);
969 1237
970 if (likely(rt_mutex_owner(lock) != current)) { 1238 ret = try_to_take_rt_mutex(lock, current, NULL);
971 1239
972 ret = try_to_take_rt_mutex(lock, current, NULL); 1240 /*
973 /* 1241 * try_to_take_rt_mutex() sets the lock waiters bit
974 * try_to_take_rt_mutex() sets the lock waiters 1242 * unconditionally. Clean this up.
975 * bit unconditionally. Clean this up. 1243 */
976 */ 1244 fixup_rt_mutex_waiters(lock);
977 fixup_rt_mutex_waiters(lock);
978 }
979 1245
980 raw_spin_unlock(&lock->wait_lock); 1246 raw_spin_unlock(&lock->wait_lock);
981 1247
@@ -1053,30 +1319,31 @@ rt_mutex_slowunlock(struct rt_mutex *lock)
1053 */ 1319 */
1054static inline int 1320static inline int
1055rt_mutex_fastlock(struct rt_mutex *lock, int state, 1321rt_mutex_fastlock(struct rt_mutex *lock, int state,
1056 int detect_deadlock,
1057 int (*slowfn)(struct rt_mutex *lock, int state, 1322 int (*slowfn)(struct rt_mutex *lock, int state,
1058 struct hrtimer_sleeper *timeout, 1323 struct hrtimer_sleeper *timeout,
1059 int detect_deadlock)) 1324 enum rtmutex_chainwalk chwalk))
1060{ 1325{
1061 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) { 1326 if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1062 rt_mutex_deadlock_account_lock(lock, current); 1327 rt_mutex_deadlock_account_lock(lock, current);
1063 return 0; 1328 return 0;
1064 } else 1329 } else
1065 return slowfn(lock, state, NULL, detect_deadlock); 1330 return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
1066} 1331}
1067 1332
1068static inline int 1333static inline int
1069rt_mutex_timed_fastlock(struct rt_mutex *lock, int state, 1334rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
1070 struct hrtimer_sleeper *timeout, int detect_deadlock, 1335 struct hrtimer_sleeper *timeout,
1336 enum rtmutex_chainwalk chwalk,
1071 int (*slowfn)(struct rt_mutex *lock, int state, 1337 int (*slowfn)(struct rt_mutex *lock, int state,
1072 struct hrtimer_sleeper *timeout, 1338 struct hrtimer_sleeper *timeout,
1073 int detect_deadlock)) 1339 enum rtmutex_chainwalk chwalk))
1074{ 1340{
1075 if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) { 1341 if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
1342 likely(rt_mutex_cmpxchg(lock, NULL, current))) {
1076 rt_mutex_deadlock_account_lock(lock, current); 1343 rt_mutex_deadlock_account_lock(lock, current);
1077 return 0; 1344 return 0;
1078 } else 1345 } else
1079 return slowfn(lock, state, timeout, detect_deadlock); 1346 return slowfn(lock, state, timeout, chwalk);
1080} 1347}
1081 1348
1082static inline int 1349static inline int
@@ -1109,54 +1376,61 @@ void __sched rt_mutex_lock(struct rt_mutex *lock)
1109{ 1376{
1110 might_sleep(); 1377 might_sleep();
1111 1378
1112 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock); 1379 rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
1113} 1380}
1114EXPORT_SYMBOL_GPL(rt_mutex_lock); 1381EXPORT_SYMBOL_GPL(rt_mutex_lock);
1115 1382
1116/** 1383/**
1117 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible 1384 * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
1118 * 1385 *
1119 * @lock: the rt_mutex to be locked 1386 * @lock: the rt_mutex to be locked
1120 * @detect_deadlock: deadlock detection on/off
1121 * 1387 *
1122 * Returns: 1388 * Returns:
1123 * 0 on success 1389 * 0 on success
1124 * -EINTR when interrupted by a signal 1390 * -EINTR when interrupted by a signal
1125 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1126 */ 1391 */
1127int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock, 1392int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
1128 int detect_deadlock)
1129{ 1393{
1130 might_sleep(); 1394 might_sleep();
1131 1395
1132 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, 1396 return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
1133 detect_deadlock, rt_mutex_slowlock);
1134} 1397}
1135EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible); 1398EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
1136 1399
1400/*
1401 * Futex variant with full deadlock detection.
1402 */
1403int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
1404 struct hrtimer_sleeper *timeout)
1405{
1406 might_sleep();
1407
1408 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1409 RT_MUTEX_FULL_CHAINWALK,
1410 rt_mutex_slowlock);
1411}
1412
1137/** 1413/**
1138 * rt_mutex_timed_lock - lock a rt_mutex interruptible 1414 * rt_mutex_timed_lock - lock a rt_mutex interruptible
1139 * the timeout structure is provided 1415 * the timeout structure is provided
1140 * by the caller 1416 * by the caller
1141 * 1417 *
1142 * @lock: the rt_mutex to be locked 1418 * @lock: the rt_mutex to be locked
1143 * @timeout: timeout structure or NULL (no timeout) 1419 * @timeout: timeout structure or NULL (no timeout)
1144 * @detect_deadlock: deadlock detection on/off
1145 * 1420 *
1146 * Returns: 1421 * Returns:
1147 * 0 on success 1422 * 0 on success
1148 * -EINTR when interrupted by a signal 1423 * -EINTR when interrupted by a signal
1149 * -ETIMEDOUT when the timeout expired 1424 * -ETIMEDOUT when the timeout expired
1150 * -EDEADLK when the lock would deadlock (when deadlock detection is on)
1151 */ 1425 */
1152int 1426int
1153rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout, 1427rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
1154 int detect_deadlock)
1155{ 1428{
1156 might_sleep(); 1429 might_sleep();
1157 1430
1158 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout, 1431 return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
1159 detect_deadlock, rt_mutex_slowlock); 1432 RT_MUTEX_MIN_CHAINWALK,
1433 rt_mutex_slowlock);
1160} 1434}
1161EXPORT_SYMBOL_GPL(rt_mutex_timed_lock); 1435EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
1162 1436
@@ -1262,7 +1536,6 @@ void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1262 * @lock: the rt_mutex to take 1536 * @lock: the rt_mutex to take
1263 * @waiter: the pre-initialized rt_mutex_waiter 1537 * @waiter: the pre-initialized rt_mutex_waiter
1264 * @task: the task to prepare 1538 * @task: the task to prepare
1265 * @detect_deadlock: perform deadlock detection (1) or not (0)
1266 * 1539 *
1267 * Returns: 1540 * Returns:
1268 * 0 - task blocked on lock 1541 * 0 - task blocked on lock
@@ -1273,7 +1546,7 @@ void rt_mutex_proxy_unlock(struct rt_mutex *lock,
1273 */ 1546 */
1274int rt_mutex_start_proxy_lock(struct rt_mutex *lock, 1547int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1275 struct rt_mutex_waiter *waiter, 1548 struct rt_mutex_waiter *waiter,
1276 struct task_struct *task, int detect_deadlock) 1549 struct task_struct *task)
1277{ 1550{
1278 int ret; 1551 int ret;
1279 1552
@@ -1285,7 +1558,8 @@ int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
1285 } 1558 }
1286 1559
1287 /* We enforce deadlock detection for futexes */ 1560 /* We enforce deadlock detection for futexes */
1288 ret = task_blocks_on_rt_mutex(lock, waiter, task, 1); 1561 ret = task_blocks_on_rt_mutex(lock, waiter, task,
1562 RT_MUTEX_FULL_CHAINWALK);
1289 1563
1290 if (ret && !rt_mutex_owner(lock)) { 1564 if (ret && !rt_mutex_owner(lock)) {
1291 /* 1565 /*
@@ -1331,22 +1605,20 @@ struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
1331 * rt_mutex_finish_proxy_lock() - Complete lock acquisition 1605 * rt_mutex_finish_proxy_lock() - Complete lock acquisition
1332 * @lock: the rt_mutex we were woken on 1606 * @lock: the rt_mutex we were woken on
1333 * @to: the timeout, null if none. hrtimer should already have 1607 * @to: the timeout, null if none. hrtimer should already have
1334 * been started. 1608 * been started.
1335 * @waiter: the pre-initialized rt_mutex_waiter 1609 * @waiter: the pre-initialized rt_mutex_waiter
1336 * @detect_deadlock: perform deadlock detection (1) or not (0)
1337 * 1610 *
1338 * Complete the lock acquisition started our behalf by another thread. 1611 * Complete the lock acquisition started our behalf by another thread.
1339 * 1612 *
1340 * Returns: 1613 * Returns:
1341 * 0 - success 1614 * 0 - success
1342 * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK 1615 * <0 - error, one of -EINTR, -ETIMEDOUT
1343 * 1616 *
1344 * Special API call for PI-futex requeue support 1617 * Special API call for PI-futex requeue support
1345 */ 1618 */
1346int rt_mutex_finish_proxy_lock(struct rt_mutex *lock, 1619int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
1347 struct hrtimer_sleeper *to, 1620 struct hrtimer_sleeper *to,
1348 struct rt_mutex_waiter *waiter, 1621 struct rt_mutex_waiter *waiter)
1349 int detect_deadlock)
1350{ 1622{
1351 int ret; 1623 int ret;
1352 1624