rtmutex.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * RT-Mutexes: simple blocking mutual exclusion locks with PI support
  4. *
  5. * started by Ingo Molnar and Thomas Gleixner.
  6. *
  7. * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  8. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  9. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  10. * Copyright (C) 2006 Esben Nielsen
  11. * Adaptive Spinlocks:
  12. * Copyright (C) 2008 Novell, Inc., Gregory Haskins, Sven Dietrich,
  13. * and Peter Morreale,
  14. * Adaptive Spinlocks simplification:
  15. * Copyright (C) 2008 Red Hat, Inc., Steven Rostedt <srostedt@redhat.com>
  16. *
  17. * See Documentation/locking/rt-mutex-design.rst for details.
  18. */
  19. #include <linux/sched.h>
  20. #include <linux/sched/debug.h>
  21. #include <linux/sched/deadline.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/sched/rt.h>
  24. #include <linux/sched/wake_q.h>
  25. #include <linux/ww_mutex.h>
  26. #include <trace/events/lock.h>
  27. #include "rtmutex_common.h"
  28. #ifndef WW_RT
  29. # define build_ww_mutex() (false)
  30. # define ww_container_of(rtm) NULL
  31. static inline int __ww_mutex_add_waiter(struct rt_mutex_waiter *waiter,
  32. struct rt_mutex *lock,
  33. struct ww_acquire_ctx *ww_ctx)
  34. {
  35. return 0;
  36. }
  37. static inline void __ww_mutex_check_waiters(struct rt_mutex *lock,
  38. struct ww_acquire_ctx *ww_ctx)
  39. {
  40. }
  41. static inline void ww_mutex_lock_acquired(struct ww_mutex *lock,
  42. struct ww_acquire_ctx *ww_ctx)
  43. {
  44. }
  45. static inline int __ww_mutex_check_kill(struct rt_mutex *lock,
  46. struct rt_mutex_waiter *waiter,
  47. struct ww_acquire_ctx *ww_ctx)
  48. {
  49. return 0;
  50. }
  51. #else
  52. # define build_ww_mutex() (true)
  53. # define ww_container_of(rtm) container_of(rtm, struct ww_mutex, base)
  54. # include "ww_mutex.h"
  55. #endif
  56. /*
  57. * lock->owner state tracking:
  58. *
  59. * lock->owner holds the task_struct pointer of the owner. Bit 0
  60. * is used to keep track of the "lock has waiters" state.
  61. *
  62. * owner bit0
  63. * NULL 0 lock is free (fast acquire possible)
  64. * NULL 1 lock is free and has waiters and the top waiter
  65. * is going to take the lock*
  66. * taskpointer 0 lock is held (fast release possible)
  67. * taskpointer 1 lock is held and has waiters**
  68. *
  69. * The fast atomic compare exchange based acquire and release is only
  70. * possible when bit 0 of lock->owner is 0.
  71. *
  72. * (*) It also can be a transitional state when grabbing the lock
  73. * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
  74. * we need to set the bit0 before looking at the lock, and the owner may be
  75. * NULL in this small time, hence this can be a transitional state.
  76. *
  77. * (**) There is a small time when bit 0 is set but there are no
  78. * waiters. This can happen when grabbing the lock in the slow path.
  79. * To prevent a cmpxchg of the owner releasing the lock, we need to
  80. * set this bit before looking at the lock.
  81. */
  82. static __always_inline struct task_struct *
  83. rt_mutex_owner_encode(struct rt_mutex_base *lock, struct task_struct *owner)
  84. {
  85. unsigned long val = (unsigned long)owner;
  86. if (rt_mutex_has_waiters(lock))
  87. val |= RT_MUTEX_HAS_WAITERS;
  88. return (struct task_struct *)val;
  89. }
  90. static __always_inline void
  91. rt_mutex_set_owner(struct rt_mutex_base *lock, struct task_struct *owner)
  92. {
  93. /*
  94. * lock->wait_lock is held but explicit acquire semantics are needed
  95. * for a new lock owner so WRITE_ONCE is insufficient.
  96. */
  97. xchg_acquire(&lock->owner, rt_mutex_owner_encode(lock, owner));
  98. }
  99. static __always_inline void rt_mutex_clear_owner(struct rt_mutex_base *lock)
  100. {
  101. /* lock->wait_lock is held so the unlock provides release semantics. */
  102. WRITE_ONCE(lock->owner, rt_mutex_owner_encode(lock, NULL));
  103. }
  104. static __always_inline void clear_rt_mutex_waiters(struct rt_mutex_base *lock)
  105. {
  106. lock->owner = (struct task_struct *)
  107. ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
  108. }
  109. static __always_inline void
  110. fixup_rt_mutex_waiters(struct rt_mutex_base *lock, bool acquire_lock)
  111. {
  112. unsigned long owner, *p = (unsigned long *) &lock->owner;
  113. if (rt_mutex_has_waiters(lock))
  114. return;
  115. /*
  116. * The rbtree has no waiters enqueued, now make sure that the
  117. * lock->owner still has the waiters bit set, otherwise the
  118. * following can happen:
  119. *
  120. * CPU 0 CPU 1 CPU2
  121. * l->owner=T1
  122. * rt_mutex_lock(l)
  123. * lock(l->lock)
  124. * l->owner = T1 | HAS_WAITERS;
  125. * enqueue(T2)
  126. * boost()
  127. * unlock(l->lock)
  128. * block()
  129. *
  130. * rt_mutex_lock(l)
  131. * lock(l->lock)
  132. * l->owner = T1 | HAS_WAITERS;
  133. * enqueue(T3)
  134. * boost()
  135. * unlock(l->lock)
  136. * block()
  137. * signal(->T2) signal(->T3)
  138. * lock(l->lock)
  139. * dequeue(T2)
  140. * deboost()
  141. * unlock(l->lock)
  142. * lock(l->lock)
  143. * dequeue(T3)
  144. * ==> wait list is empty
  145. * deboost()
  146. * unlock(l->lock)
  147. * lock(l->lock)
  148. * fixup_rt_mutex_waiters()
  149. * if (wait_list_empty(l) {
  150. * l->owner = owner
  151. * owner = l->owner & ~HAS_WAITERS;
  152. * ==> l->owner = T1
  153. * }
  154. * lock(l->lock)
  155. * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
  156. * if (wait_list_empty(l) {
  157. * owner = l->owner & ~HAS_WAITERS;
  158. * cmpxchg(l->owner, T1, NULL)
  159. * ===> Success (l->owner = NULL)
  160. *
  161. * l->owner = owner
  162. * ==> l->owner = T1
  163. * }
  164. *
  165. * With the check for the waiter bit in place T3 on CPU2 will not
  166. * overwrite. All tasks fiddling with the waiters bit are
  167. * serialized by l->lock, so nothing else can modify the waiters
  168. * bit. If the bit is set then nothing can change l->owner either
  169. * so the simple RMW is safe. The cmpxchg() will simply fail if it
  170. * happens in the middle of the RMW because the waiters bit is
  171. * still set.
  172. */
  173. owner = READ_ONCE(*p);
  174. if (owner & RT_MUTEX_HAS_WAITERS) {
  175. /*
  176. * See rt_mutex_set_owner() and rt_mutex_clear_owner() on
  177. * why xchg_acquire() is used for updating owner for
  178. * locking and WRITE_ONCE() for unlocking.
  179. *
  180. * WRITE_ONCE() would work for the acquire case too, but
  181. * in case that the lock acquisition failed it might
  182. * force other lockers into the slow path unnecessarily.
  183. */
  184. if (acquire_lock)
  185. xchg_acquire(p, owner & ~RT_MUTEX_HAS_WAITERS);
  186. else
  187. WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
  188. }
  189. }
  190. /*
  191. * We can speed up the acquire/release, if there's no debugging state to be
  192. * set up.
  193. */
  194. #ifndef CONFIG_DEBUG_RT_MUTEXES
  195. static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
  196. struct task_struct *old,
  197. struct task_struct *new)
  198. {
  199. return try_cmpxchg_acquire(&lock->owner, &old, new);
  200. }
  201. static __always_inline bool rt_mutex_try_acquire(struct rt_mutex_base *lock)
  202. {
  203. return rt_mutex_cmpxchg_acquire(lock, NULL, current);
  204. }
  205. static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
  206. struct task_struct *old,
  207. struct task_struct *new)
  208. {
  209. return try_cmpxchg_release(&lock->owner, &old, new);
  210. }
  211. /*
  212. * Callers must hold the ->wait_lock -- which is the whole purpose as we force
  213. * all future threads that attempt to [Rmw] the lock to the slowpath. As such
  214. * relaxed semantics suffice.
  215. */
  216. static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
  217. {
  218. unsigned long *p = (unsigned long *) &lock->owner;
  219. unsigned long owner, new;
  220. owner = READ_ONCE(*p);
  221. do {
  222. new = owner | RT_MUTEX_HAS_WAITERS;
  223. } while (!try_cmpxchg_relaxed(p, &owner, new));
  224. /*
  225. * The cmpxchg loop above is relaxed to avoid back-to-back ACQUIRE
  226. * operations in the event of contention. Ensure the successful
  227. * cmpxchg is visible.
  228. */
  229. smp_mb__after_atomic();
  230. }
  231. /*
  232. * Safe fastpath aware unlock:
  233. * 1) Clear the waiters bit
  234. * 2) Drop lock->wait_lock
  235. * 3) Try to unlock the lock with cmpxchg
  236. */
  237. static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
  238. unsigned long flags)
  239. __releases(lock->wait_lock)
  240. {
  241. struct task_struct *owner = rt_mutex_owner(lock);
  242. clear_rt_mutex_waiters(lock);
  243. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  244. /*
  245. * If a new waiter comes in between the unlock and the cmpxchg
  246. * we have two situations:
  247. *
  248. * unlock(wait_lock);
  249. * lock(wait_lock);
  250. * cmpxchg(p, owner, 0) == owner
  251. * mark_rt_mutex_waiters(lock);
  252. * acquire(lock);
  253. * or:
  254. *
  255. * unlock(wait_lock);
  256. * lock(wait_lock);
  257. * mark_rt_mutex_waiters(lock);
  258. *
  259. * cmpxchg(p, owner, 0) != owner
  260. * enqueue_waiter();
  261. * unlock(wait_lock);
  262. * lock(wait_lock);
  263. * wake waiter();
  264. * unlock(wait_lock);
  265. * lock(wait_lock);
  266. * acquire(lock);
  267. */
  268. return rt_mutex_cmpxchg_release(lock, owner, NULL);
  269. }
  270. #else
  271. static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock,
  272. struct task_struct *old,
  273. struct task_struct *new)
  274. {
  275. return false;
  276. }
  277. static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock);
  278. static __always_inline bool rt_mutex_try_acquire(struct rt_mutex_base *lock)
  279. {
  280. /*
  281. * With debug enabled rt_mutex_cmpxchg trylock() will always fail.
  282. *
  283. * Avoid unconditionally taking the slow path by using
  284. * rt_mutex_slow_trylock() which is covered by the debug code and can
  285. * acquire a non-contended rtmutex.
  286. */
  287. return rt_mutex_slowtrylock(lock);
  288. }
  289. static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock,
  290. struct task_struct *old,
  291. struct task_struct *new)
  292. {
  293. return false;
  294. }
  295. static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock)
  296. {
  297. lock->owner = (struct task_struct *)
  298. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  299. }
  300. /*
  301. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  302. */
  303. static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock,
  304. unsigned long flags)
  305. __releases(lock->wait_lock)
  306. {
  307. lock->owner = NULL;
  308. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  309. return true;
  310. }
  311. #endif
  312. static __always_inline int __waiter_prio(struct task_struct *task)
  313. {
  314. int prio = task->prio;
  315. if (!rt_or_dl_prio(prio))
  316. return DEFAULT_PRIO;
  317. return prio;
  318. }
  319. /*
  320. * Update the waiter->tree copy of the sort keys.
  321. */
  322. static __always_inline void
  323. waiter_update_prio(struct rt_mutex_waiter *waiter, struct task_struct *task)
  324. {
  325. lockdep_assert_held(&waiter->lock->wait_lock);
  326. lockdep_assert(RB_EMPTY_NODE(&waiter->tree.entry));
  327. waiter->tree.prio = __waiter_prio(task);
  328. waiter->tree.deadline = task->dl.deadline;
  329. }
  330. /*
  331. * Update the waiter->pi_tree copy of the sort keys (from the tree copy).
  332. */
  333. static __always_inline void
  334. waiter_clone_prio(struct rt_mutex_waiter *waiter, struct task_struct *task)
  335. {
  336. lockdep_assert_held(&waiter->lock->wait_lock);
  337. lockdep_assert_held(&task->pi_lock);
  338. lockdep_assert(RB_EMPTY_NODE(&waiter->pi_tree.entry));
  339. waiter->pi_tree.prio = waiter->tree.prio;
  340. waiter->pi_tree.deadline = waiter->tree.deadline;
  341. }
  342. /*
  343. * Only use with rt_waiter_node_{less,equal}()
  344. */
  345. #define task_to_waiter_node(p) \
  346. &(struct rt_waiter_node){ .prio = __waiter_prio(p), .deadline = (p)->dl.deadline }
  347. #define task_to_waiter(p) \
  348. &(struct rt_mutex_waiter){ .tree = *task_to_waiter_node(p) }
  349. static __always_inline int rt_waiter_node_less(struct rt_waiter_node *left,
  350. struct rt_waiter_node *right)
  351. {
  352. if (left->prio < right->prio)
  353. return 1;
  354. /*
  355. * If both waiters have dl_prio(), we check the deadlines of the
  356. * associated tasks.
  357. * If left waiter has a dl_prio(), and we didn't return 1 above,
  358. * then right waiter has a dl_prio() too.
  359. */
  360. if (dl_prio(left->prio))
  361. return dl_time_before(left->deadline, right->deadline);
  362. return 0;
  363. }
  364. static __always_inline int rt_waiter_node_equal(struct rt_waiter_node *left,
  365. struct rt_waiter_node *right)
  366. {
  367. if (left->prio != right->prio)
  368. return 0;
  369. /*
  370. * If both waiters have dl_prio(), we check the deadlines of the
  371. * associated tasks.
  372. * If left waiter has a dl_prio(), and we didn't return 0 above,
  373. * then right waiter has a dl_prio() too.
  374. */
  375. if (dl_prio(left->prio))
  376. return left->deadline == right->deadline;
  377. return 1;
  378. }
  379. static inline bool rt_mutex_steal(struct rt_mutex_waiter *waiter,
  380. struct rt_mutex_waiter *top_waiter)
  381. {
  382. if (rt_waiter_node_less(&waiter->tree, &top_waiter->tree))
  383. return true;
  384. #ifdef RT_MUTEX_BUILD_SPINLOCKS
  385. /*
  386. * Note that RT tasks are excluded from same priority (lateral)
  387. * steals to prevent the introduction of an unbounded latency.
  388. */
  389. if (rt_or_dl_prio(waiter->tree.prio))
  390. return false;
  391. return rt_waiter_node_equal(&waiter->tree, &top_waiter->tree);
  392. #else
  393. return false;
  394. #endif
  395. }
  396. #define __node_2_waiter(node) \
  397. rb_entry((node), struct rt_mutex_waiter, tree.entry)
  398. static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_node *b)
  399. {
  400. struct rt_mutex_waiter *aw = __node_2_waiter(a);
  401. struct rt_mutex_waiter *bw = __node_2_waiter(b);
  402. if (rt_waiter_node_less(&aw->tree, &bw->tree))
  403. return 1;
  404. if (!build_ww_mutex())
  405. return 0;
  406. if (rt_waiter_node_less(&bw->tree, &aw->tree))
  407. return 0;
  408. /* NOTE: relies on waiter->ww_ctx being set before insertion */
  409. if (aw->ww_ctx) {
  410. if (!bw->ww_ctx)
  411. return 1;
  412. return (signed long)(aw->ww_ctx->stamp -
  413. bw->ww_ctx->stamp) < 0;
  414. }
  415. return 0;
  416. }
  417. static __always_inline void
  418. rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
  419. {
  420. lockdep_assert_held(&lock->wait_lock);
  421. rb_add_cached(&waiter->tree.entry, &lock->waiters, __waiter_less);
  422. }
  423. static __always_inline void
  424. rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter)
  425. {
  426. lockdep_assert_held(&lock->wait_lock);
  427. if (RB_EMPTY_NODE(&waiter->tree.entry))
  428. return;
  429. rb_erase_cached(&waiter->tree.entry, &lock->waiters);
  430. RB_CLEAR_NODE(&waiter->tree.entry);
  431. }
  432. #define __node_2_rt_node(node) \
  433. rb_entry((node), struct rt_waiter_node, entry)
  434. static __always_inline bool __pi_waiter_less(struct rb_node *a, const struct rb_node *b)
  435. {
  436. return rt_waiter_node_less(__node_2_rt_node(a), __node_2_rt_node(b));
  437. }
  438. static __always_inline void
  439. rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  440. {
  441. lockdep_assert_held(&task->pi_lock);
  442. rb_add_cached(&waiter->pi_tree.entry, &task->pi_waiters, __pi_waiter_less);
  443. }
  444. static __always_inline void
  445. rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  446. {
  447. lockdep_assert_held(&task->pi_lock);
  448. if (RB_EMPTY_NODE(&waiter->pi_tree.entry))
  449. return;
  450. rb_erase_cached(&waiter->pi_tree.entry, &task->pi_waiters);
  451. RB_CLEAR_NODE(&waiter->pi_tree.entry);
  452. }
  453. static __always_inline void rt_mutex_adjust_prio(struct rt_mutex_base *lock,
  454. struct task_struct *p)
  455. {
  456. struct task_struct *pi_task = NULL;
  457. lockdep_assert_held(&lock->wait_lock);
  458. lockdep_assert(rt_mutex_owner(lock) == p);
  459. lockdep_assert_held(&p->pi_lock);
  460. if (task_has_pi_waiters(p))
  461. pi_task = task_top_pi_waiter(p)->task;
  462. rt_mutex_setprio(p, pi_task);
  463. }
  464. /* RT mutex specific wake_q wrappers */
  465. static __always_inline void rt_mutex_wake_q_add_task(struct rt_wake_q_head *wqh,
  466. struct task_struct *task,
  467. unsigned int wake_state)
  468. {
  469. if (IS_ENABLED(CONFIG_PREEMPT_RT) && wake_state == TASK_RTLOCK_WAIT) {
  470. if (IS_ENABLED(CONFIG_PROVE_LOCKING))
  471. WARN_ON_ONCE(wqh->rtlock_task);
  472. get_task_struct(task);
  473. wqh->rtlock_task = task;
  474. } else {
  475. wake_q_add(&wqh->head, task);
  476. }
  477. }
  478. static __always_inline void rt_mutex_wake_q_add(struct rt_wake_q_head *wqh,
  479. struct rt_mutex_waiter *w)
  480. {
  481. rt_mutex_wake_q_add_task(wqh, w->task, w->wake_state);
  482. }
  483. static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh)
  484. {
  485. if (IS_ENABLED(CONFIG_PREEMPT_RT) && wqh->rtlock_task) {
  486. wake_up_state(wqh->rtlock_task, TASK_RTLOCK_WAIT);
  487. put_task_struct(wqh->rtlock_task);
  488. wqh->rtlock_task = NULL;
  489. }
  490. if (!wake_q_empty(&wqh->head))
  491. wake_up_q(&wqh->head);
  492. /* Pairs with preempt_disable() in mark_wakeup_next_waiter() */
  493. preempt_enable();
  494. }
  495. /*
  496. * Deadlock detection is conditional:
  497. *
  498. * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
  499. * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
  500. *
  501. * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
  502. * conducted independent of the detect argument.
  503. *
  504. * If the waiter argument is NULL this indicates the deboost path and
  505. * deadlock detection is disabled independent of the detect argument
  506. * and the config settings.
  507. */
  508. static __always_inline bool
  509. rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  510. enum rtmutex_chainwalk chwalk)
  511. {
  512. if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES))
  513. return waiter != NULL;
  514. return chwalk == RT_MUTEX_FULL_CHAINWALK;
  515. }
  516. static __always_inline struct rt_mutex_base *task_blocked_on_lock(struct task_struct *p)
  517. {
  518. return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  519. }
  520. /*
  521. * Adjust the priority chain. Also used for deadlock detection.
  522. * Decreases task's usage by one - may thus free the task.
  523. *
  524. * @task: the task owning the mutex (owner) for which a chain walk is
  525. * probably needed
  526. * @chwalk: do we have to carry out deadlock detection?
  527. * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
  528. * things for a task that has just got its priority adjusted, and
  529. * is waiting on a mutex)
  530. * @next_lock: the mutex on which the owner of @orig_lock was blocked before
  531. * we dropped its pi_lock. Is never dereferenced, only used for
  532. * comparison to detect lock chain changes.
  533. * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
  534. * its priority to the mutex owner (can be NULL in the case
  535. * depicted above or if the top waiter is gone away and we are
  536. * actually deboosting the owner)
  537. * @top_task: the current top waiter
  538. *
  539. * Returns 0 or -EDEADLK.
  540. *
  541. * Chain walk basics and protection scope
  542. *
  543. * [R] refcount on task
  544. * [Pn] task->pi_lock held
  545. * [L] rtmutex->wait_lock held
  546. *
  547. * Normal locking order:
  548. *
  549. * rtmutex->wait_lock
  550. * task->pi_lock
  551. *
  552. * Step Description Protected by
  553. * function arguments:
  554. * @task [R]
  555. * @orig_lock if != NULL @top_task is blocked on it
  556. * @next_lock Unprotected. Cannot be
  557. * dereferenced. Only used for
  558. * comparison.
  559. * @orig_waiter if != NULL @top_task is blocked on it
  560. * @top_task current, or in case of proxy
  561. * locking protected by calling
  562. * code
  563. * again:
  564. * loop_sanity_check();
  565. * retry:
  566. * [1] lock(task->pi_lock); [R] acquire [P1]
  567. * [2] waiter = task->pi_blocked_on; [P1]
  568. * [3] check_exit_conditions_1(); [P1]
  569. * [4] lock = waiter->lock; [P1]
  570. * [5] if (!try_lock(lock->wait_lock)) { [P1] try to acquire [L]
  571. * unlock(task->pi_lock); release [P1]
  572. * goto retry;
  573. * }
  574. * [6] check_exit_conditions_2(); [P1] + [L]
  575. * [7] requeue_lock_waiter(lock, waiter); [P1] + [L]
  576. * [8] unlock(task->pi_lock); release [P1]
  577. * put_task_struct(task); release [R]
  578. * [9] check_exit_conditions_3(); [L]
  579. * [10] task = owner(lock); [L]
  580. * get_task_struct(task); [L] acquire [R]
  581. * lock(task->pi_lock); [L] acquire [P2]
  582. * [11] requeue_pi_waiter(tsk, waiters(lock));[P2] + [L]
  583. * [12] check_exit_conditions_4(); [P2] + [L]
  584. * [13] unlock(task->pi_lock); release [P2]
  585. * unlock(lock->wait_lock); release [L]
  586. * goto again;
  587. *
  588. * Where P1 is the blocking task and P2 is the lock owner; going up one step
  589. * the owner becomes the next blocked task etc..
  590. *
  591. *
  592. */
  593. static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task,
  594. enum rtmutex_chainwalk chwalk,
  595. struct rt_mutex_base *orig_lock,
  596. struct rt_mutex_base *next_lock,
  597. struct rt_mutex_waiter *orig_waiter,
  598. struct task_struct *top_task)
  599. {
  600. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  601. struct rt_mutex_waiter *prerequeue_top_waiter;
  602. int ret = 0, depth = 0;
  603. struct rt_mutex_base *lock;
  604. bool detect_deadlock;
  605. bool requeue = true;
  606. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  607. /*
  608. * The (de)boosting is a step by step approach with a lot of
  609. * pitfalls. We want this to be preemptible and we want hold a
  610. * maximum of two locks per step. So we have to check
  611. * carefully whether things change under us.
  612. */
  613. again:
  614. /*
  615. * We limit the lock chain length for each invocation.
  616. */
  617. if (++depth > max_lock_depth) {
  618. static int prev_max;
  619. /*
  620. * Print this only once. If the admin changes the limit,
  621. * print a new message when reaching the limit again.
  622. */
  623. if (prev_max != max_lock_depth) {
  624. prev_max = max_lock_depth;
  625. printk(KERN_WARNING "Maximum lock depth %d reached "
  626. "task: %s (%d)\n", max_lock_depth,
  627. top_task->comm, task_pid_nr(top_task));
  628. }
  629. put_task_struct(task);
  630. return -EDEADLK;
  631. }
  632. /*
  633. * We are fully preemptible here and only hold the refcount on
  634. * @task. So everything can have changed under us since the
  635. * caller or our own code below (goto retry/again) dropped all
  636. * locks.
  637. */
  638. retry:
  639. /*
  640. * [1] Task cannot go away as we did a get_task() before !
  641. */
  642. raw_spin_lock_irq(&task->pi_lock);
  643. /*
  644. * [2] Get the waiter on which @task is blocked on.
  645. */
  646. waiter = task->pi_blocked_on;
  647. /*
  648. * [3] check_exit_conditions_1() protected by task->pi_lock.
  649. */
  650. /*
  651. * Check whether the end of the boosting chain has been
  652. * reached or the state of the chain has changed while we
  653. * dropped the locks.
  654. */
  655. if (!waiter)
  656. goto out_unlock_pi;
  657. /*
  658. * Check the orig_waiter state. After we dropped the locks,
  659. * the previous owner of the lock might have released the lock.
  660. */
  661. if (orig_waiter && !rt_mutex_owner(orig_lock))
  662. goto out_unlock_pi;
  663. /*
  664. * We dropped all locks after taking a refcount on @task, so
  665. * the task might have moved on in the lock chain or even left
  666. * the chain completely and blocks now on an unrelated lock or
  667. * on @orig_lock.
  668. *
  669. * We stored the lock on which @task was blocked in @next_lock,
  670. * so we can detect the chain change.
  671. */
  672. if (next_lock != waiter->lock)
  673. goto out_unlock_pi;
  674. /*
  675. * There could be 'spurious' loops in the lock graph due to ww_mutex,
  676. * consider:
  677. *
  678. * P1: A, ww_A, ww_B
  679. * P2: ww_B, ww_A
  680. * P3: A
  681. *
  682. * P3 should not return -EDEADLK because it gets trapped in the cycle
  683. * created by P1 and P2 (which will resolve -- and runs into
  684. * max_lock_depth above). Therefore disable detect_deadlock such that
  685. * the below termination condition can trigger once all relevant tasks
  686. * are boosted.
  687. *
  688. * Even when we start with ww_mutex we can disable deadlock detection,
  689. * since we would supress a ww_mutex induced deadlock at [6] anyway.
  690. * Supressing it here however is not sufficient since we might still
  691. * hit [6] due to adjustment driven iteration.
  692. *
  693. * NOTE: if someone were to create a deadlock between 2 ww_classes we'd
  694. * utterly fail to report it; lockdep should.
  695. */
  696. if (IS_ENABLED(CONFIG_PREEMPT_RT) && waiter->ww_ctx && detect_deadlock)
  697. detect_deadlock = false;
  698. /*
  699. * Drop out, when the task has no waiters. Note,
  700. * top_waiter can be NULL, when we are in the deboosting
  701. * mode!
  702. */
  703. if (top_waiter) {
  704. if (!task_has_pi_waiters(task))
  705. goto out_unlock_pi;
  706. /*
  707. * If deadlock detection is off, we stop here if we
  708. * are not the top pi waiter of the task. If deadlock
  709. * detection is enabled we continue, but stop the
  710. * requeueing in the chain walk.
  711. */
  712. if (top_waiter != task_top_pi_waiter(task)) {
  713. if (!detect_deadlock)
  714. goto out_unlock_pi;
  715. else
  716. requeue = false;
  717. }
  718. }
  719. /*
  720. * If the waiter priority is the same as the task priority
  721. * then there is no further priority adjustment necessary. If
  722. * deadlock detection is off, we stop the chain walk. If its
  723. * enabled we continue, but stop the requeueing in the chain
  724. * walk.
  725. */
  726. if (rt_waiter_node_equal(&waiter->tree, task_to_waiter_node(task))) {
  727. if (!detect_deadlock)
  728. goto out_unlock_pi;
  729. else
  730. requeue = false;
  731. }
  732. /*
  733. * [4] Get the next lock; per holding task->pi_lock we can't unblock
  734. * and guarantee @lock's existence.
  735. */
  736. lock = waiter->lock;
  737. /*
  738. * [5] We need to trylock here as we are holding task->pi_lock,
  739. * which is the reverse lock order versus the other rtmutex
  740. * operations.
  741. *
  742. * Per the above, holding task->pi_lock guarantees lock exists, so
  743. * inverting this lock order is infeasible from a life-time
  744. * perspective.
  745. */
  746. if (!raw_spin_trylock(&lock->wait_lock)) {
  747. raw_spin_unlock_irq(&task->pi_lock);
  748. cpu_relax();
  749. goto retry;
  750. }
  751. /*
  752. * [6] check_exit_conditions_2() protected by task->pi_lock and
  753. * lock->wait_lock.
  754. *
  755. * Deadlock detection. If the lock is the same as the original
  756. * lock which caused us to walk the lock chain or if the
  757. * current lock is owned by the task which initiated the chain
  758. * walk, we detected a deadlock.
  759. */
  760. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  761. ret = -EDEADLK;
  762. /*
  763. * When the deadlock is due to ww_mutex; also see above. Don't
  764. * report the deadlock and instead let the ww_mutex wound/die
  765. * logic pick which of the contending threads gets -EDEADLK.
  766. *
  767. * NOTE: assumes the cycle only contains a single ww_class; any
  768. * other configuration and we fail to report; also, see
  769. * lockdep.
  770. */
  771. if (IS_ENABLED(CONFIG_PREEMPT_RT) && orig_waiter && orig_waiter->ww_ctx)
  772. ret = 0;
  773. raw_spin_unlock(&lock->wait_lock);
  774. goto out_unlock_pi;
  775. }
  776. /*
  777. * If we just follow the lock chain for deadlock detection, no
  778. * need to do all the requeue operations. To avoid a truckload
  779. * of conditionals around the various places below, just do the
  780. * minimum chain walk checks.
  781. */
  782. if (!requeue) {
  783. /*
  784. * No requeue[7] here. Just release @task [8]
  785. */
  786. raw_spin_unlock(&task->pi_lock);
  787. put_task_struct(task);
  788. /*
  789. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  790. * If there is no owner of the lock, end of chain.
  791. */
  792. if (!rt_mutex_owner(lock)) {
  793. raw_spin_unlock_irq(&lock->wait_lock);
  794. return 0;
  795. }
  796. /* [10] Grab the next task, i.e. owner of @lock */
  797. task = get_task_struct(rt_mutex_owner(lock));
  798. raw_spin_lock(&task->pi_lock);
  799. /*
  800. * No requeue [11] here. We just do deadlock detection.
  801. *
  802. * [12] Store whether owner is blocked
  803. * itself. Decision is made after dropping the locks
  804. */
  805. next_lock = task_blocked_on_lock(task);
  806. /*
  807. * Get the top waiter for the next iteration
  808. */
  809. top_waiter = rt_mutex_top_waiter(lock);
  810. /* [13] Drop locks */
  811. raw_spin_unlock(&task->pi_lock);
  812. raw_spin_unlock_irq(&lock->wait_lock);
  813. /* If owner is not blocked, end of chain. */
  814. if (!next_lock)
  815. goto out_put_task;
  816. goto again;
  817. }
  818. /*
  819. * Store the current top waiter before doing the requeue
  820. * operation on @lock. We need it for the boost/deboost
  821. * decision below.
  822. */
  823. prerequeue_top_waiter = rt_mutex_top_waiter(lock);
  824. /* [7] Requeue the waiter in the lock waiter tree. */
  825. rt_mutex_dequeue(lock, waiter);
  826. /*
  827. * Update the waiter prio fields now that we're dequeued.
  828. *
  829. * These values can have changed through either:
  830. *
  831. * sys_sched_set_scheduler() / sys_sched_setattr()
  832. *
  833. * or
  834. *
  835. * DL CBS enforcement advancing the effective deadline.
  836. */
  837. waiter_update_prio(waiter, task);
  838. rt_mutex_enqueue(lock, waiter);
  839. /*
  840. * [8] Release the (blocking) task in preparation for
  841. * taking the owner task in [10].
  842. *
  843. * Since we hold lock->waiter_lock, task cannot unblock, even if we
  844. * release task->pi_lock.
  845. */
  846. raw_spin_unlock(&task->pi_lock);
  847. put_task_struct(task);
  848. /*
  849. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  850. *
  851. * We must abort the chain walk if there is no lock owner even
  852. * in the dead lock detection case, as we have nothing to
  853. * follow here. This is the end of the chain we are walking.
  854. */
  855. if (!rt_mutex_owner(lock)) {
  856. /*
  857. * If the requeue [7] above changed the top waiter,
  858. * then we need to wake the new top waiter up to try
  859. * to get the lock.
  860. */
  861. top_waiter = rt_mutex_top_waiter(lock);
  862. if (prerequeue_top_waiter != top_waiter)
  863. wake_up_state(top_waiter->task, top_waiter->wake_state);
  864. raw_spin_unlock_irq(&lock->wait_lock);
  865. return 0;
  866. }
  867. /*
  868. * [10] Grab the next task, i.e. the owner of @lock
  869. *
  870. * Per holding lock->wait_lock and checking for !owner above, there
  871. * must be an owner and it cannot go away.
  872. */
  873. task = get_task_struct(rt_mutex_owner(lock));
  874. raw_spin_lock(&task->pi_lock);
  875. /* [11] requeue the pi waiters if necessary */
  876. if (waiter == rt_mutex_top_waiter(lock)) {
  877. /*
  878. * The waiter became the new top (highest priority)
  879. * waiter on the lock. Replace the previous top waiter
  880. * in the owner tasks pi waiters tree with this waiter
  881. * and adjust the priority of the owner.
  882. */
  883. rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
  884. waiter_clone_prio(waiter, task);
  885. rt_mutex_enqueue_pi(task, waiter);
  886. rt_mutex_adjust_prio(lock, task);
  887. } else if (prerequeue_top_waiter == waiter) {
  888. /*
  889. * The waiter was the top waiter on the lock, but is
  890. * no longer the top priority waiter. Replace waiter in
  891. * the owner tasks pi waiters tree with the new top
  892. * (highest priority) waiter and adjust the priority
  893. * of the owner.
  894. * The new top waiter is stored in @waiter so that
  895. * @waiter == @top_waiter evaluates to true below and
  896. * we continue to deboost the rest of the chain.
  897. */
  898. rt_mutex_dequeue_pi(task, waiter);
  899. waiter = rt_mutex_top_waiter(lock);
  900. waiter_clone_prio(waiter, task);
  901. rt_mutex_enqueue_pi(task, waiter);
  902. rt_mutex_adjust_prio(lock, task);
  903. } else {
  904. /*
  905. * Nothing changed. No need to do any priority
  906. * adjustment.
  907. */
  908. }
  909. /*
  910. * [12] check_exit_conditions_4() protected by task->pi_lock
  911. * and lock->wait_lock. The actual decisions are made after we
  912. * dropped the locks.
  913. *
  914. * Check whether the task which owns the current lock is pi
  915. * blocked itself. If yes we store a pointer to the lock for
  916. * the lock chain change detection above. After we dropped
  917. * task->pi_lock next_lock cannot be dereferenced anymore.
  918. */
  919. next_lock = task_blocked_on_lock(task);
  920. /*
  921. * Store the top waiter of @lock for the end of chain walk
  922. * decision below.
  923. */
  924. top_waiter = rt_mutex_top_waiter(lock);
  925. /* [13] Drop the locks */
  926. raw_spin_unlock(&task->pi_lock);
  927. raw_spin_unlock_irq(&lock->wait_lock);
  928. /*
  929. * Make the actual exit decisions [12], based on the stored
  930. * values.
  931. *
  932. * We reached the end of the lock chain. Stop right here. No
  933. * point to go back just to figure that out.
  934. */
  935. if (!next_lock)
  936. goto out_put_task;
  937. /*
  938. * If the current waiter is not the top waiter on the lock,
  939. * then we can stop the chain walk here if we are not in full
  940. * deadlock detection mode.
  941. */
  942. if (!detect_deadlock && waiter != top_waiter)
  943. goto out_put_task;
  944. goto again;
  945. out_unlock_pi:
  946. raw_spin_unlock_irq(&task->pi_lock);
  947. out_put_task:
  948. put_task_struct(task);
  949. return ret;
  950. }
  951. /*
  952. * Try to take an rt-mutex
  953. *
  954. * Must be called with lock->wait_lock held and interrupts disabled
  955. *
  956. * @lock: The lock to be acquired.
  957. * @task: The task which wants to acquire the lock
  958. * @waiter: The waiter that is queued to the lock's wait tree if the
  959. * callsite called task_blocked_on_lock(), otherwise NULL
  960. */
  961. static int __sched
  962. try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task,
  963. struct rt_mutex_waiter *waiter)
  964. {
  965. lockdep_assert_held(&lock->wait_lock);
  966. /*
  967. * Before testing whether we can acquire @lock, we set the
  968. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  969. * other tasks which try to modify @lock into the slow path
  970. * and they serialize on @lock->wait_lock.
  971. *
  972. * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
  973. * as explained at the top of this file if and only if:
  974. *
  975. * - There is a lock owner. The caller must fixup the
  976. * transient state if it does a trylock or leaves the lock
  977. * function due to a signal or timeout.
  978. *
  979. * - @task acquires the lock and there are no other
  980. * waiters. This is undone in rt_mutex_set_owner(@task) at
  981. * the end of this function.
  982. */
  983. mark_rt_mutex_waiters(lock);
  984. /*
  985. * If @lock has an owner, give up.
  986. */
  987. if (rt_mutex_owner(lock))
  988. return 0;
  989. /*
  990. * If @waiter != NULL, @task has already enqueued the waiter
  991. * into @lock waiter tree. If @waiter == NULL then this is a
  992. * trylock attempt.
  993. */
  994. if (waiter) {
  995. struct rt_mutex_waiter *top_waiter = rt_mutex_top_waiter(lock);
  996. /*
  997. * If waiter is the highest priority waiter of @lock,
  998. * or allowed to steal it, take it over.
  999. */
  1000. if (waiter == top_waiter || rt_mutex_steal(waiter, top_waiter)) {
  1001. /*
  1002. * We can acquire the lock. Remove the waiter from the
  1003. * lock waiters tree.
  1004. */
  1005. rt_mutex_dequeue(lock, waiter);
  1006. } else {
  1007. return 0;
  1008. }
  1009. } else {
  1010. /*
  1011. * If the lock has waiters already we check whether @task is
  1012. * eligible to take over the lock.
  1013. *
  1014. * If there are no other waiters, @task can acquire
  1015. * the lock. @task->pi_blocked_on is NULL, so it does
  1016. * not need to be dequeued.
  1017. */
  1018. if (rt_mutex_has_waiters(lock)) {
  1019. /* Check whether the trylock can steal it. */
  1020. if (!rt_mutex_steal(task_to_waiter(task),
  1021. rt_mutex_top_waiter(lock)))
  1022. return 0;
  1023. /*
  1024. * The current top waiter stays enqueued. We
  1025. * don't have to change anything in the lock
  1026. * waiters order.
  1027. */
  1028. } else {
  1029. /*
  1030. * No waiters. Take the lock without the
  1031. * pi_lock dance.@task->pi_blocked_on is NULL
  1032. * and we have no waiters to enqueue in @task
  1033. * pi waiters tree.
  1034. */
  1035. goto takeit;
  1036. }
  1037. }
  1038. /*
  1039. * Clear @task->pi_blocked_on. Requires protection by
  1040. * @task->pi_lock. Redundant operation for the @waiter == NULL
  1041. * case, but conditionals are more expensive than a redundant
  1042. * store.
  1043. */
  1044. raw_spin_lock(&task->pi_lock);
  1045. task->pi_blocked_on = NULL;
  1046. /*
  1047. * Finish the lock acquisition. @task is the new owner. If
  1048. * other waiters exist we have to insert the highest priority
  1049. * waiter into @task->pi_waiters tree.
  1050. */
  1051. if (rt_mutex_has_waiters(lock))
  1052. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  1053. raw_spin_unlock(&task->pi_lock);
  1054. takeit:
  1055. /*
  1056. * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
  1057. * are still waiters or clears it.
  1058. */
  1059. rt_mutex_set_owner(lock, task);
  1060. return 1;
  1061. }
  1062. /*
  1063. * Task blocks on lock.
  1064. *
  1065. * Prepare waiter and propagate pi chain
  1066. *
  1067. * This must be called with lock->wait_lock held and interrupts disabled
  1068. */
  1069. static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock,
  1070. struct rt_mutex_waiter *waiter,
  1071. struct task_struct *task,
  1072. struct ww_acquire_ctx *ww_ctx,
  1073. enum rtmutex_chainwalk chwalk)
  1074. {
  1075. struct task_struct *owner = rt_mutex_owner(lock);
  1076. struct rt_mutex_waiter *top_waiter = waiter;
  1077. struct rt_mutex_base *next_lock;
  1078. int chain_walk = 0, res;
  1079. lockdep_assert_held(&lock->wait_lock);
  1080. /*
  1081. * Early deadlock detection. We really don't want the task to
  1082. * enqueue on itself just to untangle the mess later. It's not
  1083. * only an optimization. We drop the locks, so another waiter
  1084. * can come in before the chain walk detects the deadlock. So
  1085. * the other will detect the deadlock and return -EDEADLOCK,
  1086. * which is wrong, as the other waiter is not in a deadlock
  1087. * situation.
  1088. *
  1089. * Except for ww_mutex, in that case the chain walk must already deal
  1090. * with spurious cycles, see the comments at [3] and [6].
  1091. */
  1092. if (owner == task && !(build_ww_mutex() && ww_ctx))
  1093. return -EDEADLK;
  1094. raw_spin_lock(&task->pi_lock);
  1095. waiter->task = task;
  1096. waiter->lock = lock;
  1097. waiter_update_prio(waiter, task);
  1098. waiter_clone_prio(waiter, task);
  1099. /* Get the top priority waiter on the lock */
  1100. if (rt_mutex_has_waiters(lock))
  1101. top_waiter = rt_mutex_top_waiter(lock);
  1102. rt_mutex_enqueue(lock, waiter);
  1103. task->pi_blocked_on = waiter;
  1104. raw_spin_unlock(&task->pi_lock);
  1105. if (build_ww_mutex() && ww_ctx) {
  1106. struct rt_mutex *rtm;
  1107. /* Check whether the waiter should back out immediately */
  1108. rtm = container_of(lock, struct rt_mutex, rtmutex);
  1109. res = __ww_mutex_add_waiter(waiter, rtm, ww_ctx);
  1110. if (res) {
  1111. raw_spin_lock(&task->pi_lock);
  1112. rt_mutex_dequeue(lock, waiter);
  1113. task->pi_blocked_on = NULL;
  1114. raw_spin_unlock(&task->pi_lock);
  1115. return res;
  1116. }
  1117. }
  1118. if (!owner)
  1119. return 0;
  1120. raw_spin_lock(&owner->pi_lock);
  1121. if (waiter == rt_mutex_top_waiter(lock)) {
  1122. rt_mutex_dequeue_pi(owner, top_waiter);
  1123. rt_mutex_enqueue_pi(owner, waiter);
  1124. rt_mutex_adjust_prio(lock, owner);
  1125. if (owner->pi_blocked_on)
  1126. chain_walk = 1;
  1127. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  1128. chain_walk = 1;
  1129. }
  1130. /* Store the lock on which owner is blocked or NULL */
  1131. next_lock = task_blocked_on_lock(owner);
  1132. raw_spin_unlock(&owner->pi_lock);
  1133. /*
  1134. * Even if full deadlock detection is on, if the owner is not
  1135. * blocked itself, we can avoid finding this out in the chain
  1136. * walk.
  1137. */
  1138. if (!chain_walk || !next_lock)
  1139. return 0;
  1140. /*
  1141. * The owner can't disappear while holding a lock,
  1142. * so the owner struct is protected by wait_lock.
  1143. * Gets dropped in rt_mutex_adjust_prio_chain()!
  1144. */
  1145. get_task_struct(owner);
  1146. raw_spin_unlock_irq(&lock->wait_lock);
  1147. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  1148. next_lock, waiter, task);
  1149. raw_spin_lock_irq(&lock->wait_lock);
  1150. return res;
  1151. }
  1152. /*
  1153. * Remove the top waiter from the current tasks pi waiter tree and
  1154. * queue it up.
  1155. *
  1156. * Called with lock->wait_lock held and interrupts disabled.
  1157. */
  1158. static void __sched mark_wakeup_next_waiter(struct rt_wake_q_head *wqh,
  1159. struct rt_mutex_base *lock)
  1160. {
  1161. struct rt_mutex_waiter *waiter;
  1162. lockdep_assert_held(&lock->wait_lock);
  1163. raw_spin_lock(&current->pi_lock);
  1164. waiter = rt_mutex_top_waiter(lock);
  1165. /*
  1166. * Remove it from current->pi_waiters and deboost.
  1167. *
  1168. * We must in fact deboost here in order to ensure we call
  1169. * rt_mutex_setprio() to update p->pi_top_task before the
  1170. * task unblocks.
  1171. */
  1172. rt_mutex_dequeue_pi(current, waiter);
  1173. rt_mutex_adjust_prio(lock, current);
  1174. /*
  1175. * As we are waking up the top waiter, and the waiter stays
  1176. * queued on the lock until it gets the lock, this lock
  1177. * obviously has waiters. Just set the bit here and this has
  1178. * the added benefit of forcing all new tasks into the
  1179. * slow path making sure no task of lower priority than
  1180. * the top waiter can steal this lock.
  1181. */
  1182. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  1183. /*
  1184. * We deboosted before waking the top waiter task such that we don't
  1185. * run two tasks with the 'same' priority (and ensure the
  1186. * p->pi_top_task pointer points to a blocked task). This however can
  1187. * lead to priority inversion if we would get preempted after the
  1188. * deboost but before waking our donor task, hence the preempt_disable()
  1189. * before unlock.
  1190. *
  1191. * Pairs with preempt_enable() in rt_mutex_wake_up_q();
  1192. */
  1193. preempt_disable();
  1194. rt_mutex_wake_q_add(wqh, waiter);
  1195. raw_spin_unlock(&current->pi_lock);
  1196. }
  1197. static int __sched __rt_mutex_slowtrylock(struct rt_mutex_base *lock)
  1198. {
  1199. int ret = try_to_take_rt_mutex(lock, current, NULL);
  1200. /*
  1201. * try_to_take_rt_mutex() sets the lock waiters bit
  1202. * unconditionally. Clean this up.
  1203. */
  1204. fixup_rt_mutex_waiters(lock, true);
  1205. return ret;
  1206. }
  1207. /*
  1208. * Slow path try-lock function:
  1209. */
  1210. static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock)
  1211. {
  1212. unsigned long flags;
  1213. int ret;
  1214. /*
  1215. * If the lock already has an owner we fail to get the lock.
  1216. * This can be done without taking the @lock->wait_lock as
  1217. * it is only being read, and this is a trylock anyway.
  1218. */
  1219. if (rt_mutex_owner(lock))
  1220. return 0;
  1221. /*
  1222. * The mutex has currently no owner. Lock the wait lock and try to
  1223. * acquire the lock. We use irqsave here to support early boot calls.
  1224. */
  1225. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1226. ret = __rt_mutex_slowtrylock(lock);
  1227. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1228. return ret;
  1229. }
  1230. static __always_inline int __rt_mutex_trylock(struct rt_mutex_base *lock)
  1231. {
  1232. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current)))
  1233. return 1;
  1234. return rt_mutex_slowtrylock(lock);
  1235. }
  1236. /*
  1237. * Slow path to release a rt-mutex.
  1238. */
  1239. static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock)
  1240. {
  1241. DEFINE_RT_WAKE_Q(wqh);
  1242. unsigned long flags;
  1243. /* irqsave required to support early boot calls */
  1244. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1245. debug_rt_mutex_unlock(lock);
  1246. /*
  1247. * We must be careful here if the fast path is enabled. If we
  1248. * have no waiters queued we cannot set owner to NULL here
  1249. * because of:
  1250. *
  1251. * foo->lock->owner = NULL;
  1252. * rtmutex_lock(foo->lock); <- fast path
  1253. * free = atomic_dec_and_test(foo->refcnt);
  1254. * rtmutex_unlock(foo->lock); <- fast path
  1255. * if (free)
  1256. * kfree(foo);
  1257. * raw_spin_unlock(foo->lock->wait_lock);
  1258. *
  1259. * So for the fastpath enabled kernel:
  1260. *
  1261. * Nothing can set the waiters bit as long as we hold
  1262. * lock->wait_lock. So we do the following sequence:
  1263. *
  1264. * owner = rt_mutex_owner(lock);
  1265. * clear_rt_mutex_waiters(lock);
  1266. * raw_spin_unlock(&lock->wait_lock);
  1267. * if (cmpxchg(&lock->owner, owner, 0) == owner)
  1268. * return;
  1269. * goto retry;
  1270. *
  1271. * The fastpath disabled variant is simple as all access to
  1272. * lock->owner is serialized by lock->wait_lock:
  1273. *
  1274. * lock->owner = NULL;
  1275. * raw_spin_unlock(&lock->wait_lock);
  1276. */
  1277. while (!rt_mutex_has_waiters(lock)) {
  1278. /* Drops lock->wait_lock ! */
  1279. if (unlock_rt_mutex_safe(lock, flags) == true)
  1280. return;
  1281. /* Relock the rtmutex and try again */
  1282. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1283. }
  1284. /*
  1285. * The wakeup next waiter path does not suffer from the above
  1286. * race. See the comments there.
  1287. *
  1288. * Queue the next waiter for wakeup once we release the wait_lock.
  1289. */
  1290. mark_wakeup_next_waiter(&wqh, lock);
  1291. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1292. rt_mutex_wake_up_q(&wqh);
  1293. }
  1294. static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock)
  1295. {
  1296. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL)))
  1297. return;
  1298. rt_mutex_slowunlock(lock);
  1299. }
  1300. #ifdef CONFIG_SMP
  1301. static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
  1302. struct rt_mutex_waiter *waiter,
  1303. struct task_struct *owner)
  1304. {
  1305. bool res = true;
  1306. rcu_read_lock();
  1307. for (;;) {
  1308. /* If owner changed, trylock again. */
  1309. if (owner != rt_mutex_owner(lock))
  1310. break;
  1311. /*
  1312. * Ensure that @owner is dereferenced after checking that
  1313. * the lock owner still matches @owner. If that fails,
  1314. * @owner might point to freed memory. If it still matches,
  1315. * the rcu_read_lock() ensures the memory stays valid.
  1316. */
  1317. barrier();
  1318. /*
  1319. * Stop spinning when:
  1320. * - the lock owner has been scheduled out
  1321. * - current is not longer the top waiter
  1322. * - current is requested to reschedule (redundant
  1323. * for CONFIG_PREEMPT_RCU=y)
  1324. * - the VCPU on which owner runs is preempted
  1325. */
  1326. if (!owner_on_cpu(owner) || need_resched() ||
  1327. !rt_mutex_waiter_is_top_waiter(lock, waiter)) {
  1328. res = false;
  1329. break;
  1330. }
  1331. cpu_relax();
  1332. }
  1333. rcu_read_unlock();
  1334. return res;
  1335. }
  1336. #else
  1337. static bool rtmutex_spin_on_owner(struct rt_mutex_base *lock,
  1338. struct rt_mutex_waiter *waiter,
  1339. struct task_struct *owner)
  1340. {
  1341. return false;
  1342. }
  1343. #endif
  1344. #ifdef RT_MUTEX_BUILD_MUTEX
  1345. /*
  1346. * Functions required for:
  1347. * - rtmutex, futex on all kernels
  1348. * - mutex and rwsem substitutions on RT kernels
  1349. */
  1350. /*
  1351. * Remove a waiter from a lock and give up
  1352. *
  1353. * Must be called with lock->wait_lock held and interrupts disabled. It must
  1354. * have just failed to try_to_take_rt_mutex().
  1355. */
  1356. static void __sched remove_waiter(struct rt_mutex_base *lock,
  1357. struct rt_mutex_waiter *waiter)
  1358. {
  1359. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  1360. struct task_struct *owner = rt_mutex_owner(lock);
  1361. struct rt_mutex_base *next_lock;
  1362. lockdep_assert_held(&lock->wait_lock);
  1363. raw_spin_lock(&current->pi_lock);
  1364. rt_mutex_dequeue(lock, waiter);
  1365. current->pi_blocked_on = NULL;
  1366. raw_spin_unlock(&current->pi_lock);
  1367. /*
  1368. * Only update priority if the waiter was the highest priority
  1369. * waiter of the lock and there is an owner to update.
  1370. */
  1371. if (!owner || !is_top_waiter)
  1372. return;
  1373. raw_spin_lock(&owner->pi_lock);
  1374. rt_mutex_dequeue_pi(owner, waiter);
  1375. if (rt_mutex_has_waiters(lock))
  1376. rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
  1377. rt_mutex_adjust_prio(lock, owner);
  1378. /* Store the lock on which owner is blocked or NULL */
  1379. next_lock = task_blocked_on_lock(owner);
  1380. raw_spin_unlock(&owner->pi_lock);
  1381. /*
  1382. * Don't walk the chain, if the owner task is not blocked
  1383. * itself.
  1384. */
  1385. if (!next_lock)
  1386. return;
  1387. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  1388. get_task_struct(owner);
  1389. raw_spin_unlock_irq(&lock->wait_lock);
  1390. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  1391. next_lock, NULL, current);
  1392. raw_spin_lock_irq(&lock->wait_lock);
  1393. }
  1394. /**
  1395. * rt_mutex_slowlock_block() - Perform the wait-wake-try-to-take loop
  1396. * @lock: the rt_mutex to take
  1397. * @ww_ctx: WW mutex context pointer
  1398. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  1399. * or TASK_UNINTERRUPTIBLE)
  1400. * @timeout: the pre-initialized and started timer, or NULL for none
  1401. * @waiter: the pre-initialized rt_mutex_waiter
  1402. *
  1403. * Must be called with lock->wait_lock held and interrupts disabled
  1404. */
  1405. static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock,
  1406. struct ww_acquire_ctx *ww_ctx,
  1407. unsigned int state,
  1408. struct hrtimer_sleeper *timeout,
  1409. struct rt_mutex_waiter *waiter)
  1410. {
  1411. struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
  1412. struct task_struct *owner;
  1413. int ret = 0;
  1414. for (;;) {
  1415. /* Try to acquire the lock: */
  1416. if (try_to_take_rt_mutex(lock, current, waiter))
  1417. break;
  1418. if (timeout && !timeout->task) {
  1419. ret = -ETIMEDOUT;
  1420. break;
  1421. }
  1422. if (signal_pending_state(state, current)) {
  1423. ret = -EINTR;
  1424. break;
  1425. }
  1426. if (build_ww_mutex() && ww_ctx) {
  1427. ret = __ww_mutex_check_kill(rtm, waiter, ww_ctx);
  1428. if (ret)
  1429. break;
  1430. }
  1431. if (waiter == rt_mutex_top_waiter(lock))
  1432. owner = rt_mutex_owner(lock);
  1433. else
  1434. owner = NULL;
  1435. raw_spin_unlock_irq(&lock->wait_lock);
  1436. if (!owner || !rtmutex_spin_on_owner(lock, waiter, owner))
  1437. rt_mutex_schedule();
  1438. raw_spin_lock_irq(&lock->wait_lock);
  1439. set_current_state(state);
  1440. }
  1441. __set_current_state(TASK_RUNNING);
  1442. return ret;
  1443. }
  1444. static void __sched rt_mutex_handle_deadlock(int res, int detect_deadlock,
  1445. struct rt_mutex_base *lock,
  1446. struct rt_mutex_waiter *w)
  1447. {
  1448. /*
  1449. * If the result is not -EDEADLOCK or the caller requested
  1450. * deadlock detection, nothing to do here.
  1451. */
  1452. if (res != -EDEADLOCK || detect_deadlock)
  1453. return;
  1454. if (build_ww_mutex() && w->ww_ctx)
  1455. return;
  1456. raw_spin_unlock_irq(&lock->wait_lock);
  1457. WARN(1, "rtmutex deadlock detected\n");
  1458. while (1) {
  1459. set_current_state(TASK_INTERRUPTIBLE);
  1460. rt_mutex_schedule();
  1461. }
  1462. }
  1463. /**
  1464. * __rt_mutex_slowlock - Locking slowpath invoked with lock::wait_lock held
  1465. * @lock: The rtmutex to block lock
  1466. * @ww_ctx: WW mutex context pointer
  1467. * @state: The task state for sleeping
  1468. * @chwalk: Indicator whether full or partial chainwalk is requested
  1469. * @waiter: Initializer waiter for blocking
  1470. */
  1471. static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock,
  1472. struct ww_acquire_ctx *ww_ctx,
  1473. unsigned int state,
  1474. enum rtmutex_chainwalk chwalk,
  1475. struct rt_mutex_waiter *waiter)
  1476. {
  1477. struct rt_mutex *rtm = container_of(lock, struct rt_mutex, rtmutex);
  1478. struct ww_mutex *ww = ww_container_of(rtm);
  1479. int ret;
  1480. lockdep_assert_held(&lock->wait_lock);
  1481. /* Try to acquire the lock again: */
  1482. if (try_to_take_rt_mutex(lock, current, NULL)) {
  1483. if (build_ww_mutex() && ww_ctx) {
  1484. __ww_mutex_check_waiters(rtm, ww_ctx);
  1485. ww_mutex_lock_acquired(ww, ww_ctx);
  1486. }
  1487. return 0;
  1488. }
  1489. set_current_state(state);
  1490. trace_contention_begin(lock, LCB_F_RT);
  1491. ret = task_blocks_on_rt_mutex(lock, waiter, current, ww_ctx, chwalk);
  1492. if (likely(!ret))
  1493. ret = rt_mutex_slowlock_block(lock, ww_ctx, state, NULL, waiter);
  1494. if (likely(!ret)) {
  1495. /* acquired the lock */
  1496. if (build_ww_mutex() && ww_ctx) {
  1497. if (!ww_ctx->is_wait_die)
  1498. __ww_mutex_check_waiters(rtm, ww_ctx);
  1499. ww_mutex_lock_acquired(ww, ww_ctx);
  1500. }
  1501. } else {
  1502. __set_current_state(TASK_RUNNING);
  1503. remove_waiter(lock, waiter);
  1504. rt_mutex_handle_deadlock(ret, chwalk, lock, waiter);
  1505. }
  1506. /*
  1507. * try_to_take_rt_mutex() sets the waiter bit
  1508. * unconditionally. We might have to fix that up.
  1509. */
  1510. fixup_rt_mutex_waiters(lock, true);
  1511. trace_contention_end(lock, ret);
  1512. return ret;
  1513. }
  1514. static inline int __rt_mutex_slowlock_locked(struct rt_mutex_base *lock,
  1515. struct ww_acquire_ctx *ww_ctx,
  1516. unsigned int state)
  1517. {
  1518. struct rt_mutex_waiter waiter;
  1519. int ret;
  1520. rt_mutex_init_waiter(&waiter);
  1521. waiter.ww_ctx = ww_ctx;
  1522. ret = __rt_mutex_slowlock(lock, ww_ctx, state, RT_MUTEX_MIN_CHAINWALK,
  1523. &waiter);
  1524. debug_rt_mutex_free_waiter(&waiter);
  1525. return ret;
  1526. }
  1527. /*
  1528. * rt_mutex_slowlock - Locking slowpath invoked when fast path fails
  1529. * @lock: The rtmutex to block lock
  1530. * @ww_ctx: WW mutex context pointer
  1531. * @state: The task state for sleeping
  1532. */
  1533. static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock,
  1534. struct ww_acquire_ctx *ww_ctx,
  1535. unsigned int state)
  1536. {
  1537. unsigned long flags;
  1538. int ret;
  1539. /*
  1540. * Do all pre-schedule work here, before we queue a waiter and invoke
  1541. * PI -- any such work that trips on rtlock (PREEMPT_RT spinlock) would
  1542. * otherwise recurse back into task_blocks_on_rt_mutex() through
  1543. * rtlock_slowlock() and will then enqueue a second waiter for this
  1544. * same task and things get really confusing real fast.
  1545. */
  1546. rt_mutex_pre_schedule();
  1547. /*
  1548. * Technically we could use raw_spin_[un]lock_irq() here, but this can
  1549. * be called in early boot if the cmpxchg() fast path is disabled
  1550. * (debug, no architecture support). In this case we will acquire the
  1551. * rtmutex with lock->wait_lock held. But we cannot unconditionally
  1552. * enable interrupts in that early boot case. So we need to use the
  1553. * irqsave/restore variants.
  1554. */
  1555. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1556. ret = __rt_mutex_slowlock_locked(lock, ww_ctx, state);
  1557. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1558. rt_mutex_post_schedule();
  1559. return ret;
  1560. }
  1561. static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock,
  1562. unsigned int state)
  1563. {
  1564. lockdep_assert(!current->pi_blocked_on);
  1565. if (likely(rt_mutex_try_acquire(lock)))
  1566. return 0;
  1567. return rt_mutex_slowlock(lock, NULL, state);
  1568. }
  1569. #endif /* RT_MUTEX_BUILD_MUTEX */
  1570. #ifdef RT_MUTEX_BUILD_SPINLOCKS
  1571. /*
  1572. * Functions required for spin/rw_lock substitution on RT kernels
  1573. */
  1574. /**
  1575. * rtlock_slowlock_locked - Slow path lock acquisition for RT locks
  1576. * @lock: The underlying RT mutex
  1577. */
  1578. static void __sched rtlock_slowlock_locked(struct rt_mutex_base *lock)
  1579. {
  1580. struct rt_mutex_waiter waiter;
  1581. struct task_struct *owner;
  1582. lockdep_assert_held(&lock->wait_lock);
  1583. if (try_to_take_rt_mutex(lock, current, NULL))
  1584. return;
  1585. rt_mutex_init_rtlock_waiter(&waiter);
  1586. /* Save current state and set state to TASK_RTLOCK_WAIT */
  1587. current_save_and_set_rtlock_wait_state();
  1588. trace_contention_begin(lock, LCB_F_RT);
  1589. task_blocks_on_rt_mutex(lock, &waiter, current, NULL, RT_MUTEX_MIN_CHAINWALK);
  1590. for (;;) {
  1591. /* Try to acquire the lock again */
  1592. if (try_to_take_rt_mutex(lock, current, &waiter))
  1593. break;
  1594. if (&waiter == rt_mutex_top_waiter(lock))
  1595. owner = rt_mutex_owner(lock);
  1596. else
  1597. owner = NULL;
  1598. raw_spin_unlock_irq(&lock->wait_lock);
  1599. if (!owner || !rtmutex_spin_on_owner(lock, &waiter, owner))
  1600. schedule_rtlock();
  1601. raw_spin_lock_irq(&lock->wait_lock);
  1602. set_current_state(TASK_RTLOCK_WAIT);
  1603. }
  1604. /* Restore the task state */
  1605. current_restore_rtlock_saved_state();
  1606. /*
  1607. * try_to_take_rt_mutex() sets the waiter bit unconditionally.
  1608. * We might have to fix that up:
  1609. */
  1610. fixup_rt_mutex_waiters(lock, true);
  1611. debug_rt_mutex_free_waiter(&waiter);
  1612. trace_contention_end(lock, 0);
  1613. }
  1614. static __always_inline void __sched rtlock_slowlock(struct rt_mutex_base *lock)
  1615. {
  1616. unsigned long flags;
  1617. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1618. rtlock_slowlock_locked(lock);
  1619. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1620. }
  1621. #endif /* RT_MUTEX_BUILD_SPINLOCKS */