rtmutex.c 44 KB

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