Whamcloud - gitweb
LU-11025 dne: directory restripe and auto split
[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 TASK_IDLE TASK_INTERRUPTIBLE
207
208 #define ___wait_event_idle(wq_head, condition, exclusive, ret, cmd)     \
209 ({                                                                      \
210         wait_queue_entry_t __wq_entry;                                  \
211         unsigned long flags;                                            \
212         long __ret = ret;       /* explicit shadow */                   \
213         sigset_t __old_blocked, __new_blocked;                          \
214                                                                         \
215         siginitset(&__new_blocked, LUSTRE_FATAL_SIGS);                  \
216         sigprocmask(0, &__new_blocked, &__old_blocked);                 \
217         init_wait(&__wq_entry);                                         \
218         if (exclusive)                                                  \
219                 __wq_entry.flags = WQ_FLAG_EXCLUSIVE;                   \
220         for (;;) {                                                      \
221                 prepare_to_wait_event(&wq_head,                         \
222                                    &__wq_entry,                         \
223                                    TASK_INTERRUPTIBLE);                 \
224                                                                         \
225                 if (condition)                                          \
226                         break;                                          \
227                 /* We have to do this here because some signals */      \
228                 /* are not blockable - ie from strace(1).       */      \
229                 /* In these cases we want to schedule_timeout() */      \
230                 /* again, because we don't want that to return  */      \
231                 /* -EINTR when the RPC actually succeeded.      */      \
232                 /* the recalc_sigpending() below will deliver the */    \
233                 /* signal properly.                             */      \
234                 if (signal_pending(current)) {                          \
235                         spin_lock_irqsave(&current->sighand->siglock,   \
236                                           flags);                       \
237                         clear_tsk_thread_flag(current, TIF_SIGPENDING); \
238                         spin_unlock_irqrestore(&current->sighand->siglock,\
239                                                flags);                  \
240                 }                                                       \
241                 cmd;                                                    \
242         }                                                               \
243         finish_wait(&wq_head, &__wq_entry);                             \
244         sigprocmask(SIG_SETMASK, &__old_blocked, NULL);                 \
245         __ret;                                                          \
246 })
247
248 #define wait_event_idle(wq_head, condition)                             \
249 do {                                                                    \
250         might_sleep();                                                  \
251         if (!(condition))                                               \
252                 ___wait_event_idle(wq_head, condition, 0, 0, schedule());\
253 } while (0)
254
255 #define wait_event_idle_exclusive(wq_head, condition)                   \
256 do {                                                                    \
257         might_sleep();                                                  \
258         if (!(condition))                                               \
259                 ___wait_event_idle(wq_head, condition, 1, 0, schedule());\
260 } while (0)
261
262 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout)\
263         ___wait_event_idle(wq_head, ___wait_cond_timeout1(condition),   \
264                            1, timeout,                                  \
265                            __ret = schedule_timeout(__ret))
266
267 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout)  \
268 ({                                                                      \
269         long __ret = timeout;                                           \
270         might_sleep();                                                  \
271         if (!___wait_cond_timeout1(condition))                          \
272                 __ret = __wait_event_idle_exclusive_timeout(            \
273                         wq_head, condition, timeout);                   \
274         __ret;                                                          \
275 })
276
277 #define __wait_event_idle_exclusive_timeout_cmd(wq_head, condition,     \
278                                                 timeout, cmd1, cmd2)    \
279         ___wait_event_idle(wq_head, ___wait_cond_timeout1(condition),   \
280                            1, timeout,                                  \
281                            cmd1; __ret = schedule_timeout(__ret); cmd2)
282
283 #define wait_event_idle_exclusive_timeout_cmd(wq_head, condition, timeout,\
284                                               cmd1, cmd2)               \
285 ({                                                                      \
286         long __ret = timeout;                                           \
287         if (!___wait_cond_timeout1(condition))                          \
288                 __ret = __wait_event_idle_exclusive_timeout_cmd(        \
289                         wq_head, condition, timeout, cmd1, cmd2);       \
290         __ret;                                                          \
291 })
292
293 #define __wait_event_idle_timeout(wq_head, condition, timeout)          \
294         ___wait_event_idle(wq_head, ___wait_cond_timeout1(condition),   \
295                            0, timeout,                                  \
296                            __ret = schedule_timeout(__ret))
297
298 #define wait_event_idle_timeout(wq_head, condition, timeout)            \
299 ({                                                                      \
300         long __ret = timeout;                                           \
301         might_sleep();                                                  \
302         if (!___wait_cond_timeout1(condition))                          \
303                 __ret = __wait_event_idle_timeout(wq_head, condition,   \
304                                                   timeout);             \
305         __ret;                                                          \
306 })
307
308 #else /* TASK_IDLE */
309 #ifndef wait_event_idle
310 /**
311  * wait_event_idle - wait for a condition without contributing to system load
312  * @wq_head: the waitqueue to wait on
313  * @condition: a C expression for the event to wait for
314  *
315  * The process is put to sleep (TASK_IDLE) until the
316  * @condition evaluates to true.
317  * The @condition is checked each time the waitqueue @wq_head is woken up.
318  *
319  * wake_up() has to be called after changing any variable that could
320  * change the result of the wait condition.
321  *
322  */
323 #define wait_event_idle(wq_head, condition)                             \
324 do {                                                                    \
325         might_sleep();                                                  \
326         if (!(condition))                                               \
327                 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0,      \
328                               schedule());                              \
329 } while (0)
330 #endif
331 #ifndef wait_event_idle_exclusive
332 /**
333  * wait_event_idle_exclusive - wait for a condition without contributing to
334  *               system load
335  * @wq_head: the waitqueue to wait on
336  * @condition: a C expression for the event to wait for
337  *
338  * The process is put to sleep (TASK_IDLE) until the
339  * @condition evaluates to true.
340  * The @condition is checked each time the waitqueue @wq_head is woken up.
341  *
342  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
343  * set thus if other processes wait on the same list, when this
344  * process is woken further processes are not considered.
345  *
346  * wake_up() has to be called after changing any variable that could
347  * change the result of the wait condition.
348  *
349  */
350 #define wait_event_idle_exclusive(wq_head, condition)                   \
351 do {                                                                    \
352         might_sleep();                                                  \
353         if (!(condition))                                               \
354                 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0,      \
355                               schedule());                              \
356 } while (0)
357 #endif
358 #ifndef wait_event_idle_exclusive_timeout
359 /**
360  * wait_event_idle_exclusive_timeout - sleep without load until a condition
361  *                       becomes true or a timeout elapses
362  * @wq_head: the waitqueue to wait on
363  * @condition: a C expression for the event to wait for
364  * @timeout: timeout, in jiffies
365  *
366  * The process is put to sleep (TASK_IDLE) until the
367  * @condition evaluates to true. The @condition is checked each time
368  * the waitqueue @wq_head is woken up.
369  *
370  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
371  * set thus if other processes wait on the same list, when this
372  * process is woken further processes are not considered.
373  *
374  * wake_up() has to be called after changing any variable that could
375  * change the result of the wait condition.
376  *
377  * Returns:
378  * 0 if the @condition evaluated to %false after the @timeout elapsed,
379  * 1 if the @condition evaluated to %true after the @timeout elapsed,
380  * or the remaining jiffies (at least 1) if the @condition evaluated
381  * to %true before the @timeout elapsed.
382  */
383 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout)  \
384 ({                                                                      \
385         long __ret = timeout;                                           \
386         might_sleep();                                                  \
387         if (!___wait_cond_timeout1(condition))                          \
388                 __ret = __wait_event_idle_exclusive_timeout(wq_head,    \
389                                                             condition,  \
390                                                             timeout);   \
391         __ret;                                                          \
392 })
393 #endif
394 #ifndef wait_event_idle_exclusive_timeout_cmd
395 #define __wait_event_idle_exclusive_timeout_cmd(wq_head, condition,     \
396                                                 timeout, cmd1, cmd2)    \
397         ___wait_event(wq_head, ___wait_cond_timeout1(condition),        \
398                       TASK_IDLE, 1, timeout,                            \
399                       cmd1; __ret = schedule_timeout(__ret); cmd2)
400
401 #define wait_event_idle_exclusive_timeout_cmd(wq_head, condition, timeout,\
402                                               cmd1, cmd2)               \
403 ({                                                                      \
404         long __ret = timeout;                                           \
405         if (!___wait_cond_timeout1(condition))                          \
406                 __ret = __wait_event_idle_exclusive_timeout_cmd(        \
407                         wq_head, condition, timeout, cmd1, cmd2);       \
408         __ret;                                                          \
409 })
410 #endif
411
412 #ifndef wait_event_idle_timeout
413
414 #define __wait_event_idle_timeout(wq_head, condition, timeout)          \
415         ___wait_event(wq_head, ___wait_cond_timeout1(condition),        \
416                       TASK_IDLE, 0, timeout,                            \
417                       __ret = schedule_timeout(__ret))
418
419 /**
420  * wait_event_idle_timeout - sleep without load until a condition becomes
421  *                           true or a timeout elapses
422  * @wq_head: the waitqueue to wait on
423  * @condition: a C expression for the event to wait for
424  * @timeout: timeout, in jiffies
425  *
426  * The process is put to sleep (TASK_IDLE) until the
427  * @condition evaluates to true. The @condition is checked each time
428  * the waitqueue @wq_head is woken up.
429  *
430  * wake_up() has to be called after changing any variable that could
431  * change the result of the wait condition.
432  *
433  * Returns:
434  * 0 if the @condition evaluated to %false after the @timeout elapsed,
435  * 1 if the @condition evaluated to %true after the @timeout elapsed,
436  * or the remaining jiffies (at least 1) if the @condition evaluated
437  * to %true before the @timeout elapsed.
438  */
439 #define wait_event_idle_timeout(wq_head, condition, timeout)            \
440 ({                                                                      \
441         long __ret = timeout;                                           \
442         might_sleep();                                                  \
443         if (!___wait_cond_timeout1(condition))                          \
444                 __ret = __wait_event_idle_timeout(wq_head, condition,   \
445                                                   timeout);             \
446         __ret;                                                          \
447 })
448 #endif
449 #endif /* TASK_IDLE */
450
451 /* ___wait_event_lifo is used for lifo exclusive 'idle' waits */
452 #ifdef TASK_NOLOAD
453
454 #define ___wait_event_lifo(wq_head, condition, ret, cmd)                \
455 ({                                                                      \
456         wait_queue_entry_t       __wq_entry;                            \
457         long __ret = ret;       /* explicit shadow */                   \
458                                                                         \
459         init_wait(&__wq_entry);                                         \
460         __wq_entry.flags =  WQ_FLAG_EXCLUSIVE;                          \
461         for (;;) {                                                      \
462                 prepare_to_wait_exclusive_head(&wq_head, &__wq_entry);  \
463                 prepare_to_wait_event(&wq_head, &__wq_entry, TASK_IDLE);\
464                                                                         \
465                 if (condition)                                          \
466                         break;                                          \
467                                                                         \
468                 cmd;                                                    \
469         }                                                               \
470         finish_wait(&wq_head, &__wq_entry);                             \
471         __ret;                                                          \
472 })
473 #else
474 #define ___wait_event_lifo(wq_head, condition, ret, cmd)                \
475 ({                                                                      \
476         wait_queue_entry_t __wq_entry;                                  \
477         unsigned long flags;                                            \
478         long __ret = ret;       /* explicit shadow */                   \
479         sigset_t __old_blocked, __new_blocked;                          \
480                                                                         \
481         siginitset(&__new_blocked, LUSTRE_FATAL_SIGS);                  \
482         sigprocmask(0, &__new_blocked, &__old_blocked);                 \
483         init_wait(&__wq_entry);                                         \
484         __wq_entry.flags = WQ_FLAG_EXCLUSIVE;                           \
485         for (;;) {                                                      \
486                 prepare_to_wait_exclusive_head(&wq_head, &__wq_entry);  \
487                 prepare_to_wait_event(&wq_head, &__wq_entry,            \
488                                       TASK_INTERRUPTIBLE);              \
489                                                                         \
490                 if (condition)                                          \
491                         break;                                          \
492                 /* See justification in ___wait_event_idle */           \
493                 if (signal_pending(current)) {                          \
494                         spin_lock_irqsave(&current->sighand->siglock,   \
495                                           flags);                       \
496                         clear_tsk_thread_flag(current, TIF_SIGPENDING); \
497                         spin_unlock_irqrestore(&current->sighand->siglock,\
498                                                flags);                  \
499                 }                                                       \
500                 cmd;                                                    \
501         }                                                               \
502         sigprocmask(SIG_SETMASK, &__old_blocked, NULL);                 \
503         finish_wait(&wq_head, &__wq_entry);                             \
504         __ret;                                                          \
505 })
506 #endif
507
508 #define wait_event_idle_exclusive_lifo(wq_head, condition)              \
509 do {                                                                    \
510         might_sleep();                                                  \
511         if (!(condition))                                               \
512                 ___wait_event_lifo(wq_head, condition, 0, schedule());  \
513 } while (0)
514
515 #define __wait_event_idle_lifo_timeout(wq_head, condition, timeout)     \
516         ___wait_event_lifo(wq_head, ___wait_cond_timeout1(condition),   \
517                            timeout,                                     \
518                            __ret = schedule_timeout(__ret))
519
520 #define wait_event_idle_exclusive_lifo_timeout(wq_head, condition, timeout)\
521 ({                                                                      \
522         long __ret = timeout;                                           \
523         might_sleep();                                                  \
524         if (!___wait_cond_timeout1(condition))                          \
525                 __ret = __wait_event_idle_lifo_timeout(wq_head,         \
526                                                        condition,       \
527                                                        timeout);        \
528         __ret;                                                          \
529 })
530
531 /* l_wait_event_abortable() is a bit like wait_event_killable()
532  * except there is a fixed set of signals which will abort:
533  * LUSTRE_FATAL_SIGS
534  */
535 #define LUSTRE_FATAL_SIGS                                        \
536         (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGTERM) | \
537          sigmask(SIGQUIT) | sigmask(SIGALRM))
538
539 #define l_wait_event_abortable(wq, condition)                           \
540 ({                                                                      \
541         sigset_t __new_blocked, __old_blocked;                          \
542         int __ret = 0;                                                  \
543         siginitsetinv(&__new_blocked, LUSTRE_FATAL_SIGS);               \
544         sigprocmask(SIG_BLOCK, &__new_blocked, &__old_blocked);         \
545         __ret = wait_event_interruptible(wq, condition);                \
546         sigprocmask(SIG_SETMASK, &__old_blocked, NULL);                 \
547         __ret;                                                          \
548 })
549
550 #define l_wait_event_abortable_timeout(wq, condition, timeout)          \
551 ({                                                                      \
552         sigset_t __new_blocked, __old_blocked;                          \
553         int __ret = 0;                                                  \
554         siginitsetinv(&__new_blocked, LUSTRE_FATAL_SIGS);               \
555         sigprocmask(SIG_BLOCK, &__new_blocked, &__old_blocked);         \
556         __ret = wait_event_interruptible_timeout(wq, condition, timeout);\
557         sigprocmask(SIG_SETMASK, &__old_blocked, NULL);                 \
558         __ret;                                                          \
559 })
560
561 #define l_wait_event_abortable_exclusive(wq, condition)                 \
562 ({                                                                      \
563         sigset_t __new_blocked, __old_blocked;                          \
564         int __ret = 0;                                                  \
565         siginitsetinv(&__new_blocked, LUSTRE_FATAL_SIGS);               \
566         sigprocmask(SIG_BLOCK, &__new_blocked, &__old_blocked);         \
567         __ret = wait_event_interruptible_exclusive(wq, condition);      \
568         sigprocmask(SIG_SETMASK, &__old_blocked, NULL);                 \
569         __ret;                                                          \
570 })
571
572 #endif /* __LICBFS_LINUX_WAIT_BIT_H */