swait.h 10 KB

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