wait_bit.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * The implementation of the wait_bit*() and related waiting APIs:
  3. */
  4. #include <linux/wait_bit.h>
  5. #include <linux/sched/signal.h>
  6. #include <linux/sched/debug.h>
  7. #include <linux/hash.h>
  8. #define WAIT_TABLE_BITS 8
  9. #define WAIT_TABLE_SIZE (1 << WAIT_TABLE_BITS)
  10. static wait_queue_head_t bit_wait_table[WAIT_TABLE_SIZE] __cacheline_aligned;
  11. wait_queue_head_t *bit_waitqueue(void *word, int bit)
  12. {
  13. const int shift = BITS_PER_LONG == 32 ? 5 : 6;
  14. unsigned long val = (unsigned long)word << shift | bit;
  15. return bit_wait_table + hash_long(val, WAIT_TABLE_BITS);
  16. }
  17. EXPORT_SYMBOL(bit_waitqueue);
  18. int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *arg)
  19. {
  20. struct wait_bit_key *key = arg;
  21. struct wait_bit_queue_entry *wait_bit = container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
  22. if (wait_bit->key.flags != key->flags ||
  23. wait_bit->key.bit_nr != key->bit_nr ||
  24. test_bit(key->bit_nr, key->flags))
  25. return 0;
  26. else
  27. return autoremove_wake_function(wq_entry, mode, sync, key);
  28. }
  29. EXPORT_SYMBOL(wake_bit_function);
  30. /*
  31. * To allow interruptible waiting and asynchronous (i.e. nonblocking)
  32. * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
  33. * permitted return codes. Nonzero return codes halt waiting and return.
  34. */
  35. int __sched
  36. __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
  37. wait_bit_action_f *action, unsigned mode)
  38. {
  39. int ret = 0;
  40. do {
  41. prepare_to_wait(wq_head, &wbq_entry->wq_entry, mode);
  42. if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags))
  43. ret = (*action)(&wbq_entry->key, mode);
  44. } while (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags) && !ret);
  45. finish_wait(wq_head, &wbq_entry->wq_entry);
  46. return ret;
  47. }
  48. EXPORT_SYMBOL(__wait_on_bit);
  49. int __sched out_of_line_wait_on_bit(void *word, int bit,
  50. wait_bit_action_f *action, unsigned mode)
  51. {
  52. struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
  53. DEFINE_WAIT_BIT(wq_entry, word, bit);
  54. return __wait_on_bit(wq_head, &wq_entry, action, mode);
  55. }
  56. EXPORT_SYMBOL(out_of_line_wait_on_bit);
  57. int __sched out_of_line_wait_on_bit_timeout(
  58. void *word, int bit, wait_bit_action_f *action,
  59. unsigned mode, unsigned long timeout)
  60. {
  61. struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
  62. DEFINE_WAIT_BIT(wq_entry, word, bit);
  63. wq_entry.key.timeout = jiffies + timeout;
  64. return __wait_on_bit(wq_head, &wq_entry, action, mode);
  65. }
  66. EXPORT_SYMBOL_GPL(out_of_line_wait_on_bit_timeout);
  67. int __sched
  68. __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
  69. wait_bit_action_f *action, unsigned mode)
  70. {
  71. int ret = 0;
  72. for (;;) {
  73. prepare_to_wait_exclusive(wq_head, &wbq_entry->wq_entry, mode);
  74. if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
  75. ret = action(&wbq_entry->key, mode);
  76. /*
  77. * See the comment in prepare_to_wait_event().
  78. * finish_wait() does not necessarily takes wwq_head->lock,
  79. * but test_and_set_bit() implies mb() which pairs with
  80. * smp_mb__after_atomic() before wake_up_page().
  81. */
  82. if (ret)
  83. finish_wait(wq_head, &wbq_entry->wq_entry);
  84. }
  85. if (!test_and_set_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) {
  86. if (!ret)
  87. finish_wait(wq_head, &wbq_entry->wq_entry);
  88. return 0;
  89. } else if (ret) {
  90. return ret;
  91. }
  92. }
  93. }
  94. EXPORT_SYMBOL(__wait_on_bit_lock);
  95. int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
  96. wait_bit_action_f *action, unsigned mode)
  97. {
  98. struct wait_queue_head *wq_head = bit_waitqueue(word, bit);
  99. DEFINE_WAIT_BIT(wq_entry, word, bit);
  100. return __wait_on_bit_lock(wq_head, &wq_entry, action, mode);
  101. }
  102. EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
  103. void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit)
  104. {
  105. struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
  106. if (waitqueue_active(wq_head))
  107. __wake_up(wq_head, TASK_NORMAL, 1, &key);
  108. }
  109. EXPORT_SYMBOL(__wake_up_bit);
  110. /**
  111. * wake_up_bit - wake up a waiter on a bit
  112. * @word: the word being waited on, a kernel virtual address
  113. * @bit: the bit of the word being waited on
  114. *
  115. * There is a standard hashed waitqueue table for generic use. This
  116. * is the part of the hashtable's accessor API that wakes up waiters
  117. * on a bit. For instance, if one were to have waiters on a bitflag,
  118. * one would call wake_up_bit() after clearing the bit.
  119. *
  120. * In order for this to function properly, as it uses waitqueue_active()
  121. * internally, some kind of memory barrier must be done prior to calling
  122. * this. Typically, this will be smp_mb__after_atomic(), but in some
  123. * cases where bitflags are manipulated non-atomically under a lock, one
  124. * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
  125. * because spin_unlock() does not guarantee a memory barrier.
  126. */
  127. void wake_up_bit(void *word, int bit)
  128. {
  129. __wake_up_bit(bit_waitqueue(word, bit), word, bit);
  130. }
  131. EXPORT_SYMBOL(wake_up_bit);
  132. /*
  133. * Manipulate the atomic_t address to produce a better bit waitqueue table hash
  134. * index (we're keying off bit -1, but that would produce a horrible hash
  135. * value).
  136. */
  137. static inline wait_queue_head_t *atomic_t_waitqueue(atomic_t *p)
  138. {
  139. if (BITS_PER_LONG == 64) {
  140. unsigned long q = (unsigned long)p;
  141. return bit_waitqueue((void *)(q & ~1), q & 1);
  142. }
  143. return bit_waitqueue(p, 0);
  144. }
  145. static int wake_atomic_t_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync,
  146. void *arg)
  147. {
  148. struct wait_bit_key *key = arg;
  149. struct wait_bit_queue_entry *wait_bit = container_of(wq_entry, struct wait_bit_queue_entry, wq_entry);
  150. atomic_t *val = key->flags;
  151. if (wait_bit->key.flags != key->flags ||
  152. wait_bit->key.bit_nr != key->bit_nr ||
  153. atomic_read(val) != 0)
  154. return 0;
  155. return autoremove_wake_function(wq_entry, mode, sync, key);
  156. }
  157. /*
  158. * To allow interruptible waiting and asynchronous (i.e. nonblocking) waiting,
  159. * the actions of __wait_on_atomic_t() are permitted return codes. Nonzero
  160. * return codes halt waiting and return.
  161. */
  162. static __sched
  163. int __wait_on_atomic_t(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry,
  164. int (*action)(atomic_t *), unsigned mode)
  165. {
  166. atomic_t *val;
  167. int ret = 0;
  168. do {
  169. prepare_to_wait(wq_head, &wbq_entry->wq_entry, mode);
  170. val = wbq_entry->key.flags;
  171. if (atomic_read(val) == 0)
  172. break;
  173. ret = (*action)(val);
  174. } while (!ret && atomic_read(val) != 0);
  175. finish_wait(wq_head, &wbq_entry->wq_entry);
  176. return ret;
  177. }
  178. #define DEFINE_WAIT_ATOMIC_T(name, p) \
  179. struct wait_bit_queue_entry name = { \
  180. .key = __WAIT_ATOMIC_T_KEY_INITIALIZER(p), \
  181. .wq_entry = { \
  182. .private = current, \
  183. .func = wake_atomic_t_function, \
  184. .entry = \
  185. LIST_HEAD_INIT((name).wq_entry.entry), \
  186. }, \
  187. }
  188. __sched int out_of_line_wait_on_atomic_t(atomic_t *p, int (*action)(atomic_t *),
  189. unsigned mode)
  190. {
  191. struct wait_queue_head *wq_head = atomic_t_waitqueue(p);
  192. DEFINE_WAIT_ATOMIC_T(wq_entry, p);
  193. return __wait_on_atomic_t(wq_head, &wq_entry, action, mode);
  194. }
  195. EXPORT_SYMBOL(out_of_line_wait_on_atomic_t);
  196. /**
  197. * wake_up_atomic_t - Wake up a waiter on a atomic_t
  198. * @p: The atomic_t being waited on, a kernel virtual address
  199. *
  200. * Wake up anyone waiting for the atomic_t to go to zero.
  201. *
  202. * Abuse the bit-waker function and its waitqueue hash table set (the atomic_t
  203. * check is done by the waiter's wake function, not the by the waker itself).
  204. */
  205. void wake_up_atomic_t(atomic_t *p)
  206. {
  207. __wake_up_bit(atomic_t_waitqueue(p), p, WAIT_ATOMIC_T_BIT_NR);
  208. }
  209. EXPORT_SYMBOL(wake_up_atomic_t);
  210. __sched int bit_wait(struct wait_bit_key *word, int mode)
  211. {
  212. schedule();
  213. if (signal_pending_state(mode, current))
  214. return -EINTR;
  215. return 0;
  216. }
  217. EXPORT_SYMBOL(bit_wait);
  218. __sched int bit_wait_io(struct wait_bit_key *word, int mode)
  219. {
  220. io_schedule();
  221. if (signal_pending_state(mode, current))
  222. return -EINTR;
  223. return 0;
  224. }
  225. EXPORT_SYMBOL(bit_wait_io);
  226. __sched int bit_wait_timeout(struct wait_bit_key *word, int mode)
  227. {
  228. unsigned long now = READ_ONCE(jiffies);
  229. if (time_after_eq(now, word->timeout))
  230. return -EAGAIN;
  231. schedule_timeout(word->timeout - now);
  232. if (signal_pending_state(mode, current))
  233. return -EINTR;
  234. return 0;
  235. }
  236. EXPORT_SYMBOL_GPL(bit_wait_timeout);
  237. __sched int bit_wait_io_timeout(struct wait_bit_key *word, int mode)
  238. {
  239. unsigned long now = READ_ONCE(jiffies);
  240. if (time_after_eq(now, word->timeout))
  241. return -EAGAIN;
  242. io_schedule_timeout(word->timeout - now);
  243. if (signal_pending_state(mode, current))
  244. return -EINTR;
  245. return 0;
  246. }
  247. EXPORT_SYMBOL_GPL(bit_wait_io_timeout);
  248. void __init wait_bit_init(void)
  249. {
  250. int i;
  251. for (i = 0; i < WAIT_TABLE_SIZE; i++)
  252. init_waitqueue_head(bit_wait_table + i);
  253. }