rtmutex-debug.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/sched/rt.h>
  21. #include <linux/delay.h>
  22. #include <linux/export.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/kallsyms.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/rbtree.h>
  28. #include <linux/fs.h>
  29. #include <linux/debug_locks.h>
  30. #include "rtmutex_common.h"
  31. static void printk_task(struct task_struct *p)
  32. {
  33. if (p)
  34. printk("%16s:%5d [%p, %3d]", p->comm, task_pid_nr(p), p, p->prio);
  35. else
  36. printk("<none>");
  37. }
  38. static void printk_lock(struct rt_mutex *lock, int print_owner)
  39. {
  40. if (lock->name)
  41. printk(" [%p] {%s}\n",
  42. lock, lock->name);
  43. else
  44. printk(" [%p] {%s:%d}\n",
  45. lock, lock->file, lock->line);
  46. if (print_owner && rt_mutex_owner(lock)) {
  47. printk(".. ->owner: %p\n", lock->owner);
  48. printk(".. held by: ");
  49. printk_task(rt_mutex_owner(lock));
  50. printk("\n");
  51. }
  52. }
  53. void rt_mutex_debug_task_free(struct task_struct *task)
  54. {
  55. DEBUG_LOCKS_WARN_ON(!RB_EMPTY_ROOT(&task->pi_waiters));
  56. DEBUG_LOCKS_WARN_ON(task->pi_blocked_on);
  57. }
  58. /*
  59. * We fill out the fields in the waiter to store the information about
  60. * the deadlock. We print when we return. act_waiter can be NULL in
  61. * case of a remove waiter operation.
  62. */
  63. void debug_rt_mutex_deadlock(enum rtmutex_chainwalk chwalk,
  64. struct rt_mutex_waiter *act_waiter,
  65. struct rt_mutex *lock)
  66. {
  67. struct task_struct *task;
  68. if (!debug_locks || chwalk == RT_MUTEX_FULL_CHAINWALK || !act_waiter)
  69. return;
  70. task = rt_mutex_owner(act_waiter->lock);
  71. if (task && task != current) {
  72. act_waiter->deadlock_task_pid = get_pid(task_pid(task));
  73. act_waiter->deadlock_lock = lock;
  74. }
  75. }
  76. void debug_rt_mutex_print_deadlock(struct rt_mutex_waiter *waiter)
  77. {
  78. struct task_struct *task;
  79. if (!waiter->deadlock_lock || !debug_locks)
  80. return;
  81. rcu_read_lock();
  82. task = pid_task(waiter->deadlock_task_pid, PIDTYPE_PID);
  83. if (!task) {
  84. rcu_read_unlock();
  85. return;
  86. }
  87. if (!debug_locks_off()) {
  88. rcu_read_unlock();
  89. return;
  90. }
  91. printk("\n============================================\n");
  92. printk( "[ BUG: circular locking deadlock detected! ]\n");
  93. printk("%s\n", print_tainted());
  94. printk( "--------------------------------------------\n");
  95. printk("%s/%d is deadlocking current task %s/%d\n\n",
  96. task->comm, task_pid_nr(task),
  97. current->comm, task_pid_nr(current));
  98. printk("\n1) %s/%d is trying to acquire this lock:\n",
  99. current->comm, task_pid_nr(current));
  100. printk_lock(waiter->lock, 1);
  101. printk("\n2) %s/%d is blocked on this lock:\n",
  102. task->comm, task_pid_nr(task));
  103. printk_lock(waiter->deadlock_lock, 1);
  104. debug_show_held_locks(current);
  105. debug_show_held_locks(task);
  106. printk("\n%s/%d's [blocked] stackdump:\n\n",
  107. task->comm, task_pid_nr(task));
  108. show_stack(task, NULL);
  109. printk("\n%s/%d's [current] stackdump:\n\n",
  110. current->comm, task_pid_nr(current));
  111. dump_stack();
  112. debug_show_all_locks();
  113. rcu_read_unlock();
  114. printk("[ turning off deadlock detection."
  115. "Please report this trace. ]\n\n");
  116. }
  117. void debug_rt_mutex_lock(struct rt_mutex *lock)
  118. {
  119. }
  120. void debug_rt_mutex_unlock(struct rt_mutex *lock)
  121. {
  122. DEBUG_LOCKS_WARN_ON(rt_mutex_owner(lock) != current);
  123. }
  124. void
  125. debug_rt_mutex_proxy_lock(struct rt_mutex *lock, struct task_struct *powner)
  126. {
  127. }
  128. void debug_rt_mutex_proxy_unlock(struct rt_mutex *lock)
  129. {
  130. DEBUG_LOCKS_WARN_ON(!rt_mutex_owner(lock));
  131. }
  132. void debug_rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
  133. {
  134. memset(waiter, 0x11, sizeof(*waiter));
  135. waiter->deadlock_task_pid = NULL;
  136. }
  137. void debug_rt_mutex_free_waiter(struct rt_mutex_waiter *waiter)
  138. {
  139. put_pid(waiter->deadlock_task_pid);
  140. memset(waiter, 0x22, sizeof(*waiter));
  141. }
  142. void debug_rt_mutex_init(struct rt_mutex *lock, const char *name)
  143. {
  144. /*
  145. * Make sure we are not reinitializing a held lock:
  146. */
  147. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  148. lock->name = name;
  149. }
  150. void
  151. rt_mutex_deadlock_account_lock(struct rt_mutex *lock, struct task_struct *task)
  152. {
  153. }
  154. void rt_mutex_deadlock_account_unlock(struct task_struct *task)
  155. {
  156. }