1 // SPDX-License-Identifier: GPL-2.0
3 * RT-Mutexes: blocking mutual exclusion locks with PI support
5 * started by Ingo Molnar and Thomas Gleixner:
7 * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8 * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
10 * This code is based on the rt.c implementation in the preempt-rt tree.
11 * Portions of said code are
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>
18 * See rt.c in preempt-rt for proper credits and further information
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>
31 #include <linux/debug_locks.h>
33 #include "rtmutex_common.h"
35 static void printk_task(struct task_struct *p)
38 printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio);
43 static void printk_lock(struct rt_mutex *lock, int print_owner)
46 printk(" [%p] {%s}\n",
49 printk(" [%p] {%s:%d}\n",
50 lock, lock->file, lock->line);
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));
60 void rt_mutex_debug_task_free(struct task_struct *task)
62 DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
63 DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
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.
71 void debug_rt_mutex_deadlock(enum rtmutex_chainwalk chwalk,
72 struct rt_mutex_waiter *act_waiter,
73 struct rt_mutex *lock)
75 struct task_struct *task;
77 if (!debug_locks || chwalk == RT_MUTEX_FULL_CHAINWALK || !act_waiter)
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;
87 void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
89 struct task_struct *task;
91 if (!waiter->deadlock_lock || !debug_locks)
95 task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
101 if (!debug_locks_off()) {
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));
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);
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);
123 debug_show_held_locks(current);
124 debug_show_held_locks(task);
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));
132 debug_show_all_locks();
135 printk("[ turning off deadlock detection."
136 "Please report this trace. ]\n\n");
139 void debug_rt_mutex_lock(struct rt_mutex *lock)
143 void debug_rt_mutex_unlock(struct rt_mutex *lock)
145 DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
149 debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
153 void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
155 DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
158 void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
160 memset(waiter, 0x11, sizeof(*waiter));
161 waiter->deadlock_task_pid = NULL;
164 void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
166 put_pid(waiter->deadlock_task_pid);
167 memset(waiter, 0x22, sizeof(*waiter));
170 void debug_rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key)
173 * Make sure we are not reinitializing a held lock:
175 debug_check_no_locks_freed((void *)lock, sizeof(*lock));
178 #ifdef CONFIG_DEBUG_LOCK_ALLOC
179 lockdep_init_map(&lock->dep_map, name, key, 0);