^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * RT-Mutexes: blocking mutual exclusion locks with PI support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * started by Ingo Molnar and Thomas Gleixner:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * This code is based on the rt.c implementation in the preempt-rt tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Portions of said code are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (C) 2004 LynuxWorks, Inc., Igor Manyilov, Bill Huey
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Copyright (C) 2006 Esben Nielsen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Copyright (C) 2006 Kihon Technologies Inc.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Steven Rostedt <rostedt@goodmis.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * See rt.c in preempt-rt for proper credits and further information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sched/rt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/rbtree.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/debug_locks.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "rtmutex_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static void printk_task(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) printk("<none>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void printk_lock(struct rt_mutex *lock, int print_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (lock->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) printk(" [%p] {%s}\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) lock, lock->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) printk(" [%p] {%s:%d}\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) lock, lock->file, lock->line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (print_owner && rt_mutex_owner(lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) printk(".. ->owner: %p\n", lock->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) printk(".. held by: ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) printk_task(rt_mutex_owner(lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) printk("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) void rt_mutex_debug_task_free(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * We fill out the fields in the waiter to store the information about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * the deadlock. We print when we return. act_waiter can be NULL in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * case of a remove waiter operation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) void debug_rt_mutex_deadlock(enum rtmutex_chainwalk chwalk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct rt_mutex_waiter *act_waiter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct rt_mutex *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!debug_locks || chwalk == RT_MUTEX_FULL_CHAINWALK || !act_waiter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) task = rt_mutex_owner(act_waiter->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (task && task != current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) act_waiter->deadlock_task_pid = get_pid(task_pid(task));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) act_waiter->deadlock_lock = lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct task_struct *task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!waiter->deadlock_lock || !debug_locks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (!task) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!debug_locks_off()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) pr_warn("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) pr_warn("============================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pr_warn("WARNING: circular locking deadlock detected!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pr_warn("%s\n", print_tainted());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) pr_warn("--------------------------------------------\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) printk("%s/%d is deadlocking current task %s/%d\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) task->comm, task_pid_nr(task),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) printk("\n1) %s/%d is trying to acquire this lock:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) printk_lock(waiter->lock, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) printk("\n2) %s/%d is blocked on this lock:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) task->comm, task_pid_nr(task));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) printk_lock(waiter->deadlock_lock, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) debug_show_held_locks(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) debug_show_held_locks(task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) printk("\n%s/%d's [blocked] stackdump:\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) task->comm, task_pid_nr(task));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) show_stack(task, NULL, KERN_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) printk("\n%s/%d's [current] stackdump:\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) debug_show_all_locks();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) printk("[ turning off deadlock detection."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) "Please report this trace. ]\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) void debug_rt_mutex_lock(struct rt_mutex *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) void debug_rt_mutex_unlock(struct rt_mutex *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) memset(waiter, 0x11, sizeof(*waiter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) waiter->deadlock_task_pid = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) put_pid(waiter->deadlock_task_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) memset(waiter, 0x22, sizeof(*waiter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) void debug_rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * Make sure we are not reinitializing a held lock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) debug_check_no_locks_freed((void *)lock, sizeof(*lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) lock->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) #ifdef CONFIG_DEBUG_LOCK_ALLOC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) lockdep_init_map(&lock->dep_map, name, key, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)