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 #define wait_var_event(var, condition) \ 286 do { \ 287 might_sleep(); \ 288 if (condition) \ 289 break; \ 290 __wait_var_event(var, condition); \ 291 } while (0) 292 293 #define __wait_var_event_killable(var, condition) \ 294 ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \ 295 schedule()) 296 297 #define wait_var_event_killable(var, condition) \ 298 ({ \ 299 int __ret = 0; \ 300 might_sleep(); \ 301 if (!(condition)) \ 302 __ret = __wait_var_event_killable(var, condition); \ 303 __ret; \ 304 }) 305 306 #define __wait_var_event_timeout(var, condition, timeout) \ 307 ___wait_var_event(var, ___wait_cond_timeout(condition), \ 308 TASK_UNINTERRUPTIBLE, 0, timeout, \ 309 __ret = schedule_timeout(__ret)) 310 311 #define wait_var_event_timeout(var, condition, timeout) \ 312 ({ \ 313 long __ret = timeout; \ 314 might_sleep(); \ 315 if (!___wait_cond_timeout(condition)) \ 316 __ret = __wait_var_event_timeout(var, condition, timeout); \ 317 __ret; \ 318 }) 319 320 #define __wait_var_event_interruptible(var, condition) \ 321 ___wait_var_event(var, condition, TASK_INTERRUPTIBLE, 0, 0, \ 322 schedule()) 323 324 #define wait_var_event_interruptible(var, condition) \ 325 ({ \ 326 int __ret = 0; \ 327 might_sleep(); \ 328 if (!(condition)) \ 329 __ret = __wait_var_event_interruptible(var, condition); \ 330 __ret; \ 331 }) 332 333 /** 334 * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit 335 * @bit: the bit of the word being waited on 336 * @word: the address containing the bit being waited on 337 * 338 * The designated bit is cleared and any tasks waiting in wait_on_bit() 339 * or similar will be woken. This call has RELEASE semantics so that 340 * any changes to memory made before this call are guaranteed to be visible 341 * after the corresponding wait_on_bit() completes. 342 */ 343 static inline void clear_and_wake_up_bit(int bit, unsigned long *word) 344 { 345 clear_bit_unlock(bit, word); 346 /* See wake_up_bit() for which memory barrier you need to use. */ 347 smp_mb__after_atomic(); 348 wake_up_bit(word, bit); 349 } 350 351 #endif /* _LINUX_WAIT_BIT_H */ 352