mutex-debug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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/spinlock.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/interrupt.h>
  22. #include "mutex-debug.h"
  23. /*
  24. * We need a global lock when we walk through the multi-process
  25. * lock tree. Only used in the deadlock-debugging case.
  26. */
  27. DEFINE_SPINLOCK(debug_mutex_lock);
  28. /*
  29. * All locks held by all tasks, in a single global list:
  30. */
  31. LIST_HEAD(debug_mutex_held_locks);
  32. /*
  33. * In the debug case we carry the caller's instruction pointer into
  34. * other functions, but we dont want the function argument overhead
  35. * in the nondebug case - hence these macros:
  36. */
  37. #define __IP_DECL__ , unsigned long ip
  38. #define __IP__ , ip
  39. #define __RET_IP__ , (unsigned long)__builtin_return_address(0)
  40. /*
  41. * "mutex debugging enabled" flag. We turn it off when we detect
  42. * the first problem because we dont want to recurse back
  43. * into the tracing code when doing error printk or
  44. * executing a BUG():
  45. */
  46. int debug_mutex_on = 1;
  47. static void printk_task(struct task_struct *p)
  48. {
  49. if (p)
  50. printk("%16s:%5d [%p, %3d]", p->comm, p->pid, p, p->prio);
  51. else
  52. printk("<none>");
  53. }
  54. static void printk_ti(struct thread_info *ti)
  55. {
  56. if (ti)
  57. printk_task(ti->task);
  58. else
  59. printk("<none>");
  60. }
  61. static void printk_task_short(struct task_struct *p)
  62. {
  63. if (p)
  64. printk("%s/%d [%p, %3d]", p->comm, p->pid, p, p->prio);
  65. else
  66. printk("<none>");
  67. }
  68. static void printk_lock(struct mutex *lock, int print_owner)
  69. {
  70. printk(" [%p] {%s}\n", lock, lock->name);
  71. if (print_owner && lock->owner) {
  72. printk(".. held by: ");
  73. printk_ti(lock->owner);
  74. printk("\n");
  75. }
  76. if (lock->owner) {
  77. printk("... acquired at: ");
  78. print_symbol("%s\n", lock->acquire_ip);
  79. }
  80. }
  81. /*
  82. * printk locks held by a task:
  83. */
  84. static void show_task_locks(struct task_struct *p)
  85. {
  86. switch (p->state) {
  87. case TASK_RUNNING: printk("R"); break;
  88. case TASK_INTERRUPTIBLE: printk("S"); break;
  89. case TASK_UNINTERRUPTIBLE: printk("D"); break;
  90. case TASK_STOPPED: printk("T"); break;
  91. case EXIT_ZOMBIE: printk("Z"); break;
  92. case EXIT_DEAD: printk("X"); break;
  93. default: printk("?"); break;
  94. }
  95. printk_task(p);
  96. if (p->blocked_on) {
  97. struct mutex *lock = p->blocked_on->lock;
  98. printk(" blocked on mutex:");
  99. printk_lock(lock, 1);
  100. } else
  101. printk(" (not blocked on mutex)\n");
  102. }
  103. /*
  104. * printk all locks held in the system (if filter == NULL),
  105. * or all locks belonging to a single task (if filter != NULL):
  106. */
  107. void show_held_locks(struct task_struct *filter)
  108. {
  109. struct list_head *curr, *cursor = NULL;
  110. struct mutex *lock;
  111. struct thread_info *t;
  112. unsigned long flags;
  113. int count = 0;
  114. if (filter) {
  115. printk("------------------------------\n");
  116. printk("| showing all locks held by: | (");
  117. printk_task_short(filter);
  118. printk("):\n");
  119. printk("------------------------------\n");
  120. } else {
  121. printk("---------------------------\n");
  122. printk("| showing all locks held: |\n");
  123. printk("---------------------------\n");
  124. }
  125. /*
  126. * Play safe and acquire the global trace lock. We
  127. * cannot printk with that lock held so we iterate
  128. * very carefully:
  129. */
  130. next:
  131. debug_spin_lock_save(&debug_mutex_lock, flags);
  132. list_for_each(curr, &debug_mutex_held_locks) {
  133. if (cursor && curr != cursor)
  134. continue;
  135. lock = list_entry(curr, struct mutex, held_list);
  136. t = lock->owner;
  137. if (filter && (t != filter->thread_info))
  138. continue;
  139. count++;
  140. cursor = curr->next;
  141. debug_spin_lock_restore(&debug_mutex_lock, flags);
  142. printk("\n#%03d: ", count);
  143. printk_lock(lock, filter ? 0 : 1);
  144. goto next;
  145. }
  146. debug_spin_lock_restore(&debug_mutex_lock, flags);
  147. printk("\n");
  148. }
  149. void mutex_debug_show_all_locks(void)
  150. {
  151. struct task_struct *g, *p;
  152. int count = 10;
  153. int unlock = 1;
  154. printk("\nShowing all blocking locks in the system:\n");
  155. /*
  156. * Here we try to get the tasklist_lock as hard as possible,
  157. * if not successful after 2 seconds we ignore it (but keep
  158. * trying). This is to enable a debug printout even if a
  159. * tasklist_lock-holding task deadlocks or crashes.
  160. */
  161. retry:
  162. if (!read_trylock(&tasklist_lock)) {
  163. if (count == 10)
  164. printk("hm, tasklist_lock locked, retrying... ");
  165. if (count) {
  166. count--;
  167. printk(" #%d", 10-count);
  168. mdelay(200);
  169. goto retry;
  170. }
  171. printk(" ignoring it.\n");
  172. unlock = 0;
  173. }
  174. if (count != 10)
  175. printk(" locked it.\n");
  176. do_each_thread(g, p) {
  177. show_task_locks(p);
  178. if (!unlock)
  179. if (read_trylock(&tasklist_lock))
  180. unlock = 1;
  181. } while_each_thread(g, p);
  182. printk("\n");
  183. show_held_locks(NULL);
  184. printk("=============================================\n\n");
  185. if (unlock)
  186. read_unlock(&tasklist_lock);
  187. }
  188. static void report_deadlock(struct task_struct *task, struct mutex *lock,
  189. struct mutex *lockblk, unsigned long ip)
  190. {
  191. printk("\n%s/%d is trying to acquire this lock:\n",
  192. current->comm, current->pid);
  193. printk_lock(lock, 1);
  194. printk("... trying at: ");
  195. print_symbol("%s\n", ip);
  196. show_held_locks(current);
  197. if (lockblk) {
  198. printk("but %s/%d is deadlocking current task %s/%d!\n\n",
  199. task->comm, task->pid, current->comm, current->pid);
  200. printk("\n%s/%d is blocked on this lock:\n",
  201. task->comm, task->pid);
  202. printk_lock(lockblk, 1);
  203. show_held_locks(task);
  204. printk("\n%s/%d's [blocked] stackdump:\n\n",
  205. task->comm, task->pid);
  206. show_stack(task, NULL);
  207. }
  208. printk("\n%s/%d's [current] stackdump:\n\n",
  209. current->comm, current->pid);
  210. dump_stack();
  211. mutex_debug_show_all_locks();
  212. printk("[ turning off deadlock detection. Please report this. ]\n\n");
  213. local_irq_disable();
  214. }
  215. /*
  216. * Recursively check for mutex deadlocks:
  217. */
  218. static int check_deadlock(struct mutex *lock, int depth,
  219. struct thread_info *ti, unsigned long ip)
  220. {
  221. struct mutex *lockblk;
  222. struct task_struct *task;
  223. if (!debug_mutex_on)
  224. return 0;
  225. ti = lock->owner;
  226. if (!ti)
  227. return 0;
  228. task = ti->task;
  229. lockblk = NULL;
  230. if (task->blocked_on)
  231. lockblk = task->blocked_on->lock;
  232. /* Self-deadlock: */
  233. if (current == task) {
  234. DEBUG_OFF();
  235. if (depth)
  236. return 1;
  237. printk("\n==========================================\n");
  238. printk( "[ BUG: lock recursion deadlock detected! |\n");
  239. printk( "------------------------------------------\n");
  240. report_deadlock(task, lock, NULL, ip);
  241. return 0;
  242. }
  243. /* Ugh, something corrupted the lock data structure? */
  244. if (depth > 20) {
  245. DEBUG_OFF();
  246. printk("\n===========================================\n");
  247. printk( "[ BUG: infinite lock dependency detected!? |\n");
  248. printk( "-------------------------------------------\n");
  249. report_deadlock(task, lock, lockblk, ip);
  250. return 0;
  251. }
  252. /* Recursively check for dependencies: */
  253. if (lockblk && check_deadlock(lockblk, depth+1, ti, ip)) {
  254. printk("\n============================================\n");
  255. printk( "[ BUG: circular locking deadlock detected! ]\n");
  256. printk( "--------------------------------------------\n");
  257. report_deadlock(task, lock, lockblk, ip);
  258. return 0;
  259. }
  260. return 0;
  261. }
  262. /*
  263. * Called when a task exits, this function checks whether the
  264. * task is holding any locks, and reports the first one if so:
  265. */
  266. void mutex_debug_check_no_locks_held(struct task_struct *task)
  267. {
  268. struct list_head *curr, *next;
  269. struct thread_info *t;
  270. unsigned long flags;
  271. struct mutex *lock;
  272. if (!debug_mutex_on)
  273. return;
  274. debug_spin_lock_save(&debug_mutex_lock, flags);
  275. list_for_each_safe(curr, next, &debug_mutex_held_locks) {
  276. lock = list_entry(curr, struct mutex, held_list);
  277. t = lock->owner;
  278. if (t != task->thread_info)
  279. continue;
  280. list_del_init(curr);
  281. DEBUG_OFF();
  282. debug_spin_lock_restore(&debug_mutex_lock, flags);
  283. printk("BUG: %s/%d, lock held at task exit time!\n",
  284. task->comm, task->pid);
  285. printk_lock(lock, 1);
  286. if (lock->owner != task->thread_info)
  287. printk("exiting task is not even the owner??\n");
  288. return;
  289. }
  290. debug_spin_lock_restore(&debug_mutex_lock, flags);
  291. }
  292. /*
  293. * Called when kernel memory is freed (or unmapped), or if a mutex
  294. * is destroyed or reinitialized - this code checks whether there is
  295. * any held lock in the memory range of <from> to <to>:
  296. */
  297. void mutex_debug_check_no_locks_freed(const void *from, unsigned long len)
  298. {
  299. struct list_head *curr, *next;
  300. const void *to = from + len;
  301. unsigned long flags;
  302. struct mutex *lock;
  303. void *lock_addr;
  304. if (!debug_mutex_on)
  305. return;
  306. debug_spin_lock_save(&debug_mutex_lock, flags);
  307. list_for_each_safe(curr, next, &debug_mutex_held_locks) {
  308. lock = list_entry(curr, struct mutex, held_list);
  309. lock_addr = lock;
  310. if (lock_addr < from || lock_addr >= to)
  311. continue;
  312. list_del_init(curr);
  313. DEBUG_OFF();
  314. debug_spin_lock_restore(&debug_mutex_lock, flags);
  315. printk("BUG: %s/%d, active lock [%p(%p-%p)] freed!\n",
  316. current->comm, current->pid, lock, from, to);
  317. dump_stack();
  318. printk_lock(lock, 1);
  319. if (lock->owner != current_thread_info())
  320. printk("freeing task is not even the owner??\n");
  321. return;
  322. }
  323. debug_spin_lock_restore(&debug_mutex_lock, flags);
  324. }
  325. /*
  326. * Must be called with lock->wait_lock held.
  327. */
  328. void debug_mutex_set_owner(struct mutex *lock,
  329. struct thread_info *new_owner __IP_DECL__)
  330. {
  331. lock->owner = new_owner;
  332. DEBUG_WARN_ON(!list_empty(&lock->held_list));
  333. if (debug_mutex_on) {
  334. list_add_tail(&lock->held_list, &debug_mutex_held_locks);
  335. lock->acquire_ip = ip;
  336. }
  337. }
  338. void debug_mutex_init_waiter(struct mutex_waiter *waiter)
  339. {
  340. memset(waiter, 0x11, sizeof(*waiter));
  341. waiter->magic = waiter;
  342. INIT_LIST_HEAD(&waiter->list);
  343. }
  344. void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter)
  345. {
  346. SMP_DEBUG_WARN_ON(!spin_is_locked(&lock->wait_lock));
  347. DEBUG_WARN_ON(list_empty(&lock->wait_list));
  348. DEBUG_WARN_ON(waiter->magic != waiter);
  349. DEBUG_WARN_ON(list_empty(&waiter->list));
  350. }
  351. void debug_mutex_free_waiter(struct mutex_waiter *waiter)
  352. {
  353. DEBUG_WARN_ON(!list_empty(&waiter->list));
  354. memset(waiter, 0x22, sizeof(*waiter));
  355. }
  356. void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
  357. struct thread_info *ti __IP_DECL__)
  358. {
  359. SMP_DEBUG_WARN_ON(!spin_is_locked(&lock->wait_lock));
  360. check_deadlock(lock, 0, ti, ip);
  361. /* Mark the current thread as blocked on the lock: */
  362. ti->task->blocked_on = waiter;
  363. waiter->lock = lock;
  364. }
  365. void mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
  366. struct thread_info *ti)
  367. {
  368. DEBUG_WARN_ON(list_empty(&waiter->list));
  369. DEBUG_WARN_ON(waiter->task != ti->task);
  370. DEBUG_WARN_ON(ti->task->blocked_on != waiter);
  371. ti->task->blocked_on = NULL;
  372. list_del_init(&waiter->list);
  373. waiter->task = NULL;
  374. }
  375. void debug_mutex_unlock(struct mutex *lock)
  376. {
  377. DEBUG_WARN_ON(lock->magic != lock);
  378. DEBUG_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
  379. DEBUG_WARN_ON(lock->owner != current_thread_info());
  380. if (debug_mutex_on) {
  381. DEBUG_WARN_ON(list_empty(&lock->held_list));
  382. list_del_init(&lock->held_list);
  383. }
  384. }
  385. void debug_mutex_init(struct mutex *lock, const char *name)
  386. {
  387. /*
  388. * Make sure we are not reinitializing a held lock:
  389. */
  390. mutex_debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  391. lock->owner = NULL;
  392. INIT_LIST_HEAD(&lock->held_list);
  393. lock->name = name;
  394. lock->magic = lock;
  395. }
  396. /***
  397. * mutex_destroy - mark a mutex unusable
  398. * @lock: the mutex to be destroyed
  399. *
  400. * This function marks the mutex uninitialized, and any subsequent
  401. * use of the mutex is forbidden. The mutex must not be locked when
  402. * this function is called.
  403. */
  404. void fastcall mutex_destroy(struct mutex *lock)
  405. {
  406. DEBUG_WARN_ON(mutex_is_locked(lock));
  407. lock->magic = NULL;
  408. }
  409. EXPORT_SYMBOL_GPL(mutex_destroy);