Skip to contentSkip to conversation

workqueue: Fix race condition in wq->stats incrementation

PATCH Applied Jiayuan Chen 2 participants 2 messages Apr 23, 2025 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 workqueue: Fix race condition in wq->stats incrementation

Fixed a race condition in incrementing wq->stats[PWQ_STAT_COMPLETED] by moving the operation under pool->lock.

---
 kernel/workqueue.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/workqueue.c +3 -1
index cf6203282737..1ea62b8c76b3 100644
@@ -3241,7 +3241,7 @@ __acquires(&pool->lock)
 	 * point will only record its address.
 	 */
 	trace_workqueue_execute_end(work, worker->current_func);
-	pwq->stats[PWQ_STAT_COMPLETED]++;
+
 	lock_map_release(&lockdep_map);
 	if (!bh_draining)
 		lock_map_release(pwq->wq->lockdep_map);
@@ -3272,6 +3272,8 @@ __acquires(&pool->lock)
 
 	raw_spin_lock_irq(&pool->lock);
 
+	pwq->stats[PWQ_STAT_COMPLETED]++;
+
 	/*
 	 * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked
 	 * CPU intensive by wq_worker_tick() if @work hogged CPU longer than
Signature · 1 line
2.47.1