spinlock_debug.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright 2005, Red Hat, Inc., Ingo Molnar
  3. * Released under the General Public License (GPL).
  4. *
  5. * This file contains the spinlock/rwlock implementations for
  6. * DEBUG_SPINLOCK.
  7. */
  8. #include <linux/spinlock.h>
  9. #include <linux/nmi.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/debug_locks.h>
  12. #include <linux/delay.h>
  13. #include <linux/export.h>
  14. void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
  15. struct lock_class_key *key)
  16. {
  17. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  18. /*
  19. * Make sure we are not reinitializing a held lock:
  20. */
  21. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  22. lockdep_init_map(&lock->dep_map, name, key, 0);
  23. #endif
  24. lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  25. lock->magic = SPINLOCK_MAGIC;
  26. lock->owner = SPINLOCK_OWNER_INIT;
  27. lock->owner_cpu = -1;
  28. }
  29. EXPORT_SYMBOL(__raw_spin_lock_init);
  30. void __rwlock_init(rwlock_t *lock, const char *name,
  31. struct lock_class_key *key)
  32. {
  33. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  34. /*
  35. * Make sure we are not reinitializing a held lock:
  36. */
  37. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  38. lockdep_init_map(&lock->dep_map, name, key, 0);
  39. #endif
  40. lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
  41. lock->magic = RWLOCK_MAGIC;
  42. lock->owner = SPINLOCK_OWNER_INIT;
  43. lock->owner_cpu = -1;
  44. }
  45. EXPORT_SYMBOL(__rwlock_init);
  46. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  47. {
  48. struct task_struct *owner = NULL;
  49. if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
  50. owner = lock->owner;
  51. printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
  52. msg, raw_smp_processor_id(),
  53. current->comm, task_pid_nr(current));
  54. printk(KERN_EMERG " lock: %pS, .magic: %08x, .owner: %s/%d, "
  55. ".owner_cpu: %d\n",
  56. lock, lock->magic,
  57. owner ? owner->comm : "<none>",
  58. owner ? task_pid_nr(owner) : -1,
  59. lock->owner_cpu);
  60. dump_stack();
  61. }
  62. static void spin_bug(raw_spinlock_t *lock, const char *msg)
  63. {
  64. if (!debug_locks_off())
  65. return;
  66. spin_dump(lock, msg);
  67. }
  68. #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
  69. static inline void
  70. debug_spin_lock_before(raw_spinlock_t *lock)
  71. {
  72. SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
  73. SPIN_BUG_ON(lock->owner == current, lock, "recursion");
  74. SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
  75. lock, "cpu recursion");
  76. }
  77. static inline void debug_spin_lock_after(raw_spinlock_t *lock)
  78. {
  79. lock->owner_cpu = raw_smp_processor_id();
  80. lock->owner = current;
  81. }
  82. static inline void debug_spin_unlock(raw_spinlock_t *lock)
  83. {
  84. SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
  85. SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
  86. SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
  87. SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
  88. lock, "wrong CPU");
  89. lock->owner = SPINLOCK_OWNER_INIT;
  90. lock->owner_cpu = -1;
  91. }
  92. /*
  93. * We are now relying on the NMI watchdog to detect lockup instead of doing
  94. * the detection here with an unfair lock which can cause problem of its own.
  95. */
  96. void do_raw_spin_lock(raw_spinlock_t *lock)
  97. {
  98. debug_spin_lock_before(lock);
  99. arch_spin_lock(&lock->raw_lock);
  100. debug_spin_lock_after(lock);
  101. }
  102. int do_raw_spin_trylock(raw_spinlock_t *lock)
  103. {
  104. int ret = arch_spin_trylock(&lock->raw_lock);
  105. if (ret)
  106. debug_spin_lock_after(lock);
  107. #ifndef CONFIG_SMP
  108. /*
  109. * Must not happen on UP:
  110. */
  111. SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
  112. #endif
  113. return ret;
  114. }
  115. void do_raw_spin_unlock(raw_spinlock_t *lock)
  116. {
  117. debug_spin_unlock(lock);
  118. arch_spin_unlock(&lock->raw_lock);
  119. }
  120. static void rwlock_bug(rwlock_t *lock, const char *msg)
  121. {
  122. if (!debug_locks_off())
  123. return;
  124. printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
  125. msg, raw_smp_processor_id(), current->comm,
  126. task_pid_nr(current), lock);
  127. dump_stack();
  128. }
  129. #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
  130. void do_raw_read_lock(rwlock_t *lock)
  131. {
  132. RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
  133. arch_read_lock(&lock->raw_lock);
  134. }
  135. int do_raw_read_trylock(rwlock_t *lock)
  136. {
  137. int ret = arch_read_trylock(&lock->raw_lock);
  138. #ifndef CONFIG_SMP
  139. /*
  140. * Must not happen on UP:
  141. */
  142. RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
  143. #endif
  144. return ret;
  145. }
  146. void do_raw_read_unlock(rwlock_t *lock)
  147. {
  148. RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
  149. arch_read_unlock(&lock->raw_lock);
  150. }
  151. static inline void debug_write_lock_before(rwlock_t *lock)
  152. {
  153. RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
  154. RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
  155. RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
  156. lock, "cpu recursion");
  157. }
  158. static inline void debug_write_lock_after(rwlock_t *lock)
  159. {
  160. lock->owner_cpu = raw_smp_processor_id();
  161. lock->owner = current;
  162. }
  163. static inline void debug_write_unlock(rwlock_t *lock)
  164. {
  165. RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
  166. RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
  167. RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
  168. lock, "wrong CPU");
  169. lock->owner = SPINLOCK_OWNER_INIT;
  170. lock->owner_cpu = -1;
  171. }
  172. void do_raw_write_lock(rwlock_t *lock)
  173. {
  174. debug_write_lock_before(lock);
  175. arch_write_lock(&lock->raw_lock);
  176. debug_write_lock_after(lock);
  177. }
  178. int do_raw_write_trylock(rwlock_t *lock)
  179. {
  180. int ret = arch_write_trylock(&lock->raw_lock);
  181. if (ret)
  182. debug_write_lock_after(lock);
  183. #ifndef CONFIG_SMP
  184. /*
  185. * Must not happen on UP:
  186. */
  187. RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
  188. #endif
  189. return ret;
  190. }
  191. void do_raw_write_unlock(rwlock_t *lock)
  192. {
  193. debug_write_unlock(lock);
  194. arch_write_unlock(&lock->raw_lock);
  195. }