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