Whamcloud - gitweb
ec72376b53b8d3d583b54d5255fbdf6e59d467ce
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-wait.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBCFS_LINUX_WAIT_BIT_H
3 #define __LIBCFS_LINUX_WAIT_BIT_H
4
5 /* Make sure we can see if we have TASK_NOLOAD */
6 #include <linux/sched.h>
7 /*
8  * Linux wait-bit related types and methods:
9  */
10 #ifdef HAVE_WAIT_BIT_HEADER_H
11 #include <linux/wait_bit.h>
12 #endif
13 #include <linux/wait.h>
14
15 #ifndef HAVE_WAIT_QUEUE_ENTRY
16 #define wait_queue_entry_t wait_queue_t
17 #endif
18
19 #ifndef HAVE_WAIT_BIT_HEADER_H
20 struct wait_bit_queue_entry {
21         struct wait_bit_key     key;
22         wait_queue_entry_t      wq_entry;
23 };
24
25 #define ___wait_is_interruptible(state)                                         \
26         (!__builtin_constant_p(state) ||                                        \
27                 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE)          \
28
29 #endif /* ! HAVE_WAIT_BIT_HEADER_H */
30
31 #ifndef HAVE_PREPARE_TO_WAIT_EVENT
32 extern long prepare_to_wait_event(wait_queue_head_t *wq_head,
33                                   wait_queue_entry_t *wq_entry, int state);
34 #endif
35
36 /* ___wait_cond_timeout changed number of args in v3.12-rc1-78-g35a2af94c7ce
37  * so let's define our own ___wait_cond_timeout1
38  */
39
40 #define ___wait_cond_timeout1(condition)                                \
41 ({                                                                      \
42         bool __cond = (condition);                                      \
43         if (__cond && !__ret)                                           \
44                 __ret = 1;                                              \
45         __cond || !__ret;                                               \
46 })
47
48 #ifndef HAVE_CLEAR_AND_WAKE_UP_BIT
49 /**
50  * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
51  *
52  * @bit: the bit of the word being waited on
53  * @word: the word being waited on, a kernel virtual address
54  *
55  * You can use this helper if bitflags are manipulated atomically rather than
56  * non-atomically under a lock.
57  */
58 static inline void clear_and_wake_up_bit(int bit, void *word)
59 {
60         clear_bit_unlock(bit, word);
61         /* See wake_up_bit() for which memory barrier you need to use. */
62         smp_mb__after_atomic();
63         wake_up_bit(word, bit);
64 }
65 #endif /* ! HAVE_CLEAR_AND_WAKE_UP_BIT */
66
67 #ifndef HAVE_WAIT_VAR_EVENT
68 extern void __init wait_bit_init(void);
69 extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry,
70                                 void *var, int flags);
71 extern void wake_up_var(void *var);
72 extern wait_queue_head_t *__var_waitqueue(void *p);
73
74 #define ___wait_var_event(var, condition, state, exclusive, ret, cmd)   \
75 ({                                                                      \
76         __label__ __out;                                                \
77         wait_queue_head_t *__wq_head = __var_waitqueue(var);            \
78         struct wait_bit_queue_entry __wbq_entry;                        \
79         long __ret = ret; /* explicit shadow */                         \
80                                                                         \
81         init_wait_var_entry(&__wbq_entry, var,                          \
82                             exclusive ? WQ_FLAG_EXCLUSIVE : 0);         \
83         for (;;) {                                                      \
84                 long __int = prepare_to_wait_event(__wq_head,           \
85                                                    &__wbq_entry.wq_entry, \
86                                                    state);              \
87                 if (condition)                                          \
88                         break;                                          \
89                                                                         \
90                 if (___wait_is_interruptible(state) && __int) {         \
91                         __ret = __int;                                  \
92                         goto __out;                                     \
93                 }                                                       \
94                                                                         \
95                 cmd;                                                    \
96         }                                                               \
97         finish_wait(__wq_head, &__wbq_entry.wq_entry);                  \
98 __out:  __ret;                                                          \
99 })
100
101 #define __wait_var_event(var, condition)                                \
102         ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0,   \
103                           schedule())
104
105 #define wait_var_event(var, condition)                                  \
106 do {                                                                    \
107         might_sleep();                                                  \
108         if (condition)                                                  \
109                 break;                                                  \
110         __wait_var_event(var, condition);                               \
111 } while (0)
112
113 #define __wait_var_event_killable(var, condition)                       \
114         ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0,          \
115                           schedule())
116
117 #define wait_var_event_killable(var, condition)                         \
118 ({                                                                      \
119         int __ret = 0;                                                  \
120         might_sleep();                                                  \
121         if (!(condition))                                               \
122                 __ret = __wait_var_event_killable(var, condition);      \
123         __ret;                                                          \
124 })
125
126 #define __wait_var_event_timeout(var, condition, timeout)               \
127         ___wait_var_event(var, ___wait_cond_timeout1(condition),        \
128                           TASK_UNINTERRUPTIBLE, 0, timeout,             \
129                           __ret = schedule_timeout(__ret))
130
131 #define wait_var_event_timeout(var, condition, timeout)                 \
132 ({                                                                      \
133         long __ret = timeout;                                           \
134         might_sleep();                                                  \
135         if (!___wait_cond_timeout1(condition))                          \
136                 __ret = __wait_var_event_timeout(var, condition, timeout); \
137         __ret;                                                          \
138 })
139 #endif /* ! HAVE_WAIT_VAR_EVENT */
140
141 /*
142  * prepare_to_wait_event() does not support an exclusive
143  * lifo wait.
144  * However it will not relink the wait_queue_entry if
145  * it is already linked.  So we link to the head of the
146  * queue here, and it will stay there.
147  */
148 static inline void prepare_to_wait_exclusive_head(
149         wait_queue_head_t *waitq, wait_queue_entry_t *link)
150 {
151         unsigned long flags;
152
153         spin_lock_irqsave(&(waitq->lock), flags);
154 #ifdef HAVE_WAIT_QUEUE_ENTRY_LIST
155         if (list_empty(&link->entry))
156 #else
157         if (list_empty(&link->task_list))
158 #endif
159                 __add_wait_queue_exclusive(waitq, link);
160         spin_unlock_irqrestore(&((waitq)->lock), flags);
161 }
162
163 #ifndef ___wait_event
164 /*
165  * The below macro ___wait_event() has an explicit shadow of the __ret
166  * variable when used from the wait_event_*() macros.
167  *
168  * This is so that both can use the ___wait_cond_timeout1() construct
169  * to wrap the condition.
170  *
171  * The type inconsistency of the wait_event_*() __ret variable is also
172  * on purpose; we use long where we can return timeout values and int
173  * otherwise.
174  */
175
176 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd)   \
177 ({                                                                      \
178         __label__ __out;                                                \
179         wait_queue_entry_ __wq_entry;                                   \
180         long __ret = ret;       /* explicit shadow */                   \
181                                                                         \
182         init_wait(&__wq_entry);                                         \
183         if (exclusive)                                                  \
184                 __wq_entry.flags = WQ_FLAG_EXCLUSIVE                    \
185         for (;;) {                                                      \
186                 long __int = prepare_to_wait_event(&wq_head,            \
187                                                   &__wq_entry, state);  \
188                                                                         \
189                 if (condition)                                          \
190                         break;                                          \
191                                                                         \
192                 if (___wait_is_interruptible(state) && __int) {         \
193                         __ret = __int;                                  \
194                         goto __out;                                     \
195                 }                                                       \
196                                                                         \
197                 cmd;                                                    \
198         }                                                               \
199         finish_wait(&wq_head, &__wq_entry);                             \
200 __out:  __ret;                                                          \
201 })
202 #endif
203
204 #ifndef TASK_NOLOAD
205
206 #define ___wait_event_idle(wq_head, condition, exclusive, ret, cmd)     \
207 ({                                                                      \
208         wait_queue_entry_t __wq_entry;                                  \
209         long __ret = ret;       /* explicit shadow */                   \
210         sigset_t __blocked;                                             \
211                                                                         \
212         __blocked = cfs_block_sigsinv(0);                               \
213         init_wait(&__wq_entry);                                         \
214         if (exclusive)                                                  \
215                 __wq_entry.flags = WQ_FLAG_EXCLUSIVE;                   \
216         for (;;) {                                                      \
217                 prepare_to_wait_event(&wq_head,                         \
218                                    &__wq_entry,                         \
219                                    TASK_INTERRUPTIBLE);                 \
220                                                                         \
221                 if (condition)                                          \
222                         break;                                          \
223                 /* See justification in __l_wait_event */               \
224                 if (signal_pending(current))                            \
225                         cfs_clear_sigpending();                         \
226                                                                         \
227                 cmd;                                                    \
228         }                                                               \
229         finish_wait(&wq_head, &__wq_entry);                             \
230         cfs_restore_sigs(__blocked);                                    \
231         __ret;                                                          \
232 })
233
234 #define wait_event_idle(wq_head, condition)                             \
235 do {                                                                    \
236         might_sleep();                                                  \
237         if (!(condition))                                               \
238                 ___wait_event_idle(wq_head, condition, 0, 0, schedule());\
239 } while (0)
240
241 #define wait_event_idle_exclusive(wq_head, condition)                   \
242 do {                                                                    \
243         might_sleep();                                                  \
244         if (!(condition))                                               \
245                 ___wait_event_idle(wq_head, condition, 1, 0, schedule());\
246 } while (0)
247
248 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout)\
249         ___wait_event_idle(wq_head, ___wait_cond_timeout1(condition),   \
250                            1, timeout,                                  \
251                            __ret = schedule_timeout(__ret))
252
253 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout)  \
254 ({                                                                      \
255         long __ret = timeout;                                           \
256         might_sleep();                                                  \
257         if (!___wait_cond_timeout1(condition))                          \
258                 __ret = __wait_event_idle_exclusive_timeout(            \
259                         wq_head, condition, timeout);                   \
260         __ret;                                                          \
261 })
262
263 #define __wait_event_idle_exclusive_timeout_cmd(wq_head, condition,     \
264                                                 timeout, cmd1, cmd2)    \
265         ___wait_event_idle(wq_head, ___wait_cond_timeout1(condition),   \
266                            1, timeout,                                  \
267                            cmd1; __ret = schedule_timeout(__ret); cmd2)
268
269 #define wait_event_idle_exclusive_timeout_cmd(wq_head, condition, timeout,\
270                                               cmd1, cmd2)               \
271 ({                                                                      \
272         long __ret = timeout;                                           \
273         might_sleep();                                                  \
274         if (!___wait_cond_timeout1(condition))                          \
275                 __ret = __wait_event_idle_exclusive_timeout_cmd(        \
276                         wq_head, condition, timeout, cmd1, cmd2);       \
277         __ret;                                                          \
278 })
279
280 #define __wait_event_idle_timeout(wq_head, condition, timeout)          \
281         ___wait_event_idle(wq_head, ___wait_cond_timeout1(condition),   \
282                            0, timeout,                                  \
283                            __ret = schedule_timeout(__ret))
284
285 #define wait_event_idle_timeout(wq_head, condition, timeout)            \
286 ({                                                                      \
287         long __ret = timeout;                                           \
288         might_sleep();                                                  \
289         if (!___wait_cond_timeout1(condition))                          \
290                 __ret = __wait_event_idle_timeout(wq_head, condition,   \
291                                                   timeout);             \
292         __ret;                                                          \
293 })
294
295 #else /* TASK_IDLE */
296 #ifndef wait_event_idle
297 /**
298  * wait_event_idle - wait for a condition without contributing to system load
299  * @wq_head: the waitqueue to wait on
300  * @condition: a C expression for the event to wait for
301  *
302  * The process is put to sleep (TASK_IDLE) until the
303  * @condition evaluates to true.
304  * The @condition is checked each time the waitqueue @wq_head is woken up.
305  *
306  * wake_up() has to be called after changing any variable that could
307  * change the result of the wait condition.
308  *
309  */
310 #define wait_event_idle(wq_head, condition)                             \
311 do {                                                                    \
312         might_sleep();                                                  \
313         if (!(condition))                                               \
314                 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0,      \
315                               schedule());                              \
316 } while (0)
317 #endif
318 #ifndef wait_event_idle_exclusive
319 /**
320  * wait_event_idle_exclusive - wait for a condition without contributing to
321  *               system load
322  * @wq_head: the waitqueue to wait on
323  * @condition: a C expression for the event to wait for
324  *
325  * The process is put to sleep (TASK_IDLE) until the
326  * @condition evaluates to true.
327  * The @condition is checked each time the waitqueue @wq_head is woken up.
328  *
329  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
330  * set thus if other processes wait on the same list, when this
331  * process is woken further processes are not considered.
332  *
333  * wake_up() has to be called after changing any variable that could
334  * change the result of the wait condition.
335  *
336  */
337 #define wait_event_idle_exclusive(wq_head, condition)                   \
338 do {                                                                    \
339         might_sleep();                                                  \
340         if (!(condition))                                               \
341                 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0,      \
342                               schedule());                              \
343 } while (0)
344 #endif
345 #ifndef wait_event_idle_exclusive_timeout
346 /**
347  * wait_event_idle_exclusive_timeout - sleep without load until a condition
348  *                       becomes true or a timeout elapses
349  * @wq_head: the waitqueue to wait on
350  * @condition: a C expression for the event to wait for
351  * @timeout: timeout, in jiffies
352  *
353  * The process is put to sleep (TASK_IDLE) until the
354  * @condition evaluates to true. The @condition is checked each time
355  * the waitqueue @wq_head is woken up.
356  *
357  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
358  * set thus if other processes wait on the same list, when this
359  * process is woken further processes are not considered.
360  *
361  * wake_up() has to be called after changing any variable that could
362  * change the result of the wait condition.
363  *
364  * Returns:
365  * 0 if the @condition evaluated to %false after the @timeout elapsed,
366  * 1 if the @condition evaluated to %true after the @timeout elapsed,
367  * or the remaining jiffies (at least 1) if the @condition evaluated
368  * to %true before the @timeout elapsed.
369  */
370 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout)  \
371 ({                                                                      \
372         long __ret = timeout;                                           \
373         might_sleep();                                                  \
374         if (!___wait_cond_timeout1(condition))                          \
375                 __ret = __wait_event_idle_exclusive_timeout(wq_head,    \
376                                                             condition,  \
377                                                             timeout);   \
378         __ret;                                                          \
379 })
380 #endif
381 #ifndef wait_event_idle_exclusive_timeout_cmd
382 #define __wait_event_idle_exclusive_timeout_cmd(wq_head, condition,     \
383                                                 timeout, cmd1, cmd2)    \
384         ___wait_event(wq_head, ___wait_cond_timeout1(condition),        \
385                       TASK_IDLE, 1, timeout,                            \
386                       cmd1; __ret = schedule_timeout(__ret); cmd2)
387
388 #define wait_event_idle_exclusive_timeout_cmd(wq_head, condition, timeout,\
389                                               cmd1, cmd2)               \
390 ({                                                                      \
391         long __ret = timeout;                                           \
392         might_sleep();                                                  \
393         if (!___wait_cond_timeout1(condition))                          \
394                 __ret = __wait_event_idle_exclusive_timeout_cmd(        \
395                         wq_head, condition, timeout, cmd1, cmd2);       \
396         __ret;                                                          \
397 })
398 #endif
399
400 #ifndef wait_event_idle_timeout
401
402 #define __wait_event_idle_timeout(wq_head, condition, timeout)          \
403         ___wait_event(wq_head, ___wait_cond_timeout1(condition),        \
404                       TASK_IDLE, 0, timeout,                            \
405                       __ret = schedule_timeout(__ret))
406
407 /**
408  * wait_event_idle_timeout - sleep without load until a condition becomes
409  *                           true or a timeout elapses
410  * @wq_head: the waitqueue to wait on
411  * @condition: a C expression for the event to wait for
412  * @timeout: timeout, in jiffies
413  *
414  * The process is put to sleep (TASK_IDLE) until the
415  * @condition evaluates to true. The @condition is checked each time
416  * the waitqueue @wq_head is woken up.
417  *
418  * wake_up() has to be called after changing any variable that could
419  * change the result of the wait condition.
420  *
421  * Returns:
422  * 0 if the @condition evaluated to %false after the @timeout elapsed,
423  * 1 if the @condition evaluated to %true after the @timeout elapsed,
424  * or the remaining jiffies (at least 1) if the @condition evaluated
425  * to %true before the @timeout elapsed.
426  */
427 #define wait_event_idle_timeout(wq_head, condition, timeout)            \
428 ({                                                                      \
429         long __ret = timeout;                                           \
430         might_sleep();                                                  \
431         if (!___wait_cond_timeout1(condition))                          \
432                 __ret = __wait_event_idle_timeout(wq_head, condition,   \
433                                                   timeout);             \
434         __ret;                                                          \
435 })
436 #endif
437 #endif /* TASK_IDLE */
438
439 /* ___wait_event_lifo is used for lifo exclusive 'idle' waits */
440 #ifdef TASK_NOLOAD
441
442 #define ___wait_event_lifo(wq_head, condition, ret, cmd)                \
443 ({                                                                      \
444         wait_queue_entry_t       __wq_entry;                            \
445         long __ret = ret;       /* explicit shadow */                   \
446                                                                         \
447         init_wait(&__wq_entry);                                         \
448         __wq_entry.flags =  WQ_FLAG_EXCLUSIVE;                          \
449         for (;;) {                                                      \
450                 prepare_to_wait_exclusive_head(&wq_head, &__wq_entry);  \
451                 prepare_to_wait_event(&wq_head, &__wq_entry, TASK_IDLE);\
452                                                                         \
453                 if (condition)                                          \
454                         break;                                          \
455                                                                         \
456                 cmd;                                                    \
457         }                                                               \
458         finish_wait(&wq_head, &__wq_entry);                             \
459         __ret;                                                          \
460 })
461 #else
462 #define ___wait_event_lifo(wq_head, condition, ret, cmd)                \
463 ({                                                                      \
464         wait_queue_entry_t __wq_entry;                                  \
465         long __ret = ret;       /* explicit shadow */                   \
466         sigset_t __blocked;                                             \
467                                                                         \
468         __blocked = cfs_block_sigsinv(0);                               \
469         init_wait(&__wq_entry);                                         \
470         __wq_entry.flags = WQ_FLAG_EXCLUSIVE;                           \
471         for (;;) {                                                      \
472                 prepare_to_wait_exclusive_head(&wq_head, &__wq_entry);  \
473                 prepare_to_wait_event(&wq_head, &__wq_entry,            \
474                                       TASK_INTERRUPTIBLE);              \
475                                                                         \
476                 if (condition)                                          \
477                         break;                                          \
478                 /* See justification in __l_wait_event */               \
479                 if (signal_pending(current))                            \
480                         cfs_clear_sigpending();                         \
481                                                                         \
482                 cmd;                                                    \
483         }                                                               \
484         cfs_restore_sigs(__blocked);                                    \
485         finish_wait(&wq_head, &__wq_entry);                             \
486         __ret;                                                          \
487 })
488 #endif
489
490 #define wait_event_idle_exclusive_lifo(wq_head, condition)              \
491 do {                                                                    \
492         might_sleep();                                                  \
493         if (!(condition))                                               \
494                 ___wait_event_lifo(wq_head, condition, 0, schedule());  \
495 } while (0)
496
497 #define __wait_event_idle_lifo_timeout(wq_head, condition, timeout)     \
498         ___wait_event_lifo(wq_head, ___wait_cond_timeout1(condition),   \
499                            timeout,                                     \
500                            __ret = schedule_timeout(__ret))
501
502 #define wait_event_idle_exclusive_lifo_timeout(wq_head, condition, timeout)\
503 ({                                                                      \
504         long __ret = timeout;                                           \
505         might_sleep();                                                  \
506         if (!___wait_cond_timeout1(condition))                          \
507                 __ret = __wait_event_idle_lifo_timeout(wq_head,         \
508                                                        condition,       \
509                                                        timeout);        \
510         __ret;                                                          \
511 })
512
513 /* l_wait_event_abortable() is a bit like wait_event_killable()
514  * except there is a fixed set of signals which will abort:
515  * LUSTRE_FATAL_SIGS
516  */
517 #define LUSTRE_FATAL_SIGS                                        \
518         (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGTERM) | \
519          sigmask(SIGQUIT) | sigmask(SIGALRM))
520
521 #define l_wait_event_abortable(wq, condition)                           \
522 ({                                                                      \
523         sigset_t __new_blocked, __old_blocked;                          \
524         int __ret = 0;                                                  \
525         siginitsetinv(&__new_blocked, LUSTRE_FATAL_SIGS);               \
526         sigprocmask(SIG_BLOCK, &__new_blocked, &__old_blocked);         \
527         __ret = wait_event_interruptible(wq, condition);                \
528         sigprocmask(SIG_SETMASK, &__old_blocked, NULL);                 \
529         __ret;                                                          \
530 })
531
532 #define l_wait_event_abortable_timeout(wq, condition, timeout)          \
533 ({                                                                      \
534         sigset_t __new_blocked, __old_blocked;                          \
535         int __ret = 0;                                                  \
536         siginitsetinv(&__new_blocked, LUSTRE_FATAL_SIGS);               \
537         sigprocmask(SIG_BLOCK, &__new_blocked, &__old_blocked);         \
538         __ret = wait_event_interruptible_timeout(wq, condition, timeout);\
539         sigprocmask(SIG_SETMASK, &__old_blocked, NULL);                 \
540         __ret;                                                          \
541 })
542
543 #define l_wait_event_abortable_exclusive(wq, condition)                 \
544 ({                                                                      \
545         sigset_t __new_blocked, __old_blocked;                          \
546         int __ret = 0;                                                  \
547         siginitsetinv(&__new_blocked, LUSTRE_FATAL_SIGS);               \
548         sigprocmask(SIG_BLOCK, &__new_blocked, &__old_blocked);         \
549         __ret = wait_event_interruptible_exclusive(wq, condition);      \
550         sigprocmask(SIG_SETMASK, &__old_blocked, NULL);                 \
551         __ret;                                                          \
552 })
553
554 #endif /* __LICBFS_LINUX_WAIT_BIT_H */