rtmutex.c 45 KB

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