* [GIT PULL] workqueue: Fixes for v7.4-rc2
@ 2026-11-15 20:45 Tejun Heo
2026-11-15 22:14 ` Linus Torvalds
0 siblings, 1 reply; 10+ messages in thread
From: Tejun Heo @ 2026-11-15 20:45 UTC (permalink / raw)
To: Linus Torvalds; +Cc: Lai Jiangshan, linux-kernel
Hello,
The following changes since commit d89e1d186ec9271bf78e62771779e9fc03bf0fc3:
Merge tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/linux (2026-10-29 14:00:23 -1000)
are available in the Git repository at:
https://git.kernel.org/pub/scm/linux/kernel/git/tj/wq.git tags/wq-for-7.4-rc2-fixes
for you to fetch changes up to c7d64f52bffaca093acfb34277877138f6ec243a:
workqueue: Fix drain_workqueue() missing work items chained across pwqs (2026-11-02 08:06:21 -1000)
----------------------------------------------------------------
workqueue: Fixes for v7.4-rc2
- Draining a workqueue could return while a work item queued by another work
item was still pending, making workqueue destruction warn and leak the
workqueue.
----------------------------------------------------------------
Tejun Heo (1):
workqueue: Fix drain_workqueue() missing work items chained across pwqs
kernel/workqueue.c | 68 ++++++++++++++++++++++++++++++++++++++++--------------
1 file changed, 51 insertions(+), 17 deletions(-)
--
tejun
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [GIT PULL] workqueue: Fixes for v7.4-rc2
2026-11-15 20:45 [GIT PULL] workqueue: Fixes for v7.4-rc2 Tejun Heo
@ 2026-11-15 22:14 ` Linus Torvalds
2026-11-16 17:09 ` Tejun Heo
0 siblings, 1 reply; 10+ messages in thread
From: Linus Torvalds @ 2026-11-15 22:14 UTC (permalink / raw)
To: Tejun Heo; +Cc: Lai Jiangshan, linux-kernel
On Sun, 15 Nov 2026 at 12:45, Tejun Heo <tj@kernel.org> wrote:
>
> - Draining a workqueue could return while a work item queued by another work
> item was still pending, making workqueue destruction warn and leak the
> workqueue.
Why is this re-creating - badly - the existing sequence counter code?
Not only does it do the memory barriers by hand - possibly getting
them wrong, but it seems actively broken.
There's a reason proper sequence counters have that "odd/even" rule,
and a writer increments the sequence counter *twice* - once when it
starts the modification and once when it is done.
As far as I can tell, your "fix" is still broken:
drain_workqueue:
mutex_lock(&wq->mutex);
if (!wq->nr_drainers++)
wq->flags |= __WQ_DRAINING;
mutex_unlock(&wq->mutex);
seq = atomic_read(&wq->drain_seq);
__queue_work:
if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING))) {
...
smp_mb__before_atomic();
atomic_inc(&wq->drain_seq);
smp_mb__after_atomic();
but what if __queue_work() was called before __WQ_DRAINING was set and
is running concurrently and never incremented the sequence number?
Of even if it *did* increment the sequence number, what keeps it from
doing that *before* the atomic_read() in drain_workqueue() and then
Either way drain_workqueue() never sees that drain_seq change, and
thinks everythings is fine, even though __queue_work added an entry?
Is there some other serialization that I'm missing?
Anyway, sequence numbers almost never work with a single write,
exactly beause the single write is itself then not ordered with the
changes that the writer does (ie the sequence point update happens
before them or after them, but either way the reader might miss the
sequence number update, becuase it's not atomic wrt the actual change
it is supposed to be about.
The actual 8real* sequence numbers we have do that two-step change
exactly because of these issues. The writer increments once before it
starts writing, and increments it again after it is done. That way the
reader actually can check whether something changed (an odd value
means that change is happening and needs retrying regardless - which
is why read_seqcount_begin(() will loop on odd numbers)
It is possible that I'm missing something that makes your ad-hoc
version ok, but this really does look just plain broken.
I pulled it and then unpulled it, waiting for explanations.
Linus
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [GIT PULL] workqueue: Fixes for v7.4-rc2
2026-11-15 22:14 ` Linus Torvalds
@ 2026-11-16 17:09 ` Tejun Heo
2026-11-16 17:29 ` Linus Torvalds
0 siblings, 1 reply; 10+ messages in thread
From: Tejun Heo @ 2026-11-16 17:09 UTC (permalink / raw)
To: Linus Torvalds; +Cc: Lai Jiangshan, linux-kernel
Hello, Linus.
On Sun, Nov 15, 2026 at 02:14:41PM -0800, Linus Torvalds wrote:
> On Sun, 15 Nov 2026 at 12:45, Tejun Heo <tj@kernel.org> wrote:
> >
> > - Draining a workqueue could return while a work item queued by another work
> > item was still pending, making workqueue destruction warn and leak the
> > workqueue.
>
> Why is this re-creating - badly - the existing sequence counter code?
I was trying to plug the hole minimally and it just didn't trigger "oh this
is seqcount".
> Not only does it do the memory barriers by hand - possibly getting
> them wrong, but it seems actively broken.
>
> There's a reason proper sequence counters have that "odd/even" rule,
> and a writer increments the sequence counter *twice* - once when it
> starts the modification and once when it is done.
>
> As far as I can tell, your "fix" is still broken:
>
> drain_workqueue:
>
> mutex_lock(&wq->mutex);
> if (!wq->nr_drainers++)
> wq->flags |= __WQ_DRAINING;
> mutex_unlock(&wq->mutex);
>
> seq = atomic_read(&wq->drain_seq);
>
>
> __queue_work:
>
> if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING))) {
> ...
> smp_mb__before_atomic();
> atomic_inc(&wq->drain_seq);
> smp_mb__after_atomic();
>
> but what if __queue_work() was called before __WQ_DRAINING was set and
> is running concurrently and never incremented the sequence number?
>
> Of even if it *did* increment the sequence number, what keeps it from
> doing that *before* the atomic_read() in drain_workqueue() and then
>
> Either way drain_workqueue() never sees that drain_seq change, and
> thinks everythings is fine, even though __queue_work added an entry?
>
> Is there some other serialization that I'm missing?
Hmm... so, draining looks like this:
set DRAINING;
read seq;
flush wq:
for each pwq:
flush them;
for each pwq:
lock and check whether empty;
if not drained or seq bumped, try again;
and the queueing side tests DRAINING and incs the seq while the queueing
work item is still in flight on its pwq. The chained insertion is under the
pool lock.
Any work item that hasn't seen DRAINING or inc'd the seq before draining
reads it should be visible to the flush wq step, and by the time flush
finishes, all the immediate chained work items it queued should be visible
to the following pwq empty test step.
A chained work item queued after the flush started saw DRAINING and inc'd
the seq after the read, so if it chains further and finishes before the
empty test, the seq check catches it.
> Anyway, sequence numbers almost never work with a single write,
> exactly beause the single write is itself then not ordered with the
> changes that the writer does (ie the sequence point update happens
> before them or after them, but either way the reader might miss the
> sequence number update, becuase it's not atomic wrt the actual change
> it is supposed to be about.
>
> The actual 8real* sequence numbers we have do that two-step change
> exactly because of these issues. The writer increments once before it
> starts writing, and increments it again after it is done. That way the
> reader actually can check whether something changed (an odd value
> means that change is happening and needs retrying regardless - which
> is why read_seqcount_begin(() will loop on odd numbers)
>
> It is possible that I'm missing something that makes your ad-hoc
> version ok, but this really does look just plain broken.
>
> I pulled it and then unpulled it, waiting for explanations.
I don't think it's broken but it is adhoc and difficult to get one's head
around including my own. It'd be a lot simpler w/ seqcount. I'm traveling
this week, so might be slower but will redo it.
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [GIT PULL] workqueue: Fixes for v7.4-rc2
2026-11-16 17:09 ` Tejun Heo
@ 2026-11-16 17:29 ` Linus Torvalds
2026-11-18 17:27 ` Linus Torvalds
0 siblings, 1 reply; 10+ messages in thread
From: Linus Torvalds @ 2026-11-16 17:29 UTC (permalink / raw)
To: Tejun Heo; +Cc: Lai Jiangshan, linux-kernel
On Mon, 16 Nov 2026 at 09:09, Tejun Heo <tj@kernel.org> wrote:
>
> Hmm... so, draining looks like this:
>
> set DRAINING;
> read seq;
> flush wq:
> for each pwq:
> flush them;
> for each pwq:
> lock and check whether empty;
> if not drained or seq bumped, try again;
>
> and the queueing side tests DRAINING and incs the seq while the queueing
> work item is still in flight on its pwq. The chained insertion is under the
> pool lock.
So now I don't have the patch in my tree any more (I could fetch it
again, but I'm lazy), but from memory and my lkimited quoting the
queueing side only incremented the seq number *if* it saw DRAINING.
And that seemed to be the biggest issue.
If the queueing side did *not* see DRAINING, it also wouldn't
increment the sequence number, and it also didn't have any memory
barriers.
So it looked to me like the code basically fell back to the exact old
sequence that the patch claimed to fix: if the queuing side missed
seeing that "set DRAINING" part, all the changes in the commit became
no-ops. Yes, the draining side would re-read the sequence counter and
retry, but the sequence counter didn't actually change because the
queueing side wouldn't be updating it.
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 don't think it's broken but it is adhoc and difficult to get one's head
> around including my own. It'd be a lot simpler w/ seqcount. I'm traveling
> this week, so might be slower but will redo it.
Thanks.
The main problem with a seqcount might be performance - I'm assuming
you tried to avoid the write for the common cases. I assume *draining*
is the slow and uncommon case, and typically *that* side is where you
want to do the writer side of the seqcount, and then the fast and
common case is the one that wants to do the sequence number read.
So maybe the generic seqcount code isn't what you want, and maybe your
current code is the right thing, and my "it feels wrong" is the wrong
reaction.
Put another way: you should also just feel free to tell me that I was
wrong, and explain *why* it all works the way you did it. Use small
words and call me names.
Linus
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [GIT PULL] workqueue: Fixes for v7.4-rc2
2026-11-16 17:29 ` Linus Torvalds
@ 2026-11-18 17:27 ` Linus Torvalds
2026-11-23 10:11 ` Tejun Heo
2026-11-23 12:05 ` [PATCH wq/for-7.4-fixes] workqueue: Fix drain_workqueue() missing work items chained across pwqs Tejun Heo
0 siblings, 2 replies; 10+ messages in thread
From: Linus Torvalds @ 2026-11-18 17:27 UTC (permalink / raw)
To: Tejun Heo; +Cc: Lai Jiangshan, linux-kernel
On Mon, 16 Nov 2026 at 09:29, Linus Torvalds
<torvalds@linux-foundation.org> 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",
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [GIT PULL] workqueue: Fixes for v7.4-rc2
2026-11-18 17:27 ` Linus Torvalds
@ 2026-11-23 10:11 ` Tejun Heo
2026-11-23 12:05 ` [PATCH wq/for-7.4-fixes] workqueue: Fix drain_workqueue() missing work items chained across pwqs Tejun Heo
1 sibling, 0 replies; 10+ messages in thread
From: Tejun Heo @ 2026-11-23 10:11 UTC (permalink / raw)
To: Linus Torvalds; +Cc: Lai Jiangshan, linux-kernel
Hello,
On Wed, Nov 18, 2026 at 11:27:38AM -0600, Linus Torvalds wrote:
...
> 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?
Yeah, this is a lot simpler. Instead of trying to detect events across
flush, just make sure that empty test actually can test empty.
> A *UNTESTED* patch like this, in other words...
This looks correct to me but lemme do some testing on it.
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH wq/for-7.4-fixes] workqueue: Fix drain_workqueue() missing work items chained across pwqs
2026-11-18 17:27 ` Linus Torvalds
2026-11-23 10:11 ` Tejun Heo
@ 2026-11-23 12:05 ` Tejun Heo
1 sibling, 0 replies; 10+ messages in thread
From: Tejun Heo @ 2026-11-23 12:05 UTC (permalink / raw)
To: Linus Torvalds; +Cc: Lai Jiangshan, Breno Leitao, linux-kernel, stable
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH wq/for-7.4-fixes] workqueue: Fix drain_workqueue() missing work items chained across pwqs
2026-10-30 16:42 Tejun Heo
2026-11-02 10:59 ` Breno Leitao
@ 2026-11-02 18:21 ` Tejun Heo
1 sibling, 0 replies; 10+ messages in thread
From: Tejun Heo @ 2026-11-02 18:21 UTC (permalink / raw)
To: linux-kernel; +Cc: Lai Jiangshan, Breno Leitao, stable
Applied to wq/for-7.4-fixes with the following tag added:
Reviewed-by: Breno Leitao <leitao@debian.org>
With this patch, a work item that keeps re-queueing itself forever
makes drain_workqueue() loop forever. The current code can escape that
through this race, but the escape still trips the WARN in
destroy_workqueue() and leaks the workqueue.
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH wq/for-7.4-fixes] workqueue: Fix drain_workqueue() missing work items chained across pwqs
2026-10-30 16:42 Tejun Heo
@ 2026-11-02 10:59 ` Breno Leitao
2026-11-02 18:21 ` Tejun Heo
1 sibling, 0 replies; 10+ messages in thread
From: Breno Leitao @ 2026-11-02 10:59 UTC (permalink / raw)
To: Tejun Heo; +Cc: linux-kernel, Lai Jiangshan, stable
On Fri, Oct 30, 2026 at 06:42:10AM -1000, Tejun Heo wrote:
> 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.
Very nice finding, basically drain_workqueue() could return while the
tail of a chain was still pending.
> Add wq->drain_seq, which counts chained queueings while the workqueue is
> draining. drain_workqueue() reads it before each flush and retries if it
> changed by the end of the check pass. Flushing once more after an empty pass
> is not enough, as a longer chain is missed the same way.
I am curious about a work function that keeps re-queueing itself
forever. That is a bug in the caller for sure, but the current code can
escape it by luck through this race (and then leak the workqueue), while
with this patch drain_workqueue() will loop forever. Is my reading
correct here?
> 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+
> Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: Breno Leitao <leitao@debian.org>
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH wq/for-7.4-fixes] workqueue: Fix drain_workqueue() missing work items chained across pwqs
@ 2026-10-30 16:42 Tejun Heo
2026-11-02 10:59 ` Breno Leitao
2026-11-02 18:21 ` Tejun Heo
0 siblings, 2 replies; 10+ messages in thread
From: Tejun Heo @ 2026-10-30 16:42 UTC (permalink / raw)
To: linux-kernel; +Cc: Lai Jiangshan, Breno Leitao, Tejun Heo, stable
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 wq->drain_seq, which counts chained queueings while the workqueue is
draining. drain_workqueue() reads it before each flush and retries if it
changed by the end of the check pass. Flushing once more after an empty pass
is not enough, as a longer chain is missed the same way.
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+
Signed-off-by: Tejun Heo <tj@kernel.org>
---
kernel/workqueue.c | 68 ++++++++++++++++++++++++++++++++++------------
1 file changed, 51 insertions(+), 17 deletions(-)
diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index f2e8071a6f4e..955bf021f042 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -386,6 +386,7 @@ struct workqueue_struct {
struct worker *rescuer; /* MD: rescue worker */
int nr_drainers; /* WQ: drain in progress */
+ atomic_t drain_seq; /* see drain_workqueue() */
/* See alloc_workqueue() function comment for info on min/max_active */
int max_active; /* WO: max active works */
@@ -2432,22 +2433,31 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,
* allowed. The __WQ_DESTROYING helps to spot the issue that
* queues a new work item to a wq after destroy_workqueue(wq).
*/
- if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) &&
- WARN_ONCE(!is_chained_work(wq), "workqueue: cannot queue %ps on wq %s\n",
- work->func, wq->name))) {
- struct work_offq_data offqd;
+ if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING))) {
+ if (WARN_ONCE(!is_chained_work(wq), "workqueue: cannot queue %ps on wq %s\n",
+ work->func, wq->name)) {
+ struct work_offq_data offqd;
+
+ /*
+ * State on entry: PENDING is set, work is off-queue (no
+ * insert_work() has run).
+ *
+ * Returning without clearing PENDING would leave the
+ * work in a weird state (PENDING=1, PWQ=0, entry empty)
+ */
+ work_offqd_unpack(&offqd, *work_data_bits(work));
+ set_work_pool_and_clear_pending(work, offqd.pool_id,
+ work_offqd_pack_flags(&offqd));
+ return;
+ }
/*
- * State on entry: PENDING is set, work is off-queue (no
- * insert_work() has run).
- *
- * Returning without clearing PENDING would leave the work
- * in a weird state (PENDING=1, PWQ=0, entry empty)
+ * Pairs with the two smp_mb()s in drain_workqueue(), which
+ * retries its check pass if @wq->drain_seq changed.
*/
- work_offqd_unpack(&offqd, *work_data_bits(work));
- set_work_pool_and_clear_pending(work, offqd.pool_id,
- work_offqd_pack_flags(&offqd));
- return;
+ smp_mb__before_atomic();
+ atomic_inc(&wq->drain_seq);
+ smp_mb__after_atomic();
}
rcu_read_lock();
retry:
@@ -4464,6 +4474,8 @@ void drain_workqueue(struct workqueue_struct *wq)
{
unsigned int flush_cnt = 0;
struct pool_workqueue *pwq;
+ bool drained;
+ int seq;
/*
* __queue_work() needs to test whether there are drainers, is much
@@ -4474,21 +4486,42 @@ void drain_workqueue(struct workqueue_struct *wq)
if (!wq->nr_drainers++)
wq->flags |= __WQ_DRAINING;
mutex_unlock(&wq->mutex);
+
+ /*
+ * A chained work item can land on a pwq the check pass below has
+ * already found empty. __queue_work() counts chained queueings in
+ * @wq->drain_seq. Retry if it changed across the flush and the pass.
+ */
reflush:
+ seq = atomic_read(&wq->drain_seq);
+
+ /*
+ * Pairs with smp_mb__before_atomic() in __queue_work(). For each
+ * chained queueing, either the read above doesn't see its increment or
+ * the flush sees the queueing work item and waits for it.
+ */
+ smp_mb();
__flush_workqueue(wq);
mutex_lock(&wq->mutex);
+ drained = true;
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;
+ if (!drained)
+ break;
+ }
+ /*
+ * Pairs with smp_mb__after_atomic() in __queue_work(). For the earliest
+ * queueing the pass missed, either the pass sees the queueing work item
+ * in flight or the read below sees its increment.
+ */
+ smp_mb();
+ if (!drained || atomic_read(&wq->drain_seq) != seq) {
if (++flush_cnt == 10 ||
(flush_cnt % 100 == 0 && flush_cnt <= 1000))
pr_warn("workqueue %s: %s() isn't complete after %u tries\n",
@@ -6179,6 +6212,7 @@ static struct workqueue_struct *__alloc_workqueue(const char *fmt,
wq->saved_max_active = max_active;
mutex_init(&wq->mutex);
atomic_set(&wq->nr_pwqs_to_flush, 0);
+ atomic_set(&wq->drain_seq, 0);
INIT_LIST_HEAD(&wq->pwqs);
INIT_LIST_HEAD(&wq->flusher_queue);
INIT_LIST_HEAD(&wq->flusher_overflow);
--
2.56.0
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-11-23 12:05 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 20:45 [GIT PULL] workqueue: Fixes for v7.4-rc2 Tejun Heo
2026-11-15 22:14 ` Linus Torvalds
2026-11-16 17:09 ` Tejun Heo
2026-11-16 17:29 ` Linus Torvalds
2026-11-18 17:27 ` Linus Torvalds
2026-11-23 10:11 ` Tejun Heo
2026-11-23 12:05 ` [PATCH wq/for-7.4-fixes] workqueue: Fix drain_workqueue() missing work items chained across pwqs Tejun Heo
-- strict thread matches above, loose matches on Subject: below --
2026-10-30 16:42 Tejun Heo
2026-11-02 10:59 ` Breno Leitao
2026-11-02 18:21 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®