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/sched/rt.h>
21 #include <linux/sched/debug.h>
22 #include <linux/delay.h>
23 #include <linux/export.h>
24 #include <linux/spinlock.h>
25 #include <linux/kallsyms.h>
26 #include <linux/syscalls.h>
27 #include <linux/interrupt.h>
28 #include <linux/rbtree.h>
30 #include <linux/debug_locks.h>
32 #include "rtmutex_common.h"
34 static void printk_task(struct task_struct
*p
)
37 printk("%16s:%5d [%p, %3d]", p
->comm
, task_pid_nr(p
), p
, p
->prio
);
42 static void printk_lock(struct rt_mutex
*lock
, int print_owner
)
45 printk(" [%p] {%s}\n",
48 printk(" [%p] {%s:%d}\n",
49 lock
, lock
->file
, lock
->line
);
51 if (print_owner
&& rt_mutex_owner(lock
)) {
52 printk(".. ->owner: %p\n", lock
->owner
);
53 printk(".. held by: ");
54 printk_task(rt_mutex_owner(lock
));
59 void rt_mutex_debug_task_free(struct task_struct
*task
)
61 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task
->pi_waiters
));
62 DEBUG_LOCKS_WARN_ON(task
->pi_blocked_on
);
66 * We fill out the fields in the waiter to store the information about
67 * the deadlock. We print when we return. act_waiter can be NULL in
68 * case of a remove waiter operation.
70 void debug_rt_mutex_deadlock(enum rtmutex_chainwalk chwalk
,
71 struct rt_mutex_waiter
*act_waiter
,
72 struct rt_mutex
*lock
)
74 struct task_struct
*task
;
76 if (!debug_locks
|| chwalk
== RT_MUTEX_FULL_CHAINWALK
|| !act_waiter
)
79 task
= rt_mutex_owner(act_waiter
->lock
);
80 if (task
&& task
!= current
) {
81 act_waiter
->deadlock_task_pid
= get_pid(task_pid(task
));
82 act_waiter
->deadlock_lock
= lock
;
86 void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter
*waiter
)
88 struct task_struct
*task
;
90 if (!waiter
->deadlock_lock
|| !debug_locks
)
94 task
= pid_task(waiter
->deadlock_task_pid
, PIDTYPE_PID
);
100 if (!debug_locks_off()) {
105 printk("\n============================================\n");
106 printk( "[ BUG: circular locking deadlock detected! ]\n");
107 printk("%s\n", print_tainted());
108 printk( "--------------------------------------------\n");
109 printk("%s/%d is deadlocking current task %s/%d\n\n",
110 task
->comm
, task_pid_nr(task
),
111 current
->comm
, task_pid_nr(current
));
113 printk("\n1) %s/%d is trying to acquire this lock:\n",
114 current
->comm
, task_pid_nr(current
));
115 printk_lock(waiter
->lock
, 1);
117 printk("\n2) %s/%d is blocked on this lock:\n",
118 task
->comm
, task_pid_nr(task
));
119 printk_lock(waiter
->deadlock_lock
, 1);
121 debug_show_held_locks(current
);
122 debug_show_held_locks(task
);
124 printk("\n%s/%d's [blocked] stackdump:\n\n",
125 task
->comm
, task_pid_nr(task
));
126 show_stack(task
, NULL
);
127 printk("\n%s/%d's [current] stackdump:\n\n",
128 current
->comm
, task_pid_nr(current
));
130 debug_show_all_locks();
133 printk("[ turning off deadlock detection."
134 "Please report this trace. ]\n\n");
137 void debug_rt_mutex_lock(struct rt_mutex
*lock
)
141 void debug_rt_mutex_unlock(struct rt_mutex
*lock
)
143 DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock
) != current
);
147 debug_rt_mutex_proxy_lock(struct rt_mutex
*lock
, struct task_struct
*powner
)
151 void debug_rt_mutex_proxy_unlock(struct rt_mutex
*lock
)
153 DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock
));
156 void debug_rt_mutex_init_waiter(struct rt_mutex_waiter
*waiter
)
158 memset(waiter
, 0x11, sizeof(*waiter
));
159 waiter
->deadlock_task_pid
= NULL
;
162 void debug_rt_mutex_free_waiter(struct rt_mutex_waiter
*waiter
)
164 put_pid(waiter
->deadlock_task_pid
);
165 memset(waiter
, 0x22, sizeof(*waiter
));
168 void debug_rt_mutex_init(struct rt_mutex
*lock
, const char *name
)
171 * Make sure we are not reinitializing a held lock:
173 debug_check_no_locks_freed((void *)lock
, sizeof(*lock
));
178 rt_mutex_deadlock_account_lock(struct rt_mutex
*lock
, struct task_struct
*task
)
182 void rt_mutex_deadlock_account_unlock(struct task_struct
*task
)