Skip to contentSkip to conversation

kernel: locking: rtmutex: Fix a possible sleep-in-atomic-context bug in rt_mutex_handle_deadlock()

PATCH In review Jia-Ju Bai 2 participants 2 messages Aug 11, 2018 – Sep 10, 2018 Locking primitives

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH v2 1/1 kernel: locking: rtmutex: Fix a possible sleep-in-atomic-context bug in rt_mutex_handle_deadlock()

The driver may sleep with holding a spinlock.

The function call paths (from bottom to top) in Linux-4.16 are:

[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

To fix the bug, the spinlock is released before the loop of schedule() This is found by my static analysis tool (DSAC).

---
v2:
* Release the spinlock before the loop, instead of v1 that releases the spinlock before schedule() and then acquires the spinlock again. Thank Steven for good advice.

---
 kernel/locking/rtmutex.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/locking/rtmutex.c +4 -2
index 2823d4163a37..8f25a289abe8 100644
@@ -1205,7 +1205,7 @@ __rt_mutex_slowlock(struct rt_mutex *lock, int state,
 }
 
 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
-				     struct rt_mutex_waiter *w)
+				     struct rt_mutex_waiter *w, struct rt_mutex *lock)
 {
 	/*
 	 * If the result is not -EDEADLOCK or the caller requested
@@ -1218,6 +1218,8 @@ static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
 	 * Yell lowdly and stop the task right here.
 	 */
 	rt_mutex_print_deadlock(w);
+	/* We're not going anywhere, release the wait_lock */
+	raw_spin_unlock_irq(&lock->wait_lock);
 	while (1) {
 		set_current_state(TASK_INTERRUPTIBLE);
 		schedule();
@@ -1269,7 +1271,7 @@ 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);
+		rt_mutex_handle_deadlock(ret, chwalk, &waiter, lock);
 	}
 
 	/*
Signature · 1 line
2.17.0
maintainer
#1 lore raw

On Sat, Aug 11, 2018 at 11:00:37AM +0800, Jia-Ju Bai wrote:

You forgot to Cc the person who wrote this code...

Quoted 26 lines
 kernel/locking/rtmutex.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/locking/rtmutex.c +3 -1
index 2823d4163a37..8f25a289abe8 100644
@@ -1205,7 +1205,7 @@ __rt_mutex_slowlock(struct rt_mutex *lock, int state,
 }
 
 static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
-				     struct rt_mutex_waiter *w)
+				     struct rt_mutex_waiter *w, struct rt_mutex *lock)
 {
 	/*
 	 * If the result is not -EDEADLOCK or the caller requested
@@ -1218,6 +1218,8 @@ static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
 	 * Yell lowdly and stop the task right here.
 	 */
 	rt_mutex_print_deadlock(w);
+	/* We're not going anywhere, release the wait_lock */
+	raw_spin_unlock_irq(&lock->wait_lock);
 	while (1) {
 		set_current_state(TASK_INTERRUPTIBLE);
 		schedule();

kernel/locking/rtmutex.c rt_mutex_slowlock(struct rt_mutex *lock, int state, · line 1275

Quoted 6 lines
kernel/locking/rtmutex.c +1 -1
@@ -1269,7 +1271,7 @@ 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);
+		rt_mutex_handle_deadlock(ret, chwalk, &waiter, lock);
 	}

The patch is correct; but I can't find myself liking it very much. This dinly little single use function is growing a lot of arguments.

The alternative is something like the below; not sure myself though. Thomas?

kernel/locking/rtmutex.c +6 -11
index 2823d4163a37..a44d4034e232 100644
@@ -1204,18 +1204,10 @@ __rt_mutex_slowlock(struct rt_mutex *lock, int state,
 	return ret;
 }
 
-static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
-				     struct rt_mutex_waiter *w)
+static void rt_mutex_handle_deadlock(struct rt_mutex_waiter *w)
 {
 	/*
-	 * If the result is not -EDEADLOCK or the caller requested
-	 * deadlock detection, nothing to do here.
-	 */
-	if (res != -EDEADLOCK || detect_deadlock)
-		return;
-
-	/*
-	 * Yell lowdly and stop the task right here.
+	 * Yell loudly and stop the task right here.
 	 */
 	rt_mutex_print_deadlock(w);
 	while (1) {
@@ -1269,7 +1261,10 @@ 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);
+		if (chwalk == RT_MUTEX_MIN_CHAINWALK && ret == -EDEADLOCK) {
+			raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
+			rt_mutex_handle_deadlock(&waiter);
+		}
 	}
 
 	/*