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/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>
28 #include <linux/debug_locks.h>
30 #include "rtmutex_common.h"
32 # define TRACE_WARN_ON(x) WARN_ON(x)
33 # define TRACE_BUG_ON(x) BUG_ON(x)
35 # define TRACE_OFF() \
40 if (spin_is_locked(¤t->pi_lock)) \
41 spin_unlock(¤t->pi_lock); \
45 # define TRACE_OFF_NOLOCK() \
53 # define TRACE_BUG_LOCKED() \
59 # define TRACE_WARN_ON_LOCKED(c) \
67 # define TRACE_BUG_ON_LOCKED(c) \
74 # define SMP_TRACE_BUG_ON_LOCKED(c) TRACE_BUG_ON_LOCKED(c)
76 # define SMP_TRACE_BUG_ON_LOCKED(c) do { } while (0)
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
85 static int rt_trace_on
= 1;
87 static void printk_task(struct task_struct
*p
)
90 printk("%16s:%5d [%p, %3d]", p
->comm
, task_pid_nr(p
), p
, p
->prio
);
95 static void printk_lock(struct rt_mutex
*lock
, int print_owner
)
98 printk(" [%p] {%s}\n",
101 printk(" [%p] {%s:%d}\n",
102 lock
, lock
->file
, lock
->line
);
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
));
112 void rt_mutex_debug_task_free(struct task_struct
*task
)
114 WARN_ON(!plist_head_empty(&task
->pi_waiters
));
115 WARN_ON(task
->pi_blocked_on
);
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.
123 void debug_rt_mutex_deadlock(int detect
, struct rt_mutex_waiter
*act_waiter
,
124 struct rt_mutex
*lock
)
126 struct task_struct
*task
;
128 if (!rt_trace_on
|| detect
|| !act_waiter
)
131 task
= rt_mutex_owner(act_waiter
->lock
);
132 if (task
&& task
!= current
) {
133 act_waiter
->deadlock_task_pid
= task
->pid
;
134 act_waiter
->deadlock_lock
= lock
;
138 void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter
*waiter
)
140 struct task_struct
*task
;
142 if (!waiter
->deadlock_lock
|| !rt_trace_on
)
145 task
= find_task_by_pid(waiter
->deadlock_task_pid
);
151 printk("\n============================================\n");
152 printk( "[ BUG: circular locking deadlock detected! ]\n");
153 printk( "--------------------------------------------\n");
154 printk("%s/%d is deadlocking current task %s/%d\n\n",
155 task
->comm
, task_pid_nr(task
),
156 current
->comm
, task_pid_nr(current
));
158 printk("\n1) %s/%d is trying to acquire this lock:\n",
159 current
->comm
, task_pid_nr(current
));
160 printk_lock(waiter
->lock
, 1);
162 printk("\n2) %s/%d is blocked on this lock:\n",
163 task
->comm
, task_pid_nr(task
));
164 printk_lock(waiter
->deadlock_lock
, 1);
166 debug_show_held_locks(current
);
167 debug_show_held_locks(task
);
169 printk("\n%s/%d's [blocked] stackdump:\n\n",
170 task
->comm
, task_pid_nr(task
));
171 show_stack(task
, NULL
);
172 printk("\n%s/%d's [current] stackdump:\n\n",
173 current
->comm
, task_pid_nr(current
));
175 debug_show_all_locks();
177 printk("[ turning off deadlock detection."
178 "Please report this trace. ]\n\n");
182 void debug_rt_mutex_lock(struct rt_mutex
*lock
)
186 void debug_rt_mutex_unlock(struct rt_mutex
*lock
)
188 TRACE_WARN_ON_LOCKED(rt_mutex_owner(lock
) != current
);
192 debug_rt_mutex_proxy_lock(struct rt_mutex
*lock
, struct task_struct
*powner
)
196 void debug_rt_mutex_proxy_unlock(struct rt_mutex
*lock
)
198 TRACE_WARN_ON_LOCKED(!rt_mutex_owner(lock
));
201 void debug_rt_mutex_init_waiter(struct rt_mutex_waiter
*waiter
)
203 memset(waiter
, 0x11, sizeof(*waiter
));
204 plist_node_init(&waiter
->list_entry
, MAX_PRIO
);
205 plist_node_init(&waiter
->pi_list_entry
, MAX_PRIO
);
208 void debug_rt_mutex_free_waiter(struct rt_mutex_waiter
*waiter
)
210 TRACE_WARN_ON(!plist_node_empty(&waiter
->list_entry
));
211 TRACE_WARN_ON(!plist_node_empty(&waiter
->pi_list_entry
));
212 TRACE_WARN_ON(waiter
->task
);
213 memset(waiter
, 0x22, sizeof(*waiter
));
216 void debug_rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
219 * Make sure we are not reinitializing a held lock:
221 debug_check_no_locks_freed((void *)lock
, sizeof(*lock
));
226 rt_mutex_deadlock_account_lock(struct rt_mutex
*lock
, struct task_struct
*task
)
230 void rt_mutex_deadlock_account_unlock(struct task_struct
*task
)