rtmutex.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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/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/timer.h>
  17. #include "rtmutex_common.h"
  18. /*
  19. * lock->owner state tracking:
  20. *
  21. * lock->owner holds the task_struct pointer of the owner. Bit 0
  22. * is used to keep track of the "lock has waiters" state.
  23. *
  24. * owner bit0
  25. * NULL 0 lock is free (fast acquire possible)
  26. * NULL 1 lock is free and has waiters and the top waiter
  27. * is going to take the lock*
  28. * taskpointer 0 lock is held (fast release possible)
  29. * taskpointer 1 lock is held and has waiters**
  30. *
  31. * The fast atomic compare exchange based acquire and release is only
  32. * possible when bit 0 of lock->owner is 0.
  33. *
  34. * (*) It also can be a transitional state when grabbing the lock
  35. * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
  36. * we need to set the bit0 before looking at the lock, and the owner may be
  37. * NULL in this small time, hence this can be a transitional state.
  38. *
  39. * (**) There is a small time when bit 0 is set but there are no
  40. * waiters. This can happen when grabbing the lock in the slow path.
  41. * To prevent a cmpxchg of the owner releasing the lock, we need to
  42. * set this bit before looking at the lock.
  43. */
  44. static void
  45. rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
  46. {
  47. unsigned long val = (unsigned long)owner;
  48. if (rt_mutex_has_waiters(lock))
  49. val |= RT_MUTEX_HAS_WAITERS;
  50. lock->owner = (struct task_struct *)val;
  51. }
  52. static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
  53. {
  54. lock->owner = (struct task_struct *)
  55. ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
  56. }
  57. static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  58. {
  59. unsigned long owner, *p = (unsigned long *) &lock->owner;
  60. if (rt_mutex_has_waiters(lock))
  61. return;
  62. /*
  63. * The rbtree has no waiters enqueued, now make sure that the
  64. * lock->owner still has the waiters bit set, otherwise the
  65. * following can happen:
  66. *
  67. * CPU 0 CPU 1 CPU2
  68. * l->owner=T1
  69. * rt_mutex_lock(l)
  70. * lock(l->lock)
  71. * l->owner = T1 | HAS_WAITERS;
  72. * enqueue(T2)
  73. * boost()
  74. * unlock(l->lock)
  75. * block()
  76. *
  77. * rt_mutex_lock(l)
  78. * lock(l->lock)
  79. * l->owner = T1 | HAS_WAITERS;
  80. * enqueue(T3)
  81. * boost()
  82. * unlock(l->lock)
  83. * block()
  84. * signal(->T2) signal(->T3)
  85. * lock(l->lock)
  86. * dequeue(T2)
  87. * deboost()
  88. * unlock(l->lock)
  89. * lock(l->lock)
  90. * dequeue(T3)
  91. * ==> wait list is empty
  92. * deboost()
  93. * unlock(l->lock)
  94. * lock(l->lock)
  95. * fixup_rt_mutex_waiters()
  96. * if (wait_list_empty(l) {
  97. * l->owner = owner
  98. * owner = l->owner & ~HAS_WAITERS;
  99. * ==> l->owner = T1
  100. * }
  101. * lock(l->lock)
  102. * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
  103. * if (wait_list_empty(l) {
  104. * owner = l->owner & ~HAS_WAITERS;
  105. * cmpxchg(l->owner, T1, NULL)
  106. * ===> Success (l->owner = NULL)
  107. *
  108. * l->owner = owner
  109. * ==> l->owner = T1
  110. * }
  111. *
  112. * With the check for the waiter bit in place T3 on CPU2 will not
  113. * overwrite. All tasks fiddling with the waiters bit are
  114. * serialized by l->lock, so nothing else can modify the waiters
  115. * bit. If the bit is set then nothing can change l->owner either
  116. * so the simple RMW is safe. The cmpxchg() will simply fail if it
  117. * happens in the middle of the RMW because the waiters bit is
  118. * still set.
  119. */
  120. owner = READ_ONCE(*p);
  121. if (owner & RT_MUTEX_HAS_WAITERS)
  122. WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
  123. }
  124. /*
  125. * We can speed up the acquire/release, if the architecture
  126. * supports cmpxchg and if there's no debugging state to be set up
  127. */
  128. #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
  129. # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
  130. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  131. {
  132. unsigned long owner, *p = (unsigned long *) &lock->owner;
  133. do {
  134. owner = *p;
  135. } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
  136. }
  137. /*
  138. * Safe fastpath aware unlock:
  139. * 1) Clear the waiters bit
  140. * 2) Drop lock->wait_lock
  141. * 3) Try to unlock the lock with cmpxchg
  142. */
  143. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  144. __releases(lock->wait_lock)
  145. {
  146. struct task_struct *owner = rt_mutex_owner(lock);
  147. clear_rt_mutex_waiters(lock);
  148. raw_spin_unlock(&lock->wait_lock);
  149. /*
  150. * If a new waiter comes in between the unlock and the cmpxchg
  151. * we have two situations:
  152. *
  153. * unlock(wait_lock);
  154. * lock(wait_lock);
  155. * cmpxchg(p, owner, 0) == owner
  156. * mark_rt_mutex_waiters(lock);
  157. * acquire(lock);
  158. * or:
  159. *
  160. * unlock(wait_lock);
  161. * lock(wait_lock);
  162. * mark_rt_mutex_waiters(lock);
  163. *
  164. * cmpxchg(p, owner, 0) != owner
  165. * enqueue_waiter();
  166. * unlock(wait_lock);
  167. * lock(wait_lock);
  168. * wake waiter();
  169. * unlock(wait_lock);
  170. * lock(wait_lock);
  171. * acquire(lock);
  172. */
  173. return rt_mutex_cmpxchg(lock, owner, NULL);
  174. }
  175. #else
  176. # define rt_mutex_cmpxchg(l,c,n) (0)
  177. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  178. {
  179. lock->owner = (struct task_struct *)
  180. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  181. }
  182. /*
  183. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  184. */
  185. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  186. __releases(lock->wait_lock)
  187. {
  188. lock->owner = NULL;
  189. raw_spin_unlock(&lock->wait_lock);
  190. return true;
  191. }
  192. #endif
  193. /*
  194. * Calculate task priority from the waiter list priority
  195. *
  196. * Return task->normal_prio when the waiter list is empty or when
  197. * the waiter is not allowed to do priority boosting
  198. */
  199. int rt_mutex_getprio(struct task_struct *task)
  200. {
  201. if (likely(!task_has_pi_waiters(task)))
  202. return task->normal_prio;
  203. return min(task_top_pi_waiter(task)->pi_list_entry.prio,
  204. task->normal_prio);
  205. }
  206. /*
  207. * Adjust the priority of a task, after its pi_waiters got modified.
  208. *
  209. * This can be both boosting and unboosting. task->pi_lock must be held.
  210. */
  211. static void __rt_mutex_adjust_prio(struct task_struct *task)
  212. {
  213. int prio = rt_mutex_getprio(task);
  214. if (task->prio != prio)
  215. rt_mutex_setprio(task, prio);
  216. }
  217. /*
  218. * Adjust task priority (undo boosting). Called from the exit path of
  219. * rt_mutex_slowunlock() and rt_mutex_slowlock().
  220. *
  221. * (Note: We do this outside of the protection of lock->wait_lock to
  222. * allow the lock to be taken while or before we readjust the priority
  223. * of task. We do not use the spin_xx_mutex() variants here as we are
  224. * outside of the debug path.)
  225. */
  226. static void rt_mutex_adjust_prio(struct task_struct *task)
  227. {
  228. unsigned long flags;
  229. raw_spin_lock_irqsave(&task->pi_lock, flags);
  230. __rt_mutex_adjust_prio(task);
  231. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  232. }
  233. /*
  234. * Max number of times we'll walk the boosting chain:
  235. */
  236. int max_lock_depth = 1024;
  237. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  238. {
  239. return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  240. }
  241. /*
  242. * Adjust the priority chain. Also used for deadlock detection.
  243. * Decreases task's usage by one - may thus free the task.
  244. * Returns 0 or -EDEADLK.
  245. */
  246. static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  247. int deadlock_detect,
  248. struct rt_mutex *orig_lock,
  249. struct rt_mutex *next_lock,
  250. struct rt_mutex_waiter *orig_waiter,
  251. struct task_struct *top_task)
  252. {
  253. struct rt_mutex *lock;
  254. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  255. int detect_deadlock, ret = 0, depth = 0;
  256. unsigned long flags;
  257. detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
  258. deadlock_detect);
  259. /*
  260. * The (de)boosting is a step by step approach with a lot of
  261. * pitfalls. We want this to be preemptible and we want hold a
  262. * maximum of two locks per step. So we have to check
  263. * carefully whether things change under us.
  264. */
  265. again:
  266. if (++depth > max_lock_depth) {
  267. static int prev_max;
  268. /*
  269. * Print this only once. If the admin changes the limit,
  270. * print a new message when reaching the limit again.
  271. */
  272. if (prev_max != max_lock_depth) {
  273. prev_max = max_lock_depth;
  274. printk(KERN_WARNING "Maximum lock depth %d reached "
  275. "task: %s (%d)\n", max_lock_depth,
  276. top_task->comm, task_pid_nr(top_task));
  277. }
  278. put_task_struct(task);
  279. return -EDEADLK;
  280. }
  281. retry:
  282. /*
  283. * Task can not go away as we did a get_task() before !
  284. */
  285. raw_spin_lock_irqsave(&task->pi_lock, flags);
  286. waiter = task->pi_blocked_on;
  287. /*
  288. * Check whether the end of the boosting chain has been
  289. * reached or the state of the chain has changed while we
  290. * dropped the locks.
  291. */
  292. if (!waiter)
  293. goto out_unlock_pi;
  294. /*
  295. * Check the orig_waiter state. After we dropped the locks,
  296. * the previous owner of the lock might have released the lock.
  297. */
  298. if (orig_waiter && !rt_mutex_owner(orig_lock))
  299. goto out_unlock_pi;
  300. /*
  301. * We dropped all locks after taking a refcount on @task, so
  302. * the task might have moved on in the lock chain or even left
  303. * the chain completely and blocks now on an unrelated lock or
  304. * on @orig_lock.
  305. *
  306. * We stored the lock on which @task was blocked in @next_lock,
  307. * so we can detect the chain change.
  308. */
  309. if (next_lock != waiter->lock)
  310. goto out_unlock_pi;
  311. /*
  312. * Drop out, when the task has no waiters. Note,
  313. * top_waiter can be NULL, when we are in the deboosting
  314. * mode!
  315. */
  316. if (top_waiter) {
  317. if (!task_has_pi_waiters(task))
  318. goto out_unlock_pi;
  319. /*
  320. * If deadlock detection is off, we stop here if we
  321. * are not the top pi waiter of the task.
  322. */
  323. if (!detect_deadlock && top_waiter != task_top_pi_waiter(task))
  324. goto out_unlock_pi;
  325. }
  326. /*
  327. * When deadlock detection is off then we check, if further
  328. * priority adjustment is necessary.
  329. */
  330. if (!detect_deadlock && waiter->list_entry.prio == task->prio)
  331. goto out_unlock_pi;
  332. lock = waiter->lock;
  333. if (!raw_spin_trylock(&lock->wait_lock)) {
  334. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  335. cpu_relax();
  336. goto retry;
  337. }
  338. /*
  339. * Deadlock detection. If the lock is the same as the original
  340. * lock which caused us to walk the lock chain or if the
  341. * current lock is owned by the task which initiated the chain
  342. * walk, we detected a deadlock.
  343. */
  344. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  345. debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
  346. raw_spin_unlock(&lock->wait_lock);
  347. ret = -EDEADLK;
  348. goto out_unlock_pi;
  349. }
  350. top_waiter = rt_mutex_top_waiter(lock);
  351. /* Requeue the waiter */
  352. plist_del(&waiter->list_entry, &lock->wait_list);
  353. waiter->list_entry.prio = task->prio;
  354. plist_add(&waiter->list_entry, &lock->wait_list);
  355. /* Release the task */
  356. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  357. if (!rt_mutex_owner(lock)) {
  358. /*
  359. * If the requeue above changed the top waiter, then we need
  360. * to wake the new top waiter up to try to get the lock.
  361. */
  362. if (top_waiter != rt_mutex_top_waiter(lock))
  363. wake_up_process(rt_mutex_top_waiter(lock)->task);
  364. raw_spin_unlock(&lock->wait_lock);
  365. goto out_put_task;
  366. }
  367. put_task_struct(task);
  368. /* Grab the next task */
  369. task = rt_mutex_owner(lock);
  370. get_task_struct(task);
  371. raw_spin_lock_irqsave(&task->pi_lock, flags);
  372. if (waiter == rt_mutex_top_waiter(lock)) {
  373. /* Boost the owner */
  374. plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
  375. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  376. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  377. __rt_mutex_adjust_prio(task);
  378. } else if (top_waiter == waiter) {
  379. /* Deboost the owner */
  380. plist_del(&waiter->pi_list_entry, &task->pi_waiters);
  381. waiter = rt_mutex_top_waiter(lock);
  382. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  383. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  384. __rt_mutex_adjust_prio(task);
  385. }
  386. /*
  387. * Check whether the task which owns the current lock is pi
  388. * blocked itself. If yes we store a pointer to the lock for
  389. * the lock chain change detection above. After we dropped
  390. * task->pi_lock next_lock cannot be dereferenced anymore.
  391. */
  392. next_lock = task_blocked_on_lock(task);
  393. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  394. top_waiter = rt_mutex_top_waiter(lock);
  395. raw_spin_unlock(&lock->wait_lock);
  396. /*
  397. * We reached the end of the lock chain. Stop right here. No
  398. * point to go back just to figure that out.
  399. */
  400. if (!next_lock)
  401. goto out_put_task;
  402. if (!detect_deadlock && waiter != top_waiter)
  403. goto out_put_task;
  404. goto again;
  405. out_unlock_pi:
  406. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  407. out_put_task:
  408. put_task_struct(task);
  409. return ret;
  410. }
  411. /*
  412. * Try to take an rt-mutex
  413. *
  414. * Must be called with lock->wait_lock held.
  415. *
  416. * @lock: the lock to be acquired.
  417. * @task: the task which wants to acquire the lock
  418. * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
  419. */
  420. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  421. struct rt_mutex_waiter *waiter)
  422. {
  423. /*
  424. * We have to be careful here if the atomic speedups are
  425. * enabled, such that, when
  426. * - no other waiter is on the lock
  427. * - the lock has been released since we did the cmpxchg
  428. * the lock can be released or taken while we are doing the
  429. * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
  430. *
  431. * The atomic acquire/release aware variant of
  432. * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
  433. * the WAITERS bit, the atomic release / acquire can not
  434. * happen anymore and lock->wait_lock protects us from the
  435. * non-atomic case.
  436. *
  437. * Note, that this might set lock->owner =
  438. * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
  439. * any more. This is fixed up when we take the ownership.
  440. * This is the transitional state explained at the top of this file.
  441. */
  442. mark_rt_mutex_waiters(lock);
  443. if (rt_mutex_owner(lock))
  444. return 0;
  445. /*
  446. * It will get the lock because of one of these conditions:
  447. * 1) there is no waiter
  448. * 2) higher priority than waiters
  449. * 3) it is top waiter
  450. */
  451. if (rt_mutex_has_waiters(lock)) {
  452. if (task->prio >= rt_mutex_top_waiter(lock)->list_entry.prio) {
  453. if (!waiter || waiter != rt_mutex_top_waiter(lock))
  454. return 0;
  455. }
  456. }
  457. if (waiter || rt_mutex_has_waiters(lock)) {
  458. unsigned long flags;
  459. struct rt_mutex_waiter *top;
  460. raw_spin_lock_irqsave(&task->pi_lock, flags);
  461. /* remove the queued waiter. */
  462. if (waiter) {
  463. plist_del(&waiter->list_entry, &lock->wait_list);
  464. task->pi_blocked_on = NULL;
  465. }
  466. /*
  467. * We have to enqueue the top waiter(if it exists) into
  468. * task->pi_waiters list.
  469. */
  470. if (rt_mutex_has_waiters(lock)) {
  471. top = rt_mutex_top_waiter(lock);
  472. top->pi_list_entry.prio = top->list_entry.prio;
  473. plist_add(&top->pi_list_entry, &task->pi_waiters);
  474. }
  475. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  476. }
  477. /* We got the lock. */
  478. debug_rt_mutex_lock(lock);
  479. rt_mutex_set_owner(lock, task);
  480. rt_mutex_deadlock_account_lock(lock, task);
  481. return 1;
  482. }
  483. /*
  484. * Task blocks on lock.
  485. *
  486. * Prepare waiter and propagate pi chain
  487. *
  488. * This must be called with lock->wait_lock held.
  489. */
  490. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  491. struct rt_mutex_waiter *waiter,
  492. struct task_struct *task,
  493. int detect_deadlock)
  494. {
  495. struct task_struct *owner = rt_mutex_owner(lock);
  496. struct rt_mutex_waiter *top_waiter = waiter;
  497. struct rt_mutex *next_lock;
  498. int chain_walk = 0, res;
  499. unsigned long flags;
  500. /*
  501. * Early deadlock detection. We really don't want the task to
  502. * enqueue on itself just to untangle the mess later. It's not
  503. * only an optimization. We drop the locks, so another waiter
  504. * can come in before the chain walk detects the deadlock. So
  505. * the other will detect the deadlock and return -EDEADLOCK,
  506. * which is wrong, as the other waiter is not in a deadlock
  507. * situation.
  508. */
  509. if (owner == task)
  510. return -EDEADLK;
  511. raw_spin_lock_irqsave(&task->pi_lock, flags);
  512. __rt_mutex_adjust_prio(task);
  513. waiter->task = task;
  514. waiter->lock = lock;
  515. plist_node_init(&waiter->list_entry, task->prio);
  516. plist_node_init(&waiter->pi_list_entry, task->prio);
  517. /* Get the top priority waiter on the lock */
  518. if (rt_mutex_has_waiters(lock))
  519. top_waiter = rt_mutex_top_waiter(lock);
  520. plist_add(&waiter->list_entry, &lock->wait_list);
  521. task->pi_blocked_on = waiter;
  522. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  523. if (!owner)
  524. return 0;
  525. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  526. if (waiter == rt_mutex_top_waiter(lock)) {
  527. plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
  528. plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
  529. __rt_mutex_adjust_prio(owner);
  530. if (owner->pi_blocked_on)
  531. chain_walk = 1;
  532. } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
  533. chain_walk = 1;
  534. }
  535. /* Store the lock on which owner is blocked or NULL */
  536. next_lock = task_blocked_on_lock(owner);
  537. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  538. /*
  539. * Even if full deadlock detection is on, if the owner is not
  540. * blocked itself, we can avoid finding this out in the chain
  541. * walk.
  542. */
  543. if (!chain_walk || !next_lock)
  544. return 0;
  545. /*
  546. * The owner can't disappear while holding a lock,
  547. * so the owner struct is protected by wait_lock.
  548. * Gets dropped in rt_mutex_adjust_prio_chain()!
  549. */
  550. get_task_struct(owner);
  551. raw_spin_unlock(&lock->wait_lock);
  552. res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
  553. next_lock, waiter, task);
  554. raw_spin_lock(&lock->wait_lock);
  555. return res;
  556. }
  557. /*
  558. * Wake up the next waiter on the lock.
  559. *
  560. * Remove the top waiter from the current tasks pi waiter list and
  561. * wake it up.
  562. *
  563. * Called with lock->wait_lock held.
  564. */
  565. static void wakeup_next_waiter(struct rt_mutex *lock)
  566. {
  567. struct rt_mutex_waiter *waiter;
  568. unsigned long flags;
  569. raw_spin_lock_irqsave(&current->pi_lock, flags);
  570. waiter = rt_mutex_top_waiter(lock);
  571. /*
  572. * Remove it from current->pi_waiters. We do not adjust a
  573. * possible priority boost right now. We execute wakeup in the
  574. * boosted mode and go back to normal after releasing
  575. * lock->wait_lock.
  576. */
  577. plist_del(&waiter->pi_list_entry, &current->pi_waiters);
  578. /*
  579. * As we are waking up the top waiter, and the waiter stays
  580. * queued on the lock until it gets the lock, this lock
  581. * obviously has waiters. Just set the bit here and this has
  582. * the added benefit of forcing all new tasks into the
  583. * slow path making sure no task of lower priority than
  584. * the top waiter can steal this lock.
  585. */
  586. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  587. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  588. /*
  589. * It's safe to dereference waiter as it cannot go away as
  590. * long as we hold lock->wait_lock. The waiter task needs to
  591. * acquire it in order to dequeue the waiter.
  592. */
  593. wake_up_process(waiter->task);
  594. }
  595. /*
  596. * Remove a waiter from a lock and give up
  597. *
  598. * Must be called with lock->wait_lock held and
  599. * have just failed to try_to_take_rt_mutex().
  600. */
  601. static void remove_waiter(struct rt_mutex *lock,
  602. struct rt_mutex_waiter *waiter)
  603. {
  604. int first = (waiter == rt_mutex_top_waiter(lock));
  605. struct task_struct *owner = rt_mutex_owner(lock);
  606. struct rt_mutex *next_lock = NULL;
  607. unsigned long flags;
  608. raw_spin_lock_irqsave(&current->pi_lock, flags);
  609. plist_del(&waiter->list_entry, &lock->wait_list);
  610. current->pi_blocked_on = NULL;
  611. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  612. if (!owner)
  613. return;
  614. if (first) {
  615. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  616. plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
  617. if (rt_mutex_has_waiters(lock)) {
  618. struct rt_mutex_waiter *next;
  619. next = rt_mutex_top_waiter(lock);
  620. plist_add(&next->pi_list_entry, &owner->pi_waiters);
  621. }
  622. __rt_mutex_adjust_prio(owner);
  623. /* Store the lock on which owner is blocked or NULL */
  624. next_lock = task_blocked_on_lock(owner);
  625. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  626. }
  627. WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
  628. if (!next_lock)
  629. return;
  630. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  631. get_task_struct(owner);
  632. raw_spin_unlock(&lock->wait_lock);
  633. rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
  634. raw_spin_lock(&lock->wait_lock);
  635. }
  636. /*
  637. * Recheck the pi chain, in case we got a priority setting
  638. *
  639. * Called from sched_setscheduler
  640. */
  641. void rt_mutex_adjust_pi(struct task_struct *task)
  642. {
  643. struct rt_mutex_waiter *waiter;
  644. struct rt_mutex *next_lock;
  645. unsigned long flags;
  646. raw_spin_lock_irqsave(&task->pi_lock, flags);
  647. waiter = task->pi_blocked_on;
  648. if (!waiter || waiter->list_entry.prio == task->prio) {
  649. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  650. return;
  651. }
  652. next_lock = waiter->lock;
  653. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  654. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  655. get_task_struct(task);
  656. rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
  657. }
  658. /**
  659. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  660. * @lock: the rt_mutex to take
  661. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  662. * or TASK_UNINTERRUPTIBLE)
  663. * @timeout: the pre-initialized and started timer, or NULL for none
  664. * @waiter: the pre-initialized rt_mutex_waiter
  665. *
  666. * lock->wait_lock must be held by the caller.
  667. */
  668. static int __sched
  669. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  670. struct hrtimer_sleeper *timeout,
  671. struct rt_mutex_waiter *waiter)
  672. {
  673. int ret = 0;
  674. for (;;) {
  675. /* Try to acquire the lock: */
  676. if (try_to_take_rt_mutex(lock, current, waiter))
  677. break;
  678. /*
  679. * TASK_INTERRUPTIBLE checks for signals and
  680. * timeout. Ignored otherwise.
  681. */
  682. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  683. /* Signal pending? */
  684. if (signal_pending(current))
  685. ret = -EINTR;
  686. if (timeout && !timeout->task)
  687. ret = -ETIMEDOUT;
  688. if (ret)
  689. break;
  690. }
  691. raw_spin_unlock(&lock->wait_lock);
  692. debug_rt_mutex_print_deadlock(waiter);
  693. schedule_rt_mutex(lock);
  694. raw_spin_lock(&lock->wait_lock);
  695. set_current_state(state);
  696. }
  697. return ret;
  698. }
  699. static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  700. struct rt_mutex_waiter *w)
  701. {
  702. /*
  703. * If the result is not -EDEADLOCK or the caller requested
  704. * deadlock detection, nothing to do here.
  705. */
  706. if (res != -EDEADLOCK || detect_deadlock)
  707. return;
  708. /*
  709. * Yell lowdly and stop the task right here.
  710. */
  711. rt_mutex_print_deadlock(w);
  712. while (1) {
  713. set_current_state(TASK_INTERRUPTIBLE);
  714. schedule();
  715. }
  716. }
  717. /*
  718. * Slow path lock function:
  719. */
  720. static int __sched
  721. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  722. struct hrtimer_sleeper *timeout,
  723. int detect_deadlock)
  724. {
  725. struct rt_mutex_waiter waiter;
  726. int ret = 0;
  727. debug_rt_mutex_init_waiter(&waiter);
  728. raw_spin_lock(&lock->wait_lock);
  729. /* Try to acquire the lock again: */
  730. if (try_to_take_rt_mutex(lock, current, NULL)) {
  731. raw_spin_unlock(&lock->wait_lock);
  732. return 0;
  733. }
  734. set_current_state(state);
  735. /* Setup the timer, when timeout != NULL */
  736. if (unlikely(timeout)) {
  737. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  738. if (!hrtimer_active(&timeout->timer))
  739. timeout->task = NULL;
  740. }
  741. ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
  742. if (likely(!ret))
  743. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  744. set_current_state(TASK_RUNNING);
  745. if (unlikely(ret)) {
  746. remove_waiter(lock, &waiter);
  747. rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
  748. }
  749. /*
  750. * try_to_take_rt_mutex() sets the waiter bit
  751. * unconditionally. We might have to fix that up.
  752. */
  753. fixup_rt_mutex_waiters(lock);
  754. raw_spin_unlock(&lock->wait_lock);
  755. /* Remove pending timer: */
  756. if (unlikely(timeout))
  757. hrtimer_cancel(&timeout->timer);
  758. debug_rt_mutex_free_waiter(&waiter);
  759. return ret;
  760. }
  761. /*
  762. * Slow path try-lock function:
  763. */
  764. static inline int
  765. rt_mutex_slowtrylock(struct rt_mutex *lock)
  766. {
  767. int ret = 0;
  768. raw_spin_lock(&lock->wait_lock);
  769. if (likely(rt_mutex_owner(lock) != current)) {
  770. ret = try_to_take_rt_mutex(lock, current, NULL);
  771. /*
  772. * try_to_take_rt_mutex() sets the lock waiters
  773. * bit unconditionally. Clean this up.
  774. */
  775. fixup_rt_mutex_waiters(lock);
  776. }
  777. raw_spin_unlock(&lock->wait_lock);
  778. return ret;
  779. }
  780. /*
  781. * Slow path to release a rt-mutex:
  782. */
  783. static void __sched
  784. rt_mutex_slowunlock(struct rt_mutex *lock)
  785. {
  786. raw_spin_lock(&lock->wait_lock);
  787. debug_rt_mutex_unlock(lock);
  788. rt_mutex_deadlock_account_unlock(current);
  789. /*
  790. * We must be careful here if the fast path is enabled. If we
  791. * have no waiters queued we cannot set owner to NULL here
  792. * because of:
  793. *
  794. * foo->lock->owner = NULL;
  795. * rtmutex_lock(foo->lock); <- fast path
  796. * free = atomic_dec_and_test(foo->refcnt);
  797. * rtmutex_unlock(foo->lock); <- fast path
  798. * if (free)
  799. * kfree(foo);
  800. * raw_spin_unlock(foo->lock->wait_lock);
  801. *
  802. * So for the fastpath enabled kernel:
  803. *
  804. * Nothing can set the waiters bit as long as we hold
  805. * lock->wait_lock. So we do the following sequence:
  806. *
  807. * owner = rt_mutex_owner(lock);
  808. * clear_rt_mutex_waiters(lock);
  809. * raw_spin_unlock(&lock->wait_lock);
  810. * if (cmpxchg(&lock->owner, owner, 0) == owner)
  811. * return;
  812. * goto retry;
  813. *
  814. * The fastpath disabled variant is simple as all access to
  815. * lock->owner is serialized by lock->wait_lock:
  816. *
  817. * lock->owner = NULL;
  818. * raw_spin_unlock(&lock->wait_lock);
  819. */
  820. while (!rt_mutex_has_waiters(lock)) {
  821. /* Drops lock->wait_lock ! */
  822. if (unlock_rt_mutex_safe(lock) == true)
  823. return;
  824. /* Relock the rtmutex and try again */
  825. raw_spin_lock(&lock->wait_lock);
  826. }
  827. /*
  828. * The wakeup next waiter path does not suffer from the above
  829. * race. See the comments there.
  830. */
  831. wakeup_next_waiter(lock);
  832. raw_spin_unlock(&lock->wait_lock);
  833. /* Undo pi boosting if necessary: */
  834. rt_mutex_adjust_prio(current);
  835. }
  836. /*
  837. * debug aware fast / slowpath lock,trylock,unlock
  838. *
  839. * The atomic acquire/release ops are compiled away, when either the
  840. * architecture does not support cmpxchg or when debugging is enabled.
  841. */
  842. static inline int
  843. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  844. int detect_deadlock,
  845. int (*slowfn)(struct rt_mutex *lock, int state,
  846. struct hrtimer_sleeper *timeout,
  847. int detect_deadlock))
  848. {
  849. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  850. rt_mutex_deadlock_account_lock(lock, current);
  851. return 0;
  852. } else
  853. return slowfn(lock, state, NULL, detect_deadlock);
  854. }
  855. static inline int
  856. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  857. struct hrtimer_sleeper *timeout, int detect_deadlock,
  858. int (*slowfn)(struct rt_mutex *lock, int state,
  859. struct hrtimer_sleeper *timeout,
  860. int detect_deadlock))
  861. {
  862. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  863. rt_mutex_deadlock_account_lock(lock, current);
  864. return 0;
  865. } else
  866. return slowfn(lock, state, timeout, detect_deadlock);
  867. }
  868. static inline int
  869. rt_mutex_fasttrylock(struct rt_mutex *lock,
  870. int (*slowfn)(struct rt_mutex *lock))
  871. {
  872. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  873. rt_mutex_deadlock_account_lock(lock, current);
  874. return 1;
  875. }
  876. return slowfn(lock);
  877. }
  878. static inline void
  879. rt_mutex_fastunlock(struct rt_mutex *lock,
  880. void (*slowfn)(struct rt_mutex *lock))
  881. {
  882. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  883. rt_mutex_deadlock_account_unlock(current);
  884. else
  885. slowfn(lock);
  886. }
  887. /**
  888. * rt_mutex_lock - lock a rt_mutex
  889. *
  890. * @lock: the rt_mutex to be locked
  891. */
  892. void __sched rt_mutex_lock(struct rt_mutex *lock)
  893. {
  894. might_sleep();
  895. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
  896. }
  897. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  898. /**
  899. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  900. *
  901. * @lock: the rt_mutex to be locked
  902. * @detect_deadlock: deadlock detection on/off
  903. *
  904. * Returns:
  905. * 0 on success
  906. * -EINTR when interrupted by a signal
  907. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  908. */
  909. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
  910. int detect_deadlock)
  911. {
  912. might_sleep();
  913. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
  914. detect_deadlock, rt_mutex_slowlock);
  915. }
  916. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  917. /**
  918. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  919. * the timeout structure is provided
  920. * by the caller
  921. *
  922. * @lock: the rt_mutex to be locked
  923. * @timeout: timeout structure or NULL (no timeout)
  924. * @detect_deadlock: deadlock detection on/off
  925. *
  926. * Returns:
  927. * 0 on success
  928. * -EINTR when interrupted by a signal
  929. * -ETIMEDOUT when the timeout expired
  930. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  931. */
  932. int
  933. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
  934. int detect_deadlock)
  935. {
  936. might_sleep();
  937. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  938. detect_deadlock, rt_mutex_slowlock);
  939. }
  940. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  941. /**
  942. * rt_mutex_trylock - try to lock a rt_mutex
  943. *
  944. * @lock: the rt_mutex to be locked
  945. *
  946. * Returns 1 on success and 0 on contention
  947. */
  948. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  949. {
  950. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  951. }
  952. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  953. /**
  954. * rt_mutex_unlock - unlock a rt_mutex
  955. *
  956. * @lock: the rt_mutex to be unlocked
  957. */
  958. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  959. {
  960. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  961. }
  962. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  963. /**
  964. * rt_mutex_destroy - mark a mutex unusable
  965. * @lock: the mutex to be destroyed
  966. *
  967. * This function marks the mutex uninitialized, and any subsequent
  968. * use of the mutex is forbidden. The mutex must not be locked when
  969. * this function is called.
  970. */
  971. void rt_mutex_destroy(struct rt_mutex *lock)
  972. {
  973. WARN_ON(rt_mutex_is_locked(lock));
  974. #ifdef CONFIG_DEBUG_RT_MUTEXES
  975. lock->magic = NULL;
  976. #endif
  977. }
  978. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  979. /**
  980. * __rt_mutex_init - initialize the rt lock
  981. *
  982. * @lock: the rt lock to be initialized
  983. *
  984. * Initialize the rt lock to unlocked state.
  985. *
  986. * Initializing of a locked rt lock is not allowed
  987. */
  988. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  989. {
  990. lock->owner = NULL;
  991. raw_spin_lock_init(&lock->wait_lock);
  992. plist_head_init(&lock->wait_list);
  993. debug_rt_mutex_init(lock, name);
  994. }
  995. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  996. /**
  997. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  998. * proxy owner
  999. *
  1000. * @lock: the rt_mutex to be locked
  1001. * @proxy_owner:the task to set as owner
  1002. *
  1003. * No locking. Caller has to do serializing itself
  1004. * Special API call for PI-futex support
  1005. */
  1006. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  1007. struct task_struct *proxy_owner)
  1008. {
  1009. __rt_mutex_init(lock, NULL);
  1010. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  1011. rt_mutex_set_owner(lock, proxy_owner);
  1012. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  1013. }
  1014. /**
  1015. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  1016. *
  1017. * @lock: the rt_mutex to be locked
  1018. *
  1019. * No locking. Caller has to do serializing itself
  1020. * Special API call for PI-futex support
  1021. */
  1022. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  1023. struct task_struct *proxy_owner)
  1024. {
  1025. debug_rt_mutex_proxy_unlock(lock);
  1026. rt_mutex_set_owner(lock, NULL);
  1027. rt_mutex_deadlock_account_unlock(proxy_owner);
  1028. }
  1029. /**
  1030. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  1031. * @lock: the rt_mutex to take
  1032. * @waiter: the pre-initialized rt_mutex_waiter
  1033. * @task: the task to prepare
  1034. * @detect_deadlock: perform deadlock detection (1) or not (0)
  1035. *
  1036. * Returns:
  1037. * 0 - task blocked on lock
  1038. * 1 - acquired the lock for task, caller should wake it up
  1039. * <0 - error
  1040. *
  1041. * Special API call for FUTEX_REQUEUE_PI support.
  1042. */
  1043. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  1044. struct rt_mutex_waiter *waiter,
  1045. struct task_struct *task, int detect_deadlock)
  1046. {
  1047. int ret;
  1048. raw_spin_lock(&lock->wait_lock);
  1049. if (try_to_take_rt_mutex(lock, task, NULL)) {
  1050. raw_spin_unlock(&lock->wait_lock);
  1051. return 1;
  1052. }
  1053. /* We enforce deadlock detection for futexes */
  1054. ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
  1055. if (ret && !rt_mutex_owner(lock)) {
  1056. /*
  1057. * Reset the return value. We might have
  1058. * returned with -EDEADLK and the owner
  1059. * released the lock while we were walking the
  1060. * pi chain. Let the waiter sort it out.
  1061. */
  1062. ret = 0;
  1063. }
  1064. if (unlikely(ret))
  1065. remove_waiter(lock, waiter);
  1066. raw_spin_unlock(&lock->wait_lock);
  1067. debug_rt_mutex_print_deadlock(waiter);
  1068. return ret;
  1069. }
  1070. /**
  1071. * rt_mutex_next_owner - return the next owner of the lock
  1072. *
  1073. * @lock: the rt lock query
  1074. *
  1075. * Returns the next owner of the lock or NULL
  1076. *
  1077. * Caller has to serialize against other accessors to the lock
  1078. * itself.
  1079. *
  1080. * Special API call for PI-futex support
  1081. */
  1082. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  1083. {
  1084. if (!rt_mutex_has_waiters(lock))
  1085. return NULL;
  1086. return rt_mutex_top_waiter(lock)->task;
  1087. }
  1088. /**
  1089. * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  1090. * @lock: the rt_mutex we were woken on
  1091. * @to: the timeout, null if none. hrtimer should already have
  1092. * been started.
  1093. * @waiter: the pre-initialized rt_mutex_waiter
  1094. * @detect_deadlock: perform deadlock detection (1) or not (0)
  1095. *
  1096. * Complete the lock acquisition started our behalf by another thread.
  1097. *
  1098. * Returns:
  1099. * 0 - success
  1100. * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
  1101. *
  1102. * Special API call for PI-futex requeue support
  1103. */
  1104. int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  1105. struct hrtimer_sleeper *to,
  1106. struct rt_mutex_waiter *waiter,
  1107. int detect_deadlock)
  1108. {
  1109. int ret;
  1110. raw_spin_lock(&lock->wait_lock);
  1111. set_current_state(TASK_INTERRUPTIBLE);
  1112. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  1113. set_current_state(TASK_RUNNING);
  1114. if (unlikely(ret))
  1115. remove_waiter(lock, waiter);
  1116. /*
  1117. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1118. * have to fix that up.
  1119. */
  1120. fixup_rt_mutex_waiters(lock);
  1121. raw_spin_unlock(&lock->wait_lock);
  1122. return ret;
  1123. }