rtmutex.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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/export.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/rt.h>
  17. #include <linux/sched/deadline.h>
  18. #include <linux/timer.h>
  19. #include "rtmutex_common.h"
  20. /*
  21. * lock->owner state tracking:
  22. *
  23. * lock->owner holds the task_struct pointer of the owner. Bit 0
  24. * is used to keep track of the "lock has waiters" state.
  25. *
  26. * owner bit0
  27. * NULL 0 lock is free (fast acquire possible)
  28. * NULL 1 lock is free and has waiters and the top waiter
  29. * is going to take the lock*
  30. * taskpointer 0 lock is held (fast release possible)
  31. * taskpointer 1 lock is held and has waiters**
  32. *
  33. * The fast atomic compare exchange based acquire and release is only
  34. * possible when bit 0 of lock->owner is 0.
  35. *
  36. * (*) It also can be a transitional state when grabbing the lock
  37. * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
  38. * we need to set the bit0 before looking at the lock, and the owner may be
  39. * NULL in this small time, hence this can be a transitional state.
  40. *
  41. * (**) There is a small time when bit 0 is set but there are no
  42. * waiters. This can happen when grabbing the lock in the slow path.
  43. * To prevent a cmpxchg of the owner releasing the lock, we need to
  44. * set this bit before looking at the lock.
  45. */
  46. static void
  47. rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
  48. {
  49. unsigned long val = (unsigned long)owner;
  50. if (rt_mutex_has_waiters(lock))
  51. val |= RT_MUTEX_HAS_WAITERS;
  52. lock->owner = (struct task_struct *)val;
  53. }
  54. static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
  55. {
  56. lock->owner = (struct task_struct *)
  57. ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
  58. }
  59. static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  60. {
  61. if (!rt_mutex_has_waiters(lock))
  62. clear_rt_mutex_waiters(lock);
  63. }
  64. /*
  65. * We can speed up the acquire/release, if the architecture
  66. * supports cmpxchg and if there's no debugging state to be set up
  67. */
  68. #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
  69. # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
  70. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  71. {
  72. unsigned long owner, *p = (unsigned long *) &lock->owner;
  73. do {
  74. owner = *p;
  75. } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
  76. }
  77. /*
  78. * Safe fastpath aware unlock:
  79. * 1) Clear the waiters bit
  80. * 2) Drop lock->wait_lock
  81. * 3) Try to unlock the lock with cmpxchg
  82. */
  83. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  84. __releases(lock->wait_lock)
  85. {
  86. struct task_struct *owner = rt_mutex_owner(lock);
  87. clear_rt_mutex_waiters(lock);
  88. raw_spin_unlock(&lock->wait_lock);
  89. /*
  90. * If a new waiter comes in between the unlock and the cmpxchg
  91. * we have two situations:
  92. *
  93. * unlock(wait_lock);
  94. * lock(wait_lock);
  95. * cmpxchg(p, owner, 0) == owner
  96. * mark_rt_mutex_waiters(lock);
  97. * acquire(lock);
  98. * or:
  99. *
  100. * unlock(wait_lock);
  101. * lock(wait_lock);
  102. * mark_rt_mutex_waiters(lock);
  103. *
  104. * cmpxchg(p, owner, 0) != owner
  105. * enqueue_waiter();
  106. * unlock(wait_lock);
  107. * lock(wait_lock);
  108. * wake waiter();
  109. * unlock(wait_lock);
  110. * lock(wait_lock);
  111. * acquire(lock);
  112. */
  113. return rt_mutex_cmpxchg(lock, owner, NULL);
  114. }
  115. #else
  116. # define rt_mutex_cmpxchg(l,c,n) (0)
  117. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  118. {
  119. lock->owner = (struct task_struct *)
  120. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  121. }
  122. /*
  123. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  124. */
  125. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  126. __releases(lock->wait_lock)
  127. {
  128. lock->owner = NULL;
  129. raw_spin_unlock(&lock->wait_lock);
  130. return true;
  131. }
  132. #endif
  133. static inline int
  134. rt_mutex_waiter_less(struct rt_mutex_waiter *left,
  135. struct rt_mutex_waiter *right)
  136. {
  137. if (left->prio < right->prio)
  138. return 1;
  139. /*
  140. * If both waiters have dl_prio(), we check the deadlines of the
  141. * associated tasks.
  142. * If left waiter has a dl_prio(), and we didn't return 1 above,
  143. * then right waiter has a dl_prio() too.
  144. */
  145. if (dl_prio(left->prio))
  146. return (left->task->dl.deadline < right->task->dl.deadline);
  147. return 0;
  148. }
  149. static void
  150. rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  151. {
  152. struct rb_node **link = &lock->waiters.rb_node;
  153. struct rb_node *parent = NULL;
  154. struct rt_mutex_waiter *entry;
  155. int leftmost = 1;
  156. while (*link) {
  157. parent = *link;
  158. entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
  159. if (rt_mutex_waiter_less(waiter, entry)) {
  160. link = &parent->rb_left;
  161. } else {
  162. link = &parent->rb_right;
  163. leftmost = 0;
  164. }
  165. }
  166. if (leftmost)
  167. lock->waiters_leftmost = &waiter->tree_entry;
  168. rb_link_node(&waiter->tree_entry, parent, link);
  169. rb_insert_color(&waiter->tree_entry, &lock->waiters);
  170. }
  171. static void
  172. rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  173. {
  174. if (RB_EMPTY_NODE(&waiter->tree_entry))
  175. return;
  176. if (lock->waiters_leftmost == &waiter->tree_entry)
  177. lock->waiters_leftmost = rb_next(&waiter->tree_entry);
  178. rb_erase(&waiter->tree_entry, &lock->waiters);
  179. RB_CLEAR_NODE(&waiter->tree_entry);
  180. }
  181. static void
  182. rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  183. {
  184. struct rb_node **link = &task->pi_waiters.rb_node;
  185. struct rb_node *parent = NULL;
  186. struct rt_mutex_waiter *entry;
  187. int leftmost = 1;
  188. while (*link) {
  189. parent = *link;
  190. entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
  191. if (rt_mutex_waiter_less(waiter, entry)) {
  192. link = &parent->rb_left;
  193. } else {
  194. link = &parent->rb_right;
  195. leftmost = 0;
  196. }
  197. }
  198. if (leftmost)
  199. task->pi_waiters_leftmost = &waiter->pi_tree_entry;
  200. rb_link_node(&waiter->pi_tree_entry, parent, link);
  201. rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
  202. }
  203. static void
  204. rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  205. {
  206. if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
  207. return;
  208. if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
  209. task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
  210. rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
  211. RB_CLEAR_NODE(&waiter->pi_tree_entry);
  212. }
  213. /*
  214. * Calculate task priority from the waiter tree priority
  215. *
  216. * Return task->normal_prio when the waiter tree is empty or when
  217. * the waiter is not allowed to do priority boosting
  218. */
  219. int rt_mutex_getprio(struct task_struct *task)
  220. {
  221. if (likely(!task_has_pi_waiters(task)))
  222. return task->normal_prio;
  223. return min(task_top_pi_waiter(task)->prio,
  224. task->normal_prio);
  225. }
  226. struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
  227. {
  228. if (likely(!task_has_pi_waiters(task)))
  229. return NULL;
  230. return task_top_pi_waiter(task)->task;
  231. }
  232. /*
  233. * Called by sched_setscheduler() to check whether the priority change
  234. * is overruled by a possible priority boosting.
  235. */
  236. int rt_mutex_check_prio(struct task_struct *task, int newprio)
  237. {
  238. if (!task_has_pi_waiters(task))
  239. return 0;
  240. return task_top_pi_waiter(task)->task->prio <= newprio;
  241. }
  242. /*
  243. * Adjust the priority of a task, after its pi_waiters got modified.
  244. *
  245. * This can be both boosting and unboosting. task->pi_lock must be held.
  246. */
  247. static void __rt_mutex_adjust_prio(struct task_struct *task)
  248. {
  249. int prio = rt_mutex_getprio(task);
  250. if (task->prio != prio || dl_prio(prio))
  251. rt_mutex_setprio(task, prio);
  252. }
  253. /*
  254. * Adjust task priority (undo boosting). Called from the exit path of
  255. * rt_mutex_slowunlock() and rt_mutex_slowlock().
  256. *
  257. * (Note: We do this outside of the protection of lock->wait_lock to
  258. * allow the lock to be taken while or before we readjust the priority
  259. * of task. We do not use the spin_xx_mutex() variants here as we are
  260. * outside of the debug path.)
  261. */
  262. static void rt_mutex_adjust_prio(struct task_struct *task)
  263. {
  264. unsigned long flags;
  265. raw_spin_lock_irqsave(&task->pi_lock, flags);
  266. __rt_mutex_adjust_prio(task);
  267. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  268. }
  269. /*
  270. * Max number of times we'll walk the boosting chain:
  271. */
  272. int max_lock_depth = 1024;
  273. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  274. {
  275. return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  276. }
  277. /*
  278. * Adjust the priority chain. Also used for deadlock detection.
  279. * Decreases task's usage by one - may thus free the task.
  280. *
  281. * @task: the task owning the mutex (owner) for which a chain walk is
  282. * probably needed
  283. * @deadlock_detect: do we have to carry out deadlock detection?
  284. * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
  285. * things for a task that has just got its priority adjusted, and
  286. * is waiting on a mutex)
  287. * @next_lock: the mutex on which the owner of @orig_lock was blocked before
  288. * we dropped its pi_lock. Is never dereferenced, only used for
  289. * comparison to detect lock chain changes.
  290. * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
  291. * its priority to the mutex owner (can be NULL in the case
  292. * depicted above or if the top waiter is gone away and we are
  293. * actually deboosting the owner)
  294. * @top_task: the current top waiter
  295. *
  296. * Returns 0 or -EDEADLK.
  297. *
  298. * Chain walk basics and protection scope
  299. *
  300. * [R] refcount on task
  301. * [P] task->pi_lock held
  302. * [L] rtmutex->wait_lock held
  303. *
  304. * Step Description Protected by
  305. * function arguments:
  306. * @task [R]
  307. * @orig_lock if != NULL @top_task is blocked on it
  308. * @next_lock Unprotected. Cannot be
  309. * dereferenced. Only used for
  310. * comparison.
  311. * @orig_waiter if != NULL @top_task is blocked on it
  312. * @top_task current, or in case of proxy
  313. * locking protected by calling
  314. * code
  315. * again:
  316. * loop_sanity_check();
  317. * retry:
  318. * [1] lock(task->pi_lock); [R] acquire [P]
  319. * [2] waiter = task->pi_blocked_on; [P]
  320. * [3] check_exit_conditions_1(); [P]
  321. * [4] lock = waiter->lock; [P]
  322. * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
  323. * unlock(task->pi_lock); release [P]
  324. * goto retry;
  325. * }
  326. * [6] check_exit_conditions_2(); [P] + [L]
  327. * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
  328. * [8] unlock(task->pi_lock); release [P]
  329. * put_task_struct(task); release [R]
  330. * [9] check_exit_conditions_3(); [L]
  331. * [10] task = owner(lock); [L]
  332. * get_task_struct(task); [L] acquire [R]
  333. * lock(task->pi_lock); [L] acquire [P]
  334. * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
  335. * [12] check_exit_conditions_4(); [P] + [L]
  336. * [13] unlock(task->pi_lock); release [P]
  337. * unlock(lock->wait_lock); release [L]
  338. * goto again;
  339. */
  340. static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  341. int deadlock_detect,
  342. struct rt_mutex *orig_lock,
  343. struct rt_mutex *next_lock,
  344. struct rt_mutex_waiter *orig_waiter,
  345. struct task_struct *top_task)
  346. {
  347. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  348. struct rt_mutex_waiter *prerequeue_top_waiter;
  349. int detect_deadlock, ret = 0, depth = 0;
  350. struct rt_mutex *lock;
  351. unsigned long flags;
  352. detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
  353. deadlock_detect);
  354. /*
  355. * The (de)boosting is a step by step approach with a lot of
  356. * pitfalls. We want this to be preemptible and we want hold a
  357. * maximum of two locks per step. So we have to check
  358. * carefully whether things change under us.
  359. */
  360. again:
  361. /*
  362. * We limit the lock chain length for each invocation.
  363. */
  364. if (++depth > max_lock_depth) {
  365. static int prev_max;
  366. /*
  367. * Print this only once. If the admin changes the limit,
  368. * print a new message when reaching the limit again.
  369. */
  370. if (prev_max != max_lock_depth) {
  371. prev_max = max_lock_depth;
  372. printk(KERN_WARNING "Maximum lock depth %d reached "
  373. "task: %s (%d)\n", max_lock_depth,
  374. top_task->comm, task_pid_nr(top_task));
  375. }
  376. put_task_struct(task);
  377. return -EDEADLK;
  378. }
  379. /*
  380. * We are fully preemptible here and only hold the refcount on
  381. * @task. So everything can have changed under us since the
  382. * caller or our own code below (goto retry/again) dropped all
  383. * locks.
  384. */
  385. retry:
  386. /*
  387. * [1] Task cannot go away as we did a get_task() before !
  388. */
  389. raw_spin_lock_irqsave(&task->pi_lock, flags);
  390. /*
  391. * [2] Get the waiter on which @task is blocked on.
  392. */
  393. waiter = task->pi_blocked_on;
  394. /*
  395. * [3] check_exit_conditions_1() protected by task->pi_lock.
  396. */
  397. /*
  398. * Check whether the end of the boosting chain has been
  399. * reached or the state of the chain has changed while we
  400. * dropped the locks.
  401. */
  402. if (!waiter)
  403. goto out_unlock_pi;
  404. /*
  405. * Check the orig_waiter state. After we dropped the locks,
  406. * the previous owner of the lock might have released the lock.
  407. */
  408. if (orig_waiter && !rt_mutex_owner(orig_lock))
  409. goto out_unlock_pi;
  410. /*
  411. * We dropped all locks after taking a refcount on @task, so
  412. * the task might have moved on in the lock chain or even left
  413. * the chain completely and blocks now on an unrelated lock or
  414. * on @orig_lock.
  415. *
  416. * We stored the lock on which @task was blocked in @next_lock,
  417. * so we can detect the chain change.
  418. */
  419. if (next_lock != waiter->lock)
  420. goto out_unlock_pi;
  421. /*
  422. * Drop out, when the task has no waiters. Note,
  423. * top_waiter can be NULL, when we are in the deboosting
  424. * mode!
  425. */
  426. if (top_waiter) {
  427. if (!task_has_pi_waiters(task))
  428. goto out_unlock_pi;
  429. /*
  430. * If deadlock detection is off, we stop here if we
  431. * are not the top pi waiter of the task.
  432. */
  433. if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
  434. goto out_unlock_pi;
  435. }
  436. /*
  437. * When deadlock detection is off then we check, if further
  438. * priority adjustment is necessary.
  439. */
  440. if (!detect_deadlock && waiter->prio == task->prio)
  441. goto out_unlock_pi;
  442. /*
  443. * [4] Get the next lock
  444. */
  445. lock = waiter->lock;
  446. /*
  447. * [5] We need to trylock here as we are holding task->pi_lock,
  448. * which is the reverse lock order versus the other rtmutex
  449. * operations.
  450. */
  451. if (!raw_spin_trylock(&lock->wait_lock)) {
  452. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  453. cpu_relax();
  454. goto retry;
  455. }
  456. /*
  457. * [6] check_exit_conditions_2() protected by task->pi_lock and
  458. * lock->wait_lock.
  459. *
  460. * Deadlock detection. If the lock is the same as the original
  461. * lock which caused us to walk the lock chain or if the
  462. * current lock is owned by the task which initiated the chain
  463. * walk, we detected a deadlock.
  464. */
  465. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  466. debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
  467. raw_spin_unlock(&lock->wait_lock);
  468. ret = -EDEADLK;
  469. goto out_unlock_pi;
  470. }
  471. /*
  472. * Store the current top waiter before doing the requeue
  473. * operation on @lock. We need it for the boost/deboost
  474. * decision below.
  475. */
  476. prerequeue_top_waiter = rt_mutex_top_waiter(lock);
  477. /* [7] Requeue the waiter in the lock waiter list. */
  478. rt_mutex_dequeue(lock, waiter);
  479. waiter->prio = task->prio;
  480. rt_mutex_enqueue(lock, waiter);
  481. /* [8] Release the task */
  482. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  483. put_task_struct(task);
  484. /*
  485. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  486. *
  487. * We must abort the chain walk if there is no lock owner even
  488. * in the dead lock detection case, as we have nothing to
  489. * follow here. This is the end of the chain we are walking.
  490. */
  491. if (!rt_mutex_owner(lock)) {
  492. /*
  493. * If the requeue [7] above changed the top waiter,
  494. * then we need to wake the new top waiter up to try
  495. * to get the lock.
  496. */
  497. if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  498. wake_up_process(rt_mutex_top_waiter(lock)->task);
  499. raw_spin_unlock(&lock->wait_lock);
  500. return 0;
  501. }
  502. /* [10] Grab the next task, i.e. the owner of @lock */
  503. task = rt_mutex_owner(lock);
  504. get_task_struct(task);
  505. raw_spin_lock_irqsave(&task->pi_lock, flags);
  506. /* [11] requeue the pi waiters if necessary */
  507. if (waiter == rt_mutex_top_waiter(lock)) {
  508. /*
  509. * The waiter became the new top (highest priority)
  510. * waiter on the lock. Replace the previous top waiter
  511. * in the owner tasks pi waiters list with this waiter
  512. * and adjust the priority of the owner.
  513. */
  514. rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
  515. rt_mutex_enqueue_pi(task, waiter);
  516. __rt_mutex_adjust_prio(task);
  517. } else if (prerequeue_top_waiter == waiter) {
  518. /*
  519. * The waiter was the top waiter on the lock, but is
  520. * no longer the top prority waiter. Replace waiter in
  521. * the owner tasks pi waiters list with the new top
  522. * (highest priority) waiter and adjust the priority
  523. * of the owner.
  524. * The new top waiter is stored in @waiter so that
  525. * @waiter == @top_waiter evaluates to true below and
  526. * we continue to deboost the rest of the chain.
  527. */
  528. rt_mutex_dequeue_pi(task, waiter);
  529. waiter = rt_mutex_top_waiter(lock);
  530. rt_mutex_enqueue_pi(task, waiter);
  531. __rt_mutex_adjust_prio(task);
  532. } else {
  533. /*
  534. * Nothing changed. No need to do any priority
  535. * adjustment.
  536. */
  537. }
  538. /*
  539. * [12] check_exit_conditions_4() protected by task->pi_lock
  540. * and lock->wait_lock. The actual decisions are made after we
  541. * dropped the locks.
  542. *
  543. * Check whether the task which owns the current lock is pi
  544. * blocked itself. If yes we store a pointer to the lock for
  545. * the lock chain change detection above. After we dropped
  546. * task->pi_lock next_lock cannot be dereferenced anymore.
  547. */
  548. next_lock = task_blocked_on_lock(task);
  549. /*
  550. * Store the top waiter of @lock for the end of chain walk
  551. * decision below.
  552. */
  553. top_waiter = rt_mutex_top_waiter(lock);
  554. /* [13] Drop the locks */
  555. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  556. raw_spin_unlock(&lock->wait_lock);
  557. /*
  558. * Make the actual exit decisions [12], based on the stored
  559. * values.
  560. *
  561. * We reached the end of the lock chain. Stop right here. No
  562. * point to go back just to figure that out.
  563. */
  564. if (!next_lock)
  565. goto out_put_task;
  566. /*
  567. * If the current waiter is not the top waiter on the lock,
  568. * then we can stop the chain walk here if we are not in full
  569. * deadlock detection mode.
  570. */
  571. if (!detect_deadlock && waiter != top_waiter)
  572. goto out_put_task;
  573. goto again;
  574. out_unlock_pi:
  575. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  576. out_put_task:
  577. put_task_struct(task);
  578. return ret;
  579. }
  580. /*
  581. * Try to take an rt-mutex
  582. *
  583. * Must be called with lock->wait_lock held.
  584. *
  585. * @lock: The lock to be acquired.
  586. * @task: The task which wants to acquire the lock
  587. * @waiter: The waiter that is queued to the lock's wait list if the
  588. * callsite called task_blocked_on_lock(), otherwise NULL
  589. */
  590. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  591. struct rt_mutex_waiter *waiter)
  592. {
  593. unsigned long flags;
  594. /*
  595. * Before testing whether we can acquire @lock, we set the
  596. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  597. * other tasks which try to modify @lock into the slow path
  598. * and they serialize on @lock->wait_lock.
  599. *
  600. * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
  601. * as explained at the top of this file if and only if:
  602. *
  603. * - There is a lock owner. The caller must fixup the
  604. * transient state if it does a trylock or leaves the lock
  605. * function due to a signal or timeout.
  606. *
  607. * - @task acquires the lock and there are no other
  608. * waiters. This is undone in rt_mutex_set_owner(@task) at
  609. * the end of this function.
  610. */
  611. mark_rt_mutex_waiters(lock);
  612. /*
  613. * If @lock has an owner, give up.
  614. */
  615. if (rt_mutex_owner(lock))
  616. return 0;
  617. /*
  618. * If @waiter != NULL, @task has already enqueued the waiter
  619. * into @lock waiter list. If @waiter == NULL then this is a
  620. * trylock attempt.
  621. */
  622. if (waiter) {
  623. /*
  624. * If waiter is not the highest priority waiter of
  625. * @lock, give up.
  626. */
  627. if (waiter != rt_mutex_top_waiter(lock))
  628. return 0;
  629. /*
  630. * We can acquire the lock. Remove the waiter from the
  631. * lock waiters list.
  632. */
  633. rt_mutex_dequeue(lock, waiter);
  634. } else {
  635. /*
  636. * If the lock has waiters already we check whether @task is
  637. * eligible to take over the lock.
  638. *
  639. * If there are no other waiters, @task can acquire
  640. * the lock. @task->pi_blocked_on is NULL, so it does
  641. * not need to be dequeued.
  642. */
  643. if (rt_mutex_has_waiters(lock)) {
  644. /*
  645. * If @task->prio is greater than or equal to
  646. * the top waiter priority (kernel view),
  647. * @task lost.
  648. */
  649. if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  650. return 0;
  651. /*
  652. * The current top waiter stays enqueued. We
  653. * don't have to change anything in the lock
  654. * waiters order.
  655. */
  656. } else {
  657. /*
  658. * No waiters. Take the lock without the
  659. * pi_lock dance.@task->pi_blocked_on is NULL
  660. * and we have no waiters to enqueue in @task
  661. * pi waiters list.
  662. */
  663. goto takeit;
  664. }
  665. }
  666. /*
  667. * Clear @task->pi_blocked_on. Requires protection by
  668. * @task->pi_lock. Redundant operation for the @waiter == NULL
  669. * case, but conditionals are more expensive than a redundant
  670. * store.
  671. */
  672. raw_spin_lock_irqsave(&task->pi_lock, flags);
  673. task->pi_blocked_on = NULL;
  674. /*
  675. * Finish the lock acquisition. @task is the new owner. If
  676. * other waiters exist we have to insert the highest priority
  677. * waiter into @task->pi_waiters list.
  678. */
  679. if (rt_mutex_has_waiters(lock))
  680. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  681. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  682. takeit:
  683. /* We got the lock. */
  684. debug_rt_mutex_lock(lock);
  685. /*
  686. * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
  687. * are still waiters or clears it.
  688. */
  689. rt_mutex_set_owner(lock, task);
  690. rt_mutex_deadlock_account_lock(lock, task);
  691. return 1;
  692. }
  693. /*
  694. * Task blocks on lock.
  695. *
  696. * Prepare waiter and propagate pi chain
  697. *
  698. * This must be called with lock->wait_lock held.
  699. */
  700. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  701. struct rt_mutex_waiter *waiter,
  702. struct task_struct *task,
  703. int detect_deadlock)
  704. {
  705. struct task_struct *owner = rt_mutex_owner(lock);
  706. struct rt_mutex_waiter *top_waiter = waiter;
  707. struct rt_mutex *next_lock;
  708. int chain_walk = 0, res;
  709. unsigned long flags;
  710. /*
  711. * Early deadlock detection. We really don't want the task to
  712. * enqueue on itself just to untangle the mess later. It's not
  713. * only an optimization. We drop the locks, so another waiter
  714. * can come in before the chain walk detects the deadlock. So
  715. * the other will detect the deadlock and return -EDEADLOCK,
  716. * which is wrong, as the other waiter is not in a deadlock
  717. * situation.
  718. */
  719. if (owner == task)
  720. return -EDEADLK;
  721. raw_spin_lock_irqsave(&task->pi_lock, flags);
  722. __rt_mutex_adjust_prio(task);
  723. waiter->task = task;
  724. waiter->lock = lock;
  725. waiter->prio = task->prio;
  726. /* Get the top priority waiter on the lock */
  727. if (rt_mutex_has_waiters(lock))
  728. top_waiter = rt_mutex_top_waiter(lock);
  729. rt_mutex_enqueue(lock, waiter);
  730. task->pi_blocked_on = waiter;
  731. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  732. if (!owner)
  733. return 0;
  734. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  735. if (waiter == rt_mutex_top_waiter(lock)) {
  736. rt_mutex_dequeue_pi(owner, top_waiter);
  737. rt_mutex_enqueue_pi(owner, waiter);
  738. __rt_mutex_adjust_prio(owner);
  739. if (owner->pi_blocked_on)
  740. chain_walk = 1;
  741. } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
  742. chain_walk = 1;
  743. }
  744. /* Store the lock on which owner is blocked or NULL */
  745. next_lock = task_blocked_on_lock(owner);
  746. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  747. /*
  748. * Even if full deadlock detection is on, if the owner is not
  749. * blocked itself, we can avoid finding this out in the chain
  750. * walk.
  751. */
  752. if (!chain_walk || !next_lock)
  753. return 0;
  754. /*
  755. * The owner can't disappear while holding a lock,
  756. * so the owner struct is protected by wait_lock.
  757. * Gets dropped in rt_mutex_adjust_prio_chain()!
  758. */
  759. get_task_struct(owner);
  760. raw_spin_unlock(&lock->wait_lock);
  761. res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
  762. next_lock, waiter, task);
  763. raw_spin_lock(&lock->wait_lock);
  764. return res;
  765. }
  766. /*
  767. * Wake up the next waiter on the lock.
  768. *
  769. * Remove the top waiter from the current tasks pi waiter list and
  770. * wake it up.
  771. *
  772. * Called with lock->wait_lock held.
  773. */
  774. static void wakeup_next_waiter(struct rt_mutex *lock)
  775. {
  776. struct rt_mutex_waiter *waiter;
  777. unsigned long flags;
  778. raw_spin_lock_irqsave(&current->pi_lock, flags);
  779. waiter = rt_mutex_top_waiter(lock);
  780. /*
  781. * Remove it from current->pi_waiters. We do not adjust a
  782. * possible priority boost right now. We execute wakeup in the
  783. * boosted mode and go back to normal after releasing
  784. * lock->wait_lock.
  785. */
  786. rt_mutex_dequeue_pi(current, waiter);
  787. /*
  788. * As we are waking up the top waiter, and the waiter stays
  789. * queued on the lock until it gets the lock, this lock
  790. * obviously has waiters. Just set the bit here and this has
  791. * the added benefit of forcing all new tasks into the
  792. * slow path making sure no task of lower priority than
  793. * the top waiter can steal this lock.
  794. */
  795. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  796. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  797. /*
  798. * It's safe to dereference waiter as it cannot go away as
  799. * long as we hold lock->wait_lock. The waiter task needs to
  800. * acquire it in order to dequeue the waiter.
  801. */
  802. wake_up_process(waiter->task);
  803. }
  804. /*
  805. * Remove a waiter from a lock and give up
  806. *
  807. * Must be called with lock->wait_lock held and
  808. * have just failed to try_to_take_rt_mutex().
  809. */
  810. static void remove_waiter(struct rt_mutex *lock,
  811. struct rt_mutex_waiter *waiter)
  812. {
  813. int first = (waiter == rt_mutex_top_waiter(lock));
  814. struct task_struct *owner = rt_mutex_owner(lock);
  815. struct rt_mutex *next_lock = NULL;
  816. unsigned long flags;
  817. raw_spin_lock_irqsave(&current->pi_lock, flags);
  818. rt_mutex_dequeue(lock, waiter);
  819. current->pi_blocked_on = NULL;
  820. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  821. if (!owner)
  822. return;
  823. if (first) {
  824. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  825. rt_mutex_dequeue_pi(owner, waiter);
  826. if (rt_mutex_has_waiters(lock)) {
  827. struct rt_mutex_waiter *next;
  828. next = rt_mutex_top_waiter(lock);
  829. rt_mutex_enqueue_pi(owner, next);
  830. }
  831. __rt_mutex_adjust_prio(owner);
  832. /* Store the lock on which owner is blocked or NULL */
  833. next_lock = task_blocked_on_lock(owner);
  834. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  835. }
  836. if (!next_lock)
  837. return;
  838. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  839. get_task_struct(owner);
  840. raw_spin_unlock(&lock->wait_lock);
  841. rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
  842. raw_spin_lock(&lock->wait_lock);
  843. }
  844. /*
  845. * Recheck the pi chain, in case we got a priority setting
  846. *
  847. * Called from sched_setscheduler
  848. */
  849. void rt_mutex_adjust_pi(struct task_struct *task)
  850. {
  851. struct rt_mutex_waiter *waiter;
  852. struct rt_mutex *next_lock;
  853. unsigned long flags;
  854. raw_spin_lock_irqsave(&task->pi_lock, flags);
  855. waiter = task->pi_blocked_on;
  856. if (!waiter || (waiter->prio == task->prio &&
  857. !dl_prio(task->prio))) {
  858. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  859. return;
  860. }
  861. next_lock = waiter->lock;
  862. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  863. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  864. get_task_struct(task);
  865. rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
  866. }
  867. /**
  868. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  869. * @lock: the rt_mutex to take
  870. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  871. * or TASK_UNINTERRUPTIBLE)
  872. * @timeout: the pre-initialized and started timer, or NULL for none
  873. * @waiter: the pre-initialized rt_mutex_waiter
  874. *
  875. * lock->wait_lock must be held by the caller.
  876. */
  877. static int __sched
  878. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  879. struct hrtimer_sleeper *timeout,
  880. struct rt_mutex_waiter *waiter)
  881. {
  882. int ret = 0;
  883. for (;;) {
  884. /* Try to acquire the lock: */
  885. if (try_to_take_rt_mutex(lock, current, waiter))
  886. break;
  887. /*
  888. * TASK_INTERRUPTIBLE checks for signals and
  889. * timeout. Ignored otherwise.
  890. */
  891. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  892. /* Signal pending? */
  893. if (signal_pending(current))
  894. ret = -EINTR;
  895. if (timeout && !timeout->task)
  896. ret = -ETIMEDOUT;
  897. if (ret)
  898. break;
  899. }
  900. raw_spin_unlock(&lock->wait_lock);
  901. debug_rt_mutex_print_deadlock(waiter);
  902. schedule_rt_mutex(lock);
  903. raw_spin_lock(&lock->wait_lock);
  904. set_current_state(state);
  905. }
  906. return ret;
  907. }
  908. static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  909. struct rt_mutex_waiter *w)
  910. {
  911. /*
  912. * If the result is not -EDEADLOCK or the caller requested
  913. * deadlock detection, nothing to do here.
  914. */
  915. if (res != -EDEADLOCK || detect_deadlock)
  916. return;
  917. /*
  918. * Yell lowdly and stop the task right here.
  919. */
  920. rt_mutex_print_deadlock(w);
  921. while (1) {
  922. set_current_state(TASK_INTERRUPTIBLE);
  923. schedule();
  924. }
  925. }
  926. /*
  927. * Slow path lock function:
  928. */
  929. static int __sched
  930. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  931. struct hrtimer_sleeper *timeout,
  932. int detect_deadlock)
  933. {
  934. struct rt_mutex_waiter waiter;
  935. int ret = 0;
  936. debug_rt_mutex_init_waiter(&waiter);
  937. RB_CLEAR_NODE(&waiter.pi_tree_entry);
  938. RB_CLEAR_NODE(&waiter.tree_entry);
  939. raw_spin_lock(&lock->wait_lock);
  940. /* Try to acquire the lock again: */
  941. if (try_to_take_rt_mutex(lock, current, NULL)) {
  942. raw_spin_unlock(&lock->wait_lock);
  943. return 0;
  944. }
  945. set_current_state(state);
  946. /* Setup the timer, when timeout != NULL */
  947. if (unlikely(timeout)) {
  948. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  949. if (!hrtimer_active(&timeout->timer))
  950. timeout->task = NULL;
  951. }
  952. ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
  953. if (likely(!ret))
  954. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  955. set_current_state(TASK_RUNNING);
  956. if (unlikely(ret)) {
  957. remove_waiter(lock, &waiter);
  958. rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
  959. }
  960. /*
  961. * try_to_take_rt_mutex() sets the waiter bit
  962. * unconditionally. We might have to fix that up.
  963. */
  964. fixup_rt_mutex_waiters(lock);
  965. raw_spin_unlock(&lock->wait_lock);
  966. /* Remove pending timer: */
  967. if (unlikely(timeout))
  968. hrtimer_cancel(&timeout->timer);
  969. debug_rt_mutex_free_waiter(&waiter);
  970. return ret;
  971. }
  972. /*
  973. * Slow path try-lock function:
  974. */
  975. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  976. {
  977. int ret;
  978. /*
  979. * If the lock already has an owner we fail to get the lock.
  980. * This can be done without taking the @lock->wait_lock as
  981. * it is only being read, and this is a trylock anyway.
  982. */
  983. if (rt_mutex_owner(lock))
  984. return 0;
  985. /*
  986. * The mutex has currently no owner. Lock the wait lock and
  987. * try to acquire the lock.
  988. */
  989. raw_spin_lock(&lock->wait_lock);
  990. ret = try_to_take_rt_mutex(lock, current, NULL);
  991. /*
  992. * try_to_take_rt_mutex() sets the lock waiters bit
  993. * unconditionally. Clean this up.
  994. */
  995. fixup_rt_mutex_waiters(lock);
  996. raw_spin_unlock(&lock->wait_lock);
  997. return ret;
  998. }
  999. /*
  1000. * Slow path to release a rt-mutex:
  1001. */
  1002. static void __sched
  1003. rt_mutex_slowunlock(struct rt_mutex *lock)
  1004. {
  1005. raw_spin_lock(&lock->wait_lock);
  1006. debug_rt_mutex_unlock(lock);
  1007. rt_mutex_deadlock_account_unlock(current);
  1008. /*
  1009. * We must be careful here if the fast path is enabled. If we
  1010. * have no waiters queued we cannot set owner to NULL here
  1011. * because of:
  1012. *
  1013. * foo->lock->owner = NULL;
  1014. * rtmutex_lock(foo->lock); <- fast path
  1015. * free = atomic_dec_and_test(foo->refcnt);
  1016. * rtmutex_unlock(foo->lock); <- fast path
  1017. * if (free)
  1018. * kfree(foo);
  1019. * raw_spin_unlock(foo->lock->wait_lock);
  1020. *
  1021. * So for the fastpath enabled kernel:
  1022. *
  1023. * Nothing can set the waiters bit as long as we hold
  1024. * lock->wait_lock. So we do the following sequence:
  1025. *
  1026. * owner = rt_mutex_owner(lock);
  1027. * clear_rt_mutex_waiters(lock);
  1028. * raw_spin_unlock(&lock->wait_lock);
  1029. * if (cmpxchg(&lock->owner, owner, 0) == owner)
  1030. * return;
  1031. * goto retry;
  1032. *
  1033. * The fastpath disabled variant is simple as all access to
  1034. * lock->owner is serialized by lock->wait_lock:
  1035. *
  1036. * lock->owner = NULL;
  1037. * raw_spin_unlock(&lock->wait_lock);
  1038. */
  1039. while (!rt_mutex_has_waiters(lock)) {
  1040. /* Drops lock->wait_lock ! */
  1041. if (unlock_rt_mutex_safe(lock) == true)
  1042. return;
  1043. /* Relock the rtmutex and try again */
  1044. raw_spin_lock(&lock->wait_lock);
  1045. }
  1046. /*
  1047. * The wakeup next waiter path does not suffer from the above
  1048. * race. See the comments there.
  1049. */
  1050. wakeup_next_waiter(lock);
  1051. raw_spin_unlock(&lock->wait_lock);
  1052. /* Undo pi boosting if necessary: */
  1053. rt_mutex_adjust_prio(current);
  1054. }
  1055. /*
  1056. * debug aware fast / slowpath lock,trylock,unlock
  1057. *
  1058. * The atomic acquire/release ops are compiled away, when either the
  1059. * architecture does not support cmpxchg or when debugging is enabled.
  1060. */
  1061. static inline int
  1062. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  1063. int detect_deadlock,
  1064. int (*slowfn)(struct rt_mutex *lock, int state,
  1065. struct hrtimer_sleeper *timeout,
  1066. int detect_deadlock))
  1067. {
  1068. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  1069. rt_mutex_deadlock_account_lock(lock, current);
  1070. return 0;
  1071. } else
  1072. return slowfn(lock, state, NULL, detect_deadlock);
  1073. }
  1074. static inline int
  1075. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  1076. struct hrtimer_sleeper *timeout, int detect_deadlock,
  1077. int (*slowfn)(struct rt_mutex *lock, int state,
  1078. struct hrtimer_sleeper *timeout,
  1079. int detect_deadlock))
  1080. {
  1081. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  1082. rt_mutex_deadlock_account_lock(lock, current);
  1083. return 0;
  1084. } else
  1085. return slowfn(lock, state, timeout, detect_deadlock);
  1086. }
  1087. static inline int
  1088. rt_mutex_fasttrylock(struct rt_mutex *lock,
  1089. int (*slowfn)(struct rt_mutex *lock))
  1090. {
  1091. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  1092. rt_mutex_deadlock_account_lock(lock, current);
  1093. return 1;
  1094. }
  1095. return slowfn(lock);
  1096. }
  1097. static inline void
  1098. rt_mutex_fastunlock(struct rt_mutex *lock,
  1099. void (*slowfn)(struct rt_mutex *lock))
  1100. {
  1101. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  1102. rt_mutex_deadlock_account_unlock(current);
  1103. else
  1104. slowfn(lock);
  1105. }
  1106. /**
  1107. * rt_mutex_lock - lock a rt_mutex
  1108. *
  1109. * @lock: the rt_mutex to be locked
  1110. */
  1111. void __sched rt_mutex_lock(struct rt_mutex *lock)
  1112. {
  1113. might_sleep();
  1114. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
  1115. }
  1116. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  1117. /**
  1118. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  1119. *
  1120. * @lock: the rt_mutex to be locked
  1121. * @detect_deadlock: deadlock detection on/off
  1122. *
  1123. * Returns:
  1124. * 0 on success
  1125. * -EINTR when interrupted by a signal
  1126. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  1127. */
  1128. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
  1129. int detect_deadlock)
  1130. {
  1131. might_sleep();
  1132. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
  1133. detect_deadlock, rt_mutex_slowlock);
  1134. }
  1135. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  1136. /**
  1137. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  1138. * the timeout structure is provided
  1139. * by the caller
  1140. *
  1141. * @lock: the rt_mutex to be locked
  1142. * @timeout: timeout structure or NULL (no timeout)
  1143. * @detect_deadlock: deadlock detection on/off
  1144. *
  1145. * Returns:
  1146. * 0 on success
  1147. * -EINTR when interrupted by a signal
  1148. * -ETIMEDOUT when the timeout expired
  1149. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  1150. */
  1151. int
  1152. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
  1153. int detect_deadlock)
  1154. {
  1155. might_sleep();
  1156. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1157. detect_deadlock, rt_mutex_slowlock);
  1158. }
  1159. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  1160. /**
  1161. * rt_mutex_trylock - try to lock a rt_mutex
  1162. *
  1163. * @lock: the rt_mutex to be locked
  1164. *
  1165. * Returns 1 on success and 0 on contention
  1166. */
  1167. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  1168. {
  1169. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  1170. }
  1171. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  1172. /**
  1173. * rt_mutex_unlock - unlock a rt_mutex
  1174. *
  1175. * @lock: the rt_mutex to be unlocked
  1176. */
  1177. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  1178. {
  1179. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  1180. }
  1181. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  1182. /**
  1183. * rt_mutex_destroy - mark a mutex unusable
  1184. * @lock: the mutex to be destroyed
  1185. *
  1186. * This function marks the mutex uninitialized, and any subsequent
  1187. * use of the mutex is forbidden. The mutex must not be locked when
  1188. * this function is called.
  1189. */
  1190. void rt_mutex_destroy(struct rt_mutex *lock)
  1191. {
  1192. WARN_ON(rt_mutex_is_locked(lock));
  1193. #ifdef CONFIG_DEBUG_RT_MUTEXES
  1194. lock->magic = NULL;
  1195. #endif
  1196. }
  1197. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  1198. /**
  1199. * __rt_mutex_init - initialize the rt lock
  1200. *
  1201. * @lock: the rt lock to be initialized
  1202. *
  1203. * Initialize the rt lock to unlocked state.
  1204. *
  1205. * Initializing of a locked rt lock is not allowed
  1206. */
  1207. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  1208. {
  1209. lock->owner = NULL;
  1210. raw_spin_lock_init(&lock->wait_lock);
  1211. lock->waiters = RB_ROOT;
  1212. lock->waiters_leftmost = NULL;
  1213. debug_rt_mutex_init(lock, name);
  1214. }
  1215. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  1216. /**
  1217. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  1218. * proxy owner
  1219. *
  1220. * @lock: the rt_mutex to be locked
  1221. * @proxy_owner:the task to set as owner
  1222. *
  1223. * No locking. Caller has to do serializing itself
  1224. * Special API call for PI-futex support
  1225. */
  1226. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  1227. struct task_struct *proxy_owner)
  1228. {
  1229. __rt_mutex_init(lock, NULL);
  1230. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  1231. rt_mutex_set_owner(lock, proxy_owner);
  1232. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  1233. }
  1234. /**
  1235. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  1236. *
  1237. * @lock: the rt_mutex to be locked
  1238. *
  1239. * No locking. Caller has to do serializing itself
  1240. * Special API call for PI-futex support
  1241. */
  1242. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  1243. struct task_struct *proxy_owner)
  1244. {
  1245. debug_rt_mutex_proxy_unlock(lock);
  1246. rt_mutex_set_owner(lock, NULL);
  1247. rt_mutex_deadlock_account_unlock(proxy_owner);
  1248. }
  1249. /**
  1250. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  1251. * @lock: the rt_mutex to take
  1252. * @waiter: the pre-initialized rt_mutex_waiter
  1253. * @task: the task to prepare
  1254. * @detect_deadlock: perform deadlock detection (1) or not (0)
  1255. *
  1256. * Returns:
  1257. * 0 - task blocked on lock
  1258. * 1 - acquired the lock for task, caller should wake it up
  1259. * <0 - error
  1260. *
  1261. * Special API call for FUTEX_REQUEUE_PI support.
  1262. */
  1263. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  1264. struct rt_mutex_waiter *waiter,
  1265. struct task_struct *task, int detect_deadlock)
  1266. {
  1267. int ret;
  1268. raw_spin_lock(&lock->wait_lock);
  1269. if (try_to_take_rt_mutex(lock, task, NULL)) {
  1270. raw_spin_unlock(&lock->wait_lock);
  1271. return 1;
  1272. }
  1273. /* We enforce deadlock detection for futexes */
  1274. ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
  1275. if (ret && !rt_mutex_owner(lock)) {
  1276. /*
  1277. * Reset the return value. We might have
  1278. * returned with -EDEADLK and the owner
  1279. * released the lock while we were walking the
  1280. * pi chain. Let the waiter sort it out.
  1281. */
  1282. ret = 0;
  1283. }
  1284. if (unlikely(ret))
  1285. remove_waiter(lock, waiter);
  1286. raw_spin_unlock(&lock->wait_lock);
  1287. debug_rt_mutex_print_deadlock(waiter);
  1288. return ret;
  1289. }
  1290. /**
  1291. * rt_mutex_next_owner - return the next owner of the lock
  1292. *
  1293. * @lock: the rt lock query
  1294. *
  1295. * Returns the next owner of the lock or NULL
  1296. *
  1297. * Caller has to serialize against other accessors to the lock
  1298. * itself.
  1299. *
  1300. * Special API call for PI-futex support
  1301. */
  1302. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  1303. {
  1304. if (!rt_mutex_has_waiters(lock))
  1305. return NULL;
  1306. return rt_mutex_top_waiter(lock)->task;
  1307. }
  1308. /**
  1309. * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  1310. * @lock: the rt_mutex we were woken on
  1311. * @to: the timeout, null if none. hrtimer should already have
  1312. * been started.
  1313. * @waiter: the pre-initialized rt_mutex_waiter
  1314. * @detect_deadlock: perform deadlock detection (1) or not (0)
  1315. *
  1316. * Complete the lock acquisition started our behalf by another thread.
  1317. *
  1318. * Returns:
  1319. * 0 - success
  1320. * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
  1321. *
  1322. * Special API call for PI-futex requeue support
  1323. */
  1324. int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  1325. struct hrtimer_sleeper *to,
  1326. struct rt_mutex_waiter *waiter,
  1327. int detect_deadlock)
  1328. {
  1329. int ret;
  1330. raw_spin_lock(&lock->wait_lock);
  1331. set_current_state(TASK_INTERRUPTIBLE);
  1332. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  1333. set_current_state(TASK_RUNNING);
  1334. if (unlikely(ret))
  1335. remove_waiter(lock, waiter);
  1336. /*
  1337. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1338. * have to fix that up.
  1339. */
  1340. fixup_rt_mutex_waiters(lock);
  1341. raw_spin_unlock(&lock->wait_lock);
  1342. return ret;
  1343. }