[PATCH v2] workqueue: Fix race condition in wq->stats incrementation

Jiayuan Chen posted 1 patch 7 months, 4 weeks ago
kernel/workqueue.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH v2] workqueue: Fix race condition in wq->stats incrementation
Posted by Jiayuan Chen 7 months, 4 weeks ago
Fixed a race condition in incrementing wq->stats[PWQ_STAT_COMPLETED] by
moving the operation under pool->lock.

Reported-by: syzbot+01affb1491750534256d@syzkaller.appspotmail.com
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
 kernel/workqueue.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index cf6203282737..1ea62b8c76b3 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -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
-- 
2.47.1
Re: [PATCH v2] workqueue: Fix race condition in wq->stats incrementation
Posted by Tejun Heo 7 months, 4 weeks ago
On Thu, Apr 24, 2025 at 12:17:42AM +0800, Jiayuan Chen wrote:
> Fixed a race condition in incrementing wq->stats[PWQ_STAT_COMPLETED] by
> moving the operation under pool->lock.
> 
> Reported-by: syzbot+01affb1491750534256d@syzkaller.appspotmail.com
> Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>

Applied to wq/for-6.16.

Thanks.

-- 
tejun