Skip to contentSkip to conversation

workqueue: annotate data-races around pwq->stats

PATCH In review Jiayuan Chen 2 participants 3 messages Apr 23, 2025 Workqueue

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 workqueue: annotate data-races around pwq->stats

Suppress warning by annotating these accesses using READ_ONCE() / WRITE_ONCE().

---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/workqueue.c +1 -1
index cf6203282737..d78640b5d188 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]++;
+	WRITE_ONCE(pwq->stats[PWQ_STAT_COMPLETED], READ_ONCE(pwq->stats[PWQ_STAT_COMPLETED]) + 1);
 	lock_map_release(&lockdep_map);
 	if (!bh_draining)
 		lock_map_release(pwq->wq->lockdep_map);
Signature · 1 line
2.47.1
maintainer
#1 lore raw
Quoted 14 lines · Jiayuan Chen

Suppress warning by annotating these accesses using READ_ONCE() / WRITE_ONCE().

---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/workqueue.c +0 -0
index cf6203282737..d78640b5d188 100644

kernel/workqueue.c __acquires(&pool->lock) · line 3244

Quoted 5 lines
kernel/workqueue.c +1 -1
@@ -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]++;
+	WRITE_ONCE(pwq->stats[PWQ_STAT_COMPLETED], READ_ONCE(pwq->stats[PWQ_STAT_COMPLETED]) + 1);

The function acquires pool->lock down below. Can you move it down inside the locked region instead of adding READ/WRITE_ONCE()?

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 40 lines · Tejun Heo
Quoted 37 lines · Jiayuan Chen

Suppress warning by annotating these accesses using

READ_ONCE() / WRITE_ONCE().

Reported-by [email protected]

---

 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c

index cf6203282737..d78640b5d188 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]++;

Quoted 13 lines
Quoted 2 lines

+ WRITE_ONCE(pwq->stats[PWQ_STAT_COMPLETED], READ_ONCE(pwq->stats[PWQ_STAT_COMPLETED]) + 1);

The function acquires pool->lock down below. Can you move it down inside the

locked region instead of adding READ/WRITE_ONCE()?

Thanks.

Signature · 1 line
tejun

Thanks, I can do that.
Previously, I thought it was for performance considerations that the metrics calculation is out of the lock's scope...