1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
25dd43ce2SIngo Molnar #ifndef _LINUX_WAIT_BIT_H
35dd43ce2SIngo Molnar #define _LINUX_WAIT_BIT_H
45dd43ce2SIngo Molnar
55dd43ce2SIngo Molnar /*
65dd43ce2SIngo Molnar * Linux wait-bit related types and methods:
75dd43ce2SIngo Molnar */
85dd43ce2SIngo Molnar #include <linux/wait.h>
95dd43ce2SIngo Molnar
105dd43ce2SIngo Molnar struct wait_bit_key {
112382d68dSNeilBrown unsigned long *flags;
125dd43ce2SIngo Molnar int bit_nr;
135dd43ce2SIngo Molnar unsigned long timeout;
145dd43ce2SIngo Molnar };
155dd43ce2SIngo Molnar
165dd43ce2SIngo Molnar struct wait_bit_queue_entry {
175dd43ce2SIngo Molnar struct wait_bit_key key;
185dd43ce2SIngo Molnar struct wait_queue_entry wq_entry;
195dd43ce2SIngo Molnar };
205dd43ce2SIngo Molnar
215dd43ce2SIngo Molnar #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
225dd43ce2SIngo Molnar { .flags = word, .bit_nr = bit, }
235dd43ce2SIngo Molnar
245dd43ce2SIngo Molnar typedef int wait_bit_action_f(struct wait_bit_key *key, int mode);
255e4def20SDavid Howells
262382d68dSNeilBrown void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bit);
275dd43ce2SIngo Molnar int __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
285dd43ce2SIngo Molnar int __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
292382d68dSNeilBrown void wake_up_bit(unsigned long *word, int bit);
302382d68dSNeilBrown int out_of_line_wait_on_bit(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode);
312382d68dSNeilBrown int out_of_line_wait_on_bit_timeout(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout);
322382d68dSNeilBrown int out_of_line_wait_on_bit_lock(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode);
332382d68dSNeilBrown struct wait_queue_head *bit_waitqueue(unsigned long *word, int bit);
345822a454SIngo Molnar extern void __init wait_bit_init(void);
355dd43ce2SIngo Molnar
365dd43ce2SIngo Molnar int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
375dd43ce2SIngo Molnar
385dd43ce2SIngo Molnar #define DEFINE_WAIT_BIT(name, word, bit) \
395dd43ce2SIngo Molnar struct wait_bit_queue_entry name = { \
405dd43ce2SIngo Molnar .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
415dd43ce2SIngo Molnar .wq_entry = { \
425dd43ce2SIngo Molnar .private = current, \
435dd43ce2SIngo Molnar .func = wake_bit_function, \
442055da97SIngo Molnar .entry = \
452055da97SIngo Molnar LIST_HEAD_INIT((name).wq_entry.entry), \
465dd43ce2SIngo Molnar }, \
475dd43ce2SIngo Molnar }
485dd43ce2SIngo Molnar
495e4def20SDavid Howells extern int bit_wait(struct wait_bit_key *key, int mode);
505e4def20SDavid Howells extern int bit_wait_io(struct wait_bit_key *key, int mode);
515e4def20SDavid Howells extern int bit_wait_timeout(struct wait_bit_key *key, int mode);
525dd43ce2SIngo Molnar
535dd43ce2SIngo Molnar /**
545dd43ce2SIngo Molnar * wait_on_bit - wait for a bit to be cleared
553cdee6b3SNeilBrown * @word: the address containing the bit being waited on
563cdee6b3SNeilBrown * @bit: the bit at that address being waited on
575dd43ce2SIngo Molnar * @mode: the task state to sleep in
585dd43ce2SIngo Molnar *
593cdee6b3SNeilBrown * Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP())
603cdee6b3SNeilBrown * to be cleared. The clearing of the bit must be signalled with
613cdee6b3SNeilBrown * wake_up_bit(), often as clear_and_wake_up_bit().
623cdee6b3SNeilBrown *
633cdee6b3SNeilBrown * The process will wait on a waitqueue selected by hash from a shared
643cdee6b3SNeilBrown * pool. It will only be woken on a wake_up for the target bit, even
653cdee6b3SNeilBrown * if other processes on the same queue are waiting for other bits.
663cdee6b3SNeilBrown *
673cdee6b3SNeilBrown * Returned value will be zero if the bit was cleared in which case the
683cdee6b3SNeilBrown * call has ACQUIRE semantics, or %-EINTR if the process received a
693cdee6b3SNeilBrown * signal and the mode permitted wake up on that signal.
705dd43ce2SIngo Molnar */
715dd43ce2SIngo Molnar static inline int
wait_on_bit(unsigned long * word,int bit,unsigned mode)725dd43ce2SIngo Molnar wait_on_bit(unsigned long *word, int bit, unsigned mode)
735dd43ce2SIngo Molnar {
745dd43ce2SIngo Molnar might_sleep();
758238b457SMikulas Patocka if (!test_bit_acquire(bit, word))
765dd43ce2SIngo Molnar return 0;
775dd43ce2SIngo Molnar return out_of_line_wait_on_bit(word, bit,
785dd43ce2SIngo Molnar bit_wait,
795dd43ce2SIngo Molnar mode);
805dd43ce2SIngo Molnar }
815dd43ce2SIngo Molnar
825dd43ce2SIngo Molnar /**
835dd43ce2SIngo Molnar * wait_on_bit_io - wait for a bit to be cleared
843cdee6b3SNeilBrown * @word: the address containing the bit being waited on
853cdee6b3SNeilBrown * @bit: the bit at that address being waited on
865dd43ce2SIngo Molnar * @mode: the task state to sleep in
875dd43ce2SIngo Molnar *
883cdee6b3SNeilBrown * Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP())
893cdee6b3SNeilBrown * to be cleared. The clearing of the bit must be signalled with
903cdee6b3SNeilBrown * wake_up_bit(), often as clear_and_wake_up_bit().
915dd43ce2SIngo Molnar *
923cdee6b3SNeilBrown * This is similar to wait_on_bit(), but calls io_schedule() instead of
933cdee6b3SNeilBrown * schedule() for the actual waiting.
943cdee6b3SNeilBrown *
953cdee6b3SNeilBrown * Returned value will be zero if the bit was cleared in which case the
963cdee6b3SNeilBrown * call has ACQUIRE semantics, or %-EINTR if the process received a
973cdee6b3SNeilBrown * signal and the mode permitted wake up on that signal.
985dd43ce2SIngo Molnar */
995dd43ce2SIngo Molnar static inline int
wait_on_bit_io(unsigned long * word,int bit,unsigned mode)1005dd43ce2SIngo Molnar wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
1015dd43ce2SIngo Molnar {
1025dd43ce2SIngo Molnar might_sleep();
1038238b457SMikulas Patocka if (!test_bit_acquire(bit, word))
1045dd43ce2SIngo Molnar return 0;
1055dd43ce2SIngo Molnar return out_of_line_wait_on_bit(word, bit,
1065dd43ce2SIngo Molnar bit_wait_io,
1075dd43ce2SIngo Molnar mode);
1085dd43ce2SIngo Molnar }
1095dd43ce2SIngo Molnar
1105dd43ce2SIngo Molnar /**
1113cdee6b3SNeilBrown * wait_on_bit_timeout - wait for a bit to be cleared or a timeout to elapse
1123cdee6b3SNeilBrown * @word: the address containing the bit being waited on
1133cdee6b3SNeilBrown * @bit: the bit at that address being waited on
1145dd43ce2SIngo Molnar * @mode: the task state to sleep in
1155dd43ce2SIngo Molnar * @timeout: timeout, in jiffies
1165dd43ce2SIngo Molnar *
1173cdee6b3SNeilBrown * Wait for the given bit in an unsigned long or bitmap (see
1183cdee6b3SNeilBrown * DECLARE_BITMAP()) to be cleared, or for a timeout to expire. The
1193cdee6b3SNeilBrown * clearing of the bit must be signalled with wake_up_bit(), often as
1203cdee6b3SNeilBrown * clear_and_wake_up_bit().
1215dd43ce2SIngo Molnar *
1223cdee6b3SNeilBrown * This is similar to wait_on_bit(), except it also takes a timeout
1233cdee6b3SNeilBrown * parameter.
1243cdee6b3SNeilBrown *
1253cdee6b3SNeilBrown * Returned value will be zero if the bit was cleared in which case the
1263cdee6b3SNeilBrown * call has ACQUIRE semantics, or %-EINTR if the process received a
1273cdee6b3SNeilBrown * signal and the mode permitted wake up on that signal, or %-EAGAIN if the
1283cdee6b3SNeilBrown * timeout elapsed.
1295dd43ce2SIngo Molnar */
1305dd43ce2SIngo Molnar static inline int
wait_on_bit_timeout(unsigned long * word,int bit,unsigned mode,unsigned long timeout)1315dd43ce2SIngo Molnar wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
1325dd43ce2SIngo Molnar unsigned long timeout)
1335dd43ce2SIngo Molnar {
1345dd43ce2SIngo Molnar might_sleep();
1358238b457SMikulas Patocka if (!test_bit_acquire(bit, word))
1365dd43ce2SIngo Molnar return 0;
1375dd43ce2SIngo Molnar return out_of_line_wait_on_bit_timeout(word, bit,
1385dd43ce2SIngo Molnar bit_wait_timeout,
1395dd43ce2SIngo Molnar mode, timeout);
1405dd43ce2SIngo Molnar }
1415dd43ce2SIngo Molnar
1425dd43ce2SIngo Molnar /**
1435dd43ce2SIngo Molnar * wait_on_bit_action - wait for a bit to be cleared
1443cdee6b3SNeilBrown * @word: the address containing the bit waited on
1453cdee6b3SNeilBrown * @bit: the bit at that address being waited on
1465dd43ce2SIngo Molnar * @action: the function used to sleep, which may take special actions
1475dd43ce2SIngo Molnar * @mode: the task state to sleep in
1485dd43ce2SIngo Molnar *
1493cdee6b3SNeilBrown * Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP())
1503cdee6b3SNeilBrown * to be cleared. The clearing of the bit must be signalled with
1513cdee6b3SNeilBrown * wake_up_bit(), often as clear_and_wake_up_bit().
1525dd43ce2SIngo Molnar *
1533cdee6b3SNeilBrown * This is similar to wait_on_bit(), but calls @action() instead of
1543cdee6b3SNeilBrown * schedule() for the actual waiting.
1553cdee6b3SNeilBrown *
1563cdee6b3SNeilBrown * Returned value will be zero if the bit was cleared in which case the
1573cdee6b3SNeilBrown * call has ACQUIRE semantics, or the error code returned by @action if
1583cdee6b3SNeilBrown * that call returned non-zero.
1595dd43ce2SIngo Molnar */
1605dd43ce2SIngo Molnar static inline int
wait_on_bit_action(unsigned long * word,int bit,wait_bit_action_f * action,unsigned mode)1615dd43ce2SIngo Molnar wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
1625dd43ce2SIngo Molnar unsigned mode)
1635dd43ce2SIngo Molnar {
1645dd43ce2SIngo Molnar might_sleep();
1658238b457SMikulas Patocka if (!test_bit_acquire(bit, word))
1665dd43ce2SIngo Molnar return 0;
1675dd43ce2SIngo Molnar return out_of_line_wait_on_bit(word, bit, action, mode);
1685dd43ce2SIngo Molnar }
1695dd43ce2SIngo Molnar
1705dd43ce2SIngo Molnar /**
1713cdee6b3SNeilBrown * wait_on_bit_lock - wait for a bit to be cleared, then set it
1723cdee6b3SNeilBrown * @word: the address containing the bit being waited on
1733cdee6b3SNeilBrown * @bit: the bit of the word being waited on and set
1745dd43ce2SIngo Molnar * @mode: the task state to sleep in
1755dd43ce2SIngo Molnar *
1763cdee6b3SNeilBrown * Wait for the given bit in an unsigned long or bitmap (see
1773cdee6b3SNeilBrown * DECLARE_BITMAP()) to be cleared. The clearing of the bit must be
1783cdee6b3SNeilBrown * signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As
1793cdee6b3SNeilBrown * soon as it is clear, atomically set it and return.
1805dd43ce2SIngo Molnar *
1813cdee6b3SNeilBrown * This is similar to wait_on_bit(), but sets the bit before returning.
1823cdee6b3SNeilBrown *
1833cdee6b3SNeilBrown * Returned value will be zero if the bit was successfully set in which
1843cdee6b3SNeilBrown * case the call has the same memory sequencing semantics as
1853cdee6b3SNeilBrown * test_and_clear_bit(), or %-EINTR if the process received a signal and
1863cdee6b3SNeilBrown * the mode permitted wake up on that signal.
1875dd43ce2SIngo Molnar */
1885dd43ce2SIngo Molnar static inline int
wait_on_bit_lock(unsigned long * word,int bit,unsigned mode)1895dd43ce2SIngo Molnar wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
1905dd43ce2SIngo Molnar {
1915dd43ce2SIngo Molnar might_sleep();
1925dd43ce2SIngo Molnar if (!test_and_set_bit(bit, word))
1935dd43ce2SIngo Molnar return 0;
1945dd43ce2SIngo Molnar return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1955dd43ce2SIngo Molnar }
1965dd43ce2SIngo Molnar
1975dd43ce2SIngo Molnar /**
1983cdee6b3SNeilBrown * wait_on_bit_lock_io - wait for a bit to be cleared, then set it
1993cdee6b3SNeilBrown * @word: the address containing the bit being waited on
2003cdee6b3SNeilBrown * @bit: the bit of the word being waited on and set
2015dd43ce2SIngo Molnar * @mode: the task state to sleep in
2025dd43ce2SIngo Molnar *
2033cdee6b3SNeilBrown * Wait for the given bit in an unsigned long or bitmap (see
2043cdee6b3SNeilBrown * DECLARE_BITMAP()) to be cleared. The clearing of the bit must be
2053cdee6b3SNeilBrown * signalled with wake_up_bit(), often as clear_and_wake_up_bit(). As
2063cdee6b3SNeilBrown * soon as it is clear, atomically set it and return.
2073cdee6b3SNeilBrown *
2083cdee6b3SNeilBrown * This is similar to wait_on_bit_lock(), but calls io_schedule() instead
2093cdee6b3SNeilBrown * of schedule().
2105dd43ce2SIngo Molnar *
2115dd43ce2SIngo Molnar * Returns zero if the bit was (eventually) found to be clear and was
2125dd43ce2SIngo Molnar * set. Returns non-zero if a signal was delivered to the process and
2135dd43ce2SIngo Molnar * the @mode allows that signal to wake the process.
2145dd43ce2SIngo Molnar */
2155dd43ce2SIngo Molnar static inline int
wait_on_bit_lock_io(unsigned long * word,int bit,unsigned mode)2165dd43ce2SIngo Molnar wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
2175dd43ce2SIngo Molnar {
2185dd43ce2SIngo Molnar might_sleep();
2195dd43ce2SIngo Molnar if (!test_and_set_bit(bit, word))
2205dd43ce2SIngo Molnar return 0;
2215dd43ce2SIngo Molnar return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
2225dd43ce2SIngo Molnar }
2235dd43ce2SIngo Molnar
2245dd43ce2SIngo Molnar /**
2253cdee6b3SNeilBrown * wait_on_bit_lock_action - wait for a bit to be cleared, then set it
2263cdee6b3SNeilBrown * @word: the address containing the bit being waited on
2273cdee6b3SNeilBrown * @bit: the bit of the word being waited on and set
2285dd43ce2SIngo Molnar * @action: the function used to sleep, which may take special actions
2295dd43ce2SIngo Molnar * @mode: the task state to sleep in
2305dd43ce2SIngo Molnar *
2313cdee6b3SNeilBrown * This is similar to wait_on_bit_lock(), but calls @action() instead of
2323cdee6b3SNeilBrown * schedule() for the actual waiting.
2335dd43ce2SIngo Molnar *
2343cdee6b3SNeilBrown * Returned value will be zero if the bit was successfully set in which
2353cdee6b3SNeilBrown * case the call has the same memory sequencing semantics as
2363cdee6b3SNeilBrown * test_and_clear_bit(), or the error code returned by @action if that
2373cdee6b3SNeilBrown * call returned non-zero.
2385dd43ce2SIngo Molnar */
2395dd43ce2SIngo Molnar static inline int
wait_on_bit_lock_action(unsigned long * word,int bit,wait_bit_action_f * action,unsigned mode)2405dd43ce2SIngo Molnar wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
2415dd43ce2SIngo Molnar unsigned mode)
2425dd43ce2SIngo Molnar {
2435dd43ce2SIngo Molnar might_sleep();
2445dd43ce2SIngo Molnar if (!test_and_set_bit(bit, word))
2455dd43ce2SIngo Molnar return 0;
2465dd43ce2SIngo Molnar return out_of_line_wait_on_bit_lock(word, bit, action, mode);
2475dd43ce2SIngo Molnar }
2485dd43ce2SIngo Molnar
2496b2bb726SPeter Zijlstra extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags);
2506b2bb726SPeter Zijlstra extern void wake_up_var(void *var);
2516b2bb726SPeter Zijlstra extern wait_queue_head_t *__var_waitqueue(void *p);
2526b2bb726SPeter Zijlstra
2536b2bb726SPeter Zijlstra #define ___wait_var_event(var, condition, state, exclusive, ret, cmd) \
2546b2bb726SPeter Zijlstra ({ \
2556b2bb726SPeter Zijlstra __label__ __out; \
2566b2bb726SPeter Zijlstra struct wait_queue_head *__wq_head = __var_waitqueue(var); \
2576b2bb726SPeter Zijlstra struct wait_bit_queue_entry __wbq_entry; \
2586b2bb726SPeter Zijlstra long __ret = ret; /* explicit shadow */ \
2596b2bb726SPeter Zijlstra \
2606b2bb726SPeter Zijlstra init_wait_var_entry(&__wbq_entry, var, \
2616b2bb726SPeter Zijlstra exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
2626b2bb726SPeter Zijlstra for (;;) { \
2636b2bb726SPeter Zijlstra long __int = prepare_to_wait_event(__wq_head, \
2646b2bb726SPeter Zijlstra &__wbq_entry.wq_entry, \
2656b2bb726SPeter Zijlstra state); \
2666b2bb726SPeter Zijlstra if (condition) \
2676b2bb726SPeter Zijlstra break; \
2686b2bb726SPeter Zijlstra \
2696b2bb726SPeter Zijlstra if (___wait_is_interruptible(state) && __int) { \
2706b2bb726SPeter Zijlstra __ret = __int; \
2716b2bb726SPeter Zijlstra goto __out; \
2726b2bb726SPeter Zijlstra } \
2736b2bb726SPeter Zijlstra \
2746b2bb726SPeter Zijlstra cmd; \
2756b2bb726SPeter Zijlstra } \
2766b2bb726SPeter Zijlstra finish_wait(__wq_head, &__wbq_entry.wq_entry); \
2776b2bb726SPeter Zijlstra __out: __ret; \
2786b2bb726SPeter Zijlstra })
2796b2bb726SPeter Zijlstra
2806b2bb726SPeter Zijlstra #define __wait_var_event(var, condition) \
2816b2bb726SPeter Zijlstra ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
2826b2bb726SPeter Zijlstra schedule())
283*80681c04SNeilBrown #define __wait_var_event_io(var, condition) \
284*80681c04SNeilBrown ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
285*80681c04SNeilBrown io_schedule())
2866b2bb726SPeter Zijlstra
287bf39882eSNeilBrown /**
288bf39882eSNeilBrown * wait_var_event - wait for a variable to be updated and notified
289bf39882eSNeilBrown * @var: the address of variable being waited on
290bf39882eSNeilBrown * @condition: the condition to wait for
291bf39882eSNeilBrown *
292bf39882eSNeilBrown * Wait for a @condition to be true, only re-checking when a wake up is
293bf39882eSNeilBrown * received for the given @var (an arbitrary kernel address which need
294bf39882eSNeilBrown * not be directly related to the given condition, but usually is).
295bf39882eSNeilBrown *
296bf39882eSNeilBrown * The process will wait on a waitqueue selected by hash from a shared
297bf39882eSNeilBrown * pool. It will only be woken on a wake_up for the given address.
298bf39882eSNeilBrown *
299bf39882eSNeilBrown * The condition should normally use smp_load_acquire() or a similarly
300bf39882eSNeilBrown * ordered access to ensure that any changes to memory made before the
301bf39882eSNeilBrown * condition became true will be visible after the wait completes.
302bf39882eSNeilBrown */
3036b2bb726SPeter Zijlstra #define wait_var_event(var, condition) \
3046b2bb726SPeter Zijlstra do { \
3056b2bb726SPeter Zijlstra might_sleep(); \
3066b2bb726SPeter Zijlstra if (condition) \
3076b2bb726SPeter Zijlstra break; \
3086b2bb726SPeter Zijlstra __wait_var_event(var, condition); \
3096b2bb726SPeter Zijlstra } while (0)
3106b2bb726SPeter Zijlstra
311*80681c04SNeilBrown /**
312*80681c04SNeilBrown * wait_var_event_io - wait for a variable to be updated and notified
313*80681c04SNeilBrown * @var: the address of variable being waited on
314*80681c04SNeilBrown * @condition: the condition to wait for
315*80681c04SNeilBrown *
316*80681c04SNeilBrown * Wait for an IO related @condition to be true, only re-checking when a
317*80681c04SNeilBrown * wake up is received for the given @var (an arbitrary kernel address
318*80681c04SNeilBrown * which need not be directly related to the given condition, but
319*80681c04SNeilBrown * usually is).
320*80681c04SNeilBrown *
321*80681c04SNeilBrown * The process will wait on a waitqueue selected by hash from a shared
322*80681c04SNeilBrown * pool. It will only be woken on a wake_up for the given address.
323*80681c04SNeilBrown *
324*80681c04SNeilBrown * This is similar to wait_var_event(), but calls io_schedule() instead
325*80681c04SNeilBrown * of schedule().
326*80681c04SNeilBrown *
327*80681c04SNeilBrown * The condition should normally use smp_load_acquire() or a similarly
328*80681c04SNeilBrown * ordered access to ensure that any changes to memory made before the
329*80681c04SNeilBrown * condition became true will be visible after the wait completes.
330*80681c04SNeilBrown */
331*80681c04SNeilBrown #define wait_var_event_io(var, condition) \
332*80681c04SNeilBrown do { \
333*80681c04SNeilBrown might_sleep(); \
334*80681c04SNeilBrown if (condition) \
335*80681c04SNeilBrown break; \
336*80681c04SNeilBrown __wait_var_event_io(var, condition); \
337*80681c04SNeilBrown } while (0)
338*80681c04SNeilBrown
3396b2bb726SPeter Zijlstra #define __wait_var_event_killable(var, condition) \
3406b2bb726SPeter Zijlstra ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \
3416b2bb726SPeter Zijlstra schedule())
3426b2bb726SPeter Zijlstra
343bf39882eSNeilBrown /**
344bf39882eSNeilBrown * wait_var_event_killable - wait for a variable to be updated and notified
345bf39882eSNeilBrown * @var: the address of variable being waited on
346bf39882eSNeilBrown * @condition: the condition to wait for
347bf39882eSNeilBrown *
348bf39882eSNeilBrown * Wait for a @condition to be true or a fatal signal to be received,
349bf39882eSNeilBrown * only re-checking the condition when a wake up is received for the given
350bf39882eSNeilBrown * @var (an arbitrary kernel address which need not be directly related
351bf39882eSNeilBrown * to the given condition, but usually is).
352bf39882eSNeilBrown *
353bf39882eSNeilBrown * This is similar to wait_var_event() but returns a value which is
354bf39882eSNeilBrown * 0 if the condition became true, or %-ERESTARTSYS if a fatal signal
355bf39882eSNeilBrown * was received.
356bf39882eSNeilBrown *
357bf39882eSNeilBrown * The condition should normally use smp_load_acquire() or a similarly
358bf39882eSNeilBrown * ordered access to ensure that any changes to memory made before the
359bf39882eSNeilBrown * condition became true will be visible after the wait completes.
360bf39882eSNeilBrown */
3616b2bb726SPeter Zijlstra #define wait_var_event_killable(var, condition) \
3626b2bb726SPeter Zijlstra ({ \
3636b2bb726SPeter Zijlstra int __ret = 0; \
3646b2bb726SPeter Zijlstra might_sleep(); \
3656b2bb726SPeter Zijlstra if (!(condition)) \
3666b2bb726SPeter Zijlstra __ret = __wait_var_event_killable(var, condition); \
3676b2bb726SPeter Zijlstra __ret; \
3686b2bb726SPeter Zijlstra })
3696b2bb726SPeter Zijlstra
3706b2bb726SPeter Zijlstra #define __wait_var_event_timeout(var, condition, timeout) \
3716b2bb726SPeter Zijlstra ___wait_var_event(var, ___wait_cond_timeout(condition), \
3726b2bb726SPeter Zijlstra TASK_UNINTERRUPTIBLE, 0, timeout, \
3736b2bb726SPeter Zijlstra __ret = schedule_timeout(__ret))
3746b2bb726SPeter Zijlstra
375bf39882eSNeilBrown /**
376bf39882eSNeilBrown * wait_var_event_timeout - wait for a variable to be updated or a timeout to expire
377bf39882eSNeilBrown * @var: the address of variable being waited on
378bf39882eSNeilBrown * @condition: the condition to wait for
379bf39882eSNeilBrown * @timeout: maximum time to wait in jiffies
380bf39882eSNeilBrown *
381bf39882eSNeilBrown * Wait for a @condition to be true or a timeout to expire, only
382bf39882eSNeilBrown * re-checking the condition when a wake up is received for the given
383bf39882eSNeilBrown * @var (an arbitrary kernel address which need not be directly related
384bf39882eSNeilBrown * to the given condition, but usually is).
385bf39882eSNeilBrown *
386bf39882eSNeilBrown * This is similar to wait_var_event() but returns a value which is 0 if
387bf39882eSNeilBrown * the timeout expired and the condition was still false, or the
388bf39882eSNeilBrown * remaining time left in the timeout (but at least 1) if the condition
389bf39882eSNeilBrown * was found to be true.
390bf39882eSNeilBrown *
391bf39882eSNeilBrown * The condition should normally use smp_load_acquire() or a similarly
392bf39882eSNeilBrown * ordered access to ensure that any changes to memory made before the
393bf39882eSNeilBrown * condition became true will be visible after the wait completes.
394bf39882eSNeilBrown */
3956b2bb726SPeter Zijlstra #define wait_var_event_timeout(var, condition, timeout) \
3966b2bb726SPeter Zijlstra ({ \
3976b2bb726SPeter Zijlstra long __ret = timeout; \
3986b2bb726SPeter Zijlstra might_sleep(); \
3996b2bb726SPeter Zijlstra if (!___wait_cond_timeout(condition)) \
4006b2bb726SPeter Zijlstra __ret = __wait_var_event_timeout(var, condition, timeout); \
4016b2bb726SPeter Zijlstra __ret; \
4026b2bb726SPeter Zijlstra })
4036b2bb726SPeter Zijlstra
404a49294eaSDavid Howells #define __wait_var_event_interruptible(var, condition) \
405a49294eaSDavid Howells ___wait_var_event(var, condition, TASK_INTERRUPTIBLE, 0, 0, \
406a49294eaSDavid Howells schedule())
407a49294eaSDavid Howells
408bf39882eSNeilBrown /**
409bf39882eSNeilBrown * wait_var_event_killable - wait for a variable to be updated and notified
410bf39882eSNeilBrown * @var: the address of variable being waited on
411bf39882eSNeilBrown * @condition: the condition to wait for
412bf39882eSNeilBrown *
413bf39882eSNeilBrown * Wait for a @condition to be true or a signal to be received, only
414bf39882eSNeilBrown * re-checking the condition when a wake up is received for the given
415bf39882eSNeilBrown * @var (an arbitrary kernel address which need not be directly related
416bf39882eSNeilBrown * to the given condition, but usually is).
417bf39882eSNeilBrown *
418bf39882eSNeilBrown * This is similar to wait_var_event() but returns a value which is 0 if
419bf39882eSNeilBrown * the condition became true, or %-ERESTARTSYS if a signal was received.
420bf39882eSNeilBrown *
421bf39882eSNeilBrown * The condition should normally use smp_load_acquire() or a similarly
422bf39882eSNeilBrown * ordered access to ensure that any changes to memory made before the
423bf39882eSNeilBrown * condition became true will be visible after the wait completes.
424bf39882eSNeilBrown */
425a49294eaSDavid Howells #define wait_var_event_interruptible(var, condition) \
426a49294eaSDavid Howells ({ \
427a49294eaSDavid Howells int __ret = 0; \
428a49294eaSDavid Howells might_sleep(); \
429a49294eaSDavid Howells if (!(condition)) \
430a49294eaSDavid Howells __ret = __wait_var_event_interruptible(var, condition); \
431a49294eaSDavid Howells __ret; \
432a49294eaSDavid Howells })
433a49294eaSDavid Howells
4348236b0aeSTetsuo Handa /**
435cc2e1c82SNeilBrown * wait_var_event_any_lock - wait for a variable to be updated under a lock
436cc2e1c82SNeilBrown * @var: the address of the variable being waited on
437cc2e1c82SNeilBrown * @condition: condition to wait for
438cc2e1c82SNeilBrown * @lock: the object that is locked to protect updates to the variable
439cc2e1c82SNeilBrown * @type: prefix on lock and unlock operations
440cc2e1c82SNeilBrown * @state: waiting state, %TASK_UNINTERRUPTIBLE etc.
441cc2e1c82SNeilBrown *
442cc2e1c82SNeilBrown * Wait for a condition which can only be reliably tested while holding
443cc2e1c82SNeilBrown * a lock. The variables assessed in the condition will normal be updated
444cc2e1c82SNeilBrown * under the same lock, and the wake up should be signalled with
445cc2e1c82SNeilBrown * wake_up_var_locked() under the same lock.
446cc2e1c82SNeilBrown *
447cc2e1c82SNeilBrown * This is similar to wait_var_event(), but assumes a lock is held
448cc2e1c82SNeilBrown * while calling this function and while updating the variable.
449cc2e1c82SNeilBrown *
450cc2e1c82SNeilBrown * This must be called while the given lock is held and the lock will be
451cc2e1c82SNeilBrown * dropped when schedule() is called to wait for a wake up, and will be
452cc2e1c82SNeilBrown * reclaimed before testing the condition again. The functions used to
453cc2e1c82SNeilBrown * unlock and lock the object are constructed by appending _unlock and _lock
454cc2e1c82SNeilBrown * to @type.
455cc2e1c82SNeilBrown *
456cc2e1c82SNeilBrown * Return %-ERESTARTSYS if a signal arrives which is allowed to interrupt
457cc2e1c82SNeilBrown * the wait according to @state.
458cc2e1c82SNeilBrown */
459cc2e1c82SNeilBrown #define wait_var_event_any_lock(var, condition, lock, type, state) \
460cc2e1c82SNeilBrown ({ \
461cc2e1c82SNeilBrown int __ret = 0; \
462cc2e1c82SNeilBrown if (!(condition)) \
463cc2e1c82SNeilBrown __ret = ___wait_var_event(var, condition, state, 0, 0, \
464cc2e1c82SNeilBrown type ## _unlock(lock); \
465cc2e1c82SNeilBrown schedule(); \
466cc2e1c82SNeilBrown type ## _lock(lock)); \
467cc2e1c82SNeilBrown __ret; \
468cc2e1c82SNeilBrown })
469cc2e1c82SNeilBrown
470cc2e1c82SNeilBrown /**
471cc2e1c82SNeilBrown * wait_var_event_spinlock - wait for a variable to be updated under a spinlock
472cc2e1c82SNeilBrown * @var: the address of the variable being waited on
473cc2e1c82SNeilBrown * @condition: condition to wait for
474cc2e1c82SNeilBrown * @lock: the spinlock which protects updates to the variable
475cc2e1c82SNeilBrown *
476cc2e1c82SNeilBrown * Wait for a condition which can only be reliably tested while holding
477cc2e1c82SNeilBrown * a spinlock. The variables assessed in the condition will normal be updated
478cc2e1c82SNeilBrown * under the same spinlock, and the wake up should be signalled with
479cc2e1c82SNeilBrown * wake_up_var_locked() under the same spinlock.
480cc2e1c82SNeilBrown *
481cc2e1c82SNeilBrown * This is similar to wait_var_event(), but assumes a spinlock is held
482cc2e1c82SNeilBrown * while calling this function and while updating the variable.
483cc2e1c82SNeilBrown *
484cc2e1c82SNeilBrown * This must be called while the given lock is held and the lock will be
485cc2e1c82SNeilBrown * dropped when schedule() is called to wait for a wake up, and will be
486cc2e1c82SNeilBrown * reclaimed before testing the condition again.
487cc2e1c82SNeilBrown */
488cc2e1c82SNeilBrown #define wait_var_event_spinlock(var, condition, lock) \
489cc2e1c82SNeilBrown wait_var_event_any_lock(var, condition, lock, spin, TASK_UNINTERRUPTIBLE)
490cc2e1c82SNeilBrown
491cc2e1c82SNeilBrown /**
492cc2e1c82SNeilBrown * wait_var_event_mutex - wait for a variable to be updated under a mutex
493cc2e1c82SNeilBrown * @var: the address of the variable being waited on
494cc2e1c82SNeilBrown * @condition: condition to wait for
495cc2e1c82SNeilBrown * @mutex: the mutex which protects updates to the variable
496cc2e1c82SNeilBrown *
497cc2e1c82SNeilBrown * Wait for a condition which can only be reliably tested while holding
498cc2e1c82SNeilBrown * a mutex. The variables assessed in the condition will normal be
499cc2e1c82SNeilBrown * updated under the same mutex, and the wake up should be signalled
500cc2e1c82SNeilBrown * with wake_up_var_locked() under the same mutex.
501cc2e1c82SNeilBrown *
502cc2e1c82SNeilBrown * This is similar to wait_var_event(), but assumes a mutex is held
503cc2e1c82SNeilBrown * while calling this function and while updating the variable.
504cc2e1c82SNeilBrown *
505cc2e1c82SNeilBrown * This must be called while the given mutex is held and the mutex will be
506cc2e1c82SNeilBrown * dropped when schedule() is called to wait for a wake up, and will be
507cc2e1c82SNeilBrown * reclaimed before testing the condition again.
508cc2e1c82SNeilBrown */
509cc2e1c82SNeilBrown #define wait_var_event_mutex(var, condition, lock) \
510cc2e1c82SNeilBrown wait_var_event_any_lock(var, condition, lock, mutex, TASK_UNINTERRUPTIBLE)
511cc2e1c82SNeilBrown
512cc2e1c82SNeilBrown /**
513cc2e1c82SNeilBrown * wake_up_var_protected - wake up waiters for a variable asserting that it is safe
514cc2e1c82SNeilBrown * @var: the address of the variable being waited on
515cc2e1c82SNeilBrown * @cond: the condition which afirms this is safe
516cc2e1c82SNeilBrown *
517cc2e1c82SNeilBrown * When waking waiters which use wait_var_event_any_lock() the waker must be
518cc2e1c82SNeilBrown * holding the reelvant lock to avoid races. This version of wake_up_var()
519cc2e1c82SNeilBrown * asserts that the relevant lock is held and so no barrier is needed.
520cc2e1c82SNeilBrown * The @cond is only tested when CONFIG_LOCKDEP is enabled.
521cc2e1c82SNeilBrown */
522cc2e1c82SNeilBrown #define wake_up_var_protected(var, cond) \
523cc2e1c82SNeilBrown do { \
524cc2e1c82SNeilBrown lockdep_assert(cond); \
525cc2e1c82SNeilBrown wake_up_var(var); \
526cc2e1c82SNeilBrown } while (0)
527cc2e1c82SNeilBrown
528cc2e1c82SNeilBrown /**
529cc2e1c82SNeilBrown * wake_up_var_locked - wake up waiters for a variable while holding a spinlock or mutex
530cc2e1c82SNeilBrown * @var: the address of the variable being waited on
531cc2e1c82SNeilBrown * @lock: The spinlock or mutex what protects the variable
532cc2e1c82SNeilBrown *
533cc2e1c82SNeilBrown * Send a wake up for the given variable which should be waited for with
534cc2e1c82SNeilBrown * wait_var_event_spinlock() or wait_var_event_mutex(). Unlike wake_up_var(),
535cc2e1c82SNeilBrown * no extra barriers are needed as the locking provides sufficient sequencing.
536cc2e1c82SNeilBrown */
537cc2e1c82SNeilBrown #define wake_up_var_locked(var, lock) \
538cc2e1c82SNeilBrown wake_up_var_protected(var, lockdep_is_held(lock))
539cc2e1c82SNeilBrown
540cc2e1c82SNeilBrown /**
5418236b0aeSTetsuo Handa * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
5428236b0aeSTetsuo Handa * @bit: the bit of the word being waited on
5433cdee6b3SNeilBrown * @word: the address containing the bit being waited on
5448236b0aeSTetsuo Handa *
5453cdee6b3SNeilBrown * The designated bit is cleared and any tasks waiting in wait_on_bit()
5463cdee6b3SNeilBrown * or similar will be woken. This call has RELEASE semantics so that
5473cdee6b3SNeilBrown * any changes to memory made before this call are guaranteed to be visible
5483cdee6b3SNeilBrown * after the corresponding wait_on_bit() completes.
5498236b0aeSTetsuo Handa */
clear_and_wake_up_bit(int bit,unsigned long * word)5502382d68dSNeilBrown static inline void clear_and_wake_up_bit(int bit, unsigned long *word)
5518236b0aeSTetsuo Handa {
5528236b0aeSTetsuo Handa clear_bit_unlock(bit, word);
5538236b0aeSTetsuo Handa /* See wake_up_bit() for which memory barrier you need to use. */
5548236b0aeSTetsuo Handa smp_mb__after_atomic();
5558236b0aeSTetsuo Handa wake_up_bit(word, bit);
5568236b0aeSTetsuo Handa }
5578236b0aeSTetsuo Handa
55852d633deSNeilBrown /**
55952d633deSNeilBrown * test_and_clear_wake_up_bit - clear a bit if it was set: wake up anyone waiting on that bit
56052d633deSNeilBrown * @bit: the bit of the word being waited on
56152d633deSNeilBrown * @word: the address of memory containing that bit
56252d633deSNeilBrown *
56352d633deSNeilBrown * If the bit is set and can be atomically cleared, any tasks waiting in
56452d633deSNeilBrown * wait_on_bit() or similar will be woken. This call has the same
56552d633deSNeilBrown * complete ordering semantics as test_and_clear_bit(). Any changes to
56652d633deSNeilBrown * memory made before this call are guaranteed to be visible after the
56752d633deSNeilBrown * corresponding wait_on_bit() completes.
56852d633deSNeilBrown *
56952d633deSNeilBrown * Returns %true if the bit was successfully set and the wake up was sent.
57052d633deSNeilBrown */
test_and_clear_wake_up_bit(int bit,unsigned long * word)57152d633deSNeilBrown static inline bool test_and_clear_wake_up_bit(int bit, unsigned long *word)
57252d633deSNeilBrown {
57352d633deSNeilBrown if (!test_and_clear_bit(bit, word))
57452d633deSNeilBrown return false;
57552d633deSNeilBrown /* no extra barrier required */
57652d633deSNeilBrown wake_up_bit(word, bit);
57752d633deSNeilBrown return true;
57852d633deSNeilBrown }
57952d633deSNeilBrown
58052d633deSNeilBrown /**
58152d633deSNeilBrown * atomic_dec_and_wake_up - decrement an atomic_t and if zero, wake up waiters
58252d633deSNeilBrown * @var: the variable to dec and test
58352d633deSNeilBrown *
58452d633deSNeilBrown * Decrements the atomic variable and if it reaches zero, send a wake_up to any
58552d633deSNeilBrown * processes waiting on the variable.
58652d633deSNeilBrown *
58752d633deSNeilBrown * This function has the same complete ordering semantics as atomic_dec_and_test.
58852d633deSNeilBrown *
58952d633deSNeilBrown * Returns %true is the variable reaches zero and the wake up was sent.
59052d633deSNeilBrown */
59152d633deSNeilBrown
atomic_dec_and_wake_up(atomic_t * var)59252d633deSNeilBrown static inline bool atomic_dec_and_wake_up(atomic_t *var)
59352d633deSNeilBrown {
59452d633deSNeilBrown if (!atomic_dec_and_test(var))
59552d633deSNeilBrown return false;
59652d633deSNeilBrown /* No extra barrier required */
59752d633deSNeilBrown wake_up_var(var);
59852d633deSNeilBrown return true;
59952d633deSNeilBrown }
60052d633deSNeilBrown
60152d633deSNeilBrown /**
60252d633deSNeilBrown * store_release_wake_up - update a variable and send a wake_up
60352d633deSNeilBrown * @var: the address of the variable to be updated and woken
60452d633deSNeilBrown * @val: the value to store in the variable.
60552d633deSNeilBrown *
60652d633deSNeilBrown * Store the given value in the variable send a wake up to any tasks
60752d633deSNeilBrown * waiting on the variable. All necessary barriers are included to ensure
60852d633deSNeilBrown * the task calling wait_var_event() sees the new value and all values
60952d633deSNeilBrown * written to memory before this call.
61052d633deSNeilBrown */
61152d633deSNeilBrown #define store_release_wake_up(var, val) \
61252d633deSNeilBrown do { \
61352d633deSNeilBrown smp_store_release(var, val); \
61452d633deSNeilBrown smp_mb(); \
61552d633deSNeilBrown wake_up_var(var); \
61652d633deSNeilBrown } while (0)
61752d633deSNeilBrown
6185dd43ce2SIngo Molnar #endif /* _LINUX_WAIT_BIT_H */
619