Re: [GIT PULL] workqueue: Fixes for v7.4-rc2

From: Linus Torvalds

Date: Wed Nov 18 2026 - 12:28:58 EST


On Mon, 16 Nov 2026 at 09:29, Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> But I'll freely admit that I really only looked at the "shape" of the
> code, and it all felt wrong to me. This was not some deep-dive
> understanding, it was a "this doesn't feel right".

I think your original patch worked, and the memory barriers were what I
really ended up hating. But that's solvable by just doing the counter
inside the pool lock instead.

And that means that my suggestion of using the generic seqcount code was
barking up the wrong tree too: I reacted to the ad-hoc memory barriers
and the fact that you used a sequence counter, not to the actual code
and problem.

And then the drain_workqueue() logic can just be changed a bit: after
draining, go back and do kind of what you used to do, except don't look
at just whether the pwq is empty, but also at whether new work was
started?

A *UNTESTED* patch like this, in other words...

Linus

---

kernel/workqueue.c | 47 ++++++++++++++++++++++++++++++++++++-----------
1 file changed, 36 insertions(+), 11 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index af845214e78b..31d376656504 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -281,6 +281,7 @@ struct pool_workqueue {
int total_in_flight; /* L: sum of nr_in_flight[] */
int nr_in_flight[WORK_NR_COLORS];
/* L: nr of in_flight works */
+ unsigned long nr_started; /* L: see drain_workqueue() */
struct list_head flush_node; /* FN: node on wq_flush_pnode->pwqs */
bool plugged; /* L: execution suspended */

@@ -3455,6 +3456,7 @@ __acquires(&pool->lock)
*/
set_work_pool_and_clear_pending(work, pool->id, pool_offq_flags(pool));

+ pwq->nr_started++;
pwq->stats[PWQ_STAT_STARTED]++;

rcu_read_lock();
@@ -4449,6 +4451,29 @@ void __flush_workqueue(struct workqueue_struct *wq)
}
EXPORT_SYMBOL(__flush_workqueue);

+/* 0 if any pwq is busy, otherwise 2 * sum of nr_started + 1 */
+static unsigned long pwqs_nr_started(struct workqueue_struct *wq)
+{
+ struct pool_workqueue *pwq;
+ unsigned long sum = 0;
+
+ lockdep_assert_held(&wq->mutex);
+
+ for_each_pwq(pwq, wq) {
+ bool empty;
+
+ raw_spin_lock_irq(&pwq->pool->lock);
+ empty = pwq_is_empty(pwq);
+ sum += pwq->nr_started;
+ raw_spin_unlock_irq(&pwq->pool->lock);
+
+ if (!empty)
+ return 0;
+ }
+
+ return 2 * sum + 1;
+}
+
/**
* drain_workqueue - drain a workqueue
* @wq: workqueue to drain
@@ -4462,8 +4487,8 @@ EXPORT_SYMBOL(__flush_workqueue);
*/
void drain_workqueue(struct workqueue_struct *wq)
{
+ unsigned long started;
unsigned int flush_cnt = 0;
- struct pool_workqueue *pwq;

/*
* __queue_work() needs to test whether there are drainers, is much
@@ -4479,16 +4504,16 @@ void drain_workqueue(struct workqueue_struct *wq)

mutex_lock(&wq->mutex);

- for_each_pwq(pwq, wq) {
- bool drained;
-
- raw_spin_lock_irq(&pwq->pool->lock);
- drained = pwq_is_empty(pwq);
- raw_spin_unlock_irq(&pwq->pool->lock);
-
- if (drained)
- continue;
-
+ /*
+ * A pass isn't atomic across pwqs, so a chained work item can land on
+ * a pwq it already found empty. Anything slipping past the second pass
+ * needs a work item that was in flight at its pwq's first check, or
+ * that started between its pwq's two checks. The first makes the first
+ * pass fail, the second changes nr_started. wq->mutex keeps the set of
+ * pwqs stable across both passes.
+ */
+ started = pwqs_nr_started(wq);
+ if (!started || pwqs_nr_started(wq) != started) {
if (++flush_cnt == 10 ||
(flush_cnt % 100 == 0 && flush_cnt <= 1000))
pr_warn("workqueue %s: %s() isn't complete after %u tries\n",