wait_bit.c 6.8 KB

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