mutex.c 37 KB

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