Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 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> |
Randy Dunlap | a7807a3 | 2006-06-27 02:53:54 -0700 | [diff] [blame] | 19 | #include <linux/poison.h> |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/kallsyms.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 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 | |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 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; |
Ingo Molnar | 9e7f4d4 | 2006-07-03 00:24:30 -0700 | [diff] [blame] | 61 | DEBUG_LOCKS_WARN_ON(!list_empty(&lock->held_list)); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 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 | { |
Randy Dunlap | a7807a3 | 2006-06-27 02:53:54 -0700 | [diff] [blame] | 70 | memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter)); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 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 | { |
Ingo Molnar | 9e7f4d4 | 2006-07-03 00:24:30 -0700 | [diff] [blame] | 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)); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void debug_mutex_free_waiter(struct mutex_waiter *waiter) |
| 84 | { |
Ingo Molnar | 9e7f4d4 | 2006-07-03 00:24:30 -0700 | [diff] [blame] | 85 | DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list)); |
Randy Dunlap | a7807a3 | 2006-06-27 02:53:54 -0700 | [diff] [blame] | 86 | memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter)); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, |
| 90 | struct thread_info *ti __IP_DECL__) |
| 91 | { |
Ingo Molnar | 9e7f4d4 | 2006-07-03 00:24:30 -0700 | [diff] [blame] | 92 | SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock)); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 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 | { |
Ingo Molnar | 9e7f4d4 | 2006-07-03 00:24:30 -0700 | [diff] [blame] | 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); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 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 | { |
Ingo Molnar | 9e7f4d4 | 2006-07-03 00:24:30 -0700 | [diff] [blame] | 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()); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 115 | if (debug_mutex_on) { |
Ingo Molnar | 9e7f4d4 | 2006-07-03 00:24:30 -0700 | [diff] [blame] | 116 | DEBUG_LOCKS_WARN_ON(list_empty(&lock->held_list)); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 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 | */ |
David Woodhouse | a4fc7ab | 2006-01-11 14:41:26 +0000 | [diff] [blame] | 126 | mutex_debug_check_no_locks_freed((void *)lock, sizeof(*lock)); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 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 | { |
Ingo Molnar | 9e7f4d4 | 2006-07-03 00:24:30 -0700 | [diff] [blame] | 143 | DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock)); |
Ingo Molnar | 408894e | 2006-01-09 15:59:20 -0800 | [diff] [blame] | 144 | lock->magic = NULL; |
| 145 | } |
| 146 | |
| 147 | EXPORT_SYMBOL_GPL(mutex_destroy); |