Skip to content

Commit bb4d869

Browse files
KAGA-KOKOgregkh
authored andcommitted
rtmutex: Fix deadlock detector for real
commit 397335f upstream. The current deadlock detection logic does not work reliably due to the following early exit path: /* * Drop out, when the task has no waiters. Note, * top_waiter can be NULL, when we are in the deboosting * mode! */ if (top_waiter && (!task_has_pi_waiters(task) || top_waiter != task_top_pi_waiter(task))) goto out_unlock_pi; So this not only exits when the task has no waiters, it also exits unconditionally when the current waiter is not the top priority waiter of the task. So in a nested locking scenario, it might abort the lock chain walk and therefor miss a potential deadlock. Simple fix: Continue the chain walk, when deadlock detection is enabled. We also avoid the whole enqueue, if we detect the deadlock right away (A-A). It's an optimization, but also prevents that another waiter who comes in after the detection and before the task has undone the damage observes the situation and detects the deadlock and returns -EDEADLOCK, which is wrong as the other task is not in a deadlock situation. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Peter Zijlstra <peterz@infradead.org> Reviewed-by: Steven Rostedt <rostedt@goodmis.org> Cc: Lai Jiangshan <laijs@cn.fujitsu.com> Link: http://lkml.kernel.org/r/20140522031949.725272460@linutronix.de Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d2790a6 commit bb4d869

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

‎kernel/locking/rtmutex.c‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,16 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
331331
* top_waiter can be NULL, when we are in the deboosting
332332
* mode!
333333
*/
334-
if (top_waiter && (!task_has_pi_waiters(task) ||
335-
top_waiter != task_top_pi_waiter(task)))
336-
goto out_unlock_pi;
334+
if (top_waiter) {
335+
if (!task_has_pi_waiters(task))
336+
goto out_unlock_pi;
337+
/*
338+
* If deadlock detection is off, we stop here if we
339+
* are not the top pi waiter of the task.
340+
*/
341+
if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
342+
goto out_unlock_pi;
343+
}
337344

338345
/*
339346
* When deadlock detection is off then we check, if further
@@ -349,7 +356,12 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
349356
goto retry;
350357
}
351358

352-
/* Deadlock detection */
359+
/*
360+
* Deadlock detection. If the lock is the same as the original
361+
* lock which caused us to walk the lock chain or if the
362+
* current lock is owned by the task which initiated the chain
363+
* walk, we detected a deadlock.
364+
*/
353365
if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
354366
debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
355367
raw_spin_unlock(&lock->wait_lock);
@@ -515,6 +527,18 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
515527
unsigned long flags;
516528
int chain_walk = 0, res;
517529

530+
/*
531+
* Early deadlock detection. We really don't want the task to
532+
* enqueue on itself just to untangle the mess later. It's not
533+
* only an optimization. We drop the locks, so another waiter
534+
* can come in before the chain walk detects the deadlock. So
535+
* the other will detect the deadlock and return -EDEADLOCK,
536+
* which is wrong, as the other waiter is not in a deadlock
537+
* situation.
538+
*/
539+
if (detect_deadlock && owner == task)
540+
return -EDEADLK;
541+
518542
raw_spin_lock_irqsave(&task->pi_lock, flags);
519543
__rt_mutex_adjust_prio(task);
520544
waiter->task = task;

0 commit comments

Comments
 (0)