Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 2 | /* |
| 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 Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 20 | #include <linux/sched.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 21 | #include <linux/sched/rt.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 22 | #include <linux/sched/debug.h> |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 23 | #include <linux/delay.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 24 | #include <linux/export.h> |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 25 | #include <linux/spinlock.h> |
| 26 | #include <linux/kallsyms.h> |
| 27 | #include <linux/syscalls.h> |
| 28 | #include <linux/interrupt.h> |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 29 | #include <linux/rbtree.h> |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 30 | #include <linux/fs.h> |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 31 | #include <linux/debug_locks.h> |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 32 | |
| 33 | #include "rtmutex_common.h" |
| 34 | |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 35 | static void printk_task(struct task_struct *p) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 36 | { |
| 37 | if (p) |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 38 | printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 39 | else |
| 40 | printk("<none>"); |
| 41 | } |
| 42 | |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 43 | static 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 Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void rt_mutex_debug_task_free(struct task_struct *task) |
| 61 | { |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 62 | DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root)); |
Thomas Gleixner | 0fa914c | 2011-06-08 09:58:38 +0200 | [diff] [blame] | 63 | DEBUG_LOCKS_WARN_ON(task->pi_blocked_on); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 64 | } |
| 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 Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 71 | void debug_rt_mutex_deadlock(enum rtmutex_chainwalk chwalk, |
| 72 | struct rt_mutex_waiter *act_waiter, |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 73 | struct rt_mutex *lock) |
| 74 | { |
| 75 | struct task_struct *task; |
| 76 | |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 77 | if (!debug_locks || chwalk == RT_MUTEX_FULL_CHAINWALK || !act_waiter) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 78 | return; |
| 79 | |
| 80 | task = rt_mutex_owner(act_waiter->lock); |
| 81 | if (task && task != current) { |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 82 | act_waiter->deadlock_task_pid = get_pid(task_pid(task)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 83 | act_waiter->deadlock_lock = lock; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter) |
| 88 | { |
| 89 | struct task_struct *task; |
| 90 | |
Thomas Gleixner | 0fa914c | 2011-06-08 09:58:38 +0200 | [diff] [blame] | 91 | if (!waiter->deadlock_lock || !debug_locks) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 92 | return; |
| 93 | |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 94 | rcu_read_lock(); |
| 95 | task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID); |
| 96 | if (!task) { |
| 97 | rcu_read_unlock(); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 98 | return; |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 99 | } |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 100 | |
Thomas Gleixner | 68cc399 | 2011-10-05 13:20:24 +0200 | [diff] [blame] | 101 | if (!debug_locks_off()) { |
| 102 | rcu_read_unlock(); |
Thomas Gleixner | 0fa914c | 2011-06-08 09:58:38 +0200 | [diff] [blame] | 103 | return; |
Thomas Gleixner | 68cc399 | 2011-10-05 13:20:24 +0200 | [diff] [blame] | 104 | } |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 105 | |
Paul E. McKenney | a5dd63e | 2017-01-31 07:45:13 -0800 | [diff] [blame] | 106 | 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 Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 111 | printk("%s/%d is deadlocking current task %s/%d\n\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 112 | task->comm, task_pid_nr(task), |
| 113 | current->comm, task_pid_nr(current)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 114 | |
| 115 | printk("\n1) %s/%d is trying to acquire this lock:\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 116 | current->comm, task_pid_nr(current)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 117 | printk_lock(waiter->lock, 1); |
| 118 | |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 119 | printk("\n2) %s/%d is blocked on this lock:\n", |
| 120 | task->comm, task_pid_nr(task)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 121 | printk_lock(waiter->deadlock_lock, 1); |
| 122 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 123 | debug_show_held_locks(current); |
| 124 | debug_show_held_locks(task); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 125 | |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 126 | printk("\n%s/%d's [blocked] stackdump:\n\n", |
| 127 | task->comm, task_pid_nr(task)); |
Dmitry Safonov | 9cb8f06 | 2020-06-08 21:32:29 -0700 | [diff] [blame] | 128 | show_stack(task, NULL, KERN_DEFAULT); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 129 | printk("\n%s/%d's [current] stackdump:\n\n", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 130 | current->comm, task_pid_nr(current)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 131 | dump_stack(); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 132 | debug_show_all_locks(); |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 133 | rcu_read_unlock(); |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 134 | |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 135 | printk("[ turning off deadlock detection." |
| 136 | "Please report this trace. ]\n\n"); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 139 | void debug_rt_mutex_lock(struct rt_mutex *lock) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 140 | { |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void debug_rt_mutex_unlock(struct rt_mutex *lock) |
| 144 | { |
Thomas Gleixner | 0fa914c | 2011-06-08 09:58:38 +0200 | [diff] [blame] | 145 | DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 148 | void |
| 149 | debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 150 | { |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock) |
| 154 | { |
Thomas Gleixner | 0fa914c | 2011-06-08 09:58:38 +0200 | [diff] [blame] | 155 | DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock)); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter) |
| 159 | { |
| 160 | memset(waiter, 0x11, sizeof(*waiter)); |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 161 | waiter->deadlock_task_pid = NULL; |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter) |
| 165 | { |
Pavel Emelyanov | 48d13e4 | 2008-02-08 04:21:53 -0800 | [diff] [blame] | 166 | put_pid(waiter->deadlock_task_pid); |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 167 | memset(waiter, 0x22, sizeof(*waiter)); |
| 168 | } |
| 169 | |
Peter Zijlstra | f569478 | 2016-09-19 12:15:37 +0200 | [diff] [blame] | 170 | void debug_rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key) |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 171 | { |
Ingo Molnar | 9a11b49a | 2006-07-03 00:24:33 -0700 | [diff] [blame] | 172 | /* |
| 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 Zijlstra | f569478 | 2016-09-19 12:15:37 +0200 | [diff] [blame] | 177 | |
| 178 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 179 | lockdep_init_map(&lock->dep_map, name, key, 0); |
| 180 | #endif |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 181 | } |
| 182 | |