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 | |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 32 | # define TRACE_WARN_ON(x) WARN_ON(x) |
| 33 | # define TRACE_BUG_ON(x) BUG_ON(x) |
| 34 | |
| 35 | # define TRACE_OFF() \ |
| 36 | do { \ |
| 37 | if (rt_trace_on) { \ |
| 38 | rt_trace_on = 0; \ |
| 39 | console_verbose(); \ |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 40 | if (raw_spin_is_locked(¤t->pi_lock)) \ |
| 41 | raw_spin_unlock(¤t->pi_lock); \ |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 42 | } \ |
| 43 | } while (0) |
| 44 | |
| 45 | # define TRACE_OFF_NOLOCK() \ |
| 46 | do { \ |
| 47 | if (rt_trace_on) { \ |
| 48 | rt_trace_on = 0; \ |
| 49 | console_verbose(); \ |
| 50 | } \ |
| 51 | } while (0) |
| 52 | |
| 53 | # define TRACE_BUG_LOCKED() \ |
| 54 | do { \ |
| 55 | TRACE_OFF(); \ |
| 56 | BUG(); \ |
| 57 | } while (0) |
| 58 | |
| 59 | # define TRACE_WARN_ON_LOCKED(c) \ |
| 60 | do { \ |
| 61 | if (unlikely(c)) { \ |
| 62 | TRACE_OFF(); \ |
| 63 | WARN_ON(1); \ |
| 64 | } \ |
| 65 | } while (0) |
| 66 | |
| 67 | # define TRACE_BUG_ON_LOCKED(c) \ |
| 68 | do { \ |
| 69 | if (unlikely(c)) \ |
| 70 | TRACE_BUG_LOCKED(); \ |
| 71 | } while (0) |
| 72 | |
| 73 | #ifdef CONFIG_SMP |
| 74 | # define SMP_TRACE_BUG_ON_LOCKED(c) TRACE_BUG_ON_LOCKED(c) |
| 75 | #else |
| 76 | # define SMP_TRACE_BUG_ON_LOCKED(c) do { } while (0) |
| 77 | #endif |
| 78 | |
| 79 | /* |
| 80 | * deadlock detection flag. We turn it off when we detect |
| 81 | * the first problem because we dont want to recurse back |
| 82 | * into the tracing code when doing error printk or |
| 83 | * executing a BUG(): |
| 84 | */ |
Adrian Bunk | a4d63e7 | 2007-10-16 23:26:40 -0700 | [diff] [blame] | 85 | static int rt_trace_on = 1; |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 86 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 87 | static void printk_task(struct task_struct *p) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 88 | { |
| 89 | if (p) |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 90 | printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 91 | else |
| 92 | printk("<none>"); |
| 93 | } |
| 94 | |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 95 | static void printk_lock(struct rt_mutex *lock, int print_owner) |
| 96 | { |
| 97 | if (lock->name) |
| 98 | printk(" [%p] {%s}\n", |
| 99 | lock, lock->name); |
| 100 | else |
| 101 | printk(" [%p] {%s:%d}\n", |
| 102 | lock, lock->file, lock->line); |
| 103 | |
| 104 | if (print_owner && rt_mutex_owner(lock)) { |
| 105 | printk(".. ->owner: %p\n", lock->owner); |
| 106 | printk(".. held by: "); |
| 107 | printk_task(rt_mutex_owner(lock)); |
| 108 | printk("\n"); |
| 109 | } |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void rt_mutex_debug_task_free(struct task_struct *task) |
| 113 | { |
| 114 | WARN_ON(!plist_head_empty(&task->pi_waiters)); |
| 115 | WARN_ON(task->pi_blocked_on); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * We fill out the fields in the waiter to store the information about |
| 120 | * the deadlock. We print when we return. act_waiter can be NULL in |
| 121 | * case of a remove waiter operation. |
| 122 | */ |
| 123 | void debug_rt_mutex_deadlock(int detect, struct rt_mutex_waiter *act_waiter, |
| 124 | struct rt_mutex *lock) |
| 125 | { |
| 126 | struct task_struct *task; |
| 127 | |
| 128 | if (!rt_trace_on || detect || !act_waiter) |
| 129 | return; |
| 130 | |
| 131 | task = rt_mutex_owner(act_waiter->lock); |
| 132 | if (task && task != current) { |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 133 | act_waiter->deadlock_task_pid = get_pid(task_pid(task)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 134 | act_waiter->deadlock_lock = lock; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter) |
| 139 | { |
| 140 | struct task_struct *task; |
| 141 | |
| 142 | if (!waiter->deadlock_lock || !rt_trace_on) |
| 143 | return; |
| 144 | |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 145 | rcu_read_lock(); |
| 146 | task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID); |
| 147 | if (!task) { |
| 148 | rcu_read_unlock(); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 149 | return; |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 150 | } |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 151 | |
| 152 | TRACE_OFF_NOLOCK(); |
| 153 | |
| 154 | printk("\n============================================\n"); |
| 155 | printk( "[ BUG: circular locking deadlock detected! ]\n"); |
| 156 | printk( "--------------------------------------------\n"); |
| 157 | printk("%s/%d is deadlocking current task %s/%d\n\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 158 | task->comm, task_pid_nr(task), |
| 159 | current->comm, task_pid_nr(current)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 160 | |
| 161 | printk("\n1) %s/%d is trying to acquire this lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 162 | current->comm, task_pid_nr(current)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 163 | printk_lock(waiter->lock, 1); |
| 164 | |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 165 | printk("\n2) %s/%d is blocked on this lock:\n", |
| 166 | task->comm, task_pid_nr(task)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 167 | printk_lock(waiter->deadlock_lock, 1); |
| 168 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 169 | debug_show_held_locks(current); |
| 170 | debug_show_held_locks(task); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 171 | |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 172 | printk("\n%s/%d's [blocked] stackdump:\n\n", |
| 173 | task->comm, task_pid_nr(task)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 174 | show_stack(task, NULL); |
| 175 | printk("\n%s/%d's [current] stackdump:\n\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 176 | current->comm, task_pid_nr(current)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 177 | dump_stack(); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 178 | debug_show_all_locks(); |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 179 | rcu_read_unlock(); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 180 | |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 181 | printk("[ turning off deadlock detection." |
| 182 | "Please report this trace. ]\n\n"); |
| 183 | local_irq_disable(); |
| 184 | } |
| 185 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 186 | void debug_rt_mutex_lock(struct rt_mutex *lock) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 187 | { |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void debug_rt_mutex_unlock(struct rt_mutex *lock) |
| 191 | { |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 192 | TRACE_WARN_ON_LOCKED(rt_mutex_owner(lock) != current); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 195 | void |
| 196 | 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] | 197 | { |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock) |
| 201 | { |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 202 | TRACE_WARN_ON_LOCKED(!rt_mutex_owner(lock)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter) |
| 206 | { |
| 207 | memset(waiter, 0x11, sizeof(*waiter)); |
| 208 | plist_node_init(&waiter->list_entry, MAX_PRIO); |
| 209 | plist_node_init(&waiter->pi_list_entry, MAX_PRIO); |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 210 | waiter->deadlock_task_pid = NULL; |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter) |
| 214 | { |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 215 | put_pid(waiter->deadlock_task_pid); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 216 | TRACE_WARN_ON(!plist_node_empty(&waiter->list_entry)); |
| 217 | TRACE_WARN_ON(!plist_node_empty(&waiter->pi_list_entry)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 218 | memset(waiter, 0x22, sizeof(*waiter)); |
| 219 | } |
| 220 | |
| 221 | void debug_rt_mutex_init(struct rt_mutex *lock, const char *name) |
| 222 | { |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 223 | /* |
| 224 | * Make sure we are not reinitializing a held lock: |
| 225 | */ |
| 226 | debug_check_no_locks_freed((void *)lock, sizeof(*lock)); |
| 227 | lock->name = name; |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 230 | void |
| 231 | 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] | 232 | { |
| 233 | } |
| 234 | |
| 235 | void rt_mutex_deadlock_account_unlock(struct task_struct *task) |
| 236 | { |
| 237 | } |
| 238 | |