wait_bit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_WAIT_BIT_H
  3. #define _LINUX_WAIT_BIT_H
  4. /*
  5. * Linux wait-bit related types and methods:
  6. */
  7. #include <linux/wait.h>
  8. struct wait_bit_key {
  9. void *flags;
  10. int bit_nr;
  11. unsigned long timeout;
  12. };
  13. struct wait_bit_queue_entry {
  14. struct wait_bit_key key;
  15. struct wait_queue_entry wq_entry;
  16. };
  17. #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
  18. { .flags = word, .bit_nr = bit, }
  19. typedef int wait_bit_action_f(struct wait_bit_key *key, int mode);
  20. void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit);
  21. int __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
  22. int __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
  23. void wake_up_bit(void *word, int bit);
  24. int out_of_line_wait_on_bit(void *word, int, wait_bit_action_f *action, unsigned int mode);
  25. int out_of_line_wait_on_bit_timeout(void *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout);
  26. int out_of_line_wait_on_bit_lock(void *word, int, wait_bit_action_f *action, unsigned int mode);
  27. struct wait_queue_head *bit_waitqueue(void *word, int bit);
  28. extern void __init wait_bit_init(void);
  29. int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
  30. #define DEFINE_WAIT_BIT(name, word, bit) \
  31. struct wait_bit_queue_entry name = { \
  32. .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
  33. .wq_entry = { \
  34. .private = current, \
  35. .func = wake_bit_function, \
  36. .entry = \
  37. LIST_HEAD_INIT((name).wq_entry.entry), \
  38. }, \
  39. }
  40. extern int bit_wait(struct wait_bit_key *key, int mode);
  41. extern int bit_wait_io(struct wait_bit_key *key, int mode);
  42. extern int bit_wait_timeout(struct wait_bit_key *key, int mode);
  43. extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode);
  44. /**
  45. * wait_on_bit - wait for a bit to be cleared
  46. * @word: the word being waited on, a kernel virtual address
  47. * @bit: the bit of the word being waited on
  48. * @mode: the task state to sleep in
  49. *
  50. * There is a standard hashed waitqueue table for generic use. This
  51. * is the part of the hashtable's accessor API that waits on a bit.
  52. * For instance, if one were to have waiters on a bitflag, one would
  53. * call wait_on_bit() in threads waiting for the bit to clear.
  54. * One uses wait_on_bit() where one is waiting for the bit to clear,
  55. * but has no intention of setting it.
  56. * Returned value will be zero if the bit was cleared, or non-zero
  57. * if the process received a signal and the mode permitted wakeup
  58. * on that signal.
  59. */
  60. static inline int
  61. wait_on_bit(unsigned long *word, int bit, unsigned mode)
  62. {
  63. might_sleep();
  64. if (!test_bit_acquire(bit, word))
  65. return 0;
  66. return out_of_line_wait_on_bit(word, bit,
  67. bit_wait,
  68. mode);
  69. }
  70. /**
  71. * wait_on_bit_io - wait for a bit to be cleared
  72. * @word: the word being waited on, a kernel virtual address
  73. * @bit: the bit of the word being waited on
  74. * @mode: the task state to sleep in
  75. *
  76. * Use the standard hashed waitqueue table to wait for a bit
  77. * to be cleared. This is similar to wait_on_bit(), but calls
  78. * io_schedule() instead of schedule() for the actual waiting.
  79. *
  80. * Returned value will be zero if the bit was cleared, or non-zero
  81. * if the process received a signal and the mode permitted wakeup
  82. * on that signal.
  83. */
  84. static inline int
  85. wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
  86. {
  87. might_sleep();
  88. if (!test_bit_acquire(bit, word))
  89. return 0;
  90. return out_of_line_wait_on_bit(word, bit,
  91. bit_wait_io,
  92. mode);
  93. }
  94. /**
  95. * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
  96. * @word: the word being waited on, a kernel virtual address
  97. * @bit: the bit of the word being waited on
  98. * @mode: the task state to sleep in
  99. * @timeout: timeout, in jiffies
  100. *
  101. * Use the standard hashed waitqueue table to wait for a bit
  102. * to be cleared. This is similar to wait_on_bit(), except also takes a
  103. * timeout parameter.
  104. *
  105. * Returned value will be zero if the bit was cleared before the
  106. * @timeout elapsed, or non-zero if the @timeout elapsed or process
  107. * received a signal and the mode permitted wakeup on that signal.
  108. */
  109. static inline int
  110. wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
  111. unsigned long timeout)
  112. {
  113. might_sleep();
  114. if (!test_bit_acquire(bit, word))
  115. return 0;
  116. return out_of_line_wait_on_bit_timeout(word, bit,
  117. bit_wait_timeout,
  118. mode, timeout);
  119. }
  120. /**
  121. * wait_on_bit_action - wait for a bit to be cleared
  122. * @word: the word being waited on, a kernel virtual address
  123. * @bit: the bit of the word being waited on
  124. * @action: the function used to sleep, which may take special actions
  125. * @mode: the task state to sleep in
  126. *
  127. * Use the standard hashed waitqueue table to wait for a bit
  128. * to be cleared, and allow the waiting action to be specified.
  129. * This is like wait_on_bit() but allows fine control of how the waiting
  130. * is done.
  131. *
  132. * Returned value will be zero if the bit was cleared, or non-zero
  133. * if the process received a signal and the mode permitted wakeup
  134. * on that signal.
  135. */
  136. static inline int
  137. wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
  138. unsigned mode)
  139. {
  140. might_sleep();
  141. if (!test_bit_acquire(bit, word))
  142. return 0;
  143. return out_of_line_wait_on_bit(word, bit, action, mode);
  144. }
  145. /**
  146. * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
  147. * @word: the word being waited on, a kernel virtual address
  148. * @bit: the bit of the word being waited on
  149. * @mode: the task state to sleep in
  150. *
  151. * There is a standard hashed waitqueue table for generic use. This
  152. * is the part of the hashtable's accessor API that waits on a bit
  153. * when one intends to set it, for instance, trying to lock bitflags.
  154. * For instance, if one were to have waiters trying to set bitflag
  155. * and waiting for it to clear before setting it, one would call
  156. * wait_on_bit() in threads waiting to be able to set the bit.
  157. * One uses wait_on_bit_lock() where one is waiting for the bit to
  158. * clear with the intention of setting it, and when done, clearing it.
  159. *
  160. * Returns zero if the bit was (eventually) found to be clear and was
  161. * set. Returns non-zero if a signal was delivered to the process and
  162. * the @mode allows that signal to wake the process.
  163. */
  164. static inline int
  165. wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
  166. {
  167. might_sleep();
  168. if (!test_and_set_bit(bit, word))
  169. return 0;
  170. return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
  171. }
  172. /**
  173. * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
  174. * @word: the word being waited on, a kernel virtual address
  175. * @bit: the bit of the word being waited on
  176. * @mode: the task state to sleep in
  177. *
  178. * Use the standard hashed waitqueue table to wait for a bit
  179. * to be cleared and then to atomically set it. This is similar
  180. * to wait_on_bit(), but calls io_schedule() instead of schedule()
  181. * for the actual waiting.
  182. *
  183. * Returns zero if the bit was (eventually) found to be clear and was
  184. * set. Returns non-zero if a signal was delivered to the process and
  185. * the @mode allows that signal to wake the process.
  186. */
  187. static inline int
  188. wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
  189. {
  190. might_sleep();
  191. if (!test_and_set_bit(bit, word))
  192. return 0;
  193. return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
  194. }
  195. /**
  196. * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
  197. * @word: the word being waited on, a kernel virtual address
  198. * @bit: the bit of the word being waited on
  199. * @action: the function used to sleep, which may take special actions
  200. * @mode: the task state to sleep in
  201. *
  202. * Use the standard hashed waitqueue table to wait for a bit
  203. * to be cleared and then to set it, and allow the waiting action
  204. * to be specified.
  205. * This is like wait_on_bit() but allows fine control of how the waiting
  206. * is done.
  207. *
  208. * Returns zero if the bit was (eventually) found to be clear and was
  209. * set. Returns non-zero if a signal was delivered to the process and
  210. * the @mode allows that signal to wake the process.
  211. */
  212. static inline int
  213. wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
  214. unsigned mode)
  215. {
  216. might_sleep();
  217. if (!test_and_set_bit(bit, word))
  218. return 0;
  219. return out_of_line_wait_on_bit_lock(word, bit, action, mode);
  220. }
  221. extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags);
  222. extern void wake_up_var(void *var);
  223. extern wait_queue_head_t *__var_waitqueue(void *p);
  224. #define ___wait_var_event(var, condition, state, exclusive, ret, cmd) \
  225. ({ \
  226. __label__ __out; \
  227. struct wait_queue_head *__wq_head = __var_waitqueue(var); \
  228. struct wait_bit_queue_entry __wbq_entry; \
  229. long __ret = ret; /* explicit shadow */ \
  230. \
  231. init_wait_var_entry(&__wbq_entry, var, \
  232. exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
  233. for (;;) { \
  234. long __int = prepare_to_wait_event(__wq_head, \
  235. &__wbq_entry.wq_entry, \
  236. state); \
  237. if (condition) \
  238. break; \
  239. \
  240. if (___wait_is_interruptible(state) && __int) { \
  241. __ret = __int; \
  242. goto __out; \
  243. } \
  244. \
  245. cmd; \
  246. } \
  247. finish_wait(__wq_head, &__wbq_entry.wq_entry); \
  248. __out: __ret; \
  249. })
  250. #define __wait_var_event(var, condition) \
  251. ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
  252. schedule())
  253. #define wait_var_event(var, condition) \
  254. do { \
  255. might_sleep(); \
  256. if (condition) \
  257. break; \
  258. __wait_var_event(var, condition); \
  259. } while (0)
  260. #define __wait_var_event_killable(var, condition) \
  261. ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \
  262. schedule())
  263. #define wait_var_event_killable(var, condition) \
  264. ({ \
  265. int __ret = 0; \
  266. might_sleep(); \
  267. if (!(condition)) \
  268. __ret = __wait_var_event_killable(var, condition); \
  269. __ret; \
  270. })
  271. #define __wait_var_event_timeout(var, condition, timeout) \
  272. ___wait_var_event(var, ___wait_cond_timeout(condition), \
  273. TASK_UNINTERRUPTIBLE, 0, timeout, \
  274. __ret = schedule_timeout(__ret))
  275. #define wait_var_event_timeout(var, condition, timeout) \
  276. ({ \
  277. long __ret = timeout; \
  278. might_sleep(); \
  279. if (!___wait_cond_timeout(condition)) \
  280. __ret = __wait_var_event_timeout(var, condition, timeout); \
  281. __ret; \
  282. })
  283. #define __wait_var_event_interruptible(var, condition) \
  284. ___wait_var_event(var, condition, TASK_INTERRUPTIBLE, 0, 0, \
  285. schedule())
  286. #define wait_var_event_interruptible(var, condition) \
  287. ({ \
  288. int __ret = 0; \
  289. might_sleep(); \
  290. if (!(condition)) \
  291. __ret = __wait_var_event_interruptible(var, condition); \
  292. __ret; \
  293. })
  294. /**
  295. * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
  296. *
  297. * @bit: the bit of the word being waited on
  298. * @word: the word being waited on, a kernel virtual address
  299. *
  300. * You can use this helper if bitflags are manipulated atomically rather than
  301. * non-atomically under a lock.
  302. */
  303. static inline void clear_and_wake_up_bit(int bit, void *word)
  304. {
  305. clear_bit_unlock(bit, word);
  306. /* See wake_up_bit() for which memory barrier you need to use. */
  307. smp_mb__after_atomic();
  308. wake_up_bit(word, bit);
  309. }
  310. #endif /* _LINUX_WAIT_BIT_H */