wait.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  47. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  48. * number) then we wake all the non-exclusive tasks and one exclusive task.
  49. *
  50. * There are circumstances in which we can try to wake a task which has already
  51. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  52. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  53. */
  54. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  55. int nr_exclusive, int wake_flags, void *key)
  56. {
  57. wait_queue_t *curr, *next;
  58. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  59. unsigned flags = curr->flags;
  60. if (curr->func(curr, mode, wake_flags, key) &&
  61. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  62. break;
  63. }
  64. }
  65. /**
  66. * __wake_up - wake up threads blocked on a waitqueue.
  67. * @q: the waitqueue
  68. * @mode: which threads
  69. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  70. * @key: is directly passed to the wakeup function
  71. *
  72. * It may be assumed that this function implies a write memory barrier before
  73. * changing the task state if and only if any tasks are woken up.
  74. */
  75. void __wake_up(wait_queue_head_t *q, unsigned int mode,
  76. int nr_exclusive, void *key)
  77. {
  78. unsigned long flags;
  79. spin_lock_irqsave(&q->lock, flags);
  80. __wake_up_common(q, mode, nr_exclusive, 0, key);
  81. spin_unlock_irqrestore(&q->lock, flags);
  82. }
  83. EXPORT_SYMBOL(__wake_up);
  84. /*
  85. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  86. */
  87. void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr)
  88. {
  89. __wake_up_common(q, mode, nr, 0, NULL);
  90. }
  91. EXPORT_SYMBOL_GPL(__wake_up_locked);
  92. void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
  93. {
  94. __wake_up_common(q, mode, 1, 0, key);
  95. }
  96. EXPORT_SYMBOL_GPL(__wake_up_locked_key);
  97. /**
  98. * __wake_up_sync_key - wake up threads blocked on a waitqueue.
  99. * @q: the waitqueue
  100. * @mode: which threads
  101. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  102. * @key: opaque value to be passed to wakeup targets
  103. *
  104. * The sync wakeup differs that the waker knows that it will schedule
  105. * away soon, so while the target thread will be woken up, it will not
  106. * be migrated to another CPU - ie. the two threads are 'synchronized'
  107. * with each other. This can prevent needless bouncing between CPUs.
  108. *
  109. * On UP it can prevent extra preemption.
  110. *
  111. * It may be assumed that this function implies a write memory barrier before
  112. * changing the task state if and only if any tasks are woken up.
  113. */
  114. void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
  115. int nr_exclusive, void *key)
  116. {
  117. unsigned long flags;
  118. int wake_flags = 1; /* XXX WF_SYNC */
  119. if (unlikely(!q))
  120. return;
  121. if (unlikely(nr_exclusive != 1))
  122. wake_flags = 0;
  123. spin_lock_irqsave(&q->lock, flags);
  124. __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
  125. spin_unlock_irqrestore(&q->lock, flags);
  126. }
  127. EXPORT_SYMBOL_GPL(__wake_up_sync_key);
  128. /*
  129. * __wake_up_sync - see __wake_up_sync_key()
  130. */
  131. void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  132. {
  133. __wake_up_sync_key(q, mode, nr_exclusive, NULL);
  134. }
  135. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  136. /*
  137. * Note: we use "set_current_state()" _after_ the wait-queue add,
  138. * because we need a memory barrier there on SMP, so that any
  139. * wake-function that tests for the wait-queue being active
  140. * will be guaranteed to see waitqueue addition _or_ subsequent
  141. * tests in this thread will see the wakeup having taken place.
  142. *
  143. * The spin_unlock() itself is semi-permeable and only protects
  144. * one way (it only protects stuff inside the critical region and
  145. * stops them from bleeding out - it would still allow subsequent
  146. * loads to move into the critical region).
  147. */
  148. void
  149. prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
  150. {
  151. unsigned long flags;
  152. wait->flags &= ~WQ_FLAG_EXCLUSIVE;
  153. spin_lock_irqsave(&q->lock, flags);
  154. if (list_empty(&wait->task_list))
  155. __add_wait_queue(q, wait);
  156. set_current_state(state);
  157. spin_unlock_irqrestore(&q->lock, flags);
  158. }
  159. EXPORT_SYMBOL(prepare_to_wait);
  160. void
  161. prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
  162. {
  163. unsigned long flags;
  164. wait->flags |= WQ_FLAG_EXCLUSIVE;
  165. spin_lock_irqsave(&q->lock, flags);
  166. if (list_empty(&wait->task_list))
  167. __add_wait_queue_tail(q, wait);
  168. set_current_state(state);
  169. spin_unlock_irqrestore(&q->lock, flags);
  170. }
  171. EXPORT_SYMBOL(prepare_to_wait_exclusive);
  172. long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state)
  173. {
  174. unsigned long flags;
  175. if (signal_pending_state(state, current))
  176. return -ERESTARTSYS;
  177. wait->private = current;
  178. wait->func = autoremove_wake_function;
  179. spin_lock_irqsave(&q->lock, flags);
  180. if (list_empty(&wait->task_list)) {
  181. if (wait->flags & WQ_FLAG_EXCLUSIVE)
  182. __add_wait_queue_tail(q, wait);
  183. else
  184. __add_wait_queue(q, wait);
  185. }
  186. set_current_state(state);
  187. spin_unlock_irqrestore(&q->lock, flags);
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(prepare_to_wait_event);
  191. /**
  192. * finish_wait - clean up after waiting in a queue
  193. * @q: waitqueue waited on
  194. * @wait: wait descriptor
  195. *
  196. * Sets current thread back to running state and removes
  197. * the wait descriptor from the given waitqueue if still
  198. * queued.
  199. */
  200. void finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
  201. {
  202. unsigned long flags;
  203. __set_current_state(TASK_RUNNING);
  204. /*
  205. * We can check for list emptiness outside the lock
  206. * IFF:
  207. * - we use the "careful" check that verifies both
  208. * the next and prev pointers, so that there cannot
  209. * be any half-pending updates in progress on other
  210. * CPU's that we haven't seen yet (and that might
  211. * still change the stack area.
  212. * and
  213. * - all other users take the lock (ie we can only
  214. * have _one_ other CPU that looks at or modifies
  215. * the list).
  216. */
  217. if (!list_empty_careful(&wait->task_list)) {
  218. spin_lock_irqsave(&q->lock, flags);
  219. list_del_init(&wait->task_list);
  220. spin_unlock_irqrestore(&q->lock, flags);
  221. }
  222. }
  223. EXPORT_SYMBOL(finish_wait);
  224. /**
  225. * abort_exclusive_wait - abort exclusive waiting in a queue
  226. * @q: waitqueue waited on
  227. * @wait: wait descriptor
  228. * @mode: runstate of the waiter to be woken
  229. * @key: key to identify a wait bit queue or %NULL
  230. *
  231. * Sets current thread back to running state and removes
  232. * the wait descriptor from the given waitqueue if still
  233. * queued.
  234. *
  235. * Wakes up the next waiter if the caller is concurrently
  236. * woken up through the queue.
  237. *
  238. * This prevents waiter starvation where an exclusive waiter
  239. * aborts and is woken up concurrently and no one wakes up
  240. * the next waiter.
  241. */
  242. void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
  243. unsigned int mode, void *key)
  244. {
  245. unsigned long flags;
  246. __set_current_state(TASK_RUNNING);
  247. spin_lock_irqsave(&q->lock, flags);
  248. if (!list_empty(&wait->task_list))
  249. list_del_init(&wait->task_list);
  250. else if (waitqueue_active(q))
  251. __wake_up_locked_key(q, mode, key);
  252. spin_unlock_irqrestore(&q->lock, flags);
  253. }
  254. EXPORT_SYMBOL(abort_exclusive_wait);
  255. int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
  256. {
  257. int ret = default_wake_function(wait, mode, sync, key);
  258. if (ret)
  259. list_del_init(&wait->task_list);
  260. return ret;
  261. }
  262. EXPORT_SYMBOL(autoremove_wake_function);
  263. int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
  264. {
  265. struct wait_bit_key *key = arg;
  266. struct wait_bit_queue *wait_bit
  267. = container_of(wait, struct wait_bit_queue, wait);
  268. if (wait_bit->key.flags != key->flags ||
  269. wait_bit->key.bit_nr != key->bit_nr ||
  270. test_bit(key->bit_nr, key->flags))
  271. return 0;
  272. else
  273. return autoremove_wake_function(wait, mode, sync, key);
  274. }
  275. EXPORT_SYMBOL(wake_bit_function);
  276. /*
  277. * To allow interruptible waiting and asynchronous (i.e. nonblocking)
  278. * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
  279. * permitted return codes. Nonzero return codes halt waiting and return.
  280. */
  281. int __sched
  282. __wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
  283. int (*action)(void *), unsigned mode)
  284. {
  285. int ret = 0;
  286. do {
  287. prepare_to_wait(wq, &q->wait, mode);
  288. if (test_bit(q->key.bit_nr, q->key.flags))
  289. ret = (*action)(q->key.flags);
  290. } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);
  291. finish_wait(wq, &q->wait);
  292. return ret;
  293. }
  294. EXPORT_SYMBOL(__wait_on_bit);
  295. int __sched out_of_line_wait_on_bit(void *word, int bit,
  296. int (*action)(void *), unsigned mode)
  297. {
  298. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  299. DEFINE_WAIT_BIT(wait, word, bit);
  300. return __wait_on_bit(wq, &wait, action, mode);
  301. }
  302. EXPORT_SYMBOL(out_of_line_wait_on_bit);
  303. int __sched
  304. __wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
  305. int (*action)(void *), unsigned mode)
  306. {
  307. do {
  308. int ret;
  309. prepare_to_wait_exclusive(wq, &q->wait, mode);
  310. if (!test_bit(q->key.bit_nr, q->key.flags))
  311. continue;
  312. ret = action(q->key.flags);
  313. if (!ret)
  314. continue;
  315. abort_exclusive_wait(wq, &q->wait, mode, &q->key);
  316. return ret;
  317. } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
  318. finish_wait(wq, &q->wait);
  319. return 0;
  320. }
  321. EXPORT_SYMBOL(__wait_on_bit_lock);
  322. int __sched out_of_line_wait_on_bit_lock(void *word, int bit,
  323. int (*action)(void *), unsigned mode)
  324. {
  325. wait_queue_head_t *wq = bit_waitqueue(word, bit);
  326. DEFINE_WAIT_BIT(wait, word, bit);
  327. return __wait_on_bit_lock(wq, &wait, action, mode);
  328. }
  329. EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);
  330. void __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
  331. {
  332. struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
  333. if (waitqueue_active(wq))
  334. __wake_up(wq, TASK_NORMAL, 1, &key);
  335. }
  336. EXPORT_SYMBOL(__wake_up_bit);
  337. /**
  338. * wake_up_bit - wake up a waiter on a bit
  339. * @word: the word being waited on, a kernel virtual address
  340. * @bit: the bit of the word being waited on
  341. *
  342. * There is a standard hashed waitqueue table for generic use. This
  343. * is the part of the hashtable's accessor API that wakes up waiters
  344. * on a bit. For instance, if one were to have waiters on a bitflag,
  345. * one would call wake_up_bit() after clearing the bit.
  346. *
  347. * In order for this to function properly, as it uses waitqueue_active()
  348. * internally, some kind of memory barrier must be done prior to calling
  349. * this. Typically, this will be smp_mb__after_clear_bit(), but in some
  350. * cases where bitflags are manipulated non-atomically under a lock, one
  351. * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
  352. * because spin_unlock() does not guarantee a memory barrier.
  353. */
  354. void wake_up_bit(void *word, int bit)
  355. {
  356. __wake_up_bit(bit_waitqueue(word, bit), word, bit);
  357. }
  358. EXPORT_SYMBOL(wake_up_bit);
  359. wait_queue_head_t *bit_waitqueue(void *word, int bit)
  360. {
  361. const int shift = BITS_PER_LONG == 32 ? 5 : 6;
  362. const struct zone *zone = page_zone(virt_to_page(word));
  363. unsigned long val = (unsigned long)word << shift | bit;
  364. return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
  365. }
  366. EXPORT_SYMBOL(bit_waitqueue);
  367. /*
  368. * Manipulate the atomic_t address to produce a better bit waitqueue table hash
  369. * index (we're keying off bit -1, but that would produce a horrible hash
  370. * value).
  371. */
  372. static inline wait_queue_head_t *atomic_t_waitqueue(atomic_t *p)
  373. {
  374. if (BITS_PER_LONG == 64) {
  375. unsigned long q = (unsigned long)p;
  376. return bit_waitqueue((void *)(q & ~1), q & 1);
  377. }
  378. return bit_waitqueue(p, 0);
  379. }
  380. static int wake_atomic_t_function(wait_queue_t *wait, unsigned mode, int sync,
  381. void *arg)
  382. {
  383. struct wait_bit_key *key = arg;
  384. struct wait_bit_queue *wait_bit
  385. = container_of(wait, struct wait_bit_queue, wait);
  386. atomic_t *val = key->flags;
  387. if (wait_bit->key.flags != key->flags ||
  388. wait_bit->key.bit_nr != key->bit_nr ||
  389. atomic_read(val) != 0)
  390. return 0;
  391. return autoremove_wake_function(wait, mode, sync, key);
  392. }
  393. /*
  394. * To allow interruptible waiting and asynchronous (i.e. nonblocking) waiting,
  395. * the actions of __wait_on_atomic_t() are permitted return codes. Nonzero
  396. * return codes halt waiting and return.
  397. */
  398. static __sched
  399. int __wait_on_atomic_t(wait_queue_head_t *wq, struct wait_bit_queue *q,
  400. int (*action)(atomic_t *), unsigned mode)
  401. {
  402. atomic_t *val;
  403. int ret = 0;
  404. do {
  405. prepare_to_wait(wq, &q->wait, mode);
  406. val = q->key.flags;
  407. if (atomic_read(val) == 0)
  408. break;
  409. ret = (*action)(val);
  410. } while (!ret && atomic_read(val) != 0);
  411. finish_wait(wq, &q->wait);
  412. return ret;
  413. }
  414. #define DEFINE_WAIT_ATOMIC_T(name, p) \
  415. struct wait_bit_queue name = { \
  416. .key = __WAIT_ATOMIC_T_KEY_INITIALIZER(p), \
  417. .wait = { \
  418. .private = current, \
  419. .func = wake_atomic_t_function, \
  420. .task_list = \
  421. LIST_HEAD_INIT((name).wait.task_list), \
  422. }, \
  423. }
  424. __sched int out_of_line_wait_on_atomic_t(atomic_t *p, int (*action)(atomic_t *),
  425. unsigned mode)
  426. {
  427. wait_queue_head_t *wq = atomic_t_waitqueue(p);
  428. DEFINE_WAIT_ATOMIC_T(wait, p);
  429. return __wait_on_atomic_t(wq, &wait, action, mode);
  430. }
  431. EXPORT_SYMBOL(out_of_line_wait_on_atomic_t);
  432. /**
  433. * wake_up_atomic_t - Wake up a waiter on a atomic_t
  434. * @p: The atomic_t being waited on, a kernel virtual address
  435. *
  436. * Wake up anyone waiting for the atomic_t to go to zero.
  437. *
  438. * Abuse the bit-waker function and its waitqueue hash table set (the atomic_t
  439. * check is done by the waiter's wake function, not the by the waker itself).
  440. */
  441. void wake_up_atomic_t(atomic_t *p)
  442. {
  443. __wake_up_bit(atomic_t_waitqueue(p), p, WAIT_ATOMIC_T_BIT_NR);
  444. }
  445. EXPORT_SYMBOL(wake_up_atomic_t);