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