| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * RT-Mutexes: blocking mutual exclusion locks with PI support | 
|  | 3 | * | 
|  | 4 | * started by Ingo Molnar and Thomas Gleixner: | 
|  | 5 | * | 
|  | 6 | *  Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | 
|  | 7 | *  Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com> | 
|  | 8 | * | 
|  | 9 | * This code is based on the rt.c implementation in the preempt-rt tree. | 
|  | 10 | * Portions of said code are | 
|  | 11 | * | 
|  | 12 | *  Copyright (C) 2004  LynuxWorks, Inc., Igor Manyilov, Bill Huey | 
|  | 13 | *  Copyright (C) 2006  Esben Nielsen | 
|  | 14 | *  Copyright (C) 2006  Kihon Technologies Inc., | 
|  | 15 | *			Steven Rostedt <rostedt@goodmis.org> | 
|  | 16 | * | 
|  | 17 | * See rt.c in preempt-rt for proper credits and further information | 
|  | 18 | */ | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 19 | #include <linux/sched.h> | 
|  | 20 | #include <linux/delay.h> | 
|  | 21 | #include <linux/module.h> | 
|  | 22 | #include <linux/spinlock.h> | 
|  | 23 | #include <linux/kallsyms.h> | 
|  | 24 | #include <linux/syscalls.h> | 
|  | 25 | #include <linux/interrupt.h> | 
|  | 26 | #include <linux/plist.h> | 
|  | 27 | #include <linux/fs.h> | 
| Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 28 | #include <linux/debug_locks.h> | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 29 |  | 
|  | 30 | #include "rtmutex_common.h" | 
|  | 31 |  | 
|  | 32 | #ifdef CONFIG_DEBUG_RT_MUTEXES | 
|  | 33 | # include "rtmutex-debug.h" | 
|  | 34 | #else | 
|  | 35 | # include "rtmutex.h" | 
|  | 36 | #endif | 
|  | 37 |  | 
|  | 38 | # define TRACE_WARN_ON(x)			WARN_ON(x) | 
|  | 39 | # define TRACE_BUG_ON(x)			BUG_ON(x) | 
|  | 40 |  | 
|  | 41 | # define TRACE_OFF()						\ | 
|  | 42 | do {								\ | 
|  | 43 | if (rt_trace_on) {					\ | 
|  | 44 | rt_trace_on = 0;				\ | 
|  | 45 | console_verbose();				\ | 
|  | 46 | if (spin_is_locked(¤t->pi_lock))		\ | 
|  | 47 | spin_unlock(¤t->pi_lock);		\ | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 48 | }							\ | 
|  | 49 | } while (0) | 
|  | 50 |  | 
|  | 51 | # define TRACE_OFF_NOLOCK()					\ | 
|  | 52 | do {								\ | 
|  | 53 | if (rt_trace_on) {					\ | 
|  | 54 | rt_trace_on = 0;				\ | 
|  | 55 | console_verbose();				\ | 
|  | 56 | }							\ | 
|  | 57 | } while (0) | 
|  | 58 |  | 
|  | 59 | # define TRACE_BUG_LOCKED()			\ | 
|  | 60 | do {						\ | 
|  | 61 | TRACE_OFF();				\ | 
|  | 62 | BUG();					\ | 
|  | 63 | } while (0) | 
|  | 64 |  | 
|  | 65 | # define TRACE_WARN_ON_LOCKED(c)		\ | 
|  | 66 | do {						\ | 
|  | 67 | if (unlikely(c)) {			\ | 
|  | 68 | TRACE_OFF();			\ | 
|  | 69 | WARN_ON(1);			\ | 
|  | 70 | }					\ | 
|  | 71 | } while (0) | 
|  | 72 |  | 
|  | 73 | # define TRACE_BUG_ON_LOCKED(c)			\ | 
|  | 74 | do {						\ | 
|  | 75 | if (unlikely(c))			\ | 
|  | 76 | TRACE_BUG_LOCKED();		\ | 
|  | 77 | } while (0) | 
|  | 78 |  | 
|  | 79 | #ifdef CONFIG_SMP | 
|  | 80 | # define SMP_TRACE_BUG_ON_LOCKED(c)	TRACE_BUG_ON_LOCKED(c) | 
|  | 81 | #else | 
|  | 82 | # define SMP_TRACE_BUG_ON_LOCKED(c)	do { } while (0) | 
|  | 83 | #endif | 
|  | 84 |  | 
|  | 85 | /* | 
|  | 86 | * deadlock detection flag. We turn it off when we detect | 
|  | 87 | * the first problem because we dont want to recurse back | 
|  | 88 | * into the tracing code when doing error printk or | 
|  | 89 | * executing a BUG(): | 
|  | 90 | */ | 
|  | 91 | int rt_trace_on = 1; | 
|  | 92 |  | 
|  | 93 | void deadlock_trace_off(void) | 
|  | 94 | { | 
|  | 95 | rt_trace_on = 0; | 
|  | 96 | } | 
|  | 97 |  | 
| Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 98 | static void printk_task(struct task_struct *p) | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 99 | { | 
|  | 100 | if (p) | 
|  | 101 | printk("%16s:%5d [%p, %3d]", p->comm, p->pid, p, p->prio); | 
|  | 102 | else | 
|  | 103 | printk("<none>"); | 
|  | 104 | } | 
|  | 105 |  | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 106 | static void printk_lock(struct rt_mutex *lock, int print_owner) | 
|  | 107 | { | 
|  | 108 | if (lock->name) | 
|  | 109 | printk(" [%p] {%s}\n", | 
|  | 110 | lock, lock->name); | 
|  | 111 | else | 
|  | 112 | printk(" [%p] {%s:%d}\n", | 
|  | 113 | lock, lock->file, lock->line); | 
|  | 114 |  | 
|  | 115 | if (print_owner && rt_mutex_owner(lock)) { | 
|  | 116 | printk(".. ->owner: %p\n", lock->owner); | 
|  | 117 | printk(".. held by:  "); | 
|  | 118 | printk_task(rt_mutex_owner(lock)); | 
|  | 119 | printk("\n"); | 
|  | 120 | } | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
|  | 123 | void rt_mutex_debug_task_free(struct task_struct *task) | 
|  | 124 | { | 
|  | 125 | WARN_ON(!plist_head_empty(&task->pi_waiters)); | 
|  | 126 | WARN_ON(task->pi_blocked_on); | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | /* | 
|  | 130 | * We fill out the fields in the waiter to store the information about | 
|  | 131 | * the deadlock. We print when we return. act_waiter can be NULL in | 
|  | 132 | * case of a remove waiter operation. | 
|  | 133 | */ | 
|  | 134 | void debug_rt_mutex_deadlock(int detect, struct rt_mutex_waiter *act_waiter, | 
|  | 135 | struct rt_mutex *lock) | 
|  | 136 | { | 
|  | 137 | struct task_struct *task; | 
|  | 138 |  | 
|  | 139 | if (!rt_trace_on || detect || !act_waiter) | 
|  | 140 | return; | 
|  | 141 |  | 
|  | 142 | task = rt_mutex_owner(act_waiter->lock); | 
|  | 143 | if (task && task != current) { | 
|  | 144 | act_waiter->deadlock_task_pid = task->pid; | 
|  | 145 | act_waiter->deadlock_lock = lock; | 
|  | 146 | } | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter) | 
|  | 150 | { | 
|  | 151 | struct task_struct *task; | 
|  | 152 |  | 
|  | 153 | if (!waiter->deadlock_lock || !rt_trace_on) | 
|  | 154 | return; | 
|  | 155 |  | 
|  | 156 | task = find_task_by_pid(waiter->deadlock_task_pid); | 
|  | 157 | if (!task) | 
|  | 158 | return; | 
|  | 159 |  | 
|  | 160 | TRACE_OFF_NOLOCK(); | 
|  | 161 |  | 
|  | 162 | printk("\n============================================\n"); | 
|  | 163 | printk(  "[ BUG: circular locking deadlock detected! ]\n"); | 
|  | 164 | printk(  "--------------------------------------------\n"); | 
|  | 165 | printk("%s/%d is deadlocking current task %s/%d\n\n", | 
|  | 166 | task->comm, task->pid, current->comm, current->pid); | 
|  | 167 |  | 
|  | 168 | printk("\n1) %s/%d is trying to acquire this lock:\n", | 
|  | 169 | current->comm, current->pid); | 
|  | 170 | printk_lock(waiter->lock, 1); | 
|  | 171 |  | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 172 | printk("\n2) %s/%d is blocked on this lock:\n", task->comm, task->pid); | 
|  | 173 | printk_lock(waiter->deadlock_lock, 1); | 
|  | 174 |  | 
| Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 175 | debug_show_held_locks(current); | 
|  | 176 | debug_show_held_locks(task); | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | printk("\n%s/%d's [blocked] stackdump:\n\n", task->comm, task->pid); | 
|  | 179 | show_stack(task, NULL); | 
|  | 180 | printk("\n%s/%d's [current] stackdump:\n\n", | 
|  | 181 | current->comm, current->pid); | 
|  | 182 | dump_stack(); | 
| Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 183 | debug_show_all_locks(); | 
|  | 184 |  | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 185 | printk("[ turning off deadlock detection." | 
|  | 186 | "Please report this trace. ]\n\n"); | 
|  | 187 | local_irq_disable(); | 
|  | 188 | } | 
|  | 189 |  | 
| Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 190 | void debug_rt_mutex_lock(struct rt_mutex *lock) | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 191 | { | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
|  | 194 | void debug_rt_mutex_unlock(struct rt_mutex *lock) | 
|  | 195 | { | 
| Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 196 | TRACE_WARN_ON_LOCKED(rt_mutex_owner(lock) != current); | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 199 | void | 
|  | 200 | debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner) | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 201 | { | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
|  | 204 | void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock) | 
|  | 205 | { | 
| Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 206 | TRACE_WARN_ON_LOCKED(!rt_mutex_owner(lock)); | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
|  | 209 | void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter) | 
|  | 210 | { | 
|  | 211 | memset(waiter, 0x11, sizeof(*waiter)); | 
|  | 212 | plist_node_init(&waiter->list_entry, MAX_PRIO); | 
|  | 213 | plist_node_init(&waiter->pi_list_entry, MAX_PRIO); | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 | void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter) | 
|  | 217 | { | 
|  | 218 | TRACE_WARN_ON(!plist_node_empty(&waiter->list_entry)); | 
|  | 219 | TRACE_WARN_ON(!plist_node_empty(&waiter->pi_list_entry)); | 
|  | 220 | TRACE_WARN_ON(waiter->task); | 
|  | 221 | memset(waiter, 0x22, sizeof(*waiter)); | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | void debug_rt_mutex_init(struct rt_mutex *lock, const char *name) | 
|  | 225 | { | 
| Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 226 | /* | 
|  | 227 | * Make sure we are not reinitializing a held lock: | 
|  | 228 | */ | 
|  | 229 | debug_check_no_locks_freed((void *)lock, sizeof(*lock)); | 
|  | 230 | lock->name = name; | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 231 | } | 
|  | 232 |  | 
| Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 233 | void | 
|  | 234 | rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task) | 
| Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 235 | { | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | void rt_mutex_deadlock_account_unlock(struct task_struct *task) | 
|  | 239 | { | 
|  | 240 | } | 
|  | 241 |  |