mutex.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * kernel/mutex.c
  3. *
  4. * Mutexes: blocking mutual exclusion locks
  5. *
  6. * Started by Ingo Molnar:
  7. *
  8. * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  9. *
  10. * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
  11. * David Howells for suggestions and improvements.
  12. *
  13. * Also see Documentation/mutex-design.txt.
  14. */
  15. #include <linux/mutex.h>
  16. #include <linux/sched.h>
  17. #include <linux/module.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/interrupt.h>
  20. /*
  21. * In the DEBUG case we are using the "NULL fastpath" for mutexes,
  22. * which forces all calls into the slowpath:
  23. */
  24. #ifdef CONFIG_DEBUG_MUTEXES
  25. # include "mutex-debug.h"
  26. # include <asm-generic/mutex-null.h>
  27. #else
  28. # include "mutex.h"
  29. # include <asm/mutex.h>
  30. #endif
  31. /***
  32. * mutex_init - initialize the mutex
  33. * @lock: the mutex to be initialized
  34. *
  35. * Initialize the mutex to unlocked state.
  36. *
  37. * It is not allowed to initialize an already locked mutex.
  38. */
  39. void fastcall __mutex_init(struct mutex *lock, const char *name)
  40. {
  41. atomic_set(&lock->count, 1);
  42. spin_lock_init(&lock->wait_lock);
  43. INIT_LIST_HEAD(&lock->wait_list);
  44. debug_mutex_init(lock, name);
  45. }
  46. EXPORT_SYMBOL(__mutex_init);
  47. /*
  48. * We split the mutex lock/unlock logic into separate fastpath and
  49. * slowpath functions, to reduce the register pressure on the fastpath.
  50. * We also put the fastpath first in the kernel image, to make sure the
  51. * branch is predicted by the CPU as default-untaken.
  52. */
  53. static void fastcall noinline __sched
  54. __mutex_lock_slowpath(atomic_t *lock_count __IP_DECL__);
  55. /***
  56. * mutex_lock - acquire the mutex
  57. * @lock: the mutex to be acquired
  58. *
  59. * Lock the mutex exclusively for this task. If the mutex is not
  60. * available right now, it will sleep until it can get it.
  61. *
  62. * The mutex must later on be released by the same task that
  63. * acquired it. Recursive locking is not allowed. The task
  64. * may not exit without first unlocking the mutex. Also, kernel
  65. * memory where the mutex resides mutex must not be freed with
  66. * the mutex still locked. The mutex must first be initialized
  67. * (or statically defined) before it can be locked. memset()-ing
  68. * the mutex to 0 is not allowed.
  69. *
  70. * ( The CONFIG_DEBUG_MUTEXES .config option turns on debugging
  71. * checks that will enforce the restrictions and will also do
  72. * deadlock debugging. )
  73. *
  74. * This function is similar to (but not equivalent to) down().
  75. */
  76. void fastcall __sched mutex_lock(struct mutex *lock)
  77. {
  78. might_sleep();
  79. /*
  80. * The locking fastpath is the 1->0 transition from
  81. * 'unlocked' into 'locked' state.
  82. */
  83. __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
  84. }
  85. EXPORT_SYMBOL(mutex_lock);
  86. static void fastcall noinline __sched
  87. __mutex_unlock_slowpath(atomic_t *lock_count __IP_DECL__);
  88. /***
  89. * mutex_unlock - release the mutex
  90. * @lock: the mutex to be released
  91. *
  92. * Unlock a mutex that has been locked by this task previously.
  93. *
  94. * This function must not be used in interrupt context. Unlocking
  95. * of a not locked mutex is not allowed.
  96. *
  97. * This function is similar to (but not equivalent to) up().
  98. */
  99. void fastcall __sched mutex_unlock(struct mutex *lock)
  100. {
  101. /*
  102. * The unlocking fastpath is the 0->1 transition from 'locked'
  103. * into 'unlocked' state:
  104. */
  105. __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
  106. }
  107. EXPORT_SYMBOL(mutex_unlock);
  108. /*
  109. * Lock a mutex (possibly interruptible), slowpath:
  110. */
  111. static inline int __sched
  112. __mutex_lock_common(struct mutex *lock, long state __IP_DECL__)
  113. {
  114. struct task_struct *task = current;
  115. struct mutex_waiter waiter;
  116. unsigned int old_val;
  117. unsigned long flags;
  118. debug_mutex_init_waiter(&waiter);
  119. spin_lock_mutex(&lock->wait_lock, flags);
  120. debug_mutex_add_waiter(lock, &waiter, task->thread_info, ip);
  121. /* add waiting tasks to the end of the waitqueue (FIFO): */
  122. list_add_tail(&waiter.list, &lock->wait_list);
  123. waiter.task = task;
  124. for (;;) {
  125. /*
  126. * Lets try to take the lock again - this is needed even if
  127. * we get here for the first time (shortly after failing to
  128. * acquire the lock), to make sure that we get a wakeup once
  129. * it's unlocked. Later on, if we sleep, this is the
  130. * operation that gives us the lock. We xchg it to -1, so
  131. * that when we release the lock, we properly wake up the
  132. * other waiters:
  133. */
  134. old_val = atomic_xchg(&lock->count, -1);
  135. if (old_val == 1)
  136. break;
  137. /*
  138. * got a signal? (This code gets eliminated in the
  139. * TASK_UNINTERRUPTIBLE case.)
  140. */
  141. if (unlikely(state == TASK_INTERRUPTIBLE &&
  142. signal_pending(task))) {
  143. mutex_remove_waiter(lock, &waiter, task->thread_info);
  144. spin_unlock_mutex(&lock->wait_lock, flags);
  145. debug_mutex_free_waiter(&waiter);
  146. return -EINTR;
  147. }
  148. __set_task_state(task, state);
  149. /* didnt get the lock, go to sleep: */
  150. spin_unlock_mutex(&lock->wait_lock, flags);
  151. schedule();
  152. spin_lock_mutex(&lock->wait_lock, flags);
  153. }
  154. /* got the lock - rejoice! */
  155. mutex_remove_waiter(lock, &waiter, task->thread_info);
  156. debug_mutex_set_owner(lock, task->thread_info __IP__);
  157. /* set it to 0 if there are no waiters left: */
  158. if (likely(list_empty(&lock->wait_list)))
  159. atomic_set(&lock->count, 0);
  160. spin_unlock_mutex(&lock->wait_lock, flags);
  161. debug_mutex_free_waiter(&waiter);
  162. DEBUG_WARN_ON(list_empty(&lock->held_list));
  163. DEBUG_WARN_ON(lock->owner != task->thread_info);
  164. return 0;
  165. }
  166. static void fastcall noinline __sched
  167. __mutex_lock_slowpath(atomic_t *lock_count __IP_DECL__)
  168. {
  169. struct mutex *lock = container_of(lock_count, struct mutex, count);
  170. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE __IP__);
  171. }
  172. /*
  173. * Release the lock, slowpath:
  174. */
  175. static fastcall noinline void
  176. __mutex_unlock_slowpath(atomic_t *lock_count __IP_DECL__)
  177. {
  178. struct mutex *lock = container_of(lock_count, struct mutex, count);
  179. unsigned long flags;
  180. DEBUG_WARN_ON(lock->owner != current_thread_info());
  181. spin_lock_mutex(&lock->wait_lock, flags);
  182. /*
  183. * some architectures leave the lock unlocked in the fastpath failure
  184. * case, others need to leave it locked. In the later case we have to
  185. * unlock it here
  186. */
  187. if (__mutex_slowpath_needs_to_unlock())
  188. atomic_set(&lock->count, 1);
  189. debug_mutex_unlock(lock);
  190. if (!list_empty(&lock->wait_list)) {
  191. /* get the first entry from the wait-list: */
  192. struct mutex_waiter *waiter =
  193. list_entry(lock->wait_list.next,
  194. struct mutex_waiter, list);
  195. debug_mutex_wake_waiter(lock, waiter);
  196. wake_up_process(waiter->task);
  197. }
  198. debug_mutex_clear_owner(lock);
  199. spin_unlock_mutex(&lock->wait_lock, flags);
  200. }
  201. /*
  202. * Here come the less common (and hence less performance-critical) APIs:
  203. * mutex_lock_interruptible() and mutex_trylock().
  204. */
  205. static int fastcall noinline __sched
  206. __mutex_lock_interruptible_slowpath(atomic_t *lock_count __IP_DECL__);
  207. /***
  208. * mutex_lock_interruptible - acquire the mutex, interruptable
  209. * @lock: the mutex to be acquired
  210. *
  211. * Lock the mutex like mutex_lock(), and return 0 if the mutex has
  212. * been acquired or sleep until the mutex becomes available. If a
  213. * signal arrives while waiting for the lock then this function
  214. * returns -EINTR.
  215. *
  216. * This function is similar to (but not equivalent to) down_interruptible().
  217. */
  218. int fastcall __sched mutex_lock_interruptible(struct mutex *lock)
  219. {
  220. might_sleep();
  221. return __mutex_fastpath_lock_retval
  222. (&lock->count, __mutex_lock_interruptible_slowpath);
  223. }
  224. EXPORT_SYMBOL(mutex_lock_interruptible);
  225. static int fastcall noinline __sched
  226. __mutex_lock_interruptible_slowpath(atomic_t *lock_count __IP_DECL__)
  227. {
  228. struct mutex *lock = container_of(lock_count, struct mutex, count);
  229. return __mutex_lock_common(lock, TASK_INTERRUPTIBLE __IP__);
  230. }
  231. /*
  232. * Spinlock based trylock, we take the spinlock and check whether we
  233. * can get the lock:
  234. */
  235. static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
  236. {
  237. struct mutex *lock = container_of(lock_count, struct mutex, count);
  238. unsigned long flags;
  239. int prev;
  240. spin_lock_mutex(&lock->wait_lock, flags);
  241. prev = atomic_xchg(&lock->count, -1);
  242. if (likely(prev == 1))
  243. debug_mutex_set_owner(lock, current_thread_info() __RET_IP__);
  244. /* Set it back to 0 if there are no waiters: */
  245. if (likely(list_empty(&lock->wait_list)))
  246. atomic_set(&lock->count, 0);
  247. spin_unlock_mutex(&lock->wait_lock, flags);
  248. return prev == 1;
  249. }
  250. /***
  251. * mutex_trylock - try acquire the mutex, without waiting
  252. * @lock: the mutex to be acquired
  253. *
  254. * Try to acquire the mutex atomically. Returns 1 if the mutex
  255. * has been acquired successfully, and 0 on contention.
  256. *
  257. * NOTE: this function follows the spin_trylock() convention, so
  258. * it is negated to the down_trylock() return values! Be careful
  259. * about this when converting semaphore users to mutexes.
  260. *
  261. * This function must not be used in interrupt context. The
  262. * mutex must be released by the same task that acquired it.
  263. */
  264. int fastcall mutex_trylock(struct mutex *lock)
  265. {
  266. return __mutex_fastpath_trylock(&lock->count,
  267. __mutex_trylock_slowpath);
  268. }
  269. EXPORT_SYMBOL(mutex_trylock);