mutex.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * kernel/locking/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/locking/mutex-design.txt.
  19. */
  20. #include <linux/mutex.h>
  21. #include <linux/ww_mutex.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/sched/rt.h>
  24. #include <linux/sched/wake_q.h>
  25. #include <linux/sched/debug.h>
  26. #include <linux/export.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/debug_locks.h>
  30. #include <linux/osq_lock.h>
  31. #ifdef CONFIG_DEBUG_MUTEXES
  32. # include "mutex-debug.h"
  33. #else
  34. # include "mutex.h"
  35. #endif
  36. void
  37. __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
  38. {
  39. atomic_long_set(&lock->owner, 0);
  40. spin_lock_init(&lock->wait_lock);
  41. INIT_LIST_HEAD(&lock->wait_list);
  42. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  43. osq_lock_init(&lock->osq);
  44. #endif
  45. debug_mutex_init(lock, name, key);
  46. }
  47. EXPORT_SYMBOL(__mutex_init);
  48. /*
  49. * @owner: contains: 'struct task_struct *' to the current lock owner,
  50. * NULL means not owned. Since task_struct pointers are aligned at
  51. * at least L1_CACHE_BYTES, we have low bits to store extra state.
  52. *
  53. * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
  54. * Bit1 indicates unlock needs to hand the lock to the top-waiter
  55. * Bit2 indicates handoff has been done and we're waiting for pickup.
  56. */
  57. #define MUTEX_FLAG_WAITERS 0x01
  58. #define MUTEX_FLAG_HANDOFF 0x02
  59. #define MUTEX_FLAG_PICKUP 0x04
  60. #define MUTEX_FLAGS 0x07
  61. static inline struct task_struct *__owner_task(unsigned long owner)
  62. {
  63. return (struct task_struct *)(owner & ~MUTEX_FLAGS);
  64. }
  65. static inline unsigned long __owner_flags(unsigned long owner)
  66. {
  67. return owner & MUTEX_FLAGS;
  68. }
  69. /*
  70. * Trylock variant that retuns the owning task on failure.
  71. */
  72. static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
  73. {
  74. unsigned long owner, curr = (unsigned long)current;
  75. owner = atomic_long_read(&lock->owner);
  76. for (;;) { /* must loop, can race against a flag */
  77. unsigned long old, flags = __owner_flags(owner);
  78. unsigned long task = owner & ~MUTEX_FLAGS;
  79. if (task) {
  80. if (likely(task != curr))
  81. break;
  82. if (likely(!(flags & MUTEX_FLAG_PICKUP)))
  83. break;
  84. flags &= ~MUTEX_FLAG_PICKUP;
  85. } else {
  86. #ifdef CONFIG_DEBUG_MUTEXES
  87. DEBUG_LOCKS_WARN_ON(flags & MUTEX_FLAG_PICKUP);
  88. #endif
  89. }
  90. /*
  91. * We set the HANDOFF bit, we must make sure it doesn't live
  92. * past the point where we acquire it. This would be possible
  93. * if we (accidentally) set the bit on an unlocked mutex.
  94. */
  95. flags &= ~MUTEX_FLAG_HANDOFF;
  96. old = atomic_long_cmpxchg_acquire(&lock->owner, owner, curr | flags);
  97. if (old == owner)
  98. return NULL;
  99. owner = old;
  100. }
  101. return __owner_task(owner);
  102. }
  103. /*
  104. * Actual trylock that will work on any unlocked state.
  105. */
  106. static inline bool __mutex_trylock(struct mutex *lock)
  107. {
  108. return !__mutex_trylock_or_owner(lock);
  109. }
  110. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  111. /*
  112. * Lockdep annotations are contained to the slow paths for simplicity.
  113. * There is nothing that would stop spreading the lockdep annotations outwards
  114. * except more code.
  115. */
  116. /*
  117. * Optimistic trylock that only works in the uncontended case. Make sure to
  118. * follow with a __mutex_trylock() before failing.
  119. */
  120. static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
  121. {
  122. unsigned long curr = (unsigned long)current;
  123. unsigned long zero = 0UL;
  124. if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
  125. return true;
  126. return false;
  127. }
  128. static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
  129. {
  130. unsigned long curr = (unsigned long)current;
  131. if (atomic_long_cmpxchg_release(&lock->owner, curr, 0UL) == curr)
  132. return true;
  133. return false;
  134. }
  135. #endif
  136. static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
  137. {
  138. atomic_long_or(flag, &lock->owner);
  139. }
  140. static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
  141. {
  142. atomic_long_andnot(flag, &lock->owner);
  143. }
  144. static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter)
  145. {
  146. return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter;
  147. }
  148. /*
  149. * Add @waiter to a given location in the lock wait_list and set the
  150. * FLAG_WAITERS flag if it's the first waiter.
  151. */
  152. static void __sched
  153. __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
  154. struct list_head *list)
  155. {
  156. debug_mutex_add_waiter(lock, waiter, current);
  157. list_add_tail(&waiter->list, list);
  158. if (__mutex_waiter_is_first(lock, waiter))
  159. __mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
  160. }
  161. /*
  162. * Give up ownership to a specific task, when @task = NULL, this is equivalent
  163. * to a regular unlock. Sets PICKUP on a handoff, clears HANDOF, preserves
  164. * WAITERS. Provides RELEASE semantics like a regular unlock, the
  165. * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
  166. */
  167. static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
  168. {
  169. unsigned long owner = atomic_long_read(&lock->owner);
  170. for (;;) {
  171. unsigned long old, new;
  172. #ifdef CONFIG_DEBUG_MUTEXES
  173. DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current);
  174. DEBUG_LOCKS_WARN_ON(owner & MUTEX_FLAG_PICKUP);
  175. #endif
  176. new = (owner & MUTEX_FLAG_WAITERS);
  177. new |= (unsigned long)task;
  178. if (task)
  179. new |= MUTEX_FLAG_PICKUP;
  180. old = atomic_long_cmpxchg_release(&lock->owner, owner, new);
  181. if (old == owner)
  182. break;
  183. owner = old;
  184. }
  185. }
  186. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  187. /*
  188. * We split the mutex lock/unlock logic into separate fastpath and
  189. * slowpath functions, to reduce the register pressure on the fastpath.
  190. * We also put the fastpath first in the kernel image, to make sure the
  191. * branch is predicted by the CPU as default-untaken.
  192. */
  193. static void __sched __mutex_lock_slowpath(struct mutex *lock);
  194. /**
  195. * mutex_lock - acquire the mutex
  196. * @lock: the mutex to be acquired
  197. *
  198. * Lock the mutex exclusively for this task. If the mutex is not
  199. * available right now, it will sleep until it can get it.
  200. *
  201. * The mutex must later on be released by the same task that
  202. * acquired it. Recursive locking is not allowed. The task
  203. * may not exit without first unlocking the mutex. Also, kernel
  204. * memory where the mutex resides must not be freed with
  205. * the mutex still locked. The mutex must first be initialized
  206. * (or statically defined) before it can be locked. memset()-ing
  207. * the mutex to 0 is not allowed.
  208. *
  209. * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging
  210. * checks that will enforce the restrictions and will also do
  211. * deadlock debugging)
  212. *
  213. * This function is similar to (but not equivalent to) down().
  214. */
  215. void __sched mutex_lock(struct mutex *lock)
  216. {
  217. might_sleep();
  218. if (!__mutex_trylock_fast(lock))
  219. __mutex_lock_slowpath(lock);
  220. }
  221. EXPORT_SYMBOL(mutex_lock);
  222. #endif
  223. /*
  224. * Wait-Die:
  225. * The newer transactions are killed when:
  226. * It (the new transaction) makes a request for a lock being held
  227. * by an older transaction.
  228. *
  229. * Wound-Wait:
  230. * The newer transactions are wounded when:
  231. * An older transaction makes a request for a lock being held by
  232. * the newer transaction.
  233. */
  234. /*
  235. * Associate the ww_mutex @ww with the context @ww_ctx under which we acquired
  236. * it.
  237. */
  238. static __always_inline void
  239. ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
  240. {
  241. #ifdef CONFIG_DEBUG_MUTEXES
  242. /*
  243. * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  244. * but released with a normal mutex_unlock in this call.
  245. *
  246. * This should never happen, always use ww_mutex_unlock.
  247. */
  248. DEBUG_LOCKS_WARN_ON(ww->ctx);
  249. /*
  250. * Not quite done after calling ww_acquire_done() ?
  251. */
  252. DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  253. if (ww_ctx->contending_lock) {
  254. /*
  255. * After -EDEADLK you tried to
  256. * acquire a different ww_mutex? Bad!
  257. */
  258. DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  259. /*
  260. * You called ww_mutex_lock after receiving -EDEADLK,
  261. * but 'forgot' to unlock everything else first?
  262. */
  263. DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  264. ww_ctx->contending_lock = NULL;
  265. }
  266. /*
  267. * Naughty, using a different class will lead to undefined behavior!
  268. */
  269. DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  270. #endif
  271. ww_ctx->acquired++;
  272. ww->ctx = ww_ctx;
  273. }
  274. /*
  275. * Determine if context @a is 'after' context @b. IOW, @a is a younger
  276. * transaction than @b and depending on algorithm either needs to wait for
  277. * @b or die.
  278. */
  279. static inline bool __sched
  280. __ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
  281. {
  282. return (signed long)(a->stamp - b->stamp) > 0;
  283. }
  284. /*
  285. * Wait-Die; wake a younger waiter context (when locks held) such that it can
  286. * die.
  287. *
  288. * Among waiters with context, only the first one can have other locks acquired
  289. * already (ctx->acquired > 0), because __ww_mutex_add_waiter() and
  290. * __ww_mutex_check_kill() wake any but the earliest context.
  291. */
  292. static bool __sched
  293. __ww_mutex_die(struct mutex *lock, struct mutex_waiter *waiter,
  294. struct ww_acquire_ctx *ww_ctx)
  295. {
  296. if (!ww_ctx->is_wait_die)
  297. return false;
  298. if (waiter->ww_ctx->acquired > 0 &&
  299. __ww_ctx_stamp_after(waiter->ww_ctx, ww_ctx)) {
  300. debug_mutex_wake_waiter(lock, waiter);
  301. wake_up_process(waiter->task);
  302. }
  303. return true;
  304. }
  305. /*
  306. * Wound-Wait; wound a younger @hold_ctx if it holds the lock.
  307. *
  308. * Wound the lock holder if there are waiters with older transactions than
  309. * the lock holders. Even if multiple waiters may wound the lock holder,
  310. * it's sufficient that only one does.
  311. */
  312. static bool __ww_mutex_wound(struct mutex *lock,
  313. struct ww_acquire_ctx *ww_ctx,
  314. struct ww_acquire_ctx *hold_ctx)
  315. {
  316. struct task_struct *owner = __mutex_owner(lock);
  317. lockdep_assert_held(&lock->wait_lock);
  318. /*
  319. * Possible through __ww_mutex_add_waiter() when we race with
  320. * ww_mutex_set_context_fastpath(). In that case we'll get here again
  321. * through __ww_mutex_check_waiters().
  322. */
  323. if (!hold_ctx)
  324. return false;
  325. /*
  326. * Can have !owner because of __mutex_unlock_slowpath(), but if owner,
  327. * it cannot go away because we'll have FLAG_WAITERS set and hold
  328. * wait_lock.
  329. */
  330. if (!owner)
  331. return false;
  332. if (ww_ctx->acquired > 0 && __ww_ctx_stamp_after(hold_ctx, ww_ctx)) {
  333. hold_ctx->wounded = 1;
  334. /*
  335. * wake_up_process() paired with set_current_state()
  336. * inserts sufficient barriers to make sure @owner either sees
  337. * it's wounded in __ww_mutex_check_kill() or has a
  338. * wakeup pending to re-read the wounded state.
  339. */
  340. if (owner != current)
  341. wake_up_process(owner);
  342. return true;
  343. }
  344. return false;
  345. }
  346. /*
  347. * We just acquired @lock under @ww_ctx, if there are later contexts waiting
  348. * behind us on the wait-list, check if they need to die, or wound us.
  349. *
  350. * See __ww_mutex_add_waiter() for the list-order construction; basically the
  351. * list is ordered by stamp, smallest (oldest) first.
  352. *
  353. * This relies on never mixing wait-die/wound-wait on the same wait-list;
  354. * which is currently ensured by that being a ww_class property.
  355. *
  356. * The current task must not be on the wait list.
  357. */
  358. static void __sched
  359. __ww_mutex_check_waiters(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
  360. {
  361. struct mutex_waiter *cur;
  362. lockdep_assert_held(&lock->wait_lock);
  363. list_for_each_entry(cur, &lock->wait_list, list) {
  364. if (!cur->ww_ctx)
  365. continue;
  366. if (__ww_mutex_die(lock, cur, ww_ctx) ||
  367. __ww_mutex_wound(lock, cur->ww_ctx, ww_ctx))
  368. break;
  369. }
  370. }
  371. /*
  372. * After acquiring lock with fastpath, where we do not hold wait_lock, set ctx
  373. * and wake up any waiters so they can recheck.
  374. */
  375. static __always_inline void
  376. ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  377. {
  378. ww_mutex_lock_acquired(lock, ctx);
  379. /*
  380. * The lock->ctx update should be visible on all cores before
  381. * the WAITERS check is done, otherwise contended waiters might be
  382. * missed. The contended waiters will either see ww_ctx == NULL
  383. * and keep spinning, or it will acquire wait_lock, add itself
  384. * to waiter list and sleep.
  385. */
  386. smp_mb(); /* See comments above and below. */
  387. /*
  388. * [W] ww->ctx = ctx [W] MUTEX_FLAG_WAITERS
  389. * MB MB
  390. * [R] MUTEX_FLAG_WAITERS [R] ww->ctx
  391. *
  392. * The memory barrier above pairs with the memory barrier in
  393. * __ww_mutex_add_waiter() and makes sure we either observe ww->ctx
  394. * and/or !empty list.
  395. */
  396. if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS)))
  397. return;
  398. /*
  399. * Uh oh, we raced in fastpath, check if any of the waiters need to
  400. * die or wound us.
  401. */
  402. spin_lock(&lock->base.wait_lock);
  403. __ww_mutex_check_waiters(&lock->base, ctx);
  404. spin_unlock(&lock->base.wait_lock);
  405. }
  406. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  407. static inline
  408. bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
  409. struct mutex_waiter *waiter)
  410. {
  411. struct ww_mutex *ww;
  412. ww = container_of(lock, struct ww_mutex, base);
  413. /*
  414. * If ww->ctx is set the contents are undefined, only
  415. * by acquiring wait_lock there is a guarantee that
  416. * they are not invalid when reading.
  417. *
  418. * As such, when deadlock detection needs to be
  419. * performed the optimistic spinning cannot be done.
  420. *
  421. * Check this in every inner iteration because we may
  422. * be racing against another thread's ww_mutex_lock.
  423. */
  424. if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
  425. return false;
  426. /*
  427. * If we aren't on the wait list yet, cancel the spin
  428. * if there are waiters. We want to avoid stealing the
  429. * lock from a waiter with an earlier stamp, since the
  430. * other thread may already own a lock that we also
  431. * need.
  432. */
  433. if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
  434. return false;
  435. /*
  436. * Similarly, stop spinning if we are no longer the
  437. * first waiter.
  438. */
  439. if (waiter && !__mutex_waiter_is_first(lock, waiter))
  440. return false;
  441. return true;
  442. }
  443. /*
  444. * Look out! "owner" is an entirely speculative pointer access and not
  445. * reliable.
  446. *
  447. * "noinline" so that this function shows up on perf profiles.
  448. */
  449. static noinline
  450. bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
  451. struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
  452. {
  453. bool ret = true;
  454. rcu_read_lock();
  455. while (__mutex_owner(lock) == owner) {
  456. /*
  457. * Ensure we emit the owner->on_cpu, dereference _after_
  458. * checking lock->owner still matches owner. If that fails,
  459. * owner might point to freed memory. If it still matches,
  460. * the rcu_read_lock() ensures the memory stays valid.
  461. */
  462. barrier();
  463. /*
  464. * Use vcpu_is_preempted to detect lock holder preemption issue.
  465. */
  466. if (!owner->on_cpu || need_resched() ||
  467. vcpu_is_preempted(task_cpu(owner))) {
  468. ret = false;
  469. break;
  470. }
  471. if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
  472. ret = false;
  473. break;
  474. }
  475. cpu_relax();
  476. }
  477. rcu_read_unlock();
  478. return ret;
  479. }
  480. /*
  481. * Initial check for entering the mutex spinning loop
  482. */
  483. static inline int mutex_can_spin_on_owner(struct mutex *lock)
  484. {
  485. struct task_struct *owner;
  486. int retval = 1;
  487. if (need_resched())
  488. return 0;
  489. rcu_read_lock();
  490. owner = __mutex_owner(lock);
  491. /*
  492. * As lock holder preemption issue, we both skip spinning if task is not
  493. * on cpu or its cpu is preempted
  494. */
  495. if (owner)
  496. retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
  497. rcu_read_unlock();
  498. /*
  499. * If lock->owner is not set, the mutex has been released. Return true
  500. * such that we'll trylock in the spin path, which is a faster option
  501. * than the blocking slow path.
  502. */
  503. return retval;
  504. }
  505. /*
  506. * Optimistic spinning.
  507. *
  508. * We try to spin for acquisition when we find that the lock owner
  509. * is currently running on a (different) CPU and while we don't
  510. * need to reschedule. The rationale is that if the lock owner is
  511. * running, it is likely to release the lock soon.
  512. *
  513. * The mutex spinners are queued up using MCS lock so that only one
  514. * spinner can compete for the mutex. However, if mutex spinning isn't
  515. * going to happen, there is no point in going through the lock/unlock
  516. * overhead.
  517. *
  518. * Returns true when the lock was taken, otherwise false, indicating
  519. * that we need to jump to the slowpath and sleep.
  520. *
  521. * The waiter flag is set to true if the spinner is a waiter in the wait
  522. * queue. The waiter-spinner will spin on the lock directly and concurrently
  523. * with the spinner at the head of the OSQ, if present, until the owner is
  524. * changed to itself.
  525. */
  526. static __always_inline bool
  527. mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
  528. const bool use_ww_ctx, struct mutex_waiter *waiter)
  529. {
  530. if (!waiter) {
  531. /*
  532. * The purpose of the mutex_can_spin_on_owner() function is
  533. * to eliminate the overhead of osq_lock() and osq_unlock()
  534. * in case spinning isn't possible. As a waiter-spinner
  535. * is not going to take OSQ lock anyway, there is no need
  536. * to call mutex_can_spin_on_owner().
  537. */
  538. if (!mutex_can_spin_on_owner(lock))
  539. goto fail;
  540. /*
  541. * In order to avoid a stampede of mutex spinners trying to
  542. * acquire the mutex all at once, the spinners need to take a
  543. * MCS (queued) lock first before spinning on the owner field.
  544. */
  545. if (!osq_lock(&lock->osq))
  546. goto fail;
  547. }
  548. for (;;) {
  549. struct task_struct *owner;
  550. /* Try to acquire the mutex... */
  551. owner = __mutex_trylock_or_owner(lock);
  552. if (!owner)
  553. break;
  554. /*
  555. * There's an owner, wait for it to either
  556. * release the lock or go to sleep.
  557. */
  558. if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
  559. goto fail_unlock;
  560. /*
  561. * The cpu_relax() call is a compiler barrier which forces
  562. * everything in this loop to be re-loaded. We don't need
  563. * memory barriers as we'll eventually observe the right
  564. * values at the cost of a few extra spins.
  565. */
  566. cpu_relax();
  567. }
  568. if (!waiter)
  569. osq_unlock(&lock->osq);
  570. return true;
  571. fail_unlock:
  572. if (!waiter)
  573. osq_unlock(&lock->osq);
  574. fail:
  575. /*
  576. * If we fell out of the spin path because of need_resched(),
  577. * reschedule now, before we try-lock the mutex. This avoids getting
  578. * scheduled out right after we obtained the mutex.
  579. */
  580. if (need_resched()) {
  581. /*
  582. * We _should_ have TASK_RUNNING here, but just in case
  583. * we do not, make it so, otherwise we might get stuck.
  584. */
  585. __set_current_state(TASK_RUNNING);
  586. schedule_preempt_disabled();
  587. }
  588. return false;
  589. }
  590. #else
  591. static __always_inline bool
  592. mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
  593. const bool use_ww_ctx, struct mutex_waiter *waiter)
  594. {
  595. return false;
  596. }
  597. #endif
  598. static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
  599. /**
  600. * mutex_unlock - release the mutex
  601. * @lock: the mutex to be released
  602. *
  603. * Unlock a mutex that has been locked by this task previously.
  604. *
  605. * This function must not be used in interrupt context. Unlocking
  606. * of a not locked mutex is not allowed.
  607. *
  608. * This function is similar to (but not equivalent to) up().
  609. */
  610. void __sched mutex_unlock(struct mutex *lock)
  611. {
  612. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  613. if (__mutex_unlock_fast(lock))
  614. return;
  615. #endif
  616. __mutex_unlock_slowpath(lock, _RET_IP_);
  617. }
  618. EXPORT_SYMBOL(mutex_unlock);
  619. /**
  620. * ww_mutex_unlock - release the w/w mutex
  621. * @lock: the mutex to be released
  622. *
  623. * Unlock a mutex that has been locked by this task previously with any of the
  624. * ww_mutex_lock* functions (with or without an acquire context). It is
  625. * forbidden to release the locks after releasing the acquire context.
  626. *
  627. * This function must not be used in interrupt context. Unlocking
  628. * of a unlocked mutex is not allowed.
  629. */
  630. void __sched ww_mutex_unlock(struct ww_mutex *lock)
  631. {
  632. /*
  633. * The unlocking fastpath is the 0->1 transition from 'locked'
  634. * into 'unlocked' state:
  635. */
  636. if (lock->ctx) {
  637. #ifdef CONFIG_DEBUG_MUTEXES
  638. DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  639. #endif
  640. if (lock->ctx->acquired > 0)
  641. lock->ctx->acquired--;
  642. lock->ctx = NULL;
  643. }
  644. mutex_unlock(&lock->base);
  645. }
  646. EXPORT_SYMBOL(ww_mutex_unlock);
  647. static __always_inline int __sched
  648. __ww_mutex_kill(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
  649. {
  650. if (ww_ctx->acquired > 0) {
  651. #ifdef CONFIG_DEBUG_MUTEXES
  652. struct ww_mutex *ww;
  653. ww = container_of(lock, struct ww_mutex, base);
  654. DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock);
  655. ww_ctx->contending_lock = ww;
  656. #endif
  657. return -EDEADLK;
  658. }
  659. return 0;
  660. }
  661. /*
  662. * Check the wound condition for the current lock acquire.
  663. *
  664. * Wound-Wait: If we're wounded, kill ourself.
  665. *
  666. * Wait-Die: If we're trying to acquire a lock already held by an older
  667. * context, kill ourselves.
  668. *
  669. * Since __ww_mutex_add_waiter() orders the wait-list on stamp, we only have to
  670. * look at waiters before us in the wait-list.
  671. */
  672. static inline int __sched
  673. __ww_mutex_check_kill(struct mutex *lock, struct mutex_waiter *waiter,
  674. struct ww_acquire_ctx *ctx)
  675. {
  676. struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
  677. struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);
  678. struct mutex_waiter *cur;
  679. if (ctx->acquired == 0)
  680. return 0;
  681. if (!ctx->is_wait_die) {
  682. if (ctx->wounded)
  683. return __ww_mutex_kill(lock, ctx);
  684. return 0;
  685. }
  686. if (hold_ctx && __ww_ctx_stamp_after(ctx, hold_ctx))
  687. return __ww_mutex_kill(lock, ctx);
  688. /*
  689. * If there is a waiter in front of us that has a context, then its
  690. * stamp is earlier than ours and we must kill ourself.
  691. */
  692. cur = waiter;
  693. list_for_each_entry_continue_reverse(cur, &lock->wait_list, list) {
  694. if (!cur->ww_ctx)
  695. continue;
  696. return __ww_mutex_kill(lock, ctx);
  697. }
  698. return 0;
  699. }
  700. /*
  701. * Add @waiter to the wait-list, keep the wait-list ordered by stamp, smallest
  702. * first. Such that older contexts are preferred to acquire the lock over
  703. * younger contexts.
  704. *
  705. * Waiters without context are interspersed in FIFO order.
  706. *
  707. * Furthermore, for Wait-Die kill ourself immediately when possible (there are
  708. * older contexts already waiting) to avoid unnecessary waiting and for
  709. * Wound-Wait ensure we wound the owning context when it is younger.
  710. */
  711. static inline int __sched
  712. __ww_mutex_add_waiter(struct mutex_waiter *waiter,
  713. struct mutex *lock,
  714. struct ww_acquire_ctx *ww_ctx)
  715. {
  716. struct mutex_waiter *cur;
  717. struct list_head *pos;
  718. bool is_wait_die;
  719. if (!ww_ctx) {
  720. __mutex_add_waiter(lock, waiter, &lock->wait_list);
  721. return 0;
  722. }
  723. is_wait_die = ww_ctx->is_wait_die;
  724. /*
  725. * Add the waiter before the first waiter with a higher stamp.
  726. * Waiters without a context are skipped to avoid starving
  727. * them. Wait-Die waiters may die here. Wound-Wait waiters
  728. * never die here, but they are sorted in stamp order and
  729. * may wound the lock holder.
  730. */
  731. pos = &lock->wait_list;
  732. list_for_each_entry_reverse(cur, &lock->wait_list, list) {
  733. if (!cur->ww_ctx)
  734. continue;
  735. if (__ww_ctx_stamp_after(ww_ctx, cur->ww_ctx)) {
  736. /*
  737. * Wait-Die: if we find an older context waiting, there
  738. * is no point in queueing behind it, as we'd have to
  739. * die the moment it would acquire the lock.
  740. */
  741. if (is_wait_die) {
  742. int ret = __ww_mutex_kill(lock, ww_ctx);
  743. if (ret)
  744. return ret;
  745. }
  746. break;
  747. }
  748. pos = &cur->list;
  749. /* Wait-Die: ensure younger waiters die. */
  750. __ww_mutex_die(lock, cur, ww_ctx);
  751. }
  752. __mutex_add_waiter(lock, waiter, pos);
  753. /*
  754. * Wound-Wait: if we're blocking on a mutex owned by a younger context,
  755. * wound that such that we might proceed.
  756. */
  757. if (!is_wait_die) {
  758. struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
  759. /*
  760. * See ww_mutex_set_context_fastpath(). Orders setting
  761. * MUTEX_FLAG_WAITERS vs the ww->ctx load,
  762. * such that either we or the fastpath will wound @ww->ctx.
  763. */
  764. smp_mb();
  765. __ww_mutex_wound(lock, ww_ctx, ww->ctx);
  766. }
  767. return 0;
  768. }
  769. /*
  770. * Lock a mutex (possibly interruptible), slowpath:
  771. */
  772. static __always_inline int __sched
  773. __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
  774. struct lockdep_map *nest_lock, unsigned long ip,
  775. struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
  776. {
  777. struct mutex_waiter waiter;
  778. bool first = false;
  779. struct ww_mutex *ww;
  780. int ret;
  781. might_sleep();
  782. ww = container_of(lock, struct ww_mutex, base);
  783. if (use_ww_ctx && ww_ctx) {
  784. if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
  785. return -EALREADY;
  786. /*
  787. * Reset the wounded flag after a kill. No other process can
  788. * race and wound us here since they can't have a valid owner
  789. * pointer if we don't have any locks held.
  790. */
  791. if (ww_ctx->acquired == 0)
  792. ww_ctx->wounded = 0;
  793. }
  794. preempt_disable();
  795. mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
  796. if (__mutex_trylock(lock) ||
  797. mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, NULL)) {
  798. /* got the lock, yay! */
  799. lock_acquired(&lock->dep_map, ip);
  800. if (use_ww_ctx && ww_ctx)
  801. ww_mutex_set_context_fastpath(ww, ww_ctx);
  802. preempt_enable();
  803. return 0;
  804. }
  805. spin_lock(&lock->wait_lock);
  806. /*
  807. * After waiting to acquire the wait_lock, try again.
  808. */
  809. if (__mutex_trylock(lock)) {
  810. if (use_ww_ctx && ww_ctx)
  811. __ww_mutex_check_waiters(lock, ww_ctx);
  812. goto skip_wait;
  813. }
  814. debug_mutex_lock_common(lock, &waiter);
  815. lock_contended(&lock->dep_map, ip);
  816. if (!use_ww_ctx) {
  817. /* add waiting tasks to the end of the waitqueue (FIFO): */
  818. __mutex_add_waiter(lock, &waiter, &lock->wait_list);
  819. #ifdef CONFIG_DEBUG_MUTEXES
  820. waiter.ww_ctx = MUTEX_POISON_WW_CTX;
  821. #endif
  822. } else {
  823. /*
  824. * Add in stamp order, waking up waiters that must kill
  825. * themselves.
  826. */
  827. ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
  828. if (ret)
  829. goto err_early_kill;
  830. waiter.ww_ctx = ww_ctx;
  831. }
  832. waiter.task = current;
  833. set_current_state(state);
  834. for (;;) {
  835. /*
  836. * Once we hold wait_lock, we're serialized against
  837. * mutex_unlock() handing the lock off to us, do a trylock
  838. * before testing the error conditions to make sure we pick up
  839. * the handoff.
  840. */
  841. if (__mutex_trylock(lock))
  842. goto acquired;
  843. /*
  844. * Check for signals and kill conditions while holding
  845. * wait_lock. This ensures the lock cancellation is ordered
  846. * against mutex_unlock() and wake-ups do not go missing.
  847. */
  848. if (unlikely(signal_pending_state(state, current))) {
  849. ret = -EINTR;
  850. goto err;
  851. }
  852. if (use_ww_ctx && ww_ctx) {
  853. ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
  854. if (ret)
  855. goto err;
  856. }
  857. spin_unlock(&lock->wait_lock);
  858. schedule_preempt_disabled();
  859. /*
  860. * ww_mutex needs to always recheck its position since its waiter
  861. * list is not FIFO ordered.
  862. */
  863. if ((use_ww_ctx && ww_ctx) || !first) {
  864. first = __mutex_waiter_is_first(lock, &waiter);
  865. if (first)
  866. __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
  867. }
  868. set_current_state(state);
  869. /*
  870. * Here we order against unlock; we must either see it change
  871. * state back to RUNNING and fall through the next schedule(),
  872. * or we must see its unlock and acquire.
  873. */
  874. if (__mutex_trylock(lock) ||
  875. (first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, &waiter)))
  876. break;
  877. spin_lock(&lock->wait_lock);
  878. }
  879. spin_lock(&lock->wait_lock);
  880. acquired:
  881. __set_current_state(TASK_RUNNING);
  882. if (use_ww_ctx && ww_ctx) {
  883. /*
  884. * Wound-Wait; we stole the lock (!first_waiter), check the
  885. * waiters as anyone might want to wound us.
  886. */
  887. if (!ww_ctx->is_wait_die &&
  888. !__mutex_waiter_is_first(lock, &waiter))
  889. __ww_mutex_check_waiters(lock, ww_ctx);
  890. }
  891. mutex_remove_waiter(lock, &waiter, current);
  892. if (likely(list_empty(&lock->wait_list)))
  893. __mutex_clear_flag(lock, MUTEX_FLAGS);
  894. debug_mutex_free_waiter(&waiter);
  895. skip_wait:
  896. /* got the lock - cleanup and rejoice! */
  897. lock_acquired(&lock->dep_map, ip);
  898. if (use_ww_ctx && ww_ctx)
  899. ww_mutex_lock_acquired(ww, ww_ctx);
  900. spin_unlock(&lock->wait_lock);
  901. preempt_enable();
  902. return 0;
  903. err:
  904. __set_current_state(TASK_RUNNING);
  905. mutex_remove_waiter(lock, &waiter, current);
  906. err_early_kill:
  907. spin_unlock(&lock->wait_lock);
  908. debug_mutex_free_waiter(&waiter);
  909. mutex_release(&lock->dep_map, 1, ip);
  910. preempt_enable();
  911. return ret;
  912. }
  913. static int __sched
  914. __mutex_lock(struct mutex *lock, long state, unsigned int subclass,
  915. struct lockdep_map *nest_lock, unsigned long ip)
  916. {
  917. return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
  918. }
  919. static int __sched
  920. __ww_mutex_lock(struct mutex *lock, long state, unsigned int subclass,
  921. struct lockdep_map *nest_lock, unsigned long ip,
  922. struct ww_acquire_ctx *ww_ctx)
  923. {
  924. return __mutex_lock_common(lock, state, subclass, nest_lock, ip, ww_ctx, true);
  925. }
  926. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  927. void __sched
  928. mutex_lock_nested(struct mutex *lock, unsigned int subclass)
  929. {
  930. __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
  931. }
  932. EXPORT_SYMBOL_GPL(mutex_lock_nested);
  933. void __sched
  934. _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  935. {
  936. __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
  937. }
  938. EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
  939. int __sched
  940. mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
  941. {
  942. return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
  943. }
  944. EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
  945. int __sched
  946. mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
  947. {
  948. return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
  949. }
  950. EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
  951. void __sched
  952. mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
  953. {
  954. int token;
  955. might_sleep();
  956. token = io_schedule_prepare();
  957. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
  958. subclass, NULL, _RET_IP_, NULL, 0);
  959. io_schedule_finish(token);
  960. }
  961. EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
  962. static inline int
  963. ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  964. {
  965. #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  966. unsigned tmp;
  967. if (ctx->deadlock_inject_countdown-- == 0) {
  968. tmp = ctx->deadlock_inject_interval;
  969. if (tmp > UINT_MAX/4)
  970. tmp = UINT_MAX;
  971. else
  972. tmp = tmp*2 + tmp + tmp/2;
  973. ctx->deadlock_inject_interval = tmp;
  974. ctx->deadlock_inject_countdown = tmp;
  975. ctx->contending_lock = lock;
  976. ww_mutex_unlock(lock);
  977. return -EDEADLK;
  978. }
  979. #endif
  980. return 0;
  981. }
  982. int __sched
  983. ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  984. {
  985. int ret;
  986. might_sleep();
  987. ret = __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
  988. 0, ctx ? &ctx->dep_map : NULL, _RET_IP_,
  989. ctx);
  990. if (!ret && ctx && ctx->acquired > 1)
  991. return ww_mutex_deadlock_injection(lock, ctx);
  992. return ret;
  993. }
  994. EXPORT_SYMBOL_GPL(ww_mutex_lock);
  995. int __sched
  996. ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  997. {
  998. int ret;
  999. might_sleep();
  1000. ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
  1001. 0, ctx ? &ctx->dep_map : NULL, _RET_IP_,
  1002. ctx);
  1003. if (!ret && ctx && ctx->acquired > 1)
  1004. return ww_mutex_deadlock_injection(lock, ctx);
  1005. return ret;
  1006. }
  1007. EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
  1008. #endif
  1009. /*
  1010. * Release the lock, slowpath:
  1011. */
  1012. static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
  1013. {
  1014. struct task_struct *next = NULL;
  1015. DEFINE_WAKE_Q(wake_q);
  1016. unsigned long owner;
  1017. mutex_release(&lock->dep_map, 1, ip);
  1018. /*
  1019. * Release the lock before (potentially) taking the spinlock such that
  1020. * other contenders can get on with things ASAP.
  1021. *
  1022. * Except when HANDOFF, in that case we must not clear the owner field,
  1023. * but instead set it to the top waiter.
  1024. */
  1025. owner = atomic_long_read(&lock->owner);
  1026. for (;;) {
  1027. unsigned long old;
  1028. #ifdef CONFIG_DEBUG_MUTEXES
  1029. DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current);
  1030. DEBUG_LOCKS_WARN_ON(owner & MUTEX_FLAG_PICKUP);
  1031. #endif
  1032. if (owner & MUTEX_FLAG_HANDOFF)
  1033. break;
  1034. old = atomic_long_cmpxchg_release(&lock->owner, owner,
  1035. __owner_flags(owner));
  1036. if (old == owner) {
  1037. if (owner & MUTEX_FLAG_WAITERS)
  1038. break;
  1039. return;
  1040. }
  1041. owner = old;
  1042. }
  1043. spin_lock(&lock->wait_lock);
  1044. debug_mutex_unlock(lock);
  1045. if (!list_empty(&lock->wait_list)) {
  1046. /* get the first entry from the wait-list: */
  1047. struct mutex_waiter *waiter =
  1048. list_first_entry(&lock->wait_list,
  1049. struct mutex_waiter, list);
  1050. next = waiter->task;
  1051. debug_mutex_wake_waiter(lock, waiter);
  1052. wake_q_add(&wake_q, next);
  1053. }
  1054. if (owner & MUTEX_FLAG_HANDOFF)
  1055. __mutex_handoff(lock, next);
  1056. spin_unlock(&lock->wait_lock);
  1057. wake_up_q(&wake_q);
  1058. }
  1059. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  1060. /*
  1061. * Here come the less common (and hence less performance-critical) APIs:
  1062. * mutex_lock_interruptible() and mutex_trylock().
  1063. */
  1064. static noinline int __sched
  1065. __mutex_lock_killable_slowpath(struct mutex *lock);
  1066. static noinline int __sched
  1067. __mutex_lock_interruptible_slowpath(struct mutex *lock);
  1068. /**
  1069. * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
  1070. * @lock: The mutex to be acquired.
  1071. *
  1072. * Lock the mutex like mutex_lock(). If a signal is delivered while the
  1073. * process is sleeping, this function will return without acquiring the
  1074. * mutex.
  1075. *
  1076. * Context: Process context.
  1077. * Return: 0 if the lock was successfully acquired or %-EINTR if a
  1078. * signal arrived.
  1079. */
  1080. int __sched mutex_lock_interruptible(struct mutex *lock)
  1081. {
  1082. might_sleep();
  1083. if (__mutex_trylock_fast(lock))
  1084. return 0;
  1085. return __mutex_lock_interruptible_slowpath(lock);
  1086. }
  1087. EXPORT_SYMBOL(mutex_lock_interruptible);
  1088. /**
  1089. * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
  1090. * @lock: The mutex to be acquired.
  1091. *
  1092. * Lock the mutex like mutex_lock(). If a signal which will be fatal to
  1093. * the current process is delivered while the process is sleeping, this
  1094. * function will return without acquiring the mutex.
  1095. *
  1096. * Context: Process context.
  1097. * Return: 0 if the lock was successfully acquired or %-EINTR if a
  1098. * fatal signal arrived.
  1099. */
  1100. int __sched mutex_lock_killable(struct mutex *lock)
  1101. {
  1102. might_sleep();
  1103. if (__mutex_trylock_fast(lock))
  1104. return 0;
  1105. return __mutex_lock_killable_slowpath(lock);
  1106. }
  1107. EXPORT_SYMBOL(mutex_lock_killable);
  1108. /**
  1109. * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
  1110. * @lock: The mutex to be acquired.
  1111. *
  1112. * Lock the mutex like mutex_lock(). While the task is waiting for this
  1113. * mutex, it will be accounted as being in the IO wait state by the
  1114. * scheduler.
  1115. *
  1116. * Context: Process context.
  1117. */
  1118. void __sched mutex_lock_io(struct mutex *lock)
  1119. {
  1120. int token;
  1121. token = io_schedule_prepare();
  1122. mutex_lock(lock);
  1123. io_schedule_finish(token);
  1124. }
  1125. EXPORT_SYMBOL_GPL(mutex_lock_io);
  1126. static noinline void __sched
  1127. __mutex_lock_slowpath(struct mutex *lock)
  1128. {
  1129. __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
  1130. }
  1131. static noinline int __sched
  1132. __mutex_lock_killable_slowpath(struct mutex *lock)
  1133. {
  1134. return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
  1135. }
  1136. static noinline int __sched
  1137. __mutex_lock_interruptible_slowpath(struct mutex *lock)
  1138. {
  1139. return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
  1140. }
  1141. static noinline int __sched
  1142. __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  1143. {
  1144. return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0, NULL,
  1145. _RET_IP_, ctx);
  1146. }
  1147. static noinline int __sched
  1148. __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
  1149. struct ww_acquire_ctx *ctx)
  1150. {
  1151. return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0, NULL,
  1152. _RET_IP_, ctx);
  1153. }
  1154. #endif
  1155. /**
  1156. * mutex_trylock - try to acquire the mutex, without waiting
  1157. * @lock: the mutex to be acquired
  1158. *
  1159. * Try to acquire the mutex atomically. Returns 1 if the mutex
  1160. * has been acquired successfully, and 0 on contention.
  1161. *
  1162. * NOTE: this function follows the spin_trylock() convention, so
  1163. * it is negated from the down_trylock() return values! Be careful
  1164. * about this when converting semaphore users to mutexes.
  1165. *
  1166. * This function must not be used in interrupt context. The
  1167. * mutex must be released by the same task that acquired it.
  1168. */
  1169. int __sched mutex_trylock(struct mutex *lock)
  1170. {
  1171. bool locked = __mutex_trylock(lock);
  1172. if (locked)
  1173. mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  1174. return locked;
  1175. }
  1176. EXPORT_SYMBOL(mutex_trylock);
  1177. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  1178. int __sched
  1179. ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  1180. {
  1181. might_sleep();
  1182. if (__mutex_trylock_fast(&lock->base)) {
  1183. if (ctx)
  1184. ww_mutex_set_context_fastpath(lock, ctx);
  1185. return 0;
  1186. }
  1187. return __ww_mutex_lock_slowpath(lock, ctx);
  1188. }
  1189. EXPORT_SYMBOL(ww_mutex_lock);
  1190. int __sched
  1191. ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  1192. {
  1193. might_sleep();
  1194. if (__mutex_trylock_fast(&lock->base)) {
  1195. if (ctx)
  1196. ww_mutex_set_context_fastpath(lock, ctx);
  1197. return 0;
  1198. }
  1199. return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
  1200. }
  1201. EXPORT_SYMBOL(ww_mutex_lock_interruptible);
  1202. #endif
  1203. /**
  1204. * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  1205. * @cnt: the atomic which we are to dec
  1206. * @lock: the mutex to return holding if we dec to 0
  1207. *
  1208. * return true and hold lock if we dec to 0, return false otherwise
  1209. */
  1210. int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  1211. {
  1212. /* dec if we can't possibly hit 0 */
  1213. if (atomic_add_unless(cnt, -1, 1))
  1214. return 0;
  1215. /* we might hit 0, so take the lock */
  1216. mutex_lock(lock);
  1217. if (!atomic_dec_and_test(cnt)) {
  1218. /* when we actually did the dec, we didn't hit 0 */
  1219. mutex_unlock(lock);
  1220. return 0;
  1221. }
  1222. /* we hit 0, and we hold the lock */
  1223. return 1;
  1224. }
  1225. EXPORT_SYMBOL(atomic_dec_and_mutex_lock);