diff options
Diffstat (limited to 'kernel/sched/completion.c')
| -rw-r--r-- | kernel/sched/completion.c | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/kernel/sched/completion.c b/kernel/sched/completion.c index 607f852b4d04..8d0f35debf35 100644 --- a/kernel/sched/completion.c +++ b/kernel/sched/completion.c | |||
| @@ -268,6 +268,15 @@ bool try_wait_for_completion(struct completion *x) | |||
| 268 | unsigned long flags; | 268 | unsigned long flags; |
| 269 | int ret = 1; | 269 | int ret = 1; |
| 270 | 270 | ||
| 271 | /* | ||
| 272 | * Since x->done will need to be locked only | ||
| 273 | * in the non-blocking case, we check x->done | ||
| 274 | * first without taking the lock so we can | ||
| 275 | * return early in the blocking case. | ||
| 276 | */ | ||
| 277 | if (!READ_ONCE(x->done)) | ||
| 278 | return 0; | ||
| 279 | |||
| 271 | spin_lock_irqsave(&x->wait.lock, flags); | 280 | spin_lock_irqsave(&x->wait.lock, flags); |
| 272 | if (!x->done) | 281 | if (!x->done) |
| 273 | ret = 0; | 282 | ret = 0; |
| @@ -288,13 +297,21 @@ EXPORT_SYMBOL(try_wait_for_completion); | |||
| 288 | */ | 297 | */ |
| 289 | bool completion_done(struct completion *x) | 298 | bool completion_done(struct completion *x) |
| 290 | { | 299 | { |
| 291 | unsigned long flags; | 300 | if (!READ_ONCE(x->done)) |
| 292 | int ret = 1; | 301 | return false; |
| 293 | 302 | ||
| 294 | spin_lock_irqsave(&x->wait.lock, flags); | 303 | /* |
| 295 | if (!x->done) | 304 | * If ->done, we need to wait for complete() to release ->wait.lock |
| 296 | ret = 0; | 305 | * otherwise we can end up freeing the completion before complete() |
| 297 | spin_unlock_irqrestore(&x->wait.lock, flags); | 306 | * is done referencing it. |
| 298 | return ret; | 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. | ||
| 312 | */ | ||
| 313 | smp_rmb(); | ||
| 314 | spin_unlock_wait(&x->wait.lock); | ||
| 315 | return true; | ||
| 299 | } | 316 | } |
| 300 | EXPORT_SYMBOL(completion_done); | 317 | EXPORT_SYMBOL(completion_done); |
