wait.c 8.2 KB

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