swait.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_SWAIT_H
  3. #define _LINUX_SWAIT_H
  4. #include <linux/list.h>
  5. #include <linux/stddef.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/wait.h>
  8. #include <asm/current.h>
  9. /*
  10. * Simple waitqueues are semantically very different to regular wait queues
  11. * (wait.h). The most important difference is that the simple waitqueue allows
  12. * for deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold
  13. * times.
  14. *
  15. * Mainly, this is accomplished by two things. Firstly not allowing swake_up_all
  16. * from IRQ disabled, and dropping the lock upon every wakeup, giving a higher
  17. * priority task a chance to run.
  18. *
  19. * Secondly, we had to drop a fair number of features of the other waitqueue
  20. * code; notably:
  21. *
  22. * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue;
  23. * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right
  24. * sleeper state.
  25. *
  26. * - the !exclusive mode; because that leads to O(n) wakeups, everything is
  27. * exclusive. As such swake_up_one will only ever awake _one_ waiter.
  28. *
  29. * - custom wake callback functions; because you cannot give any guarantees
  30. * about random code. This also allows swait to be used in RT, such that
  31. * raw spinlock can be used for the swait queue head.
  32. *
  33. * As a side effect of these; the data structures are slimmer albeit more ad-hoc.
  34. * For all the above, note that simple wait queues should _only_ be used under
  35. * very specific realtime constraints -- it is best to stick with the regular
  36. * wait queues in most cases.
  37. */
  38. struct task_struct;
  39. struct swait_queue_head {
  40. raw_spinlock_t lock;
  41. struct list_head task_list;
  42. };
  43. struct swait_queue {
  44. struct task_struct *task;
  45. struct list_head task_list;
  46. };
  47. #define __SWAITQUEUE_INITIALIZER(name) { \
  48. .task = current, \
  49. .task_list = LIST_HEAD_INIT((name).task_list), \
  50. }
  51. #define DECLARE_SWAITQUEUE(name) \
  52. struct swait_queue name = __SWAITQUEUE_INITIALIZER(name)
  53. #define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \
  54. .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \
  55. .task_list = LIST_HEAD_INIT((name).task_list), \
  56. }
  57. #define DECLARE_SWAIT_QUEUE_HEAD(name) \
  58. struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name)
  59. extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name,
  60. struct lock_class_key *key);
  61. #define init_swait_queue_head(q) \
  62. do { \
  63. static struct lock_class_key __key; \
  64. __init_swait_queue_head((q), #q, &__key); \
  65. } while (0)
  66. #ifdef CONFIG_LOCKDEP
  67. # define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
  68. ({ init_swait_queue_head(&name); name; })
  69. # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  70. struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name)
  71. #else
  72. # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \
  73. DECLARE_SWAIT_QUEUE_HEAD(name)
  74. #endif
  75. /**
  76. * swait_active -- locklessly test for waiters on the queue
  77. * @wq: the waitqueue to test for waiters
  78. *
  79. * returns true if the wait list is not empty
  80. *
  81. * NOTE: this function is lockless and requires care, incorrect usage _will_
  82. * lead to sporadic and non-obvious failure.
  83. *
  84. * NOTE2: this function has the same above implications as regular waitqueues.
  85. *
  86. * Use either while holding swait_queue_head::lock or when used for wakeups
  87. * with an extra smp_mb() like:
  88. *
  89. * CPU0 - waker CPU1 - waiter
  90. *
  91. * for (;;) {
  92. * @cond = true; prepare_to_swait_exclusive(&wq_head, &wait, state);
  93. * smp_mb(); // smp_mb() from set_current_state()
  94. * if (swait_active(wq_head)) if (@cond)
  95. * wake_up(wq_head); break;
  96. * schedule();
  97. * }
  98. * finish_swait(&wq_head, &wait);
  99. *
  100. * Because without the explicit smp_mb() it's possible for the
  101. * swait_active() load to get hoisted over the @cond store such that we'll
  102. * observe an empty wait list while the waiter might not observe @cond.
  103. * This, in turn, can trigger missing wakeups.
  104. *
  105. * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
  106. * which (when the lock is uncontended) are of roughly equal cost.
  107. */
  108. static inline int swait_active(struct swait_queue_head *wq)
  109. {
  110. return !list_empty(&wq->task_list);
  111. }
  112. /**
  113. * swq_has_sleeper - check if there are any waiting processes
  114. * @wq: the waitqueue to test for waiters
  115. *
  116. * Returns true if @wq has waiting processes
  117. *
  118. * Please refer to the comment for swait_active.
  119. */
  120. static inline bool swq_has_sleeper(struct swait_queue_head *wq)
  121. {
  122. /*
  123. * We need to be sure we are in sync with the list_add()
  124. * modifications to the wait queue (task_list).
  125. *
  126. * This memory barrier should be paired with one on the
  127. * waiting side.
  128. */
  129. smp_mb();
  130. return swait_active(wq);
  131. }
  132. extern void swake_up_one(struct swait_queue_head *q);
  133. extern void swake_up_all(struct swait_queue_head *q);
  134. extern void swake_up_locked(struct swait_queue_head *q);
  135. extern void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state);
  136. extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);
  137. extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  138. extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait);
  139. /* as per ___wait_event() but for swait, therefore "exclusive == 1" */
  140. #define ___swait_event(wq, condition, state, ret, cmd) \
  141. ({ \
  142. __label__ __out; \
  143. struct swait_queue __wait; \
  144. long __ret = ret; \
  145. \
  146. INIT_LIST_HEAD(&__wait.task_list); \
  147. for (;;) { \
  148. long __int = prepare_to_swait_event(&wq, &__wait, state);\
  149. \
  150. if (condition) \
  151. break; \
  152. \
  153. if (___wait_is_interruptible(state) && __int) { \
  154. __ret = __int; \
  155. goto __out; \
  156. } \
  157. \
  158. cmd; \
  159. } \
  160. finish_swait(&wq, &__wait); \
  161. __out: __ret; \
  162. })
  163. #define __swait_event(wq, condition) \
  164. (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
  165. schedule())
  166. #define swait_event_exclusive(wq, condition) \
  167. do { \
  168. if (condition) \
  169. break; \
  170. __swait_event(wq, condition); \
  171. } while (0)
  172. #define __swait_event_timeout(wq, condition, timeout) \
  173. ___swait_event(wq, ___wait_cond_timeout(condition), \
  174. TASK_UNINTERRUPTIBLE, timeout, \
  175. __ret = schedule_timeout(__ret))
  176. #define swait_event_timeout_exclusive(wq, condition, timeout) \
  177. ({ \
  178. long __ret = timeout; \
  179. if (!___wait_cond_timeout(condition)) \
  180. __ret = __swait_event_timeout(wq, condition, timeout); \
  181. __ret; \
  182. })
  183. #define __swait_event_interruptible(wq, condition) \
  184. ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \
  185. schedule())
  186. #define swait_event_interruptible_exclusive(wq, condition) \
  187. ({ \
  188. int __ret = 0; \
  189. if (!(condition)) \
  190. __ret = __swait_event_interruptible(wq, condition); \
  191. __ret; \
  192. })
  193. #define __swait_event_interruptible_timeout(wq, condition, timeout) \
  194. ___swait_event(wq, ___wait_cond_timeout(condition), \
  195. TASK_INTERRUPTIBLE, timeout, \
  196. __ret = schedule_timeout(__ret))
  197. #define swait_event_interruptible_timeout_exclusive(wq, condition, timeout)\
  198. ({ \
  199. long __ret = timeout; \
  200. if (!___wait_cond_timeout(condition)) \
  201. __ret = __swait_event_interruptible_timeout(wq, \
  202. condition, timeout); \
  203. __ret; \
  204. })
  205. #define __swait_event_idle(wq, condition) \
  206. (void)___swait_event(wq, condition, TASK_IDLE, 0, schedule())
  207. /**
  208. * swait_event_idle_exclusive - wait without system load contribution
  209. * @wq: the waitqueue to wait on
  210. * @condition: a C expression for the event to wait for
  211. *
  212. * The process is put to sleep (TASK_IDLE) until the @condition evaluates to
  213. * true. The @condition is checked each time the waitqueue @wq is woken up.
  214. *
  215. * This function is mostly used when a kthread or workqueue waits for some
  216. * condition and doesn't want to contribute to system load. Signals are
  217. * ignored.
  218. */
  219. #define swait_event_idle_exclusive(wq, condition) \
  220. do { \
  221. if (condition) \
  222. break; \
  223. __swait_event_idle(wq, condition); \
  224. } while (0)
  225. #define __swait_event_idle_timeout(wq, condition, timeout) \
  226. ___swait_event(wq, ___wait_cond_timeout(condition), \
  227. TASK_IDLE, timeout, \
  228. __ret = schedule_timeout(__ret))
  229. /**
  230. * swait_event_idle_timeout_exclusive - wait up to timeout without load contribution
  231. * @wq: the waitqueue to wait on
  232. * @condition: a C expression for the event to wait for
  233. * @timeout: timeout at which we'll give up in jiffies
  234. *
  235. * The process is put to sleep (TASK_IDLE) until the @condition evaluates to
  236. * true. The @condition is checked each time the waitqueue @wq is woken up.
  237. *
  238. * This function is mostly used when a kthread or workqueue waits for some
  239. * condition and doesn't want to contribute to system load. Signals are
  240. * ignored.
  241. *
  242. * Returns:
  243. * 0 if the @condition evaluated to %false after the @timeout elapsed,
  244. * 1 if the @condition evaluated to %true after the @timeout elapsed,
  245. * or the remaining jiffies (at least 1) if the @condition evaluated
  246. * to %true before the @timeout elapsed.
  247. */
  248. #define swait_event_idle_timeout_exclusive(wq, condition, timeout) \
  249. ({ \
  250. long __ret = timeout; \
  251. if (!___wait_cond_timeout(condition)) \
  252. __ret = __swait_event_idle_timeout(wq, \
  253. condition, timeout); \
  254. __ret; \
  255. })
  256. #endif /* _LINUX_SWAIT_H */