rtmutex-debug.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/delay.h>
  21. #include <linux/module.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/kallsyms.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/plist.h>
  27. #include <linux/fs.h>
  28. #include <linux/debug_locks.h>
  29. #include "rtmutex_common.h"
  30. # define TRACE_WARN_ON(x) WARN_ON(x)
  31. # define TRACE_BUG_ON(x) BUG_ON(x)
  32. # define TRACE_OFF() \
  33. do { \
  34. if (rt_trace_on) { \
  35. rt_trace_on = 0; \
  36. console_verbose(); \
  37. if (raw_spin_is_locked(&current->pi_lock)) \
  38. raw_spin_unlock(&current->pi_lock); \
  39. } \
  40. } while (0)
  41. # define TRACE_OFF_NOLOCK() \
  42. do { \
  43. if (rt_trace_on) { \
  44. rt_trace_on = 0; \
  45. console_verbose(); \
  46. } \
  47. } while (0)
  48. # define TRACE_BUG_LOCKED() \
  49. do { \
  50. TRACE_OFF(); \
  51. BUG(); \
  52. } while (0)
  53. # define TRACE_WARN_ON_LOCKED(c) \
  54. do { \
  55. if (unlikely(c)) { \
  56. TRACE_OFF(); \
  57. WARN_ON(1); \
  58. } \
  59. } while (0)
  60. # define TRACE_BUG_ON_LOCKED(c) \
  61. do { \
  62. if (unlikely(c)) \
  63. TRACE_BUG_LOCKED(); \
  64. } while (0)
  65. #ifdef CONFIG_SMP
  66. # define SMP_TRACE_BUG_ON_LOCKED(c) TRACE_BUG_ON_LOCKED(c)
  67. #else
  68. # define SMP_TRACE_BUG_ON_LOCKED(c) do { } while (0)
  69. #endif
  70. /*
  71. * deadlock detection flag. We turn it off when we detect
  72. * the first problem because we dont want to recurse back
  73. * into the tracing code when doing error printk or
  74. * executing a BUG():
  75. */
  76. static int rt_trace_on = 1;
  77. static void printk_task(struct task_struct *p)
  78. {
  79. if (p)
  80. printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio);
  81. else
  82. printk("<none>");
  83. }
  84. static void printk_lock(struct rt_mutex *lock, int print_owner)
  85. {
  86. if (lock->name)
  87. printk(" [%p] {%s}\n",
  88. lock, lock->name);
  89. else
  90. printk(" [%p] {%s:%d}\n",
  91. lock, lock->file, lock->line);
  92. if (print_owner && rt_mutex_owner(lock)) {
  93. printk(".. ->owner: %p\n", lock->owner);
  94. printk(".. held by: ");
  95. printk_task(rt_mutex_owner(lock));
  96. printk("\n");
  97. }
  98. }
  99. void rt_mutex_debug_task_free(struct task_struct *task)
  100. {
  101. WARN_ON(!plist_head_empty(&task->pi_waiters));
  102. WARN_ON(task->pi_blocked_on);
  103. }
  104. /*
  105. * We fill out the fields in the waiter to store the information about
  106. * the deadlock. We print when we return. act_waiter can be NULL in
  107. * case of a remove waiter operation.
  108. */
  109. void debug_rt_mutex_deadlock(int detect, struct rt_mutex_waiter *act_waiter,
  110. struct rt_mutex *lock)
  111. {
  112. struct task_struct *task;
  113. if (!rt_trace_on || detect || !act_waiter)
  114. return;
  115. task = rt_mutex_owner(act_waiter->lock);
  116. if (task && task != current) {
  117. act_waiter->deadlock_task_pid = get_pid(task_pid(task));
  118. act_waiter->deadlock_lock = lock;
  119. }
  120. }
  121. void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
  122. {
  123. struct task_struct *task;
  124. if (!waiter->deadlock_lock || !rt_trace_on)
  125. return;
  126. rcu_read_lock();
  127. task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
  128. if (!task) {
  129. rcu_read_unlock();
  130. return;
  131. }
  132. TRACE_OFF_NOLOCK();
  133. printk("\n============================================\n");
  134. printk( "[ BUG: circular locking deadlock detected! ]\n");
  135. printk( "--------------------------------------------\n");
  136. printk("%s/%d is deadlocking current task %s/%d\n\n",
  137. task->comm, task_pid_nr(task),
  138. current->comm, task_pid_nr(current));
  139. printk("\n1) %s/%d is trying to acquire this lock:\n",
  140. current->comm, task_pid_nr(current));
  141. printk_lock(waiter->lock, 1);
  142. printk("\n2) %s/%d is blocked on this lock:\n",
  143. task->comm, task_pid_nr(task));
  144. printk_lock(waiter->deadlock_lock, 1);
  145. debug_show_held_locks(current);
  146. debug_show_held_locks(task);
  147. printk("\n%s/%d's [blocked] stackdump:\n\n",
  148. task->comm, task_pid_nr(task));
  149. show_stack(task, NULL);
  150. printk("\n%s/%d's [current] stackdump:\n\n",
  151. current->comm, task_pid_nr(current));
  152. dump_stack();
  153. debug_show_all_locks();
  154. rcu_read_unlock();
  155. printk("[ turning off deadlock detection."
  156. "Please report this trace. ]\n\n");
  157. local_irq_disable();
  158. }
  159. void debug_rt_mutex_lock(struct rt_mutex *lock)
  160. {
  161. }
  162. void debug_rt_mutex_unlock(struct rt_mutex *lock)
  163. {
  164. TRACE_WARN_ON_LOCKED(rt_mutex_owner(lock) != current);
  165. }
  166. void
  167. debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
  168. {
  169. }
  170. void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
  171. {
  172. TRACE_WARN_ON_LOCKED(!rt_mutex_owner(lock));
  173. }
  174. void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
  175. {
  176. memset(waiter, 0x11, sizeof(*waiter));
  177. plist_node_init(&waiter->list_entry, MAX_PRIO);
  178. plist_node_init(&waiter->pi_list_entry, MAX_PRIO);
  179. waiter->deadlock_task_pid = NULL;
  180. }
  181. void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
  182. {
  183. put_pid(waiter->deadlock_task_pid);
  184. TRACE_WARN_ON(!plist_node_empty(&waiter->list_entry));
  185. TRACE_WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
  186. memset(waiter, 0x22, sizeof(*waiter));
  187. }
  188. void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
  189. {
  190. /*
  191. * Make sure we are not reinitializing a held lock:
  192. */
  193. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  194. lock->name = name;
  195. }
  196. void
  197. rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task)
  198. {
  199. }
  200. void rt_mutex_deadlock_account_unlock(struct task_struct *task)
  201. {
  202. }