spinlock_debug.c 5.9 KB

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