rtmutex-debug.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "rtmutex_common.h"
  33. static 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. static void printk_lock(struct rt_mutex *lock, int print_owner)
  41. {
  42. if (lock->name)
  43. printk(" [%p] {%s}\n",
  44. lock, lock->name);
  45. else
  46. printk(" [%p] {%s:%d}\n",
  47. lock, lock->file, lock->line);
  48. if (print_owner && rt_mutex_owner(lock)) {
  49. printk(".. ->owner: %p\n", lock->owner);
  50. printk(".. held by: ");
  51. printk_task(rt_mutex_owner(lock));
  52. printk("\n");
  53. }
  54. }
  55. void rt_mutex_debug_task_free(struct task_struct *task)
  56. {
  57. DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters.rb_root));
  58. DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
  59. }
  60. /*
  61. * We fill out the fields in the waiter to store the information about
  62. * the deadlock. We print when we return. act_waiter can be NULL in
  63. * case of a remove waiter operation.
  64. */
  65. void debug_rt_mutex_deadlock(enum rtmutex_chainwalk chwalk,
  66. struct rt_mutex_waiter *act_waiter,
  67. struct rt_mutex *lock)
  68. {
  69. struct task_struct *task;
  70. if (!debug_locks || chwalk == RT_MUTEX_FULL_CHAINWALK || !act_waiter)
  71. return;
  72. task = rt_mutex_owner(act_waiter->lock);
  73. if (task && task != current) {
  74. act_waiter->deadlock_task_pid = get_pid(task_pid(task));
  75. act_waiter->deadlock_lock = lock;
  76. }
  77. }
  78. void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
  79. {
  80. struct task_struct *task;
  81. if (!waiter->deadlock_lock || !debug_locks)
  82. return;
  83. rcu_read_lock();
  84. task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
  85. if (!task) {
  86. rcu_read_unlock();
  87. return;
  88. }
  89. if (!debug_locks_off()) {
  90. rcu_read_unlock();
  91. return;
  92. }
  93. pr_warn("\n");
  94. pr_warn("============================================\n");
  95. pr_warn("WARNING: circular locking deadlock detected!\n");
  96. pr_warn("%s\n", print_tainted());
  97. pr_warn("--------------------------------------------\n");
  98. printk("%s/%d is deadlocking current task %s/%d\n\n",
  99. task->comm, task_pid_nr(task),
  100. current->comm, task_pid_nr(current));
  101. printk("\n1) %s/%d is trying to acquire this lock:\n",
  102. current->comm, task_pid_nr(current));
  103. printk_lock(waiter->lock, 1);
  104. printk("\n2) %s/%d is blocked on this lock:\n",
  105. task->comm, task_pid_nr(task));
  106. printk_lock(waiter->deadlock_lock, 1);
  107. debug_show_held_locks(current);
  108. debug_show_held_locks(task);
  109. printk("\n%s/%d's [blocked] stackdump:\n\n",
  110. task->comm, task_pid_nr(task));
  111. show_stack(task, NULL);
  112. printk("\n%s/%d's [current] stackdump:\n\n",
  113. current->comm, task_pid_nr(current));
  114. dump_stack();
  115. debug_show_all_locks();
  116. rcu_read_unlock();
  117. printk("[ turning off deadlock detection."
  118. "Please report this trace. ]\n\n");
  119. }
  120. void debug_rt_mutex_lock(struct rt_mutex *lock)
  121. {
  122. }
  123. void debug_rt_mutex_unlock(struct rt_mutex *lock)
  124. {
  125. DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
  126. }
  127. void
  128. debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
  129. {
  130. }
  131. void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
  132. {
  133. DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
  134. }
  135. void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
  136. {
  137. memset(waiter, 0x11, sizeof(*waiter));
  138. waiter->deadlock_task_pid = NULL;
  139. }
  140. void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
  141. {
  142. put_pid(waiter->deadlock_task_pid);
  143. memset(waiter, 0x22, sizeof(*waiter));
  144. }
  145. void debug_rt_mutex_init(struct rt_mutex *lock, const char *name, struct lock_class_key *key)
  146. {
  147. /*
  148. * Make sure we are not reinitializing a held lock:
  149. */
  150. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  151. lock->name = name;
  152. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  153. lockdep_init_map(&lock->dep_map, name, key, 0);
  154. #endif
  155. }