rtmutex.c 44 KB

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