rtmutex.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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/timer.h>
  18. #include "rtmutex_common.h"
  19. /*
  20. * lock->owner state tracking:
  21. *
  22. * lock->owner holds the task_struct pointer of the owner. Bit 0
  23. * is used to keep track of the "lock has waiters" state.
  24. *
  25. * owner bit0
  26. * NULL 0 lock is free (fast acquire possible)
  27. * NULL 1 lock is free and has waiters and the top waiter
  28. * is going to take the lock*
  29. * taskpointer 0 lock is held (fast release possible)
  30. * taskpointer 1 lock is held and has waiters**
  31. *
  32. * The fast atomic compare exchange based acquire and release is only
  33. * possible when bit 0 of lock->owner is 0.
  34. *
  35. * (*) It also can be a transitional state when grabbing the lock
  36. * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
  37. * we need to set the bit0 before looking at the lock, and the owner may be
  38. * NULL in this small time, hence this can be a transitional state.
  39. *
  40. * (**) There is a small time when bit 0 is set but there are no
  41. * waiters. This can happen when grabbing the lock in the slow path.
  42. * To prevent a cmpxchg of the owner releasing the lock, we need to
  43. * set this bit before looking at the lock.
  44. */
  45. static void
  46. rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
  47. {
  48. unsigned long val = (unsigned long)owner;
  49. if (rt_mutex_has_waiters(lock))
  50. val |= RT_MUTEX_HAS_WAITERS;
  51. lock->owner = (struct task_struct *)val;
  52. }
  53. static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
  54. {
  55. lock->owner = (struct task_struct *)
  56. ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
  57. }
  58. static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  59. {
  60. if (!rt_mutex_has_waiters(lock))
  61. clear_rt_mutex_waiters(lock);
  62. }
  63. /*
  64. * We can speed up the acquire/release, if the architecture
  65. * supports cmpxchg and if there's no debugging state to be set up
  66. */
  67. #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
  68. # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
  69. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  70. {
  71. unsigned long owner, *p = (unsigned long *) &lock->owner;
  72. do {
  73. owner = *p;
  74. } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
  75. }
  76. #else
  77. # define rt_mutex_cmpxchg(l,c,n) (0)
  78. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  79. {
  80. lock->owner = (struct task_struct *)
  81. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  82. }
  83. #endif
  84. /*
  85. * Calculate task priority from the waiter list priority
  86. *
  87. * Return task->normal_prio when the waiter list is empty or when
  88. * the waiter is not allowed to do priority boosting
  89. */
  90. int rt_mutex_getprio(struct task_struct *task)
  91. {
  92. if (likely(!task_has_pi_waiters(task)))
  93. return task->normal_prio;
  94. return min(task_top_pi_waiter(task)->pi_list_entry.prio,
  95. task->normal_prio);
  96. }
  97. /*
  98. * Adjust the priority of a task, after its pi_waiters got modified.
  99. *
  100. * This can be both boosting and unboosting. task->pi_lock must be held.
  101. */
  102. static void __rt_mutex_adjust_prio(struct task_struct *task)
  103. {
  104. int prio = rt_mutex_getprio(task);
  105. if (task->prio != prio)
  106. rt_mutex_setprio(task, prio);
  107. }
  108. /*
  109. * Adjust task priority (undo boosting). Called from the exit path of
  110. * rt_mutex_slowunlock() and rt_mutex_slowlock().
  111. *
  112. * (Note: We do this outside of the protection of lock->wait_lock to
  113. * allow the lock to be taken while or before we readjust the priority
  114. * of task. We do not use the spin_xx_mutex() variants here as we are
  115. * outside of the debug path.)
  116. */
  117. static void rt_mutex_adjust_prio(struct task_struct *task)
  118. {
  119. unsigned long flags;
  120. raw_spin_lock_irqsave(&task->pi_lock, flags);
  121. __rt_mutex_adjust_prio(task);
  122. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  123. }
  124. /*
  125. * Max number of times we'll walk the boosting chain:
  126. */
  127. int max_lock_depth = 1024;
  128. /*
  129. * Adjust the priority chain. Also used for deadlock detection.
  130. * Decreases task's usage by one - may thus free the task.
  131. * Returns 0 or -EDEADLK.
  132. */
  133. static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  134. int deadlock_detect,
  135. struct rt_mutex *orig_lock,
  136. struct rt_mutex_waiter *orig_waiter,
  137. struct task_struct *top_task)
  138. {
  139. struct rt_mutex *lock;
  140. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  141. int detect_deadlock, ret = 0, depth = 0;
  142. unsigned long flags;
  143. detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
  144. deadlock_detect);
  145. /*
  146. * The (de)boosting is a step by step approach with a lot of
  147. * pitfalls. We want this to be preemptible and we want hold a
  148. * maximum of two locks per step. So we have to check
  149. * carefully whether things change under us.
  150. */
  151. again:
  152. if (++depth > max_lock_depth) {
  153. static int prev_max;
  154. /*
  155. * Print this only once. If the admin changes the limit,
  156. * print a new message when reaching the limit again.
  157. */
  158. if (prev_max != max_lock_depth) {
  159. prev_max = max_lock_depth;
  160. printk(KERN_WARNING "Maximum lock depth %d reached "
  161. "task: %s (%d)\n", max_lock_depth,
  162. top_task->comm, task_pid_nr(top_task));
  163. }
  164. put_task_struct(task);
  165. return deadlock_detect ? -EDEADLK : 0;
  166. }
  167. retry:
  168. /*
  169. * Task can not go away as we did a get_task() before !
  170. */
  171. raw_spin_lock_irqsave(&task->pi_lock, flags);
  172. waiter = task->pi_blocked_on;
  173. /*
  174. * Check whether the end of the boosting chain has been
  175. * reached or the state of the chain has changed while we
  176. * dropped the locks.
  177. */
  178. if (!waiter)
  179. goto out_unlock_pi;
  180. /*
  181. * Check the orig_waiter state. After we dropped the locks,
  182. * the previous owner of the lock might have released the lock.
  183. */
  184. if (orig_waiter && !rt_mutex_owner(orig_lock))
  185. goto out_unlock_pi;
  186. /*
  187. * Drop out, when the task has no waiters. Note,
  188. * top_waiter can be NULL, when we are in the deboosting
  189. * mode!
  190. */
  191. if (top_waiter && (!task_has_pi_waiters(task) ||
  192. top_waiter != task_top_pi_waiter(task)))
  193. goto out_unlock_pi;
  194. /*
  195. * When deadlock detection is off then we check, if further
  196. * priority adjustment is necessary.
  197. */
  198. if (!detect_deadlock && waiter->list_entry.prio == task->prio)
  199. goto out_unlock_pi;
  200. lock = waiter->lock;
  201. if (!raw_spin_trylock(&lock->wait_lock)) {
  202. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  203. cpu_relax();
  204. goto retry;
  205. }
  206. /* Deadlock detection */
  207. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  208. debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
  209. raw_spin_unlock(&lock->wait_lock);
  210. ret = deadlock_detect ? -EDEADLK : 0;
  211. goto out_unlock_pi;
  212. }
  213. top_waiter = rt_mutex_top_waiter(lock);
  214. /* Requeue the waiter */
  215. plist_del(&waiter->list_entry, &lock->wait_list);
  216. waiter->list_entry.prio = task->prio;
  217. plist_add(&waiter->list_entry, &lock->wait_list);
  218. /* Release the task */
  219. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  220. if (!rt_mutex_owner(lock)) {
  221. /*
  222. * If the requeue above changed the top waiter, then we need
  223. * to wake the new top waiter up to try to get the lock.
  224. */
  225. if (top_waiter != rt_mutex_top_waiter(lock))
  226. wake_up_process(rt_mutex_top_waiter(lock)->task);
  227. raw_spin_unlock(&lock->wait_lock);
  228. goto out_put_task;
  229. }
  230. put_task_struct(task);
  231. /* Grab the next task */
  232. task = rt_mutex_owner(lock);
  233. get_task_struct(task);
  234. raw_spin_lock_irqsave(&task->pi_lock, flags);
  235. if (waiter == rt_mutex_top_waiter(lock)) {
  236. /* Boost the owner */
  237. plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
  238. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  239. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  240. __rt_mutex_adjust_prio(task);
  241. } else if (top_waiter == waiter) {
  242. /* Deboost the owner */
  243. plist_del(&waiter->pi_list_entry, &task->pi_waiters);
  244. waiter = rt_mutex_top_waiter(lock);
  245. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  246. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  247. __rt_mutex_adjust_prio(task);
  248. }
  249. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  250. top_waiter = rt_mutex_top_waiter(lock);
  251. raw_spin_unlock(&lock->wait_lock);
  252. if (!detect_deadlock && waiter != top_waiter)
  253. goto out_put_task;
  254. goto again;
  255. out_unlock_pi:
  256. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  257. out_put_task:
  258. put_task_struct(task);
  259. return ret;
  260. }
  261. /*
  262. * Try to take an rt-mutex
  263. *
  264. * Must be called with lock->wait_lock held.
  265. *
  266. * @lock: the lock to be acquired.
  267. * @task: the task which wants to acquire the lock
  268. * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
  269. */
  270. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  271. struct rt_mutex_waiter *waiter)
  272. {
  273. /*
  274. * We have to be careful here if the atomic speedups are
  275. * enabled, such that, when
  276. * - no other waiter is on the lock
  277. * - the lock has been released since we did the cmpxchg
  278. * the lock can be released or taken while we are doing the
  279. * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
  280. *
  281. * The atomic acquire/release aware variant of
  282. * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
  283. * the WAITERS bit, the atomic release / acquire can not
  284. * happen anymore and lock->wait_lock protects us from the
  285. * non-atomic case.
  286. *
  287. * Note, that this might set lock->owner =
  288. * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
  289. * any more. This is fixed up when we take the ownership.
  290. * This is the transitional state explained at the top of this file.
  291. */
  292. mark_rt_mutex_waiters(lock);
  293. if (rt_mutex_owner(lock))
  294. return 0;
  295. /*
  296. * It will get the lock because of one of these conditions:
  297. * 1) there is no waiter
  298. * 2) higher priority than waiters
  299. * 3) it is top waiter
  300. */
  301. if (rt_mutex_has_waiters(lock)) {
  302. if (task->prio >= rt_mutex_top_waiter(lock)->list_entry.prio) {
  303. if (!waiter || waiter != rt_mutex_top_waiter(lock))
  304. return 0;
  305. }
  306. }
  307. if (waiter || rt_mutex_has_waiters(lock)) {
  308. unsigned long flags;
  309. struct rt_mutex_waiter *top;
  310. raw_spin_lock_irqsave(&task->pi_lock, flags);
  311. /* remove the queued waiter. */
  312. if (waiter) {
  313. plist_del(&waiter->list_entry, &lock->wait_list);
  314. task->pi_blocked_on = NULL;
  315. }
  316. /*
  317. * We have to enqueue the top waiter(if it exists) into
  318. * task->pi_waiters list.
  319. */
  320. if (rt_mutex_has_waiters(lock)) {
  321. top = rt_mutex_top_waiter(lock);
  322. top->pi_list_entry.prio = top->list_entry.prio;
  323. plist_add(&top->pi_list_entry, &task->pi_waiters);
  324. }
  325. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  326. }
  327. /* We got the lock. */
  328. debug_rt_mutex_lock(lock);
  329. rt_mutex_set_owner(lock, task);
  330. rt_mutex_deadlock_account_lock(lock, task);
  331. return 1;
  332. }
  333. /*
  334. * Task blocks on lock.
  335. *
  336. * Prepare waiter and propagate pi chain
  337. *
  338. * This must be called with lock->wait_lock held.
  339. */
  340. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  341. struct rt_mutex_waiter *waiter,
  342. struct task_struct *task,
  343. int detect_deadlock)
  344. {
  345. struct task_struct *owner = rt_mutex_owner(lock);
  346. struct rt_mutex_waiter *top_waiter = waiter;
  347. unsigned long flags;
  348. int chain_walk = 0, res;
  349. raw_spin_lock_irqsave(&task->pi_lock, flags);
  350. __rt_mutex_adjust_prio(task);
  351. waiter->task = task;
  352. waiter->lock = lock;
  353. plist_node_init(&waiter->list_entry, task->prio);
  354. plist_node_init(&waiter->pi_list_entry, task->prio);
  355. /* Get the top priority waiter on the lock */
  356. if (rt_mutex_has_waiters(lock))
  357. top_waiter = rt_mutex_top_waiter(lock);
  358. plist_add(&waiter->list_entry, &lock->wait_list);
  359. task->pi_blocked_on = waiter;
  360. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  361. if (!owner)
  362. return 0;
  363. if (waiter == rt_mutex_top_waiter(lock)) {
  364. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  365. plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
  366. plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
  367. __rt_mutex_adjust_prio(owner);
  368. if (owner->pi_blocked_on)
  369. chain_walk = 1;
  370. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  371. }
  372. else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock))
  373. chain_walk = 1;
  374. if (!chain_walk)
  375. return 0;
  376. /*
  377. * The owner can't disappear while holding a lock,
  378. * so the owner struct is protected by wait_lock.
  379. * Gets dropped in rt_mutex_adjust_prio_chain()!
  380. */
  381. get_task_struct(owner);
  382. raw_spin_unlock(&lock->wait_lock);
  383. res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
  384. task);
  385. raw_spin_lock(&lock->wait_lock);
  386. return res;
  387. }
  388. /*
  389. * Wake up the next waiter on the lock.
  390. *
  391. * Remove the top waiter from the current tasks waiter list and wake it up.
  392. *
  393. * Called with lock->wait_lock held.
  394. */
  395. static void wakeup_next_waiter(struct rt_mutex *lock)
  396. {
  397. struct rt_mutex_waiter *waiter;
  398. unsigned long flags;
  399. raw_spin_lock_irqsave(&current->pi_lock, flags);
  400. waiter = rt_mutex_top_waiter(lock);
  401. /*
  402. * Remove it from current->pi_waiters. We do not adjust a
  403. * possible priority boost right now. We execute wakeup in the
  404. * boosted mode and go back to normal after releasing
  405. * lock->wait_lock.
  406. */
  407. plist_del(&waiter->pi_list_entry, &current->pi_waiters);
  408. rt_mutex_set_owner(lock, NULL);
  409. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  410. wake_up_process(waiter->task);
  411. }
  412. /*
  413. * Remove a waiter from a lock and give up
  414. *
  415. * Must be called with lock->wait_lock held and
  416. * have just failed to try_to_take_rt_mutex().
  417. */
  418. static void remove_waiter(struct rt_mutex *lock,
  419. struct rt_mutex_waiter *waiter)
  420. {
  421. int first = (waiter == rt_mutex_top_waiter(lock));
  422. struct task_struct *owner = rt_mutex_owner(lock);
  423. unsigned long flags;
  424. int chain_walk = 0;
  425. raw_spin_lock_irqsave(&current->pi_lock, flags);
  426. plist_del(&waiter->list_entry, &lock->wait_list);
  427. current->pi_blocked_on = NULL;
  428. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  429. if (!owner)
  430. return;
  431. if (first) {
  432. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  433. plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
  434. if (rt_mutex_has_waiters(lock)) {
  435. struct rt_mutex_waiter *next;
  436. next = rt_mutex_top_waiter(lock);
  437. plist_add(&next->pi_list_entry, &owner->pi_waiters);
  438. }
  439. __rt_mutex_adjust_prio(owner);
  440. if (owner->pi_blocked_on)
  441. chain_walk = 1;
  442. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  443. }
  444. WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
  445. if (!chain_walk)
  446. return;
  447. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  448. get_task_struct(owner);
  449. raw_spin_unlock(&lock->wait_lock);
  450. rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current);
  451. raw_spin_lock(&lock->wait_lock);
  452. }
  453. /*
  454. * Recheck the pi chain, in case we got a priority setting
  455. *
  456. * Called from sched_setscheduler
  457. */
  458. void rt_mutex_adjust_pi(struct task_struct *task)
  459. {
  460. struct rt_mutex_waiter *waiter;
  461. unsigned long flags;
  462. raw_spin_lock_irqsave(&task->pi_lock, flags);
  463. waiter = task->pi_blocked_on;
  464. if (!waiter || waiter->list_entry.prio == task->prio) {
  465. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  466. return;
  467. }
  468. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  469. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  470. get_task_struct(task);
  471. rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task);
  472. }
  473. /**
  474. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  475. * @lock: the rt_mutex to take
  476. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  477. * or TASK_UNINTERRUPTIBLE)
  478. * @timeout: the pre-initialized and started timer, or NULL for none
  479. * @waiter: the pre-initialized rt_mutex_waiter
  480. *
  481. * lock->wait_lock must be held by the caller.
  482. */
  483. static int __sched
  484. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  485. struct hrtimer_sleeper *timeout,
  486. struct rt_mutex_waiter *waiter)
  487. {
  488. int ret = 0;
  489. for (;;) {
  490. /* Try to acquire the lock: */
  491. if (try_to_take_rt_mutex(lock, current, waiter))
  492. break;
  493. /*
  494. * TASK_INTERRUPTIBLE checks for signals and
  495. * timeout. Ignored otherwise.
  496. */
  497. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  498. /* Signal pending? */
  499. if (signal_pending(current))
  500. ret = -EINTR;
  501. if (timeout && !timeout->task)
  502. ret = -ETIMEDOUT;
  503. if (ret)
  504. break;
  505. }
  506. raw_spin_unlock(&lock->wait_lock);
  507. debug_rt_mutex_print_deadlock(waiter);
  508. schedule_rt_mutex(lock);
  509. raw_spin_lock(&lock->wait_lock);
  510. set_current_state(state);
  511. }
  512. return ret;
  513. }
  514. /*
  515. * Slow path lock function:
  516. */
  517. static int __sched
  518. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  519. struct hrtimer_sleeper *timeout,
  520. int detect_deadlock)
  521. {
  522. struct rt_mutex_waiter waiter;
  523. int ret = 0;
  524. debug_rt_mutex_init_waiter(&waiter);
  525. raw_spin_lock(&lock->wait_lock);
  526. /* Try to acquire the lock again: */
  527. if (try_to_take_rt_mutex(lock, current, NULL)) {
  528. raw_spin_unlock(&lock->wait_lock);
  529. return 0;
  530. }
  531. set_current_state(state);
  532. /* Setup the timer, when timeout != NULL */
  533. if (unlikely(timeout)) {
  534. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  535. if (!hrtimer_active(&timeout->timer))
  536. timeout->task = NULL;
  537. }
  538. ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
  539. if (likely(!ret))
  540. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  541. set_current_state(TASK_RUNNING);
  542. if (unlikely(ret))
  543. remove_waiter(lock, &waiter);
  544. /*
  545. * try_to_take_rt_mutex() sets the waiter bit
  546. * unconditionally. We might have to fix that up.
  547. */
  548. fixup_rt_mutex_waiters(lock);
  549. raw_spin_unlock(&lock->wait_lock);
  550. /* Remove pending timer: */
  551. if (unlikely(timeout))
  552. hrtimer_cancel(&timeout->timer);
  553. debug_rt_mutex_free_waiter(&waiter);
  554. return ret;
  555. }
  556. /*
  557. * Slow path try-lock function:
  558. */
  559. static inline int
  560. rt_mutex_slowtrylock(struct rt_mutex *lock)
  561. {
  562. int ret = 0;
  563. raw_spin_lock(&lock->wait_lock);
  564. if (likely(rt_mutex_owner(lock) != current)) {
  565. ret = try_to_take_rt_mutex(lock, current, NULL);
  566. /*
  567. * try_to_take_rt_mutex() sets the lock waiters
  568. * bit unconditionally. Clean this up.
  569. */
  570. fixup_rt_mutex_waiters(lock);
  571. }
  572. raw_spin_unlock(&lock->wait_lock);
  573. return ret;
  574. }
  575. /*
  576. * Slow path to release a rt-mutex:
  577. */
  578. static void __sched
  579. rt_mutex_slowunlock(struct rt_mutex *lock)
  580. {
  581. raw_spin_lock(&lock->wait_lock);
  582. debug_rt_mutex_unlock(lock);
  583. rt_mutex_deadlock_account_unlock(current);
  584. if (!rt_mutex_has_waiters(lock)) {
  585. lock->owner = NULL;
  586. raw_spin_unlock(&lock->wait_lock);
  587. return;
  588. }
  589. wakeup_next_waiter(lock);
  590. raw_spin_unlock(&lock->wait_lock);
  591. /* Undo pi boosting if necessary: */
  592. rt_mutex_adjust_prio(current);
  593. }
  594. /*
  595. * debug aware fast / slowpath lock,trylock,unlock
  596. *
  597. * The atomic acquire/release ops are compiled away, when either the
  598. * architecture does not support cmpxchg or when debugging is enabled.
  599. */
  600. static inline int
  601. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  602. int detect_deadlock,
  603. int (*slowfn)(struct rt_mutex *lock, int state,
  604. struct hrtimer_sleeper *timeout,
  605. int detect_deadlock))
  606. {
  607. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  608. rt_mutex_deadlock_account_lock(lock, current);
  609. return 0;
  610. } else
  611. return slowfn(lock, state, NULL, detect_deadlock);
  612. }
  613. static inline int
  614. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  615. struct hrtimer_sleeper *timeout, int detect_deadlock,
  616. int (*slowfn)(struct rt_mutex *lock, int state,
  617. struct hrtimer_sleeper *timeout,
  618. int detect_deadlock))
  619. {
  620. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  621. rt_mutex_deadlock_account_lock(lock, current);
  622. return 0;
  623. } else
  624. return slowfn(lock, state, timeout, detect_deadlock);
  625. }
  626. static inline int
  627. rt_mutex_fasttrylock(struct rt_mutex *lock,
  628. int (*slowfn)(struct rt_mutex *lock))
  629. {
  630. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  631. rt_mutex_deadlock_account_lock(lock, current);
  632. return 1;
  633. }
  634. return slowfn(lock);
  635. }
  636. static inline void
  637. rt_mutex_fastunlock(struct rt_mutex *lock,
  638. void (*slowfn)(struct rt_mutex *lock))
  639. {
  640. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  641. rt_mutex_deadlock_account_unlock(current);
  642. else
  643. slowfn(lock);
  644. }
  645. /**
  646. * rt_mutex_lock - lock a rt_mutex
  647. *
  648. * @lock: the rt_mutex to be locked
  649. */
  650. void __sched rt_mutex_lock(struct rt_mutex *lock)
  651. {
  652. might_sleep();
  653. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
  654. }
  655. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  656. /**
  657. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  658. *
  659. * @lock: the rt_mutex to be locked
  660. * @detect_deadlock: deadlock detection on/off
  661. *
  662. * Returns:
  663. * 0 on success
  664. * -EINTR when interrupted by a signal
  665. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  666. */
  667. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
  668. int detect_deadlock)
  669. {
  670. might_sleep();
  671. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
  672. detect_deadlock, rt_mutex_slowlock);
  673. }
  674. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  675. /**
  676. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  677. * the timeout structure is provided
  678. * by the caller
  679. *
  680. * @lock: the rt_mutex to be locked
  681. * @timeout: timeout structure or NULL (no timeout)
  682. * @detect_deadlock: deadlock detection on/off
  683. *
  684. * Returns:
  685. * 0 on success
  686. * -EINTR when interrupted by a signal
  687. * -ETIMEDOUT when the timeout expired
  688. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  689. */
  690. int
  691. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
  692. int detect_deadlock)
  693. {
  694. might_sleep();
  695. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  696. detect_deadlock, rt_mutex_slowlock);
  697. }
  698. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  699. /**
  700. * rt_mutex_trylock - try to lock a rt_mutex
  701. *
  702. * @lock: the rt_mutex to be locked
  703. *
  704. * Returns 1 on success and 0 on contention
  705. */
  706. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  707. {
  708. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  709. }
  710. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  711. /**
  712. * rt_mutex_unlock - unlock a rt_mutex
  713. *
  714. * @lock: the rt_mutex to be unlocked
  715. */
  716. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  717. {
  718. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  719. }
  720. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  721. /**
  722. * rt_mutex_destroy - mark a mutex unusable
  723. * @lock: the mutex to be destroyed
  724. *
  725. * This function marks the mutex uninitialized, and any subsequent
  726. * use of the mutex is forbidden. The mutex must not be locked when
  727. * this function is called.
  728. */
  729. void rt_mutex_destroy(struct rt_mutex *lock)
  730. {
  731. WARN_ON(rt_mutex_is_locked(lock));
  732. #ifdef CONFIG_DEBUG_RT_MUTEXES
  733. lock->magic = NULL;
  734. #endif
  735. }
  736. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  737. /**
  738. * __rt_mutex_init - initialize the rt lock
  739. *
  740. * @lock: the rt lock to be initialized
  741. *
  742. * Initialize the rt lock to unlocked state.
  743. *
  744. * Initializing of a locked rt lock is not allowed
  745. */
  746. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  747. {
  748. lock->owner = NULL;
  749. raw_spin_lock_init(&lock->wait_lock);
  750. plist_head_init(&lock->wait_list);
  751. debug_rt_mutex_init(lock, name);
  752. }
  753. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  754. /**
  755. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  756. * proxy owner
  757. *
  758. * @lock: the rt_mutex to be locked
  759. * @proxy_owner:the task to set as owner
  760. *
  761. * No locking. Caller has to do serializing itself
  762. * Special API call for PI-futex support
  763. */
  764. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  765. struct task_struct *proxy_owner)
  766. {
  767. __rt_mutex_init(lock, NULL);
  768. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  769. rt_mutex_set_owner(lock, proxy_owner);
  770. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  771. }
  772. /**
  773. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  774. *
  775. * @lock: the rt_mutex to be locked
  776. *
  777. * No locking. Caller has to do serializing itself
  778. * Special API call for PI-futex support
  779. */
  780. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  781. struct task_struct *proxy_owner)
  782. {
  783. debug_rt_mutex_proxy_unlock(lock);
  784. rt_mutex_set_owner(lock, NULL);
  785. rt_mutex_deadlock_account_unlock(proxy_owner);
  786. }
  787. /**
  788. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  789. * @lock: the rt_mutex to take
  790. * @waiter: the pre-initialized rt_mutex_waiter
  791. * @task: the task to prepare
  792. * @detect_deadlock: perform deadlock detection (1) or not (0)
  793. *
  794. * Returns:
  795. * 0 - task blocked on lock
  796. * 1 - acquired the lock for task, caller should wake it up
  797. * <0 - error
  798. *
  799. * Special API call for FUTEX_REQUEUE_PI support.
  800. */
  801. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  802. struct rt_mutex_waiter *waiter,
  803. struct task_struct *task, int detect_deadlock)
  804. {
  805. int ret;
  806. raw_spin_lock(&lock->wait_lock);
  807. if (try_to_take_rt_mutex(lock, task, NULL)) {
  808. raw_spin_unlock(&lock->wait_lock);
  809. return 1;
  810. }
  811. ret = task_blocks_on_rt_mutex(lock, waiter, task, detect_deadlock);
  812. if (ret && !rt_mutex_owner(lock)) {
  813. /*
  814. * Reset the return value. We might have
  815. * returned with -EDEADLK and the owner
  816. * released the lock while we were walking the
  817. * pi chain. Let the waiter sort it out.
  818. */
  819. ret = 0;
  820. }
  821. if (unlikely(ret))
  822. remove_waiter(lock, waiter);
  823. raw_spin_unlock(&lock->wait_lock);
  824. debug_rt_mutex_print_deadlock(waiter);
  825. return ret;
  826. }
  827. /**
  828. * rt_mutex_next_owner - return the next owner of the lock
  829. *
  830. * @lock: the rt lock query
  831. *
  832. * Returns the next owner of the lock or NULL
  833. *
  834. * Caller has to serialize against other accessors to the lock
  835. * itself.
  836. *
  837. * Special API call for PI-futex support
  838. */
  839. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  840. {
  841. if (!rt_mutex_has_waiters(lock))
  842. return NULL;
  843. return rt_mutex_top_waiter(lock)->task;
  844. }
  845. /**
  846. * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  847. * @lock: the rt_mutex we were woken on
  848. * @to: the timeout, null if none. hrtimer should already have
  849. * been started.
  850. * @waiter: the pre-initialized rt_mutex_waiter
  851. * @detect_deadlock: perform deadlock detection (1) or not (0)
  852. *
  853. * Complete the lock acquisition started our behalf by another thread.
  854. *
  855. * Returns:
  856. * 0 - success
  857. * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
  858. *
  859. * Special API call for PI-futex requeue support
  860. */
  861. int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  862. struct hrtimer_sleeper *to,
  863. struct rt_mutex_waiter *waiter,
  864. int detect_deadlock)
  865. {
  866. int ret;
  867. raw_spin_lock(&lock->wait_lock);
  868. set_current_state(TASK_INTERRUPTIBLE);
  869. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  870. set_current_state(TASK_RUNNING);
  871. if (unlikely(ret))
  872. remove_waiter(lock, waiter);
  873. /*
  874. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  875. * have to fix that up.
  876. */
  877. fixup_rt_mutex_waiters(lock);
  878. raw_spin_unlock(&lock->wait_lock);
  879. return ret;
  880. }