blob: 5aedbee014dfca7f2ebdce84e62d1c1bac87fab2 [file] [log] [blame]
Ingo Molnare7eebaf2006-06-27 02:54:55 -07001/*
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 Molnare7eebaf2006-06-27 02:54:55 -070019#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 Molnar9a11b49a2006-07-03 00:24:33 -070028#include <linux/debug_locks.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -070029
30#include "rtmutex_common.h"
31
Ingo Molnare7eebaf2006-06-27 02:54:55 -070032# define TRACE_WARN_ON(x) WARN_ON(x)
33# define TRACE_BUG_ON(x) BUG_ON(x)
34
35# define TRACE_OFF() \
36do { \
37 if (rt_trace_on) { \
38 rt_trace_on = 0; \
39 console_verbose(); \
40 if (spin_is_locked(&current->pi_lock)) \
41 spin_unlock(&current->pi_lock); \
Ingo Molnare7eebaf2006-06-27 02:54:55 -070042 } \
43} while (0)
44
45# define TRACE_OFF_NOLOCK() \
46do { \
47 if (rt_trace_on) { \
48 rt_trace_on = 0; \
49 console_verbose(); \
50 } \
51} while (0)
52
53# define TRACE_BUG_LOCKED() \
54do { \
55 TRACE_OFF(); \
56 BUG(); \
57} while (0)
58
59# define TRACE_WARN_ON_LOCKED(c) \
60do { \
61 if (unlikely(c)) { \
62 TRACE_OFF(); \
63 WARN_ON(1); \
64 } \
65} while (0)
66
67# define TRACE_BUG_ON_LOCKED(c) \
68do { \
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 */
85int rt_trace_on = 1;
86
87void deadlock_trace_off(void)
88{
89 rt_trace_on = 0;
90}
91
Ingo Molnar36c8b582006-07-03 00:25:41 -070092static void printk_task(struct task_struct *p)
Ingo Molnare7eebaf2006-06-27 02:54:55 -070093{
94 if (p)
95 printk("%16s:%5d [%p, %3d]", p->comm, p->pid, p, p->prio);
96 else
97 printk("<none>");
98}
99
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700100static void printk_lock(struct rt_mutex *lock, int print_owner)
101{
102 if (lock->name)
103 printk(" [%p] {%s}\n",
104 lock, lock->name);
105 else
106 printk(" [%p] {%s:%d}\n",
107 lock, lock->file, lock->line);
108
109 if (print_owner && rt_mutex_owner(lock)) {
110 printk(".. ->owner: %p\n", lock->owner);
111 printk(".. held by: ");
112 printk_task(rt_mutex_owner(lock));
113 printk("\n");
114 }
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700115}
116
117void rt_mutex_debug_task_free(struct task_struct *task)
118{
119 WARN_ON(!plist_head_empty(&task->pi_waiters));
120 WARN_ON(task->pi_blocked_on);
121}
122
123/*
124 * We fill out the fields in the waiter to store the information about
125 * the deadlock. We print when we return. act_waiter can be NULL in
126 * case of a remove waiter operation.
127 */
128void debug_rt_mutex_deadlock(int detect, struct rt_mutex_waiter *act_waiter,
129 struct rt_mutex *lock)
130{
131 struct task_struct *task;
132
133 if (!rt_trace_on || detect || !act_waiter)
134 return;
135
136 task = rt_mutex_owner(act_waiter->lock);
137 if (task && task != current) {
138 act_waiter->deadlock_task_pid = task->pid;
139 act_waiter->deadlock_lock = lock;
140 }
141}
142
143void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
144{
145 struct task_struct *task;
146
147 if (!waiter->deadlock_lock || !rt_trace_on)
148 return;
149
150 task = find_task_by_pid(waiter->deadlock_task_pid);
151 if (!task)
152 return;
153
154 TRACE_OFF_NOLOCK();
155
156 printk("\n============================================\n");
157 printk( "[ BUG: circular locking deadlock detected! ]\n");
158 printk( "--------------------------------------------\n");
159 printk("%s/%d is deadlocking current task %s/%d\n\n",
160 task->comm, task->pid, current->comm, current->pid);
161
162 printk("\n1) %s/%d is trying to acquire this lock:\n",
163 current->comm, current->pid);
164 printk_lock(waiter->lock, 1);
165
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700166 printk("\n2) %s/%d is blocked on this lock:\n", task->comm, task->pid);
167 printk_lock(waiter->deadlock_lock, 1);
168
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700169 debug_show_held_locks(current);
170 debug_show_held_locks(task);
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700171
172 printk("\n%s/%d's [blocked] stackdump:\n\n", task->comm, task->pid);
173 show_stack(task, NULL);
174 printk("\n%s/%d's [current] stackdump:\n\n",
175 current->comm, current->pid);
176 dump_stack();
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700177 debug_show_all_locks();
178
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700179 printk("[ turning off deadlock detection."
180 "Please report this trace. ]\n\n");
181 local_irq_disable();
182}
183
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700184void debug_rt_mutex_lock(struct rt_mutex *lock)
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700185{
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700186}
187
188void debug_rt_mutex_unlock(struct rt_mutex *lock)
189{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700190 TRACE_WARN_ON_LOCKED(rt_mutex_owner(lock) != current);
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700191}
192
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700193void
194debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700195{
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700196}
197
198void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
199{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700200 TRACE_WARN_ON_LOCKED(!rt_mutex_owner(lock));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700201}
202
203void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
204{
205 memset(waiter, 0x11, sizeof(*waiter));
206 plist_node_init(&waiter->list_entry, MAX_PRIO);
207 plist_node_init(&waiter->pi_list_entry, MAX_PRIO);
208}
209
210void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
211{
212 TRACE_WARN_ON(!plist_node_empty(&waiter->list_entry));
213 TRACE_WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
214 TRACE_WARN_ON(waiter->task);
215 memset(waiter, 0x22, sizeof(*waiter));
216}
217
218void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
219{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700220 /*
221 * Make sure we are not reinitializing a held lock:
222 */
223 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
224 lock->name = name;
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700225}
226
Ingo Molnar36c8b582006-07-03 00:25:41 -0700227void
228rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task)
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700229{
230}
231
232void rt_mutex_deadlock_account_unlock(struct task_struct *task)
233{
234}
235