]>
Commit | Line | Data |
---|---|---|
408894ee IM |
1 | /* |
2 | * kernel/mutex-debug.c | |
3 | * | |
4 | * Debugging code for mutexes | |
5 | * | |
6 | * Started by Ingo Molnar: | |
7 | * | |
8 | * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | |
9 | * | |
10 | * lock debugging, locking tree, deadlock detection started by: | |
11 | * | |
12 | * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey | |
13 | * Released under the General Public License (GPL). | |
14 | */ | |
15 | #include <linux/mutex.h> | |
16 | #include <linux/sched.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/module.h> | |
a7807a32 | 19 | #include <linux/poison.h> |
408894ee IM |
20 | #include <linux/spinlock.h> |
21 | #include <linux/kallsyms.h> | |
22 | #include <linux/interrupt.h> | |
23 | ||
408894ee IM |
24 | #include "mutex-debug.h" |
25 | ||
26 | /* | |
27 | * We need a global lock when we walk through the multi-process | |
28 | * lock tree. Only used in the deadlock-debugging case. | |
29 | */ | |
30 | DEFINE_SPINLOCK(debug_mutex_lock); | |
31 | ||
32 | /* | |
33 | * All locks held by all tasks, in a single global list: | |
34 | */ | |
35 | LIST_HEAD(debug_mutex_held_locks); | |
36 | ||
37 | /* | |
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: | |
41 | */ | |
42 | #define __IP_DECL__ , unsigned long ip | |
43 | #define __IP__ , ip | |
44 | #define __RET_IP__ , (unsigned long)__builtin_return_address(0) | |
45 | ||
46 | /* | |
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 | |
50 | * executing a BUG(): | |
51 | */ | |
52 | int debug_mutex_on = 1; | |
53 | ||
408894ee IM |
54 | /* |
55 | * Must be called with lock->wait_lock held. | |
56 | */ | |
57 | void debug_mutex_set_owner(struct mutex *lock, | |
58 | struct thread_info *new_owner __IP_DECL__) | |
59 | { | |
60 | lock->owner = new_owner; | |
9e7f4d45 | 61 | DEBUG_LOCKS_WARN_ON(!list_empty(&lock->held_list)); |
408894ee IM |
62 | if (debug_mutex_on) { |
63 | list_add_tail(&lock->held_list, &debug_mutex_held_locks); | |
64 | lock->acquire_ip = ip; | |
65 | } | |
66 | } | |
67 | ||
68 | void debug_mutex_init_waiter(struct mutex_waiter *waiter) | |
69 | { | |
a7807a32 | 70 | memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter)); |
408894ee IM |
71 | waiter->magic = waiter; |
72 | INIT_LIST_HEAD(&waiter->list); | |
73 | } | |
74 | ||
75 | void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter) | |
76 | { | |
9e7f4d45 IM |
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)); | |
408894ee IM |
81 | } |
82 | ||
83 | void debug_mutex_free_waiter(struct mutex_waiter *waiter) | |
84 | { | |
9e7f4d45 | 85 | DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list)); |
a7807a32 | 86 | memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter)); |
408894ee IM |
87 | } |
88 | ||
89 | void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, | |
90 | struct thread_info *ti __IP_DECL__) | |
91 | { | |
9e7f4d45 | 92 | SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock)); |
408894ee IM |
93 | /* Mark the current thread as blocked on the lock: */ |
94 | ti->task->blocked_on = waiter; | |
95 | waiter->lock = lock; | |
96 | } | |
97 | ||
98 | void mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter, | |
99 | struct thread_info *ti) | |
100 | { | |
9e7f4d45 IM |
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); | |
408894ee IM |
104 | ti->task->blocked_on = NULL; |
105 | ||
106 | list_del_init(&waiter->list); | |
107 | waiter->task = NULL; | |
108 | } | |
109 | ||
110 | void debug_mutex_unlock(struct mutex *lock) | |
111 | { | |
9e7f4d45 IM |
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()); | |
408894ee | 115 | if (debug_mutex_on) { |
9e7f4d45 | 116 | DEBUG_LOCKS_WARN_ON(list_empty(&lock->held_list)); |
408894ee IM |
117 | list_del_init(&lock->held_list); |
118 | } | |
119 | } | |
120 | ||
121 | void debug_mutex_init(struct mutex *lock, const char *name) | |
122 | { | |
123 | /* | |
124 | * Make sure we are not reinitializing a held lock: | |
125 | */ | |
a4fc7ab1 | 126 | mutex_debug_check_no_locks_freed((void *)lock, sizeof(*lock)); |
408894ee IM |
127 | lock->owner = NULL; |
128 | INIT_LIST_HEAD(&lock->held_list); | |
129 | lock->name = name; | |
130 | lock->magic = lock; | |
131 | } | |
132 | ||
133 | /*** | |
134 | * mutex_destroy - mark a mutex unusable | |
135 | * @lock: the mutex to be destroyed | |
136 | * | |
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. | |
140 | */ | |
141 | void fastcall mutex_destroy(struct mutex *lock) | |
142 | { | |
9e7f4d45 | 143 | DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock)); |
408894ee IM |
144 | lock->magic = NULL; |
145 | } | |
146 | ||
147 | EXPORT_SYMBOL_GPL(mutex_destroy); |