blob: 62b6cee8ea7f9e7f9dab390011775863af71e597 [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>
Clark Williams8bd75c72013-02-07 09:47:07 -060020#include <linux/sched/rt.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -070021#include <linux/delay.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040022#include <linux/export.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -070023#include <linux/spinlock.h>
24#include <linux/kallsyms.h>
25#include <linux/syscalls.h>
26#include <linux/interrupt.h>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010027#include <linux/rbtree.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -070028#include <linux/fs.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070029#include <linux/debug_locks.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -070030
31#include "rtmutex_common.h"
32
Ingo Molnar36c8b582006-07-03 00:25:41 -070033static void printk_task(struct task_struct *p)
Ingo Molnare7eebaf2006-06-27 02:54:55 -070034{
35 if (p)
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070036 printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio);
Ingo Molnare7eebaf2006-06-27 02:54:55 -070037 else
38 printk("<none>");
39}
40
Ingo Molnare7eebaf2006-06-27 02:54:55 -070041static void printk_lock(struct rt_mutex *lock, int print_owner)
42{
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);
49
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");
55 }
Ingo Molnare7eebaf2006-06-27 02:54:55 -070056}
57
58void rt_mutex_debug_task_free(struct task_struct *task)
59{
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010060 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters));
Thomas Gleixner0fa914c2011-06-08 09:58:38 +020061 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
Ingo Molnare7eebaf2006-06-27 02:54:55 -070062}
63
64/*
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.
68 */
Thomas Gleixner8930ed82014-05-22 03:25:47 +000069void debug_rt_mutex_deadlock(enum rtmutex_chainwalk chwalk,
70 struct rt_mutex_waiter *act_waiter,
Ingo Molnare7eebaf2006-06-27 02:54:55 -070071 struct rt_mutex *lock)
72{
73 struct task_struct *task;
74
Thomas Gleixner8930ed82014-05-22 03:25:47 +000075 if (!debug_locks || chwalk == RT_MUTEX_FULL_CHAINWALK || !act_waiter)
Ingo Molnare7eebaf2006-06-27 02:54:55 -070076 return;
77
78 task = rt_mutex_owner(act_waiter->lock);
79 if (task && task != current) {
Pavel Emelyanov48d13e42008-02-08 04:21:53 -080080 act_waiter->deadlock_task_pid = get_pid(task_pid(task));
Ingo Molnare7eebaf2006-06-27 02:54:55 -070081 act_waiter->deadlock_lock = lock;
82 }
83}
84
85void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
86{
87 struct task_struct *task;
88
Thomas Gleixner0fa914c2011-06-08 09:58:38 +020089 if (!waiter->deadlock_lock || !debug_locks)
Ingo Molnare7eebaf2006-06-27 02:54:55 -070090 return;
91
Pavel Emelyanov48d13e42008-02-08 04:21:53 -080092 rcu_read_lock();
93 task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
94 if (!task) {
95 rcu_read_unlock();
Ingo Molnare7eebaf2006-06-27 02:54:55 -070096 return;
Pavel Emelyanov48d13e42008-02-08 04:21:53 -080097 }
Ingo Molnare7eebaf2006-06-27 02:54:55 -070098
Thomas Gleixner68cc3992011-10-05 13:20:24 +020099 if (!debug_locks_off()) {
100 rcu_read_unlock();
Thomas Gleixner0fa914c2011-06-08 09:58:38 +0200101 return;
Thomas Gleixner68cc3992011-10-05 13:20:24 +0200102 }
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700103
104 printk("\n============================================\n");
105 printk( "[ BUG: circular locking deadlock detected! ]\n");
Ben Hutchingsfbdc4b92011-10-28 04:36:55 +0100106 printk("%s\n", print_tainted());
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700107 printk( "--------------------------------------------\n");
108 printk("%s/%d is deadlocking current task %s/%d\n\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700109 task->comm, task_pid_nr(task),
110 current->comm, task_pid_nr(current));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700111
112 printk("\n1) %s/%d is trying to acquire this lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700113 current->comm, task_pid_nr(current));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700114 printk_lock(waiter->lock, 1);
115
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700116 printk("\n2) %s/%d is blocked on this lock:\n",
117 task->comm, task_pid_nr(task));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700118 printk_lock(waiter->deadlock_lock, 1);
119
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700120 debug_show_held_locks(current);
121 debug_show_held_locks(task);
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700122
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700123 printk("\n%s/%d's [blocked] stackdump:\n\n",
124 task->comm, task_pid_nr(task));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700125 show_stack(task, NULL);
126 printk("\n%s/%d's [current] stackdump:\n\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700127 current->comm, task_pid_nr(current));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700128 dump_stack();
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700129 debug_show_all_locks();
Pavel Emelyanov48d13e42008-02-08 04:21:53 -0800130 rcu_read_unlock();
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700131
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700132 printk("[ turning off deadlock detection."
133 "Please report this trace. ]\n\n");
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700134}
135
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700136void debug_rt_mutex_lock(struct rt_mutex *lock)
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700137{
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700138}
139
140void debug_rt_mutex_unlock(struct rt_mutex *lock)
141{
Thomas Gleixner0fa914c2011-06-08 09:58:38 +0200142 DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700143}
144
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700145void
146debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700147{
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700148}
149
150void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
151{
Thomas Gleixner0fa914c2011-06-08 09:58:38 +0200152 DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700153}
154
155void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
156{
157 memset(waiter, 0x11, sizeof(*waiter));
Pavel Emelyanov48d13e42008-02-08 04:21:53 -0800158 waiter->deadlock_task_pid = NULL;
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700159}
160
161void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
162{
Pavel Emelyanov48d13e42008-02-08 04:21:53 -0800163 put_pid(waiter->deadlock_task_pid);
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700164 memset(waiter, 0x22, sizeof(*waiter));
165}
166
167void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
168{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700169 /*
170 * Make sure we are not reinitializing a held lock:
171 */
172 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
173 lock->name = name;
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700174}
175
Ingo Molnar36c8b582006-07-03 00:25:41 -0700176void
177rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task)
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700178{
179}
180
181void rt_mutex_deadlock_account_unlock(struct task_struct *task)
182{
183}
184