spinlock_debug.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. #define pr_fmt(fmt) "spinlock_debug: " fmt
  9. #include <linux/spinlock.h>
  10. #include <linux/nmi.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/debug_locks.h>
  13. #include <linux/delay.h>
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/printk.h>
  17. #include <mt-plat/aee.h>
  18. #ifdef CONFIG_MTPROF
  19. #include "mt_sched_mon.h"
  20. #endif
  21. void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
  22. struct lock_class_key *key)
  23. {
  24. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  25. /*
  26. * Make sure we are not reinitializing a held lock:
  27. */
  28. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  29. lockdep_init_map(&lock->dep_map, name, key, 0);
  30. #endif
  31. lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  32. lock->magic = SPINLOCK_MAGIC;
  33. lock->owner = SPINLOCK_OWNER_INIT;
  34. lock->owner_cpu = -1;
  35. }
  36. EXPORT_SYMBOL(__raw_spin_lock_init);
  37. void __rwlock_init(rwlock_t *lock, const char *name,
  38. struct lock_class_key *key)
  39. {
  40. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  41. /*
  42. * Make sure we are not reinitializing a held lock:
  43. */
  44. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  45. lockdep_init_map(&lock->dep_map, name, key, 0);
  46. #endif
  47. lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
  48. lock->magic = RWLOCK_MAGIC;
  49. lock->owner = SPINLOCK_OWNER_INIT;
  50. lock->owner_cpu = -1;
  51. }
  52. EXPORT_SYMBOL(__rwlock_init);
  53. static void spin_dump(raw_spinlock_t *lock, const char *msg)
  54. {
  55. struct task_struct *owner = NULL;
  56. if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
  57. owner = lock->owner;
  58. printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
  59. msg, raw_smp_processor_id(),
  60. current->comm, task_pid_nr(current));
  61. pr_emerg(" lock: %pS, .magic: %08x, .owner: %s/%d, .owner_cpu: %d value:0x%08x\n",
  62. lock, lock->magic, owner ? owner->comm : "<none>", owner ? task_pid_nr(owner) : -1,
  63. lock->owner_cpu, *((unsigned int *)&lock->raw_lock));
  64. dump_stack();
  65. }
  66. static void spin_bug(raw_spinlock_t *lock, const char *msg)
  67. {
  68. char aee_str[50];
  69. /*
  70. if (!debug_locks_off())
  71. return;
  72. */
  73. spin_dump(lock, msg);
  74. snprintf(aee_str, 50, "Spinlock %s :%s\n", current->comm, msg);
  75. if ((!strcmp(msg, "bad magic")) || (!strcmp(msg, "already unlocked"))
  76. || (!strcmp(msg, "wrong owner")) || (!strcmp(msg, "wrong CPU"))) {
  77. pr_emerg("%s\n", aee_str);
  78. pr_emerg("[spindebug] maybe use an un-initial spin_lock or mem corrupt\n");
  79. pr_emerg("[spindebug] maybe already unlocked or wrong owner or wrong CPU\n");
  80. pr_emerg("[spindebug] maybe bad magic:%08x, should be %08x\n", lock->magic, SPINLOCK_MAGIC);
  81. pr_emerg(">>>>>>>>>>>>>> Let's KE <<<<<<<<<<<<<<\n");
  82. BUG_ON(1);
  83. }
  84. aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DUMMY_DUMP | DB_OPT_FTRACE,
  85. aee_str, "spinlock debugger\n");
  86. }
  87. #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
  88. static inline void
  89. debug_spin_lock_before(raw_spinlock_t *lock)
  90. {
  91. SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
  92. SPIN_BUG_ON(lock->owner == current, lock, "recursion");
  93. SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
  94. lock, "cpu recursion");
  95. }
  96. static inline void debug_spin_lock_after(raw_spinlock_t *lock)
  97. {
  98. lock->owner_cpu = raw_smp_processor_id();
  99. lock->owner = current;
  100. }
  101. static inline void debug_spin_unlock(raw_spinlock_t *lock)
  102. {
  103. SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
  104. SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
  105. SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
  106. SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
  107. lock, "wrong CPU");
  108. lock->owner = SPINLOCK_OWNER_INIT;
  109. lock->owner_cpu = -1;
  110. }
  111. /*
  112. Select appropriate loop counts to 1~2sec
  113. */
  114. #if HZ == 100
  115. #define LOOP_HZ 100 /* temp 10 */
  116. #elif HZ == 10
  117. #define LOOP_HZ 2 /* temp 2 */
  118. #else
  119. #define LOOP_HZ HZ
  120. #endif
  121. #define WARNING_TIME 1000000000 /* warning time 1 seconds */
  122. static void __spin_lock_debug(raw_spinlock_t *lock)
  123. {
  124. #ifdef CONFIG_MTK_LOCK_DEBUG
  125. u64 i;
  126. u64 loops = loops_per_jiffy * LOOP_HZ;
  127. int print_once = 1;
  128. char aee_str[50];
  129. unsigned long long t1, t2, t3;
  130. struct task_struct *owner = NULL;
  131. #ifdef CONFIG_MTPROF
  132. MT_trace_raw_spin_lock_s(lock);
  133. #endif
  134. t1 = sched_clock();
  135. t2 = t1;
  136. for (;;) {
  137. for (i = 0; i < loops; i++) {
  138. if (arch_spin_trylock(&lock->raw_lock)) {
  139. #ifdef CONFIG_MTPROF
  140. MT_trace_raw_spin_lock_e(lock);
  141. #endif
  142. return;
  143. }
  144. __delay(1);
  145. }
  146. t3 = sched_clock();
  147. if (t3 < t2)
  148. continue;
  149. else if (t3 - t2 < WARNING_TIME)
  150. continue;
  151. /* if(sched_clock() - t2 < WARNING_TIME) continue; */
  152. t2 = sched_clock();
  153. if (oops_in_progress != 0)
  154. continue; /* in exception follow, printk maybe spinlock error */
  155. if (is_logbuf_lock(lock)) /*block by logbuf lock */
  156. continue;
  157. /* lockup suspected: */
  158. if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
  159. owner = lock->owner;
  160. pr_emerg("spin time: %llu ns(start:%llu ns, lpj:%lu, LPHZ:%d), value: 0x%08x\n",
  161. sched_clock() - t1, t1, loops_per_jiffy, (int)LOOP_HZ,
  162. *((unsigned int *)&lock->raw_lock));
  163. pr_emerg("spinlock .owner: %s/%d, .owner_cpu: %d\n",
  164. owner ? owner->comm : "<none>",
  165. owner ? task_pid_nr(owner) : -1,
  166. lock->owner_cpu);
  167. if (print_once) {
  168. print_once = 0;
  169. spin_dump(lock, "lockup suspected");
  170. #ifdef CONFIG_SMP
  171. trigger_all_cpu_backtrace();
  172. #endif
  173. /* ensure debug_locks is true,then can call aee */
  174. if (debug_locks) {
  175. debug_show_all_locks();
  176. snprintf(aee_str, 50, "Spinlock lockup:%s\n", current->comm);
  177. aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DUMMY_DUMP | DB_OPT_FTRACE,
  178. aee_str, "spinlock debugger\n");
  179. }
  180. }
  181. }
  182. #else /* CONFIG_MTK_LOCK_DEBUG*/
  183. u64 i;
  184. u64 loops = loops_per_jiffy * HZ;
  185. for (i = 0; i < loops; i++) {
  186. if (arch_spin_trylock(&lock->raw_lock))
  187. return;
  188. __delay(1);
  189. }
  190. /* lockup suspected: */
  191. spin_dump(lock, "lockup suspected");
  192. #ifdef CONFIG_SMP
  193. trigger_all_cpu_backtrace();
  194. #endif
  195. /*
  196. * The trylock above was causing a livelock. Give the lower level arch
  197. * specific lock code a chance to acquire the lock. We have already
  198. * printed a warning/backtrace at this point. The non-debug arch
  199. * specific code might actually succeed in acquiring the lock. If it is
  200. * not successful, the end-result is the same - there is no forward
  201. * progress.
  202. */
  203. arch_spin_lock(&lock->raw_lock);
  204. #endif /* CONFIG_MTK_LOCK_DEBUG */
  205. }
  206. void do_raw_spin_lock(raw_spinlock_t *lock)
  207. {
  208. debug_spin_lock_before(lock);
  209. if (unlikely(!arch_spin_trylock(&lock->raw_lock)))
  210. __spin_lock_debug(lock);
  211. debug_spin_lock_after(lock);
  212. }
  213. int do_raw_spin_trylock(raw_spinlock_t *lock)
  214. {
  215. int ret = arch_spin_trylock(&lock->raw_lock);
  216. if (ret)
  217. debug_spin_lock_after(lock);
  218. #ifndef CONFIG_SMP
  219. /*
  220. * Must not happen on UP:
  221. */
  222. SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
  223. #endif
  224. return ret;
  225. }
  226. void do_raw_spin_unlock(raw_spinlock_t *lock)
  227. {
  228. debug_spin_unlock(lock);
  229. arch_spin_unlock(&lock->raw_lock);
  230. }
  231. static void rwlock_bug(rwlock_t *lock, const char *msg)
  232. {
  233. if (!debug_locks_off())
  234. return;
  235. printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
  236. msg, raw_smp_processor_id(), current->comm,
  237. task_pid_nr(current), lock);
  238. dump_stack();
  239. }
  240. #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
  241. #if 0 /* __write_lock_debug() can lock up - maybe this can too? */
  242. static void __read_lock_debug(rwlock_t *lock)
  243. {
  244. u64 i;
  245. u64 loops = loops_per_jiffy * HZ;
  246. int print_once = 1;
  247. for (;;) {
  248. for (i = 0; i < loops; i++) {
  249. if (arch_read_trylock(&lock->raw_lock))
  250. return;
  251. __delay(1);
  252. }
  253. /* lockup suspected: */
  254. if (print_once) {
  255. print_once = 0;
  256. printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
  257. "%s/%d, %p\n",
  258. raw_smp_processor_id(), current->comm,
  259. current->pid, lock);
  260. dump_stack();
  261. }
  262. }
  263. }
  264. #endif
  265. void do_raw_read_lock(rwlock_t *lock)
  266. {
  267. RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
  268. arch_read_lock(&lock->raw_lock);
  269. }
  270. int do_raw_read_trylock(rwlock_t *lock)
  271. {
  272. int ret = arch_read_trylock(&lock->raw_lock);
  273. #ifndef CONFIG_SMP
  274. /*
  275. * Must not happen on UP:
  276. */
  277. RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
  278. #endif
  279. return ret;
  280. }
  281. void do_raw_read_unlock(rwlock_t *lock)
  282. {
  283. RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
  284. arch_read_unlock(&lock->raw_lock);
  285. }
  286. static inline void debug_write_lock_before(rwlock_t *lock)
  287. {
  288. RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
  289. RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
  290. RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
  291. lock, "cpu recursion");
  292. }
  293. static inline void debug_write_lock_after(rwlock_t *lock)
  294. {
  295. lock->owner_cpu = raw_smp_processor_id();
  296. lock->owner = current;
  297. }
  298. static inline void debug_write_unlock(rwlock_t *lock)
  299. {
  300. RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
  301. RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
  302. RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
  303. lock, "wrong CPU");
  304. lock->owner = SPINLOCK_OWNER_INIT;
  305. lock->owner_cpu = -1;
  306. }
  307. #if 0 /* This can cause lockups */
  308. static void __write_lock_debug(rwlock_t *lock)
  309. {
  310. u64 i;
  311. u64 loops = loops_per_jiffy * HZ;
  312. int print_once = 1;
  313. for (;;) {
  314. for (i = 0; i < loops; i++) {
  315. if (arch_write_trylock(&lock->raw_lock))
  316. return;
  317. __delay(1);
  318. }
  319. /* lockup suspected: */
  320. if (print_once) {
  321. print_once = 0;
  322. printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
  323. "%s/%d, %p\n",
  324. raw_smp_processor_id(), current->comm,
  325. current->pid, lock);
  326. dump_stack();
  327. }
  328. }
  329. }
  330. #endif
  331. void do_raw_write_lock(rwlock_t *lock)
  332. {
  333. debug_write_lock_before(lock);
  334. arch_write_lock(&lock->raw_lock);
  335. debug_write_lock_after(lock);
  336. }
  337. int do_raw_write_trylock(rwlock_t *lock)
  338. {
  339. int ret = arch_write_trylock(&lock->raw_lock);
  340. if (ret)
  341. debug_write_lock_after(lock);
  342. #ifndef CONFIG_SMP
  343. /*
  344. * Must not happen on UP:
  345. */
  346. RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
  347. #endif
  348. return ret;
  349. }
  350. void do_raw_write_unlock(rwlock_t *lock)
  351. {
  352. debug_write_unlock(lock);
  353. arch_write_unlock(&lock->raw_lock);
  354. }