mutex-debug.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/delay.h>
  17. #include <linux/module.h>
  18. #include <linux/poison.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/debug_locks.h>
  23. #include "mutex-debug.h"
  24. /*
  25. * Must be called with lock->wait_lock held.
  26. */
  27. void debug_mutex_set_owner(struct mutex *lock, struct thread_info *new_owner)
  28. {
  29. lock->owner = new_owner;
  30. }
  31. void debug_mutex_lock_common(struct mutex *lock, struct mutex_waiter *waiter)
  32. {
  33. memset(waiter, MUTEX_DEBUG_INIT, sizeof(*waiter));
  34. waiter->magic = waiter;
  35. INIT_LIST_HEAD(&waiter->list);
  36. }
  37. void debug_mutex_wake_waiter(struct mutex *lock, struct mutex_waiter *waiter)
  38. {
  39. SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock));
  40. DEBUG_LOCKS_WARN_ON(list_empty(&lock->wait_list));
  41. DEBUG_LOCKS_WARN_ON(waiter->magic != waiter);
  42. DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
  43. }
  44. void debug_mutex_free_waiter(struct mutex_waiter *waiter)
  45. {
  46. DEBUG_LOCKS_WARN_ON(!list_empty(&waiter->list));
  47. memset(waiter, MUTEX_DEBUG_FREE, sizeof(*waiter));
  48. }
  49. void debug_mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
  50. struct thread_info *ti)
  51. {
  52. SMP_DEBUG_LOCKS_WARN_ON(!spin_is_locked(&lock->wait_lock));
  53. /* Mark the current thread as blocked on the lock: */
  54. ti->task->blocked_on = waiter;
  55. waiter->lock = lock;
  56. }
  57. void mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter,
  58. struct thread_info *ti)
  59. {
  60. DEBUG_LOCKS_WARN_ON(list_empty(&waiter->list));
  61. DEBUG_LOCKS_WARN_ON(waiter->task != ti->task);
  62. DEBUG_LOCKS_WARN_ON(ti->task->blocked_on != waiter);
  63. ti->task->blocked_on = NULL;
  64. list_del_init(&waiter->list);
  65. waiter->task = NULL;
  66. }
  67. void debug_mutex_unlock(struct mutex *lock)
  68. {
  69. if (unlikely(!debug_locks))
  70. return;
  71. DEBUG_LOCKS_WARN_ON(lock->owner != current_thread_info());
  72. DEBUG_LOCKS_WARN_ON(lock->magic != lock);
  73. DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
  74. DEBUG_LOCKS_WARN_ON(lock->owner != current_thread_info());
  75. }
  76. void debug_mutex_init(struct mutex *lock, const char *name,
  77. struct lock_class_key *key)
  78. {
  79. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  80. /*
  81. * Make sure we are not reinitializing a held lock:
  82. */
  83. debug_check_no_locks_freed((void *)lock, sizeof(*lock));
  84. lockdep_init_map(&lock->dep_map, name, key, 0);
  85. #endif
  86. lock->owner = NULL;
  87. lock->magic = lock;
  88. }
  89. /***
  90. * mutex_destroy - mark a mutex unusable
  91. * @lock: the mutex to be destroyed
  92. *
  93. * This function marks the mutex uninitialized, and any subsequent
  94. * use of the mutex is forbidden. The mutex must not be locked when
  95. * this function is called.
  96. */
  97. void fastcall mutex_destroy(struct mutex *lock)
  98. {
  99. DEBUG_LOCKS_WARN_ON(mutex_is_locked(lock));
  100. lock->magic = NULL;
  101. }
  102. EXPORT_SYMBOL_GPL(mutex_destroy);