rtmutex-debug.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. 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, p->pid, 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 = task->pid;
  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. task = find_task_by_pid(waiter->deadlock_task_pid);
  127. if (!task)
  128. return;
  129. TRACE_OFF_NOLOCK();
  130. printk("\n============================================\n");
  131. printk( "[ BUG: circular locking deadlock detected! ]\n");
  132. printk( "--------------------------------------------\n");
  133. printk("%s/%d is deadlocking current task %s/%d\n\n",
  134. task->comm, task->pid, current->comm, current->pid);
  135. printk("\n1) %s/%d is trying to acquire this lock:\n",
  136. current->comm, current->pid);
  137. printk_lock(waiter->lock, 1);
  138. printk("\n2) %s/%d is blocked on this lock:\n", task->comm, task->pid);
  139. printk_lock(waiter->deadlock_lock, 1);
  140. debug_show_held_locks(current);
  141. debug_show_held_locks(task);
  142. printk("\n%s/%d's [blocked] stackdump:\n\n", task->comm, task->pid);
  143. show_stack(task, NULL);
  144. printk("\n%s/%d's [current] stackdump:\n\n",
  145. current->comm, current->pid);
  146. dump_stack();
  147. debug_show_all_locks();
  148. printk("[ turning off deadlock detection."
  149. "Please report this trace. ]\n\n");
  150. local_irq_disable();
  151. }
  152. void debug_rt_mutex_lock(struct rt_mutex *lock)
  153. {
  154. }
  155. void debug_rt_mutex_unlock(struct rt_mutex *lock)
  156. {
  157. TRACE_WARN_ON_LOCKED(rt_mutex_owner(lock) != current);
  158. }
  159. void
  160. debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
  161. {
  162. }
  163. void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
  164. {
  165. TRACE_WARN_ON_LOCKED(!rt_mutex_owner(lock));
  166. }
  167. void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
  168. {
  169. memset(waiter, 0x11, sizeof(*waiter));
  170. plist_node_init(&waiter->list_entry, MAX_PRIO);
  171. plist_node_init(&waiter->pi_list_entry, MAX_PRIO);
  172. }
  173. void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
  174. {
  175. TRACE_WARN_ON(!plist_node_empty(&waiter->list_entry));
  176. TRACE_WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
  177. TRACE_WARN_ON(waiter->task);
  178. memset(waiter, 0x22, sizeof(*waiter));
  179. }
  180. void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
  181. {
  182. /*
  183. * Make sure we are not reinitializing a held lock:
  184. */
  185. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  186. lock->name = name;
  187. }
  188. void
  189. rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task)
  190. {
  191. }
  192. void rt_mutex_deadlock_account_unlock(struct task_struct *task)
  193. {
  194. }