wait.c 8.1 KB

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