Skip to contentSkip to conversation

debug workqueue locking sanity

PATCH In review Peter Zijlstra 2 participants 4 messages Nov 5, 2006 – Nov 6, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 debug workqueue locking sanity

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

---
 kernel/workqueue.c |   16 ++++++++++++++++
 1 file changed, 16 insertions(+)
kernel/workqueue.c +16 -0
Index: linux-2.6-twins/kernel/workqueue.c
===================================================================
@@ -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);
#1 lore raw

kernel/workqueue.c static void run_workqueue(struct cpu_wor · line 230

Quoted 5 lines · Peter Zijlstra
kernel/workqueue.c +5 -0
+		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

#2 lore raw

PATCH 1/1 debug workqueue locking sanity -v2

Quoted 11 lines · Ingo Molnar

Review

Quoted 5 lines · Peter Zijlstra
(quoted diff) +5 -0
+		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.

---
 include/linux/lockdep.h |   11 +++++++++++
 kernel/workqueue.c      |   12 ++++++++++++
 2 files changed, 23 insertions(+)
kernel/workqueue.c +12 -0
Index: linux-2.6-twins/kernel/workqueue.c
===================================================================
@@ -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);
include/linux/lockdep.h +11 -0
Index: linux-2.6-twins/include/linux/lockdep.h
===================================================================
@@ -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)
Acked-by on v1 1/1
#3 lore raw

Re: [PATCH] debug workqueue locking sanity -v2

Quoted 9 lines · Peter Zijlstra
Quoted 7 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+			|| 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!

Ingo