mutex.c 8.7 KB

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