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