xref: /linux-6.15/include/linux/wait_bit.h (revision cc2e1c82)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_WAIT_BIT_H
3 #define _LINUX_WAIT_BIT_H
4 
5 /*
6  * Linux wait-bit related types and methods:
7  */
8 #include <linux/wait.h>
9 
10 struct wait_bit_key {
11 	unsigned long		*flags;
12 	int			bit_nr;
13 	unsigned long		timeout;
14 };
15 
16 struct wait_bit_queue_entry {
17 	struct wait_bit_key	key;
18 	struct wait_queue_entry	wq_entry;
19 };
20 
21 #define __WAIT_BIT_KEY_INITIALIZER(word, bit)					\
22 	{ .flags = word, .bit_nr = bit, }
23 
24 typedef int wait_bit_action_f(struct wait_bit_key *key, int mode);
25 
26 void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bit);
27 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 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 void wake_up_bit(unsigned long *word, int bit);
30 int out_of_line_wait_on_bit(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode);
31 int out_of_line_wait_on_bit_timeout(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout);
32 int out_of_line_wait_on_bit_lock(unsigned long *word, int, wait_bit_action_f *action, unsigned int mode);
33 struct wait_queue_head *bit_waitqueue(unsigned long *word, int bit);
34 extern void __init wait_bit_init(void);
35 
36 int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
37 
38 #define DEFINE_WAIT_BIT(name, word, bit)					\
39 	struct wait_bit_queue_entry name = {					\
40 		.key = __WAIT_BIT_KEY_INITIALIZER(word, bit),			\
41 		.wq_entry = {							\
42 			.private	= current,				\
43 			.func		= wake_bit_function,			\
44 			.entry		=					\
45 				LIST_HEAD_INIT((name).wq_entry.entry),		\
46 		},								\
47 	}
48 
49 extern int bit_wait(struct wait_bit_key *key, int mode);
50 extern int bit_wait_io(struct wait_bit_key *key, int mode);
51 extern int bit_wait_timeout(struct wait_bit_key *key, int mode);
52 extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode);
53 
54 /**
55  * wait_on_bit - wait for a bit to be cleared
56  * @word: the address containing the bit being waited on
57  * @bit: the bit at that address being waited on
58  * @mode: the task state to sleep in
59  *
60  * Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP())
61  * to be cleared.  The clearing of the bit must be signalled with
62  * wake_up_bit(), often as clear_and_wake_up_bit().
63  *
64  * The process will wait on a waitqueue selected by hash from a shared
65  * pool.  It will only be woken on a wake_up for the target bit, even
66  * if other processes on the same queue are waiting for other bits.
67  *
68  * Returned value will be zero if the bit was cleared in which case the
69  * call has ACQUIRE semantics, or %-EINTR if the process received a
70  * signal and the mode permitted wake up on that signal.
71  */
72 static inline int
73 wait_on_bit(unsigned long *word, int bit, unsigned mode)
74 {
75 	might_sleep();
76 	if (!test_bit_acquire(bit, word))
77 		return 0;
78 	return out_of_line_wait_on_bit(word, bit,
79 				       bit_wait,
80 				       mode);
81 }
82 
83 /**
84  * wait_on_bit_io - wait for a bit to be cleared
85  * @word: the address containing the bit being waited on
86  * @bit: the bit at that address being waited on
87  * @mode: the task state to sleep in
88  *
89  * Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP())
90  * to be cleared.  The clearing of the bit must be signalled with
91  * wake_up_bit(), often as clear_and_wake_up_bit().
92  *
93  * This is similar to wait_on_bit(), but calls io_schedule() instead of
94  * schedule() for the actual waiting.
95  *
96  * Returned value will be zero if the bit was cleared in which case the
97  * call has ACQUIRE semantics, or %-EINTR if the process received a
98  * signal and the mode permitted wake up on that signal.
99  */
100 static inline int
101 wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
102 {
103 	might_sleep();
104 	if (!test_bit_acquire(bit, word))
105 		return 0;
106 	return out_of_line_wait_on_bit(word, bit,
107 				       bit_wait_io,
108 				       mode);
109 }
110 
111 /**
112  * wait_on_bit_timeout - wait for a bit to be cleared or a timeout to elapse
113  * @word: the address containing the bit being waited on
114  * @bit: the bit at that address being waited on
115  * @mode: the task state to sleep in
116  * @timeout: timeout, in jiffies
117  *
118  * Wait for the given bit in an unsigned long or bitmap (see
119  * DECLARE_BITMAP()) to be cleared, or for a timeout to expire.  The
120  * clearing of the bit must be signalled with wake_up_bit(), often as
121  * clear_and_wake_up_bit().
122  *
123  * This is similar to wait_on_bit(), except it also takes a timeout
124  * parameter.
125  *
126  * Returned value will be zero if the bit was cleared in which case the
127  * call has ACQUIRE semantics, or %-EINTR if the process received a
128  * signal and the mode permitted wake up on that signal, or %-EAGAIN if the
129  * timeout elapsed.
130  */
131 static inline int
132 wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
133 		    unsigned long timeout)
134 {
135 	might_sleep();
136 	if (!test_bit_acquire(bit, word))
137 		return 0;
138 	return out_of_line_wait_on_bit_timeout(word, bit,
139 					       bit_wait_timeout,
140 					       mode, timeout);
141 }
142 
143 /**
144  * wait_on_bit_action - wait for a bit to be cleared
145  * @word: the address containing the bit waited on
146  * @bit: the bit at that address being waited on
147  * @action: the function used to sleep, which may take special actions
148  * @mode: the task state to sleep in
149  *
150  * Wait for the given bit in an unsigned long or bitmap (see DECLARE_BITMAP())
151  * to be cleared.  The clearing of the bit must be signalled with
152  * wake_up_bit(), often as clear_and_wake_up_bit().
153  *
154  * This is similar to wait_on_bit(), but calls @action() instead of
155  * schedule() for the actual waiting.
156  *
157  * Returned value will be zero if the bit was cleared in which case the
158  * call has ACQUIRE semantics, or the error code returned by @action if
159  * that call returned non-zero.
160  */
161 static inline int
162 wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
163 		   unsigned mode)
164 {
165 	might_sleep();
166 	if (!test_bit_acquire(bit, word))
167 		return 0;
168 	return out_of_line_wait_on_bit(word, bit, action, mode);
169 }
170 
171 /**
172  * wait_on_bit_lock - wait for a bit to be cleared, then set it
173  * @word: the address containing the bit being waited on
174  * @bit: the bit of the word being waited on and set
175  * @mode: the task state to sleep in
176  *
177  * Wait for the given bit in an unsigned long or bitmap (see
178  * DECLARE_BITMAP()) to be cleared.  The clearing of the bit must be
179  * signalled with wake_up_bit(), often as clear_and_wake_up_bit().  As
180  * soon as it is clear, atomically set it and return.
181  *
182  * This is similar to wait_on_bit(), but sets the bit before returning.
183  *
184  * Returned value will be zero if the bit was successfully set in which
185  * case the call has the same memory sequencing semantics as
186  * test_and_clear_bit(), or %-EINTR if the process received a signal and
187  * the mode permitted wake up on that signal.
188  */
189 static inline int
190 wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
191 {
192 	might_sleep();
193 	if (!test_and_set_bit(bit, word))
194 		return 0;
195 	return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
196 }
197 
198 /**
199  * wait_on_bit_lock_io - wait for a bit to be cleared, then set it
200  * @word: the address containing the bit being waited on
201  * @bit: the bit of the word being waited on and set
202  * @mode: the task state to sleep in
203  *
204  * Wait for the given bit in an unsigned long or bitmap (see
205  * DECLARE_BITMAP()) to be cleared.  The clearing of the bit must be
206  * signalled with wake_up_bit(), often as clear_and_wake_up_bit().  As
207  * soon as it is clear, atomically set it and return.
208  *
209  * This is similar to wait_on_bit_lock(), but calls io_schedule() instead
210  * of schedule().
211  *
212  * Returns zero if the bit was (eventually) found to be clear and was
213  * set.  Returns non-zero if a signal was delivered to the process and
214  * the @mode allows that signal to wake the process.
215  */
216 static inline int
217 wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
218 {
219 	might_sleep();
220 	if (!test_and_set_bit(bit, word))
221 		return 0;
222 	return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
223 }
224 
225 /**
226  * wait_on_bit_lock_action - wait for a bit to be cleared, then set it
227  * @word: the address containing the bit being waited on
228  * @bit: the bit of the word being waited on and set
229  * @action: the function used to sleep, which may take special actions
230  * @mode: the task state to sleep in
231  *
232  * This is similar to wait_on_bit_lock(), but calls @action() instead of
233  * schedule() for the actual waiting.
234  *
235  * Returned value will be zero if the bit was successfully set in which
236  * case the call has the same memory sequencing semantics as
237  * test_and_clear_bit(), or the error code returned by @action if that
238  * call returned non-zero.
239  */
240 static inline int
241 wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
242 			unsigned mode)
243 {
244 	might_sleep();
245 	if (!test_and_set_bit(bit, word))
246 		return 0;
247 	return out_of_line_wait_on_bit_lock(word, bit, action, mode);
248 }
249 
250 extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags);
251 extern void wake_up_var(void *var);
252 extern wait_queue_head_t *__var_waitqueue(void *p);
253 
254 #define ___wait_var_event(var, condition, state, exclusive, ret, cmd)	\
255 ({									\
256 	__label__ __out;						\
257 	struct wait_queue_head *__wq_head = __var_waitqueue(var);	\
258 	struct wait_bit_queue_entry __wbq_entry;			\
259 	long __ret = ret; /* explicit shadow */				\
260 									\
261 	init_wait_var_entry(&__wbq_entry, var,				\
262 			    exclusive ? WQ_FLAG_EXCLUSIVE : 0);		\
263 	for (;;) {							\
264 		long __int = prepare_to_wait_event(__wq_head,		\
265 						   &__wbq_entry.wq_entry, \
266 						   state);		\
267 		if (condition)						\
268 			break;						\
269 									\
270 		if (___wait_is_interruptible(state) && __int) {		\
271 			__ret = __int;					\
272 			goto __out;					\
273 		}							\
274 									\
275 		cmd;							\
276 	}								\
277 	finish_wait(__wq_head, &__wbq_entry.wq_entry);			\
278 __out:	__ret;								\
279 })
280 
281 #define __wait_var_event(var, condition)				\
282 	___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0,	\
283 			  schedule())
284 
285 /**
286  * wait_var_event - wait for a variable to be updated and notified
287  * @var: the address of variable being waited on
288  * @condition: the condition to wait for
289  *
290  * Wait for a @condition to be true, only re-checking when a wake up is
291  * received for the given @var (an arbitrary kernel address which need
292  * not be directly related to the given condition, but usually is).
293  *
294  * The process will wait on a waitqueue selected by hash from a shared
295  * pool.  It will only be woken on a wake_up for the given address.
296  *
297  * The condition should normally use smp_load_acquire() or a similarly
298  * ordered access to ensure that any changes to memory made before the
299  * condition became true will be visible after the wait completes.
300  */
301 #define wait_var_event(var, condition)					\
302 do {									\
303 	might_sleep();							\
304 	if (condition)							\
305 		break;							\
306 	__wait_var_event(var, condition);				\
307 } while (0)
308 
309 #define __wait_var_event_killable(var, condition)			\
310 	___wait_var_event(var, condition, TASK_KILLABLE, 0, 0,		\
311 			  schedule())
312 
313 /**
314  * wait_var_event_killable - wait for a variable to be updated and notified
315  * @var: the address of variable being waited on
316  * @condition: the condition to wait for
317  *
318  * Wait for a @condition to be true or a fatal signal to be received,
319  * only re-checking the condition when a wake up is received for the given
320  * @var (an arbitrary kernel address which need not be directly related
321  * to the given condition, but usually is).
322  *
323  * This is similar to wait_var_event() but returns a value which is
324  * 0 if the condition became true, or %-ERESTARTSYS if a fatal signal
325  * was received.
326  *
327  * The condition should normally use smp_load_acquire() or a similarly
328  * ordered access to ensure that any changes to memory made before the
329  * condition became true will be visible after the wait completes.
330  */
331 #define wait_var_event_killable(var, condition)				\
332 ({									\
333 	int __ret = 0;							\
334 	might_sleep();							\
335 	if (!(condition))						\
336 		__ret = __wait_var_event_killable(var, condition);	\
337 	__ret;								\
338 })
339 
340 #define __wait_var_event_timeout(var, condition, timeout)		\
341 	___wait_var_event(var, ___wait_cond_timeout(condition),		\
342 			  TASK_UNINTERRUPTIBLE, 0, timeout,		\
343 			  __ret = schedule_timeout(__ret))
344 
345 /**
346  * wait_var_event_timeout - wait for a variable to be updated or a timeout to expire
347  * @var: the address of variable being waited on
348  * @condition: the condition to wait for
349  * @timeout: maximum time to wait in jiffies
350  *
351  * Wait for a @condition to be true or a timeout to expire, only
352  * re-checking the condition when a wake up is received for the given
353  * @var (an arbitrary kernel address which need not be directly related
354  * to the given condition, but usually is).
355  *
356  * This is similar to wait_var_event() but returns a value which is 0 if
357  * the timeout expired and the condition was still false, or the
358  * remaining time left in the timeout (but at least 1) if the condition
359  * was found to be true.
360  *
361  * The condition should normally use smp_load_acquire() or a similarly
362  * ordered access to ensure that any changes to memory made before the
363  * condition became true will be visible after the wait completes.
364  */
365 #define wait_var_event_timeout(var, condition, timeout)			\
366 ({									\
367 	long __ret = timeout;						\
368 	might_sleep();							\
369 	if (!___wait_cond_timeout(condition))				\
370 		__ret = __wait_var_event_timeout(var, condition, timeout); \
371 	__ret;								\
372 })
373 
374 #define __wait_var_event_interruptible(var, condition)			\
375 	___wait_var_event(var, condition, TASK_INTERRUPTIBLE, 0, 0,	\
376 			  schedule())
377 
378 /**
379  * wait_var_event_killable - wait for a variable to be updated and notified
380  * @var: the address of variable being waited on
381  * @condition: the condition to wait for
382  *
383  * Wait for a @condition to be true or a signal to be received, only
384  * re-checking the condition when a wake up is received for the given
385  * @var (an arbitrary kernel address which need not be directly related
386  * to the given condition, but usually is).
387  *
388  * This is similar to wait_var_event() but returns a value which is 0 if
389  * the condition became true, or %-ERESTARTSYS if a signal was received.
390  *
391  * The condition should normally use smp_load_acquire() or a similarly
392  * ordered access to ensure that any changes to memory made before the
393  * condition became true will be visible after the wait completes.
394  */
395 #define wait_var_event_interruptible(var, condition)			\
396 ({									\
397 	int __ret = 0;							\
398 	might_sleep();							\
399 	if (!(condition))						\
400 		__ret = __wait_var_event_interruptible(var, condition);	\
401 	__ret;								\
402 })
403 
404 /**
405  * wait_var_event_any_lock - wait for a variable to be updated under a lock
406  * @var: the address of the variable being waited on
407  * @condition: condition to wait for
408  * @lock: the object that is locked to protect updates to the variable
409  * @type: prefix on lock and unlock operations
410  * @state: waiting state, %TASK_UNINTERRUPTIBLE etc.
411  *
412  * Wait for a condition which can only be reliably tested while holding
413  * a lock.  The variables assessed in the condition will normal be updated
414  * under the same lock, and the wake up should be signalled with
415  * wake_up_var_locked() under the same lock.
416  *
417  * This is similar to wait_var_event(), but assumes a lock is held
418  * while calling this function and while updating the variable.
419  *
420  * This must be called while the given lock is held and the lock will be
421  * dropped when schedule() is called to wait for a wake up, and will be
422  * reclaimed before testing the condition again.  The functions used to
423  * unlock and lock the object are constructed by appending _unlock and _lock
424  * to @type.
425  *
426  * Return %-ERESTARTSYS if a signal arrives which is allowed to interrupt
427  * the wait according to @state.
428  */
429 #define wait_var_event_any_lock(var, condition, lock, type, state)	\
430 ({									\
431 	int __ret = 0;							\
432 	if (!(condition))						\
433 		__ret = ___wait_var_event(var, condition, state, 0, 0,	\
434 					  type ## _unlock(lock);	\
435 					  schedule();			\
436 					  type ## _lock(lock));		\
437 	__ret;								\
438 })
439 
440 /**
441  * wait_var_event_spinlock - wait for a variable to be updated under a spinlock
442  * @var: the address of the variable being waited on
443  * @condition: condition to wait for
444  * @lock: the spinlock which protects updates to the variable
445  *
446  * Wait for a condition which can only be reliably tested while holding
447  * a spinlock.  The variables assessed in the condition will normal be updated
448  * under the same spinlock, and the wake up should be signalled with
449  * wake_up_var_locked() under the same spinlock.
450  *
451  * This is similar to wait_var_event(), but assumes a spinlock is held
452  * while calling this function and while updating the variable.
453  *
454  * This must be called while the given lock is held and the lock will be
455  * dropped when schedule() is called to wait for a wake up, and will be
456  * reclaimed before testing the condition again.
457  */
458 #define wait_var_event_spinlock(var, condition, lock)			\
459 	wait_var_event_any_lock(var, condition, lock, spin, TASK_UNINTERRUPTIBLE)
460 
461 /**
462  * wait_var_event_mutex - wait for a variable to be updated under a mutex
463  * @var: the address of the variable being waited on
464  * @condition: condition to wait for
465  * @mutex: the mutex which protects updates to the variable
466  *
467  * Wait for a condition which can only be reliably tested while holding
468  * a mutex.  The variables assessed in the condition will normal be
469  * updated under the same mutex, and the wake up should be signalled
470  * with wake_up_var_locked() under the same mutex.
471  *
472  * This is similar to wait_var_event(), but assumes a mutex is held
473  * while calling this function and while updating the variable.
474  *
475  * This must be called while the given mutex is held and the mutex will be
476  * dropped when schedule() is called to wait for a wake up, and will be
477  * reclaimed before testing the condition again.
478  */
479 #define wait_var_event_mutex(var, condition, lock)			\
480 	wait_var_event_any_lock(var, condition, lock, mutex, TASK_UNINTERRUPTIBLE)
481 
482 /**
483  * wake_up_var_protected - wake up waiters for a variable asserting that it is safe
484  * @var: the address of the variable being waited on
485  * @cond: the condition which afirms this is safe
486  *
487  * When waking waiters which use wait_var_event_any_lock() the waker must be
488  * holding the reelvant lock to avoid races.  This version of wake_up_var()
489  * asserts that the relevant lock is held and so no barrier is needed.
490  * The @cond is only tested when CONFIG_LOCKDEP is enabled.
491  */
492 #define wake_up_var_protected(var, cond)				\
493 do {									\
494 	lockdep_assert(cond);						\
495 	wake_up_var(var);						\
496 } while (0)
497 
498 /**
499  * wake_up_var_locked - wake up waiters for a variable while holding a spinlock or mutex
500  * @var: the address of the variable being waited on
501  * @lock: The spinlock or mutex what protects the variable
502  *
503  * Send a wake up for the given variable which should be waited for with
504  * wait_var_event_spinlock() or wait_var_event_mutex().  Unlike wake_up_var(),
505  * no extra barriers are needed as the locking provides sufficient sequencing.
506  */
507 #define wake_up_var_locked(var, lock)					\
508 	wake_up_var_protected(var, lockdep_is_held(lock))
509 
510 /**
511  * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
512  * @bit: the bit of the word being waited on
513  * @word: the address containing the bit being waited on
514  *
515  * The designated bit is cleared and any tasks waiting in wait_on_bit()
516  * or similar will be woken.  This call has RELEASE semantics so that
517  * any changes to memory made before this call are guaranteed to be visible
518  * after the corresponding wait_on_bit() completes.
519  */
520 static inline void clear_and_wake_up_bit(int bit, unsigned long *word)
521 {
522 	clear_bit_unlock(bit, word);
523 	/* See wake_up_bit() for which memory barrier you need to use. */
524 	smp_mb__after_atomic();
525 	wake_up_bit(word, bit);
526 }
527 
528 /**
529  * test_and_clear_wake_up_bit - clear a bit if it was set: wake up anyone waiting on that bit
530  * @bit: the bit of the word being waited on
531  * @word: the address of memory containing that bit
532  *
533  * If the bit is set and can be atomically cleared, any tasks waiting in
534  * wait_on_bit() or similar will be woken.  This call has the same
535  * complete ordering semantics as test_and_clear_bit().  Any changes to
536  * memory made before this call are guaranteed to be visible after the
537  * corresponding wait_on_bit() completes.
538  *
539  * Returns %true if the bit was successfully set and the wake up was sent.
540  */
541 static inline bool test_and_clear_wake_up_bit(int bit, unsigned long *word)
542 {
543 	if (!test_and_clear_bit(bit, word))
544 		return false;
545 	/* no extra barrier required */
546 	wake_up_bit(word, bit);
547 	return true;
548 }
549 
550 /**
551  * atomic_dec_and_wake_up - decrement an atomic_t and if zero, wake up waiters
552  * @var: the variable to dec and test
553  *
554  * Decrements the atomic variable and if it reaches zero, send a wake_up to any
555  * processes waiting on the variable.
556  *
557  * This function has the same complete ordering semantics as atomic_dec_and_test.
558  *
559  * Returns %true is the variable reaches zero and the wake up was sent.
560  */
561 
562 static inline bool atomic_dec_and_wake_up(atomic_t *var)
563 {
564 	if (!atomic_dec_and_test(var))
565 		return false;
566 	/* No extra barrier required */
567 	wake_up_var(var);
568 	return true;
569 }
570 
571 /**
572  * store_release_wake_up - update a variable and send a wake_up
573  * @var: the address of the variable to be updated and woken
574  * @val: the value to store in the variable.
575  *
576  * Store the given value in the variable send a wake up to any tasks
577  * waiting on the variable.  All necessary barriers are included to ensure
578  * the task calling wait_var_event() sees the new value and all values
579  * written to memory before this call.
580  */
581 #define store_release_wake_up(var, val)					\
582 do {									\
583 	smp_store_release(var, val);					\
584 	smp_mb();							\
585 	wake_up_var(var);						\
586 } while (0)
587 
588 #endif /* _LINUX_WAIT_BIT_H */
589