wait.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Generic waiting primitives.
  3. *
  4. * (C) 2004 William Irwin, Oracle
  5. */
  6. #include <linux/init.h>
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/wait.h>
  11. #include <linux/hash.h>
  12. struct lock_class_key waitqueue_lock_key;
  13. EXPORT_SYMBOL(waitqueue_lock_key);
  14. void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
  15. {
  16. unsigned long flags;
  17. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  18. spin_lock_irqsave(&q->lock, flags);
  19. __add_wait_queue(q, wait);
  20. spin_unlock_irqrestore(&q->lock, flags);
  21. }
  22. EXPORT_SYMBOL(add_wait_queue);
  23. void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
  24. {
  25. unsigned long flags;
  26. wait->flags |= WQ_FLAG_EXCLUSIVE;
  27. spin_lock_irqsave(&q->lock, flags);
  28. __add_wait_queue_tail(q, wait);
  29. spin_unlock_irqrestore(&q->lock, flags);
  30. }
  31. EXPORT_SYMBOL(add_wait_queue_exclusive);
  32. void fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
  33. {
  34. unsigned long flags;
  35. spin_lock_irqsave(&q->lock, flags);
  36. __remove_wait_queue(q, wait);
  37. spin_unlock_irqrestore(&q->lock, flags);
  38. }
  39. EXPORT_SYMBOL(remove_wait_queue);
  40. /*
  41. * Note: we use "set_current_state()" _after_ the wait-queue add,
  42. * because we need a memory barrier there on SMP, so that any
  43. * wake-function that tests for the wait-queue being active
  44. * will be guaranteed to see waitqueue addition _or_ subsequent
  45. * tests in this thread will see the wakeup having taken place.
  46. *
  47. * The spin_unlock() itself is semi-permeable and only protects
  48. * one way (it only protects stuff inside the critical region and
  49. * stops them from bleeding out - it would still allow subsequent
  50. * loads to move into the the critical region).
  51. */
  52. void fastcall
  53. prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
  54. {
  55. unsigned long flags;
  56. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  57. spin_lock_irqsave(&q->lock, flags);
  58. if (list_empty(&wait->task_list))
  59. __add_wait_queue(q, wait);
  60. /*
  61. * don't alter the task state if this is just going to
  62. * queue an async wait queue callback
  63. */
  64. if (is_sync_wait(wait))
  65. set_current_state(state);
  66. spin_unlock_irqrestore(&q->lock, flags);
  67. }
  68. EXPORT_SYMBOL(prepare_to_wait);
  69. void fastcall
  70. prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
  71. {
  72. unsigned long flags;
  73. wait->flags |= WQ_FLAG_EXCLUSIVE;
  74. spin_lock_irqsave(&q->lock, flags);
  75. if (list_empty(&wait->task_list))
  76. __add_wait_queue_tail(q, wait);
  77. /*
  78. * don't alter the task state if this is just going to
  79. * queue an async wait queue callback
  80. */
  81. if (is_sync_wait(wait))
  82. set_current_state(state);
  83. spin_unlock_irqrestore(&q->lock, flags);
  84. }
  85. EXPORT_SYMBOL(prepare_to_wait_exclusive);
  86. void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
  87. {
  88. unsigned long flags;
  89. __set_current_state(TASK_RUNNING);
  90. /*
  91. * We can check for list emptiness outside the lock
  92. * IFF:
  93. * - we use the "careful" check that verifies both
  94. * the next and prev pointers, so that there cannot
  95. * be any half-pending updates in progress on other
  96. * CPU's that we haven't seen yet (and that might
  97. * still change the stack area.
  98. * and
  99. * - all other users take the lock (ie we can only
  100. * have _one_ other CPU that looks at or modifies
  101. * the list).
  102. */
  103. if (!list_empty_careful(&wait->task_list)) {
  104. spin_lock_irqsave(&q->lock, flags);
  105. list_del_init(&wait->task_list);
  106. spin_unlock_irqrestore(&q->lock, flags);
  107. }
  108. }
  109. EXPORT_SYMBOL(finish_wait);
  110. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  111. {
  112. int ret = default_wake_function(wait, mode, sync, key);
  113. if (ret)
  114. list_del_init(&wait->task_list);
  115. return ret;
  116. }
  117. EXPORT_SYMBOL(autoremove_wake_function);
  118. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
  119. {
  120. struct wait_bit_key *key = arg;
  121. struct wait_bit_queue *wait_bit
  122. = container_of(wait, struct wait_bit_queue, wait);
  123. if (wait_bit->key.flags != key->flags ||
  124. wait_bit->key.bit_nr != key->bit_nr ||
  125. test_bit(key->bit_nr, key->flags))
  126. return 0;
  127. else
  128. return autoremove_wake_function(wait, mode, sync, key);
  129. }
  130. EXPORT_SYMBOL(wake_bit_function);
  131. /*
  132. * To allow interruptible waiting and asynchronous (i.e. nonblocking)
  133. * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
  134. * permitted return codes. Nonzero return codes halt waiting and return.
  135. */
  136. int __sched fastcall
  137. __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
  138. int (*action)(void *), unsigned mode)
  139. {
  140. int ret = 0;
  141. do {
  142. prepare_to_wait(wq, &q->wait, mode);
  143. if (test_bit(q->key.bit_nr, q->key.flags))
  144. ret = (*action)(q->key.flags);
  145. } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
  146. finish_wait(wq, &q->wait);
  147. return ret;
  148. }
  149. EXPORT_SYMBOL(__wait_on_bit);
  150. int __sched fastcall out_of_line_wait_on_bit(void *word, int bit,
  151. int (*action)(void *), unsigned mode)
  152. {
  153. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  154. DEFINE_WAIT_BIT(wait, word, bit);
  155. return __wait_on_bit(wq, &wait, action, mode);
  156. }
  157. EXPORT_SYMBOL(out_of_line_wait_on_bit);
  158. int __sched fastcall
  159. __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
  160. int (*action)(void *), unsigned mode)
  161. {
  162. int ret = 0;
  163. do {
  164. prepare_to_wait_exclusive(wq, &q->wait, mode);
  165. if (test_bit(q->key.bit_nr, q->key.flags)) {
  166. if ((ret = (*action)(q->key.flags)))
  167. break;
  168. }
  169. } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
  170. finish_wait(wq, &q->wait);
  171. return ret;
  172. }
  173. EXPORT_SYMBOL(__wait_on_bit_lock);
  174. int __sched fastcall out_of_line_wait_on_bit_lock(void *word, int bit,
  175. int (*action)(void *), unsigned mode)
  176. {
  177. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  178. DEFINE_WAIT_BIT(wait, word, bit);
  179. return __wait_on_bit_lock(wq, &wait, action, mode);
  180. }
  181. EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
  182. void fastcall __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
  183. {
  184. struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
  185. if (waitqueue_active(wq))
  186. __wake_up(wq, TASK_INTERRUPTIBLE|TASK_UNINTERRUPTIBLE, 1, &key);
  187. }
  188. EXPORT_SYMBOL(__wake_up_bit);
  189. /**
  190. * wake_up_bit - wake up a waiter on a bit
  191. * @word: the word being waited on, a kernel virtual address
  192. * @bit: the bit of the word being waited on
  193. *
  194. * There is a standard hashed waitqueue table for generic use. This
  195. * is the part of the hashtable's accessor API that wakes up waiters
  196. * on a bit. For instance, if one were to have waiters on a bitflag,
  197. * one would call wake_up_bit() after clearing the bit.
  198. *
  199. * In order for this to function properly, as it uses waitqueue_active()
  200. * internally, some kind of memory barrier must be done prior to calling
  201. * this. Typically, this will be smp_mb__after_clear_bit(), but in some
  202. * cases where bitflags are manipulated non-atomically under a lock, one
  203. * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
  204. * because spin_unlock() does not guarantee a memory barrier.
  205. */
  206. void fastcall wake_up_bit(void *word, int bit)
  207. {
  208. __wake_up_bit(bit_waitqueue(word, bit), word, bit);
  209. }
  210. EXPORT_SYMBOL(wake_up_bit);
  211. fastcall wait_queue_head_t *bit_waitqueue(void *word, int bit)
  212. {
  213. const int shift = BITS_PER_LONG == 32 ? 5 : 6;
  214. const struct zone *zone = page_zone(virt_to_page(word));
  215. unsigned long val = (unsigned long)word << shift | bit;
  216. return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
  217. }
  218. EXPORT_SYMBOL(bit_waitqueue);