1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 << 3 /* 2 /* 4 * Generic wait-for-completion handler; 3 * Generic wait-for-completion handler; 5 * 4 * 6 * It differs from semaphores in that their de 5 * It differs from semaphores in that their default case is the opposite, 7 * wait_for_completion default blocks whereas 6 * wait_for_completion default blocks whereas semaphore default non-block. The 8 * interface also makes it easy to 'complete' 7 * interface also makes it easy to 'complete' multiple waiting threads, 9 * something which isn't entirely natural for 8 * something which isn't entirely natural for semaphores. 10 * 9 * 11 * But more importantly, the primitive documen 10 * But more importantly, the primitive documents the usage. Semaphores would 12 * typically be used for exclusion which gives 11 * typically be used for exclusion which gives rise to priority inversion. 13 * Waiting for completion is a typically sync 12 * Waiting for completion is a typically sync point, but not an exclusion point. 14 */ 13 */ 15 !! 14 #include "sched.h" 16 static void complete_with_flags(struct complet << 17 { << 18 unsigned long flags; << 19 << 20 raw_spin_lock_irqsave(&x->wait.lock, f << 21 << 22 if (x->done != UINT_MAX) << 23 x->done++; << 24 swake_up_locked(&x->wait, wake_flags); << 25 raw_spin_unlock_irqrestore(&x->wait.lo << 26 } << 27 << 28 void complete_on_current_cpu(struct completion << 29 { << 30 return complete_with_flags(x, WF_CURRE << 31 } << 32 15 33 /** 16 /** 34 * complete: - signals a single thread waiting 17 * complete: - signals a single thread waiting on this completion 35 * @x: holds the state of this particular com 18 * @x: holds the state of this particular completion 36 * 19 * 37 * This will wake up a single thread waiting o 20 * This will wake up a single thread waiting on this completion. Threads will be 38 * awakened in the same order in which they we 21 * awakened in the same order in which they were queued. 39 * 22 * 40 * See also complete_all(), wait_for_completio 23 * See also complete_all(), wait_for_completion() and related routines. 41 * 24 * 42 * If this function wakes up a task, it execut !! 25 * It may be assumed that this function implies a write memory barrier before 43 * accessing the task state. !! 26 * changing the task state if and only if any tasks are woken up. 44 */ 27 */ 45 void complete(struct completion *x) 28 void complete(struct completion *x) 46 { 29 { 47 complete_with_flags(x, 0); !! 30 unsigned long flags; >> 31 >> 32 spin_lock_irqsave(&x->wait.lock, flags); >> 33 >> 34 if (x->done != UINT_MAX) >> 35 x->done++; >> 36 __wake_up_locked(&x->wait, TASK_NORMAL, 1); >> 37 spin_unlock_irqrestore(&x->wait.lock, flags); 48 } 38 } 49 EXPORT_SYMBOL(complete); 39 EXPORT_SYMBOL(complete); 50 40 51 /** 41 /** 52 * complete_all: - signals all threads waiting 42 * complete_all: - signals all threads waiting on this completion 53 * @x: holds the state of this particular com 43 * @x: holds the state of this particular completion 54 * 44 * 55 * This will wake up all threads waiting on th 45 * This will wake up all threads waiting on this particular completion event. 56 * 46 * 57 * If this function wakes up a task, it execut !! 47 * It may be assumed that this function implies a write memory barrier before 58 * accessing the task state. !! 48 * changing the task state if and only if any tasks are woken up. 59 * 49 * 60 * Since complete_all() sets the completion of 50 * Since complete_all() sets the completion of @x permanently to done 61 * to allow multiple waiters to finish, a call 51 * to allow multiple waiters to finish, a call to reinit_completion() 62 * must be used on @x if @x is to be used agai 52 * must be used on @x if @x is to be used again. The code must make 63 * sure that all waiters have woken and finish 53 * sure that all waiters have woken and finished before reinitializing 64 * @x. Also note that the function completion_ 54 * @x. Also note that the function completion_done() can not be used 65 * to know if there are still waiters after co 55 * to know if there are still waiters after complete_all() has been called. 66 */ 56 */ 67 void complete_all(struct completion *x) 57 void complete_all(struct completion *x) 68 { 58 { 69 unsigned long flags; 59 unsigned long flags; 70 60 71 lockdep_assert_RT_in_threaded_ctx(); !! 61 spin_lock_irqsave(&x->wait.lock, flags); 72 << 73 raw_spin_lock_irqsave(&x->wait.lock, f << 74 x->done = UINT_MAX; 62 x->done = UINT_MAX; 75 swake_up_all_locked(&x->wait); !! 63 __wake_up_locked(&x->wait, TASK_NORMAL, 0); 76 raw_spin_unlock_irqrestore(&x->wait.lo !! 64 spin_unlock_irqrestore(&x->wait.lock, flags); 77 } 65 } 78 EXPORT_SYMBOL(complete_all); 66 EXPORT_SYMBOL(complete_all); 79 67 80 static inline long __sched 68 static inline long __sched 81 do_wait_for_common(struct completion *x, 69 do_wait_for_common(struct completion *x, 82 long (*action)(long), long 70 long (*action)(long), long timeout, int state) 83 { 71 { 84 if (!x->done) { 72 if (!x->done) { 85 DECLARE_SWAITQUEUE(wait); !! 73 DECLARE_WAITQUEUE(wait, current); 86 74 >> 75 __add_wait_queue_entry_tail_exclusive(&x->wait, &wait); 87 do { 76 do { 88 if (signal_pending_sta 77 if (signal_pending_state(state, current)) { 89 timeout = -ERE 78 timeout = -ERESTARTSYS; 90 break; 79 break; 91 } 80 } 92 __prepare_to_swait(&x- << 93 __set_current_state(st 81 __set_current_state(state); 94 raw_spin_unlock_irq(&x !! 82 spin_unlock_irq(&x->wait.lock); 95 timeout = action(timeo 83 timeout = action(timeout); 96 raw_spin_lock_irq(&x-> !! 84 spin_lock_irq(&x->wait.lock); 97 } while (!x->done && timeout); 85 } while (!x->done && timeout); 98 __finish_swait(&x->wait, &wait !! 86 __remove_wait_queue(&x->wait, &wait); 99 if (!x->done) 87 if (!x->done) 100 return timeout; 88 return timeout; 101 } 89 } 102 if (x->done != UINT_MAX) 90 if (x->done != UINT_MAX) 103 x->done--; 91 x->done--; 104 return timeout ?: 1; 92 return timeout ?: 1; 105 } 93 } 106 94 107 static inline long __sched 95 static inline long __sched 108 __wait_for_common(struct completion *x, 96 __wait_for_common(struct completion *x, 109 long (*action)(long), long t 97 long (*action)(long), long timeout, int state) 110 { 98 { 111 might_sleep(); 99 might_sleep(); 112 100 113 complete_acquire(x); 101 complete_acquire(x); 114 102 115 raw_spin_lock_irq(&x->wait.lock); !! 103 spin_lock_irq(&x->wait.lock); 116 timeout = do_wait_for_common(x, action 104 timeout = do_wait_for_common(x, action, timeout, state); 117 raw_spin_unlock_irq(&x->wait.lock); !! 105 spin_unlock_irq(&x->wait.lock); 118 106 119 complete_release(x); 107 complete_release(x); 120 108 121 return timeout; 109 return timeout; 122 } 110 } 123 111 124 static long __sched 112 static long __sched 125 wait_for_common(struct completion *x, long tim 113 wait_for_common(struct completion *x, long timeout, int state) 126 { 114 { 127 return __wait_for_common(x, schedule_t 115 return __wait_for_common(x, schedule_timeout, timeout, state); 128 } 116 } 129 117 130 static long __sched 118 static long __sched 131 wait_for_common_io(struct completion *x, long 119 wait_for_common_io(struct completion *x, long timeout, int state) 132 { 120 { 133 return __wait_for_common(x, io_schedul 121 return __wait_for_common(x, io_schedule_timeout, timeout, state); 134 } 122 } 135 123 136 /** 124 /** 137 * wait_for_completion: - waits for completion 125 * wait_for_completion: - waits for completion of a task 138 * @x: holds the state of this particular com 126 * @x: holds the state of this particular completion 139 * 127 * 140 * This waits to be signaled for completion of 128 * This waits to be signaled for completion of a specific task. It is NOT 141 * interruptible and there is no timeout. 129 * interruptible and there is no timeout. 142 * 130 * 143 * See also similar routines (i.e. wait_for_co 131 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout 144 * and interrupt capability. Also see complete 132 * and interrupt capability. Also see complete(). 145 */ 133 */ 146 void __sched wait_for_completion(struct comple 134 void __sched wait_for_completion(struct completion *x) 147 { 135 { 148 wait_for_common(x, MAX_SCHEDULE_TIMEOU 136 wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); 149 } 137 } 150 EXPORT_SYMBOL(wait_for_completion); 138 EXPORT_SYMBOL(wait_for_completion); 151 139 152 /** 140 /** 153 * wait_for_completion_timeout: - waits for co 141 * wait_for_completion_timeout: - waits for completion of a task (w/timeout) 154 * @x: holds the state of this particular com 142 * @x: holds the state of this particular completion 155 * @timeout: timeout value in jiffies 143 * @timeout: timeout value in jiffies 156 * 144 * 157 * This waits for either a completion of a spe 145 * This waits for either a completion of a specific task to be signaled or for a 158 * specified timeout to expire. The timeout is 146 * specified timeout to expire. The timeout is in jiffies. It is not 159 * interruptible. 147 * interruptible. 160 * 148 * 161 * Return: 0 if timed out, and positive (at le 149 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left 162 * till timeout) if completed. 150 * till timeout) if completed. 163 */ 151 */ 164 unsigned long __sched 152 unsigned long __sched 165 wait_for_completion_timeout(struct completion 153 wait_for_completion_timeout(struct completion *x, unsigned long timeout) 166 { 154 { 167 return wait_for_common(x, timeout, TAS 155 return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE); 168 } 156 } 169 EXPORT_SYMBOL(wait_for_completion_timeout); 157 EXPORT_SYMBOL(wait_for_completion_timeout); 170 158 171 /** 159 /** 172 * wait_for_completion_io: - waits for complet 160 * wait_for_completion_io: - waits for completion of a task 173 * @x: holds the state of this particular com 161 * @x: holds the state of this particular completion 174 * 162 * 175 * This waits to be signaled for completion of 163 * This waits to be signaled for completion of a specific task. It is NOT 176 * interruptible and there is no timeout. The 164 * interruptible and there is no timeout. The caller is accounted as waiting 177 * for IO (which traditionally means blkio onl 165 * for IO (which traditionally means blkio only). 178 */ 166 */ 179 void __sched wait_for_completion_io(struct com 167 void __sched wait_for_completion_io(struct completion *x) 180 { 168 { 181 wait_for_common_io(x, MAX_SCHEDULE_TIM 169 wait_for_common_io(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE); 182 } 170 } 183 EXPORT_SYMBOL(wait_for_completion_io); 171 EXPORT_SYMBOL(wait_for_completion_io); 184 172 185 /** 173 /** 186 * wait_for_completion_io_timeout: - waits for 174 * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout) 187 * @x: holds the state of this particular com 175 * @x: holds the state of this particular completion 188 * @timeout: timeout value in jiffies 176 * @timeout: timeout value in jiffies 189 * 177 * 190 * This waits for either a completion of a spe 178 * This waits for either a completion of a specific task to be signaled or for a 191 * specified timeout to expire. The timeout is 179 * specified timeout to expire. The timeout is in jiffies. It is not 192 * interruptible. The caller is accounted as w 180 * interruptible. The caller is accounted as waiting for IO (which traditionally 193 * means blkio only). 181 * means blkio only). 194 * 182 * 195 * Return: 0 if timed out, and positive (at le 183 * Return: 0 if timed out, and positive (at least 1, or number of jiffies left 196 * till timeout) if completed. 184 * till timeout) if completed. 197 */ 185 */ 198 unsigned long __sched 186 unsigned long __sched 199 wait_for_completion_io_timeout(struct completi 187 wait_for_completion_io_timeout(struct completion *x, unsigned long timeout) 200 { 188 { 201 return wait_for_common_io(x, timeout, 189 return wait_for_common_io(x, timeout, TASK_UNINTERRUPTIBLE); 202 } 190 } 203 EXPORT_SYMBOL(wait_for_completion_io_timeout); 191 EXPORT_SYMBOL(wait_for_completion_io_timeout); 204 192 205 /** 193 /** 206 * wait_for_completion_interruptible: - waits 194 * wait_for_completion_interruptible: - waits for completion of a task (w/intr) 207 * @x: holds the state of this particular com 195 * @x: holds the state of this particular completion 208 * 196 * 209 * This waits for completion of a specific tas 197 * This waits for completion of a specific task to be signaled. It is 210 * interruptible. 198 * interruptible. 211 * 199 * 212 * Return: -ERESTARTSYS if interrupted, 0 if c 200 * Return: -ERESTARTSYS if interrupted, 0 if completed. 213 */ 201 */ 214 int __sched wait_for_completion_interruptible( 202 int __sched wait_for_completion_interruptible(struct completion *x) 215 { 203 { 216 long t = wait_for_common(x, MAX_SCHEDU 204 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE); 217 << 218 if (t == -ERESTARTSYS) 205 if (t == -ERESTARTSYS) 219 return t; 206 return t; 220 return 0; 207 return 0; 221 } 208 } 222 EXPORT_SYMBOL(wait_for_completion_interruptibl 209 EXPORT_SYMBOL(wait_for_completion_interruptible); 223 210 224 /** 211 /** 225 * wait_for_completion_interruptible_timeout: 212 * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr)) 226 * @x: holds the state of this particular com 213 * @x: holds the state of this particular completion 227 * @timeout: timeout value in jiffies 214 * @timeout: timeout value in jiffies 228 * 215 * 229 * This waits for either a completion of a spe 216 * This waits for either a completion of a specific task to be signaled or for a 230 * specified timeout to expire. It is interrup 217 * specified timeout to expire. It is interruptible. The timeout is in jiffies. 231 * 218 * 232 * Return: -ERESTARTSYS if interrupted, 0 if t 219 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1, 233 * or number of jiffies left till timeout) if 220 * or number of jiffies left till timeout) if completed. 234 */ 221 */ 235 long __sched 222 long __sched 236 wait_for_completion_interruptible_timeout(stru 223 wait_for_completion_interruptible_timeout(struct completion *x, 237 unsi 224 unsigned long timeout) 238 { 225 { 239 return wait_for_common(x, timeout, TAS 226 return wait_for_common(x, timeout, TASK_INTERRUPTIBLE); 240 } 227 } 241 EXPORT_SYMBOL(wait_for_completion_interruptibl 228 EXPORT_SYMBOL(wait_for_completion_interruptible_timeout); 242 229 243 /** 230 /** 244 * wait_for_completion_killable: - waits for c 231 * wait_for_completion_killable: - waits for completion of a task (killable) 245 * @x: holds the state of this particular com 232 * @x: holds the state of this particular completion 246 * 233 * 247 * This waits to be signaled for completion of 234 * This waits to be signaled for completion of a specific task. It can be 248 * interrupted by a kill signal. 235 * interrupted by a kill signal. 249 * 236 * 250 * Return: -ERESTARTSYS if interrupted, 0 if c 237 * Return: -ERESTARTSYS if interrupted, 0 if completed. 251 */ 238 */ 252 int __sched wait_for_completion_killable(struc 239 int __sched wait_for_completion_killable(struct completion *x) 253 { 240 { 254 long t = wait_for_common(x, MAX_SCHEDU 241 long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE); 255 << 256 if (t == -ERESTARTSYS) 242 if (t == -ERESTARTSYS) 257 return t; 243 return t; 258 return 0; 244 return 0; 259 } 245 } 260 EXPORT_SYMBOL(wait_for_completion_killable); 246 EXPORT_SYMBOL(wait_for_completion_killable); 261 247 262 int __sched wait_for_completion_state(struct c << 263 { << 264 long t = wait_for_common(x, MAX_SCHEDU << 265 << 266 if (t == -ERESTARTSYS) << 267 return t; << 268 return 0; << 269 } << 270 EXPORT_SYMBOL(wait_for_completion_state); << 271 << 272 /** 248 /** 273 * wait_for_completion_killable_timeout: - wai 249 * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable)) 274 * @x: holds the state of this particular com 250 * @x: holds the state of this particular completion 275 * @timeout: timeout value in jiffies 251 * @timeout: timeout value in jiffies 276 * 252 * 277 * This waits for either a completion of a spe 253 * This waits for either a completion of a specific task to be 278 * signaled or for a specified timeout to expi 254 * signaled or for a specified timeout to expire. It can be 279 * interrupted by a kill signal. The timeout i 255 * interrupted by a kill signal. The timeout is in jiffies. 280 * 256 * 281 * Return: -ERESTARTSYS if interrupted, 0 if t 257 * Return: -ERESTARTSYS if interrupted, 0 if timed out, positive (at least 1, 282 * or number of jiffies left till timeout) if 258 * or number of jiffies left till timeout) if completed. 283 */ 259 */ 284 long __sched 260 long __sched 285 wait_for_completion_killable_timeout(struct co 261 wait_for_completion_killable_timeout(struct completion *x, 286 unsigned 262 unsigned long timeout) 287 { 263 { 288 return wait_for_common(x, timeout, TAS 264 return wait_for_common(x, timeout, TASK_KILLABLE); 289 } 265 } 290 EXPORT_SYMBOL(wait_for_completion_killable_tim 266 EXPORT_SYMBOL(wait_for_completion_killable_timeout); 291 267 292 /** 268 /** 293 * try_wait_for_completion - try to decre 269 * try_wait_for_completion - try to decrement a completion without blocking 294 * @x: completion structure 270 * @x: completion structure 295 * 271 * 296 * Return: 0 if a decrement cannot be don 272 * Return: 0 if a decrement cannot be done without blocking 297 * 1 if a decrement succeeded. 273 * 1 if a decrement succeeded. 298 * 274 * 299 * If a completion is being used as a cou 275 * If a completion is being used as a counting completion, 300 * attempt to decrement the counter witho 276 * attempt to decrement the counter without blocking. This 301 * enables us to avoid waiting if the res 277 * enables us to avoid waiting if the resource the completion 302 * is protecting is not available. 278 * is protecting is not available. 303 */ 279 */ 304 bool try_wait_for_completion(struct completion 280 bool try_wait_for_completion(struct completion *x) 305 { 281 { 306 unsigned long flags; 282 unsigned long flags; 307 bool ret = true; 283 bool ret = true; 308 284 309 /* 285 /* 310 * Since x->done will need to be locke 286 * Since x->done will need to be locked only 311 * in the non-blocking case, we check 287 * in the non-blocking case, we check x->done 312 * first without taking the lock so we 288 * first without taking the lock so we can 313 * return early in the blocking case. 289 * return early in the blocking case. 314 */ 290 */ 315 if (!READ_ONCE(x->done)) 291 if (!READ_ONCE(x->done)) 316 return false; 292 return false; 317 293 318 raw_spin_lock_irqsave(&x->wait.lock, f !! 294 spin_lock_irqsave(&x->wait.lock, flags); 319 if (!x->done) 295 if (!x->done) 320 ret = false; 296 ret = false; 321 else if (x->done != UINT_MAX) 297 else if (x->done != UINT_MAX) 322 x->done--; 298 x->done--; 323 raw_spin_unlock_irqrestore(&x->wait.lo !! 299 spin_unlock_irqrestore(&x->wait.lock, flags); 324 return ret; 300 return ret; 325 } 301 } 326 EXPORT_SYMBOL(try_wait_for_completion); 302 EXPORT_SYMBOL(try_wait_for_completion); 327 303 328 /** 304 /** 329 * completion_done - Test to see if a com 305 * completion_done - Test to see if a completion has any waiters 330 * @x: completion structure 306 * @x: completion structure 331 * 307 * 332 * Return: 0 if there are waiters (wait_f 308 * Return: 0 if there are waiters (wait_for_completion() in progress) 333 * 1 if there are no waiters. 309 * 1 if there are no waiters. 334 * 310 * 335 * Note, this will always return true if 311 * Note, this will always return true if complete_all() was called on @X. 336 */ 312 */ 337 bool completion_done(struct completion *x) 313 bool completion_done(struct completion *x) 338 { 314 { 339 unsigned long flags; 315 unsigned long flags; 340 316 341 if (!READ_ONCE(x->done)) 317 if (!READ_ONCE(x->done)) 342 return false; 318 return false; 343 319 344 /* 320 /* 345 * If ->done, we need to wait for comp 321 * If ->done, we need to wait for complete() to release ->wait.lock 346 * otherwise we can end up freeing the 322 * otherwise we can end up freeing the completion before complete() 347 * is done referencing it. 323 * is done referencing it. 348 */ 324 */ 349 raw_spin_lock_irqsave(&x->wait.lock, f !! 325 spin_lock_irqsave(&x->wait.lock, flags); 350 raw_spin_unlock_irqrestore(&x->wait.lo !! 326 spin_unlock_irqrestore(&x->wait.lock, flags); 351 return true; 327 return true; 352 } 328 } 353 EXPORT_SYMBOL(completion_done); 329 EXPORT_SYMBOL(completion_done); 354 330
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.