mutex.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. * - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
  14. * from the -rt tree, where it was originally implemented for rtmutexes
  15. * by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
  16. * and Sven Dietrich.
  17. *
  18. * Also see Documentation/mutex-design.txt.
  19. */
  20. #include <linux/mutex.h>
  21. #include <linux/sched.h>
  22. #include <linux/export.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/debug_locks.h>
  26. /*
  27. * In the DEBUG case we are using the "NULL fastpath" for mutexes,
  28. * which forces all calls into the slowpath:
  29. */
  30. #ifdef CONFIG_DEBUG_MUTEXES
  31. # include "mutex-debug.h"
  32. # include <asm-generic/mutex-null.h>
  33. #else
  34. # include "mutex.h"
  35. # include <asm/mutex.h>
  36. #endif
  37. void
  38. __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
  39. {
  40. atomic_set(&lock->count, 1);
  41. spin_lock_init(&lock->wait_lock);
  42. INIT_LIST_HEAD(&lock->wait_list);
  43. mutex_clear_owner(lock);
  44. debug_mutex_init(lock, name, key);
  45. }
  46. EXPORT_SYMBOL(__mutex_init);
  47. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  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 __used noinline void __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 __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. mutex_set_owner(lock);
  86. }
  87. EXPORT_SYMBOL(mutex_lock);
  88. #endif
  89. static __used noinline void __sched __mutex_unlock_slowpath(atomic_t *lock_count);
  90. /**
  91. * mutex_unlock - release the mutex
  92. * @lock: the mutex to be released
  93. *
  94. * Unlock a mutex that has been locked by this task previously.
  95. *
  96. * This function must not be used in interrupt context. Unlocking
  97. * of a not locked mutex is not allowed.
  98. *
  99. * This function is similar to (but not equivalent to) up().
  100. */
  101. void __sched mutex_unlock(struct mutex *lock)
  102. {
  103. /*
  104. * The unlocking fastpath is the 0->1 transition from 'locked'
  105. * into 'unlocked' state:
  106. */
  107. #ifndef CONFIG_DEBUG_MUTEXES
  108. /*
  109. * When debugging is enabled we must not clear the owner before time,
  110. * the slow path will always be taken, and that clears the owner field
  111. * after verifying that it was indeed current.
  112. */
  113. mutex_clear_owner(lock);
  114. #endif
  115. __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
  116. }
  117. EXPORT_SYMBOL(mutex_unlock);
  118. /*
  119. * Lock a mutex (possibly interruptible), slowpath:
  120. */
  121. static inline int __sched
  122. __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
  123. struct lockdep_map *nest_lock, unsigned long ip)
  124. {
  125. struct task_struct *task = current;
  126. struct mutex_waiter waiter;
  127. unsigned long flags;
  128. preempt_disable();
  129. mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
  130. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  131. /*
  132. * Optimistic spinning.
  133. *
  134. * We try to spin for acquisition when we find that there are no
  135. * pending waiters and the lock owner is currently running on a
  136. * (different) CPU.
  137. *
  138. * The rationale is that if the lock owner is running, it is likely to
  139. * release the lock soon.
  140. *
  141. * Since this needs the lock owner, and this mutex implementation
  142. * doesn't track the owner atomically in the lock field, we need to
  143. * track it non-atomically.
  144. *
  145. * We can't do this for DEBUG_MUTEXES because that relies on wait_lock
  146. * to serialize everything.
  147. */
  148. for (;;) {
  149. struct task_struct *owner;
  150. /*
  151. * If there's an owner, wait for it to either
  152. * release the lock or go to sleep.
  153. */
  154. owner = ACCESS_ONCE(lock->owner);
  155. if (owner && !mutex_spin_on_owner(lock, owner))
  156. break;
  157. if (atomic_cmpxchg(&lock->count, 1, 0) == 1) {
  158. lock_acquired(&lock->dep_map, ip);
  159. mutex_set_owner(lock);
  160. preempt_enable();
  161. return 0;
  162. }
  163. /*
  164. * When there's no owner, we might have preempted between the
  165. * owner acquiring the lock and setting the owner field. If
  166. * we're an RT task that will live-lock because we won't let
  167. * the owner complete.
  168. */
  169. if (!owner && (need_resched() || rt_task(task)))
  170. break;
  171. /*
  172. * The cpu_relax() call is a compiler barrier which forces
  173. * everything in this loop to be re-loaded. We don't need
  174. * memory barriers as we'll eventually observe the right
  175. * values at the cost of a few extra spins.
  176. */
  177. arch_mutex_cpu_relax();
  178. }
  179. #endif
  180. spin_lock_mutex(&lock->wait_lock, flags);
  181. debug_mutex_lock_common(lock, &waiter);
  182. debug_mutex_add_waiter(lock, &waiter, task_thread_info(task));
  183. /* add waiting tasks to the end of the waitqueue (FIFO): */
  184. list_add_tail(&waiter.list, &lock->wait_list);
  185. waiter.task = task;
  186. if (atomic_xchg(&lock->count, -1) == 1)
  187. goto done;
  188. lock_contended(&lock->dep_map, ip);
  189. for (;;) {
  190. /*
  191. * Lets try to take the lock again - this is needed even if
  192. * we get here for the first time (shortly after failing to
  193. * acquire the lock), to make sure that we get a wakeup once
  194. * it's unlocked. Later on, if we sleep, this is the
  195. * operation that gives us the lock. We xchg it to -1, so
  196. * that when we release the lock, we properly wake up the
  197. * other waiters:
  198. */
  199. if (atomic_xchg(&lock->count, -1) == 1)
  200. break;
  201. /*
  202. * got a signal? (This code gets eliminated in the
  203. * TASK_UNINTERRUPTIBLE case.)
  204. */
  205. if (unlikely(signal_pending_state(state, task))) {
  206. mutex_remove_waiter(lock, &waiter,
  207. task_thread_info(task));
  208. mutex_release(&lock->dep_map, 1, ip);
  209. spin_unlock_mutex(&lock->wait_lock, flags);
  210. debug_mutex_free_waiter(&waiter);
  211. preempt_enable();
  212. return -EINTR;
  213. }
  214. __set_task_state(task, state);
  215. /* didn't get the lock, go to sleep: */
  216. spin_unlock_mutex(&lock->wait_lock, flags);
  217. schedule_preempt_disabled();
  218. spin_lock_mutex(&lock->wait_lock, flags);
  219. }
  220. done:
  221. lock_acquired(&lock->dep_map, ip);
  222. /* got the lock - rejoice! */
  223. mutex_remove_waiter(lock, &waiter, current_thread_info());
  224. mutex_set_owner(lock);
  225. /* set it to 0 if there are no waiters left: */
  226. if (likely(list_empty(&lock->wait_list)))
  227. atomic_set(&lock->count, 0);
  228. spin_unlock_mutex(&lock->wait_lock, flags);
  229. debug_mutex_free_waiter(&waiter);
  230. preempt_enable();
  231. return 0;
  232. }
  233. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  234. void __sched
  235. mutex_lock_nested(struct mutex *lock, unsigned int subclass)
  236. {
  237. might_sleep();
  238. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
  239. }
  240. EXPORT_SYMBOL_GPL(mutex_lock_nested);
  241. void __sched
  242. _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  243. {
  244. might_sleep();
  245. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
  246. }
  247. EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
  248. int __sched
  249. mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
  250. {
  251. might_sleep();
  252. return __mutex_lock_common(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
  253. }
  254. EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
  255. int __sched
  256. mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
  257. {
  258. might_sleep();
  259. return __mutex_lock_common(lock, TASK_INTERRUPTIBLE,
  260. subclass, NULL, _RET_IP_);
  261. }
  262. EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
  263. #endif
  264. /*
  265. * Release the lock, slowpath:
  266. */
  267. static inline void
  268. __mutex_unlock_common_slowpath(atomic_t *lock_count, int nested)
  269. {
  270. struct mutex *lock = container_of(lock_count, struct mutex, count);
  271. unsigned long flags;
  272. spin_lock_mutex(&lock->wait_lock, flags);
  273. mutex_release(&lock->dep_map, nested, _RET_IP_);
  274. debug_mutex_unlock(lock);
  275. /*
  276. * some architectures leave the lock unlocked in the fastpath failure
  277. * case, others need to leave it locked. In the later case we have to
  278. * unlock it here
  279. */
  280. if (__mutex_slowpath_needs_to_unlock())
  281. atomic_set(&lock->count, 1);
  282. if (!list_empty(&lock->wait_list)) {
  283. /* get the first entry from the wait-list: */
  284. struct mutex_waiter *waiter =
  285. list_entry(lock->wait_list.next,
  286. struct mutex_waiter, list);
  287. debug_mutex_wake_waiter(lock, waiter);
  288. wake_up_process(waiter->task);
  289. }
  290. spin_unlock_mutex(&lock->wait_lock, flags);
  291. }
  292. /*
  293. * Release the lock, slowpath:
  294. */
  295. static __used noinline void
  296. __mutex_unlock_slowpath(atomic_t *lock_count)
  297. {
  298. __mutex_unlock_common_slowpath(lock_count, 1);
  299. }
  300. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  301. /*
  302. * Here come the less common (and hence less performance-critical) APIs:
  303. * mutex_lock_interruptible() and mutex_trylock().
  304. */
  305. static noinline int __sched
  306. __mutex_lock_killable_slowpath(atomic_t *lock_count);
  307. static noinline int __sched
  308. __mutex_lock_interruptible_slowpath(atomic_t *lock_count);
  309. /**
  310. * mutex_lock_interruptible - acquire the mutex, interruptible
  311. * @lock: the mutex to be acquired
  312. *
  313. * Lock the mutex like mutex_lock(), and return 0 if the mutex has
  314. * been acquired or sleep until the mutex becomes available. If a
  315. * signal arrives while waiting for the lock then this function
  316. * returns -EINTR.
  317. *
  318. * This function is similar to (but not equivalent to) down_interruptible().
  319. */
  320. int __sched mutex_lock_interruptible(struct mutex *lock)
  321. {
  322. int ret;
  323. might_sleep();
  324. ret = __mutex_fastpath_lock_retval
  325. (&lock->count, __mutex_lock_interruptible_slowpath);
  326. if (!ret)
  327. mutex_set_owner(lock);
  328. return ret;
  329. }
  330. EXPORT_SYMBOL(mutex_lock_interruptible);
  331. int __sched mutex_lock_killable(struct mutex *lock)
  332. {
  333. int ret;
  334. might_sleep();
  335. ret = __mutex_fastpath_lock_retval
  336. (&lock->count, __mutex_lock_killable_slowpath);
  337. if (!ret)
  338. mutex_set_owner(lock);
  339. return ret;
  340. }
  341. EXPORT_SYMBOL(mutex_lock_killable);
  342. static __used noinline void __sched
  343. __mutex_lock_slowpath(atomic_t *lock_count)
  344. {
  345. struct mutex *lock = container_of(lock_count, struct mutex, count);
  346. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
  347. }
  348. static noinline int __sched
  349. __mutex_lock_killable_slowpath(atomic_t *lock_count)
  350. {
  351. struct mutex *lock = container_of(lock_count, struct mutex, count);
  352. return __mutex_lock_common(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
  353. }
  354. static noinline int __sched
  355. __mutex_lock_interruptible_slowpath(atomic_t *lock_count)
  356. {
  357. struct mutex *lock = container_of(lock_count, struct mutex, count);
  358. return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
  359. }
  360. #endif
  361. /*
  362. * Spinlock based trylock, we take the spinlock and check whether we
  363. * can get the lock:
  364. */
  365. static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
  366. {
  367. struct mutex *lock = container_of(lock_count, struct mutex, count);
  368. unsigned long flags;
  369. int prev;
  370. spin_lock_mutex(&lock->wait_lock, flags);
  371. prev = atomic_xchg(&lock->count, -1);
  372. if (likely(prev == 1)) {
  373. mutex_set_owner(lock);
  374. mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  375. }
  376. /* Set it back to 0 if there are no waiters: */
  377. if (likely(list_empty(&lock->wait_list)))
  378. atomic_set(&lock->count, 0);
  379. spin_unlock_mutex(&lock->wait_lock, flags);
  380. return prev == 1;
  381. }
  382. /**
  383. * mutex_trylock - try to acquire the mutex, without waiting
  384. * @lock: the mutex to be acquired
  385. *
  386. * Try to acquire the mutex atomically. Returns 1 if the mutex
  387. * has been acquired successfully, and 0 on contention.
  388. *
  389. * NOTE: this function follows the spin_trylock() convention, so
  390. * it is negated from the down_trylock() return values! Be careful
  391. * about this when converting semaphore users to mutexes.
  392. *
  393. * This function must not be used in interrupt context. The
  394. * mutex must be released by the same task that acquired it.
  395. */
  396. int __sched mutex_trylock(struct mutex *lock)
  397. {
  398. int ret;
  399. ret = __mutex_fastpath_trylock(&lock->count, __mutex_trylock_slowpath);
  400. if (ret)
  401. mutex_set_owner(lock);
  402. return ret;
  403. }
  404. EXPORT_SYMBOL(mutex_trylock);
  405. /**
  406. * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  407. * @cnt: the atomic which we are to dec
  408. * @lock: the mutex to return holding if we dec to 0
  409. *
  410. * return true and hold lock if we dec to 0, return false otherwise
  411. */
  412. int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  413. {
  414. /* dec if we can't possibly hit 0 */
  415. if (atomic_add_unless(cnt, -1, 1))
  416. return 0;
  417. /* we might hit 0, so take the lock */
  418. mutex_lock(lock);
  419. if (!atomic_dec_and_test(cnt)) {
  420. /* when we actually did the dec, we didn't hit 0 */
  421. mutex_unlock(lock);
  422. return 0;
  423. }
  424. /* we hit 0, and we hold the lock */
  425. return 1;
  426. }
  427. EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
  428. #ifdef CONFIG_ARCH_MSM8610
  429. struct task_struct * mutex_get_owner(struct mutex *lock)
  430. {
  431. struct task_struct *owner;
  432. unsigned long flags;
  433. spin_lock_mutex(&lock->wait_lock, flags);
  434. owner = lock->owner;
  435. spin_unlock_mutex(&lock->wait_lock, flags);
  436. return owner;
  437. }
  438. EXPORT_SYMBOL(mutex_get_owner);
  439. #endif