Skip to contentSkip to conversation

mutex subsystem, debugging code

PATCH In review Ingo Molnar 2 participants 3 messages Jan 5, 2006 – Jan 6, 2006

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

PATCH 10/21 mutex subsystem, debugging code

mutex implementation - add debugging code.

----

 include/linux/mutex-debug.h |   21 +
 include/linux/sched.h       |    5 
 kernel/Makefile             |    1 
 kernel/fork.c               |    4 
 kernel/mutex-debug.c        |  464 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/mutex-debug.h        |  134 ++++++++++++
 lib/Kconfig.debug           |    8 
 7 files changed, 637 insertions(+)
include/linux/mutex-debug.h new file +21 -0
Index: linux/include/linux/mutex-debug.h
===================================================================
@@ -0,0 +1,21 @@
+#ifndef __LINUX_MUTEX_DEBUG_H
+#define __LINUX_MUTEX_DEBUG_H
+
+/*
+ * Mutexes - debugging helpers:
+ */
+
+#define __DEBUG_MUTEX_INITIALIZER(lockname) \
+	, .held_list = LIST_HEAD_INIT(lockname.held_list), \
+	  .name = #lockname , .magic = &lockname
+
+#define mutex_init(sem)		__mutex_init(sem, __FUNCTION__)
+
+extern void FASTCALL(mutex_destroy(struct mutex *lock));
+
+extern void mutex_debug_show_all_locks(void);
+extern void mutex_debug_show_held_locks(struct task_struct *filter);
+extern void mutex_debug_check_no_locks_held(struct task_struct *task);
+extern void mutex_debug_check_no_locks_freed(const void *from, const void *to);
+
+#endif
include/linux/sched.h +5 -0
Index: linux/include/linux/sched.h
===================================================================
@@ -820,6 +820,11 @@ struct task_struct {
 /* Protection of proc_dentry: nesting proc_lock, dcache_lock, write_lock_irq(&tasklist_lock); */
 	spinlock_t proc_lock;
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	/* mutex deadlock detection */
+	struct mutex_waiter *blocked_on;
+#endif
+
 /* journalling filesystem info */
 	void *journal_info;
 
kernel/Makefile +1 -0
Index: linux/kernel/Makefile
===================================================================
@@ -9,6 +9,7 @@ obj-y     = sched.o fork.o exec_domain.o
 	    rcupdate.o intermodule.o extable.o params.o posix-timers.o \
 	    kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o
 
+obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
 obj-$(CONFIG_FUTEX) += futex.o
 obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o
 obj-$(CONFIG_SMP) += cpu.o spinlock.o
kernel/fork.c +4 -0
Index: linux/kernel/fork.c
===================================================================
@@ -973,6 +973,10 @@ static task_t *copy_process(unsigned lon
  	}
 #endif
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	p->blocked_on = NULL; /* not blocked yet */
+#endif
+
 	p->tgid = p->pid;
 	if (clone_flags & CLONE_THREAD)
 		p->tgid = current->tgid;
kernel/mutex-debug.c new file +464 -0

464 lines not on this page · raw message

kernel/mutex-debug.h new file +134 -0

134 lines not on this page · raw message

lib/Kconfig.debug +8 -0
Index: linux/lib/Kconfig.debug
===================================================================
@@ -95,6 +95,14 @@ config DEBUG_PREEMPT
 	  if kernel code uses it in a preemption-unsafe way. Also, the kernel
 	  will detect preemption count underflows.
 
+config DEBUG_MUTEXES
+	bool "Mutex debugging, deadlock detection"
+	default y
+	depends on DEBUG_KERNEL
+	help
+	 This allows mutex semantics violations and mutex related deadlocks
+	 (lockups) to be detected and reported automatically.
+
 config DEBUG_SPINLOCK
 	bool "Spinlock debugging"
 	depends on DEBUG_KERNEL
#1 lore raw
Quoted 75 lines · Ingo Molnar

mutex implementation - add debugging code.

----

 include/linux/mutex-debug.h |   21 +
 include/linux/sched.h       |    5 
 kernel/Makefile             |    1 
 kernel/fork.c               |    4 
 kernel/mutex-debug.c        |  464 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/mutex-debug.h        |  134 ++++++++++++
 lib/Kconfig.debug           |    8 
 7 files changed, 637 insertions(+)

Index: linux/include/linux/mutex-debug.h
===================================================================

include/linux/mutex-debug.h new file +21 -0
@@ -0,0 +1,21 @@
+#ifndef __LINUX_MUTEX_DEBUG_H
+#define __LINUX_MUTEX_DEBUG_H
+
+/*
+ * Mutexes - debugging helpers:
+ */
+
+#define __DEBUG_MUTEX_INITIALIZER(lockname) \
+	, .held_list = LIST_HEAD_INIT(lockname.held_list), \
+	  .name = #lockname , .magic = &lockname
+
+#define mutex_init(sem)		__mutex_init(sem, __FUNCTION__)
+
+extern void FASTCALL(mutex_destroy(struct mutex *lock));
+
+extern void mutex_debug_show_all_locks(void);
+extern void mutex_debug_show_held_locks(struct task_struct *filter);
+extern void mutex_debug_check_no_locks_held(struct task_struct *task);
+extern void mutex_debug_check_no_locks_freed(const void *from, const void *to);
+
+#endif

Index: linux/include/linux/sched.h
===================================================================

include/linux/sched.h +5 -0
@@ -820,6 +820,11 @@ struct task_struct {
 /* Protection of proc_dentry: nesting proc_lock, dcache_lock, write_lock_irq(&tasklist_lock); */
 	spinlock_t proc_lock;
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	/* mutex deadlock detection */
+	struct mutex_waiter *blocked_on;
+#endif
+
 /* journalling filesystem info */
 	void *journal_info;
 

Index: linux/kernel/Makefile
===================================================================

kernel/Makefile +1 -0
@@ -9,6 +9,7 @@ obj-y     = sched.o fork.o exec_domain.o
 	    rcupdate.o intermodule.o extable.o params.o posix-timers.o \
 	    kthread.o wait.o kfifo.o sys_ni.o posix-cpu-timers.o mutex.o
 
+obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
 obj-$(CONFIG_FUTEX) += futex.o
 obj-$(CONFIG_GENERIC_ISA_DMA) += dma.o
 obj-$(CONFIG_SMP) += cpu.o spinlock.o

Index: linux/kernel/fork.c
===================================================================

kernel/fork.c static task_t *copy_process(unsigned lon · line 982

Quoted 10 lines
kernel/fork.c +4 -0
@@ -973,6 +973,10 @@ static task_t *copy_process(unsigned lon
  	}
 #endif
 
+#ifdef CONFIG_DEBUG_MUTEXES
+	p->blocked_on = NULL; /* not blocked yet */
+#endif
+
 	p->tgid = p->pid;
 	if (clone_flags & CLONE_THREAD)
 		p->tgid = current->tgid;

Should there be a corresponding initialization of init's blocked_on field in include/linux/init_task.h?

<snip>

Cheers,
-Matt Helsley

#2 lore raw
Quoted 8 lines · Matt Helsley
Quoted 8 lines

Index: linux/kernel/fork.c
===================================================================

kernel/fork.c +0 -0
@@ -973,6 +973,10 @@ static task_t *copy_process(unsigned lon
  	}
 #endif
 
Quoted 10 lines

kernel/fork.c static task_t *copy_process(unsigned lon · line 982

Quoted 7 lines
kernel/fork.c +4 -0
+#ifdef CONFIG_DEBUG_MUTEXES
+	p->blocked_on = NULL; /* not blocked yet */
+#endif
+
 	p->tgid = p->pid;
 	if (clone_flags & CLONE_THREAD)
 		p->tgid = current->tgid;

Should there be a corresponding initialization of init's blocked_on field in include/linux/init_task.h?

no, since it's initialized to zero.

Ingo