rtmutex-debug.c 5.7 KB

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