wait.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Generic waiting primitives.
  3. *
  4. * (C) 2004 Nadia Yvette Chambers, Oracle
  5. */
  6. #include <linux/init.h>
  7. #include <linux/export.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, const char *name, struct lock_class_key *key)
  13. {
  14. spin_lock_init(&q->lock);
  15. lockdep_set_class_and_name(&q->lock, key, name);
  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. long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state)
  82. {
  83. unsigned long flags;
  84. if (signal_pending_state(state, current))
  85. return -ERESTARTSYS;
  86. wait->private = current;
  87. wait->func = autoremove_wake_function;
  88. spin_lock_irqsave(&q->lock, flags);
  89. if (list_empty(&wait->task_list)) {
  90. if (wait->flags & WQ_FLAG_EXCLUSIVE)
  91. __add_wait_queue_tail(q, wait);
  92. else
  93. __add_wait_queue(q, wait);
  94. }
  95. set_current_state(state);
  96. spin_unlock_irqrestore(&q->lock, flags);
  97. return 0;
  98. }
  99. EXPORT_SYMBOL(prepare_to_wait_event);
  100. /**
  101. * finish_wait - clean up after waiting in a queue
  102. * @q: waitqueue waited on
  103. * @wait: wait descriptor
  104. *
  105. * Sets current thread back to running state and removes
  106. * the wait descriptor from the given waitqueue if still
  107. * queued.
  108. */
  109. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
  110. {
  111. unsigned long flags;
  112. __set_current_state(TASK_RUNNING);
  113. /*
  114. * We can check for list emptiness outside the lock
  115. * IFF:
  116. * - we use the "careful" check that verifies both
  117. * the next and prev pointers, so that there cannot
  118. * be any half-pending updates in progress on other
  119. * CPU's that we haven't seen yet (and that might
  120. * still change the stack area.
  121. * and
  122. * - all other users take the lock (ie we can only
  123. * have _one_ other CPU that looks at or modifies
  124. * the list).
  125. */
  126. if (!list_empty_careful(&wait->task_list)) {
  127. spin_lock_irqsave(&q->lock, flags);
  128. list_del_init(&wait->task_list);
  129. spin_unlock_irqrestore(&q->lock, flags);
  130. }
  131. }
  132. EXPORT_SYMBOL(finish_wait);
  133. /**
  134. * abort_exclusive_wait - abort exclusive waiting in a queue
  135. * @q: waitqueue waited on
  136. * @wait: wait descriptor
  137. * @mode: runstate of the waiter to be woken
  138. * @key: key to identify a wait bit queue or %NULL
  139. *
  140. * Sets current thread back to running state and removes
  141. * the wait descriptor from the given waitqueue if still
  142. * queued.
  143. *
  144. * Wakes up the next waiter if the caller is concurrently
  145. * woken up through the queue.
  146. *
  147. * This prevents waiter starvation where an exclusive waiter
  148. * aborts and is woken up concurrently and no one wakes up
  149. * the next waiter.
  150. */
  151. void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
  152. unsigned int mode, void *key)
  153. {
  154. unsigned long flags;
  155. __set_current_state(TASK_RUNNING);
  156. spin_lock_irqsave(&q->lock, flags);
  157. if (!list_empty(&wait->task_list))
  158. list_del_init(&wait->task_list);
  159. else if (waitqueue_active(q))
  160. __wake_up_locked_key(q, mode, key);
  161. spin_unlock_irqrestore(&q->lock, flags);
  162. }
  163. EXPORT_SYMBOL(abort_exclusive_wait);
  164. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  165. {
  166. int ret = default_wake_function(wait, mode, sync, key);
  167. if (ret)
  168. list_del_init(&wait->task_list);
  169. return ret;
  170. }
  171. EXPORT_SYMBOL(autoremove_wake_function);
  172. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
  173. {
  174. struct wait_bit_key *key = arg;
  175. struct wait_bit_queue *wait_bit
  176. = container_of(wait, struct wait_bit_queue, wait);
  177. if (wait_bit->key.flags != key->flags ||
  178. wait_bit->key.bit_nr != key->bit_nr ||
  179. test_bit(key->bit_nr, key->flags))
  180. return 0;
  181. else
  182. return autoremove_wake_function(wait, mode, sync, key);
  183. }
  184. EXPORT_SYMBOL(wake_bit_function);
  185. /*
  186. * To allow interruptible waiting and asynchronous (i.e. nonblocking)
  187. * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
  188. * permitted return codes. Nonzero return codes halt waiting and return.
  189. */
  190. int __sched
  191. __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
  192. int (*action)(void *), unsigned mode)
  193. {
  194. int ret = 0;
  195. do {
  196. prepare_to_wait(wq, &q->wait, mode);
  197. if (test_bit(q->key.bit_nr, q->key.flags))
  198. ret = (*action)(q->key.flags);
  199. } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
  200. finish_wait(wq, &q->wait);
  201. return ret;
  202. }
  203. EXPORT_SYMBOL(__wait_on_bit);
  204. int __sched out_of_line_wait_on_bit(void *word, int bit,
  205. int (*action)(void *), unsigned mode)
  206. {
  207. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  208. DEFINE_WAIT_BIT(wait, word, bit);
  209. return __wait_on_bit(wq, &wait, action, mode);
  210. }
  211. EXPORT_SYMBOL(out_of_line_wait_on_bit);
  212. int __sched
  213. __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
  214. int (*action)(void *), unsigned mode)
  215. {
  216. do {
  217. int ret;
  218. prepare_to_wait_exclusive(wq, &q->wait, mode);
  219. if (!test_bit(q->key.bit_nr, q->key.flags))
  220. continue;
  221. ret = action(q->key.flags);
  222. if (!ret)
  223. continue;
  224. abort_exclusive_wait(wq, &q->wait, mode, &q->key);
  225. return ret;
  226. } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
  227. finish_wait(wq, &q->wait);
  228. return 0;
  229. }
  230. EXPORT_SYMBOL(__wait_on_bit_lock);
  231. int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
  232. int (*action)(void *), unsigned mode)
  233. {
  234. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  235. DEFINE_WAIT_BIT(wait, word, bit);
  236. return __wait_on_bit_lock(wq, &wait, action, mode);
  237. }
  238. EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
  239. void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
  240. {
  241. struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
  242. if (waitqueue_active(wq))
  243. __wake_up(wq, TASK_NORMAL, 1, &key);
  244. }
  245. EXPORT_SYMBOL(__wake_up_bit);
  246. /**
  247. * wake_up_bit - wake up a waiter on a bit
  248. * @word: the word being waited on, a kernel virtual address
  249. * @bit: the bit of the word being waited on
  250. *
  251. * There is a standard hashed waitqueue table for generic use. This
  252. * is the part of the hashtable's accessor API that wakes up waiters
  253. * on a bit. For instance, if one were to have waiters on a bitflag,
  254. * one would call wake_up_bit() after clearing the bit.
  255. *
  256. * In order for this to function properly, as it uses waitqueue_active()
  257. * internally, some kind of memory barrier must be done prior to calling
  258. * this. Typically, this will be smp_mb__after_clear_bit(), but in some
  259. * cases where bitflags are manipulated non-atomically under a lock, one
  260. * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
  261. * because spin_unlock() does not guarantee a memory barrier.
  262. */
  263. void wake_up_bit(void *word, int bit)
  264. {
  265. __wake_up_bit(bit_waitqueue(word, bit), word, bit);
  266. }
  267. EXPORT_SYMBOL(wake_up_bit);
  268. wait_queue_head_t *bit_waitqueue(void *word, int bit)
  269. {
  270. const int shift = BITS_PER_LONG == 32 ? 5 : 6;
  271. const struct zone *zone = page_zone(virt_to_page(word));
  272. unsigned long val = (unsigned long)word << shift | bit;
  273. return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
  274. }
  275. EXPORT_SYMBOL(bit_waitqueue);
  276. /*
  277. * Manipulate the atomic_t address to produce a better bit waitqueue table hash
  278. * index (we're keying off bit -1, but that would produce a horrible hash
  279. * value).
  280. */
  281. static inline wait_queue_head_t *atomic_t_waitqueue(atomic_t *p)
  282. {
  283. if (BITS_PER_LONG == 64) {
  284. unsigned long q = (unsigned long)p;
  285. return bit_waitqueue((void *)(q & ~1), q & 1);
  286. }
  287. return bit_waitqueue(p, 0);
  288. }
  289. static int wake_atomic_t_function(wait_queue_t *wait, unsigned mode, int sync,
  290. void *arg)
  291. {
  292. struct wait_bit_key *key = arg;
  293. struct wait_bit_queue *wait_bit
  294. = container_of(wait, struct wait_bit_queue, wait);
  295. atomic_t *val = key->flags;
  296. if (wait_bit->key.flags != key->flags ||
  297. wait_bit->key.bit_nr != key->bit_nr ||
  298. atomic_read(val) != 0)
  299. return 0;
  300. return autoremove_wake_function(wait, mode, sync, key);
  301. }
  302. /*
  303. * To allow interruptible waiting and asynchronous (i.e. nonblocking) waiting,
  304. * the actions of __wait_on_atomic_t() are permitted return codes. Nonzero
  305. * return codes halt waiting and return.
  306. */
  307. static __sched
  308. int __wait_on_atomic_t(wait_queue_head_t *wq, struct wait_bit_queue *q,
  309. int (*action)(atomic_t *), unsigned mode)
  310. {
  311. atomic_t *val;
  312. int ret = 0;
  313. do {
  314. prepare_to_wait(wq, &q->wait, mode);
  315. val = q->key.flags;
  316. if (atomic_read(val) == 0)
  317. break;
  318. ret = (*action)(val);
  319. } while (!ret && atomic_read(val) != 0);
  320. finish_wait(wq, &q->wait);
  321. return ret;
  322. }
  323. #define DEFINE_WAIT_ATOMIC_T(name, p) \
  324. struct wait_bit_queue name = { \
  325. .key = __WAIT_ATOMIC_T_KEY_INITIALIZER(p), \
  326. .wait = { \
  327. .private = current, \
  328. .func = wake_atomic_t_function, \
  329. .task_list = \
  330. LIST_HEAD_INIT((name).wait.task_list), \
  331. }, \
  332. }
  333. __sched int out_of_line_wait_on_atomic_t(atomic_t *p, int (*action)(atomic_t *),
  334. unsigned mode)
  335. {
  336. wait_queue_head_t *wq = atomic_t_waitqueue(p);
  337. DEFINE_WAIT_ATOMIC_T(wait, p);
  338. return __wait_on_atomic_t(wq, &wait, action, mode);
  339. }
  340. EXPORT_SYMBOL(out_of_line_wait_on_atomic_t);
  341. /**
  342. * wake_up_atomic_t - Wake up a waiter on a atomic_t
  343. * @p: The atomic_t being waited on, a kernel virtual address
  344. *
  345. * Wake up anyone waiting for the atomic_t to go to zero.
  346. *
  347. * Abuse the bit-waker function and its waitqueue hash table set (the atomic_t
  348. * check is done by the waiter's wake function, not the by the waker itself).
  349. */
  350. void wake_up_atomic_t(atomic_t *p)
  351. {
  352. __wake_up_bit(atomic_t_waitqueue(p), p, WAIT_ATOMIC_T_BIT_NR);
  353. }
  354. EXPORT_SYMBOL(wake_up_atomic_t);