Skip to content

Commit

Permalink
workqueue: fix race condition in flush_workqueue()
Browse files Browse the repository at this point in the history
When one flusher is cascading to the next flusher, it first sets
wq->first_flusher to the next one and sets up the next flush cycle.
If there's nothing to do for the next cycle, it clears
wq->flush_flusher and proceeds to the one after that.

If the woken up flusher checks wq->first_flusher before it gets
cleared, it will incorrectly assume the role of the first flusher,
which triggers BUG_ON() sanity check.

Fix it by checking wq->first_flusher again after grabbing the mutex.

Signed-off-by: Tejun Heo <tj@kernel.org>
  • Loading branch information
Tejun Heo committed Jul 2, 2010
1 parent cb44476 commit 4ce48b3
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions kernel/workqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -2138,6 +2138,10 @@ void flush_workqueue(struct workqueue_struct *wq)

mutex_lock(&wq->flush_mutex);

/* we might have raced, check again with mutex held */
if (wq->first_flusher != &this_flusher)
goto out_unlock;

wq->first_flusher = NULL;

BUG_ON(!list_empty(&this_flusher.list));
Expand Down

0 comments on commit 4ce48b3

Please sign in to comment.