Re: [PATCH 04/10] workqueue: Move nr_active handling into helpers

From: Lai Jiangshan
Date: Tue Dec 26 2023 - 04:13:15 EST


On Wed, Dec 20, 2023 at 3:25 PM Tejun Heo <tj@xxxxxxxxxx> wrote:

> +static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq)
> {
> - struct work_struct *work = list_first_entry(&pwq->inactive_works,
> - struct work_struct, entry);
> + struct workqueue_struct *wq = pwq->wq;
> + struct worker_pool *pool = pwq->pool;
> + bool obtained;
>
> - pwq_activate_work(pwq, work);
> + lockdep_assert_held(&pool->lock);
> +
> + obtained = pwq->nr_active < wq->max_active;

It is better to use READ_ONCE(wq->max_active) here in case
the compiler issues code to calculate "obtained" multi-time.

Theoretically, READ_ONCE(wq->max_active) is recommended
from the patch1 in pwq_dec_nr_in_flight() and __queue_work()
since there is no wq->mutex for them.


> +
> + if (obtained)
> + pwq->nr_active++;
> + return obtained;
> +}
> +