Skip to contentSkip to conversation

Fix lockup on panic with lockdep

PATCH In review Derek Basehore 3 participants 3 messages May 13, 2014 – May 14, 2014 Printk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix lockup on panic with lockdep

If we don't call mutex_acquire at the beginning of console_unblank, we can run into a lockup on the logbuf_lock between console_unlock and printk during panic. What happens in console_unlock is:

-locks logbuf_lock
-calls mutex_release
-which calls printk
-which locks logbuf_lock

This fixes the problem by calling console_trylock (which calls mutex_acquire) instead of directly accessing the semaphore in console_unblank and moves the mutex_release to after we unlock logbuf_lock in console_unlock (interrupts are still disabled).

---
 kernel/printk/printk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/printk/printk.c +2 -2
index 7228258..c599ab5 100644
@@ -2084,7 +2084,6 @@ skip:
 		local_irq_restore(flags);
 	}
 	console_locked = 0;
-	mutex_release(&console_lock_dep_map, 1, _RET_IP_);
 
 	/* Release the exclusive_console once it is used */
 	if (unlikely(exclusive_console))
@@ -2092,6 +2091,7 @@ skip:
 
 	raw_spin_unlock(&logbuf_lock);
 
+	mutex_release(&console_lock_dep_map, 1, _RET_IP_);
 	up(&console_sem);
 
 	/*
@@ -2137,7 +2137,7 @@ void console_unblank(void)
 	 * oops_in_progress is set to 1..
 	 */
 	if (oops_in_progress) {
-		if (down_trylock(&console_sem) != 0)
+		if (!console_trylock())
 			return;
 	} else
 		console_lock();
Signature · 1 line
1.9.1.423.g4596e3a
#1 lore raw
Quoted 9 lines · On Mon, 12 May 2014 17:41:54 -0700 Derek Basehore <[email protected]>…

If we don't call mutex_acquire at the beginning of console_unblank, we can run into a lockup on the logbuf_lock between console_unlock and printk during panic. What happens in console_unlock is:

(quoted diff) +0 -2
-locks logbuf_lock
-calls mutex_release
 -which calls printk
  -which locks logbuf_lock
Quoted 4 lines

This fixes the problem by calling console_trylock (which calls mutex_acquire) instead of directly accessing the semaphore in console_unblank and moves the mutex_release to after we unlock logbuf_lock in console_unlock (interrupts are still disabled).

Please take a look at linux-next, where this code has changed a lot.

Quoted 20 lines

index 7228258..c599ab5 100644

kernel/printk/printk.c +1 -1
@@ -2084,7 +2084,6 @@ skip:
 		local_irq_restore(flags);
 	}
 	console_locked = 0;
-	mutex_release(&console_lock_dep_map, 1, _RET_IP_);
 
 	/* Release the exclusive_console once it is used */
 	if (unlikely(exclusive_console))
@@ -2092,6 +2091,7 @@ skip:
 
 	raw_spin_unlock(&logbuf_lock);
 
+	mutex_release(&console_lock_dep_map, 1, _RET_IP_);
 	up(&console_sem);
 
 	/*

kernel/printk/printk.c void console_unblank(void) · line 2143

Quoted 8 lines
kernel/printk/printk.c +1 -1
@@ -2137,7 +2137,7 @@ void console_unblank(void)
 	 * oops_in_progress is set to 1..
 	 */
 	if (oops_in_progress) {
-		if (down_trylock(&console_sem) != 0)
+		if (!console_trylock())
 			return;
 	} else
 		console_lock();

Although the code is quite different, an equivalent change appears to have been made already. I'm not sure that bugfix was intentional ;)

#2 lore raw
Quoted 36 lines · Andrew Morton
Quoted 9 lines · On Mon, 12 May 2014 17:41:54 -0700 Derek Basehore <[email protected]>…

If we don't call mutex_acquire at the beginning of console_unblank, we can run into a lockup on the logbuf_lock between console_unlock and printk during panic. What happens in console_unlock is:

(quoted diff) +0 -2
-locks logbuf_lock
-calls mutex_release
 -which calls printk
  -which locks logbuf_lock
Quoted 4 lines

This fixes the problem by calling console_trylock (which calls mutex_acquire) instead of directly accessing the semaphore in console_unblank and moves the mutex_release to after we unlock logbuf_lock in console_unlock (interrupts are still disabled).

Please take a look at linux-next, where this code has changed a lot.

Quoted 18 lines

index 7228258..c599ab5 100644

kernel/printk/printk.c +1 -1
@@ -2084,7 +2084,6 @@ skip:
 		local_irq_restore(flags);
 	}
 	console_locked = 0;
-	mutex_release(&console_lock_dep_map, 1, _RET_IP_);
 
 	/* Release the exclusive_console once it is used */
 	if (unlikely(exclusive_console))
@@ -2092,6 +2091,7 @@ skip:
 
 	raw_spin_unlock(&logbuf_lock);
 
+	mutex_release(&console_lock_dep_map, 1, _RET_IP_);
 	up(&console_sem);
 
Quoted 13 lines

kernel/printk/printk.c void console_unblank(void) · line 2143

Quoted 10 lines
kernel/printk/printk.c +1 -1
 	/*
@@ -2137,7 +2137,7 @@ void console_unblank(void)
 	 * oops_in_progress is set to 1..
 	 */
 	if (oops_in_progress) {
-		if (down_trylock(&console_sem) != 0)
+		if (!console_trylock())
 			return;
 	} else
 		console_lock();

Although the code is quite different, an equivalent change appears to have been made already. I'm not sure that bugfix was intentional ;)

It was intentional. It would take extra work to not fix the bug when I
was cleaning up those lockdep annotations so I decided to just fix it... ;)

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR