Now it works.
[cbs-scheduler.git] / kernel / rtmutex-debug.c
blob51a59218d30804d181e66e45c3d80209a619764c
1 /*
2 * RT-Mutexes: blocking mutual exclusion locks with PI support
4 * started by Ingo Molnar and Thomas Gleixner:
6 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
7 * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
9 * This code is based on the rt.c implementation in the preempt-rt tree.
10 * Portions of said code are
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>
17 * See rt.c in preempt-rt for proper credits and further information
19 #include <linux/rt_lock.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>
31 #include "rtmutex_common.h"
33 static void printk_task(struct task_struct *p)
35 if (p)
36 printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio);
37 else
38 printk("<none>");
41 static void printk_lock(struct rt_mutex *lock, int print_owner)
43 if (lock->name)
44 printk(" [%p] {%s}\n",
45 lock, lock->name);
46 else
47 printk(" [%p] {%s:%d}\n",
48 lock, lock->file, lock->line);
50 if (print_owner && rt_mutex_owner(lock)) {
51 printk(".. ->owner: %p\n", lock->owner);
52 printk(".. held by: ");
53 printk_task(rt_mutex_owner(lock));
54 printk("\n");
58 void rt_mutex_debug_task_free(struct task_struct *task)
60 DEBUG_LOCKS_WARN_ON(!plist_head_empty(&task->pi_waiters));
61 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
65 * We fill out the fields in the waiter to store the information about
66 * the deadlock. We print when we return. act_waiter can be NULL in
67 * case of a remove waiter operation.
69 void debug_rt_mutex_deadlock(int detect, struct rt_mutex_waiter *act_waiter,
70 struct rt_mutex *lock)
72 struct task_struct *task;
74 if (!debug_locks || detect || !act_waiter)
75 return;
77 task = rt_mutex_owner(act_waiter->lock);
78 if (task && task != current) {
79 act_waiter->deadlock_task_pid = get_pid(task_pid(task));
80 act_waiter->deadlock_lock = lock;
84 void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
86 struct task_struct *task;
88 if (!waiter->deadlock_lock || !debug_locks)
89 return;
91 rcu_read_lock();
92 task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
93 if (!task) {
94 rcu_read_unlock();
95 return;
98 if (!debug_locks_off())
99 return;
101 printk("\n============================================\n");
102 printk( "[ BUG: circular locking deadlock detected! ]\n");
103 printk( "--------------------------------------------\n");
104 printk("%s/%d is deadlocking current task %s/%d\n\n",
105 task->comm, task_pid_nr(task),
106 current->comm, task_pid_nr(current));
108 printk("\n1) %s/%d is trying to acquire this lock:\n",
109 current->comm, task_pid_nr(current));
110 printk_lock(waiter->lock, 1);
112 printk("\n2) %s/%d is blocked on this lock:\n",
113 task->comm, task_pid_nr(task));
114 printk_lock(waiter->deadlock_lock, 1);
116 debug_show_held_locks(current);
117 debug_show_held_locks(task);
119 printk("\n%s/%d's [blocked] stackdump:\n\n",
120 task->comm, task_pid_nr(task));
121 show_stack(task, NULL);
122 printk("\n%s/%d's [current] stackdump:\n\n",
123 current->comm, task_pid_nr(current));
124 dump_stack();
125 debug_show_all_locks();
126 rcu_read_unlock();
128 printk("[ turning off deadlock detection."
129 "Please report this trace. ]\n\n");
132 void debug_rt_mutex_lock(struct rt_mutex *lock)
136 void debug_rt_mutex_unlock(struct rt_mutex *lock)
138 if (debug_locks)
139 DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
142 void
143 debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
147 void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
149 DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
152 void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
154 memset(waiter, 0x11, sizeof(*waiter));
155 plist_node_init(&waiter->list_entry, MAX_PRIO);
156 plist_node_init(&waiter->pi_list_entry, MAX_PRIO);
157 waiter->deadlock_task_pid = NULL;
160 void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
162 put_pid(waiter->deadlock_task_pid);
163 DEBUG_LOCKS_WARN_ON(!plist_node_empty(&waiter->list_entry));
164 DEBUG_LOCKS_WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
165 DEBUG_LOCKS_WARN_ON(waiter->task);
166 memset(waiter, 0x22, sizeof(*waiter));
169 void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
172 * Make sure we are not reinitializing a held lock:
174 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
175 lock->name = name;
178 void
179 rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task)
181 #ifdef CONFIG_DEBUG_PREEMPT
182 if (atomic_read(&task->lock_count) >= MAX_LOCK_STACK) {
183 if (!debug_locks_off())
184 return;
185 printk("BUG: %s/%d: lock count overflow!\n",
186 task->comm, task->pid);
187 dump_stack();
188 return;
190 #ifdef CONFIG_PREEMPT_RT
191 task->owned_lock[atomic_read(&task->lock_count)] = lock;
192 #endif
193 atomic_inc(&task->lock_count);
194 #endif
197 void rt_mutex_deadlock_account_unlock(struct task_struct *task)
199 #ifdef CONFIG_DEBUG_PREEMPT
200 if (!atomic_read(&task->lock_count)) {
201 if (!debug_locks_off())
202 return;
203 printk("BUG: %s/%d: lock count underflow!\n",
204 task->comm, task->pid);
205 dump_stack();
206 return;
208 atomic_dec(&task->lock_count);
209 #ifdef CONFIG_PREEMPT_RT
210 task->owned_lock[atomic_read(&task->lock_count)] = NULL;
211 #endif
212 #endif