rtmutex.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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 if the
  469. * callsite called task_blocked_on_lock(), otherwise NULL
  470. */
  471. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  472. struct rt_mutex_waiter *waiter)
  473. {
  474. unsigned long flags;
  475. /*
  476. * Before testing whether we can acquire @lock, we set the
  477. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  478. * other tasks which try to modify @lock into the slow path
  479. * and they serialize on @lock->wait_lock.
  480. *
  481. * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
  482. * as explained at the top of this file if and only if:
  483. *
  484. * - There is a lock owner. The caller must fixup the
  485. * transient state if it does a trylock or leaves the lock
  486. * function due to a signal or timeout.
  487. *
  488. * - @task acquires the lock and there are no other
  489. * waiters. This is undone in rt_mutex_set_owner(@task) at
  490. * the end of this function.
  491. */
  492. mark_rt_mutex_waiters(lock);
  493. /*
  494. * If @lock has an owner, give up.
  495. */
  496. if (rt_mutex_owner(lock))
  497. return 0;
  498. /*
  499. * If @waiter != NULL, @task has already enqueued the waiter
  500. * into @lock waiter list. If @waiter == NULL then this is a
  501. * trylock attempt.
  502. */
  503. if (waiter) {
  504. /*
  505. * If waiter is not the highest priority waiter of
  506. * @lock, give up.
  507. */
  508. if (waiter != rt_mutex_top_waiter(lock))
  509. return 0;
  510. /*
  511. * We can acquire the lock. Remove the waiter from the
  512. * lock waiters list.
  513. */
  514. rt_mutex_dequeue(lock, waiter);
  515. } else {
  516. /*
  517. * If the lock has waiters already we check whether @task is
  518. * eligible to take over the lock.
  519. *
  520. * If there are no other waiters, @task can acquire
  521. * the lock. @task->pi_blocked_on is NULL, so it does
  522. * not need to be dequeued.
  523. */
  524. if (rt_mutex_has_waiters(lock)) {
  525. /*
  526. * If @task->prio is greater than or equal to
  527. * the top waiter priority (kernel view),
  528. * @task lost.
  529. */
  530. if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  531. return 0;
  532. /*
  533. * The current top waiter stays enqueued. We
  534. * don't have to change anything in the lock
  535. * waiters order.
  536. */
  537. } else {
  538. /*
  539. * No waiters. Take the lock without the
  540. * pi_lock dance.@task->pi_blocked_on is NULL
  541. * and we have no waiters to enqueue in @task
  542. * pi waiters list.
  543. */
  544. goto takeit;
  545. }
  546. }
  547. /*
  548. * Clear @task->pi_blocked_on. Requires protection by
  549. * @task->pi_lock. Redundant operation for the @waiter == NULL
  550. * case, but conditionals are more expensive than a redundant
  551. * store.
  552. */
  553. raw_spin_lock_irqsave(&task->pi_lock, flags);
  554. task->pi_blocked_on = NULL;
  555. /*
  556. * Finish the lock acquisition. @task is the new owner. If
  557. * other waiters exist we have to insert the highest priority
  558. * waiter into @task->pi_waiters list.
  559. */
  560. if (rt_mutex_has_waiters(lock))
  561. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  562. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  563. takeit:
  564. /* We got the lock. */
  565. debug_rt_mutex_lock(lock);
  566. /*
  567. * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
  568. * are still waiters or clears it.
  569. */
  570. rt_mutex_set_owner(lock, task);
  571. rt_mutex_deadlock_account_lock(lock, task);
  572. return 1;
  573. }
  574. /*
  575. * Task blocks on lock.
  576. *
  577. * Prepare waiter and propagate pi chain
  578. *
  579. * This must be called with lock->wait_lock held.
  580. */
  581. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  582. struct rt_mutex_waiter *waiter,
  583. struct task_struct *task,
  584. int detect_deadlock)
  585. {
  586. struct task_struct *owner = rt_mutex_owner(lock);
  587. struct rt_mutex_waiter *top_waiter = waiter;
  588. struct rt_mutex *next_lock;
  589. int chain_walk = 0, res;
  590. unsigned long flags;
  591. /*
  592. * Early deadlock detection. We really don't want the task to
  593. * enqueue on itself just to untangle the mess later. It's not
  594. * only an optimization. We drop the locks, so another waiter
  595. * can come in before the chain walk detects the deadlock. So
  596. * the other will detect the deadlock and return -EDEADLOCK,
  597. * which is wrong, as the other waiter is not in a deadlock
  598. * situation.
  599. */
  600. if (owner == task)
  601. return -EDEADLK;
  602. raw_spin_lock_irqsave(&task->pi_lock, flags);
  603. __rt_mutex_adjust_prio(task);
  604. waiter->task = task;
  605. waiter->lock = lock;
  606. waiter->prio = task->prio;
  607. /* Get the top priority waiter on the lock */
  608. if (rt_mutex_has_waiters(lock))
  609. top_waiter = rt_mutex_top_waiter(lock);
  610. rt_mutex_enqueue(lock, waiter);
  611. task->pi_blocked_on = waiter;
  612. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  613. if (!owner)
  614. return 0;
  615. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  616. if (waiter == rt_mutex_top_waiter(lock)) {
  617. rt_mutex_dequeue_pi(owner, top_waiter);
  618. rt_mutex_enqueue_pi(owner, waiter);
  619. __rt_mutex_adjust_prio(owner);
  620. if (owner->pi_blocked_on)
  621. chain_walk = 1;
  622. } else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
  623. chain_walk = 1;
  624. }
  625. /* Store the lock on which owner is blocked or NULL */
  626. next_lock = task_blocked_on_lock(owner);
  627. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  628. /*
  629. * Even if full deadlock detection is on, if the owner is not
  630. * blocked itself, we can avoid finding this out in the chain
  631. * walk.
  632. */
  633. if (!chain_walk || !next_lock)
  634. return 0;
  635. /*
  636. * The owner can't disappear while holding a lock,
  637. * so the owner struct is protected by wait_lock.
  638. * Gets dropped in rt_mutex_adjust_prio_chain()!
  639. */
  640. get_task_struct(owner);
  641. raw_spin_unlock(&lock->wait_lock);
  642. res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
  643. next_lock, waiter, task);
  644. raw_spin_lock(&lock->wait_lock);
  645. return res;
  646. }
  647. /*
  648. * Wake up the next waiter on the lock.
  649. *
  650. * Remove the top waiter from the current tasks pi waiter list and
  651. * wake it up.
  652. *
  653. * Called with lock->wait_lock held.
  654. */
  655. static void wakeup_next_waiter(struct rt_mutex *lock)
  656. {
  657. struct rt_mutex_waiter *waiter;
  658. unsigned long flags;
  659. raw_spin_lock_irqsave(&current->pi_lock, flags);
  660. waiter = rt_mutex_top_waiter(lock);
  661. /*
  662. * Remove it from current->pi_waiters. We do not adjust a
  663. * possible priority boost right now. We execute wakeup in the
  664. * boosted mode and go back to normal after releasing
  665. * lock->wait_lock.
  666. */
  667. rt_mutex_dequeue_pi(current, waiter);
  668. /*
  669. * As we are waking up the top waiter, and the waiter stays
  670. * queued on the lock until it gets the lock, this lock
  671. * obviously has waiters. Just set the bit here and this has
  672. * the added benefit of forcing all new tasks into the
  673. * slow path making sure no task of lower priority than
  674. * the top waiter can steal this lock.
  675. */
  676. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  677. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  678. /*
  679. * It's safe to dereference waiter as it cannot go away as
  680. * long as we hold lock->wait_lock. The waiter task needs to
  681. * acquire it in order to dequeue the waiter.
  682. */
  683. wake_up_process(waiter->task);
  684. }
  685. /*
  686. * Remove a waiter from a lock and give up
  687. *
  688. * Must be called with lock->wait_lock held and
  689. * have just failed to try_to_take_rt_mutex().
  690. */
  691. static void remove_waiter(struct rt_mutex *lock,
  692. struct rt_mutex_waiter *waiter)
  693. {
  694. int first = (waiter == rt_mutex_top_waiter(lock));
  695. struct task_struct *owner = rt_mutex_owner(lock);
  696. struct rt_mutex *next_lock = NULL;
  697. unsigned long flags;
  698. raw_spin_lock_irqsave(&current->pi_lock, flags);
  699. rt_mutex_dequeue(lock, waiter);
  700. current->pi_blocked_on = NULL;
  701. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  702. if (!owner)
  703. return;
  704. if (first) {
  705. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  706. rt_mutex_dequeue_pi(owner, waiter);
  707. if (rt_mutex_has_waiters(lock)) {
  708. struct rt_mutex_waiter *next;
  709. next = rt_mutex_top_waiter(lock);
  710. rt_mutex_enqueue_pi(owner, next);
  711. }
  712. __rt_mutex_adjust_prio(owner);
  713. /* Store the lock on which owner is blocked or NULL */
  714. next_lock = task_blocked_on_lock(owner);
  715. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  716. }
  717. if (!next_lock)
  718. return;
  719. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  720. get_task_struct(owner);
  721. raw_spin_unlock(&lock->wait_lock);
  722. rt_mutex_adjust_prio_chain(owner, 0, lock, next_lock, NULL, current);
  723. raw_spin_lock(&lock->wait_lock);
  724. }
  725. /*
  726. * Recheck the pi chain, in case we got a priority setting
  727. *
  728. * Called from sched_setscheduler
  729. */
  730. void rt_mutex_adjust_pi(struct task_struct *task)
  731. {
  732. struct rt_mutex_waiter *waiter;
  733. struct rt_mutex *next_lock;
  734. unsigned long flags;
  735. raw_spin_lock_irqsave(&task->pi_lock, flags);
  736. waiter = task->pi_blocked_on;
  737. if (!waiter || (waiter->prio == task->prio &&
  738. !dl_prio(task->prio))) {
  739. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  740. return;
  741. }
  742. next_lock = waiter->lock;
  743. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  744. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  745. get_task_struct(task);
  746. rt_mutex_adjust_prio_chain(task, 0, NULL, next_lock, NULL, task);
  747. }
  748. /**
  749. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  750. * @lock: the rt_mutex to take
  751. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  752. * or TASK_UNINTERRUPTIBLE)
  753. * @timeout: the pre-initialized and started timer, or NULL for none
  754. * @waiter: the pre-initialized rt_mutex_waiter
  755. *
  756. * lock->wait_lock must be held by the caller.
  757. */
  758. static int __sched
  759. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  760. struct hrtimer_sleeper *timeout,
  761. struct rt_mutex_waiter *waiter)
  762. {
  763. int ret = 0;
  764. for (;;) {
  765. /* Try to acquire the lock: */
  766. if (try_to_take_rt_mutex(lock, current, waiter))
  767. break;
  768. /*
  769. * TASK_INTERRUPTIBLE checks for signals and
  770. * timeout. Ignored otherwise.
  771. */
  772. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  773. /* Signal pending? */
  774. if (signal_pending(current))
  775. ret = -EINTR;
  776. if (timeout && !timeout->task)
  777. ret = -ETIMEDOUT;
  778. if (ret)
  779. break;
  780. }
  781. raw_spin_unlock(&lock->wait_lock);
  782. debug_rt_mutex_print_deadlock(waiter);
  783. schedule_rt_mutex(lock);
  784. raw_spin_lock(&lock->wait_lock);
  785. set_current_state(state);
  786. }
  787. return ret;
  788. }
  789. static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  790. struct rt_mutex_waiter *w)
  791. {
  792. /*
  793. * If the result is not -EDEADLOCK or the caller requested
  794. * deadlock detection, nothing to do here.
  795. */
  796. if (res != -EDEADLOCK || detect_deadlock)
  797. return;
  798. /*
  799. * Yell lowdly and stop the task right here.
  800. */
  801. rt_mutex_print_deadlock(w);
  802. while (1) {
  803. set_current_state(TASK_INTERRUPTIBLE);
  804. schedule();
  805. }
  806. }
  807. /*
  808. * Slow path lock function:
  809. */
  810. static int __sched
  811. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  812. struct hrtimer_sleeper *timeout,
  813. int detect_deadlock)
  814. {
  815. struct rt_mutex_waiter waiter;
  816. int ret = 0;
  817. debug_rt_mutex_init_waiter(&waiter);
  818. RB_CLEAR_NODE(&waiter.pi_tree_entry);
  819. RB_CLEAR_NODE(&waiter.tree_entry);
  820. raw_spin_lock(&lock->wait_lock);
  821. /* Try to acquire the lock again: */
  822. if (try_to_take_rt_mutex(lock, current, NULL)) {
  823. raw_spin_unlock(&lock->wait_lock);
  824. return 0;
  825. }
  826. set_current_state(state);
  827. /* Setup the timer, when timeout != NULL */
  828. if (unlikely(timeout)) {
  829. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  830. if (!hrtimer_active(&timeout->timer))
  831. timeout->task = NULL;
  832. }
  833. ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
  834. if (likely(!ret))
  835. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  836. set_current_state(TASK_RUNNING);
  837. if (unlikely(ret)) {
  838. remove_waiter(lock, &waiter);
  839. rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
  840. }
  841. /*
  842. * try_to_take_rt_mutex() sets the waiter bit
  843. * unconditionally. We might have to fix that up.
  844. */
  845. fixup_rt_mutex_waiters(lock);
  846. raw_spin_unlock(&lock->wait_lock);
  847. /* Remove pending timer: */
  848. if (unlikely(timeout))
  849. hrtimer_cancel(&timeout->timer);
  850. debug_rt_mutex_free_waiter(&waiter);
  851. return ret;
  852. }
  853. /*
  854. * Slow path try-lock function:
  855. */
  856. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  857. {
  858. int ret;
  859. /*
  860. * If the lock already has an owner we fail to get the lock.
  861. * This can be done without taking the @lock->wait_lock as
  862. * it is only being read, and this is a trylock anyway.
  863. */
  864. if (rt_mutex_owner(lock))
  865. return 0;
  866. /*
  867. * The mutex has currently no owner. Lock the wait lock and
  868. * try to acquire the lock.
  869. */
  870. raw_spin_lock(&lock->wait_lock);
  871. ret = try_to_take_rt_mutex(lock, current, NULL);
  872. /*
  873. * try_to_take_rt_mutex() sets the lock waiters bit
  874. * unconditionally. Clean this up.
  875. */
  876. fixup_rt_mutex_waiters(lock);
  877. raw_spin_unlock(&lock->wait_lock);
  878. return ret;
  879. }
  880. /*
  881. * Slow path to release a rt-mutex:
  882. */
  883. static void __sched
  884. rt_mutex_slowunlock(struct rt_mutex *lock)
  885. {
  886. raw_spin_lock(&lock->wait_lock);
  887. debug_rt_mutex_unlock(lock);
  888. rt_mutex_deadlock_account_unlock(current);
  889. /*
  890. * We must be careful here if the fast path is enabled. If we
  891. * have no waiters queued we cannot set owner to NULL here
  892. * because of:
  893. *
  894. * foo->lock->owner = NULL;
  895. * rtmutex_lock(foo->lock); <- fast path
  896. * free = atomic_dec_and_test(foo->refcnt);
  897. * rtmutex_unlock(foo->lock); <- fast path
  898. * if (free)
  899. * kfree(foo);
  900. * raw_spin_unlock(foo->lock->wait_lock);
  901. *
  902. * So for the fastpath enabled kernel:
  903. *
  904. * Nothing can set the waiters bit as long as we hold
  905. * lock->wait_lock. So we do the following sequence:
  906. *
  907. * owner = rt_mutex_owner(lock);
  908. * clear_rt_mutex_waiters(lock);
  909. * raw_spin_unlock(&lock->wait_lock);
  910. * if (cmpxchg(&lock->owner, owner, 0) == owner)
  911. * return;
  912. * goto retry;
  913. *
  914. * The fastpath disabled variant is simple as all access to
  915. * lock->owner is serialized by lock->wait_lock:
  916. *
  917. * lock->owner = NULL;
  918. * raw_spin_unlock(&lock->wait_lock);
  919. */
  920. while (!rt_mutex_has_waiters(lock)) {
  921. /* Drops lock->wait_lock ! */
  922. if (unlock_rt_mutex_safe(lock) == true)
  923. return;
  924. /* Relock the rtmutex and try again */
  925. raw_spin_lock(&lock->wait_lock);
  926. }
  927. /*
  928. * The wakeup next waiter path does not suffer from the above
  929. * race. See the comments there.
  930. */
  931. wakeup_next_waiter(lock);
  932. raw_spin_unlock(&lock->wait_lock);
  933. /* Undo pi boosting if necessary: */
  934. rt_mutex_adjust_prio(current);
  935. }
  936. /*
  937. * debug aware fast / slowpath lock,trylock,unlock
  938. *
  939. * The atomic acquire/release ops are compiled away, when either the
  940. * architecture does not support cmpxchg or when debugging is enabled.
  941. */
  942. static inline int
  943. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  944. int detect_deadlock,
  945. int (*slowfn)(struct rt_mutex *lock, int state,
  946. struct hrtimer_sleeper *timeout,
  947. int detect_deadlock))
  948. {
  949. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  950. rt_mutex_deadlock_account_lock(lock, current);
  951. return 0;
  952. } else
  953. return slowfn(lock, state, NULL, detect_deadlock);
  954. }
  955. static inline int
  956. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  957. struct hrtimer_sleeper *timeout, int detect_deadlock,
  958. int (*slowfn)(struct rt_mutex *lock, int state,
  959. struct hrtimer_sleeper *timeout,
  960. int detect_deadlock))
  961. {
  962. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  963. rt_mutex_deadlock_account_lock(lock, current);
  964. return 0;
  965. } else
  966. return slowfn(lock, state, timeout, detect_deadlock);
  967. }
  968. static inline int
  969. rt_mutex_fasttrylock(struct rt_mutex *lock,
  970. int (*slowfn)(struct rt_mutex *lock))
  971. {
  972. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  973. rt_mutex_deadlock_account_lock(lock, current);
  974. return 1;
  975. }
  976. return slowfn(lock);
  977. }
  978. static inline void
  979. rt_mutex_fastunlock(struct rt_mutex *lock,
  980. void (*slowfn)(struct rt_mutex *lock))
  981. {
  982. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  983. rt_mutex_deadlock_account_unlock(current);
  984. else
  985. slowfn(lock);
  986. }
  987. /**
  988. * rt_mutex_lock - lock a rt_mutex
  989. *
  990. * @lock: the rt_mutex to be locked
  991. */
  992. void __sched rt_mutex_lock(struct rt_mutex *lock)
  993. {
  994. might_sleep();
  995. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
  996. }
  997. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  998. /**
  999. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  1000. *
  1001. * @lock: the rt_mutex to be locked
  1002. * @detect_deadlock: deadlock detection on/off
  1003. *
  1004. * Returns:
  1005. * 0 on success
  1006. * -EINTR when interrupted by a signal
  1007. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  1008. */
  1009. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
  1010. int detect_deadlock)
  1011. {
  1012. might_sleep();
  1013. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
  1014. detect_deadlock, rt_mutex_slowlock);
  1015. }
  1016. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  1017. /**
  1018. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  1019. * the timeout structure is provided
  1020. * by the caller
  1021. *
  1022. * @lock: the rt_mutex to be locked
  1023. * @timeout: timeout structure or NULL (no timeout)
  1024. * @detect_deadlock: deadlock detection on/off
  1025. *
  1026. * Returns:
  1027. * 0 on success
  1028. * -EINTR when interrupted by a signal
  1029. * -ETIMEDOUT when the timeout expired
  1030. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  1031. */
  1032. int
  1033. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
  1034. int detect_deadlock)
  1035. {
  1036. might_sleep();
  1037. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1038. detect_deadlock, rt_mutex_slowlock);
  1039. }
  1040. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  1041. /**
  1042. * rt_mutex_trylock - try to lock a rt_mutex
  1043. *
  1044. * @lock: the rt_mutex to be locked
  1045. *
  1046. * Returns 1 on success and 0 on contention
  1047. */
  1048. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  1049. {
  1050. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  1051. }
  1052. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  1053. /**
  1054. * rt_mutex_unlock - unlock a rt_mutex
  1055. *
  1056. * @lock: the rt_mutex to be unlocked
  1057. */
  1058. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  1059. {
  1060. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  1061. }
  1062. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  1063. /**
  1064. * rt_mutex_destroy - mark a mutex unusable
  1065. * @lock: the mutex to be destroyed
  1066. *
  1067. * This function marks the mutex uninitialized, and any subsequent
  1068. * use of the mutex is forbidden. The mutex must not be locked when
  1069. * this function is called.
  1070. */
  1071. void rt_mutex_destroy(struct rt_mutex *lock)
  1072. {
  1073. WARN_ON(rt_mutex_is_locked(lock));
  1074. #ifdef CONFIG_DEBUG_RT_MUTEXES
  1075. lock->magic = NULL;
  1076. #endif
  1077. }
  1078. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  1079. /**
  1080. * __rt_mutex_init - initialize the rt lock
  1081. *
  1082. * @lock: the rt lock to be initialized
  1083. *
  1084. * Initialize the rt lock to unlocked state.
  1085. *
  1086. * Initializing of a locked rt lock is not allowed
  1087. */
  1088. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  1089. {
  1090. lock->owner = NULL;
  1091. raw_spin_lock_init(&lock->wait_lock);
  1092. lock->waiters = RB_ROOT;
  1093. lock->waiters_leftmost = NULL;
  1094. debug_rt_mutex_init(lock, name);
  1095. }
  1096. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  1097. /**
  1098. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  1099. * proxy owner
  1100. *
  1101. * @lock: the rt_mutex to be locked
  1102. * @proxy_owner:the task to set as owner
  1103. *
  1104. * No locking. Caller has to do serializing itself
  1105. * Special API call for PI-futex support
  1106. */
  1107. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  1108. struct task_struct *proxy_owner)
  1109. {
  1110. __rt_mutex_init(lock, NULL);
  1111. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  1112. rt_mutex_set_owner(lock, proxy_owner);
  1113. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  1114. }
  1115. /**
  1116. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  1117. *
  1118. * @lock: the rt_mutex to be locked
  1119. *
  1120. * No locking. Caller has to do serializing itself
  1121. * Special API call for PI-futex support
  1122. */
  1123. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  1124. struct task_struct *proxy_owner)
  1125. {
  1126. debug_rt_mutex_proxy_unlock(lock);
  1127. rt_mutex_set_owner(lock, NULL);
  1128. rt_mutex_deadlock_account_unlock(proxy_owner);
  1129. }
  1130. /**
  1131. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  1132. * @lock: the rt_mutex to take
  1133. * @waiter: the pre-initialized rt_mutex_waiter
  1134. * @task: the task to prepare
  1135. * @detect_deadlock: perform deadlock detection (1) or not (0)
  1136. *
  1137. * Returns:
  1138. * 0 - task blocked on lock
  1139. * 1 - acquired the lock for task, caller should wake it up
  1140. * <0 - error
  1141. *
  1142. * Special API call for FUTEX_REQUEUE_PI support.
  1143. */
  1144. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  1145. struct rt_mutex_waiter *waiter,
  1146. struct task_struct *task, int detect_deadlock)
  1147. {
  1148. int ret;
  1149. raw_spin_lock(&lock->wait_lock);
  1150. if (try_to_take_rt_mutex(lock, task, NULL)) {
  1151. raw_spin_unlock(&lock->wait_lock);
  1152. return 1;
  1153. }
  1154. /* We enforce deadlock detection for futexes */
  1155. ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
  1156. if (ret && !rt_mutex_owner(lock)) {
  1157. /*
  1158. * Reset the return value. We might have
  1159. * returned with -EDEADLK and the owner
  1160. * released the lock while we were walking the
  1161. * pi chain. Let the waiter sort it out.
  1162. */
  1163. ret = 0;
  1164. }
  1165. if (unlikely(ret))
  1166. remove_waiter(lock, waiter);
  1167. raw_spin_unlock(&lock->wait_lock);
  1168. debug_rt_mutex_print_deadlock(waiter);
  1169. return ret;
  1170. }
  1171. /**
  1172. * rt_mutex_next_owner - return the next owner of the lock
  1173. *
  1174. * @lock: the rt lock query
  1175. *
  1176. * Returns the next owner of the lock or NULL
  1177. *
  1178. * Caller has to serialize against other accessors to the lock
  1179. * itself.
  1180. *
  1181. * Special API call for PI-futex support
  1182. */
  1183. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  1184. {
  1185. if (!rt_mutex_has_waiters(lock))
  1186. return NULL;
  1187. return rt_mutex_top_waiter(lock)->task;
  1188. }
  1189. /**
  1190. * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  1191. * @lock: the rt_mutex we were woken on
  1192. * @to: the timeout, null if none. hrtimer should already have
  1193. * been started.
  1194. * @waiter: the pre-initialized rt_mutex_waiter
  1195. * @detect_deadlock: perform deadlock detection (1) or not (0)
  1196. *
  1197. * Complete the lock acquisition started our behalf by another thread.
  1198. *
  1199. * Returns:
  1200. * 0 - success
  1201. * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
  1202. *
  1203. * Special API call for PI-futex requeue support
  1204. */
  1205. int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  1206. struct hrtimer_sleeper *to,
  1207. struct rt_mutex_waiter *waiter,
  1208. int detect_deadlock)
  1209. {
  1210. int ret;
  1211. raw_spin_lock(&lock->wait_lock);
  1212. set_current_state(TASK_INTERRUPTIBLE);
  1213. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  1214. set_current_state(TASK_RUNNING);
  1215. if (unlikely(ret))
  1216. remove_waiter(lock, waiter);
  1217. /*
  1218. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1219. * have to fix that up.
  1220. */
  1221. fixup_rt_mutex_waiters(lock);
  1222. raw_spin_unlock(&lock->wait_lock);
  1223. return ret;
  1224. }