rtmutex.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  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. __releases(lock->wait_lock)
  155. {
  156. struct task_struct *owner = rt_mutex_owner(lock);
  157. clear_rt_mutex_waiters(lock);
  158. raw_spin_unlock(&lock->wait_lock);
  159. /*
  160. * If a new waiter comes in between the unlock and the cmpxchg
  161. * we have two situations:
  162. *
  163. * unlock(wait_lock);
  164. * lock(wait_lock);
  165. * cmpxchg(p, owner, 0) == owner
  166. * mark_rt_mutex_waiters(lock);
  167. * acquire(lock);
  168. * or:
  169. *
  170. * unlock(wait_lock);
  171. * lock(wait_lock);
  172. * mark_rt_mutex_waiters(lock);
  173. *
  174. * cmpxchg(p, owner, 0) != owner
  175. * enqueue_waiter();
  176. * unlock(wait_lock);
  177. * lock(wait_lock);
  178. * wake waiter();
  179. * unlock(wait_lock);
  180. * lock(wait_lock);
  181. * acquire(lock);
  182. */
  183. return rt_mutex_cmpxchg_release(lock, owner, NULL);
  184. }
  185. #else
  186. # define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
  187. # define rt_mutex_cmpxchg_acquire(l,c,n) (0)
  188. # define rt_mutex_cmpxchg_release(l,c,n) (0)
  189. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  190. {
  191. lock->owner = (struct task_struct *)
  192. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  193. }
  194. /*
  195. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  196. */
  197. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  198. __releases(lock->wait_lock)
  199. {
  200. lock->owner = NULL;
  201. raw_spin_unlock(&lock->wait_lock);
  202. return true;
  203. }
  204. #endif
  205. static inline int
  206. rt_mutex_waiter_less(struct rt_mutex_waiter *left,
  207. struct rt_mutex_waiter *right)
  208. {
  209. if (left->prio < right->prio)
  210. return 1;
  211. /*
  212. * If both waiters have dl_prio(), we check the deadlines of the
  213. * associated tasks.
  214. * If left waiter has a dl_prio(), and we didn't return 1 above,
  215. * then right waiter has a dl_prio() too.
  216. */
  217. if (dl_prio(left->prio))
  218. return dl_time_before(left->task->dl.deadline,
  219. right->task->dl.deadline);
  220. return 0;
  221. }
  222. static void
  223. rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  224. {
  225. struct rb_node **link = &lock->waiters.rb_node;
  226. struct rb_node *parent = NULL;
  227. struct rt_mutex_waiter *entry;
  228. int leftmost = 1;
  229. while (*link) {
  230. parent = *link;
  231. entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
  232. if (rt_mutex_waiter_less(waiter, entry)) {
  233. link = &parent->rb_left;
  234. } else {
  235. link = &parent->rb_right;
  236. leftmost = 0;
  237. }
  238. }
  239. if (leftmost)
  240. lock->waiters_leftmost = &waiter->tree_entry;
  241. rb_link_node(&waiter->tree_entry, parent, link);
  242. rb_insert_color(&waiter->tree_entry, &lock->waiters);
  243. }
  244. static void
  245. rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  246. {
  247. if (RB_EMPTY_NODE(&waiter->tree_entry))
  248. return;
  249. if (lock->waiters_leftmost == &waiter->tree_entry)
  250. lock->waiters_leftmost = rb_next(&waiter->tree_entry);
  251. rb_erase(&waiter->tree_entry, &lock->waiters);
  252. RB_CLEAR_NODE(&waiter->tree_entry);
  253. }
  254. static void
  255. rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  256. {
  257. struct rb_node **link = &task->pi_waiters.rb_node;
  258. struct rb_node *parent = NULL;
  259. struct rt_mutex_waiter *entry;
  260. int leftmost = 1;
  261. while (*link) {
  262. parent = *link;
  263. entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
  264. if (rt_mutex_waiter_less(waiter, entry)) {
  265. link = &parent->rb_left;
  266. } else {
  267. link = &parent->rb_right;
  268. leftmost = 0;
  269. }
  270. }
  271. if (leftmost)
  272. task->pi_waiters_leftmost = &waiter->pi_tree_entry;
  273. rb_link_node(&waiter->pi_tree_entry, parent, link);
  274. rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
  275. }
  276. static void
  277. rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  278. {
  279. if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
  280. return;
  281. if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
  282. task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
  283. rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
  284. RB_CLEAR_NODE(&waiter->pi_tree_entry);
  285. }
  286. /*
  287. * Calculate task priority from the waiter tree priority
  288. *
  289. * Return task->normal_prio when the waiter tree is empty or when
  290. * the waiter is not allowed to do priority boosting
  291. */
  292. int rt_mutex_getprio(struct task_struct *task)
  293. {
  294. if (likely(!task_has_pi_waiters(task)))
  295. return task->normal_prio;
  296. return min(task_top_pi_waiter(task)->prio,
  297. task->normal_prio);
  298. }
  299. struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
  300. {
  301. if (likely(!task_has_pi_waiters(task)))
  302. return NULL;
  303. return task_top_pi_waiter(task)->task;
  304. }
  305. /*
  306. * Called by sched_setscheduler() to get the priority which will be
  307. * effective after the change.
  308. */
  309. int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
  310. {
  311. if (!task_has_pi_waiters(task))
  312. return newprio;
  313. if (task_top_pi_waiter(task)->task->prio <= newprio)
  314. return task_top_pi_waiter(task)->task->prio;
  315. return newprio;
  316. }
  317. /*
  318. * Adjust the priority of a task, after its pi_waiters got modified.
  319. *
  320. * This can be both boosting and unboosting. task->pi_lock must be held.
  321. */
  322. static void __rt_mutex_adjust_prio(struct task_struct *task)
  323. {
  324. int prio = rt_mutex_getprio(task);
  325. if (task->prio != prio || dl_prio(prio))
  326. rt_mutex_setprio(task, prio);
  327. }
  328. /*
  329. * Adjust task priority (undo boosting). Called from the exit path of
  330. * rt_mutex_slowunlock() and rt_mutex_slowlock().
  331. *
  332. * (Note: We do this outside of the protection of lock->wait_lock to
  333. * allow the lock to be taken while or before we readjust the priority
  334. * of task. We do not use the spin_xx_mutex() variants here as we are
  335. * outside of the debug path.)
  336. */
  337. void rt_mutex_adjust_prio(struct task_struct *task)
  338. {
  339. unsigned long flags;
  340. raw_spin_lock_irqsave(&task->pi_lock, flags);
  341. __rt_mutex_adjust_prio(task);
  342. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  343. }
  344. /*
  345. * Deadlock detection is conditional:
  346. *
  347. * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
  348. * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
  349. *
  350. * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
  351. * conducted independent of the detect argument.
  352. *
  353. * If the waiter argument is NULL this indicates the deboost path and
  354. * deadlock detection is disabled independent of the detect argument
  355. * and the config settings.
  356. */
  357. static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  358. enum rtmutex_chainwalk chwalk)
  359. {
  360. /*
  361. * This is just a wrapper function for the following call,
  362. * because debug_rt_mutex_detect_deadlock() smells like a magic
  363. * debug feature and I wanted to keep the cond function in the
  364. * main source file along with the comments instead of having
  365. * two of the same in the headers.
  366. */
  367. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  368. }
  369. /*
  370. * Max number of times we'll walk the boosting chain:
  371. */
  372. int max_lock_depth = 1024;
  373. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  374. {
  375. return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  376. }
  377. /*
  378. * Adjust the priority chain. Also used for deadlock detection.
  379. * Decreases task's usage by one - may thus free the task.
  380. *
  381. * @task: the task owning the mutex (owner) for which a chain walk is
  382. * probably needed
  383. * @chwalk: do we have to carry out deadlock detection?
  384. * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
  385. * things for a task that has just got its priority adjusted, and
  386. * is waiting on a mutex)
  387. * @next_lock: the mutex on which the owner of @orig_lock was blocked before
  388. * we dropped its pi_lock. Is never dereferenced, only used for
  389. * comparison to detect lock chain changes.
  390. * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
  391. * its priority to the mutex owner (can be NULL in the case
  392. * depicted above or if the top waiter is gone away and we are
  393. * actually deboosting the owner)
  394. * @top_task: the current top waiter
  395. *
  396. * Returns 0 or -EDEADLK.
  397. *
  398. * Chain walk basics and protection scope
  399. *
  400. * [R] refcount on task
  401. * [P] task->pi_lock held
  402. * [L] rtmutex->wait_lock held
  403. *
  404. * Step Description Protected by
  405. * function arguments:
  406. * @task [R]
  407. * @orig_lock if != NULL @top_task is blocked on it
  408. * @next_lock Unprotected. Cannot be
  409. * dereferenced. Only used for
  410. * comparison.
  411. * @orig_waiter if != NULL @top_task is blocked on it
  412. * @top_task current, or in case of proxy
  413. * locking protected by calling
  414. * code
  415. * again:
  416. * loop_sanity_check();
  417. * retry:
  418. * [1] lock(task->pi_lock); [R] acquire [P]
  419. * [2] waiter = task->pi_blocked_on; [P]
  420. * [3] check_exit_conditions_1(); [P]
  421. * [4] lock = waiter->lock; [P]
  422. * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
  423. * unlock(task->pi_lock); release [P]
  424. * goto retry;
  425. * }
  426. * [6] check_exit_conditions_2(); [P] + [L]
  427. * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
  428. * [8] unlock(task->pi_lock); release [P]
  429. * put_task_struct(task); release [R]
  430. * [9] check_exit_conditions_3(); [L]
  431. * [10] task = owner(lock); [L]
  432. * get_task_struct(task); [L] acquire [R]
  433. * lock(task->pi_lock); [L] acquire [P]
  434. * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
  435. * [12] check_exit_conditions_4(); [P] + [L]
  436. * [13] unlock(task->pi_lock); release [P]
  437. * unlock(lock->wait_lock); release [L]
  438. * goto again;
  439. */
  440. static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  441. enum rtmutex_chainwalk chwalk,
  442. struct rt_mutex *orig_lock,
  443. struct rt_mutex *next_lock,
  444. struct rt_mutex_waiter *orig_waiter,
  445. struct task_struct *top_task)
  446. {
  447. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  448. struct rt_mutex_waiter *prerequeue_top_waiter;
  449. int ret = 0, depth = 0;
  450. struct rt_mutex *lock;
  451. bool detect_deadlock;
  452. unsigned long flags;
  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_irqsave(&task->pi_lock, flags);
  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_irqrestore(&task->pi_lock, flags);
  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_irqrestore(&task->pi_lock, flags);
  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(&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_irqsave(&task->pi_lock, flags);
  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_irqrestore(&task->pi_lock, flags);
  622. raw_spin_unlock(&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. waiter->prio = task->prio;
  637. rt_mutex_enqueue(lock, waiter);
  638. /* [8] Release the task */
  639. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  640. put_task_struct(task);
  641. /*
  642. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  643. *
  644. * We must abort the chain walk if there is no lock owner even
  645. * in the dead lock detection case, as we have nothing to
  646. * follow here. This is the end of the chain we are walking.
  647. */
  648. if (!rt_mutex_owner(lock)) {
  649. /*
  650. * If the requeue [7] above changed the top waiter,
  651. * then we need to wake the new top waiter up to try
  652. * to get the lock.
  653. */
  654. if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  655. wake_up_process(rt_mutex_top_waiter(lock)->task);
  656. raw_spin_unlock(&lock->wait_lock);
  657. return 0;
  658. }
  659. /* [10] Grab the next task, i.e. the owner of @lock */
  660. task = rt_mutex_owner(lock);
  661. get_task_struct(task);
  662. raw_spin_lock_irqsave(&task->pi_lock, flags);
  663. /* [11] requeue the pi waiters if necessary */
  664. if (waiter == rt_mutex_top_waiter(lock)) {
  665. /*
  666. * The waiter became the new top (highest priority)
  667. * waiter on the lock. Replace the previous top waiter
  668. * in the owner tasks pi waiters tree with this waiter
  669. * and adjust the priority of the owner.
  670. */
  671. rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
  672. rt_mutex_enqueue_pi(task, waiter);
  673. __rt_mutex_adjust_prio(task);
  674. } else if (prerequeue_top_waiter == waiter) {
  675. /*
  676. * The waiter was the top waiter on the lock, but is
  677. * no longer the top prority waiter. Replace waiter in
  678. * the owner tasks pi waiters tree with the new top
  679. * (highest priority) waiter and adjust the priority
  680. * of the owner.
  681. * The new top waiter is stored in @waiter so that
  682. * @waiter == @top_waiter evaluates to true below and
  683. * we continue to deboost the rest of the chain.
  684. */
  685. rt_mutex_dequeue_pi(task, waiter);
  686. waiter = rt_mutex_top_waiter(lock);
  687. rt_mutex_enqueue_pi(task, waiter);
  688. __rt_mutex_adjust_prio(task);
  689. } else {
  690. /*
  691. * Nothing changed. No need to do any priority
  692. * adjustment.
  693. */
  694. }
  695. /*
  696. * [12] check_exit_conditions_4() protected by task->pi_lock
  697. * and lock->wait_lock. The actual decisions are made after we
  698. * dropped the locks.
  699. *
  700. * Check whether the task which owns the current lock is pi
  701. * blocked itself. If yes we store a pointer to the lock for
  702. * the lock chain change detection above. After we dropped
  703. * task->pi_lock next_lock cannot be dereferenced anymore.
  704. */
  705. next_lock = task_blocked_on_lock(task);
  706. /*
  707. * Store the top waiter of @lock for the end of chain walk
  708. * decision below.
  709. */
  710. top_waiter = rt_mutex_top_waiter(lock);
  711. /* [13] Drop the locks */
  712. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  713. raw_spin_unlock(&lock->wait_lock);
  714. /*
  715. * Make the actual exit decisions [12], based on the stored
  716. * values.
  717. *
  718. * We reached the end of the lock chain. Stop right here. No
  719. * point to go back just to figure that out.
  720. */
  721. if (!next_lock)
  722. goto out_put_task;
  723. /*
  724. * If the current waiter is not the top waiter on the lock,
  725. * then we can stop the chain walk here if we are not in full
  726. * deadlock detection mode.
  727. */
  728. if (!detect_deadlock && waiter != top_waiter)
  729. goto out_put_task;
  730. goto again;
  731. out_unlock_pi:
  732. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  733. out_put_task:
  734. put_task_struct(task);
  735. return ret;
  736. }
  737. /*
  738. * Try to take an rt-mutex
  739. *
  740. * Must be called with lock->wait_lock held.
  741. *
  742. * @lock: The lock to be acquired.
  743. * @task: The task which wants to acquire the lock
  744. * @waiter: The waiter that is queued to the lock's wait tree if the
  745. * callsite called task_blocked_on_lock(), otherwise NULL
  746. */
  747. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  748. struct rt_mutex_waiter *waiter)
  749. {
  750. unsigned long flags;
  751. /*
  752. * Before testing whether we can acquire @lock, we set the
  753. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  754. * other tasks which try to modify @lock into the slow path
  755. * and they serialize on @lock->wait_lock.
  756. *
  757. * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
  758. * as explained at the top of this file if and only if:
  759. *
  760. * - There is a lock owner. The caller must fixup the
  761. * transient state if it does a trylock or leaves the lock
  762. * function due to a signal or timeout.
  763. *
  764. * - @task acquires the lock and there are no other
  765. * waiters. This is undone in rt_mutex_set_owner(@task) at
  766. * the end of this function.
  767. */
  768. mark_rt_mutex_waiters(lock);
  769. /*
  770. * If @lock has an owner, give up.
  771. */
  772. if (rt_mutex_owner(lock))
  773. return 0;
  774. /*
  775. * If @waiter != NULL, @task has already enqueued the waiter
  776. * into @lock waiter tree. If @waiter == NULL then this is a
  777. * trylock attempt.
  778. */
  779. if (waiter) {
  780. /*
  781. * If waiter is not the highest priority waiter of
  782. * @lock, give up.
  783. */
  784. if (waiter != rt_mutex_top_waiter(lock))
  785. return 0;
  786. /*
  787. * We can acquire the lock. Remove the waiter from the
  788. * lock waiters tree.
  789. */
  790. rt_mutex_dequeue(lock, waiter);
  791. } else {
  792. /*
  793. * If the lock has waiters already we check whether @task is
  794. * eligible to take over the lock.
  795. *
  796. * If there are no other waiters, @task can acquire
  797. * the lock. @task->pi_blocked_on is NULL, so it does
  798. * not need to be dequeued.
  799. */
  800. if (rt_mutex_has_waiters(lock)) {
  801. /*
  802. * If @task->prio is greater than or equal to
  803. * the top waiter priority (kernel view),
  804. * @task lost.
  805. */
  806. if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  807. return 0;
  808. /*
  809. * The current top waiter stays enqueued. We
  810. * don't have to change anything in the lock
  811. * waiters order.
  812. */
  813. } else {
  814. /*
  815. * No waiters. Take the lock without the
  816. * pi_lock dance.@task->pi_blocked_on is NULL
  817. * and we have no waiters to enqueue in @task
  818. * pi waiters tree.
  819. */
  820. goto takeit;
  821. }
  822. }
  823. /*
  824. * Clear @task->pi_blocked_on. Requires protection by
  825. * @task->pi_lock. Redundant operation for the @waiter == NULL
  826. * case, but conditionals are more expensive than a redundant
  827. * store.
  828. */
  829. raw_spin_lock_irqsave(&task->pi_lock, flags);
  830. task->pi_blocked_on = NULL;
  831. /*
  832. * Finish the lock acquisition. @task is the new owner. If
  833. * other waiters exist we have to insert the highest priority
  834. * waiter into @task->pi_waiters tree.
  835. */
  836. if (rt_mutex_has_waiters(lock))
  837. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  838. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  839. takeit:
  840. /* We got the lock. */
  841. debug_rt_mutex_lock(lock);
  842. /*
  843. * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
  844. * are still waiters or clears it.
  845. */
  846. rt_mutex_set_owner(lock, task);
  847. rt_mutex_deadlock_account_lock(lock, task);
  848. return 1;
  849. }
  850. /*
  851. * Task blocks on lock.
  852. *
  853. * Prepare waiter and propagate pi chain
  854. *
  855. * This must be called with lock->wait_lock held.
  856. */
  857. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  858. struct rt_mutex_waiter *waiter,
  859. struct task_struct *task,
  860. enum rtmutex_chainwalk chwalk)
  861. {
  862. struct task_struct *owner = rt_mutex_owner(lock);
  863. struct rt_mutex_waiter *top_waiter = waiter;
  864. struct rt_mutex *next_lock;
  865. int chain_walk = 0, res;
  866. unsigned long flags;
  867. /*
  868. * Early deadlock detection. We really don't want the task to
  869. * enqueue on itself just to untangle the mess later. It's not
  870. * only an optimization. We drop the locks, so another waiter
  871. * can come in before the chain walk detects the deadlock. So
  872. * the other will detect the deadlock and return -EDEADLOCK,
  873. * which is wrong, as the other waiter is not in a deadlock
  874. * situation.
  875. */
  876. if (owner == task)
  877. return -EDEADLK;
  878. raw_spin_lock_irqsave(&task->pi_lock, flags);
  879. __rt_mutex_adjust_prio(task);
  880. waiter->task = task;
  881. waiter->lock = lock;
  882. waiter->prio = task->prio;
  883. /* Get the top priority waiter on the lock */
  884. if (rt_mutex_has_waiters(lock))
  885. top_waiter = rt_mutex_top_waiter(lock);
  886. rt_mutex_enqueue(lock, waiter);
  887. task->pi_blocked_on = waiter;
  888. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  889. if (!owner)
  890. return 0;
  891. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  892. if (waiter == rt_mutex_top_waiter(lock)) {
  893. rt_mutex_dequeue_pi(owner, top_waiter);
  894. rt_mutex_enqueue_pi(owner, waiter);
  895. __rt_mutex_adjust_prio(owner);
  896. if (owner->pi_blocked_on)
  897. chain_walk = 1;
  898. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  899. chain_walk = 1;
  900. }
  901. /* Store the lock on which owner is blocked or NULL */
  902. next_lock = task_blocked_on_lock(owner);
  903. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  904. /*
  905. * Even if full deadlock detection is on, if the owner is not
  906. * blocked itself, we can avoid finding this out in the chain
  907. * walk.
  908. */
  909. if (!chain_walk || !next_lock)
  910. return 0;
  911. /*
  912. * The owner can't disappear while holding a lock,
  913. * so the owner struct is protected by wait_lock.
  914. * Gets dropped in rt_mutex_adjust_prio_chain()!
  915. */
  916. get_task_struct(owner);
  917. raw_spin_unlock(&lock->wait_lock);
  918. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  919. next_lock, waiter, task);
  920. raw_spin_lock(&lock->wait_lock);
  921. return res;
  922. }
  923. /*
  924. * Remove the top waiter from the current tasks pi waiter tree and
  925. * queue it up.
  926. *
  927. * Called with lock->wait_lock held.
  928. */
  929. static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  930. struct rt_mutex *lock)
  931. {
  932. struct rt_mutex_waiter *waiter;
  933. unsigned long flags;
  934. raw_spin_lock_irqsave(&current->pi_lock, flags);
  935. waiter = rt_mutex_top_waiter(lock);
  936. /*
  937. * Remove it from current->pi_waiters. We do not adjust a
  938. * possible priority boost right now. We execute wakeup in the
  939. * boosted mode and go back to normal after releasing
  940. * lock->wait_lock.
  941. */
  942. rt_mutex_dequeue_pi(current, waiter);
  943. /*
  944. * As we are waking up the top waiter, and the waiter stays
  945. * queued on the lock until it gets the lock, this lock
  946. * obviously has waiters. Just set the bit here and this has
  947. * the added benefit of forcing all new tasks into the
  948. * slow path making sure no task of lower priority than
  949. * the top waiter can steal this lock.
  950. */
  951. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  952. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  953. wake_q_add(wake_q, waiter->task);
  954. }
  955. /*
  956. * Remove a waiter from a lock and give up
  957. *
  958. * Must be called with lock->wait_lock held and
  959. * have just failed to try_to_take_rt_mutex().
  960. */
  961. static void remove_waiter(struct rt_mutex *lock,
  962. struct rt_mutex_waiter *waiter)
  963. {
  964. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  965. struct task_struct *owner = rt_mutex_owner(lock);
  966. struct rt_mutex *next_lock;
  967. unsigned long flags;
  968. raw_spin_lock_irqsave(&current->pi_lock, flags);
  969. rt_mutex_dequeue(lock, waiter);
  970. current->pi_blocked_on = NULL;
  971. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  972. /*
  973. * Only update priority if the waiter was the highest priority
  974. * waiter of the lock and there is an owner to update.
  975. */
  976. if (!owner || !is_top_waiter)
  977. return;
  978. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  979. rt_mutex_dequeue_pi(owner, waiter);
  980. if (rt_mutex_has_waiters(lock))
  981. rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
  982. __rt_mutex_adjust_prio(owner);
  983. /* Store the lock on which owner is blocked or NULL */
  984. next_lock = task_blocked_on_lock(owner);
  985. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  986. /*
  987. * Don't walk the chain, if the owner task is not blocked
  988. * itself.
  989. */
  990. if (!next_lock)
  991. return;
  992. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  993. get_task_struct(owner);
  994. raw_spin_unlock(&lock->wait_lock);
  995. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  996. next_lock, NULL, current);
  997. raw_spin_lock(&lock->wait_lock);
  998. }
  999. /*
  1000. * Recheck the pi chain, in case we got a priority setting
  1001. *
  1002. * Called from sched_setscheduler
  1003. */
  1004. void rt_mutex_adjust_pi(struct task_struct *task)
  1005. {
  1006. struct rt_mutex_waiter *waiter;
  1007. struct rt_mutex *next_lock;
  1008. unsigned long flags;
  1009. raw_spin_lock_irqsave(&task->pi_lock, flags);
  1010. waiter = task->pi_blocked_on;
  1011. if (!waiter || (waiter->prio == task->prio &&
  1012. !dl_prio(task->prio))) {
  1013. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  1014. return;
  1015. }
  1016. next_lock = waiter->lock;
  1017. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  1018. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  1019. get_task_struct(task);
  1020. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  1021. next_lock, NULL, task);
  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. * lock->wait_lock must be held by the caller.
  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 (unlikely(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(&lock->wait_lock);
  1057. debug_rt_mutex_print_deadlock(waiter);
  1058. schedule();
  1059. raw_spin_lock(&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. int ret = 0;
  1093. debug_rt_mutex_init_waiter(&waiter);
  1094. RB_CLEAR_NODE(&waiter.pi_tree_entry);
  1095. RB_CLEAR_NODE(&waiter.tree_entry);
  1096. raw_spin_lock(&lock->wait_lock);
  1097. /* Try to acquire the lock again: */
  1098. if (try_to_take_rt_mutex(lock, current, NULL)) {
  1099. raw_spin_unlock(&lock->wait_lock);
  1100. return 0;
  1101. }
  1102. set_current_state(state);
  1103. /* Setup the timer, when timeout != NULL */
  1104. if (unlikely(timeout))
  1105. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1106. ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
  1107. if (likely(!ret))
  1108. /* sleep on the mutex */
  1109. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  1110. if (unlikely(ret)) {
  1111. __set_current_state(TASK_RUNNING);
  1112. if (rt_mutex_has_waiters(lock))
  1113. remove_waiter(lock, &waiter);
  1114. rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  1115. }
  1116. /*
  1117. * try_to_take_rt_mutex() sets the waiter bit
  1118. * unconditionally. We might have to fix that up.
  1119. */
  1120. fixup_rt_mutex_waiters(lock);
  1121. raw_spin_unlock(&lock->wait_lock);
  1122. /* Remove pending timer: */
  1123. if (unlikely(timeout))
  1124. hrtimer_cancel(&timeout->timer);
  1125. debug_rt_mutex_free_waiter(&waiter);
  1126. return ret;
  1127. }
  1128. /*
  1129. * Slow path try-lock function:
  1130. */
  1131. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  1132. {
  1133. int ret;
  1134. /*
  1135. * If the lock already has an owner we fail to get the lock.
  1136. * This can be done without taking the @lock->wait_lock as
  1137. * it is only being read, and this is a trylock anyway.
  1138. */
  1139. if (rt_mutex_owner(lock))
  1140. return 0;
  1141. /*
  1142. * The mutex has currently no owner. Lock the wait lock and
  1143. * try to acquire the lock.
  1144. */
  1145. raw_spin_lock(&lock->wait_lock);
  1146. ret = try_to_take_rt_mutex(lock, current, NULL);
  1147. /*
  1148. * try_to_take_rt_mutex() sets the lock waiters bit
  1149. * unconditionally. Clean this up.
  1150. */
  1151. fixup_rt_mutex_waiters(lock);
  1152. raw_spin_unlock(&lock->wait_lock);
  1153. return ret;
  1154. }
  1155. /*
  1156. * Slow path to release a rt-mutex.
  1157. * Return whether the current task needs to undo a potential priority boosting.
  1158. */
  1159. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  1160. struct wake_q_head *wake_q)
  1161. {
  1162. raw_spin_lock(&lock->wait_lock);
  1163. debug_rt_mutex_unlock(lock);
  1164. rt_mutex_deadlock_account_unlock(current);
  1165. /*
  1166. * We must be careful here if the fast path is enabled. If we
  1167. * have no waiters queued we cannot set owner to NULL here
  1168. * because of:
  1169. *
  1170. * foo->lock->owner = NULL;
  1171. * rtmutex_lock(foo->lock); <- fast path
  1172. * free = atomic_dec_and_test(foo->refcnt);
  1173. * rtmutex_unlock(foo->lock); <- fast path
  1174. * if (free)
  1175. * kfree(foo);
  1176. * raw_spin_unlock(foo->lock->wait_lock);
  1177. *
  1178. * So for the fastpath enabled kernel:
  1179. *
  1180. * Nothing can set the waiters bit as long as we hold
  1181. * lock->wait_lock. So we do the following sequence:
  1182. *
  1183. * owner = rt_mutex_owner(lock);
  1184. * clear_rt_mutex_waiters(lock);
  1185. * raw_spin_unlock(&lock->wait_lock);
  1186. * if (cmpxchg(&lock->owner, owner, 0) == owner)
  1187. * return;
  1188. * goto retry;
  1189. *
  1190. * The fastpath disabled variant is simple as all access to
  1191. * lock->owner is serialized by lock->wait_lock:
  1192. *
  1193. * lock->owner = NULL;
  1194. * raw_spin_unlock(&lock->wait_lock);
  1195. */
  1196. while (!rt_mutex_has_waiters(lock)) {
  1197. /* Drops lock->wait_lock ! */
  1198. if (unlock_rt_mutex_safe(lock) == true)
  1199. return false;
  1200. /* Relock the rtmutex and try again */
  1201. raw_spin_lock(&lock->wait_lock);
  1202. }
  1203. /*
  1204. * The wakeup next waiter path does not suffer from the above
  1205. * race. See the comments there.
  1206. *
  1207. * Queue the next waiter for wakeup once we release the wait_lock.
  1208. */
  1209. mark_wakeup_next_waiter(wake_q, lock);
  1210. raw_spin_unlock(&lock->wait_lock);
  1211. /* check PI boosting */
  1212. return true;
  1213. }
  1214. /*
  1215. * debug aware fast / slowpath lock,trylock,unlock
  1216. *
  1217. * The atomic acquire/release ops are compiled away, when either the
  1218. * architecture does not support cmpxchg or when debugging is enabled.
  1219. */
  1220. static inline int
  1221. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  1222. int (*slowfn)(struct rt_mutex *lock, int state,
  1223. struct hrtimer_sleeper *timeout,
  1224. enum rtmutex_chainwalk chwalk))
  1225. {
  1226. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  1227. rt_mutex_deadlock_account_lock(lock, current);
  1228. return 0;
  1229. } else
  1230. return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  1231. }
  1232. static inline int
  1233. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  1234. struct hrtimer_sleeper *timeout,
  1235. enum rtmutex_chainwalk chwalk,
  1236. int (*slowfn)(struct rt_mutex *lock, int state,
  1237. struct hrtimer_sleeper *timeout,
  1238. enum rtmutex_chainwalk chwalk))
  1239. {
  1240. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  1241. likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  1242. rt_mutex_deadlock_account_lock(lock, current);
  1243. return 0;
  1244. } else
  1245. return slowfn(lock, state, timeout, chwalk);
  1246. }
  1247. static inline int
  1248. rt_mutex_fasttrylock(struct rt_mutex *lock,
  1249. int (*slowfn)(struct rt_mutex *lock))
  1250. {
  1251. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  1252. rt_mutex_deadlock_account_lock(lock, current);
  1253. return 1;
  1254. }
  1255. return slowfn(lock);
  1256. }
  1257. static inline void
  1258. rt_mutex_fastunlock(struct rt_mutex *lock,
  1259. bool (*slowfn)(struct rt_mutex *lock,
  1260. struct wake_q_head *wqh))
  1261. {
  1262. WAKE_Q(wake_q);
  1263. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  1264. rt_mutex_deadlock_account_unlock(current);
  1265. } else {
  1266. bool deboost = slowfn(lock, &wake_q);
  1267. wake_up_q(&wake_q);
  1268. /* Undo pi boosting if necessary: */
  1269. if (deboost)
  1270. rt_mutex_adjust_prio(current);
  1271. }
  1272. }
  1273. /**
  1274. * rt_mutex_lock - lock a rt_mutex
  1275. *
  1276. * @lock: the rt_mutex to be locked
  1277. */
  1278. void __sched rt_mutex_lock(struct rt_mutex *lock)
  1279. {
  1280. might_sleep();
  1281. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  1282. }
  1283. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  1284. /**
  1285. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  1286. *
  1287. * @lock: the rt_mutex to be locked
  1288. *
  1289. * Returns:
  1290. * 0 on success
  1291. * -EINTR when interrupted by a signal
  1292. */
  1293. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  1294. {
  1295. might_sleep();
  1296. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  1297. }
  1298. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  1299. /*
  1300. * Futex variant with full deadlock detection.
  1301. */
  1302. int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
  1303. struct hrtimer_sleeper *timeout)
  1304. {
  1305. might_sleep();
  1306. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1307. RT_MUTEX_FULL_CHAINWALK,
  1308. rt_mutex_slowlock);
  1309. }
  1310. /**
  1311. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  1312. * the timeout structure is provided
  1313. * by the caller
  1314. *
  1315. * @lock: the rt_mutex to be locked
  1316. * @timeout: timeout structure or NULL (no timeout)
  1317. *
  1318. * Returns:
  1319. * 0 on success
  1320. * -EINTR when interrupted by a signal
  1321. * -ETIMEDOUT when the timeout expired
  1322. */
  1323. int
  1324. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
  1325. {
  1326. might_sleep();
  1327. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1328. RT_MUTEX_MIN_CHAINWALK,
  1329. rt_mutex_slowlock);
  1330. }
  1331. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  1332. /**
  1333. * rt_mutex_trylock - try to lock a rt_mutex
  1334. *
  1335. * @lock: the rt_mutex to be locked
  1336. *
  1337. * This function can only be called in thread context. It's safe to
  1338. * call it from atomic regions, but not from hard interrupt or soft
  1339. * interrupt context.
  1340. *
  1341. * Returns 1 on success and 0 on contention
  1342. */
  1343. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  1344. {
  1345. if (WARN_ON(in_irq() || in_nmi() || in_serving_softirq()))
  1346. return 0;
  1347. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  1348. }
  1349. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  1350. /**
  1351. * rt_mutex_unlock - unlock a rt_mutex
  1352. *
  1353. * @lock: the rt_mutex to be unlocked
  1354. */
  1355. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  1356. {
  1357. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  1358. }
  1359. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  1360. /**
  1361. * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
  1362. * @lock: the rt_mutex to be unlocked
  1363. *
  1364. * Returns: true/false indicating whether priority adjustment is
  1365. * required or not.
  1366. */
  1367. bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
  1368. struct wake_q_head *wqh)
  1369. {
  1370. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  1371. rt_mutex_deadlock_account_unlock(current);
  1372. return false;
  1373. }
  1374. return rt_mutex_slowunlock(lock, wqh);
  1375. }
  1376. /**
  1377. * rt_mutex_destroy - mark a mutex unusable
  1378. * @lock: the mutex to be destroyed
  1379. *
  1380. * This function marks the mutex uninitialized, and any subsequent
  1381. * use of the mutex is forbidden. The mutex must not be locked when
  1382. * this function is called.
  1383. */
  1384. void rt_mutex_destroy(struct rt_mutex *lock)
  1385. {
  1386. WARN_ON(rt_mutex_is_locked(lock));
  1387. #ifdef CONFIG_DEBUG_RT_MUTEXES
  1388. lock->magic = NULL;
  1389. #endif
  1390. }
  1391. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  1392. /**
  1393. * __rt_mutex_init - initialize the rt lock
  1394. *
  1395. * @lock: the rt lock to be initialized
  1396. *
  1397. * Initialize the rt lock to unlocked state.
  1398. *
  1399. * Initializing of a locked rt lock is not allowed
  1400. */
  1401. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  1402. {
  1403. lock->owner = NULL;
  1404. raw_spin_lock_init(&lock->wait_lock);
  1405. lock->waiters = RB_ROOT;
  1406. lock->waiters_leftmost = NULL;
  1407. debug_rt_mutex_init(lock, name);
  1408. }
  1409. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  1410. /**
  1411. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  1412. * proxy owner
  1413. *
  1414. * @lock: the rt_mutex to be locked
  1415. * @proxy_owner:the task to set as owner
  1416. *
  1417. * No locking. Caller has to do serializing itself
  1418. * Special API call for PI-futex support
  1419. */
  1420. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  1421. struct task_struct *proxy_owner)
  1422. {
  1423. __rt_mutex_init(lock, NULL);
  1424. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  1425. rt_mutex_set_owner(lock, proxy_owner);
  1426. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  1427. }
  1428. /**
  1429. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  1430. *
  1431. * @lock: the rt_mutex to be locked
  1432. *
  1433. * No locking. Caller has to do serializing itself
  1434. * Special API call for PI-futex support
  1435. */
  1436. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  1437. struct task_struct *proxy_owner)
  1438. {
  1439. debug_rt_mutex_proxy_unlock(lock);
  1440. rt_mutex_set_owner(lock, NULL);
  1441. rt_mutex_deadlock_account_unlock(proxy_owner);
  1442. }
  1443. /**
  1444. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  1445. * @lock: the rt_mutex to take
  1446. * @waiter: the pre-initialized rt_mutex_waiter
  1447. * @task: the task to prepare
  1448. *
  1449. * Returns:
  1450. * 0 - task blocked on lock
  1451. * 1 - acquired the lock for task, caller should wake it up
  1452. * <0 - error
  1453. *
  1454. * Special API call for FUTEX_REQUEUE_PI support.
  1455. */
  1456. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  1457. struct rt_mutex_waiter *waiter,
  1458. struct task_struct *task)
  1459. {
  1460. int ret;
  1461. raw_spin_lock(&lock->wait_lock);
  1462. if (try_to_take_rt_mutex(lock, task, NULL)) {
  1463. raw_spin_unlock(&lock->wait_lock);
  1464. return 1;
  1465. }
  1466. /* We enforce deadlock detection for futexes */
  1467. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  1468. RT_MUTEX_FULL_CHAINWALK);
  1469. if (ret && !rt_mutex_owner(lock)) {
  1470. /*
  1471. * Reset the return value. We might have
  1472. * returned with -EDEADLK and the owner
  1473. * released the lock while we were walking the
  1474. * pi chain. Let the waiter sort it out.
  1475. */
  1476. ret = 0;
  1477. }
  1478. if (unlikely(ret))
  1479. remove_waiter(lock, waiter);
  1480. raw_spin_unlock(&lock->wait_lock);
  1481. debug_rt_mutex_print_deadlock(waiter);
  1482. return ret;
  1483. }
  1484. /**
  1485. * rt_mutex_next_owner - return the next owner of the lock
  1486. *
  1487. * @lock: the rt lock query
  1488. *
  1489. * Returns the next owner of the lock or NULL
  1490. *
  1491. * Caller has to serialize against other accessors to the lock
  1492. * itself.
  1493. *
  1494. * Special API call for PI-futex support
  1495. */
  1496. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  1497. {
  1498. if (!rt_mutex_has_waiters(lock))
  1499. return NULL;
  1500. return rt_mutex_top_waiter(lock)->task;
  1501. }
  1502. /**
  1503. * rt_mutex_wait_proxy_lock() - Wait for lock acquisition
  1504. * @lock: the rt_mutex we were woken on
  1505. * @to: the timeout, null if none. hrtimer should already have
  1506. * been started.
  1507. * @waiter: the pre-initialized rt_mutex_waiter
  1508. *
  1509. * Wait for the the lock acquisition started on our behalf by
  1510. * rt_mutex_start_proxy_lock(). Upon failure, the caller must call
  1511. * rt_mutex_cleanup_proxy_lock().
  1512. *
  1513. * Returns:
  1514. * 0 - success
  1515. * <0 - error, one of -EINTR, -ETIMEDOUT
  1516. *
  1517. * Special API call for PI-futex support
  1518. */
  1519. int rt_mutex_wait_proxy_lock(struct rt_mutex *lock,
  1520. struct hrtimer_sleeper *to,
  1521. struct rt_mutex_waiter *waiter)
  1522. {
  1523. int ret;
  1524. raw_spin_lock(&lock->wait_lock);
  1525. set_current_state(TASK_INTERRUPTIBLE);
  1526. /* sleep on the mutex */
  1527. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  1528. /*
  1529. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1530. * have to fix that up.
  1531. */
  1532. fixup_rt_mutex_waiters(lock);
  1533. raw_spin_unlock(&lock->wait_lock);
  1534. return ret;
  1535. }
  1536. /**
  1537. * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition
  1538. * @lock: the rt_mutex we were woken on
  1539. * @waiter: the pre-initialized rt_mutex_waiter
  1540. *
  1541. * Attempt to clean up after a failed rt_mutex_wait_proxy_lock().
  1542. *
  1543. * Unless we acquired the lock; we're still enqueued on the wait-list and can
  1544. * in fact still be granted ownership until we're removed. Therefore we can
  1545. * find we are in fact the owner and must disregard the
  1546. * rt_mutex_wait_proxy_lock() failure.
  1547. *
  1548. * Returns:
  1549. * true - did the cleanup, we done.
  1550. * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned,
  1551. * caller should disregards its return value.
  1552. *
  1553. * Special API call for PI-futex support
  1554. */
  1555. bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock,
  1556. struct rt_mutex_waiter *waiter)
  1557. {
  1558. bool cleanup = false;
  1559. raw_spin_lock_irq(&lock->wait_lock);
  1560. /*
  1561. * Unless we're the owner; we're still enqueued on the wait_list.
  1562. * So check if we became owner, if not, take us off the wait_list.
  1563. */
  1564. if (rt_mutex_owner(lock) != current) {
  1565. remove_waiter(lock, waiter);
  1566. fixup_rt_mutex_waiters(lock);
  1567. cleanup = true;
  1568. }
  1569. raw_spin_unlock_irq(&lock->wait_lock);
  1570. return cleanup;
  1571. }