Re: [PATCH] workqueue: annotate the lockless nr_running read in need_more_worker()
From: Tejun Heo
Date: Mon Oct 12 2026 - 14:33:50 EST
Hello,
On Mon, Oct 12, 2026 at 09:36:05PM +0700, Nguyen Ngoc Thang wrote:
> Subject: [PATCH] workqueue: annotate the lockless nr_running read in need_more_worker()
The read is under pool->lock. The lockless side is the increment in
wq_worker_running(). Can you fix the subject?
> The race is benign: nr_running only goes 0 -> 1 here, and a stale 0
> merely causes a spurious wakeup of an idle worker. Annotate the read
> with data_race().
The increment can also go 1 -> 2: a worker sleeping in a work item wakes
up after kick_pool() woke another worker which is now running. The reason
it's benign is that the only lockless write is an increment. Decrements
and the reset are under pool->lock, so a reader holding the lock sees
either the old or the new count. 0 costs at most a spurious wakeup.
Nonzero is correct because the incrementing worker is running and
re-checks the worklist under pool->lock before it stops counting. Can you
update the description accordingly?
> + /*
> + * wq_worker_running() bumps nr_running without pool->lock. A stale
> + * read at worst causes a spurious wakeup, so the race is benign.
> + */
This duplicates the comment on nr_running in struct worker_pool. A pointer
to that would do.
Thanks.
--
tejun