Re: [PATCH 01/10] workqueue: Move pwq->max_active to wq->max_active

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


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

> +static void wq_adjust_max_active(struct workqueue_struct *wq)
> +{
> + struct pool_workqueue *pwq;
> +
> + lockdep_assert_held(&wq->mutex);
> +
> + if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) {
> + wq->max_active = 0;
> + return;
> + }
> +
> + if (wq->max_active == wq->saved_max_active)
> + return;
> +
> + wq->max_active = wq->saved_max_active;
> +

If a work item gets queued now, it will get scheduled earlier than a
previous queued one which is still in the inactive list.

To solve it, I recommend adding wq->queue_max_active which will be
updated after the following code and used only when queue_work().
But it requires round-robin through PWQs the second time after
wq->queue_max_active is updated to catch the new inactivated items.

Or just keep pwq->max_active and will be
updated after activating inactivated items and used only when queue_work().


> + for_each_pwq(pwq, wq) {
> + unsigned long flags;
> +
> + /* this function can be called during early boot w/ irq disabled */
> + raw_spin_lock_irqsave(&pwq->pool->lock, flags);
> +
> + while (!list_empty(&pwq->inactive_works) &&
> + pwq->nr_active < wq->max_active)
> + pwq_activate_first_inactive(pwq);
> +
> + kick_pool(pwq->pool);
> +
> + raw_spin_unlock_irqrestore(&pwq->pool->lock, flags);
> + }
> +}
> +