rtmutex-debug.c 4.5 KB

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