rtmutex.c 35 KB

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