1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBCFS_LINUX_WAIT_BIT_H
3 #define __LIBCFS_LINUX_WAIT_BIT_H
5 /* Make sure we can see if we have TASK_NOLOAD */
6 #include <linux/sched.h>
8 * Linux wait-bit related types and methods:
10 #ifdef HAVE_WAIT_BIT_HEADER_H
11 #include <linux/wait_bit.h>
13 #include <linux/wait.h>
15 #ifndef HAVE_WAIT_QUEUE_ENTRY
16 #define wait_queue_entry_t wait_queue_t
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;
25 #define ___wait_is_interruptible(state) \
26 (!__builtin_constant_p(state) || \
27 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
29 #endif /* ! HAVE_WAIT_BIT_HEADER_H */
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);
36 /* ___wait_cond_timeout changed number of args in v3.12-rc1-78-g35a2af94c7ce
37 * so let's define our own ___wait_cond_timeout1
40 #define ___wait_cond_timeout1(condition) \
42 bool __cond = (condition); \
43 if (__cond && !__ret) \
48 #ifndef HAVE_CLEAR_AND_WAKE_UP_BIT
50 * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
52 * @bit: the bit of the word being waited on
53 * @word: the word being waited on, a kernel virtual address
55 * You can use this helper if bitflags are manipulated atomically rather than
56 * non-atomically under a lock.
58 static inline void clear_and_wake_up_bit(int bit, void *word)
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);
65 #endif /* ! HAVE_CLEAR_AND_WAKE_UP_BIT */
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);
74 #define ___wait_var_event(var, condition, state, exclusive, ret, cmd) \
77 wait_queue_head_t *__wq_head = __var_waitqueue(var); \
78 struct wait_bit_queue_entry __wbq_entry; \
79 long __ret = ret; /* explicit shadow */ \
81 init_wait_var_entry(&__wbq_entry, var, \
82 exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
84 long __int = prepare_to_wait_event(__wq_head, \
85 &__wbq_entry.wq_entry, \
90 if (___wait_is_interruptible(state) && __int) { \
97 finish_wait(__wq_head, &__wbq_entry.wq_entry); \
101 #define __wait_var_event(var, condition) \
102 ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
105 #define wait_var_event(var, condition) \
110 __wait_var_event(var, condition); \
113 #define __wait_var_event_killable(var, condition) \
114 ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \
117 #define wait_var_event_killable(var, condition) \
122 __ret = __wait_var_event_killable(var, condition); \
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))
131 #define wait_var_event_timeout(var, condition, timeout) \
133 long __ret = timeout; \
135 if (!___wait_cond_timeout1(condition)) \
136 __ret = __wait_var_event_timeout(var, condition, timeout); \
139 #endif /* ! HAVE_WAIT_VAR_EVENT */
142 * prepare_to_wait_event() does not support an exclusive
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.
148 static inline void prepare_to_wait_exclusive_head(
149 wait_queue_head_t *waitq, wait_queue_entry_t *link)
153 spin_lock_irqsave(&(waitq->lock), flags);
154 #ifdef HAVE_WAIT_QUEUE_ENTRY_LIST
155 if (list_empty(&link->entry))
157 if (list_empty(&link->task_list))
159 __add_wait_queue_exclusive(waitq, link);
160 spin_unlock_irqrestore(&((waitq)->lock), flags);
163 #ifndef ___wait_event
165 * The below macro ___wait_event() has an explicit shadow of the __ret
166 * variable when used from the wait_event_*() macros.
168 * This is so that both can use the ___wait_cond_timeout1() construct
169 * to wrap the condition.
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
176 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
179 wait_queue_entry_ __wq_entry; \
180 long __ret = ret; /* explicit shadow */ \
182 init_wait(&__wq_entry); \
184 __wq_entry.flags = WQ_FLAG_EXCLUSIVE \
186 long __int = prepare_to_wait_event(&wq_head, \
187 &__wq_entry, state); \
192 if (___wait_is_interruptible(state) && __int) { \
199 finish_wait(&wq_head, &__wq_entry); \
206 #define ___wait_event_idle(wq_head, condition, exclusive, ret, cmd) \
208 wait_queue_entry_t __wq_entry; \
209 unsigned long flags; \
210 long __ret = ret; /* explicit shadow */ \
211 sigset_t __old_blocked; \
213 cfs_block_sigsinv(0, &__old_blocked); \
214 init_wait(&__wq_entry); \
216 __wq_entry.flags = WQ_FLAG_EXCLUSIVE; \
218 prepare_to_wait_event(&wq_head, \
220 TASK_INTERRUPTIBLE); \
224 /* We have to do this here because some signals */ \
225 /* are not blockable - ie from strace(1). */ \
226 /* In these cases we want to schedule_timeout() */ \
227 /* again, because we don't want that to return */ \
228 /* -EINTR when the RPC actually succeeded. */ \
229 /* the recalc_sigpending() below will deliver the */ \
230 /* signal properly. */ \
231 if (signal_pending(current)) { \
232 spin_lock_irqsave(¤t->sighand->siglock, \
234 clear_tsk_thread_flag(current, TIF_SIGPENDING); \
235 spin_unlock_irqrestore(¤t->sighand->siglock,\
240 finish_wait(&wq_head, &__wq_entry); \
241 cfs_restore_sigs(&__old_blocked); \
245 #define wait_event_idle(wq_head, condition) \
249 ___wait_event_idle(wq_head, condition, 0, 0, schedule());\
252 #define wait_event_idle_exclusive(wq_head, condition) \
256 ___wait_event_idle(wq_head, condition, 1, 0, schedule());\
259 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout)\
260 ___wait_event_idle(wq_head, ___wait_cond_timeout1(condition), \
262 __ret = schedule_timeout(__ret))
264 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
266 long __ret = timeout; \
268 if (!___wait_cond_timeout1(condition)) \
269 __ret = __wait_event_idle_exclusive_timeout( \
270 wq_head, condition, timeout); \
274 #define __wait_event_idle_exclusive_timeout_cmd(wq_head, condition, \
275 timeout, cmd1, cmd2) \
276 ___wait_event_idle(wq_head, ___wait_cond_timeout1(condition), \
278 cmd1; __ret = schedule_timeout(__ret); cmd2)
280 #define wait_event_idle_exclusive_timeout_cmd(wq_head, condition, timeout,\
283 long __ret = timeout; \
284 if (!___wait_cond_timeout1(condition)) \
285 __ret = __wait_event_idle_exclusive_timeout_cmd( \
286 wq_head, condition, timeout, cmd1, cmd2); \
290 #define __wait_event_idle_timeout(wq_head, condition, timeout) \
291 ___wait_event_idle(wq_head, ___wait_cond_timeout1(condition), \
293 __ret = schedule_timeout(__ret))
295 #define wait_event_idle_timeout(wq_head, condition, timeout) \
297 long __ret = timeout; \
299 if (!___wait_cond_timeout1(condition)) \
300 __ret = __wait_event_idle_timeout(wq_head, condition, \
305 #else /* TASK_IDLE */
306 #ifndef wait_event_idle
308 * wait_event_idle - wait for a condition without contributing to system load
309 * @wq_head: the waitqueue to wait on
310 * @condition: a C expression for the event to wait for
312 * The process is put to sleep (TASK_IDLE) until the
313 * @condition evaluates to true.
314 * The @condition is checked each time the waitqueue @wq_head is woken up.
316 * wake_up() has to be called after changing any variable that could
317 * change the result of the wait condition.
320 #define wait_event_idle(wq_head, condition) \
324 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, \
328 #ifndef wait_event_idle_exclusive
330 * wait_event_idle_exclusive - wait for a condition without contributing to
332 * @wq_head: the waitqueue to wait on
333 * @condition: a C expression for the event to wait for
335 * The process is put to sleep (TASK_IDLE) until the
336 * @condition evaluates to true.
337 * The @condition is checked each time the waitqueue @wq_head is woken up.
339 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
340 * set thus if other processes wait on the same list, when this
341 * process is woken further processes are not considered.
343 * wake_up() has to be called after changing any variable that could
344 * change the result of the wait condition.
347 #define wait_event_idle_exclusive(wq_head, condition) \
351 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, \
355 #ifndef wait_event_idle_exclusive_timeout
357 * wait_event_idle_exclusive_timeout - sleep without load until a condition
358 * becomes true or a timeout elapses
359 * @wq_head: the waitqueue to wait on
360 * @condition: a C expression for the event to wait for
361 * @timeout: timeout, in jiffies
363 * The process is put to sleep (TASK_IDLE) until the
364 * @condition evaluates to true. The @condition is checked each time
365 * the waitqueue @wq_head is woken up.
367 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
368 * set thus if other processes wait on the same list, when this
369 * process is woken further processes are not considered.
371 * wake_up() has to be called after changing any variable that could
372 * change the result of the wait condition.
375 * 0 if the @condition evaluated to %false after the @timeout elapsed,
376 * 1 if the @condition evaluated to %true after the @timeout elapsed,
377 * or the remaining jiffies (at least 1) if the @condition evaluated
378 * to %true before the @timeout elapsed.
380 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
382 long __ret = timeout; \
384 if (!___wait_cond_timeout1(condition)) \
385 __ret = __wait_event_idle_exclusive_timeout(wq_head, \
391 #ifndef wait_event_idle_exclusive_timeout_cmd
392 #define __wait_event_idle_exclusive_timeout_cmd(wq_head, condition, \
393 timeout, cmd1, cmd2) \
394 ___wait_event(wq_head, ___wait_cond_timeout1(condition), \
395 TASK_IDLE, 1, timeout, \
396 cmd1; __ret = schedule_timeout(__ret); cmd2)
398 #define wait_event_idle_exclusive_timeout_cmd(wq_head, condition, timeout,\
401 long __ret = timeout; \
402 if (!___wait_cond_timeout1(condition)) \
403 __ret = __wait_event_idle_exclusive_timeout_cmd( \
404 wq_head, condition, timeout, cmd1, cmd2); \
409 #ifndef wait_event_idle_timeout
411 #define __wait_event_idle_timeout(wq_head, condition, timeout) \
412 ___wait_event(wq_head, ___wait_cond_timeout1(condition), \
413 TASK_IDLE, 0, timeout, \
414 __ret = schedule_timeout(__ret))
417 * wait_event_idle_timeout - sleep without load until a condition becomes
418 * true or a timeout elapses
419 * @wq_head: the waitqueue to wait on
420 * @condition: a C expression for the event to wait for
421 * @timeout: timeout, in jiffies
423 * The process is put to sleep (TASK_IDLE) until the
424 * @condition evaluates to true. The @condition is checked each time
425 * the waitqueue @wq_head is woken up.
427 * wake_up() has to be called after changing any variable that could
428 * change the result of the wait condition.
431 * 0 if the @condition evaluated to %false after the @timeout elapsed,
432 * 1 if the @condition evaluated to %true after the @timeout elapsed,
433 * or the remaining jiffies (at least 1) if the @condition evaluated
434 * to %true before the @timeout elapsed.
436 #define wait_event_idle_timeout(wq_head, condition, timeout) \
438 long __ret = timeout; \
440 if (!___wait_cond_timeout1(condition)) \
441 __ret = __wait_event_idle_timeout(wq_head, condition, \
446 #endif /* TASK_IDLE */
448 /* ___wait_event_lifo is used for lifo exclusive 'idle' waits */
451 #define ___wait_event_lifo(wq_head, condition, ret, cmd) \
453 wait_queue_entry_t __wq_entry; \
454 long __ret = ret; /* explicit shadow */ \
456 init_wait(&__wq_entry); \
457 __wq_entry.flags = WQ_FLAG_EXCLUSIVE; \
459 prepare_to_wait_exclusive_head(&wq_head, &__wq_entry); \
460 prepare_to_wait_event(&wq_head, &__wq_entry, TASK_IDLE);\
467 finish_wait(&wq_head, &__wq_entry); \
471 #define ___wait_event_lifo(wq_head, condition, ret, cmd) \
473 wait_queue_entry_t __wq_entry; \
474 unsigned long flags; \
475 long __ret = ret; /* explicit shadow */ \
476 sigset_t __old_blocked; \
478 cfs_block_sigsinv(0, &__old_blocked); \
479 init_wait(&__wq_entry); \
480 __wq_entry.flags = WQ_FLAG_EXCLUSIVE; \
482 prepare_to_wait_exclusive_head(&wq_head, &__wq_entry); \
483 prepare_to_wait_event(&wq_head, &__wq_entry, \
484 TASK_INTERRUPTIBLE); \
488 /* See justification in ___wait_event_idle */ \
489 if (signal_pending(current)) { \
490 spin_lock_irqsave(¤t->sighand->siglock, \
492 clear_tsk_thread_flag(current, TIF_SIGPENDING); \
493 spin_unlock_irqrestore(¤t->sighand->siglock,\
498 cfs_restore_sigs(&__old_blocked); \
499 finish_wait(&wq_head, &__wq_entry); \
504 #define wait_event_idle_exclusive_lifo(wq_head, condition) \
508 ___wait_event_lifo(wq_head, condition, 0, schedule()); \
511 #define __wait_event_idle_lifo_timeout(wq_head, condition, timeout) \
512 ___wait_event_lifo(wq_head, ___wait_cond_timeout1(condition), \
514 __ret = schedule_timeout(__ret))
516 #define wait_event_idle_exclusive_lifo_timeout(wq_head, condition, timeout)\
518 long __ret = timeout; \
520 if (!___wait_cond_timeout1(condition)) \
521 __ret = __wait_event_idle_lifo_timeout(wq_head, \
527 /* l_wait_event_abortable() is a bit like wait_event_killable()
528 * except there is a fixed set of signals which will abort:
531 #define LUSTRE_FATAL_SIGS \
532 (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGTERM) | \
533 sigmask(SIGQUIT) | sigmask(SIGALRM))
535 #define l_wait_event_abortable(wq, condition) \
537 sigset_t __new_blocked, __old_blocked; \
539 siginitsetinv(&__new_blocked, LUSTRE_FATAL_SIGS); \
540 sigprocmask(SIG_BLOCK, &__new_blocked, &__old_blocked); \
541 __ret = wait_event_interruptible(wq, condition); \
542 sigprocmask(SIG_SETMASK, &__old_blocked, NULL); \
546 #define l_wait_event_abortable_timeout(wq, condition, timeout) \
548 sigset_t __new_blocked, __old_blocked; \
550 siginitsetinv(&__new_blocked, LUSTRE_FATAL_SIGS); \
551 sigprocmask(SIG_BLOCK, &__new_blocked, &__old_blocked); \
552 __ret = wait_event_interruptible_timeout(wq, condition, timeout);\
553 sigprocmask(SIG_SETMASK, &__old_blocked, NULL); \
557 #define l_wait_event_abortable_exclusive(wq, condition) \
559 sigset_t __new_blocked, __old_blocked; \
561 siginitsetinv(&__new_blocked, LUSTRE_FATAL_SIGS); \
562 sigprocmask(SIG_BLOCK, &__new_blocked, &__old_blocked); \
563 __ret = wait_event_interruptible_exclusive(wq, condition); \
564 sigprocmask(SIG_SETMASK, &__old_blocked, NULL); \
568 #endif /* __LICBFS_LINUX_WAIT_BIT_H */