4 * Debugging code for mutexes
6 * Started by Ingo Molnar:
8 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
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/spinlock.h>
20 #include <linux/kallsyms.h>
21 #include <linux/interrupt.h>
23 #include "mutex-debug.h"
26 * We need a global lock when we walk through the multi-process
27 * lock tree. Only used in the deadlock-debugging case.
29 DEFINE_SPINLOCK(debug_mutex_lock);
32 * All locks held by all tasks, in a single global list:
34 LIST_HEAD(debug_mutex_held_locks);
37 * In the debug case we carry the caller's instruction pointer into
38 * other functions, but we dont want the function argument overhead
39 * in the nondebug case - hence these macros:
41 #define __IP_DECL__ , unsigned long ip
43 #define __RET_IP__ , (unsigned long)__builtin_return_address(0)
46 * "mutex debugging enabled" flag. We turn it off when we detect
47 * the first problem because we dont want to recurse back
48 * into the tracing code when doing error printk or
51 int debug_mutex_on = 1;
53 static void printk_task(struct task_struct *p)
56 printk("%16s:%5d [%p, %3d]", p->comm, p->pid, p, p->prio);
61 static void printk_ti(struct thread_info *ti)
64 printk_task(ti->task);
69 static void printk_task_short(struct task_struct *p)
72 printk("%s/%d [%p, %3d]", p->comm, p->pid, p, p->prio);
77 static void printk_lock(struct mutex *lock, int print_owner)
79 printk(" [%p] {%s}\n", lock, lock->name);
81 if (print_owner && lock->owner) {
82 printk(".. held by: ");
83 printk_ti(lock->owner);
87 printk("... acquired at: ");
88 print_symbol("%s\n", lock->acquire_ip);
93 * printk locks held by a task:
95 static void show_task_locks(struct task_struct *p)
98 case TASK_RUNNING: printk("R"); break;
99 case TASK_INTERRUPTIBLE: printk("S"); break;
100 case TASK_UNINTERRUPTIBLE: printk("D"); break;
101 case TASK_STOPPED: printk("T"); break;
102 case EXIT_ZOMBIE: printk("Z"); break;
103 case EXIT_DEAD: printk("X"); break;
104 default: printk("?"); break;
108 struct mutex *lock = p->blocked_on->lock;
110 printk(" blocked on mutex:");
111 printk_lock(lock, 1);
113 printk(" (not blocked on mutex)\n");
117 * printk all locks held in the system (if filter == NULL),
118 * or all locks belonging to a single task (if filter != NULL):
120 void show_held_locks(struct task_struct *filter)
122 struct list_head *curr, *cursor = NULL;
124 struct thread_info *t;
129 printk("------------------------------\n");
130 printk("| showing all locks held by: | (");
131 printk_task_short(filter);
133 printk("------------------------------\n");
135 printk("---------------------------\n");
136 printk("| showing all locks held: |\n");
137 printk("---------------------------\n");
141 * Play safe and acquire the global trace lock. We
142 * cannot printk with that lock held so we iterate
146 debug_spin_lock_save(&debug_mutex_lock, flags);
147 list_for_each(curr, &debug_mutex_held_locks) {
148 if (cursor && curr != cursor)
150 lock = list_entry(curr, struct mutex, held_list);
152 if (filter && (t != filter->thread_info))
156 debug_spin_lock_restore(&debug_mutex_lock, flags);
158 printk("\n#%03d: ", count);
159 printk_lock(lock, filter ? 0 : 1);
162 debug_spin_lock_restore(&debug_mutex_lock, flags);
166 void mutex_debug_show_all_locks(void)
168 struct task_struct *g, *p;
172 printk("\nShowing all blocking locks in the system:\n");
175 * Here we try to get the tasklist_lock as hard as possible,
176 * if not successful after 2 seconds we ignore it (but keep
177 * trying). This is to enable a debug printout even if a
178 * tasklist_lock-holding task deadlocks or crashes.
181 if (!read_trylock(&tasklist_lock)) {
183 printk("hm, tasklist_lock locked, retrying... ");
186 printk(" #%d", 10-count);
190 printk(" ignoring it.\n");
194 printk(" locked it.\n");
196 do_each_thread(g, p) {
199 if (read_trylock(&tasklist_lock))
201 } while_each_thread(g, p);
204 show_held_locks(NULL);
205 printk("=============================================\n\n");
208 read_unlock(&tasklist_lock);
211 static void report_deadlock(struct task_struct *task, struct mutex *lock,
212 struct mutex *lockblk, unsigned long ip)
214 printk("\n%s/%d is trying to acquire this lock:\n",
215 current->comm, current->pid);
216 printk_lock(lock, 1);
217 printk("... trying at: ");
218 print_symbol("%s\n", ip);
219 show_held_locks(current);
222 printk("but %s/%d is deadlocking current task %s/%d!\n\n",
223 task->comm, task->pid, current->comm, current->pid);
224 printk("\n%s/%d is blocked on this lock:\n",
225 task->comm, task->pid);
226 printk_lock(lockblk, 1);
228 show_held_locks(task);
230 printk("\n%s/%d's [blocked] stackdump:\n\n",
231 task->comm, task->pid);
232 show_stack(task, NULL);
235 printk("\n%s/%d's [current] stackdump:\n\n",
236 current->comm, current->pid);
238 mutex_debug_show_all_locks();
239 printk("[ turning off deadlock detection. Please report this. ]\n\n");
244 * Recursively check for mutex deadlocks:
246 static int check_deadlock(struct mutex *lock, int depth,
247 struct thread_info *ti, unsigned long ip)
249 struct mutex *lockblk;
250 struct task_struct *task;
261 if (task->blocked_on)
262 lockblk = task->blocked_on->lock;
265 if (current == task) {
269 printk("\n==========================================\n");
270 printk( "[ BUG: lock recursion deadlock detected! |\n");
271 printk( "------------------------------------------\n");
272 report_deadlock(task, lock, NULL, ip);
276 /* Ugh, something corrupted the lock data structure? */
279 printk("\n===========================================\n");
280 printk( "[ BUG: infinite lock dependency detected!? |\n");
281 printk( "-------------------------------------------\n");
282 report_deadlock(task, lock, lockblk, ip);
286 /* Recursively check for dependencies: */
287 if (lockblk && check_deadlock(lockblk, depth+1, ti, ip)) {
288 printk("\n============================================\n");
289 printk( "[ BUG: circular locking deadlock detected! ]\n");
290 printk( "--------------------------------------------\n");
291 report_deadlock(task, lock, lockblk, ip);
298 * Called when a task exits, this function checks whether the
299 * task is holding any locks, and reports the first one if so:
301 void mutex_debug_check_no_locks_held(struct task_struct *task)
303 struct list_head *curr, *next;
304 struct thread_info *t;
311 debug_spin_lock_save(&debug_mutex_lock, flags);
312 list_for_each_safe(curr, next, &debug_mutex_held_locks) {
313 lock = list_entry(curr, struct mutex, held_list);
315 if (t != task->thread_info)
319 debug_spin_lock_restore(&debug_mutex_lock, flags);
321 printk("BUG: %s/%d, lock held at task exit time!\n",
322 task->comm, task->pid);
323 printk_lock(lock, 1);
324 if (lock->owner != task->thread_info)
325 printk("exiting task is not even the owner??\n");
328 debug_spin_lock_restore(&debug_mutex_lock, flags);
332 * Called when kernel memory is freed (or unmapped), or if a mutex
333 * is destroyed or reinitialized - this code checks whether there is
334 * any held lock in the memory range of <from> to <to>:
336 void mutex_debug_check_no_locks_freed(const void *from, const void *to)
338 struct list_head *curr, *next;
346 debug_spin_lock_save(&debug_mutex_lock, flags);
347 list_for_each_safe(curr, next, &debug_mutex_held_locks) {
348 lock = list_entry(curr, struct mutex, held_list);
350 if (lock_addr < from || lock_addr >= to)
354 debug_spin_lock_restore(&debug_mutex_lock, flags);
356 printk("BUG: %s/%d, active lock [%p(%p-%p)] freed!\n",
357 current->comm, current->pid, lock, from, to);
359 printk_lock(lock, 1);
360 if (lock->owner != current_thread_info())
361 printk("freeing task is not even the owner??\n");
364 debug_spin_lock_restore(&debug_mutex_lock, flags);
368 * Must be called with lock->wait_lock held.
370 void debug_mutex_set_owner(struct mutex *lock,
371 struct thread_info *new_owner __IP_DECL__)
373 lock->owner = new_owner;
374 DEBUG_WARN_ON(!list_empty(&lock->held_list));
375 if (debug_mutex_on) {
376 list_add_tail(&lock->held_list, &debug_mutex_held_locks);
377 lock->acquire_ip = ip;
381 void debug_mutex_init_waiter(struct mutex_waiter *waiter)
383 memset(waiter, 0x11, sizeof(*waiter));
384 waiter->magic = waiter;
385 INIT_LIST_HEAD(&waiter->list);
388 void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter)
390 SMP_DEBUG_WARN_ON(!spin_is_locked(&lock->wait_lock));
391 DEBUG_WARN_ON(list_empty(&lock->wait_list));
392 DEBUG_WARN_ON(waiter->magic != waiter);
393 DEBUG_WARN_ON(list_empty(&waiter->list));
396 void debug_mutex_free_waiter(struct mutex_waiter *waiter)
398 DEBUG_WARN_ON(!list_empty(&waiter->list));
399 memset(waiter, 0x22, sizeof(*waiter));
402 void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
403 struct thread_info *ti __IP_DECL__)
405 SMP_DEBUG_WARN_ON(!spin_is_locked(&lock->wait_lock));
406 check_deadlock(lock, 0, ti, ip);
407 /* Mark the current thread as blocked on the lock: */
408 ti->task->blocked_on = waiter;
412 void mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
413 struct thread_info *ti)
415 DEBUG_WARN_ON(list_empty(&waiter->list));
416 DEBUG_WARN_ON(waiter->task != ti->task);
417 DEBUG_WARN_ON(ti->task->blocked_on != waiter);
418 ti->task->blocked_on = NULL;
420 list_del_init(&waiter->list);
424 void debug_mutex_unlock(struct mutex *lock)
426 DEBUG_WARN_ON(lock->magic != lock);
427 DEBUG_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
428 DEBUG_WARN_ON(lock->owner != current_thread_info());
429 if (debug_mutex_on) {
430 DEBUG_WARN_ON(list_empty(&lock->held_list));
431 list_del_init(&lock->held_list);
435 void debug_mutex_init(struct mutex *lock, const char *name)
438 * Make sure we are not reinitializing a held lock:
440 mutex_debug_check_no_locks_freed((void *)lock, (void *)(lock + 1));
442 INIT_LIST_HEAD(&lock->held_list);
448 * mutex_destroy - mark a mutex unusable
449 * @lock: the mutex to be destroyed
451 * This function marks the mutex uninitialized, and any subsequent
452 * use of the mutex is forbidden. The mutex must not be locked when
453 * this function is called.
455 void fastcall mutex_destroy(struct mutex *lock)
457 DEBUG_WARN_ON(mutex_is_locked(lock));
461 EXPORT_SYMBOL_GPL(mutex_destroy);