rtmutex.c 47 KB

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