4 * Debugging code for mutexes
6 * Started by Ingo Molnar:
10 * lock debugging, locking tree, deadlock detection started by:
12 * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
13 * Released under the General Public License (GPL).
15 #include <linux/mutex.h>
16 #include <linux/sched.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/poison.h>
20 #include <linux/spinlock.h>
21 #include <linux/kallsyms.h>
22 #include <linux/interrupt.h>
24 #include "mutex-debug.h"
27 * We need a global lock when we walk through the multi-process
28 * lock tree. Only used in the deadlock-debugging case.
30 DEFINE_SPINLOCK(debug_mutex_lock);
33 * All locks held by all tasks, in a single global list:
35 LIST_HEAD(debug_mutex_held_locks);
38 * In the debug case we carry the caller's instruction pointer into
39 * other functions, but we dont want the function argument overhead
40 * in the nondebug case - hence these macros:
42 #define __IP_DECL__ , unsigned long ip
44 #define __RET_IP__ , (unsigned long)__builtin_return_address(0)
47 * "mutex debugging enabled" flag. We turn it off when we detect
48 * the first problem because we dont want to recurse back
49 * into the tracing code when doing error printk or
52 int debug_mutex_on = 1;
55 * Must be called with lock->wait_lock held.
57 void debug_mutex_set_owner(struct mutex *lock,
58 struct thread_info *new_owner __IP_DECL__)
60 lock->owner = new_owner;
61 DEBUG_LOCKS_WARN_ON(!list_empty(&lock->held_list));
63 list_add_tail(&lock->held_list, &debug_mutex_held_locks);
64 lock->acquire_ip = ip;
68 void debug_mutex_init_waiter(struct mutex_waiter *waiter)
70 memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
71 waiter->magic = waiter;
72 INIT_LIST_HEAD(&waiter->list);
75 void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter)
77 SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock));
78 DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list));
79 DEBUG_LOCKS_WARN_ON(waiter->magic != waiter);
80 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
83 void debug_mutex_free_waiter(struct mutex_waiter *waiter)
85 DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list));
86 memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter));
89 void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
90 struct thread_info *ti __IP_DECL__)
92 SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock));
93 /* Mark the current thread as blocked on the lock: */
94 ti->task->blocked_on = waiter;
98 void mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
99 struct thread_info *ti)
101 DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
102 DEBUG_LOCKS_WARN_ON(waiter->task != ti->task);
103 DEBUG_LOCKS_WARN_ON(ti->task->blocked_on != waiter);
104 ti->task->blocked_on = NULL;
106 list_del_init(&waiter->list);
110 void debug_mutex_unlock(struct mutex *lock)
112 DEBUG_LOCKS_WARN_ON(lock->magic != lock);
113 DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
114 DEBUG_LOCKS_WARN_ON(lock->owner != current_thread_info());
115 if (debug_mutex_on) {
116 DEBUG_LOCKS_WARN_ON(list_empty(&lock->held_list));
117 list_del_init(&lock->held_list);
121 void debug_mutex_init(struct mutex *lock, const char *name)
124 * Make sure we are not reinitializing a held lock:
126 mutex_debug_check_no_locks_freed((void *)lock, sizeof(*lock));
128 INIT_LIST_HEAD(&lock->held_list);
134 * mutex_destroy - mark a mutex unusable
135 * @lock: the mutex to be destroyed
137 * This function marks the mutex uninitialized, and any subsequent
138 * use of the mutex is forbidden. The mutex must not be locked when
139 * this function is called.
141 void fastcall mutex_destroy(struct mutex *lock)
143 DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock));
147 EXPORT_SYMBOL_GPL(mutex_destroy);