rtmutex.c 26 KB

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