diff options
author | Tejun Heo <tj@kernel.org> | 2013-04-01 14:23:31 -0400 |
---|---|---|
committer | Tejun Heo <tj@kernel.org> | 2013-04-01 14:23:31 -0400 |
commit | bc0caf099d9df4dd0fad24992b043b40541f4200 (patch) | |
tree | dddcccaf93af1eacd1606aadb06105e1a8f5ee11 /kernel/workqueue.c | |
parent | b5927605478b740d73192f587e458de1632106e8 (diff) |
workqueue: fix race condition in unbound workqueue free path
8864b4e59 ("workqueue: implement get/put_pwq()") implemented pwq
(pool_workqueue) refcnting which frees workqueue when the last pwq
goes away. It determined whether it was the last pwq by testing
wq->pwqs is empty. Unfortunately, the test was done outside wq->mutex
and multiple pwq release could race and try to free wq multiple times
leading to oops.
Test wq->pwqs emptiness while holding wq->mutex.
Signed-off-by: Tejun Heo <tj@kernel.org>
Diffstat (limited to 'kernel/workqueue.c')
-rw-r--r-- | kernel/workqueue.c | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/kernel/workqueue.c b/kernel/workqueue.c index 04a8b98d30ce..4d344326ae97 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c | |||
@@ -3534,6 +3534,7 @@ static void pwq_unbound_release_workfn(struct work_struct *work) | |||
3534 | unbound_release_work); | 3534 | unbound_release_work); |
3535 | struct workqueue_struct *wq = pwq->wq; | 3535 | struct workqueue_struct *wq = pwq->wq; |
3536 | struct worker_pool *pool = pwq->pool; | 3536 | struct worker_pool *pool = pwq->pool; |
3537 | bool is_last; | ||
3537 | 3538 | ||
3538 | if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) | 3539 | if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) |
3539 | return; | 3540 | return; |
@@ -3545,6 +3546,7 @@ static void pwq_unbound_release_workfn(struct work_struct *work) | |||
3545 | */ | 3546 | */ |
3546 | mutex_lock(&wq->mutex); | 3547 | mutex_lock(&wq->mutex); |
3547 | list_del_rcu(&pwq->pwqs_node); | 3548 | list_del_rcu(&pwq->pwqs_node); |
3549 | is_last = list_empty(&wq->pwqs); | ||
3548 | mutex_unlock(&wq->mutex); | 3550 | mutex_unlock(&wq->mutex); |
3549 | 3551 | ||
3550 | put_unbound_pool(pool); | 3552 | put_unbound_pool(pool); |
@@ -3554,7 +3556,7 @@ static void pwq_unbound_release_workfn(struct work_struct *work) | |||
3554 | * If we're the last pwq going away, @wq is already dead and no one | 3556 | * If we're the last pwq going away, @wq is already dead and no one |
3555 | * is gonna access it anymore. Free it. | 3557 | * is gonna access it anymore. Free it. |
3556 | */ | 3558 | */ |
3557 | if (list_empty(&wq->pwqs)) | 3559 | if (is_last) |
3558 | kfree(wq); | 3560 | kfree(wq); |
3559 | } | 3561 | } |
3560 | 3562 | ||