rtmutex.c 32 KB

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