rwbase_rt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * RT-specific reader/writer semaphores and reader/writer locks
  4. *
  5. * down_write/write_lock()
  6. * 1) Lock rtmutex
  7. * 2) Remove the reader BIAS to force readers into the slow path
  8. * 3) Wait until all readers have left the critical section
  9. * 4) Mark it write locked
  10. *
  11. * up_write/write_unlock()
  12. * 1) Remove the write locked marker
  13. * 2) Set the reader BIAS, so readers can use the fast path again
  14. * 3) Unlock rtmutex, to release blocked readers
  15. *
  16. * down_read/read_lock()
  17. * 1) Try fast path acquisition (reader BIAS is set)
  18. * 2) Take tmutex::wait_lock, which protects the writelocked flag
  19. * 3) If !writelocked, acquire it for read
  20. * 4) If writelocked, block on tmutex
  21. * 5) unlock rtmutex, goto 1)
  22. *
  23. * up_read/read_unlock()
  24. * 1) Try fast path release (reader count != 1)
  25. * 2) Wake the writer waiting in down_write()/write_lock() #3
  26. *
  27. * down_read/read_lock()#3 has the consequence, that rw semaphores and rw
  28. * locks on RT are not writer fair, but writers, which should be avoided in
  29. * RT tasks (think mmap_sem), are subject to the rtmutex priority/DL
  30. * inheritance mechanism.
  31. *
  32. * It's possible to make the rw primitives writer fair by keeping a list of
  33. * active readers. A blocked writer would force all newly incoming readers
  34. * to block on the rtmutex, but the rtmutex would have to be proxy locked
  35. * for one reader after the other. We can't use multi-reader inheritance
  36. * because there is no way to support that with SCHED_DEADLINE.
  37. * Implementing the one by one reader boosting/handover mechanism is a
  38. * major surgery for a very dubious value.
  39. *
  40. * The risk of writer starvation is there, but the pathological use cases
  41. * which trigger it are not necessarily the typical RT workloads.
  42. *
  43. * Fast-path orderings:
  44. * The lock/unlock of readers can run in fast paths: lock and unlock are only
  45. * atomic ops, and there is no inner lock to provide ACQUIRE and RELEASE
  46. * semantics of rwbase_rt. Atomic ops should thus provide _acquire()
  47. * and _release() (or stronger).
  48. *
  49. * Common code shared between RT rw_semaphore and rwlock
  50. */
  51. static __always_inline int rwbase_read_trylock(struct rwbase_rt *rwb)
  52. {
  53. int r;
  54. /*
  55. * Increment reader count, if sem->readers < 0, i.e. READER_BIAS is
  56. * set.
  57. */
  58. for (r = atomic_read(&rwb->readers); r < 0;) {
  59. if (likely(atomic_try_cmpxchg_acquire(&rwb->readers, &r, r + 1)))
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. static int __sched __rwbase_read_lock(struct rwbase_rt *rwb,
  65. unsigned int state)
  66. {
  67. struct rt_mutex_base *rtm = &rwb->rtmutex;
  68. int ret;
  69. rwbase_pre_schedule();
  70. raw_spin_lock_irq(&rtm->wait_lock);
  71. /*
  72. * Call into the slow lock path with the rtmutex->wait_lock
  73. * held, so this can't result in the following race:
  74. *
  75. * Reader1 Reader2 Writer
  76. * down_read()
  77. * down_write()
  78. * rtmutex_lock(m)
  79. * wait()
  80. * down_read()
  81. * unlock(m->wait_lock)
  82. * up_read()
  83. * wake(Writer)
  84. * lock(m->wait_lock)
  85. * sem->writelocked=true
  86. * unlock(m->wait_lock)
  87. *
  88. * up_write()
  89. * sem->writelocked=false
  90. * rtmutex_unlock(m)
  91. * down_read()
  92. * down_write()
  93. * rtmutex_lock(m)
  94. * wait()
  95. * rtmutex_lock(m)
  96. *
  97. * That would put Reader1 behind the writer waiting on
  98. * Reader2 to call up_read(), which might be unbound.
  99. */
  100. trace_contention_begin(rwb, LCB_F_RT | LCB_F_READ);
  101. /*
  102. * For rwlocks this returns 0 unconditionally, so the below
  103. * !ret conditionals are optimized out.
  104. */
  105. ret = rwbase_rtmutex_slowlock_locked(rtm, state);
  106. /*
  107. * On success the rtmutex is held, so there can't be a writer
  108. * active. Increment the reader count and immediately drop the
  109. * rtmutex again.
  110. *
  111. * rtmutex->wait_lock has to be unlocked in any case of course.
  112. */
  113. if (!ret)
  114. atomic_inc(&rwb->readers);
  115. raw_spin_unlock_irq(&rtm->wait_lock);
  116. if (!ret)
  117. rwbase_rtmutex_unlock(rtm);
  118. trace_contention_end(rwb, ret);
  119. rwbase_post_schedule();
  120. return ret;
  121. }
  122. static __always_inline int rwbase_read_lock(struct rwbase_rt *rwb,
  123. unsigned int state)
  124. {
  125. lockdep_assert(!current->pi_blocked_on);
  126. if (rwbase_read_trylock(rwb))
  127. return 0;
  128. return __rwbase_read_lock(rwb, state);
  129. }
  130. static void __sched __rwbase_read_unlock(struct rwbase_rt *rwb,
  131. unsigned int state)
  132. {
  133. struct rt_mutex_base *rtm = &rwb->rtmutex;
  134. struct task_struct *owner;
  135. DEFINE_RT_WAKE_Q(wqh);
  136. raw_spin_lock_irq(&rtm->wait_lock);
  137. /*
  138. * Wake the writer, i.e. the rtmutex owner. It might release the
  139. * rtmutex concurrently in the fast path (due to a signal), but to
  140. * clean up rwb->readers it needs to acquire rtm->wait_lock. The
  141. * worst case which can happen is a spurious wakeup.
  142. */
  143. owner = rt_mutex_owner(rtm);
  144. if (owner)
  145. rt_mutex_wake_q_add_task(&wqh, owner, state);
  146. /* Pairs with the preempt_enable in rt_mutex_wake_up_q() */
  147. preempt_disable();
  148. raw_spin_unlock_irq(&rtm->wait_lock);
  149. rt_mutex_wake_up_q(&wqh);
  150. }
  151. static __always_inline void rwbase_read_unlock(struct rwbase_rt *rwb,
  152. unsigned int state)
  153. {
  154. /*
  155. * rwb->readers can only hit 0 when a writer is waiting for the
  156. * active readers to leave the critical section.
  157. *
  158. * dec_and_test() is fully ordered, provides RELEASE.
  159. */
  160. if (unlikely(atomic_dec_and_test(&rwb->readers)))
  161. __rwbase_read_unlock(rwb, state);
  162. }
  163. static inline void __rwbase_write_unlock(struct rwbase_rt *rwb, int bias,
  164. unsigned long flags)
  165. {
  166. struct rt_mutex_base *rtm = &rwb->rtmutex;
  167. /*
  168. * _release() is needed in case that reader is in fast path, pairing
  169. * with atomic_try_cmpxchg_acquire() in rwbase_read_trylock().
  170. */
  171. (void)atomic_add_return_release(READER_BIAS - bias, &rwb->readers);
  172. raw_spin_unlock_irqrestore(&rtm->wait_lock, flags);
  173. rwbase_rtmutex_unlock(rtm);
  174. }
  175. static inline void rwbase_write_unlock(struct rwbase_rt *rwb)
  176. {
  177. struct rt_mutex_base *rtm = &rwb->rtmutex;
  178. unsigned long flags;
  179. raw_spin_lock_irqsave(&rtm->wait_lock, flags);
  180. __rwbase_write_unlock(rwb, WRITER_BIAS, flags);
  181. }
  182. static inline void rwbase_write_downgrade(struct rwbase_rt *rwb)
  183. {
  184. struct rt_mutex_base *rtm = &rwb->rtmutex;
  185. unsigned long flags;
  186. raw_spin_lock_irqsave(&rtm->wait_lock, flags);
  187. /* Release it and account current as reader */
  188. __rwbase_write_unlock(rwb, WRITER_BIAS - 1, flags);
  189. }
  190. static inline bool __rwbase_write_trylock(struct rwbase_rt *rwb)
  191. {
  192. /* Can do without CAS because we're serialized by wait_lock. */
  193. lockdep_assert_held(&rwb->rtmutex.wait_lock);
  194. /*
  195. * _acquire is needed in case the reader is in the fast path, pairing
  196. * with rwbase_read_unlock(), provides ACQUIRE.
  197. */
  198. if (!atomic_read_acquire(&rwb->readers)) {
  199. atomic_set(&rwb->readers, WRITER_BIAS);
  200. return 1;
  201. }
  202. return 0;
  203. }
  204. static int __sched rwbase_write_lock(struct rwbase_rt *rwb,
  205. unsigned int state)
  206. {
  207. struct rt_mutex_base *rtm = &rwb->rtmutex;
  208. unsigned long flags;
  209. /* Take the rtmutex as a first step */
  210. if (rwbase_rtmutex_lock_state(rtm, state))
  211. return -EINTR;
  212. /* Force readers into slow path */
  213. atomic_sub(READER_BIAS, &rwb->readers);
  214. rwbase_pre_schedule();
  215. raw_spin_lock_irqsave(&rtm->wait_lock, flags);
  216. if (__rwbase_write_trylock(rwb))
  217. goto out_unlock;
  218. rwbase_set_and_save_current_state(state);
  219. trace_contention_begin(rwb, LCB_F_RT | LCB_F_WRITE);
  220. for (;;) {
  221. /* Optimized out for rwlocks */
  222. if (rwbase_signal_pending_state(state, current)) {
  223. rwbase_restore_current_state();
  224. __rwbase_write_unlock(rwb, 0, flags);
  225. rwbase_post_schedule();
  226. trace_contention_end(rwb, -EINTR);
  227. return -EINTR;
  228. }
  229. if (__rwbase_write_trylock(rwb))
  230. break;
  231. raw_spin_unlock_irqrestore(&rtm->wait_lock, flags);
  232. rwbase_schedule();
  233. raw_spin_lock_irqsave(&rtm->wait_lock, flags);
  234. set_current_state(state);
  235. }
  236. rwbase_restore_current_state();
  237. trace_contention_end(rwb, 0);
  238. out_unlock:
  239. raw_spin_unlock_irqrestore(&rtm->wait_lock, flags);
  240. rwbase_post_schedule();
  241. return 0;
  242. }
  243. static inline int rwbase_write_trylock(struct rwbase_rt *rwb)
  244. {
  245. struct rt_mutex_base *rtm = &rwb->rtmutex;
  246. unsigned long flags;
  247. if (!rwbase_rtmutex_trylock(rtm))
  248. return 0;
  249. atomic_sub(READER_BIAS, &rwb->readers);
  250. raw_spin_lock_irqsave(&rtm->wait_lock, flags);
  251. if (__rwbase_write_trylock(rwb)) {
  252. raw_spin_unlock_irqrestore(&rtm->wait_lock, flags);
  253. return 1;
  254. }
  255. __rwbase_write_unlock(rwb, 0, flags);
  256. return 0;
  257. }