rtmutex.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. /*
  2. * RT-Mutexes: simple blocking mutual exclusion locks with PI support
  3. *
  4. * started by Ingo Molnar and Thomas Gleixner.
  5. *
  6. * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  7. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  8. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  9. * Copyright (C) 2006 Esben Nielsen
  10. */
  11. #include <linux/spinlock.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/timer.h>
  15. #include "rtmutex_common.h"
  16. #ifdef CONFIG_DEBUG_RT_MUTEXES
  17. # include "rtmutex-debug.h"
  18. #else
  19. # include "rtmutex.h"
  20. #endif
  21. /*
  22. * lock->owner state tracking:
  23. *
  24. * lock->owner holds the task_struct pointer of the owner. Bit 0 and 1
  25. * are used to keep track of the "owner is pending" and "lock has
  26. * waiters" state.
  27. *
  28. * owner bit1 bit0
  29. * NULL 0 0 lock is free (fast acquire possible)
  30. * NULL 0 1 invalid state
  31. * NULL 1 0 Transitional State*
  32. * NULL 1 1 invalid state
  33. * taskpointer 0 0 lock is held (fast release possible)
  34. * taskpointer 0 1 task is pending owner
  35. * taskpointer 1 0 lock is held and has waiters
  36. * taskpointer 1 1 task is pending owner and lock has more waiters
  37. *
  38. * Pending ownership is assigned to the top (highest priority)
  39. * waiter of the lock, when the lock is released. The thread is woken
  40. * up and can now take the lock. Until the lock is taken (bit 0
  41. * cleared) a competing higher priority thread can steal the lock
  42. * which puts the woken up thread back on the waiters list.
  43. *
  44. * The fast atomic compare exchange based acquire and release is only
  45. * possible when bit 0 and 1 of lock->owner are 0.
  46. *
  47. * (*) There's a small time where the owner can be NULL and the
  48. * "lock has waiters" bit is set. This can happen when grabbing the lock.
  49. * To prevent a cmpxchg of the owner releasing the lock, we need to set this
  50. * bit before looking at the lock, hence the reason this is a transitional
  51. * state.
  52. */
  53. static void
  54. rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner,
  55. unsigned long mask)
  56. {
  57. unsigned long val = (unsigned long)owner | mask;
  58. if (rt_mutex_has_waiters(lock))
  59. val |= RT_MUTEX_HAS_WAITERS;
  60. lock->owner = (struct task_struct *)val;
  61. }
  62. static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
  63. {
  64. lock->owner = (struct task_struct *)
  65. ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
  66. }
  67. static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  68. {
  69. if (!rt_mutex_has_waiters(lock))
  70. clear_rt_mutex_waiters(lock);
  71. }
  72. /*
  73. * We can speed up the acquire/release, if the architecture
  74. * supports cmpxchg and if there's no debugging state to be set up
  75. */
  76. #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
  77. # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
  78. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  79. {
  80. unsigned long owner, *p = (unsigned long *) &lock->owner;
  81. do {
  82. owner = *p;
  83. } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
  84. }
  85. #else
  86. # define rt_mutex_cmpxchg(l,c,n) (0)
  87. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  88. {
  89. lock->owner = (struct task_struct *)
  90. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  91. }
  92. #endif
  93. /*
  94. * Calculate task priority from the waiter list priority
  95. *
  96. * Return task->normal_prio when the waiter list is empty or when
  97. * the waiter is not allowed to do priority boosting
  98. */
  99. int rt_mutex_getprio(struct task_struct *task)
  100. {
  101. if (likely(!task_has_pi_waiters(task)))
  102. return task->normal_prio;
  103. return min(task_top_pi_waiter(task)->pi_list_entry.prio,
  104. task->normal_prio);
  105. }
  106. /*
  107. * Adjust the priority of a task, after its pi_waiters got modified.
  108. *
  109. * This can be both boosting and unboosting. task->pi_lock must be held.
  110. */
  111. static void __rt_mutex_adjust_prio(struct task_struct *task)
  112. {
  113. int prio = rt_mutex_getprio(task);
  114. if (task->prio != prio)
  115. rt_mutex_setprio(task, prio);
  116. }
  117. /*
  118. * Adjust task priority (undo boosting). Called from the exit path of
  119. * rt_mutex_slowunlock() and rt_mutex_slowlock().
  120. *
  121. * (Note: We do this outside of the protection of lock->wait_lock to
  122. * allow the lock to be taken while or before we readjust the priority
  123. * of task. We do not use the spin_xx_mutex() variants here as we are
  124. * outside of the debug path.)
  125. */
  126. static void rt_mutex_adjust_prio(struct task_struct *task)
  127. {
  128. unsigned long flags;
  129. spin_lock_irqsave(&task->pi_lock, flags);
  130. __rt_mutex_adjust_prio(task);
  131. spin_unlock_irqrestore(&task->pi_lock, flags);
  132. }
  133. /*
  134. * Max number of times we'll walk the boosting chain:
  135. */
  136. int max_lock_depth = 1024;
  137. /*
  138. * Adjust the priority chain. Also used for deadlock detection.
  139. * Decreases task's usage by one - may thus free the task.
  140. * Returns 0 or -EDEADLK.
  141. */
  142. static int rt_mutex_adjust_prio_chain(task_t *task,
  143. int deadlock_detect,
  144. struct rt_mutex *orig_lock,
  145. struct rt_mutex_waiter *orig_waiter,
  146. struct task_struct *top_task)
  147. {
  148. struct rt_mutex *lock;
  149. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  150. int detect_deadlock, ret = 0, depth = 0;
  151. unsigned long flags;
  152. detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
  153. deadlock_detect);
  154. /*
  155. * The (de)boosting is a step by step approach with a lot of
  156. * pitfalls. We want this to be preemptible and we want hold a
  157. * maximum of two locks per step. So we have to check
  158. * carefully whether things change under us.
  159. */
  160. again:
  161. if (++depth > max_lock_depth) {
  162. static int prev_max;
  163. /*
  164. * Print this only once. If the admin changes the limit,
  165. * print a new message when reaching the limit again.
  166. */
  167. if (prev_max != max_lock_depth) {
  168. prev_max = max_lock_depth;
  169. printk(KERN_WARNING "Maximum lock depth %d reached "
  170. "task: %s (%d)\n", max_lock_depth,
  171. top_task->comm, top_task->pid);
  172. }
  173. put_task_struct(task);
  174. return deadlock_detect ? -EDEADLK : 0;
  175. }
  176. retry:
  177. /*
  178. * Task can not go away as we did a get_task() before !
  179. */
  180. spin_lock_irqsave(&task->pi_lock, flags);
  181. waiter = task->pi_blocked_on;
  182. /*
  183. * Check whether the end of the boosting chain has been
  184. * reached or the state of the chain has changed while we
  185. * dropped the locks.
  186. */
  187. if (!waiter || !waiter->task)
  188. goto out_unlock_pi;
  189. if (top_waiter && (!task_has_pi_waiters(task) ||
  190. top_waiter != task_top_pi_waiter(task)))
  191. goto out_unlock_pi;
  192. /*
  193. * When deadlock detection is off then we check, if further
  194. * priority adjustment is necessary.
  195. */
  196. if (!detect_deadlock && waiter->list_entry.prio == task->prio)
  197. goto out_unlock_pi;
  198. lock = waiter->lock;
  199. if (!spin_trylock(&lock->wait_lock)) {
  200. spin_unlock_irqrestore(&task->pi_lock, flags);
  201. cpu_relax();
  202. goto retry;
  203. }
  204. /* Deadlock detection */
  205. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  206. debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
  207. spin_unlock(&lock->wait_lock);
  208. ret = deadlock_detect ? -EDEADLK : 0;
  209. goto out_unlock_pi;
  210. }
  211. top_waiter = rt_mutex_top_waiter(lock);
  212. /* Requeue the waiter */
  213. plist_del(&waiter->list_entry, &lock->wait_list);
  214. waiter->list_entry.prio = task->prio;
  215. plist_add(&waiter->list_entry, &lock->wait_list);
  216. /* Release the task */
  217. spin_unlock_irqrestore(&task->pi_lock, flags);
  218. put_task_struct(task);
  219. /* Grab the next task */
  220. task = rt_mutex_owner(lock);
  221. spin_lock_irqsave(&task->pi_lock, flags);
  222. if (waiter == rt_mutex_top_waiter(lock)) {
  223. /* Boost the owner */
  224. plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
  225. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  226. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  227. __rt_mutex_adjust_prio(task);
  228. } else if (top_waiter == waiter) {
  229. /* Deboost the owner */
  230. plist_del(&waiter->pi_list_entry, &task->pi_waiters);
  231. waiter = rt_mutex_top_waiter(lock);
  232. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  233. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  234. __rt_mutex_adjust_prio(task);
  235. }
  236. get_task_struct(task);
  237. spin_unlock_irqrestore(&task->pi_lock, flags);
  238. top_waiter = rt_mutex_top_waiter(lock);
  239. spin_unlock(&lock->wait_lock);
  240. if (!detect_deadlock && waiter != top_waiter)
  241. goto out_put_task;
  242. goto again;
  243. out_unlock_pi:
  244. spin_unlock_irqrestore(&task->pi_lock, flags);
  245. out_put_task:
  246. put_task_struct(task);
  247. return ret;
  248. }
  249. /*
  250. * Optimization: check if we can steal the lock from the
  251. * assigned pending owner [which might not have taken the
  252. * lock yet]:
  253. */
  254. static inline int try_to_steal_lock(struct rt_mutex *lock)
  255. {
  256. struct task_struct *pendowner = rt_mutex_owner(lock);
  257. struct rt_mutex_waiter *next;
  258. unsigned long flags;
  259. if (!rt_mutex_owner_pending(lock))
  260. return 0;
  261. if (pendowner == current)
  262. return 1;
  263. spin_lock_irqsave(&pendowner->pi_lock, flags);
  264. if (current->prio >= pendowner->prio) {
  265. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  266. return 0;
  267. }
  268. /*
  269. * Check if a waiter is enqueued on the pending owners
  270. * pi_waiters list. Remove it and readjust pending owners
  271. * priority.
  272. */
  273. if (likely(!rt_mutex_has_waiters(lock))) {
  274. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  275. return 1;
  276. }
  277. /* No chain handling, pending owner is not blocked on anything: */
  278. next = rt_mutex_top_waiter(lock);
  279. plist_del(&next->pi_list_entry, &pendowner->pi_waiters);
  280. __rt_mutex_adjust_prio(pendowner);
  281. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  282. /*
  283. * We are going to steal the lock and a waiter was
  284. * enqueued on the pending owners pi_waiters queue. So
  285. * we have to enqueue this waiter into
  286. * current->pi_waiters list. This covers the case,
  287. * where current is boosted because it holds another
  288. * lock and gets unboosted because the booster is
  289. * interrupted, so we would delay a waiter with higher
  290. * priority as current->normal_prio.
  291. *
  292. * Note: in the rare case of a SCHED_OTHER task changing
  293. * its priority and thus stealing the lock, next->task
  294. * might be current:
  295. */
  296. if (likely(next->task != current)) {
  297. spin_lock_irqsave(&current->pi_lock, flags);
  298. plist_add(&next->pi_list_entry, &current->pi_waiters);
  299. __rt_mutex_adjust_prio(current);
  300. spin_unlock_irqrestore(&current->pi_lock, flags);
  301. }
  302. return 1;
  303. }
  304. /*
  305. * Try to take an rt-mutex
  306. *
  307. * This fails
  308. * - when the lock has a real owner
  309. * - when a different pending owner exists and has higher priority than current
  310. *
  311. * Must be called with lock->wait_lock held.
  312. */
  313. static int try_to_take_rt_mutex(struct rt_mutex *lock)
  314. {
  315. /*
  316. * We have to be careful here if the atomic speedups are
  317. * enabled, such that, when
  318. * - no other waiter is on the lock
  319. * - the lock has been released since we did the cmpxchg
  320. * the lock can be released or taken while we are doing the
  321. * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
  322. *
  323. * The atomic acquire/release aware variant of
  324. * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
  325. * the WAITERS bit, the atomic release / acquire can not
  326. * happen anymore and lock->wait_lock protects us from the
  327. * non-atomic case.
  328. *
  329. * Note, that this might set lock->owner =
  330. * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
  331. * any more. This is fixed up when we take the ownership.
  332. * This is the transitional state explained at the top of this file.
  333. */
  334. mark_rt_mutex_waiters(lock);
  335. if (rt_mutex_owner(lock) && !try_to_steal_lock(lock))
  336. return 0;
  337. /* We got the lock. */
  338. debug_rt_mutex_lock(lock);
  339. rt_mutex_set_owner(lock, current, 0);
  340. rt_mutex_deadlock_account_lock(lock, current);
  341. return 1;
  342. }
  343. /*
  344. * Task blocks on lock.
  345. *
  346. * Prepare waiter and propagate pi chain
  347. *
  348. * This must be called with lock->wait_lock held.
  349. */
  350. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  351. struct rt_mutex_waiter *waiter,
  352. int detect_deadlock)
  353. {
  354. struct rt_mutex_waiter *top_waiter = waiter;
  355. task_t *owner = rt_mutex_owner(lock);
  356. int boost = 0, res;
  357. unsigned long flags;
  358. spin_lock_irqsave(&current->pi_lock, flags);
  359. __rt_mutex_adjust_prio(current);
  360. waiter->task = current;
  361. waiter->lock = lock;
  362. plist_node_init(&waiter->list_entry, current->prio);
  363. plist_node_init(&waiter->pi_list_entry, current->prio);
  364. /* Get the top priority waiter on the lock */
  365. if (rt_mutex_has_waiters(lock))
  366. top_waiter = rt_mutex_top_waiter(lock);
  367. plist_add(&waiter->list_entry, &lock->wait_list);
  368. current->pi_blocked_on = waiter;
  369. spin_unlock_irqrestore(&current->pi_lock, flags);
  370. if (waiter == rt_mutex_top_waiter(lock)) {
  371. spin_lock_irqsave(&owner->pi_lock, flags);
  372. plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
  373. plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
  374. __rt_mutex_adjust_prio(owner);
  375. if (owner->pi_blocked_on) {
  376. boost = 1;
  377. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  378. get_task_struct(owner);
  379. }
  380. spin_unlock_irqrestore(&owner->pi_lock, flags);
  381. }
  382. else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
  383. spin_lock_irqsave(&owner->pi_lock, flags);
  384. if (owner->pi_blocked_on) {
  385. boost = 1;
  386. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  387. get_task_struct(owner);
  388. }
  389. spin_unlock_irqrestore(&owner->pi_lock, flags);
  390. }
  391. if (!boost)
  392. return 0;
  393. spin_unlock(&lock->wait_lock);
  394. res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
  395. current);
  396. spin_lock(&lock->wait_lock);
  397. return res;
  398. }
  399. /*
  400. * Wake up the next waiter on the lock.
  401. *
  402. * Remove the top waiter from the current tasks waiter list and from
  403. * the lock waiter list. Set it as pending owner. Then wake it up.
  404. *
  405. * Called with lock->wait_lock held.
  406. */
  407. static void wakeup_next_waiter(struct rt_mutex *lock)
  408. {
  409. struct rt_mutex_waiter *waiter;
  410. struct task_struct *pendowner;
  411. unsigned long flags;
  412. spin_lock_irqsave(&current->pi_lock, flags);
  413. waiter = rt_mutex_top_waiter(lock);
  414. plist_del(&waiter->list_entry, &lock->wait_list);
  415. /*
  416. * Remove it from current->pi_waiters. We do not adjust a
  417. * possible priority boost right now. We execute wakeup in the
  418. * boosted mode and go back to normal after releasing
  419. * lock->wait_lock.
  420. */
  421. plist_del(&waiter->pi_list_entry, &current->pi_waiters);
  422. pendowner = waiter->task;
  423. waiter->task = NULL;
  424. rt_mutex_set_owner(lock, pendowner, RT_MUTEX_OWNER_PENDING);
  425. spin_unlock_irqrestore(&current->pi_lock, flags);
  426. /*
  427. * Clear the pi_blocked_on variable and enqueue a possible
  428. * waiter into the pi_waiters list of the pending owner. This
  429. * prevents that in case the pending owner gets unboosted a
  430. * waiter with higher priority than pending-owner->normal_prio
  431. * is blocked on the unboosted (pending) owner.
  432. */
  433. spin_lock_irqsave(&pendowner->pi_lock, flags);
  434. WARN_ON(!pendowner->pi_blocked_on);
  435. WARN_ON(pendowner->pi_blocked_on != waiter);
  436. WARN_ON(pendowner->pi_blocked_on->lock != lock);
  437. pendowner->pi_blocked_on = NULL;
  438. if (rt_mutex_has_waiters(lock)) {
  439. struct rt_mutex_waiter *next;
  440. next = rt_mutex_top_waiter(lock);
  441. plist_add(&next->pi_list_entry, &pendowner->pi_waiters);
  442. }
  443. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  444. wake_up_process(pendowner);
  445. }
  446. /*
  447. * Remove a waiter from a lock
  448. *
  449. * Must be called with lock->wait_lock held
  450. */
  451. static void remove_waiter(struct rt_mutex *lock,
  452. struct rt_mutex_waiter *waiter)
  453. {
  454. int first = (waiter == rt_mutex_top_waiter(lock));
  455. int boost = 0;
  456. task_t *owner = rt_mutex_owner(lock);
  457. unsigned long flags;
  458. spin_lock_irqsave(&current->pi_lock, flags);
  459. plist_del(&waiter->list_entry, &lock->wait_list);
  460. waiter->task = NULL;
  461. current->pi_blocked_on = NULL;
  462. spin_unlock_irqrestore(&current->pi_lock, flags);
  463. if (first && owner != current) {
  464. spin_lock_irqsave(&owner->pi_lock, flags);
  465. plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
  466. if (rt_mutex_has_waiters(lock)) {
  467. struct rt_mutex_waiter *next;
  468. next = rt_mutex_top_waiter(lock);
  469. plist_add(&next->pi_list_entry, &owner->pi_waiters);
  470. }
  471. __rt_mutex_adjust_prio(owner);
  472. if (owner->pi_blocked_on) {
  473. boost = 1;
  474. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  475. get_task_struct(owner);
  476. }
  477. spin_unlock_irqrestore(&owner->pi_lock, flags);
  478. }
  479. WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
  480. if (!boost)
  481. return;
  482. spin_unlock(&lock->wait_lock);
  483. rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current);
  484. spin_lock(&lock->wait_lock);
  485. }
  486. /*
  487. * Recheck the pi chain, in case we got a priority setting
  488. *
  489. * Called from sched_setscheduler
  490. */
  491. void rt_mutex_adjust_pi(struct task_struct *task)
  492. {
  493. struct rt_mutex_waiter *waiter;
  494. unsigned long flags;
  495. spin_lock_irqsave(&task->pi_lock, flags);
  496. waiter = task->pi_blocked_on;
  497. if (!waiter || waiter->list_entry.prio == task->prio) {
  498. spin_unlock_irqrestore(&task->pi_lock, flags);
  499. return;
  500. }
  501. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  502. get_task_struct(task);
  503. spin_unlock_irqrestore(&task->pi_lock, flags);
  504. rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task);
  505. }
  506. /*
  507. * Slow path lock function:
  508. */
  509. static int __sched
  510. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  511. struct hrtimer_sleeper *timeout,
  512. int detect_deadlock)
  513. {
  514. struct rt_mutex_waiter waiter;
  515. int ret = 0;
  516. debug_rt_mutex_init_waiter(&waiter);
  517. waiter.task = NULL;
  518. spin_lock(&lock->wait_lock);
  519. /* Try to acquire the lock again: */
  520. if (try_to_take_rt_mutex(lock)) {
  521. spin_unlock(&lock->wait_lock);
  522. return 0;
  523. }
  524. set_current_state(state);
  525. /* Setup the timer, when timeout != NULL */
  526. if (unlikely(timeout))
  527. hrtimer_start(&timeout->timer, timeout->timer.expires,
  528. HRTIMER_ABS);
  529. for (;;) {
  530. /* Try to acquire the lock: */
  531. if (try_to_take_rt_mutex(lock))
  532. break;
  533. /*
  534. * TASK_INTERRUPTIBLE checks for signals and
  535. * timeout. Ignored otherwise.
  536. */
  537. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  538. /* Signal pending? */
  539. if (signal_pending(current))
  540. ret = -EINTR;
  541. if (timeout && !timeout->task)
  542. ret = -ETIMEDOUT;
  543. if (ret)
  544. break;
  545. }
  546. /*
  547. * waiter.task is NULL the first time we come here and
  548. * when we have been woken up by the previous owner
  549. * but the lock got stolen by a higher prio task.
  550. */
  551. if (!waiter.task) {
  552. ret = task_blocks_on_rt_mutex(lock, &waiter,
  553. detect_deadlock);
  554. /*
  555. * If we got woken up by the owner then start loop
  556. * all over without going into schedule to try
  557. * to get the lock now:
  558. */
  559. if (unlikely(!waiter.task))
  560. continue;
  561. if (unlikely(ret))
  562. break;
  563. }
  564. spin_unlock(&lock->wait_lock);
  565. debug_rt_mutex_print_deadlock(&waiter);
  566. if (waiter.task)
  567. schedule_rt_mutex(lock);
  568. spin_lock(&lock->wait_lock);
  569. set_current_state(state);
  570. }
  571. set_current_state(TASK_RUNNING);
  572. if (unlikely(waiter.task))
  573. remove_waiter(lock, &waiter);
  574. /*
  575. * try_to_take_rt_mutex() sets the waiter bit
  576. * unconditionally. We might have to fix that up.
  577. */
  578. fixup_rt_mutex_waiters(lock);
  579. spin_unlock(&lock->wait_lock);
  580. /* Remove pending timer: */
  581. if (unlikely(timeout))
  582. hrtimer_cancel(&timeout->timer);
  583. /*
  584. * Readjust priority, when we did not get the lock. We might
  585. * have been the pending owner and boosted. Since we did not
  586. * take the lock, the PI boost has to go.
  587. */
  588. if (unlikely(ret))
  589. rt_mutex_adjust_prio(current);
  590. debug_rt_mutex_free_waiter(&waiter);
  591. return ret;
  592. }
  593. /*
  594. * Slow path try-lock function:
  595. */
  596. static inline int
  597. rt_mutex_slowtrylock(struct rt_mutex *lock)
  598. {
  599. int ret = 0;
  600. spin_lock(&lock->wait_lock);
  601. if (likely(rt_mutex_owner(lock) != current)) {
  602. ret = try_to_take_rt_mutex(lock);
  603. /*
  604. * try_to_take_rt_mutex() sets the lock waiters
  605. * bit unconditionally. Clean this up.
  606. */
  607. fixup_rt_mutex_waiters(lock);
  608. }
  609. spin_unlock(&lock->wait_lock);
  610. return ret;
  611. }
  612. /*
  613. * Slow path to release a rt-mutex:
  614. */
  615. static void __sched
  616. rt_mutex_slowunlock(struct rt_mutex *lock)
  617. {
  618. spin_lock(&lock->wait_lock);
  619. debug_rt_mutex_unlock(lock);
  620. rt_mutex_deadlock_account_unlock(current);
  621. if (!rt_mutex_has_waiters(lock)) {
  622. lock->owner = NULL;
  623. spin_unlock(&lock->wait_lock);
  624. return;
  625. }
  626. wakeup_next_waiter(lock);
  627. spin_unlock(&lock->wait_lock);
  628. /* Undo pi boosting if necessary: */
  629. rt_mutex_adjust_prio(current);
  630. }
  631. /*
  632. * debug aware fast / slowpath lock,trylock,unlock
  633. *
  634. * The atomic acquire/release ops are compiled away, when either the
  635. * architecture does not support cmpxchg or when debugging is enabled.
  636. */
  637. static inline int
  638. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  639. int detect_deadlock,
  640. int (*slowfn)(struct rt_mutex *lock, int state,
  641. struct hrtimer_sleeper *timeout,
  642. int detect_deadlock))
  643. {
  644. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  645. rt_mutex_deadlock_account_lock(lock, current);
  646. return 0;
  647. } else
  648. return slowfn(lock, state, NULL, detect_deadlock);
  649. }
  650. static inline int
  651. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  652. struct hrtimer_sleeper *timeout, int detect_deadlock,
  653. int (*slowfn)(struct rt_mutex *lock, int state,
  654. struct hrtimer_sleeper *timeout,
  655. int detect_deadlock))
  656. {
  657. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  658. rt_mutex_deadlock_account_lock(lock, current);
  659. return 0;
  660. } else
  661. return slowfn(lock, state, timeout, detect_deadlock);
  662. }
  663. static inline int
  664. rt_mutex_fasttrylock(struct rt_mutex *lock,
  665. int (*slowfn)(struct rt_mutex *lock))
  666. {
  667. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  668. rt_mutex_deadlock_account_lock(lock, current);
  669. return 1;
  670. }
  671. return slowfn(lock);
  672. }
  673. static inline void
  674. rt_mutex_fastunlock(struct rt_mutex *lock,
  675. void (*slowfn)(struct rt_mutex *lock))
  676. {
  677. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  678. rt_mutex_deadlock_account_unlock(current);
  679. else
  680. slowfn(lock);
  681. }
  682. /**
  683. * rt_mutex_lock - lock a rt_mutex
  684. *
  685. * @lock: the rt_mutex to be locked
  686. */
  687. void __sched rt_mutex_lock(struct rt_mutex *lock)
  688. {
  689. might_sleep();
  690. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
  691. }
  692. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  693. /**
  694. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  695. *
  696. * @lock: the rt_mutex to be locked
  697. * @detect_deadlock: deadlock detection on/off
  698. *
  699. * Returns:
  700. * 0 on success
  701. * -EINTR when interrupted by a signal
  702. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  703. */
  704. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
  705. int detect_deadlock)
  706. {
  707. might_sleep();
  708. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
  709. detect_deadlock, rt_mutex_slowlock);
  710. }
  711. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  712. /**
  713. * rt_mutex_lock_interruptible_ktime - lock a rt_mutex interruptible
  714. * the timeout structure is provided
  715. * by the caller
  716. *
  717. * @lock: the rt_mutex to be locked
  718. * @timeout: timeout structure or NULL (no timeout)
  719. * @detect_deadlock: deadlock detection on/off
  720. *
  721. * Returns:
  722. * 0 on success
  723. * -EINTR when interrupted by a signal
  724. * -ETIMEOUT when the timeout expired
  725. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  726. */
  727. int
  728. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
  729. int detect_deadlock)
  730. {
  731. might_sleep();
  732. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  733. detect_deadlock, rt_mutex_slowlock);
  734. }
  735. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  736. /**
  737. * rt_mutex_trylock - try to lock a rt_mutex
  738. *
  739. * @lock: the rt_mutex to be locked
  740. *
  741. * Returns 1 on success and 0 on contention
  742. */
  743. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  744. {
  745. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  746. }
  747. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  748. /**
  749. * rt_mutex_unlock - unlock a rt_mutex
  750. *
  751. * @lock: the rt_mutex to be unlocked
  752. */
  753. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  754. {
  755. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  756. }
  757. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  758. /***
  759. * rt_mutex_destroy - mark a mutex unusable
  760. * @lock: the mutex to be destroyed
  761. *
  762. * This function marks the mutex uninitialized, and any subsequent
  763. * use of the mutex is forbidden. The mutex must not be locked when
  764. * this function is called.
  765. */
  766. void rt_mutex_destroy(struct rt_mutex *lock)
  767. {
  768. WARN_ON(rt_mutex_is_locked(lock));
  769. #ifdef CONFIG_DEBUG_RT_MUTEXES
  770. lock->magic = NULL;
  771. #endif
  772. }
  773. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  774. /**
  775. * __rt_mutex_init - initialize the rt lock
  776. *
  777. * @lock: the rt lock to be initialized
  778. *
  779. * Initialize the rt lock to unlocked state.
  780. *
  781. * Initializing of a locked rt lock is not allowed
  782. */
  783. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  784. {
  785. lock->owner = NULL;
  786. spin_lock_init(&lock->wait_lock);
  787. plist_head_init(&lock->wait_list, &lock->wait_lock);
  788. debug_rt_mutex_init(lock, name);
  789. }
  790. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  791. /**
  792. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  793. * proxy owner
  794. *
  795. * @lock: the rt_mutex to be locked
  796. * @proxy_owner:the task to set as owner
  797. *
  798. * No locking. Caller has to do serializing itself
  799. * Special API call for PI-futex support
  800. */
  801. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  802. struct task_struct *proxy_owner)
  803. {
  804. __rt_mutex_init(lock, NULL);
  805. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  806. rt_mutex_set_owner(lock, proxy_owner, 0);
  807. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  808. }
  809. /**
  810. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  811. *
  812. * @lock: the rt_mutex to be locked
  813. *
  814. * No locking. Caller has to do serializing itself
  815. * Special API call for PI-futex support
  816. */
  817. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  818. struct task_struct *proxy_owner)
  819. {
  820. debug_rt_mutex_proxy_unlock(lock);
  821. rt_mutex_set_owner(lock, NULL, 0);
  822. rt_mutex_deadlock_account_unlock(proxy_owner);
  823. }
  824. /**
  825. * rt_mutex_next_owner - return the next owner of the lock
  826. *
  827. * @lock: the rt lock query
  828. *
  829. * Returns the next owner of the lock or NULL
  830. *
  831. * Caller has to serialize against other accessors to the lock
  832. * itself.
  833. *
  834. * Special API call for PI-futex support
  835. */
  836. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  837. {
  838. if (!rt_mutex_has_waiters(lock))
  839. return NULL;
  840. return rt_mutex_top_waiter(lock)->task;
  841. }