rtmutex.c 44 KB

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