From: Tejun Heo <tj@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Lai Jiangshan <jiangshanlai@gmail.com>,
Breno Leitao <leitao@debian.org>,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH wq/for-7.4-fixes] workqueue: Fix drain_workqueue() missing work items chained across pwqs
Date: Mon, 23 Nov 2026 02:05:00 -1000 [thread overview]
Message-ID: <20261123120125.375076-1-tj@kernel.org> (raw)
In-Reply-To: <CAHk-=wjm3vbiNYsR+aHxmM3KCEgsSWb379ZQbCuZEsN1dhvbKg@mail.gmail.com>
From: Linus Torvalds <torvalds@linux-foundation.org>
drain_workqueue() flushes the workqueue, then checks each pwq under its
pool's lock and returns once a pass finds every pwq empty. Only chained
queueing is allowed meanwhile, and a chained work item can land on a pwq
other than the one the queueing work item is executing on. The pass misses
it when:
1. The pass finds pwq A empty.
2. A work item executing on pwq B queues a work item on pwq A.
3. The work item on pwq B finishes.
4. The pass finds pwq B empty.
drain_workqueue() returns with the work item on pwq A pending, and
destroy_workqueue() can then trip WARN_ON(pwq_busy()) and leak the
workqueue.
Add pwq->nr_started, which counts the work items a pwq has started
executing, and check the pwqs twice after each flush. The drain is complete
only if both passes find every pwq empty and no work item started in
between. The counter is updated and read under pool->lock, and the queueing
path is not touched.
Verified with reproducers of both shapes: self-requeueing work items on an
unbound workqueue and chained queue_work_on() of another work item on a
per-cpu workqueue.
Fixes: c8efcc258946 ("workqueue: allow chained queueing during destruction")
Cc: stable@vger.kernel.org # v2.6.38+
Link: https://lore.kernel.org/all/CAHk-=wjm3vbiNYsR+aHxmM3KCEgsSWb379ZQbCuZEsN1dhvbKg@mail.gmail.com/
[ tj: Wrote the description and updated the pwqs_nr_started() comment. ]
Signed-off-by: Tejun Heo <tj@kernel.org>
---
Applied to wq/for-7.4-fixes, replacing c7d64f52bffa ("workqueue: Fix
drain_workqueue() missing work items chained across pwqs") from the pull
request.
kernel/workqueue.c | 51 ++++++++++++++++++++++++++++++++++++----------
1 file changed, 40 insertions(+), 11 deletions(-)
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index f2e8071a6f4e..08046ff0d697 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,33 @@ void __flush_workqueue(struct workqueue_struct *wq)
}
EXPORT_SYMBOL(__flush_workqueue);
+/*
+ * Return 0 if any pwq of @wq is busy. Otherwise, return the sum of nr_started
+ * of all pwqs encoded as 2 * sum + 1, which is always odd, so that 0 can only
+ * mean busy.
+ */
+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 +4491,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 +4508,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",
--
2.56.1
next parent reply other threads:[~2026-11-23 12:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <fb224bad9baf5f44cf17e1d664776a4e@kernel.org>
[not found] ` <CAHk-=wg4jAWZvxWo0OZ+fFTU=SBtQ7dnsUCV7hx+UAifkwPUwQ@mail.gmail.com>
[not found] ` <avs4uWg9ch-Bqobj@slm.duckdns.org>
[not found] ` <CAHk-=wg+MO19H7KVgA+SaZvvt5O+3Tp8AxHVhpN23JtaSq=G4g@mail.gmail.com>
[not found] ` <CAHk-=wjm3vbiNYsR+aHxmM3KCEgsSWb379ZQbCuZEsN1dhvbKg@mail.gmail.com>
2026-11-23 12:05 ` Tejun Heo [this message]
2026-10-30 16:42 [PATCH wq/for-7.4-fixes] workqueue: Fix drain_workqueue() missing work items chained across pwqs Tejun Heo
2026-11-02 10:59 ` Breno Leitao
2026-11-02 18:21 ` Tejun Heo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123120125.375076-1-tj@kernel.org \
--to=tj@kernel.org \
--cc=jiangshanlai@gmail.com \
--cc=leitao@debian.org \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox