Loading...
1// SPDX-License-Identifier: GPL-2.0
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 */
20#include <linux/sched.h>
21#include <linux/sched/rt.h>
22#include <linux/sched/debug.h>
23#include <linux/delay.h>
24#include <linux/export.h>
25#include <linux/spinlock.h>
26#include <linux/kallsyms.h>
27#include <linux/syscalls.h>
28#include <linux/interrupt.h>
29#include <linux/rbtree.h>
30#include <linux/fs.h>
31#include <linux/debug_locks.h>
32
33#include "rtmutex_common.h"
34
35static void printk_task(struct task_struct *p)
36{
37 if (p)
38 printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio);
39 else
40 printk("<none>");
41}
42
43static 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 }
58}
59
60void rt_mutex_debug_task_free(struct task_struct *task)
61{
62 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
63 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
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 */
71void debug_rt_mutex_deadlock(enum rtmutex_chainwalk chwalk,
72 struct rt_mutex_waiter *act_waiter,
73 struct rt_mutex *lock)
74{
75 struct task_struct *task;
76
77 if (!debug_locks || chwalk == RT_MUTEX_FULL_CHAINWALK || !act_waiter)
78 return;
79
80 task = rt_mutex_owner(act_waiter->lock);
81 if (task && task != current) {
82 act_waiter->deadlock_task_pid = get_pid(task_pid(task));
83 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
91 if (!waiter->deadlock_lock || !debug_locks)
92 return;
93
94 rcu_read_lock();
95 task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
96 if (!task) {
97 rcu_read_unlock();
98 return;
99 }
100
101 if (!debug_locks_off()) {
102 rcu_read_unlock();
103 return;
104 }
105
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");
111 printk("%s/%d is deadlocking current task %s/%d\n\n",
112 task->comm, task_pid_nr(task),
113 current->comm, task_pid_nr(current));
114
115 printk("\n1) %s/%d is trying to acquire this lock:\n",
116 current->comm, task_pid_nr(current));
117 printk_lock(waiter->lock, 1);
118
119 printk("\n2) %s/%d is blocked on this lock:\n",
120 task->comm, task_pid_nr(task));
121 printk_lock(waiter->deadlock_lock, 1);
122
123 debug_show_held_locks(current);
124 debug_show_held_locks(task);
125
126 printk("\n%s/%d's [blocked] stackdump:\n\n",
127 task->comm, task_pid_nr(task));
128 show_stack(task, NULL);
129 printk("\n%s/%d's [current] stackdump:\n\n",
130 current->comm, task_pid_nr(current));
131 dump_stack();
132 debug_show_all_locks();
133 rcu_read_unlock();
134
135 printk("[ turning off deadlock detection."
136 "Please report this trace. ]\n\n");
137}
138
139void debug_rt_mutex_lock(struct rt_mutex *lock)
140{
141}
142
143void debug_rt_mutex_unlock(struct rt_mutex *lock)
144{
145 DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
146}
147
148void
149debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
150{
151}
152
153void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
154{
155 DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
156}
157
158void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
159{
160 memset(waiter, 0x11, sizeof(*waiter));
161 waiter->deadlock_task_pid = NULL;
162}
163
164void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
165{
166 put_pid(waiter->deadlock_task_pid);
167 memset(waiter, 0x22, sizeof(*waiter));
168}
169
170void debug_rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key)
171{
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;
177
178#ifdef CONFIG_DEBUG_LOCK_ALLOC
179 lockdep_init_map(&lock->dep_map, name, key, 0);
180#endif
181}
182
1/*
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 */
19#include <linux/sched.h>
20#include <linux/sched/rt.h>
21#include <linux/delay.h>
22#include <linux/export.h>
23#include <linux/spinlock.h>
24#include <linux/kallsyms.h>
25#include <linux/syscalls.h>
26#include <linux/interrupt.h>
27#include <linux/rbtree.h>
28#include <linux/fs.h>
29#include <linux/debug_locks.h>
30
31#include "rtmutex_common.h"
32
33static void printk_task(struct task_struct *p)
34{
35 if (p)
36 printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio);
37 else
38 printk("<none>");
39}
40
41static 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 }
56}
57
58void rt_mutex_debug_task_free(struct task_struct *task)
59{
60 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters));
61 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
62}
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 */
69void debug_rt_mutex_deadlock(int detect, struct rt_mutex_waiter *act_waiter,
70 struct rt_mutex *lock)
71{
72 struct task_struct *task;
73
74 if (!debug_locks || detect || !act_waiter)
75 return;
76
77 task = rt_mutex_owner(act_waiter->lock);
78 if (task && task != current) {
79 act_waiter->deadlock_task_pid = get_pid(task_pid(task));
80 act_waiter->deadlock_lock = lock;
81 }
82}
83
84void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
85{
86 struct task_struct *task;
87
88 if (!waiter->deadlock_lock || !debug_locks)
89 return;
90
91 rcu_read_lock();
92 task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
93 if (!task) {
94 rcu_read_unlock();
95 return;
96 }
97
98 if (!debug_locks_off()) {
99 rcu_read_unlock();
100 return;
101 }
102
103 printk("\n============================================\n");
104 printk( "[ BUG: circular locking deadlock detected! ]\n");
105 printk("%s\n", print_tainted());
106 printk( "--------------------------------------------\n");
107 printk("%s/%d is deadlocking current task %s/%d\n\n",
108 task->comm, task_pid_nr(task),
109 current->comm, task_pid_nr(current));
110
111 printk("\n1) %s/%d is trying to acquire this lock:\n",
112 current->comm, task_pid_nr(current));
113 printk_lock(waiter->lock, 1);
114
115 printk("\n2) %s/%d is blocked on this lock:\n",
116 task->comm, task_pid_nr(task));
117 printk_lock(waiter->deadlock_lock, 1);
118
119 debug_show_held_locks(current);
120 debug_show_held_locks(task);
121
122 printk("\n%s/%d's [blocked] stackdump:\n\n",
123 task->comm, task_pid_nr(task));
124 show_stack(task, NULL);
125 printk("\n%s/%d's [current] stackdump:\n\n",
126 current->comm, task_pid_nr(current));
127 dump_stack();
128 debug_show_all_locks();
129 rcu_read_unlock();
130
131 printk("[ turning off deadlock detection."
132 "Please report this trace. ]\n\n");
133}
134
135void debug_rt_mutex_lock(struct rt_mutex *lock)
136{
137}
138
139void debug_rt_mutex_unlock(struct rt_mutex *lock)
140{
141 DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
142}
143
144void
145debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
146{
147}
148
149void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
150{
151 DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
152}
153
154void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
155{
156 memset(waiter, 0x11, sizeof(*waiter));
157 waiter->deadlock_task_pid = NULL;
158}
159
160void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
161{
162 put_pid(waiter->deadlock_task_pid);
163 memset(waiter, 0x22, sizeof(*waiter));
164}
165
166void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
167{
168 /*
169 * Make sure we are not reinitializing a held lock:
170 */
171 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
172 lock->name = name;
173}
174
175void
176rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task)
177{
178}
179
180void rt_mutex_deadlock_account_unlock(struct task_struct *task)
181{
182}
183