Skip to content

Commit dd6c3c5

Browse files
committed
workqueue: Move pwq_dec_nr_in_flight() to the end of work item handling
The planned shared nr_active handling for unbound workqueues will make pwq_dec_nr_active() sometimes drop the pool lock temporarily to acquire other pool locks, which is necessary as retirement of an nr_active count from one pool may need kick off an inactive work item in another pool. This patch moves pwq_dec_nr_in_flight() call in try_to_grab_pending() to the end of work item handling so that work item state changes stay atomic. process_one_work() which is the other user of pwq_dec_nr_in_flight() already calls it at the end of work item handling. Comments are added to both call sites and pwq_dec_nr_in_flight(). This shouldn't cause any behavior changes. Signed-off-by: Tejun Heo <[email protected]> Reviewed-by: Lai Jiangshan <[email protected]>
1 parent 9f66cff commit dd6c3c5

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

kernel/workqueue.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,11 @@ static void pwq_dec_nr_active(struct pool_workqueue *pwq)
15871587
* A work either has completed or is removed from pending queue,
15881588
* decrement nr_in_flight of its pwq and handle workqueue flushing.
15891589
*
1590+
* NOTE:
1591+
* For unbound workqueues, this function may temporarily drop @pwq->pool->lock
1592+
* and thus should be called after all other state updates for the in-flight
1593+
* work item is complete.
1594+
*
15901595
* CONTEXT:
15911596
* raw_spin_lock_irq(pool->lock).
15921597
*/
@@ -1711,11 +1716,13 @@ static int try_to_grab_pending(struct work_struct *work, bool is_dwork,
17111716
pwq_activate_work(pwq, work);
17121717

17131718
list_del_init(&work->entry);
1714-
pwq_dec_nr_in_flight(pwq, *work_data_bits(work));
17151719

17161720
/* work->data points to pwq iff queued, point to pool */
17171721
set_work_pool_and_keep_pending(work, pool->id);
17181722

1723+
/* must be the last step, see the function comment */
1724+
pwq_dec_nr_in_flight(pwq, *work_data_bits(work));
1725+
17191726
raw_spin_unlock(&pool->lock);
17201727
rcu_read_unlock();
17211728
return 1;
@@ -2780,6 +2787,8 @@ __acquires(&pool->lock)
27802787
worker->current_func = NULL;
27812788
worker->current_pwq = NULL;
27822789
worker->current_color = INT_MAX;
2790+
2791+
/* must be the last step, see the function comment */
27832792
pwq_dec_nr_in_flight(pwq, work_data);
27842793
}
27852794

0 commit comments

Comments
 (0)