All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib/spinlock_debug.c: prevent deadlock in spin_dump() by console_sem
@ 2016-01-15  9:24 Byungchul Park
  2016-01-15  9:30 ` Ingo Molnar
  0 siblings, 1 reply; 2+ messages in thread
From: Byungchul Park @ 2016-01-15  9:24 UTC (permalink / raw)
  To: akpm; +Cc: mingo, linux-kernel, akinobu.mita, jack, torvalds

Deadlock occured when using CONFIG_DEBUG_SPINLOCK, in spin_dump().
Backtrace prints printk() -> console_trylock() -> do_raw_spin_lock() ->
spin_dump() -> printk()... infinitely.

If spin_bug() is called from a function like printk() which is using
console lock, we should prevent the debug spinlock code from calling
printk() any more in the context.

Signed-off-by: Byungchul Park <byungchul.park@lge.com>
---
 kernel/locking/spinlock_debug.c | 10 ++++++++++
 kernel/printk/printk.c          |  5 +++++
 2 files changed, 15 insertions(+)

diff --git a/kernel/locking/spinlock_debug.c b/kernel/locking/spinlock_debug.c
index 0374a59..b339c5e 100644
--- a/kernel/locking/spinlock_debug.c
+++ b/kernel/locking/spinlock_debug.c
@@ -67,11 +67,21 @@ static void spin_dump(raw_spinlock_t *lock, const char *msg)
 	dump_stack();
 }
 
+extern int is_console_lock(raw_spinlock_t *lock);
+
 static void spin_bug(raw_spinlock_t *lock, const char *msg)
 {
 	if (!debug_locks_off())
 		return;
 
+	/*
+	 * If this function is called from a function like printk()
+	 * which is trying console lock, we cannot call printk() any
+	 * more. Or it's obviously deadlock!
+	 */
+	if (unlikely(is_console_lock(lock)))
+		return;
+
 	spin_dump(lock, msg);
 }
 
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 2ce8826..50ea552 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -119,6 +119,11 @@ static int __down_trylock_console_sem(unsigned long ip)
 	up(&console_sem);\
 } while (0)
 
+int is_console_lock(raw_spinlock_t *lock)
+{
+	return &console_sem.lock == lock;
+}
+
 /*
  * This is used for debugging the mess that is the VT code by
  * keeping track if we have the console semaphore held. It's
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] lib/spinlock_debug.c: prevent deadlock in spin_dump() by console_sem
  2016-01-15  9:24 [PATCH] lib/spinlock_debug.c: prevent deadlock in spin_dump() by console_sem Byungchul Park
@ 2016-01-15  9:30 ` Ingo Molnar
  0 siblings, 0 replies; 2+ messages in thread
From: Ingo Molnar @ 2016-01-15  9:30 UTC (permalink / raw)
  To: Byungchul Park
  Cc: akpm, linux-kernel, akinobu.mita, jack, torvalds, Peter Zijlstra


* Byungchul Park <byungchul.park@lge.com> wrote:

> Deadlock occured when using CONFIG_DEBUG_SPINLOCK, in spin_dump().
> Backtrace prints printk() -> console_trylock() -> do_raw_spin_lock() ->
> spin_dump() -> printk()... infinitely.
> 
> If spin_bug() is called from a function like printk() which is using
> console lock, we should prevent the debug spinlock code from calling
> printk() any more in the context.
> 
> Signed-off-by: Byungchul Park <byungchul.park@lge.com>
> ---
>  kernel/locking/spinlock_debug.c | 10 ++++++++++
>  kernel/printk/printk.c          |  5 +++++
>  2 files changed, 15 insertions(+)
> 
> diff --git a/kernel/locking/spinlock_debug.c b/kernel/locking/spinlock_debug.c
> index 0374a59..b339c5e 100644
> --- a/kernel/locking/spinlock_debug.c
> +++ b/kernel/locking/spinlock_debug.c
> @@ -67,11 +67,21 @@ static void spin_dump(raw_spinlock_t *lock, const char *msg)
>  	dump_stack();
>  }
>  
> +extern int is_console_lock(raw_spinlock_t *lock);
> +
>  static void spin_bug(raw_spinlock_t *lock, const char *msg)
>  {
>  	if (!debug_locks_off())
>  		return;
>  
> +	/*
> +	 * If this function is called from a function like printk()
> +	 * which is trying console lock, we cannot call printk() any
> +	 * more. Or it's obviously deadlock!

Small spelling nit, the last sentence is not correct, pick one of these variants:

  'It's an obvious deadlock.'
  'It's obviously a deadlock.'

Thanks,

	Ingo

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-01-15  9:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-15  9:24 [PATCH] lib/spinlock_debug.c: prevent deadlock in spin_dump() by console_sem Byungchul Park
2016-01-15  9:30 ` Ingo Molnar

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.