rtmutex.c 47 KB

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