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 * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit 406 * @bit: the bit of the word being waited on 407 * @word: the address containing the bit being waited on 408 * 409 * The designated bit is cleared and any tasks waiting in wait_on_bit() 410 * or similar will be woken. This call has RELEASE semantics so that 411 * any changes to memory made before this call are guaranteed to be visible 412 * after the corresponding wait_on_bit() completes. 413 */ 414 static inline void clear_and_wake_up_bit(int bit, unsigned long *word) 415 { 416 clear_bit_unlock(bit, word); 417 /* See wake_up_bit() for which memory barrier you need to use. */ 418 smp_mb__after_atomic(); 419 wake_up_bit(word, bit); 420 } 421 422 #endif /* _LINUX_WAIT_BIT_H */ 423