rtmutex-debug.c 5.7 KB

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