mutex-debug.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * kernel/mutex-debug.c
  3. *
  4. * Debugging code for mutexes
  5. *
  6. * Started by Ingo Molnar:
  7. *
  8. * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  9. *
  10. * lock debugging, locking tree, deadlock detection started by:
  11. *
  12. * Copyright (C) 2004, LynuxWorks, Inc., Igor Manyilov, Bill Huey
  13. * Released under the General Public License (GPL).
  14. */
  15. #include <linux/mutex.h>
  16. #include <linux/sched.h>
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/poison.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/interrupt.h>
  23. #include "mutex-debug.h"
  24. /*
  25. * We need a global lock when we walk through the multi-process
  26. * lock tree. Only used in the deadlock-debugging case.
  27. */
  28. DEFINE_SPINLOCK(debug_mutex_lock);
  29. /*
  30. * All locks held by all tasks, in a single global list:
  31. */
  32. LIST_HEAD(debug_mutex_held_locks);
  33. /*
  34. * In the debug case we carry the caller's instruction pointer into
  35. * other functions, but we dont want the function argument overhead
  36. * in the nondebug case - hence these macros:
  37. */
  38. #define __IP_DECL__ , unsigned long ip
  39. #define __IP__ , ip
  40. #define __RET_IP__ , (unsigned long)__builtin_return_address(0)
  41. /*
  42. * "mutex debugging enabled" flag. We turn it off when we detect
  43. * the first problem because we dont want to recurse back
  44. * into the tracing code when doing error printk or
  45. * executing a BUG():
  46. */
  47. int debug_mutex_on = 1;
  48. /*
  49. * Must be called with lock->wait_lock held.
  50. */
  51. void debug_mutex_set_owner(struct mutex *lock,
  52. struct thread_info *new_owner __IP_DECL__)
  53. {
  54. lock->owner = new_owner;
  55. DEBUG_LOCKS_WARN_ON(!list_empty(&lock->held_list));
  56. if (debug_mutex_on) {
  57. list_add_tail(&lock->held_list, &debug_mutex_held_locks);
  58. lock->acquire_ip = ip;
  59. }
  60. }
  61. void debug_mutex_init_waiter(struct mutex_waiter *waiter)
  62. {
  63. memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
  64. waiter->magic = waiter;
  65. INIT_LIST_HEAD(&waiter->list);
  66. }
  67. void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter)
  68. {
  69. SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock));
  70. DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list));
  71. DEBUG_LOCKS_WARN_ON(waiter->magic != waiter);
  72. DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
  73. }
  74. void debug_mutex_free_waiter(struct mutex_waiter *waiter)
  75. {
  76. DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list));
  77. memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter));
  78. }
  79. void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
  80. struct thread_info *ti __IP_DECL__)
  81. {
  82. SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock));
  83. /* Mark the current thread as blocked on the lock: */
  84. ti->task->blocked_on = waiter;
  85. waiter->lock = lock;
  86. }
  87. void mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
  88. struct thread_info *ti)
  89. {
  90. DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
  91. DEBUG_LOCKS_WARN_ON(waiter->task != ti->task);
  92. DEBUG_LOCKS_WARN_ON(ti->task->blocked_on != waiter);
  93. ti->task->blocked_on = NULL;
  94. list_del_init(&waiter->list);
  95. waiter->task = NULL;
  96. }
  97. void debug_mutex_unlock(struct mutex *lock)
  98. {
  99. DEBUG_LOCKS_WARN_ON(lock->magic != lock);
  100. DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
  101. DEBUG_LOCKS_WARN_ON(lock->owner != current_thread_info());
  102. if (debug_mutex_on) {
  103. DEBUG_LOCKS_WARN_ON(list_empty(&lock->held_list));
  104. list_del_init(&lock->held_list);
  105. }
  106. }
  107. void debug_mutex_init(struct mutex *lock, const char *name)
  108. {
  109. /*
  110. * Make sure we are not reinitializing a held lock:
  111. */
  112. mutex_debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  113. lock->owner = NULL;
  114. INIT_LIST_HEAD(&lock->held_list);
  115. lock->name = name;
  116. lock->magic = lock;
  117. }
  118. /***
  119. * mutex_destroy - mark a mutex unusable
  120. * @lock: the mutex to be destroyed
  121. *
  122. * This function marks the mutex uninitialized, and any subsequent
  123. * use of the mutex is forbidden. The mutex must not be locked when
  124. * this function is called.
  125. */
  126. void fastcall mutex_destroy(struct mutex *lock)
  127. {
  128. DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock));
  129. lock->magic = NULL;
  130. }
  131. EXPORT_SYMBOL_GPL(mutex_destroy);