rtmutex-debug.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 (spin_is_locked(&current->pi_lock)) \
  38. 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. int rt_trace_on = 1;
  77. void deadlock_trace_off(void)
  78. {
  79. rt_trace_on = 0;
  80. }
  81. static void printk_task(struct task_struct *p)
  82. {
  83. if (p)
  84. printk("%16s:%5d [%p, %3d]", p->comm, p->pid, p, p->prio);
  85. else
  86. printk("<none>");
  87. }
  88. static void printk_lock(struct rt_mutex *lock, int print_owner)
  89. {
  90. if (lock->name)
  91. printk(" [%p] {%s}\n",
  92. lock, lock->name);
  93. else
  94. printk(" [%p] {%s:%d}\n",
  95. lock, lock->file, lock->line);
  96. if (print_owner && rt_mutex_owner(lock)) {
  97. printk(".. ->owner: %p\n", lock->owner);
  98. printk(".. held by: ");
  99. printk_task(rt_mutex_owner(lock));
  100. printk("\n");
  101. }
  102. }
  103. void rt_mutex_debug_task_free(struct task_struct *task)
  104. {
  105. WARN_ON(!plist_head_empty(&task->pi_waiters));
  106. WARN_ON(task->pi_blocked_on);
  107. }
  108. /*
  109. * We fill out the fields in the waiter to store the information about
  110. * the deadlock. We print when we return. act_waiter can be NULL in
  111. * case of a remove waiter operation.
  112. */
  113. void debug_rt_mutex_deadlock(int detect, struct rt_mutex_waiter *act_waiter,
  114. struct rt_mutex *lock)
  115. {
  116. struct task_struct *task;
  117. if (!rt_trace_on || detect || !act_waiter)
  118. return;
  119. task = rt_mutex_owner(act_waiter->lock);
  120. if (task && task != current) {
  121. act_waiter->deadlock_task_pid = task->pid;
  122. act_waiter->deadlock_lock = lock;
  123. }
  124. }
  125. void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
  126. {
  127. struct task_struct *task;
  128. if (!waiter->deadlock_lock || !rt_trace_on)
  129. return;
  130. task = find_task_by_pid(waiter->deadlock_task_pid);
  131. if (!task)
  132. return;
  133. TRACE_OFF_NOLOCK();
  134. printk("\n============================================\n");
  135. printk( "[ BUG: circular locking deadlock detected! ]\n");
  136. printk( "--------------------------------------------\n");
  137. printk("%s/%d is deadlocking current task %s/%d\n\n",
  138. task->comm, task->pid, current->comm, current->pid);
  139. printk("\n1) %s/%d is trying to acquire this lock:\n",
  140. current->comm, current->pid);
  141. printk_lock(waiter->lock, 1);
  142. printk("\n2) %s/%d is blocked on this lock:\n", task->comm, task->pid);
  143. printk_lock(waiter->deadlock_lock, 1);
  144. debug_show_held_locks(current);
  145. debug_show_held_locks(task);
  146. printk("\n%s/%d's [blocked] stackdump:\n\n", task->comm, task->pid);
  147. show_stack(task, NULL);
  148. printk("\n%s/%d's [current] stackdump:\n\n",
  149. current->comm, current->pid);
  150. dump_stack();
  151. debug_show_all_locks();
  152. printk("[ turning off deadlock detection."
  153. "Please report this trace. ]\n\n");
  154. local_irq_disable();
  155. }
  156. void debug_rt_mutex_lock(struct rt_mutex *lock)
  157. {
  158. }
  159. void debug_rt_mutex_unlock(struct rt_mutex *lock)
  160. {
  161. TRACE_WARN_ON_LOCKED(rt_mutex_owner(lock) != current);
  162. }
  163. void
  164. debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
  165. {
  166. }
  167. void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
  168. {
  169. TRACE_WARN_ON_LOCKED(!rt_mutex_owner(lock));
  170. }
  171. void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
  172. {
  173. memset(waiter, 0x11, sizeof(*waiter));
  174. plist_node_init(&waiter->list_entry, MAX_PRIO);
  175. plist_node_init(&waiter->pi_list_entry, MAX_PRIO);
  176. }
  177. void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
  178. {
  179. TRACE_WARN_ON(!plist_node_empty(&waiter->list_entry));
  180. TRACE_WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
  181. TRACE_WARN_ON(waiter->task);
  182. memset(waiter, 0x22, sizeof(*waiter));
  183. }
  184. void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
  185. {
  186. /*
  187. * Make sure we are not reinitializing a held lock:
  188. */
  189. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  190. lock->name = name;
  191. }
  192. void
  193. rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task)
  194. {
  195. }
  196. void rt_mutex_deadlock_account_unlock(struct task_struct *task)
  197. {
  198. }