blob: 36e69100e8e0620850d957f668f3045e962bb2d9 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnare7eebaf2006-06-27 02:54:55 -07002/*
3 * RT-Mutexes: blocking mutual exclusion locks with PI support
4 *
5 * started by Ingo Molnar and Thomas Gleixner:
6 *
7 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
9 *
10 * This code is based on the rt.c implementation in the preempt-rt tree.
11 * Portions of said code are
12 *
13 * Copyright (C) 2004 LynuxWorks, Inc., Igor Manyilov, Bill Huey
14 * Copyright (C) 2006 Esben Nielsen
15 * Copyright (C) 2006 Kihon Technologies Inc.,
16 * Steven Rostedt <rostedt@goodmis.org>
17 *
18 * See rt.c in preempt-rt for proper credits and further information
19 */
Ingo Molnare7eebaf2006-06-27 02:54:55 -070020#include <linux/sched.h>
Clark Williams8bd75c72013-02-07 09:47:07 -060021#include <linux/sched/rt.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010022#include <linux/sched/debug.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -070023#include <linux/delay.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040024#include <linux/export.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -070025#include <linux/spinlock.h>
26#include <linux/kallsyms.h>
27#include <linux/syscalls.h>
28#include <linux/interrupt.h>
Peter Zijlstrafb00aca2013-11-07 14:43:43 +010029#include <linux/rbtree.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -070030#include <linux/fs.h>
Ingo Molnar9a11b49a2006-07-03 00:24:33 -070031#include <linux/debug_locks.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -070032
33#include "rtmutex_common.h"
34
Ingo Molnar36c8b582006-07-03 00:25:41 -070035static void printk_task(struct task_struct *p)
Ingo Molnare7eebaf2006-06-27 02:54:55 -070036{
37 if (p)
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -070038 printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio);
Ingo Molnare7eebaf2006-06-27 02:54:55 -070039 else
40 printk("<none>");
41}
42
Ingo Molnare7eebaf2006-06-27 02:54:55 -070043static void printk_lock(struct rt_mutex *lock, int print_owner)
44{
45 if (lock->name)
46 printk(" [%p] {%s}\n",
47 lock, lock->name);
48 else
49 printk(" [%p] {%s:%d}\n",
50 lock, lock->file, lock->line);
51
52 if (print_owner && rt_mutex_owner(lock)) {
53 printk(".. ->owner: %p\n", lock->owner);
54 printk(".. held by: ");
55 printk_task(rt_mutex_owner(lock));
56 printk("\n");
57 }
Ingo Molnare7eebaf2006-06-27 02:54:55 -070058}
59
60void rt_mutex_debug_task_free(struct task_struct *task)
61{
Davidlohr Buesoa23ba902017-09-08 16:15:01 -070062 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
Thomas Gleixner0fa914c2011-06-08 09:58:38 +020063 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
Ingo Molnare7eebaf2006-06-27 02:54:55 -070064}
65
66/*
67 * We fill out the fields in the waiter to store the information about
68 * the deadlock. We print when we return. act_waiter can be NULL in
69 * case of a remove waiter operation.
70 */
Thomas Gleixner8930ed82014-05-22 03:25:47 +000071void debug_rt_mutex_deadlock(enum rtmutex_chainwalk chwalk,
72 struct rt_mutex_waiter *act_waiter,
Ingo Molnare7eebaf2006-06-27 02:54:55 -070073 struct rt_mutex *lock)
74{
75 struct task_struct *task;
76
Thomas Gleixner8930ed82014-05-22 03:25:47 +000077 if (!debug_locks || chwalk == RT_MUTEX_FULL_CHAINWALK || !act_waiter)
Ingo Molnare7eebaf2006-06-27 02:54:55 -070078 return;
79
80 task = rt_mutex_owner(act_waiter->lock);
81 if (task && task != current) {
Pavel Emelyanov48d13e42008-02-08 04:21:53 -080082 act_waiter->deadlock_task_pid = get_pid(task_pid(task));
Ingo Molnare7eebaf2006-06-27 02:54:55 -070083 act_waiter->deadlock_lock = lock;
84 }
85}
86
87void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
88{
89 struct task_struct *task;
90
Thomas Gleixner0fa914c2011-06-08 09:58:38 +020091 if (!waiter->deadlock_lock || !debug_locks)
Ingo Molnare7eebaf2006-06-27 02:54:55 -070092 return;
93
Pavel Emelyanov48d13e42008-02-08 04:21:53 -080094 rcu_read_lock();
95 task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
96 if (!task) {
97 rcu_read_unlock();
Ingo Molnare7eebaf2006-06-27 02:54:55 -070098 return;
Pavel Emelyanov48d13e42008-02-08 04:21:53 -080099 }
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700100
Thomas Gleixner68cc3992011-10-05 13:20:24 +0200101 if (!debug_locks_off()) {
102 rcu_read_unlock();
Thomas Gleixner0fa914c2011-06-08 09:58:38 +0200103 return;
Thomas Gleixner68cc3992011-10-05 13:20:24 +0200104 }
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700105
Paul E. McKenneya5dd63e2017-01-31 07:45:13 -0800106 pr_warn("\n");
107 pr_warn("============================================\n");
108 pr_warn("WARNING: circular locking deadlock detected!\n");
109 pr_warn("%s\n", print_tainted());
110 pr_warn("--------------------------------------------\n");
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700111 printk("%s/%d is deadlocking current task %s/%d\n\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700112 task->comm, task_pid_nr(task),
113 current->comm, task_pid_nr(current));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700114
115 printk("\n1) %s/%d is trying to acquire this lock:\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700116 current->comm, task_pid_nr(current));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700117 printk_lock(waiter->lock, 1);
118
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700119 printk("\n2) %s/%d is blocked on this lock:\n",
120 task->comm, task_pid_nr(task));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700121 printk_lock(waiter->deadlock_lock, 1);
122
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700123 debug_show_held_locks(current);
124 debug_show_held_locks(task);
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700125
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700126 printk("\n%s/%d's [blocked] stackdump:\n\n",
127 task->comm, task_pid_nr(task));
Dmitry Safonov9cb8f062020-06-08 21:32:29 -0700128 show_stack(task, NULL, KERN_DEFAULT);
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700129 printk("\n%s/%d's [current] stackdump:\n\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700130 current->comm, task_pid_nr(current));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700131 dump_stack();
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700132 debug_show_all_locks();
Pavel Emelyanov48d13e42008-02-08 04:21:53 -0800133 rcu_read_unlock();
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700134
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700135 printk("[ turning off deadlock detection."
136 "Please report this trace. ]\n\n");
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700137}
138
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700139void debug_rt_mutex_lock(struct rt_mutex *lock)
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700140{
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700141}
142
143void debug_rt_mutex_unlock(struct rt_mutex *lock)
144{
Thomas Gleixner0fa914c2011-06-08 09:58:38 +0200145 DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700146}
147
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700148void
149debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700150{
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700151}
152
153void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
154{
Thomas Gleixner0fa914c2011-06-08 09:58:38 +0200155 DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700156}
157
158void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
159{
160 memset(waiter, 0x11, sizeof(*waiter));
Pavel Emelyanov48d13e42008-02-08 04:21:53 -0800161 waiter->deadlock_task_pid = NULL;
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700162}
163
164void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
165{
Pavel Emelyanov48d13e42008-02-08 04:21:53 -0800166 put_pid(waiter->deadlock_task_pid);
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700167 memset(waiter, 0x22, sizeof(*waiter));
168}
169
Peter Zijlstraf5694782016-09-19 12:15:37 +0200170void debug_rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key)
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700171{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700172 /*
173 * Make sure we are not reinitializing a held lock:
174 */
175 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
176 lock->name = name;
Peter Zijlstraf5694782016-09-19 12:15:37 +0200177
178#ifdef CONFIG_DEBUG_LOCK_ALLOC
179 lockdep_init_map(&lock->dep_map, name, key, 0);
180#endif
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700181}
182