1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_WAIT_BIT_H
3*4882a593Smuzhiyun #define _LINUX_WAIT_BIT_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * Linux wait-bit related types and methods:
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun #include <linux/wait.h>
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun struct wait_bit_key {
11*4882a593Smuzhiyun void *flags;
12*4882a593Smuzhiyun int bit_nr;
13*4882a593Smuzhiyun unsigned long timeout;
14*4882a593Smuzhiyun };
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun struct wait_bit_queue_entry {
17*4882a593Smuzhiyun struct wait_bit_key key;
18*4882a593Smuzhiyun struct wait_queue_entry wq_entry;
19*4882a593Smuzhiyun };
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
22*4882a593Smuzhiyun { .flags = word, .bit_nr = bit, }
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun typedef int wait_bit_action_f(struct wait_bit_key *key, int mode);
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit);
27*4882a593Smuzhiyun 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);
28*4882a593Smuzhiyun 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);
29*4882a593Smuzhiyun void wake_up_bit(void *word, int bit);
30*4882a593Smuzhiyun int out_of_line_wait_on_bit(void *word, int, wait_bit_action_f *action, unsigned int mode);
31*4882a593Smuzhiyun int out_of_line_wait_on_bit_timeout(void *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout);
32*4882a593Smuzhiyun int out_of_line_wait_on_bit_lock(void *word, int, wait_bit_action_f *action, unsigned int mode);
33*4882a593Smuzhiyun struct wait_queue_head *bit_waitqueue(void *word, int bit);
34*4882a593Smuzhiyun extern void __init wait_bit_init(void);
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #define DEFINE_WAIT_BIT(name, word, bit) \
39*4882a593Smuzhiyun struct wait_bit_queue_entry name = { \
40*4882a593Smuzhiyun .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
41*4882a593Smuzhiyun .wq_entry = { \
42*4882a593Smuzhiyun .private = current, \
43*4882a593Smuzhiyun .func = wake_bit_function, \
44*4882a593Smuzhiyun .entry = \
45*4882a593Smuzhiyun LIST_HEAD_INIT((name).wq_entry.entry), \
46*4882a593Smuzhiyun }, \
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun extern int bit_wait(struct wait_bit_key *key, int mode);
50*4882a593Smuzhiyun extern int bit_wait_io(struct wait_bit_key *key, int mode);
51*4882a593Smuzhiyun extern int bit_wait_timeout(struct wait_bit_key *key, int mode);
52*4882a593Smuzhiyun extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun * wait_on_bit - wait for a bit to be cleared
56*4882a593Smuzhiyun * @word: the word being waited on, a kernel virtual address
57*4882a593Smuzhiyun * @bit: the bit of the word being waited on
58*4882a593Smuzhiyun * @mode: the task state to sleep in
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * There is a standard hashed waitqueue table for generic use. This
61*4882a593Smuzhiyun * is the part of the hashtable's accessor API that waits on a bit.
62*4882a593Smuzhiyun * For instance, if one were to have waiters on a bitflag, one would
63*4882a593Smuzhiyun * call wait_on_bit() in threads waiting for the bit to clear.
64*4882a593Smuzhiyun * One uses wait_on_bit() where one is waiting for the bit to clear,
65*4882a593Smuzhiyun * but has no intention of setting it.
66*4882a593Smuzhiyun * Returned value will be zero if the bit was cleared, or non-zero
67*4882a593Smuzhiyun * if the process received a signal and the mode permitted wakeup
68*4882a593Smuzhiyun * on that signal.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun static inline int
wait_on_bit(unsigned long * word,int bit,unsigned mode)71*4882a593Smuzhiyun wait_on_bit(unsigned long *word, int bit, unsigned mode)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun might_sleep();
74*4882a593Smuzhiyun if (!test_bit(bit, word))
75*4882a593Smuzhiyun return 0;
76*4882a593Smuzhiyun return out_of_line_wait_on_bit(word, bit,
77*4882a593Smuzhiyun bit_wait,
78*4882a593Smuzhiyun mode);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /**
82*4882a593Smuzhiyun * wait_on_bit_io - wait for a bit to be cleared
83*4882a593Smuzhiyun * @word: the word being waited on, a kernel virtual address
84*4882a593Smuzhiyun * @bit: the bit of the word being waited on
85*4882a593Smuzhiyun * @mode: the task state to sleep in
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * Use the standard hashed waitqueue table to wait for a bit
88*4882a593Smuzhiyun * to be cleared. This is similar to wait_on_bit(), but calls
89*4882a593Smuzhiyun * io_schedule() instead of schedule() for the actual waiting.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * Returned value will be zero if the bit was cleared, or non-zero
92*4882a593Smuzhiyun * if the process received a signal and the mode permitted wakeup
93*4882a593Smuzhiyun * on that signal.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun static inline int
wait_on_bit_io(unsigned long * word,int bit,unsigned mode)96*4882a593Smuzhiyun wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun might_sleep();
99*4882a593Smuzhiyun if (!test_bit(bit, word))
100*4882a593Smuzhiyun return 0;
101*4882a593Smuzhiyun return out_of_line_wait_on_bit(word, bit,
102*4882a593Smuzhiyun bit_wait_io,
103*4882a593Smuzhiyun mode);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /**
107*4882a593Smuzhiyun * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
108*4882a593Smuzhiyun * @word: the word being waited on, a kernel virtual address
109*4882a593Smuzhiyun * @bit: the bit of the word being waited on
110*4882a593Smuzhiyun * @mode: the task state to sleep in
111*4882a593Smuzhiyun * @timeout: timeout, in jiffies
112*4882a593Smuzhiyun *
113*4882a593Smuzhiyun * Use the standard hashed waitqueue table to wait for a bit
114*4882a593Smuzhiyun * to be cleared. This is similar to wait_on_bit(), except also takes a
115*4882a593Smuzhiyun * timeout parameter.
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * Returned value will be zero if the bit was cleared before the
118*4882a593Smuzhiyun * @timeout elapsed, or non-zero if the @timeout elapsed or process
119*4882a593Smuzhiyun * received a signal and the mode permitted wakeup on that signal.
120*4882a593Smuzhiyun */
121*4882a593Smuzhiyun static inline int
wait_on_bit_timeout(unsigned long * word,int bit,unsigned mode,unsigned long timeout)122*4882a593Smuzhiyun wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
123*4882a593Smuzhiyun unsigned long timeout)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun might_sleep();
126*4882a593Smuzhiyun if (!test_bit(bit, word))
127*4882a593Smuzhiyun return 0;
128*4882a593Smuzhiyun return out_of_line_wait_on_bit_timeout(word, bit,
129*4882a593Smuzhiyun bit_wait_timeout,
130*4882a593Smuzhiyun mode, timeout);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /**
134*4882a593Smuzhiyun * wait_on_bit_action - wait for a bit to be cleared
135*4882a593Smuzhiyun * @word: the word being waited on, a kernel virtual address
136*4882a593Smuzhiyun * @bit: the bit of the word being waited on
137*4882a593Smuzhiyun * @action: the function used to sleep, which may take special actions
138*4882a593Smuzhiyun * @mode: the task state to sleep in
139*4882a593Smuzhiyun *
140*4882a593Smuzhiyun * Use the standard hashed waitqueue table to wait for a bit
141*4882a593Smuzhiyun * to be cleared, and allow the waiting action to be specified.
142*4882a593Smuzhiyun * This is like wait_on_bit() but allows fine control of how the waiting
143*4882a593Smuzhiyun * is done.
144*4882a593Smuzhiyun *
145*4882a593Smuzhiyun * Returned value will be zero if the bit was cleared, or non-zero
146*4882a593Smuzhiyun * if the process received a signal and the mode permitted wakeup
147*4882a593Smuzhiyun * on that signal.
148*4882a593Smuzhiyun */
149*4882a593Smuzhiyun static inline int
wait_on_bit_action(unsigned long * word,int bit,wait_bit_action_f * action,unsigned mode)150*4882a593Smuzhiyun wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
151*4882a593Smuzhiyun unsigned mode)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun might_sleep();
154*4882a593Smuzhiyun if (!test_bit(bit, word))
155*4882a593Smuzhiyun return 0;
156*4882a593Smuzhiyun return out_of_line_wait_on_bit(word, bit, action, mode);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /**
160*4882a593Smuzhiyun * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
161*4882a593Smuzhiyun * @word: the word being waited on, a kernel virtual address
162*4882a593Smuzhiyun * @bit: the bit of the word being waited on
163*4882a593Smuzhiyun * @mode: the task state to sleep in
164*4882a593Smuzhiyun *
165*4882a593Smuzhiyun * There is a standard hashed waitqueue table for generic use. This
166*4882a593Smuzhiyun * is the part of the hashtable's accessor API that waits on a bit
167*4882a593Smuzhiyun * when one intends to set it, for instance, trying to lock bitflags.
168*4882a593Smuzhiyun * For instance, if one were to have waiters trying to set bitflag
169*4882a593Smuzhiyun * and waiting for it to clear before setting it, one would call
170*4882a593Smuzhiyun * wait_on_bit() in threads waiting to be able to set the bit.
171*4882a593Smuzhiyun * One uses wait_on_bit_lock() where one is waiting for the bit to
172*4882a593Smuzhiyun * clear with the intention of setting it, and when done, clearing it.
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * Returns zero if the bit was (eventually) found to be clear and was
175*4882a593Smuzhiyun * set. Returns non-zero if a signal was delivered to the process and
176*4882a593Smuzhiyun * the @mode allows that signal to wake the process.
177*4882a593Smuzhiyun */
178*4882a593Smuzhiyun static inline int
wait_on_bit_lock(unsigned long * word,int bit,unsigned mode)179*4882a593Smuzhiyun wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun might_sleep();
182*4882a593Smuzhiyun if (!test_and_set_bit(bit, word))
183*4882a593Smuzhiyun return 0;
184*4882a593Smuzhiyun return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun /**
188*4882a593Smuzhiyun * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
189*4882a593Smuzhiyun * @word: the word being waited on, a kernel virtual address
190*4882a593Smuzhiyun * @bit: the bit of the word being waited on
191*4882a593Smuzhiyun * @mode: the task state to sleep in
192*4882a593Smuzhiyun *
193*4882a593Smuzhiyun * Use the standard hashed waitqueue table to wait for a bit
194*4882a593Smuzhiyun * to be cleared and then to atomically set it. This is similar
195*4882a593Smuzhiyun * to wait_on_bit(), but calls io_schedule() instead of schedule()
196*4882a593Smuzhiyun * for the actual waiting.
197*4882a593Smuzhiyun *
198*4882a593Smuzhiyun * Returns zero if the bit was (eventually) found to be clear and was
199*4882a593Smuzhiyun * set. Returns non-zero if a signal was delivered to the process and
200*4882a593Smuzhiyun * the @mode allows that signal to wake the process.
201*4882a593Smuzhiyun */
202*4882a593Smuzhiyun static inline int
wait_on_bit_lock_io(unsigned long * word,int bit,unsigned mode)203*4882a593Smuzhiyun wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun might_sleep();
206*4882a593Smuzhiyun if (!test_and_set_bit(bit, word))
207*4882a593Smuzhiyun return 0;
208*4882a593Smuzhiyun return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /**
212*4882a593Smuzhiyun * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
213*4882a593Smuzhiyun * @word: the word being waited on, a kernel virtual address
214*4882a593Smuzhiyun * @bit: the bit of the word being waited on
215*4882a593Smuzhiyun * @action: the function used to sleep, which may take special actions
216*4882a593Smuzhiyun * @mode: the task state to sleep in
217*4882a593Smuzhiyun *
218*4882a593Smuzhiyun * Use the standard hashed waitqueue table to wait for a bit
219*4882a593Smuzhiyun * to be cleared and then to set it, and allow the waiting action
220*4882a593Smuzhiyun * to be specified.
221*4882a593Smuzhiyun * This is like wait_on_bit() but allows fine control of how the waiting
222*4882a593Smuzhiyun * is done.
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * Returns zero if the bit was (eventually) found to be clear and was
225*4882a593Smuzhiyun * set. Returns non-zero if a signal was delivered to the process and
226*4882a593Smuzhiyun * the @mode allows that signal to wake the process.
227*4882a593Smuzhiyun */
228*4882a593Smuzhiyun static inline int
wait_on_bit_lock_action(unsigned long * word,int bit,wait_bit_action_f * action,unsigned mode)229*4882a593Smuzhiyun wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
230*4882a593Smuzhiyun unsigned mode)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun might_sleep();
233*4882a593Smuzhiyun if (!test_and_set_bit(bit, word))
234*4882a593Smuzhiyun return 0;
235*4882a593Smuzhiyun return out_of_line_wait_on_bit_lock(word, bit, action, mode);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags);
239*4882a593Smuzhiyun extern void wake_up_var(void *var);
240*4882a593Smuzhiyun extern wait_queue_head_t *__var_waitqueue(void *p);
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun #define ___wait_var_event(var, condition, state, exclusive, ret, cmd) \
243*4882a593Smuzhiyun ({ \
244*4882a593Smuzhiyun __label__ __out; \
245*4882a593Smuzhiyun struct wait_queue_head *__wq_head = __var_waitqueue(var); \
246*4882a593Smuzhiyun struct wait_bit_queue_entry __wbq_entry; \
247*4882a593Smuzhiyun long __ret = ret; /* explicit shadow */ \
248*4882a593Smuzhiyun \
249*4882a593Smuzhiyun init_wait_var_entry(&__wbq_entry, var, \
250*4882a593Smuzhiyun exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
251*4882a593Smuzhiyun for (;;) { \
252*4882a593Smuzhiyun long __int = prepare_to_wait_event(__wq_head, \
253*4882a593Smuzhiyun &__wbq_entry.wq_entry, \
254*4882a593Smuzhiyun state); \
255*4882a593Smuzhiyun if (condition) \
256*4882a593Smuzhiyun break; \
257*4882a593Smuzhiyun \
258*4882a593Smuzhiyun if (___wait_is_interruptible(state) && __int) { \
259*4882a593Smuzhiyun __ret = __int; \
260*4882a593Smuzhiyun goto __out; \
261*4882a593Smuzhiyun } \
262*4882a593Smuzhiyun \
263*4882a593Smuzhiyun cmd; \
264*4882a593Smuzhiyun } \
265*4882a593Smuzhiyun finish_wait(__wq_head, &__wbq_entry.wq_entry); \
266*4882a593Smuzhiyun __out: __ret; \
267*4882a593Smuzhiyun })
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun #define __wait_var_event(var, condition) \
270*4882a593Smuzhiyun ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
271*4882a593Smuzhiyun schedule())
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun #define wait_var_event(var, condition) \
274*4882a593Smuzhiyun do { \
275*4882a593Smuzhiyun might_sleep(); \
276*4882a593Smuzhiyun if (condition) \
277*4882a593Smuzhiyun break; \
278*4882a593Smuzhiyun __wait_var_event(var, condition); \
279*4882a593Smuzhiyun } while (0)
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun #define __wait_var_event_killable(var, condition) \
282*4882a593Smuzhiyun ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \
283*4882a593Smuzhiyun schedule())
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun #define wait_var_event_killable(var, condition) \
286*4882a593Smuzhiyun ({ \
287*4882a593Smuzhiyun int __ret = 0; \
288*4882a593Smuzhiyun might_sleep(); \
289*4882a593Smuzhiyun if (!(condition)) \
290*4882a593Smuzhiyun __ret = __wait_var_event_killable(var, condition); \
291*4882a593Smuzhiyun __ret; \
292*4882a593Smuzhiyun })
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun #define __wait_var_event_timeout(var, condition, timeout) \
295*4882a593Smuzhiyun ___wait_var_event(var, ___wait_cond_timeout(condition), \
296*4882a593Smuzhiyun TASK_UNINTERRUPTIBLE, 0, timeout, \
297*4882a593Smuzhiyun __ret = schedule_timeout(__ret))
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun #define wait_var_event_timeout(var, condition, timeout) \
300*4882a593Smuzhiyun ({ \
301*4882a593Smuzhiyun long __ret = timeout; \
302*4882a593Smuzhiyun might_sleep(); \
303*4882a593Smuzhiyun if (!___wait_cond_timeout(condition)) \
304*4882a593Smuzhiyun __ret = __wait_var_event_timeout(var, condition, timeout); \
305*4882a593Smuzhiyun __ret; \
306*4882a593Smuzhiyun })
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun #define __wait_var_event_interruptible(var, condition) \
309*4882a593Smuzhiyun ___wait_var_event(var, condition, TASK_INTERRUPTIBLE, 0, 0, \
310*4882a593Smuzhiyun schedule())
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun #define wait_var_event_interruptible(var, condition) \
313*4882a593Smuzhiyun ({ \
314*4882a593Smuzhiyun int __ret = 0; \
315*4882a593Smuzhiyun might_sleep(); \
316*4882a593Smuzhiyun if (!(condition)) \
317*4882a593Smuzhiyun __ret = __wait_var_event_interruptible(var, condition); \
318*4882a593Smuzhiyun __ret; \
319*4882a593Smuzhiyun })
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun /**
322*4882a593Smuzhiyun * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * @bit: the bit of the word being waited on
325*4882a593Smuzhiyun * @word: the word being waited on, a kernel virtual address
326*4882a593Smuzhiyun *
327*4882a593Smuzhiyun * You can use this helper if bitflags are manipulated atomically rather than
328*4882a593Smuzhiyun * non-atomically under a lock.
329*4882a593Smuzhiyun */
clear_and_wake_up_bit(int bit,void * word)330*4882a593Smuzhiyun static inline void clear_and_wake_up_bit(int bit, void *word)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun clear_bit_unlock(bit, word);
333*4882a593Smuzhiyun /* See wake_up_bit() for which memory barrier you need to use. */
334*4882a593Smuzhiyun smp_mb__after_atomic();
335*4882a593Smuzhiyun wake_up_bit(word, bit);
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun #endif /* _LINUX_WAIT_BIT_H */
339