Skip to contentSkip to conversation

kernel: locking: a possible sleep-in-atomic-context bug in rt_mutex_slowlock()

BUG Jia-Ju Bai 2 participants 2 messages Jun 21, 2018 Locking primitives

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

The kernel may sleep with holding a spinlock.
The function call path (from bottom to top) in Linux-4.16.7 is:

[FUNC] schedule
kernel/locking/rtmutex.c, 1223: schedule in rt_mutex_handle_deadlock kernel/locking/rtmutex.c, 1273: rt_mutex_handle_deadlock in rt_mutex_slowlock kernel/locking/rtmutex.c, 1249: _raw_spin_lock_irqsave in rt_mutex_slowlock

This bug is found by my static analysis tool (DSAC-2) and checked by my code review.

I do not know how to correctly fix this bug, so I just report them.

Best wishes,
Jia-Ju Bai

maintainer
#1 lore raw
Quoted 9 lines · Jia-Ju Bai

The kernel may sleep with holding a spinlock.
The function call path (from bottom to top) in Linux-4.16.7 is:

[FUNC] schedule
kernel/locking/rtmutex.c, 1223: schedule in rt_mutex_handle_deadlock kernel/locking/rtmutex.c, 1273: rt_mutex_handle_deadlock in rt_mutex_slowlock
kernel/locking/rtmutex.c, 1249: _raw_spin_lock_irqsave in rt_mutex_slowlock

Quoted 4 lines

This bug is found by my static analysis tool (DSAC-2) and checked by my code review.

I do not know how to correctly fix this bug, so I just report them.

I think the below will do. The purpose seems to be to take out the task when we cannot return -EDEADLOCK to userspace. And it can do so just fine after we unlock the wait_lock.

Thomas?

---
 kernel/locking/rtmutex.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/locking/rtmutex.c +2 -1
index 4f014be7a4b8..d33446a05681 100644
@@ -1269,7 +1269,6 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
 	if (unlikely(ret)) {
 		__set_current_state(TASK_RUNNING);
 		remove_waiter(lock, &waiter);
-		rt_mutex_handle_deadlock(ret, chwalk, &waiter);
 	}
 
 	/*
@@ -1280,6 +1279,8 @@ rt_mutex_slowlock(struct rt_mutex *lock, int state,
 
 	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
 
+	rt_mutex_handle_deadlock(ret, chwalk, &waiter);
+
 	/* Remove pending timer: */
 	if (unlikely(timeout))
 		hrtimer_cancel(&timeout->timer);