public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] debug workqueue locking sanity
@ 2006-11-05 20:36 Peter Zijlstra
  2006-11-06  7:23 ` Ingo Molnar
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Zijlstra @ 2006-11-05 20:36 UTC (permalink / raw)
  To: linux-kernel, Andrew Morton; +Cc: Ingo Molnar, arjan


Workqueue functions should not leak locks, assert so, printing the
last function ran.

Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
---
 kernel/workqueue.c |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)

Index: linux-2.6-twins/kernel/workqueue.c
===================================================================
--- linux-2.6-twins.orig/kernel/workqueue.c	2006-11-05 21:04:13.000000000 +0100
+++ linux-2.6-twins/kernel/workqueue.c	2006-11-05 21:16:05.000000000 +0100
@@ -29,6 +29,7 @@
 #include <linux/kthread.h>
 #include <linux/hardirq.h>
 #include <linux/mempolicy.h>
+#include <linux/kallsyms.h>
 
 /*
  * The per-CPU workqueue (if single thread, we always use the first
@@ -222,6 +223,21 @@ static void run_workqueue(struct cpu_wor
 		clear_bit(0, &work->pending);
 		f(data);
 
+		if (unlikely(in_atomic()
+#ifdef CONFIG_LOCKDEP
+			|| current->lockdep_depth > 0
+#endif
+			)) {
+			printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
+					"%s/0x%08x/%d\n",
+					current->comm, preempt_count(),
+				       	current->pid);
+			printk(KERN_ERR "    last function: ");
+			print_symbol("%s\n", (unsigned long)f);
+			debug_show_held_locks(current);
+			dump_stack();
+		}
+
 		spin_lock_irqsave(&cwq->lock, flags);
 		cwq->remove_sequence++;
 		wake_up(&cwq->work_done);



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

* Re: [PATCH] debug workqueue locking sanity
  2006-11-05 20:36 [PATCH] debug workqueue locking sanity Peter Zijlstra
@ 2006-11-06  7:23 ` Ingo Molnar
  2006-11-06  7:36   ` [PATCH] debug workqueue locking sanity -v2 Peter Zijlstra
  0 siblings, 1 reply; 4+ messages in thread
From: Ingo Molnar @ 2006-11-06  7:23 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: linux-kernel, Andrew Morton, arjan


* Peter Zijlstra <a.p.zijlstra@chello.nl> wrote:

> +		if (unlikely(in_atomic()
> +#ifdef CONFIG_LOCKDEP
> +			|| current->lockdep_depth > 0
> +#endif
> +			)) {

i agree with this patch, but shouldnt this #ifdef be hidden via some 
sort of lockdep_depth() inline in lockdep.h that just returns 0 if 
!LOCKDEP?

	Ingo

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

* [PATCH] debug workqueue locking sanity -v2
  2006-11-06  7:23 ` Ingo Molnar
@ 2006-11-06  7:36   ` Peter Zijlstra
  2006-11-06  7:46     ` Ingo Molnar
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Zijlstra @ 2006-11-06  7:36 UTC (permalink / raw)
  To: Ingo Molnar; +Cc: linux-kernel, Andrew Morton, arjan

On Mon, 2006-11-06 at 08:23 +0100, Ingo Molnar wrote:
> * Peter Zijlstra <a.p.zijlstra@chello.nl> wrote:
> 
> > +		if (unlikely(in_atomic()
> > +#ifdef CONFIG_LOCKDEP
> > +			|| current->lockdep_depth > 0
> > +#endif
> > +			)) {
> 
> i agree with this patch, but shouldnt this #ifdef be hidden via some 
> sort of lockdep_depth() inline in lockdep.h that just returns 0 if 
> !LOCKDEP?

Like so.

---

Workqueue functions should not leak locks, assert so, printing the
last function ran.

Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
---
 include/linux/lockdep.h |   11 +++++++++++
 kernel/workqueue.c      |   12 ++++++++++++
 2 files changed, 23 insertions(+)

Index: linux-2.6-twins/kernel/workqueue.c
===================================================================
--- linux-2.6-twins.orig/kernel/workqueue.c	2006-11-05 21:22:35.000000000 +0100
+++ linux-2.6-twins/kernel/workqueue.c	2006-11-06 08:31:21.000000000 +0100
@@ -29,6 +29,7 @@
 #include <linux/kthread.h>
 #include <linux/hardirq.h>
 #include <linux/mempolicy.h>
+#include <linux/kallsyms.h>
 
 /*
  * The per-CPU workqueue (if single thread, we always use the first
@@ -222,6 +223,17 @@ static void run_workqueue(struct cpu_wor
 		clear_bit(0, &work->pending);
 		f(data);
 
+		if (unlikely(in_atomic() || lockdep_depth(current) > 0)) {
+			printk(KERN_ERR "BUG: workqueue leaked lock or atomic: "
+					"%s/0x%08x/%d\n",
+					current->comm, preempt_count(),
+				       	current->pid);
+			printk(KERN_ERR "    last function: ");
+			print_symbol("%s\n", (unsigned long)f);
+			debug_show_held_locks(current);
+			dump_stack();
+		}
+
 		spin_lock_irqsave(&cwq->lock, flags);
 		cwq->remove_sequence++;
 		wake_up(&cwq->work_done);
Index: linux-2.6-twins/include/linux/lockdep.h
===================================================================
--- linux-2.6-twins.orig/include/linux/lockdep.h	2006-11-06 08:32:25.000000000 +0100
+++ linux-2.6-twins/include/linux/lockdep.h	2006-11-06 08:32:28.000000000 +0100
@@ -243,6 +243,11 @@ extern void lock_release(struct lockdep_
 
 # define INIT_LOCKDEP				.lockdep_recursion = 0,
 
+static inline int lockdep_depth(struct task *tsk)
+{
+	return tsk->lockdep_depth;
+}
+
 #else /* !LOCKDEP */
 
 static inline void lockdep_off(void)
@@ -277,6 +282,12 @@ static inline int lockdep_internal(void)
  * The class key takes no space if lockdep is disabled:
  */
 struct lock_class_key { };
+
+static inline int lockdep_depth(struct task *tsk)
+{
+	return 0;
+}
+
 #endif /* !LOCKDEP */
 
 #if defined(CONFIG_TRACE_IRQFLAGS) && defined(CONFIG_GENERIC_HARDIRQS)



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

* Re: [PATCH] debug workqueue locking sanity -v2
  2006-11-06  7:36   ` [PATCH] debug workqueue locking sanity -v2 Peter Zijlstra
@ 2006-11-06  7:46     ` Ingo Molnar
  0 siblings, 0 replies; 4+ messages in thread
From: Ingo Molnar @ 2006-11-06  7:46 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: linux-kernel, Andrew Morton, arjan


* Peter Zijlstra <a.p.zijlstra@chello.nl> wrote:

> > > +			|| current->lockdep_depth > 0
> > > +#endif
> > > +			)) {
> > 
> > i agree with this patch, but shouldnt this #ifdef be hidden via some 
> > sort of lockdep_depth() inline in lockdep.h that just returns 0 if 
> > !LOCKDEP?
> 
> Like so.

perfect!

Acked-by: Ingo Molnar <mingo@elte.hu>

	Ingo

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

end of thread, other threads:[~2006-11-06  7:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-11-05 20:36 [PATCH] debug workqueue locking sanity Peter Zijlstra
2006-11-06  7:23 ` Ingo Molnar
2006-11-06  7:36   ` [PATCH] debug workqueue locking sanity -v2 Peter Zijlstra
2006-11-06  7:46     ` Ingo Molnar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox