rtmutex.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  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/locking/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. * Deadlock detection is conditional:
  271. *
  272. * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
  273. * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
  274. *
  275. * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
  276. * conducted independent of the detect argument.
  277. *
  278. * If the waiter argument is NULL this indicates the deboost path and
  279. * deadlock detection is disabled independent of the detect argument
  280. * and the config settings.
  281. */
  282. static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  283. enum rtmutex_chainwalk chwalk)
  284. {
  285. /*
  286. * This is just a wrapper function for the following call,
  287. * because debug_rt_mutex_detect_deadlock() smells like a magic
  288. * debug feature and I wanted to keep the cond function in the
  289. * main source file along with the comments instead of having
  290. * two of the same in the headers.
  291. */
  292. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  293. }
  294. /*
  295. * Max number of times we'll walk the boosting chain:
  296. */
  297. int max_lock_depth = 1024;
  298. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  299. {
  300. return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  301. }
  302. /*
  303. * Adjust the priority chain. Also used for deadlock detection.
  304. * Decreases task's usage by one - may thus free the task.
  305. *
  306. * @task: the task owning the mutex (owner) for which a chain walk is
  307. * probably needed
  308. * @deadlock_detect: do we have to carry out deadlock detection?
  309. * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
  310. * things for a task that has just got its priority adjusted, and
  311. * is waiting on a mutex)
  312. * @next_lock: the mutex on which the owner of @orig_lock was blocked before
  313. * we dropped its pi_lock. Is never dereferenced, only used for
  314. * comparison to detect lock chain changes.
  315. * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
  316. * its priority to the mutex owner (can be NULL in the case
  317. * depicted above or if the top waiter is gone away and we are
  318. * actually deboosting the owner)
  319. * @top_task: the current top waiter
  320. *
  321. * Returns 0 or -EDEADLK.
  322. *
  323. * Chain walk basics and protection scope
  324. *
  325. * [R] refcount on task
  326. * [P] task->pi_lock held
  327. * [L] rtmutex->wait_lock held
  328. *
  329. * Step Description Protected by
  330. * function arguments:
  331. * @task [R]
  332. * @orig_lock if != NULL @top_task is blocked on it
  333. * @next_lock Unprotected. Cannot be
  334. * dereferenced. Only used for
  335. * comparison.
  336. * @orig_waiter if != NULL @top_task is blocked on it
  337. * @top_task current, or in case of proxy
  338. * locking protected by calling
  339. * code
  340. * again:
  341. * loop_sanity_check();
  342. * retry:
  343. * [1] lock(task->pi_lock); [R] acquire [P]
  344. * [2] waiter = task->pi_blocked_on; [P]
  345. * [3] check_exit_conditions_1(); [P]
  346. * [4] lock = waiter->lock; [P]
  347. * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
  348. * unlock(task->pi_lock); release [P]
  349. * goto retry;
  350. * }
  351. * [6] check_exit_conditions_2(); [P] + [L]
  352. * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
  353. * [8] unlock(task->pi_lock); release [P]
  354. * put_task_struct(task); release [R]
  355. * [9] check_exit_conditions_3(); [L]
  356. * [10] task = owner(lock); [L]
  357. * get_task_struct(task); [L] acquire [R]
  358. * lock(task->pi_lock); [L] acquire [P]
  359. * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
  360. * [12] check_exit_conditions_4(); [P] + [L]
  361. * [13] unlock(task->pi_lock); release [P]
  362. * unlock(lock->wait_lock); release [L]
  363. * goto again;
  364. */
  365. static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  366. enum rtmutex_chainwalk chwalk,
  367. struct rt_mutex *orig_lock,
  368. struct rt_mutex *next_lock,
  369. struct rt_mutex_waiter *orig_waiter,
  370. struct task_struct *top_task)
  371. {
  372. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  373. struct rt_mutex_waiter *prerequeue_top_waiter;
  374. int ret = 0, depth = 0;
  375. struct rt_mutex *lock;
  376. bool detect_deadlock;
  377. unsigned long flags;
  378. bool requeue = true;
  379. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  380. /*
  381. * The (de)boosting is a step by step approach with a lot of
  382. * pitfalls. We want this to be preemptible and we want hold a
  383. * maximum of two locks per step. So we have to check
  384. * carefully whether things change under us.
  385. */
  386. again:
  387. /*
  388. * We limit the lock chain length for each invocation.
  389. */
  390. if (++depth > max_lock_depth) {
  391. static int prev_max;
  392. /*
  393. * Print this only once. If the admin changes the limit,
  394. * print a new message when reaching the limit again.
  395. */
  396. if (prev_max != max_lock_depth) {
  397. prev_max = max_lock_depth;
  398. printk(KERN_WARNING "Maximum lock depth %d reached "
  399. "task: %s (%d)\n", max_lock_depth,
  400. top_task->comm, task_pid_nr(top_task));
  401. }
  402. put_task_struct(task);
  403. return -EDEADLK;
  404. }
  405. /*
  406. * We are fully preemptible here and only hold the refcount on
  407. * @task. So everything can have changed under us since the
  408. * caller or our own code below (goto retry/again) dropped all
  409. * locks.
  410. */
  411. retry:
  412. /*
  413. * [1] Task cannot go away as we did a get_task() before !
  414. */
  415. raw_spin_lock_irqsave(&task->pi_lock, flags);
  416. /*
  417. * [2] Get the waiter on which @task is blocked on.
  418. */
  419. waiter = task->pi_blocked_on;
  420. /*
  421. * [3] check_exit_conditions_1() protected by task->pi_lock.
  422. */
  423. /*
  424. * Check whether the end of the boosting chain has been
  425. * reached or the state of the chain has changed while we
  426. * dropped the locks.
  427. */
  428. if (!waiter)
  429. goto out_unlock_pi;
  430. /*
  431. * Check the orig_waiter state. After we dropped the locks,
  432. * the previous owner of the lock might have released the lock.
  433. */
  434. if (orig_waiter && !rt_mutex_owner(orig_lock))
  435. goto out_unlock_pi;
  436. /*
  437. * We dropped all locks after taking a refcount on @task, so
  438. * the task might have moved on in the lock chain or even left
  439. * the chain completely and blocks now on an unrelated lock or
  440. * on @orig_lock.
  441. *
  442. * We stored the lock on which @task was blocked in @next_lock,
  443. * so we can detect the chain change.
  444. */
  445. if (next_lock != waiter->lock)
  446. goto out_unlock_pi;
  447. /*
  448. * Drop out, when the task has no waiters. Note,
  449. * top_waiter can be NULL, when we are in the deboosting
  450. * mode!
  451. */
  452. if (top_waiter) {
  453. if (!task_has_pi_waiters(task))
  454. goto out_unlock_pi;
  455. /*
  456. * If deadlock detection is off, we stop here if we
  457. * are not the top pi waiter of the task. If deadlock
  458. * detection is enabled we continue, but stop the
  459. * requeueing in the chain walk.
  460. */
  461. if (top_waiter != task_top_pi_waiter(task)) {
  462. if (!detect_deadlock)
  463. goto out_unlock_pi;
  464. else
  465. requeue = false;
  466. }
  467. }
  468. /*
  469. * If the waiter priority is the same as the task priority
  470. * then there is no further priority adjustment necessary. If
  471. * deadlock detection is off, we stop the chain walk. If its
  472. * enabled we continue, but stop the requeueing in the chain
  473. * walk.
  474. */
  475. if (waiter->prio == task->prio) {
  476. if (!detect_deadlock)
  477. goto out_unlock_pi;
  478. else
  479. requeue = false;
  480. }
  481. /*
  482. * [4] Get the next lock
  483. */
  484. lock = waiter->lock;
  485. /*
  486. * [5] We need to trylock here as we are holding task->pi_lock,
  487. * which is the reverse lock order versus the other rtmutex
  488. * operations.
  489. */
  490. if (!raw_spin_trylock(&lock->wait_lock)) {
  491. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  492. cpu_relax();
  493. goto retry;
  494. }
  495. /*
  496. * [6] check_exit_conditions_2() protected by task->pi_lock and
  497. * lock->wait_lock.
  498. *
  499. * Deadlock detection. If the lock is the same as the original
  500. * lock which caused us to walk the lock chain or if the
  501. * current lock is owned by the task which initiated the chain
  502. * walk, we detected a deadlock.
  503. */
  504. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  505. debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
  506. raw_spin_unlock(&lock->wait_lock);
  507. ret = -EDEADLK;
  508. goto out_unlock_pi;
  509. }
  510. /*
  511. * If we just follow the lock chain for deadlock detection, no
  512. * need to do all the requeue operations. To avoid a truckload
  513. * of conditionals around the various places below, just do the
  514. * minimum chain walk checks.
  515. */
  516. if (!requeue) {
  517. /*
  518. * No requeue[7] here. Just release @task [8]
  519. */
  520. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  521. put_task_struct(task);
  522. /*
  523. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  524. * If there is no owner of the lock, end of chain.
  525. */
  526. if (!rt_mutex_owner(lock)) {
  527. raw_spin_unlock(&lock->wait_lock);
  528. return 0;
  529. }
  530. /* [10] Grab the next task, i.e. owner of @lock */
  531. task = rt_mutex_owner(lock);
  532. get_task_struct(task);
  533. raw_spin_lock_irqsave(&task->pi_lock, flags);
  534. /*
  535. * No requeue [11] here. We just do deadlock detection.
  536. *
  537. * [12] Store whether owner is blocked
  538. * itself. Decision is made after dropping the locks
  539. */
  540. next_lock = task_blocked_on_lock(task);
  541. /*
  542. * Get the top waiter for the next iteration
  543. */
  544. top_waiter = rt_mutex_top_waiter(lock);
  545. /* [13] Drop locks */
  546. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  547. raw_spin_unlock(&lock->wait_lock);
  548. /* If owner is not blocked, end of chain. */
  549. if (!next_lock)
  550. goto out_put_task;
  551. goto again;
  552. }
  553. /*
  554. * Store the current top waiter before doing the requeue
  555. * operation on @lock. We need it for the boost/deboost
  556. * decision below.
  557. */
  558. prerequeue_top_waiter = rt_mutex_top_waiter(lock);
  559. /* [7] Requeue the waiter in the lock waiter list. */
  560. rt_mutex_dequeue(lock, waiter);
  561. waiter->prio = task->prio;
  562. rt_mutex_enqueue(lock, waiter);
  563. /* [8] Release the task */
  564. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  565. put_task_struct(task);
  566. /*
  567. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  568. *
  569. * We must abort the chain walk if there is no lock owner even
  570. * in the dead lock detection case, as we have nothing to
  571. * follow here. This is the end of the chain we are walking.
  572. */
  573. if (!rt_mutex_owner(lock)) {
  574. /*
  575. * If the requeue [7] above changed the top waiter,
  576. * then we need to wake the new top waiter up to try
  577. * to get the lock.
  578. */
  579. if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  580. wake_up_process(rt_mutex_top_waiter(lock)->task);
  581. raw_spin_unlock(&lock->wait_lock);
  582. return 0;
  583. }
  584. /* [10] Grab the next task, i.e. the owner of @lock */
  585. task = rt_mutex_owner(lock);
  586. get_task_struct(task);
  587. raw_spin_lock_irqsave(&task->pi_lock, flags);
  588. /* [11] requeue the pi waiters if necessary */
  589. if (waiter == rt_mutex_top_waiter(lock)) {
  590. /*
  591. * The waiter became the new top (highest priority)
  592. * waiter on the lock. Replace the previous top waiter
  593. * in the owner tasks pi waiters list with this waiter
  594. * and adjust the priority of the owner.
  595. */
  596. rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
  597. rt_mutex_enqueue_pi(task, waiter);
  598. __rt_mutex_adjust_prio(task);
  599. } else if (prerequeue_top_waiter == waiter) {
  600. /*
  601. * The waiter was the top waiter on the lock, but is
  602. * no longer the top prority waiter. Replace waiter in
  603. * the owner tasks pi waiters list with the new top
  604. * (highest priority) waiter and adjust the priority
  605. * of the owner.
  606. * The new top waiter is stored in @waiter so that
  607. * @waiter == @top_waiter evaluates to true below and
  608. * we continue to deboost the rest of the chain.
  609. */
  610. rt_mutex_dequeue_pi(task, waiter);
  611. waiter = rt_mutex_top_waiter(lock);
  612. rt_mutex_enqueue_pi(task, waiter);
  613. __rt_mutex_adjust_prio(task);
  614. } else {
  615. /*
  616. * Nothing changed. No need to do any priority
  617. * adjustment.
  618. */
  619. }
  620. /*
  621. * [12] check_exit_conditions_4() protected by task->pi_lock
  622. * and lock->wait_lock. The actual decisions are made after we
  623. * dropped the locks.
  624. *
  625. * Check whether the task which owns the current lock is pi
  626. * blocked itself. If yes we store a pointer to the lock for
  627. * the lock chain change detection above. After we dropped
  628. * task->pi_lock next_lock cannot be dereferenced anymore.
  629. */
  630. next_lock = task_blocked_on_lock(task);
  631. /*
  632. * Store the top waiter of @lock for the end of chain walk
  633. * decision below.
  634. */
  635. top_waiter = rt_mutex_top_waiter(lock);
  636. /* [13] Drop the locks */
  637. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  638. raw_spin_unlock(&lock->wait_lock);
  639. /*
  640. * Make the actual exit decisions [12], based on the stored
  641. * values.
  642. *
  643. * We reached the end of the lock chain. Stop right here. No
  644. * point to go back just to figure that out.
  645. */
  646. if (!next_lock)
  647. goto out_put_task;
  648. /*
  649. * If the current waiter is not the top waiter on the lock,
  650. * then we can stop the chain walk here if we are not in full
  651. * deadlock detection mode.
  652. */
  653. if (!detect_deadlock && waiter != top_waiter)
  654. goto out_put_task;
  655. goto again;
  656. out_unlock_pi:
  657. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  658. out_put_task:
  659. put_task_struct(task);
  660. return ret;
  661. }
  662. /*
  663. * Try to take an rt-mutex
  664. *
  665. * Must be called with lock->wait_lock held.
  666. *
  667. * @lock: The lock to be acquired.
  668. * @task: The task which wants to acquire the lock
  669. * @waiter: The waiter that is queued to the lock's wait list if the
  670. * callsite called task_blocked_on_lock(), otherwise NULL
  671. */
  672. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  673. struct rt_mutex_waiter *waiter)
  674. {
  675. unsigned long flags;
  676. /*
  677. * Before testing whether we can acquire @lock, we set the
  678. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  679. * other tasks which try to modify @lock into the slow path
  680. * and they serialize on @lock->wait_lock.
  681. *
  682. * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
  683. * as explained at the top of this file if and only if:
  684. *
  685. * - There is a lock owner. The caller must fixup the
  686. * transient state if it does a trylock or leaves the lock
  687. * function due to a signal or timeout.
  688. *
  689. * - @task acquires the lock and there are no other
  690. * waiters. This is undone in rt_mutex_set_owner(@task) at
  691. * the end of this function.
  692. */
  693. mark_rt_mutex_waiters(lock);
  694. /*
  695. * If @lock has an owner, give up.
  696. */
  697. if (rt_mutex_owner(lock))
  698. return 0;
  699. /*
  700. * If @waiter != NULL, @task has already enqueued the waiter
  701. * into @lock waiter list. If @waiter == NULL then this is a
  702. * trylock attempt.
  703. */
  704. if (waiter) {
  705. /*
  706. * If waiter is not the highest priority waiter of
  707. * @lock, give up.
  708. */
  709. if (waiter != rt_mutex_top_waiter(lock))
  710. return 0;
  711. /*
  712. * We can acquire the lock. Remove the waiter from the
  713. * lock waiters list.
  714. */
  715. rt_mutex_dequeue(lock, waiter);
  716. } else {
  717. /*
  718. * If the lock has waiters already we check whether @task is
  719. * eligible to take over the lock.
  720. *
  721. * If there are no other waiters, @task can acquire
  722. * the lock. @task->pi_blocked_on is NULL, so it does
  723. * not need to be dequeued.
  724. */
  725. if (rt_mutex_has_waiters(lock)) {
  726. /*
  727. * If @task->prio is greater than or equal to
  728. * the top waiter priority (kernel view),
  729. * @task lost.
  730. */
  731. if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  732. return 0;
  733. /*
  734. * The current top waiter stays enqueued. We
  735. * don't have to change anything in the lock
  736. * waiters order.
  737. */
  738. } else {
  739. /*
  740. * No waiters. Take the lock without the
  741. * pi_lock dance.@task->pi_blocked_on is NULL
  742. * and we have no waiters to enqueue in @task
  743. * pi waiters list.
  744. */
  745. goto takeit;
  746. }
  747. }
  748. /*
  749. * Clear @task->pi_blocked_on. Requires protection by
  750. * @task->pi_lock. Redundant operation for the @waiter == NULL
  751. * case, but conditionals are more expensive than a redundant
  752. * store.
  753. */
  754. raw_spin_lock_irqsave(&task->pi_lock, flags);
  755. task->pi_blocked_on = NULL;
  756. /*
  757. * Finish the lock acquisition. @task is the new owner. If
  758. * other waiters exist we have to insert the highest priority
  759. * waiter into @task->pi_waiters list.
  760. */
  761. if (rt_mutex_has_waiters(lock))
  762. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  763. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  764. takeit:
  765. /* We got the lock. */
  766. debug_rt_mutex_lock(lock);
  767. /*
  768. * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
  769. * are still waiters or clears it.
  770. */
  771. rt_mutex_set_owner(lock, task);
  772. rt_mutex_deadlock_account_lock(lock, task);
  773. return 1;
  774. }
  775. /*
  776. * Task blocks on lock.
  777. *
  778. * Prepare waiter and propagate pi chain
  779. *
  780. * This must be called with lock->wait_lock held.
  781. */
  782. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  783. struct rt_mutex_waiter *waiter,
  784. struct task_struct *task,
  785. enum rtmutex_chainwalk chwalk)
  786. {
  787. struct task_struct *owner = rt_mutex_owner(lock);
  788. struct rt_mutex_waiter *top_waiter = waiter;
  789. struct rt_mutex *next_lock;
  790. int chain_walk = 0, res;
  791. unsigned long flags;
  792. /*
  793. * Early deadlock detection. We really don't want the task to
  794. * enqueue on itself just to untangle the mess later. It's not
  795. * only an optimization. We drop the locks, so another waiter
  796. * can come in before the chain walk detects the deadlock. So
  797. * the other will detect the deadlock and return -EDEADLOCK,
  798. * which is wrong, as the other waiter is not in a deadlock
  799. * situation.
  800. */
  801. if (owner == task)
  802. return -EDEADLK;
  803. raw_spin_lock_irqsave(&task->pi_lock, flags);
  804. __rt_mutex_adjust_prio(task);
  805. waiter->task = task;
  806. waiter->lock = lock;
  807. waiter->prio = task->prio;
  808. /* Get the top priority waiter on the lock */
  809. if (rt_mutex_has_waiters(lock))
  810. top_waiter = rt_mutex_top_waiter(lock);
  811. rt_mutex_enqueue(lock, waiter);
  812. task->pi_blocked_on = waiter;
  813. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  814. if (!owner)
  815. return 0;
  816. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  817. if (waiter == rt_mutex_top_waiter(lock)) {
  818. rt_mutex_dequeue_pi(owner, top_waiter);
  819. rt_mutex_enqueue_pi(owner, waiter);
  820. __rt_mutex_adjust_prio(owner);
  821. if (owner->pi_blocked_on)
  822. chain_walk = 1;
  823. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  824. chain_walk = 1;
  825. }
  826. /* Store the lock on which owner is blocked or NULL */
  827. next_lock = task_blocked_on_lock(owner);
  828. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  829. /*
  830. * Even if full deadlock detection is on, if the owner is not
  831. * blocked itself, we can avoid finding this out in the chain
  832. * walk.
  833. */
  834. if (!chain_walk || !next_lock)
  835. return 0;
  836. /*
  837. * The owner can't disappear while holding a lock,
  838. * so the owner struct is protected by wait_lock.
  839. * Gets dropped in rt_mutex_adjust_prio_chain()!
  840. */
  841. get_task_struct(owner);
  842. raw_spin_unlock(&lock->wait_lock);
  843. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  844. next_lock, waiter, task);
  845. raw_spin_lock(&lock->wait_lock);
  846. return res;
  847. }
  848. /*
  849. * Wake up the next waiter on the lock.
  850. *
  851. * Remove the top waiter from the current tasks pi waiter list and
  852. * wake it up.
  853. *
  854. * Called with lock->wait_lock held.
  855. */
  856. static void wakeup_next_waiter(struct rt_mutex *lock)
  857. {
  858. struct rt_mutex_waiter *waiter;
  859. unsigned long flags;
  860. raw_spin_lock_irqsave(&current->pi_lock, flags);
  861. waiter = rt_mutex_top_waiter(lock);
  862. /*
  863. * Remove it from current->pi_waiters. We do not adjust a
  864. * possible priority boost right now. We execute wakeup in the
  865. * boosted mode and go back to normal after releasing
  866. * lock->wait_lock.
  867. */
  868. rt_mutex_dequeue_pi(current, waiter);
  869. /*
  870. * As we are waking up the top waiter, and the waiter stays
  871. * queued on the lock until it gets the lock, this lock
  872. * obviously has waiters. Just set the bit here and this has
  873. * the added benefit of forcing all new tasks into the
  874. * slow path making sure no task of lower priority than
  875. * the top waiter can steal this lock.
  876. */
  877. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  878. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  879. /*
  880. * It's safe to dereference waiter as it cannot go away as
  881. * long as we hold lock->wait_lock. The waiter task needs to
  882. * acquire it in order to dequeue the waiter.
  883. */
  884. wake_up_process(waiter->task);
  885. }
  886. /*
  887. * Remove a waiter from a lock and give up
  888. *
  889. * Must be called with lock->wait_lock held and
  890. * have just failed to try_to_take_rt_mutex().
  891. */
  892. static void remove_waiter(struct rt_mutex *lock,
  893. struct rt_mutex_waiter *waiter)
  894. {
  895. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  896. struct task_struct *owner = rt_mutex_owner(lock);
  897. struct rt_mutex *next_lock;
  898. unsigned long flags;
  899. raw_spin_lock_irqsave(&current->pi_lock, flags);
  900. rt_mutex_dequeue(lock, waiter);
  901. current->pi_blocked_on = NULL;
  902. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  903. /*
  904. * Only update priority if the waiter was the highest priority
  905. * waiter of the lock and there is an owner to update.
  906. */
  907. if (!owner || !is_top_waiter)
  908. return;
  909. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  910. rt_mutex_dequeue_pi(owner, waiter);
  911. if (rt_mutex_has_waiters(lock))
  912. rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
  913. __rt_mutex_adjust_prio(owner);
  914. /* Store the lock on which owner is blocked or NULL */
  915. next_lock = task_blocked_on_lock(owner);
  916. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  917. /*
  918. * Don't walk the chain, if the owner task is not blocked
  919. * itself.
  920. */
  921. if (!next_lock)
  922. return;
  923. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  924. get_task_struct(owner);
  925. raw_spin_unlock(&lock->wait_lock);
  926. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  927. next_lock, NULL, current);
  928. raw_spin_lock(&lock->wait_lock);
  929. }
  930. /*
  931. * Recheck the pi chain, in case we got a priority setting
  932. *
  933. * Called from sched_setscheduler
  934. */
  935. void rt_mutex_adjust_pi(struct task_struct *task)
  936. {
  937. struct rt_mutex_waiter *waiter;
  938. struct rt_mutex *next_lock;
  939. unsigned long flags;
  940. raw_spin_lock_irqsave(&task->pi_lock, flags);
  941. waiter = task->pi_blocked_on;
  942. if (!waiter || (waiter->prio == task->prio &&
  943. !dl_prio(task->prio))) {
  944. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  945. return;
  946. }
  947. next_lock = waiter->lock;
  948. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  949. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  950. get_task_struct(task);
  951. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  952. next_lock, NULL, task);
  953. }
  954. /**
  955. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  956. * @lock: the rt_mutex to take
  957. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  958. * or TASK_UNINTERRUPTIBLE)
  959. * @timeout: the pre-initialized and started timer, or NULL for none
  960. * @waiter: the pre-initialized rt_mutex_waiter
  961. *
  962. * lock->wait_lock must be held by the caller.
  963. */
  964. static int __sched
  965. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  966. struct hrtimer_sleeper *timeout,
  967. struct rt_mutex_waiter *waiter)
  968. {
  969. int ret = 0;
  970. for (;;) {
  971. /* Try to acquire the lock: */
  972. if (try_to_take_rt_mutex(lock, current, waiter))
  973. break;
  974. /*
  975. * TASK_INTERRUPTIBLE checks for signals and
  976. * timeout. Ignored otherwise.
  977. */
  978. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  979. /* Signal pending? */
  980. if (signal_pending(current))
  981. ret = -EINTR;
  982. if (timeout && !timeout->task)
  983. ret = -ETIMEDOUT;
  984. if (ret)
  985. break;
  986. }
  987. raw_spin_unlock(&lock->wait_lock);
  988. debug_rt_mutex_print_deadlock(waiter);
  989. schedule_rt_mutex(lock);
  990. raw_spin_lock(&lock->wait_lock);
  991. set_current_state(state);
  992. }
  993. return ret;
  994. }
  995. static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  996. struct rt_mutex_waiter *w)
  997. {
  998. /*
  999. * If the result is not -EDEADLOCK or the caller requested
  1000. * deadlock detection, nothing to do here.
  1001. */
  1002. if (res != -EDEADLOCK || detect_deadlock)
  1003. return;
  1004. /*
  1005. * Yell lowdly and stop the task right here.
  1006. */
  1007. rt_mutex_print_deadlock(w);
  1008. while (1) {
  1009. set_current_state(TASK_INTERRUPTIBLE);
  1010. schedule();
  1011. }
  1012. }
  1013. /*
  1014. * Slow path lock function:
  1015. */
  1016. static int __sched
  1017. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  1018. struct hrtimer_sleeper *timeout,
  1019. enum rtmutex_chainwalk chwalk)
  1020. {
  1021. struct rt_mutex_waiter waiter;
  1022. int ret = 0;
  1023. debug_rt_mutex_init_waiter(&waiter);
  1024. RB_CLEAR_NODE(&waiter.pi_tree_entry);
  1025. RB_CLEAR_NODE(&waiter.tree_entry);
  1026. raw_spin_lock(&lock->wait_lock);
  1027. /* Try to acquire the lock again: */
  1028. if (try_to_take_rt_mutex(lock, current, NULL)) {
  1029. raw_spin_unlock(&lock->wait_lock);
  1030. return 0;
  1031. }
  1032. set_current_state(state);
  1033. /* Setup the timer, when timeout != NULL */
  1034. if (unlikely(timeout)) {
  1035. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1036. if (!hrtimer_active(&timeout->timer))
  1037. timeout->task = NULL;
  1038. }
  1039. ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
  1040. if (likely(!ret))
  1041. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  1042. set_current_state(TASK_RUNNING);
  1043. if (unlikely(ret)) {
  1044. remove_waiter(lock, &waiter);
  1045. rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  1046. }
  1047. /*
  1048. * try_to_take_rt_mutex() sets the waiter bit
  1049. * unconditionally. We might have to fix that up.
  1050. */
  1051. fixup_rt_mutex_waiters(lock);
  1052. raw_spin_unlock(&lock->wait_lock);
  1053. /* Remove pending timer: */
  1054. if (unlikely(timeout))
  1055. hrtimer_cancel(&timeout->timer);
  1056. debug_rt_mutex_free_waiter(&waiter);
  1057. return ret;
  1058. }
  1059. /*
  1060. * Slow path try-lock function:
  1061. */
  1062. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  1063. {
  1064. int ret;
  1065. /*
  1066. * If the lock already has an owner we fail to get the lock.
  1067. * This can be done without taking the @lock->wait_lock as
  1068. * it is only being read, and this is a trylock anyway.
  1069. */
  1070. if (rt_mutex_owner(lock))
  1071. return 0;
  1072. /*
  1073. * The mutex has currently no owner. Lock the wait lock and
  1074. * try to acquire the lock.
  1075. */
  1076. raw_spin_lock(&lock->wait_lock);
  1077. ret = try_to_take_rt_mutex(lock, current, NULL);
  1078. /*
  1079. * try_to_take_rt_mutex() sets the lock waiters bit
  1080. * unconditionally. Clean this up.
  1081. */
  1082. fixup_rt_mutex_waiters(lock);
  1083. raw_spin_unlock(&lock->wait_lock);
  1084. return ret;
  1085. }
  1086. /*
  1087. * Slow path to release a rt-mutex:
  1088. */
  1089. static void __sched
  1090. rt_mutex_slowunlock(struct rt_mutex *lock)
  1091. {
  1092. raw_spin_lock(&lock->wait_lock);
  1093. debug_rt_mutex_unlock(lock);
  1094. rt_mutex_deadlock_account_unlock(current);
  1095. /*
  1096. * We must be careful here if the fast path is enabled. If we
  1097. * have no waiters queued we cannot set owner to NULL here
  1098. * because of:
  1099. *
  1100. * foo->lock->owner = NULL;
  1101. * rtmutex_lock(foo->lock); <- fast path
  1102. * free = atomic_dec_and_test(foo->refcnt);
  1103. * rtmutex_unlock(foo->lock); <- fast path
  1104. * if (free)
  1105. * kfree(foo);
  1106. * raw_spin_unlock(foo->lock->wait_lock);
  1107. *
  1108. * So for the fastpath enabled kernel:
  1109. *
  1110. * Nothing can set the waiters bit as long as we hold
  1111. * lock->wait_lock. So we do the following sequence:
  1112. *
  1113. * owner = rt_mutex_owner(lock);
  1114. * clear_rt_mutex_waiters(lock);
  1115. * raw_spin_unlock(&lock->wait_lock);
  1116. * if (cmpxchg(&lock->owner, owner, 0) == owner)
  1117. * return;
  1118. * goto retry;
  1119. *
  1120. * The fastpath disabled variant is simple as all access to
  1121. * lock->owner is serialized by lock->wait_lock:
  1122. *
  1123. * lock->owner = NULL;
  1124. * raw_spin_unlock(&lock->wait_lock);
  1125. */
  1126. while (!rt_mutex_has_waiters(lock)) {
  1127. /* Drops lock->wait_lock ! */
  1128. if (unlock_rt_mutex_safe(lock) == true)
  1129. return;
  1130. /* Relock the rtmutex and try again */
  1131. raw_spin_lock(&lock->wait_lock);
  1132. }
  1133. /*
  1134. * The wakeup next waiter path does not suffer from the above
  1135. * race. See the comments there.
  1136. */
  1137. wakeup_next_waiter(lock);
  1138. raw_spin_unlock(&lock->wait_lock);
  1139. /* Undo pi boosting if necessary: */
  1140. rt_mutex_adjust_prio(current);
  1141. }
  1142. /*
  1143. * debug aware fast / slowpath lock,trylock,unlock
  1144. *
  1145. * The atomic acquire/release ops are compiled away, when either the
  1146. * architecture does not support cmpxchg or when debugging is enabled.
  1147. */
  1148. static inline int
  1149. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  1150. int (*slowfn)(struct rt_mutex *lock, int state,
  1151. struct hrtimer_sleeper *timeout,
  1152. enum rtmutex_chainwalk chwalk))
  1153. {
  1154. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  1155. rt_mutex_deadlock_account_lock(lock, current);
  1156. return 0;
  1157. } else
  1158. return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  1159. }
  1160. static inline int
  1161. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  1162. struct hrtimer_sleeper *timeout,
  1163. enum rtmutex_chainwalk chwalk,
  1164. int (*slowfn)(struct rt_mutex *lock, int state,
  1165. struct hrtimer_sleeper *timeout,
  1166. enum rtmutex_chainwalk chwalk))
  1167. {
  1168. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  1169. likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  1170. rt_mutex_deadlock_account_lock(lock, current);
  1171. return 0;
  1172. } else
  1173. return slowfn(lock, state, timeout, chwalk);
  1174. }
  1175. static inline int
  1176. rt_mutex_fasttrylock(struct rt_mutex *lock,
  1177. int (*slowfn)(struct rt_mutex *lock))
  1178. {
  1179. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  1180. rt_mutex_deadlock_account_lock(lock, current);
  1181. return 1;
  1182. }
  1183. return slowfn(lock);
  1184. }
  1185. static inline void
  1186. rt_mutex_fastunlock(struct rt_mutex *lock,
  1187. void (*slowfn)(struct rt_mutex *lock))
  1188. {
  1189. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  1190. rt_mutex_deadlock_account_unlock(current);
  1191. else
  1192. slowfn(lock);
  1193. }
  1194. /**
  1195. * rt_mutex_lock - lock a rt_mutex
  1196. *
  1197. * @lock: the rt_mutex to be locked
  1198. */
  1199. void __sched rt_mutex_lock(struct rt_mutex *lock)
  1200. {
  1201. might_sleep();
  1202. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  1203. }
  1204. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  1205. /**
  1206. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  1207. *
  1208. * @lock: the rt_mutex to be locked
  1209. *
  1210. * Returns:
  1211. * 0 on success
  1212. * -EINTR when interrupted by a signal
  1213. */
  1214. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  1215. {
  1216. might_sleep();
  1217. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  1218. }
  1219. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  1220. /*
  1221. * Futex variant with full deadlock detection.
  1222. */
  1223. int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
  1224. struct hrtimer_sleeper *timeout)
  1225. {
  1226. might_sleep();
  1227. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1228. RT_MUTEX_FULL_CHAINWALK,
  1229. rt_mutex_slowlock);
  1230. }
  1231. /**
  1232. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  1233. * the timeout structure is provided
  1234. * by the caller
  1235. *
  1236. * @lock: the rt_mutex to be locked
  1237. * @timeout: timeout structure or NULL (no timeout)
  1238. *
  1239. * Returns:
  1240. * 0 on success
  1241. * -EINTR when interrupted by a signal
  1242. * -ETIMEDOUT when the timeout expired
  1243. */
  1244. int
  1245. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
  1246. {
  1247. might_sleep();
  1248. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1249. RT_MUTEX_MIN_CHAINWALK,
  1250. rt_mutex_slowlock);
  1251. }
  1252. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  1253. /**
  1254. * rt_mutex_trylock - try to lock a rt_mutex
  1255. *
  1256. * @lock: the rt_mutex to be locked
  1257. *
  1258. * Returns 1 on success and 0 on contention
  1259. */
  1260. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  1261. {
  1262. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  1263. }
  1264. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  1265. /**
  1266. * rt_mutex_unlock - unlock a rt_mutex
  1267. *
  1268. * @lock: the rt_mutex to be unlocked
  1269. */
  1270. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  1271. {
  1272. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  1273. }
  1274. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  1275. /**
  1276. * rt_mutex_destroy - mark a mutex unusable
  1277. * @lock: the mutex to be destroyed
  1278. *
  1279. * This function marks the mutex uninitialized, and any subsequent
  1280. * use of the mutex is forbidden. The mutex must not be locked when
  1281. * this function is called.
  1282. */
  1283. void rt_mutex_destroy(struct rt_mutex *lock)
  1284. {
  1285. WARN_ON(rt_mutex_is_locked(lock));
  1286. #ifdef CONFIG_DEBUG_RT_MUTEXES
  1287. lock->magic = NULL;
  1288. #endif
  1289. }
  1290. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  1291. /**
  1292. * __rt_mutex_init - initialize the rt lock
  1293. *
  1294. * @lock: the rt lock to be initialized
  1295. *
  1296. * Initialize the rt lock to unlocked state.
  1297. *
  1298. * Initializing of a locked rt lock is not allowed
  1299. */
  1300. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  1301. {
  1302. lock->owner = NULL;
  1303. raw_spin_lock_init(&lock->wait_lock);
  1304. lock->waiters = RB_ROOT;
  1305. lock->waiters_leftmost = NULL;
  1306. debug_rt_mutex_init(lock, name);
  1307. }
  1308. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  1309. /**
  1310. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  1311. * proxy owner
  1312. *
  1313. * @lock: the rt_mutex to be locked
  1314. * @proxy_owner:the task to set as owner
  1315. *
  1316. * No locking. Caller has to do serializing itself
  1317. * Special API call for PI-futex support
  1318. */
  1319. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  1320. struct task_struct *proxy_owner)
  1321. {
  1322. __rt_mutex_init(lock, NULL);
  1323. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  1324. rt_mutex_set_owner(lock, proxy_owner);
  1325. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  1326. }
  1327. /**
  1328. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  1329. *
  1330. * @lock: the rt_mutex to be locked
  1331. *
  1332. * No locking. Caller has to do serializing itself
  1333. * Special API call for PI-futex support
  1334. */
  1335. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  1336. struct task_struct *proxy_owner)
  1337. {
  1338. debug_rt_mutex_proxy_unlock(lock);
  1339. rt_mutex_set_owner(lock, NULL);
  1340. rt_mutex_deadlock_account_unlock(proxy_owner);
  1341. }
  1342. /**
  1343. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  1344. * @lock: the rt_mutex to take
  1345. * @waiter: the pre-initialized rt_mutex_waiter
  1346. * @task: the task to prepare
  1347. *
  1348. * Returns:
  1349. * 0 - task blocked on lock
  1350. * 1 - acquired the lock for task, caller should wake it up
  1351. * <0 - error
  1352. *
  1353. * Special API call for FUTEX_REQUEUE_PI support.
  1354. */
  1355. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  1356. struct rt_mutex_waiter *waiter,
  1357. struct task_struct *task)
  1358. {
  1359. int ret;
  1360. raw_spin_lock(&lock->wait_lock);
  1361. if (try_to_take_rt_mutex(lock, task, NULL)) {
  1362. raw_spin_unlock(&lock->wait_lock);
  1363. return 1;
  1364. }
  1365. /* We enforce deadlock detection for futexes */
  1366. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  1367. RT_MUTEX_FULL_CHAINWALK);
  1368. if (ret && !rt_mutex_owner(lock)) {
  1369. /*
  1370. * Reset the return value. We might have
  1371. * returned with -EDEADLK and the owner
  1372. * released the lock while we were walking the
  1373. * pi chain. Let the waiter sort it out.
  1374. */
  1375. ret = 0;
  1376. }
  1377. if (unlikely(ret))
  1378. remove_waiter(lock, waiter);
  1379. raw_spin_unlock(&lock->wait_lock);
  1380. debug_rt_mutex_print_deadlock(waiter);
  1381. return ret;
  1382. }
  1383. /**
  1384. * rt_mutex_next_owner - return the next owner of the lock
  1385. *
  1386. * @lock: the rt lock query
  1387. *
  1388. * Returns the next owner of the lock or NULL
  1389. *
  1390. * Caller has to serialize against other accessors to the lock
  1391. * itself.
  1392. *
  1393. * Special API call for PI-futex support
  1394. */
  1395. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  1396. {
  1397. if (!rt_mutex_has_waiters(lock))
  1398. return NULL;
  1399. return rt_mutex_top_waiter(lock)->task;
  1400. }
  1401. /**
  1402. * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  1403. * @lock: the rt_mutex we were woken on
  1404. * @to: the timeout, null if none. hrtimer should already have
  1405. * been started.
  1406. * @waiter: the pre-initialized rt_mutex_waiter
  1407. *
  1408. * Complete the lock acquisition started our behalf by another thread.
  1409. *
  1410. * Returns:
  1411. * 0 - success
  1412. * <0 - error, one of -EINTR, -ETIMEDOUT
  1413. *
  1414. * Special API call for PI-futex requeue support
  1415. */
  1416. int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  1417. struct hrtimer_sleeper *to,
  1418. struct rt_mutex_waiter *waiter)
  1419. {
  1420. int ret;
  1421. raw_spin_lock(&lock->wait_lock);
  1422. set_current_state(TASK_INTERRUPTIBLE);
  1423. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  1424. set_current_state(TASK_RUNNING);
  1425. if (unlikely(ret))
  1426. remove_waiter(lock, waiter);
  1427. /*
  1428. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1429. * have to fix that up.
  1430. */
  1431. fixup_rt_mutex_waiters(lock);
  1432. raw_spin_unlock(&lock->wait_lock);
  1433. return ret;
  1434. }