rtmutex.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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/signal.h>
  16. #include <linux/sched/rt.h>
  17. #include <linux/sched/deadline.h>
  18. #include <linux/sched/wake_q.h>
  19. #include <linux/sched/debug.h>
  20. #include <linux/timer.h>
  21. #include "rtmutex_common.h"
  22. /*
  23. * lock->owner state tracking:
  24. *
  25. * lock->owner holds the task_struct pointer of the owner. Bit 0
  26. * is used to keep track of the "lock has waiters" state.
  27. *
  28. * owner bit0
  29. * NULL 0 lock is free (fast acquire possible)
  30. * NULL 1 lock is free and has waiters and the top waiter
  31. * is going to take the lock*
  32. * taskpointer 0 lock is held (fast release possible)
  33. * taskpointer 1 lock is held and has waiters**
  34. *
  35. * The fast atomic compare exchange based acquire and release is only
  36. * possible when bit 0 of lock->owner is 0.
  37. *
  38. * (*) It also can be a transitional state when grabbing the lock
  39. * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
  40. * we need to set the bit0 before looking at the lock, and the owner may be
  41. * NULL in this small time, hence this can be a transitional state.
  42. *
  43. * (**) There is a small time when bit 0 is set but there are no
  44. * waiters. This can happen when grabbing the lock in the slow path.
  45. * To prevent a cmpxchg of the owner releasing the lock, we need to
  46. * set this bit before looking at the lock.
  47. */
  48. static void
  49. rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
  50. {
  51. unsigned long val = (unsigned long)owner;
  52. if (rt_mutex_has_waiters(lock))
  53. val |= RT_MUTEX_HAS_WAITERS;
  54. lock->owner = (struct task_struct *)val;
  55. }
  56. static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
  57. {
  58. lock->owner = (struct task_struct *)
  59. ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
  60. }
  61. static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  62. {
  63. unsigned long owner, *p = (unsigned long *) &lock->owner;
  64. if (rt_mutex_has_waiters(lock))
  65. return;
  66. /*
  67. * The rbtree has no waiters enqueued, now make sure that the
  68. * lock->owner still has the waiters bit set, otherwise the
  69. * following can happen:
  70. *
  71. * CPU 0 CPU 1 CPU2
  72. * l->owner=T1
  73. * rt_mutex_lock(l)
  74. * lock(l->lock)
  75. * l->owner = T1 | HAS_WAITERS;
  76. * enqueue(T2)
  77. * boost()
  78. * unlock(l->lock)
  79. * block()
  80. *
  81. * rt_mutex_lock(l)
  82. * lock(l->lock)
  83. * l->owner = T1 | HAS_WAITERS;
  84. * enqueue(T3)
  85. * boost()
  86. * unlock(l->lock)
  87. * block()
  88. * signal(->T2) signal(->T3)
  89. * lock(l->lock)
  90. * dequeue(T2)
  91. * deboost()
  92. * unlock(l->lock)
  93. * lock(l->lock)
  94. * dequeue(T3)
  95. * ==> wait list is empty
  96. * deboost()
  97. * unlock(l->lock)
  98. * lock(l->lock)
  99. * fixup_rt_mutex_waiters()
  100. * if (wait_list_empty(l) {
  101. * l->owner = owner
  102. * owner = l->owner & ~HAS_WAITERS;
  103. * ==> l->owner = T1
  104. * }
  105. * lock(l->lock)
  106. * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
  107. * if (wait_list_empty(l) {
  108. * owner = l->owner & ~HAS_WAITERS;
  109. * cmpxchg(l->owner, T1, NULL)
  110. * ===> Success (l->owner = NULL)
  111. *
  112. * l->owner = owner
  113. * ==> l->owner = T1
  114. * }
  115. *
  116. * With the check for the waiter bit in place T3 on CPU2 will not
  117. * overwrite. All tasks fiddling with the waiters bit are
  118. * serialized by l->lock, so nothing else can modify the waiters
  119. * bit. If the bit is set then nothing can change l->owner either
  120. * so the simple RMW is safe. The cmpxchg() will simply fail if it
  121. * happens in the middle of the RMW because the waiters bit is
  122. * still set.
  123. */
  124. owner = READ_ONCE(*p);
  125. if (owner & RT_MUTEX_HAS_WAITERS)
  126. WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
  127. }
  128. /*
  129. * We can speed up the acquire/release, if there's no debugging state to be
  130. * set up.
  131. */
  132. #ifndef CONFIG_DEBUG_RT_MUTEXES
  133. # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
  134. # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
  135. # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
  136. /*
  137. * Callers must hold the ->wait_lock -- which is the whole purpose as we force
  138. * all future threads that attempt to [Rmw] the lock to the slowpath. As such
  139. * relaxed semantics suffice.
  140. */
  141. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  142. {
  143. unsigned long owner, *p = (unsigned long *) &lock->owner;
  144. do {
  145. owner = *p;
  146. } while (cmpxchg_relaxed(p, owner,
  147. owner | RT_MUTEX_HAS_WAITERS) != owner);
  148. }
  149. /*
  150. * Safe fastpath aware unlock:
  151. * 1) Clear the waiters bit
  152. * 2) Drop lock->wait_lock
  153. * 3) Try to unlock the lock with cmpxchg
  154. */
  155. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  156. unsigned long flags)
  157. __releases(lock->wait_lock)
  158. {
  159. struct task_struct *owner = rt_mutex_owner(lock);
  160. clear_rt_mutex_waiters(lock);
  161. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  162. /*
  163. * If a new waiter comes in between the unlock and the cmpxchg
  164. * we have two situations:
  165. *
  166. * unlock(wait_lock);
  167. * lock(wait_lock);
  168. * cmpxchg(p, owner, 0) == owner
  169. * mark_rt_mutex_waiters(lock);
  170. * acquire(lock);
  171. * or:
  172. *
  173. * unlock(wait_lock);
  174. * lock(wait_lock);
  175. * mark_rt_mutex_waiters(lock);
  176. *
  177. * cmpxchg(p, owner, 0) != owner
  178. * enqueue_waiter();
  179. * unlock(wait_lock);
  180. * lock(wait_lock);
  181. * wake waiter();
  182. * unlock(wait_lock);
  183. * lock(wait_lock);
  184. * acquire(lock);
  185. */
  186. return rt_mutex_cmpxchg_release(lock, owner, NULL);
  187. }
  188. #else
  189. # define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
  190. # define rt_mutex_cmpxchg_acquire(l,c,n) (0)
  191. # define rt_mutex_cmpxchg_release(l,c,n) (0)
  192. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  193. {
  194. lock->owner = (struct task_struct *)
  195. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  196. }
  197. /*
  198. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  199. */
  200. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  201. unsigned long flags)
  202. __releases(lock->wait_lock)
  203. {
  204. lock->owner = NULL;
  205. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  206. return true;
  207. }
  208. #endif
  209. /*
  210. * Only use with rt_mutex_waiter_{less,equal}()
  211. */
  212. #define task_to_waiter(p) \
  213. &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline }
  214. static inline int
  215. rt_mutex_waiter_less(struct rt_mutex_waiter *left,
  216. struct rt_mutex_waiter *right)
  217. {
  218. if (left->prio < right->prio)
  219. return 1;
  220. /*
  221. * If both waiters have dl_prio(), we check the deadlines of the
  222. * associated tasks.
  223. * If left waiter has a dl_prio(), and we didn't return 1 above,
  224. * then right waiter has a dl_prio() too.
  225. */
  226. if (dl_prio(left->prio))
  227. return dl_time_before(left->deadline, right->deadline);
  228. return 0;
  229. }
  230. static inline int
  231. rt_mutex_waiter_equal(struct rt_mutex_waiter *left,
  232. struct rt_mutex_waiter *right)
  233. {
  234. if (left->prio != right->prio)
  235. return 0;
  236. /*
  237. * If both waiters have dl_prio(), we check the deadlines of the
  238. * associated tasks.
  239. * If left waiter has a dl_prio(), and we didn't return 0 above,
  240. * then right waiter has a dl_prio() too.
  241. */
  242. if (dl_prio(left->prio))
  243. return left->deadline == right->deadline;
  244. return 1;
  245. }
  246. static void
  247. rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  248. {
  249. struct rb_node **link = &lock->waiters.rb_root.rb_node;
  250. struct rb_node *parent = NULL;
  251. struct rt_mutex_waiter *entry;
  252. bool leftmost = true;
  253. while (*link) {
  254. parent = *link;
  255. entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
  256. if (rt_mutex_waiter_less(waiter, entry)) {
  257. link = &parent->rb_left;
  258. } else {
  259. link = &parent->rb_right;
  260. leftmost = false;
  261. }
  262. }
  263. rb_link_node(&waiter->tree_entry, parent, link);
  264. rb_insert_color_cached(&waiter->tree_entry, &lock->waiters, leftmost);
  265. }
  266. static void
  267. rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  268. {
  269. if (RB_EMPTY_NODE(&waiter->tree_entry))
  270. return;
  271. rb_erase_cached(&waiter->tree_entry, &lock->waiters);
  272. RB_CLEAR_NODE(&waiter->tree_entry);
  273. }
  274. static void
  275. rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  276. {
  277. struct rb_node **link = &task->pi_waiters.rb_root.rb_node;
  278. struct rb_node *parent = NULL;
  279. struct rt_mutex_waiter *entry;
  280. bool leftmost = true;
  281. while (*link) {
  282. parent = *link;
  283. entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
  284. if (rt_mutex_waiter_less(waiter, entry)) {
  285. link = &parent->rb_left;
  286. } else {
  287. link = &parent->rb_right;
  288. leftmost = false;
  289. }
  290. }
  291. rb_link_node(&waiter->pi_tree_entry, parent, link);
  292. rb_insert_color_cached(&waiter->pi_tree_entry, &task->pi_waiters, leftmost);
  293. }
  294. static void
  295. rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  296. {
  297. if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
  298. return;
  299. rb_erase_cached(&waiter->pi_tree_entry, &task->pi_waiters);
  300. RB_CLEAR_NODE(&waiter->pi_tree_entry);
  301. }
  302. static void rt_mutex_adjust_prio(struct task_struct *p)
  303. {
  304. struct task_struct *pi_task = NULL;
  305. lockdep_assert_held(&p->pi_lock);
  306. if (task_has_pi_waiters(p))
  307. pi_task = task_top_pi_waiter(p)->task;
  308. rt_mutex_setprio(p, pi_task);
  309. }
  310. /*
  311. * Deadlock detection is conditional:
  312. *
  313. * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
  314. * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
  315. *
  316. * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
  317. * conducted independent of the detect argument.
  318. *
  319. * If the waiter argument is NULL this indicates the deboost path and
  320. * deadlock detection is disabled independent of the detect argument
  321. * and the config settings.
  322. */
  323. static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  324. enum rtmutex_chainwalk chwalk)
  325. {
  326. /*
  327. * This is just a wrapper function for the following call,
  328. * because debug_rt_mutex_detect_deadlock() smells like a magic
  329. * debug feature and I wanted to keep the cond function in the
  330. * main source file along with the comments instead of having
  331. * two of the same in the headers.
  332. */
  333. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  334. }
  335. /*
  336. * Max number of times we'll walk the boosting chain:
  337. */
  338. int max_lock_depth = 1024;
  339. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  340. {
  341. return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  342. }
  343. /*
  344. * Adjust the priority chain. Also used for deadlock detection.
  345. * Decreases task's usage by one - may thus free the task.
  346. *
  347. * @task: the task owning the mutex (owner) for which a chain walk is
  348. * probably needed
  349. * @chwalk: do we have to carry out deadlock detection?
  350. * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
  351. * things for a task that has just got its priority adjusted, and
  352. * is waiting on a mutex)
  353. * @next_lock: the mutex on which the owner of @orig_lock was blocked before
  354. * we dropped its pi_lock. Is never dereferenced, only used for
  355. * comparison to detect lock chain changes.
  356. * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
  357. * its priority to the mutex owner (can be NULL in the case
  358. * depicted above or if the top waiter is gone away and we are
  359. * actually deboosting the owner)
  360. * @top_task: the current top waiter
  361. *
  362. * Returns 0 or -EDEADLK.
  363. *
  364. * Chain walk basics and protection scope
  365. *
  366. * [R] refcount on task
  367. * [P] task->pi_lock held
  368. * [L] rtmutex->wait_lock held
  369. *
  370. * Step Description Protected by
  371. * function arguments:
  372. * @task [R]
  373. * @orig_lock if != NULL @top_task is blocked on it
  374. * @next_lock Unprotected. Cannot be
  375. * dereferenced. Only used for
  376. * comparison.
  377. * @orig_waiter if != NULL @top_task is blocked on it
  378. * @top_task current, or in case of proxy
  379. * locking protected by calling
  380. * code
  381. * again:
  382. * loop_sanity_check();
  383. * retry:
  384. * [1] lock(task->pi_lock); [R] acquire [P]
  385. * [2] waiter = task->pi_blocked_on; [P]
  386. * [3] check_exit_conditions_1(); [P]
  387. * [4] lock = waiter->lock; [P]
  388. * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
  389. * unlock(task->pi_lock); release [P]
  390. * goto retry;
  391. * }
  392. * [6] check_exit_conditions_2(); [P] + [L]
  393. * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
  394. * [8] unlock(task->pi_lock); release [P]
  395. * put_task_struct(task); release [R]
  396. * [9] check_exit_conditions_3(); [L]
  397. * [10] task = owner(lock); [L]
  398. * get_task_struct(task); [L] acquire [R]
  399. * lock(task->pi_lock); [L] acquire [P]
  400. * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
  401. * [12] check_exit_conditions_4(); [P] + [L]
  402. * [13] unlock(task->pi_lock); release [P]
  403. * unlock(lock->wait_lock); release [L]
  404. * goto again;
  405. */
  406. static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  407. enum rtmutex_chainwalk chwalk,
  408. struct rt_mutex *orig_lock,
  409. struct rt_mutex *next_lock,
  410. struct rt_mutex_waiter *orig_waiter,
  411. struct task_struct *top_task)
  412. {
  413. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  414. struct rt_mutex_waiter *prerequeue_top_waiter;
  415. int ret = 0, depth = 0;
  416. struct rt_mutex *lock;
  417. bool detect_deadlock;
  418. bool requeue = true;
  419. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  420. /*
  421. * The (de)boosting is a step by step approach with a lot of
  422. * pitfalls. We want this to be preemptible and we want hold a
  423. * maximum of two locks per step. So we have to check
  424. * carefully whether things change under us.
  425. */
  426. again:
  427. /*
  428. * We limit the lock chain length for each invocation.
  429. */
  430. if (++depth > max_lock_depth) {
  431. static int prev_max;
  432. /*
  433. * Print this only once. If the admin changes the limit,
  434. * print a new message when reaching the limit again.
  435. */
  436. if (prev_max != max_lock_depth) {
  437. prev_max = max_lock_depth;
  438. printk(KERN_WARNING "Maximum lock depth %d reached "
  439. "task: %s (%d)\n", max_lock_depth,
  440. top_task->comm, task_pid_nr(top_task));
  441. }
  442. put_task_struct(task);
  443. return -EDEADLK;
  444. }
  445. /*
  446. * We are fully preemptible here and only hold the refcount on
  447. * @task. So everything can have changed under us since the
  448. * caller or our own code below (goto retry/again) dropped all
  449. * locks.
  450. */
  451. retry:
  452. /*
  453. * [1] Task cannot go away as we did a get_task() before !
  454. */
  455. raw_spin_lock_irq(&task->pi_lock);
  456. /*
  457. * [2] Get the waiter on which @task is blocked on.
  458. */
  459. waiter = task->pi_blocked_on;
  460. /*
  461. * [3] check_exit_conditions_1() protected by task->pi_lock.
  462. */
  463. /*
  464. * Check whether the end of the boosting chain has been
  465. * reached or the state of the chain has changed while we
  466. * dropped the locks.
  467. */
  468. if (!waiter)
  469. goto out_unlock_pi;
  470. /*
  471. * Check the orig_waiter state. After we dropped the locks,
  472. * the previous owner of the lock might have released the lock.
  473. */
  474. if (orig_waiter && !rt_mutex_owner(orig_lock))
  475. goto out_unlock_pi;
  476. /*
  477. * We dropped all locks after taking a refcount on @task, so
  478. * the task might have moved on in the lock chain or even left
  479. * the chain completely and blocks now on an unrelated lock or
  480. * on @orig_lock.
  481. *
  482. * We stored the lock on which @task was blocked in @next_lock,
  483. * so we can detect the chain change.
  484. */
  485. if (next_lock != waiter->lock)
  486. goto out_unlock_pi;
  487. /*
  488. * Drop out, when the task has no waiters. Note,
  489. * top_waiter can be NULL, when we are in the deboosting
  490. * mode!
  491. */
  492. if (top_waiter) {
  493. if (!task_has_pi_waiters(task))
  494. goto out_unlock_pi;
  495. /*
  496. * If deadlock detection is off, we stop here if we
  497. * are not the top pi waiter of the task. If deadlock
  498. * detection is enabled we continue, but stop the
  499. * requeueing in the chain walk.
  500. */
  501. if (top_waiter != task_top_pi_waiter(task)) {
  502. if (!detect_deadlock)
  503. goto out_unlock_pi;
  504. else
  505. requeue = false;
  506. }
  507. }
  508. /*
  509. * If the waiter priority is the same as the task priority
  510. * then there is no further priority adjustment necessary. If
  511. * deadlock detection is off, we stop the chain walk. If its
  512. * enabled we continue, but stop the requeueing in the chain
  513. * walk.
  514. */
  515. if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
  516. if (!detect_deadlock)
  517. goto out_unlock_pi;
  518. else
  519. requeue = false;
  520. }
  521. /*
  522. * [4] Get the next lock
  523. */
  524. lock = waiter->lock;
  525. /*
  526. * [5] We need to trylock here as we are holding task->pi_lock,
  527. * which is the reverse lock order versus the other rtmutex
  528. * operations.
  529. */
  530. if (!raw_spin_trylock(&lock->wait_lock)) {
  531. raw_spin_unlock_irq(&task->pi_lock);
  532. cpu_relax();
  533. goto retry;
  534. }
  535. /*
  536. * [6] check_exit_conditions_2() protected by task->pi_lock and
  537. * lock->wait_lock.
  538. *
  539. * Deadlock detection. If the lock is the same as the original
  540. * lock which caused us to walk the lock chain or if the
  541. * current lock is owned by the task which initiated the chain
  542. * walk, we detected a deadlock.
  543. */
  544. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  545. debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
  546. raw_spin_unlock(&lock->wait_lock);
  547. ret = -EDEADLK;
  548. goto out_unlock_pi;
  549. }
  550. /*
  551. * If we just follow the lock chain for deadlock detection, no
  552. * need to do all the requeue operations. To avoid a truckload
  553. * of conditionals around the various places below, just do the
  554. * minimum chain walk checks.
  555. */
  556. if (!requeue) {
  557. /*
  558. * No requeue[7] here. Just release @task [8]
  559. */
  560. raw_spin_unlock(&task->pi_lock);
  561. put_task_struct(task);
  562. /*
  563. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  564. * If there is no owner of the lock, end of chain.
  565. */
  566. if (!rt_mutex_owner(lock)) {
  567. raw_spin_unlock_irq(&lock->wait_lock);
  568. return 0;
  569. }
  570. /* [10] Grab the next task, i.e. owner of @lock */
  571. task = rt_mutex_owner(lock);
  572. get_task_struct(task);
  573. raw_spin_lock(&task->pi_lock);
  574. /*
  575. * No requeue [11] here. We just do deadlock detection.
  576. *
  577. * [12] Store whether owner is blocked
  578. * itself. Decision is made after dropping the locks
  579. */
  580. next_lock = task_blocked_on_lock(task);
  581. /*
  582. * Get the top waiter for the next iteration
  583. */
  584. top_waiter = rt_mutex_top_waiter(lock);
  585. /* [13] Drop locks */
  586. raw_spin_unlock(&task->pi_lock);
  587. raw_spin_unlock_irq(&lock->wait_lock);
  588. /* If owner is not blocked, end of chain. */
  589. if (!next_lock)
  590. goto out_put_task;
  591. goto again;
  592. }
  593. /*
  594. * Store the current top waiter before doing the requeue
  595. * operation on @lock. We need it for the boost/deboost
  596. * decision below.
  597. */
  598. prerequeue_top_waiter = rt_mutex_top_waiter(lock);
  599. /* [7] Requeue the waiter in the lock waiter tree. */
  600. rt_mutex_dequeue(lock, waiter);
  601. /*
  602. * Update the waiter prio fields now that we're dequeued.
  603. *
  604. * These values can have changed through either:
  605. *
  606. * sys_sched_set_scheduler() / sys_sched_setattr()
  607. *
  608. * or
  609. *
  610. * DL CBS enforcement advancing the effective deadline.
  611. *
  612. * Even though pi_waiters also uses these fields, and that tree is only
  613. * updated in [11], we can do this here, since we hold [L], which
  614. * serializes all pi_waiters access and rb_erase() does not care about
  615. * the values of the node being removed.
  616. */
  617. waiter->prio = task->prio;
  618. waiter->deadline = task->dl.deadline;
  619. rt_mutex_enqueue(lock, waiter);
  620. /* [8] Release the task */
  621. raw_spin_unlock(&task->pi_lock);
  622. put_task_struct(task);
  623. /*
  624. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  625. *
  626. * We must abort the chain walk if there is no lock owner even
  627. * in the dead lock detection case, as we have nothing to
  628. * follow here. This is the end of the chain we are walking.
  629. */
  630. if (!rt_mutex_owner(lock)) {
  631. /*
  632. * If the requeue [7] above changed the top waiter,
  633. * then we need to wake the new top waiter up to try
  634. * to get the lock.
  635. */
  636. if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  637. wake_up_process(rt_mutex_top_waiter(lock)->task);
  638. raw_spin_unlock_irq(&lock->wait_lock);
  639. return 0;
  640. }
  641. /* [10] Grab the next task, i.e. the owner of @lock */
  642. task = rt_mutex_owner(lock);
  643. get_task_struct(task);
  644. raw_spin_lock(&task->pi_lock);
  645. /* [11] requeue the pi waiters if necessary */
  646. if (waiter == rt_mutex_top_waiter(lock)) {
  647. /*
  648. * The waiter became the new top (highest priority)
  649. * waiter on the lock. Replace the previous top waiter
  650. * in the owner tasks pi waiters tree with this waiter
  651. * and adjust the priority of the owner.
  652. */
  653. rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
  654. rt_mutex_enqueue_pi(task, waiter);
  655. rt_mutex_adjust_prio(task);
  656. } else if (prerequeue_top_waiter == waiter) {
  657. /*
  658. * The waiter was the top waiter on the lock, but is
  659. * no longer the top prority waiter. Replace waiter in
  660. * the owner tasks pi waiters tree with the new top
  661. * (highest priority) waiter and adjust the priority
  662. * of the owner.
  663. * The new top waiter is stored in @waiter so that
  664. * @waiter == @top_waiter evaluates to true below and
  665. * we continue to deboost the rest of the chain.
  666. */
  667. rt_mutex_dequeue_pi(task, waiter);
  668. waiter = rt_mutex_top_waiter(lock);
  669. rt_mutex_enqueue_pi(task, waiter);
  670. rt_mutex_adjust_prio(task);
  671. } else {
  672. /*
  673. * Nothing changed. No need to do any priority
  674. * adjustment.
  675. */
  676. }
  677. /*
  678. * [12] check_exit_conditions_4() protected by task->pi_lock
  679. * and lock->wait_lock. The actual decisions are made after we
  680. * dropped the locks.
  681. *
  682. * Check whether the task which owns the current lock is pi
  683. * blocked itself. If yes we store a pointer to the lock for
  684. * the lock chain change detection above. After we dropped
  685. * task->pi_lock next_lock cannot be dereferenced anymore.
  686. */
  687. next_lock = task_blocked_on_lock(task);
  688. /*
  689. * Store the top waiter of @lock for the end of chain walk
  690. * decision below.
  691. */
  692. top_waiter = rt_mutex_top_waiter(lock);
  693. /* [13] Drop the locks */
  694. raw_spin_unlock(&task->pi_lock);
  695. raw_spin_unlock_irq(&lock->wait_lock);
  696. /*
  697. * Make the actual exit decisions [12], based on the stored
  698. * values.
  699. *
  700. * We reached the end of the lock chain. Stop right here. No
  701. * point to go back just to figure that out.
  702. */
  703. if (!next_lock)
  704. goto out_put_task;
  705. /*
  706. * If the current waiter is not the top waiter on the lock,
  707. * then we can stop the chain walk here if we are not in full
  708. * deadlock detection mode.
  709. */
  710. if (!detect_deadlock && waiter != top_waiter)
  711. goto out_put_task;
  712. goto again;
  713. out_unlock_pi:
  714. raw_spin_unlock_irq(&task->pi_lock);
  715. out_put_task:
  716. put_task_struct(task);
  717. return ret;
  718. }
  719. /*
  720. * Try to take an rt-mutex
  721. *
  722. * Must be called with lock->wait_lock held and interrupts disabled
  723. *
  724. * @lock: The lock to be acquired.
  725. * @task: The task which wants to acquire the lock
  726. * @waiter: The waiter that is queued to the lock's wait tree if the
  727. * callsite called task_blocked_on_lock(), otherwise NULL
  728. */
  729. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  730. struct rt_mutex_waiter *waiter)
  731. {
  732. lockdep_assert_held(&lock->wait_lock);
  733. /*
  734. * Before testing whether we can acquire @lock, we set the
  735. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  736. * other tasks which try to modify @lock into the slow path
  737. * and they serialize on @lock->wait_lock.
  738. *
  739. * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
  740. * as explained at the top of this file if and only if:
  741. *
  742. * - There is a lock owner. The caller must fixup the
  743. * transient state if it does a trylock or leaves the lock
  744. * function due to a signal or timeout.
  745. *
  746. * - @task acquires the lock and there are no other
  747. * waiters. This is undone in rt_mutex_set_owner(@task) at
  748. * the end of this function.
  749. */
  750. mark_rt_mutex_waiters(lock);
  751. /*
  752. * If @lock has an owner, give up.
  753. */
  754. if (rt_mutex_owner(lock))
  755. return 0;
  756. /*
  757. * If @waiter != NULL, @task has already enqueued the waiter
  758. * into @lock waiter tree. If @waiter == NULL then this is a
  759. * trylock attempt.
  760. */
  761. if (waiter) {
  762. /*
  763. * If waiter is not the highest priority waiter of
  764. * @lock, give up.
  765. */
  766. if (waiter != rt_mutex_top_waiter(lock))
  767. return 0;
  768. /*
  769. * We can acquire the lock. Remove the waiter from the
  770. * lock waiters tree.
  771. */
  772. rt_mutex_dequeue(lock, waiter);
  773. } else {
  774. /*
  775. * If the lock has waiters already we check whether @task is
  776. * eligible to take over the lock.
  777. *
  778. * If there are no other waiters, @task can acquire
  779. * the lock. @task->pi_blocked_on is NULL, so it does
  780. * not need to be dequeued.
  781. */
  782. if (rt_mutex_has_waiters(lock)) {
  783. /*
  784. * If @task->prio is greater than or equal to
  785. * the top waiter priority (kernel view),
  786. * @task lost.
  787. */
  788. if (!rt_mutex_waiter_less(task_to_waiter(task),
  789. rt_mutex_top_waiter(lock)))
  790. return 0;
  791. /*
  792. * The current top waiter stays enqueued. We
  793. * don't have to change anything in the lock
  794. * waiters order.
  795. */
  796. } else {
  797. /*
  798. * No waiters. Take the lock without the
  799. * pi_lock dance.@task->pi_blocked_on is NULL
  800. * and we have no waiters to enqueue in @task
  801. * pi waiters tree.
  802. */
  803. goto takeit;
  804. }
  805. }
  806. /*
  807. * Clear @task->pi_blocked_on. Requires protection by
  808. * @task->pi_lock. Redundant operation for the @waiter == NULL
  809. * case, but conditionals are more expensive than a redundant
  810. * store.
  811. */
  812. raw_spin_lock(&task->pi_lock);
  813. task->pi_blocked_on = NULL;
  814. /*
  815. * Finish the lock acquisition. @task is the new owner. If
  816. * other waiters exist we have to insert the highest priority
  817. * waiter into @task->pi_waiters tree.
  818. */
  819. if (rt_mutex_has_waiters(lock))
  820. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  821. raw_spin_unlock(&task->pi_lock);
  822. takeit:
  823. /* We got the lock. */
  824. debug_rt_mutex_lock(lock);
  825. /*
  826. * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
  827. * are still waiters or clears it.
  828. */
  829. rt_mutex_set_owner(lock, task);
  830. return 1;
  831. }
  832. /*
  833. * Task blocks on lock.
  834. *
  835. * Prepare waiter and propagate pi chain
  836. *
  837. * This must be called with lock->wait_lock held and interrupts disabled
  838. */
  839. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  840. struct rt_mutex_waiter *waiter,
  841. struct task_struct *task,
  842. enum rtmutex_chainwalk chwalk)
  843. {
  844. struct task_struct *owner = rt_mutex_owner(lock);
  845. struct rt_mutex_waiter *top_waiter = waiter;
  846. struct rt_mutex *next_lock;
  847. int chain_walk = 0, res;
  848. lockdep_assert_held(&lock->wait_lock);
  849. /*
  850. * Early deadlock detection. We really don't want the task to
  851. * enqueue on itself just to untangle the mess later. It's not
  852. * only an optimization. We drop the locks, so another waiter
  853. * can come in before the chain walk detects the deadlock. So
  854. * the other will detect the deadlock and return -EDEADLOCK,
  855. * which is wrong, as the other waiter is not in a deadlock
  856. * situation.
  857. */
  858. if (owner == task)
  859. return -EDEADLK;
  860. raw_spin_lock(&task->pi_lock);
  861. waiter->task = task;
  862. waiter->lock = lock;
  863. waiter->prio = task->prio;
  864. waiter->deadline = task->dl.deadline;
  865. /* Get the top priority waiter on the lock */
  866. if (rt_mutex_has_waiters(lock))
  867. top_waiter = rt_mutex_top_waiter(lock);
  868. rt_mutex_enqueue(lock, waiter);
  869. task->pi_blocked_on = waiter;
  870. raw_spin_unlock(&task->pi_lock);
  871. if (!owner)
  872. return 0;
  873. raw_spin_lock(&owner->pi_lock);
  874. if (waiter == rt_mutex_top_waiter(lock)) {
  875. rt_mutex_dequeue_pi(owner, top_waiter);
  876. rt_mutex_enqueue_pi(owner, waiter);
  877. rt_mutex_adjust_prio(owner);
  878. if (owner->pi_blocked_on)
  879. chain_walk = 1;
  880. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  881. chain_walk = 1;
  882. }
  883. /* Store the lock on which owner is blocked or NULL */
  884. next_lock = task_blocked_on_lock(owner);
  885. raw_spin_unlock(&owner->pi_lock);
  886. /*
  887. * Even if full deadlock detection is on, if the owner is not
  888. * blocked itself, we can avoid finding this out in the chain
  889. * walk.
  890. */
  891. if (!chain_walk || !next_lock)
  892. return 0;
  893. /*
  894. * The owner can't disappear while holding a lock,
  895. * so the owner struct is protected by wait_lock.
  896. * Gets dropped in rt_mutex_adjust_prio_chain()!
  897. */
  898. get_task_struct(owner);
  899. raw_spin_unlock_irq(&lock->wait_lock);
  900. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  901. next_lock, waiter, task);
  902. raw_spin_lock_irq(&lock->wait_lock);
  903. return res;
  904. }
  905. /*
  906. * Remove the top waiter from the current tasks pi waiter tree and
  907. * queue it up.
  908. *
  909. * Called with lock->wait_lock held and interrupts disabled.
  910. */
  911. static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  912. struct rt_mutex *lock)
  913. {
  914. struct rt_mutex_waiter *waiter;
  915. raw_spin_lock(&current->pi_lock);
  916. waiter = rt_mutex_top_waiter(lock);
  917. /*
  918. * Remove it from current->pi_waiters and deboost.
  919. *
  920. * We must in fact deboost here in order to ensure we call
  921. * rt_mutex_setprio() to update p->pi_top_task before the
  922. * task unblocks.
  923. */
  924. rt_mutex_dequeue_pi(current, waiter);
  925. rt_mutex_adjust_prio(current);
  926. /*
  927. * As we are waking up the top waiter, and the waiter stays
  928. * queued on the lock until it gets the lock, this lock
  929. * obviously has waiters. Just set the bit here and this has
  930. * the added benefit of forcing all new tasks into the
  931. * slow path making sure no task of lower priority than
  932. * the top waiter can steal this lock.
  933. */
  934. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  935. /*
  936. * We deboosted before waking the top waiter task such that we don't
  937. * run two tasks with the 'same' priority (and ensure the
  938. * p->pi_top_task pointer points to a blocked task). This however can
  939. * lead to priority inversion if we would get preempted after the
  940. * deboost but before waking our donor task, hence the preempt_disable()
  941. * before unlock.
  942. *
  943. * Pairs with preempt_enable() in rt_mutex_postunlock();
  944. */
  945. preempt_disable();
  946. wake_q_add(wake_q, waiter->task);
  947. raw_spin_unlock(&current->pi_lock);
  948. }
  949. /*
  950. * Remove a waiter from a lock and give up
  951. *
  952. * Must be called with lock->wait_lock held and interrupts disabled. I must
  953. * have just failed to try_to_take_rt_mutex().
  954. */
  955. static void remove_waiter(struct rt_mutex *lock,
  956. struct rt_mutex_waiter *waiter)
  957. {
  958. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  959. struct task_struct *owner = rt_mutex_owner(lock);
  960. struct rt_mutex *next_lock;
  961. lockdep_assert_held(&lock->wait_lock);
  962. raw_spin_lock(&current->pi_lock);
  963. rt_mutex_dequeue(lock, waiter);
  964. current->pi_blocked_on = NULL;
  965. raw_spin_unlock(&current->pi_lock);
  966. /*
  967. * Only update priority if the waiter was the highest priority
  968. * waiter of the lock and there is an owner to update.
  969. */
  970. if (!owner || !is_top_waiter)
  971. return;
  972. raw_spin_lock(&owner->pi_lock);
  973. rt_mutex_dequeue_pi(owner, waiter);
  974. if (rt_mutex_has_waiters(lock))
  975. rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
  976. rt_mutex_adjust_prio(owner);
  977. /* Store the lock on which owner is blocked or NULL */
  978. next_lock = task_blocked_on_lock(owner);
  979. raw_spin_unlock(&owner->pi_lock);
  980. /*
  981. * Don't walk the chain, if the owner task is not blocked
  982. * itself.
  983. */
  984. if (!next_lock)
  985. return;
  986. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  987. get_task_struct(owner);
  988. raw_spin_unlock_irq(&lock->wait_lock);
  989. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  990. next_lock, NULL, current);
  991. raw_spin_lock_irq(&lock->wait_lock);
  992. }
  993. /*
  994. * Recheck the pi chain, in case we got a priority setting
  995. *
  996. * Called from sched_setscheduler
  997. */
  998. void rt_mutex_adjust_pi(struct task_struct *task)
  999. {
  1000. struct rt_mutex_waiter *waiter;
  1001. struct rt_mutex *next_lock;
  1002. unsigned long flags;
  1003. raw_spin_lock_irqsave(&task->pi_lock, flags);
  1004. waiter = task->pi_blocked_on;
  1005. if (!waiter || rt_mutex_waiter_equal(waiter, task_to_waiter(task))) {
  1006. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  1007. return;
  1008. }
  1009. next_lock = waiter->lock;
  1010. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  1011. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  1012. get_task_struct(task);
  1013. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  1014. next_lock, NULL, task);
  1015. }
  1016. void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter)
  1017. {
  1018. debug_rt_mutex_init_waiter(waiter);
  1019. RB_CLEAR_NODE(&waiter->pi_tree_entry);
  1020. RB_CLEAR_NODE(&waiter->tree_entry);
  1021. waiter->task = NULL;
  1022. }
  1023. /**
  1024. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  1025. * @lock: the rt_mutex to take
  1026. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  1027. * or TASK_UNINTERRUPTIBLE)
  1028. * @timeout: the pre-initialized and started timer, or NULL for none
  1029. * @waiter: the pre-initialized rt_mutex_waiter
  1030. *
  1031. * Must be called with lock->wait_lock held and interrupts disabled
  1032. */
  1033. static int __sched
  1034. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  1035. struct hrtimer_sleeper *timeout,
  1036. struct rt_mutex_waiter *waiter)
  1037. {
  1038. int ret = 0;
  1039. for (;;) {
  1040. /* Try to acquire the lock: */
  1041. if (try_to_take_rt_mutex(lock, current, waiter))
  1042. break;
  1043. /*
  1044. * TASK_INTERRUPTIBLE checks for signals and
  1045. * timeout. Ignored otherwise.
  1046. */
  1047. if (likely(state == TASK_INTERRUPTIBLE)) {
  1048. /* Signal pending? */
  1049. if (signal_pending(current))
  1050. ret = -EINTR;
  1051. if (timeout && !timeout->task)
  1052. ret = -ETIMEDOUT;
  1053. if (ret)
  1054. break;
  1055. }
  1056. raw_spin_unlock_irq(&lock->wait_lock);
  1057. debug_rt_mutex_print_deadlock(waiter);
  1058. schedule();
  1059. raw_spin_lock_irq(&lock->wait_lock);
  1060. set_current_state(state);
  1061. }
  1062. __set_current_state(TASK_RUNNING);
  1063. return ret;
  1064. }
  1065. static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  1066. struct rt_mutex_waiter *w)
  1067. {
  1068. /*
  1069. * If the result is not -EDEADLOCK or the caller requested
  1070. * deadlock detection, nothing to do here.
  1071. */
  1072. if (res != -EDEADLOCK || detect_deadlock)
  1073. return;
  1074. /*
  1075. * Yell lowdly and stop the task right here.
  1076. */
  1077. rt_mutex_print_deadlock(w);
  1078. while (1) {
  1079. set_current_state(TASK_INTERRUPTIBLE);
  1080. schedule();
  1081. }
  1082. }
  1083. /*
  1084. * Slow path lock function:
  1085. */
  1086. static int __sched
  1087. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  1088. struct hrtimer_sleeper *timeout,
  1089. enum rtmutex_chainwalk chwalk)
  1090. {
  1091. struct rt_mutex_waiter waiter;
  1092. unsigned long flags;
  1093. int ret = 0;
  1094. rt_mutex_init_waiter(&waiter);
  1095. /*
  1096. * Technically we could use raw_spin_[un]lock_irq() here, but this can
  1097. * be called in early boot if the cmpxchg() fast path is disabled
  1098. * (debug, no architecture support). In this case we will acquire the
  1099. * rtmutex with lock->wait_lock held. But we cannot unconditionally
  1100. * enable interrupts in that early boot case. So we need to use the
  1101. * irqsave/restore variants.
  1102. */
  1103. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1104. /* Try to acquire the lock again: */
  1105. if (try_to_take_rt_mutex(lock, current, NULL)) {
  1106. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1107. return 0;
  1108. }
  1109. set_current_state(state);
  1110. /* Setup the timer, when timeout != NULL */
  1111. if (unlikely(timeout))
  1112. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1113. ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
  1114. if (likely(!ret))
  1115. /* sleep on the mutex */
  1116. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  1117. if (unlikely(ret)) {
  1118. __set_current_state(TASK_RUNNING);
  1119. remove_waiter(lock, &waiter);
  1120. rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  1121. }
  1122. /*
  1123. * try_to_take_rt_mutex() sets the waiter bit
  1124. * unconditionally. We might have to fix that up.
  1125. */
  1126. fixup_rt_mutex_waiters(lock);
  1127. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1128. /* Remove pending timer: */
  1129. if (unlikely(timeout))
  1130. hrtimer_cancel(&timeout->timer);
  1131. debug_rt_mutex_free_waiter(&waiter);
  1132. return ret;
  1133. }
  1134. static inline int __rt_mutex_slowtrylock(struct rt_mutex *lock)
  1135. {
  1136. int ret = try_to_take_rt_mutex(lock, current, NULL);
  1137. /*
  1138. * try_to_take_rt_mutex() sets the lock waiters bit
  1139. * unconditionally. Clean this up.
  1140. */
  1141. fixup_rt_mutex_waiters(lock);
  1142. return ret;
  1143. }
  1144. /*
  1145. * Slow path try-lock function:
  1146. */
  1147. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  1148. {
  1149. unsigned long flags;
  1150. int ret;
  1151. /*
  1152. * If the lock already has an owner we fail to get the lock.
  1153. * This can be done without taking the @lock->wait_lock as
  1154. * it is only being read, and this is a trylock anyway.
  1155. */
  1156. if (rt_mutex_owner(lock))
  1157. return 0;
  1158. /*
  1159. * The mutex has currently no owner. Lock the wait lock and try to
  1160. * acquire the lock. We use irqsave here to support early boot calls.
  1161. */
  1162. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1163. ret = __rt_mutex_slowtrylock(lock);
  1164. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1165. return ret;
  1166. }
  1167. /*
  1168. * Slow path to release a rt-mutex.
  1169. *
  1170. * Return whether the current task needs to call rt_mutex_postunlock().
  1171. */
  1172. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  1173. struct wake_q_head *wake_q)
  1174. {
  1175. unsigned long flags;
  1176. /* irqsave required to support early boot calls */
  1177. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1178. debug_rt_mutex_unlock(lock);
  1179. /*
  1180. * We must be careful here if the fast path is enabled. If we
  1181. * have no waiters queued we cannot set owner to NULL here
  1182. * because of:
  1183. *
  1184. * foo->lock->owner = NULL;
  1185. * rtmutex_lock(foo->lock); <- fast path
  1186. * free = atomic_dec_and_test(foo->refcnt);
  1187. * rtmutex_unlock(foo->lock); <- fast path
  1188. * if (free)
  1189. * kfree(foo);
  1190. * raw_spin_unlock(foo->lock->wait_lock);
  1191. *
  1192. * So for the fastpath enabled kernel:
  1193. *
  1194. * Nothing can set the waiters bit as long as we hold
  1195. * lock->wait_lock. So we do the following sequence:
  1196. *
  1197. * owner = rt_mutex_owner(lock);
  1198. * clear_rt_mutex_waiters(lock);
  1199. * raw_spin_unlock(&lock->wait_lock);
  1200. * if (cmpxchg(&lock->owner, owner, 0) == owner)
  1201. * return;
  1202. * goto retry;
  1203. *
  1204. * The fastpath disabled variant is simple as all access to
  1205. * lock->owner is serialized by lock->wait_lock:
  1206. *
  1207. * lock->owner = NULL;
  1208. * raw_spin_unlock(&lock->wait_lock);
  1209. */
  1210. while (!rt_mutex_has_waiters(lock)) {
  1211. /* Drops lock->wait_lock ! */
  1212. if (unlock_rt_mutex_safe(lock, flags) == true)
  1213. return false;
  1214. /* Relock the rtmutex and try again */
  1215. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1216. }
  1217. /*
  1218. * The wakeup next waiter path does not suffer from the above
  1219. * race. See the comments there.
  1220. *
  1221. * Queue the next waiter for wakeup once we release the wait_lock.
  1222. */
  1223. mark_wakeup_next_waiter(wake_q, lock);
  1224. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1225. return true; /* call rt_mutex_postunlock() */
  1226. }
  1227. /*
  1228. * debug aware fast / slowpath lock,trylock,unlock
  1229. *
  1230. * The atomic acquire/release ops are compiled away, when either the
  1231. * architecture does not support cmpxchg or when debugging is enabled.
  1232. */
  1233. static inline int
  1234. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  1235. int (*slowfn)(struct rt_mutex *lock, int state,
  1236. struct hrtimer_sleeper *timeout,
  1237. enum rtmutex_chainwalk chwalk))
  1238. {
  1239. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  1240. return 0;
  1241. return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  1242. }
  1243. static inline int
  1244. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  1245. struct hrtimer_sleeper *timeout,
  1246. enum rtmutex_chainwalk chwalk,
  1247. int (*slowfn)(struct rt_mutex *lock, int state,
  1248. struct hrtimer_sleeper *timeout,
  1249. enum rtmutex_chainwalk chwalk))
  1250. {
  1251. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  1252. likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  1253. return 0;
  1254. return slowfn(lock, state, timeout, chwalk);
  1255. }
  1256. static inline int
  1257. rt_mutex_fasttrylock(struct rt_mutex *lock,
  1258. int (*slowfn)(struct rt_mutex *lock))
  1259. {
  1260. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  1261. return 1;
  1262. return slowfn(lock);
  1263. }
  1264. /*
  1265. * Performs the wakeup of the the top-waiter and re-enables preemption.
  1266. */
  1267. void rt_mutex_postunlock(struct wake_q_head *wake_q)
  1268. {
  1269. wake_up_q(wake_q);
  1270. /* Pairs with preempt_disable() in rt_mutex_slowunlock() */
  1271. preempt_enable();
  1272. }
  1273. static inline void
  1274. rt_mutex_fastunlock(struct rt_mutex *lock,
  1275. bool (*slowfn)(struct rt_mutex *lock,
  1276. struct wake_q_head *wqh))
  1277. {
  1278. DEFINE_WAKE_Q(wake_q);
  1279. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
  1280. return;
  1281. if (slowfn(lock, &wake_q))
  1282. rt_mutex_postunlock(&wake_q);
  1283. }
  1284. static inline void __rt_mutex_lock(struct rt_mutex *lock, unsigned int subclass)
  1285. {
  1286. might_sleep();
  1287. mutex_acquire(&lock->dep_map, subclass, 0, _RET_IP_);
  1288. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  1289. }
  1290. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  1291. /**
  1292. * rt_mutex_lock_nested - lock a rt_mutex
  1293. *
  1294. * @lock: the rt_mutex to be locked
  1295. * @subclass: the lockdep subclass
  1296. */
  1297. void __sched rt_mutex_lock_nested(struct rt_mutex *lock, unsigned int subclass)
  1298. {
  1299. __rt_mutex_lock(lock, subclass);
  1300. }
  1301. EXPORT_SYMBOL_GPL(rt_mutex_lock_nested);
  1302. #else /* !CONFIG_DEBUG_LOCK_ALLOC */
  1303. /**
  1304. * rt_mutex_lock - lock a rt_mutex
  1305. *
  1306. * @lock: the rt_mutex to be locked
  1307. */
  1308. void __sched rt_mutex_lock(struct rt_mutex *lock)
  1309. {
  1310. __rt_mutex_lock(lock, 0);
  1311. }
  1312. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  1313. #endif
  1314. /**
  1315. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  1316. *
  1317. * @lock: the rt_mutex to be locked
  1318. *
  1319. * Returns:
  1320. * 0 on success
  1321. * -EINTR when interrupted by a signal
  1322. */
  1323. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  1324. {
  1325. int ret;
  1326. might_sleep();
  1327. mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  1328. ret = rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  1329. if (ret)
  1330. mutex_release(&lock->dep_map, 1, _RET_IP_);
  1331. return ret;
  1332. }
  1333. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  1334. /*
  1335. * Futex variant, must not use fastpath.
  1336. */
  1337. int __sched rt_mutex_futex_trylock(struct rt_mutex *lock)
  1338. {
  1339. return rt_mutex_slowtrylock(lock);
  1340. }
  1341. int __sched __rt_mutex_futex_trylock(struct rt_mutex *lock)
  1342. {
  1343. return __rt_mutex_slowtrylock(lock);
  1344. }
  1345. /**
  1346. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  1347. * the timeout structure is provided
  1348. * by the caller
  1349. *
  1350. * @lock: the rt_mutex to be locked
  1351. * @timeout: timeout structure or NULL (no timeout)
  1352. *
  1353. * Returns:
  1354. * 0 on success
  1355. * -EINTR when interrupted by a signal
  1356. * -ETIMEDOUT when the timeout expired
  1357. */
  1358. int
  1359. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
  1360. {
  1361. int ret;
  1362. might_sleep();
  1363. mutex_acquire(&lock->dep_map, 0, 0, _RET_IP_);
  1364. ret = rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1365. RT_MUTEX_MIN_CHAINWALK,
  1366. rt_mutex_slowlock);
  1367. if (ret)
  1368. mutex_release(&lock->dep_map, 1, _RET_IP_);
  1369. return ret;
  1370. }
  1371. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  1372. /**
  1373. * rt_mutex_trylock - try to lock a rt_mutex
  1374. *
  1375. * @lock: the rt_mutex to be locked
  1376. *
  1377. * This function can only be called in thread context. It's safe to
  1378. * call it from atomic regions, but not from hard interrupt or soft
  1379. * interrupt context.
  1380. *
  1381. * Returns 1 on success and 0 on contention
  1382. */
  1383. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  1384. {
  1385. int ret;
  1386. if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
  1387. return 0;
  1388. ret = rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  1389. if (ret)
  1390. mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  1391. return ret;
  1392. }
  1393. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  1394. /**
  1395. * rt_mutex_unlock - unlock a rt_mutex
  1396. *
  1397. * @lock: the rt_mutex to be unlocked
  1398. */
  1399. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  1400. {
  1401. mutex_release(&lock->dep_map, 1, _RET_IP_);
  1402. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  1403. }
  1404. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  1405. /**
  1406. * Futex variant, that since futex variants do not use the fast-path, can be
  1407. * simple and will not need to retry.
  1408. */
  1409. bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock,
  1410. struct wake_q_head *wake_q)
  1411. {
  1412. lockdep_assert_held(&lock->wait_lock);
  1413. debug_rt_mutex_unlock(lock);
  1414. if (!rt_mutex_has_waiters(lock)) {
  1415. lock->owner = NULL;
  1416. return false; /* done */
  1417. }
  1418. /*
  1419. * We've already deboosted, mark_wakeup_next_waiter() will
  1420. * retain preempt_disabled when we drop the wait_lock, to
  1421. * avoid inversion prior to the wakeup. preempt_disable()
  1422. * therein pairs with rt_mutex_postunlock().
  1423. */
  1424. mark_wakeup_next_waiter(wake_q, lock);
  1425. return true; /* call postunlock() */
  1426. }
  1427. void __sched rt_mutex_futex_unlock(struct rt_mutex *lock)
  1428. {
  1429. DEFINE_WAKE_Q(wake_q);
  1430. unsigned long flags;
  1431. bool postunlock;
  1432. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1433. postunlock = __rt_mutex_futex_unlock(lock, &wake_q);
  1434. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1435. if (postunlock)
  1436. rt_mutex_postunlock(&wake_q);
  1437. }
  1438. /**
  1439. * rt_mutex_destroy - mark a mutex unusable
  1440. * @lock: the mutex to be destroyed
  1441. *
  1442. * This function marks the mutex uninitialized, and any subsequent
  1443. * use of the mutex is forbidden. The mutex must not be locked when
  1444. * this function is called.
  1445. */
  1446. void rt_mutex_destroy(struct rt_mutex *lock)
  1447. {
  1448. WARN_ON(rt_mutex_is_locked(lock));
  1449. #ifdef CONFIG_DEBUG_RT_MUTEXES
  1450. lock->magic = NULL;
  1451. #endif
  1452. }
  1453. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  1454. /**
  1455. * __rt_mutex_init - initialize the rt lock
  1456. *
  1457. * @lock: the rt lock to be initialized
  1458. *
  1459. * Initialize the rt lock to unlocked state.
  1460. *
  1461. * Initializing of a locked rt lock is not allowed
  1462. */
  1463. void __rt_mutex_init(struct rt_mutex *lock, const char *name,
  1464. struct lock_class_key *key)
  1465. {
  1466. lock->owner = NULL;
  1467. raw_spin_lock_init(&lock->wait_lock);
  1468. lock->waiters = RB_ROOT_CACHED;
  1469. if (name && key)
  1470. debug_rt_mutex_init(lock, name, key);
  1471. }
  1472. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  1473. /**
  1474. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  1475. * proxy owner
  1476. *
  1477. * @lock: the rt_mutex to be locked
  1478. * @proxy_owner:the task to set as owner
  1479. *
  1480. * No locking. Caller has to do serializing itself
  1481. *
  1482. * Special API call for PI-futex support. This initializes the rtmutex and
  1483. * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
  1484. * possible at this point because the pi_state which contains the rtmutex
  1485. * is not yet visible to other tasks.
  1486. */
  1487. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  1488. struct task_struct *proxy_owner)
  1489. {
  1490. __rt_mutex_init(lock, NULL, NULL);
  1491. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  1492. rt_mutex_set_owner(lock, proxy_owner);
  1493. }
  1494. /**
  1495. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  1496. *
  1497. * @lock: the rt_mutex to be locked
  1498. *
  1499. * No locking. Caller has to do serializing itself
  1500. *
  1501. * Special API call for PI-futex support. This merrily cleans up the rtmutex
  1502. * (debugging) state. Concurrent operations on this rt_mutex are not
  1503. * possible because it belongs to the pi_state which is about to be freed
  1504. * and it is not longer visible to other tasks.
  1505. */
  1506. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  1507. struct task_struct *proxy_owner)
  1508. {
  1509. debug_rt_mutex_proxy_unlock(lock);
  1510. rt_mutex_set_owner(lock, NULL);
  1511. }
  1512. int __rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  1513. struct rt_mutex_waiter *waiter,
  1514. struct task_struct *task)
  1515. {
  1516. int ret;
  1517. if (try_to_take_rt_mutex(lock, task, NULL))
  1518. return 1;
  1519. /* We enforce deadlock detection for futexes */
  1520. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  1521. RT_MUTEX_FULL_CHAINWALK);
  1522. if (ret && !rt_mutex_owner(lock)) {
  1523. /*
  1524. * Reset the return value. We might have
  1525. * returned with -EDEADLK and the owner
  1526. * released the lock while we were walking the
  1527. * pi chain. Let the waiter sort it out.
  1528. */
  1529. ret = 0;
  1530. }
  1531. if (unlikely(ret))
  1532. remove_waiter(lock, waiter);
  1533. debug_rt_mutex_print_deadlock(waiter);
  1534. return ret;
  1535. }
  1536. /**
  1537. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  1538. * @lock: the rt_mutex to take
  1539. * @waiter: the pre-initialized rt_mutex_waiter
  1540. * @task: the task to prepare
  1541. *
  1542. * Returns:
  1543. * 0 - task blocked on lock
  1544. * 1 - acquired the lock for task, caller should wake it up
  1545. * <0 - error
  1546. *
  1547. * Special API call for FUTEX_REQUEUE_PI support.
  1548. */
  1549. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  1550. struct rt_mutex_waiter *waiter,
  1551. struct task_struct *task)
  1552. {
  1553. int ret;
  1554. raw_spin_lock_irq(&lock->wait_lock);
  1555. ret = __rt_mutex_start_proxy_lock(lock, waiter, task);
  1556. raw_spin_unlock_irq(&lock->wait_lock);
  1557. return ret;
  1558. }
  1559. /**
  1560. * rt_mutex_next_owner - return the next owner of the lock
  1561. *
  1562. * @lock: the rt lock query
  1563. *
  1564. * Returns the next owner of the lock or NULL
  1565. *
  1566. * Caller has to serialize against other accessors to the lock
  1567. * itself.
  1568. *
  1569. * Special API call for PI-futex support
  1570. */
  1571. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  1572. {
  1573. if (!rt_mutex_has_waiters(lock))
  1574. return NULL;
  1575. return rt_mutex_top_waiter(lock)->task;
  1576. }
  1577. /**
  1578. * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
  1579. * @lock: the rt_mutex we were woken on
  1580. * @to: the timeout, null if none. hrtimer should already have
  1581. * been started.
  1582. * @waiter: the pre-initialized rt_mutex_waiter
  1583. *
  1584. * Wait for the the lock acquisition started on our behalf by
  1585. * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
  1586. * rt_mutex_cleanup_proxy_lock().
  1587. *
  1588. * Returns:
  1589. * 0 - success
  1590. * <0 - error, one of -EINTR, -ETIMEDOUT
  1591. *
  1592. * Special API call for PI-futex support
  1593. */
  1594. int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
  1595. struct hrtimer_sleeper *to,
  1596. struct rt_mutex_waiter *waiter)
  1597. {
  1598. int ret;
  1599. raw_spin_lock_irq(&lock->wait_lock);
  1600. /* sleep on the mutex */
  1601. set_current_state(TASK_INTERRUPTIBLE);
  1602. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  1603. /*
  1604. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1605. * have to fix that up.
  1606. */
  1607. fixup_rt_mutex_waiters(lock);
  1608. raw_spin_unlock_irq(&lock->wait_lock);
  1609. return ret;
  1610. }
  1611. /**
  1612. * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
  1613. * @lock: the rt_mutex we were woken on
  1614. * @waiter: the pre-initialized rt_mutex_waiter
  1615. *
  1616. * Attempt to clean up after a failed rt_mutex_wait_proxy_lock().
  1617. *
  1618. * Unless we acquired the lock; we're still enqueued on the wait-list and can
  1619. * in fact still be granted ownership until we're removed. Therefore we can
  1620. * find we are in fact the owner and must disregard the
  1621. * rt_mutex_wait_proxy_lock() failure.
  1622. *
  1623. * Returns:
  1624. * true - did the cleanup, we done.
  1625. * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
  1626. * caller should disregards its return value.
  1627. *
  1628. * Special API call for PI-futex support
  1629. */
  1630. bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
  1631. struct rt_mutex_waiter *waiter)
  1632. {
  1633. bool cleanup = false;
  1634. raw_spin_lock_irq(&lock->wait_lock);
  1635. /*
  1636. * Do an unconditional try-lock, this deals with the lock stealing
  1637. * state where __rt_mutex_futex_unlock() -> mark_wakeup_next_waiter()
  1638. * sets a NULL owner.
  1639. *
  1640. * We're not interested in the return value, because the subsequent
  1641. * test on rt_mutex_owner() will infer that. If the trylock succeeded,
  1642. * we will own the lock and it will have removed the waiter. If we
  1643. * failed the trylock, we're still not owner and we need to remove
  1644. * ourselves.
  1645. */
  1646. try_to_take_rt_mutex(lock, current, waiter);
  1647. /*
  1648. * Unless we're the owner; we're still enqueued on the wait_list.
  1649. * So check if we became owner, if not, take us off the wait_list.
  1650. */
  1651. if (rt_mutex_owner(lock) != current) {
  1652. remove_waiter(lock, waiter);
  1653. cleanup = true;
  1654. }
  1655. /*
  1656. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1657. * have to fix that up.
  1658. */
  1659. fixup_rt_mutex_waiters(lock);
  1660. raw_spin_unlock_irq(&lock->wait_lock);
  1661. return cleanup;
  1662. }