Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH 0/2] workqueue: fix cancel_work_sync() on ordered workqueues and clarify docs
@ 2026-11-18 17:09 Alice Ryhl
  2026-11-18 17:09 ` [PATCH 1/2] workqueue: fix cancel_work_sync() on ordered workqueues Alice Ryhl
  0 siblings, 1 reply; 3+ messages in thread
From: Alice Ryhl @ 2026-11-18 17:09 UTC (permalink / raw)
  To: Tejun Heo, Lai Jiangshan, Jonathan Corbet
  Cc: Boqun Feng, Miguel Ojeda, Danilo Krummrich, Philipp Stanner,
	Matthew Brost, Christian König, Thomas Hellström,
	Rodrigo Vivi, Boris Brezillon, Steven Price, Liviu Dudau,
	Onur Özkan, dri-devel, intel-xe, rust-for-linux, linux-doc,
	linux-kernel, stable, Alice Ryhl

Please see the commit message of patch 1 for details.

This came up while figuring out what to do about this sashiko review:
https://sashiko.dev/#/patchset/20261020-rust-workqueue-v3-0-ea7c74d02068%40google.com

---
Alice Ryhl (2):
      workqueue: fix cancel_work_sync() on ordered workqueues
      workqueue: doc: clarify cancel_work_sync() cross-workqueue behavior

 Documentation/core-api/workqueue.rst |  7 ++++++-
 kernel/workqueue.c                   | 37 +++++++++++++++++++++++++++++-------
 2 files changed, 36 insertions(+), 8 deletions(-)
---
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
change-id: 20261118-ordered-wq-cancel-63b02e0feac4

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] workqueue: fix cancel_work_sync() on ordered workqueues
  2026-11-18 17:09 [PATCH 0/2] workqueue: fix cancel_work_sync() on ordered workqueues and clarify docs Alice Ryhl
@ 2026-11-18 17:09 ` Alice Ryhl
  2026-11-25  0:29   ` Tejun Heo
  0 siblings, 1 reply; 3+ messages in thread
From: Alice Ryhl @ 2026-11-18 17:09 UTC (permalink / raw)
  To: Tejun Heo, Lai Jiangshan, Jonathan Corbet
  Cc: Boqun Feng, Miguel Ojeda, Danilo Krummrich, Philipp Stanner,
	Matthew Brost, Christian König, Thomas Hellström,
	Rodrigo Vivi, Boris Brezillon, Steven Price, Liviu Dudau,
	Onur Özkan, dri-devel, intel-xe, rust-for-linux, linux-doc,
	linux-kernel, stable, Alice Ryhl

There is currently a scenario where cancel_work_sync() /
disable_work_sync() does not wait for the work to finish execution on an
ordered workqueue (__WQ_ORDERED). This is because ordered workqueues
contain multiple workqueues internally (pwqs). Consider the following
scenario:

1. @work starts executing on pwq A (pool A), the oldest and only
   unplugged pwq in wq->pwqs.
2. Writing a new cpumask to /sys/devices/virtual/workqueue/cpumask
   allocates a new dfl_pwq (pwq B, backed by pool B) with
   pwq->plugged = true and appends it to wq->pwqs behind pwq A.
3. While @work is still executing on pool A, it is re-queued to the same
   ordered workqueue. Unlike normal workqueues, __queue_work() skips
   last_pool redirection on ordered workqueues so re-queued items do not
   jump ahead of work already waiting on pwq B, so @work is placed on
   pwq B->inactive_works and work->data is overwritten to point to
   pwq B.
4. cancel_work_sync() or disable_work_sync() steals @work from pwq B,
   and try_to_grab_pending() records pwq B->pool->id (pool B) in
   work->data.
5. start_flush_work() only inspects the single pool recorded in
   work->data (pool B, where @work has never executed), finds no worker
   running @work, and returns while @work is still executing on pool A.

Unfortunately, this scenario can lead to UAF if a driver uses
cancel_work_sync() and then frees the work item (or resources used by
the work item). And this happens on ordered workqueues in practice:

- drm/sched: drm_sched_wqueue_stop() calling cancel_work_sync() on
  work_run_job and work_free_job before scheduler teardown.
- drm/xe: gt_worker_disable() calling disable_work_sync() on
  gt->reset.worker and gt->wedge.worker.
- drm/panthor: panthor_device_suspend() calling cancel_work_sync() on
  ptdev->reset.work before suspending hardware.

Thus, fix try_to_grab_pending() to record the pool of the oldest pwq in
wq->pwqs when stealing a pending work item from an ordered workqueue, so
start_flush_work() flushes pool A and waits for @work to finish. If
@work is not actually executing on pool A, find_worker_executing_work()
simply returns NULL.

It is also needed for the soundness of the upcoming Rust ScopedWork
abstraction, which binds a work item to a single workqueue and relies on
cancel_work_sync() in Drop to guarantee that data borrowed by the work
item is no longer accessed once drop() returns.

Cc: stable@vger.kernel.org
Fixes: 58629d4871e8 ("workqueue: Always queue work items to the newest PWQ for order workqueues")
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 kernel/workqueue.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/kernel/workqueue.c b/kernel/workqueue.c
index af845214e78b..d7acedfb8b27 100644
--- a/kernel/workqueue.c
+++ b/kernel/workqueue.c
@@ -2263,6 +2263,7 @@ static int try_to_grab_pending(struct work_struct *work, u32 cflags,
 	pwq = get_work_pwq(work);
 	if (pwq && pwq->pool == pool) {
 		unsigned long work_data = *work_data_bits(work);
+		struct worker_pool *offq_pool = pool;
 
 		debug_work_deactivate(work);
 
@@ -2288,9 +2289,24 @@ static int try_to_grab_pending(struct work_struct *work, u32 cflags,
 		/*
 		 * work->data points to pwq iff queued. Let's point to pool. As
 		 * this destroys work->data needed by the next step, stash it.
+		 *
+		 * For ordered workqueues, __queue_work() skips last_pool
+		 * redirection so @pwq may be plugged while @work is still
+		 * executing on the oldest pwq after a cpumask update. Only the
+		 * oldest pwq is unplugged and can be executing @work, so record
+		 * its pool instead.
 		 */
-		set_work_pool_and_keep_pending(work, pool->id,
-					       pool_offq_flags(pool));
+		if (unlikely(pwq->wq->flags & __WQ_ORDERED)) {
+			struct pool_workqueue *oldest_pwq =
+				list_first_or_null_rcu(&pwq->wq->pwqs,
+						       struct pool_workqueue,
+						       pwqs_node);
+
+			if (oldest_pwq)
+				offq_pool = oldest_pwq->pool;
+		}
+		set_work_pool_and_keep_pending(work, offq_pool->id,
+					       pool_offq_flags(offq_pool));
 
 		/* must be the last step, see the function comment */
 		pwq_dec_nr_in_flight(pwq, work_data);

-- 
2.56.1.703.gf848ed807c-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] workqueue: fix cancel_work_sync() on ordered workqueues
  2026-11-18 17:09 ` [PATCH 1/2] workqueue: fix cancel_work_sync() on ordered workqueues Alice Ryhl
@ 2026-11-25  0:29   ` Tejun Heo
  0 siblings, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2026-11-25  0:29 UTC (permalink / raw)
  To: Alice Ryhl
  Cc: Lai Jiangshan, Jonathan Corbet, Boqun Feng, Miguel Ojeda,
	Danilo Krummrich, Philipp Stanner, Matthew Brost,
	Christian König, Thomas Hellström, Rodrigo Vivi,
	Boris Brezillon, Steven Price, Liviu Dudau, Onur Özkan,
	dri-devel, intel-xe, rust-for-linux, linux-doc, linux-kernel,
	stable

Hello, Alice.

On Wed, Nov 18, 2026 at 05:09:51PM +0000, Alice Ryhl wrote:
> +		if (unlikely(pwq->wq->flags & __WQ_ORDERED)) {
> +			struct pool_workqueue *oldest_pwq =
> +				list_first_or_null_rcu(&pwq->wq->pwqs,
> +						       struct pool_workqueue,
> +						       pwqs_node);
> +
> +			if (oldest_pwq)
> +				offq_pool = oldest_pwq->pool;
> +		}

@pwq still has @work queued on it here, so it's linked on wq->pwqs and the
list can't be empty. Maybe drop the NULL check? e.g.

  if (unlikely(pwq->wq->flags & __WQ_ORDERED))
  	offq_pool = list_entry_rcu(pwq->wq->pwqs.next, struct pool_workqueue,
  				   pwqs_node)->pool;

Thanks.

--
tejun

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-25  0:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 17:09 [PATCH 0/2] workqueue: fix cancel_work_sync() on ordered workqueues and clarify docs Alice Ryhl
2026-11-18 17:09 ` [PATCH 1/2] workqueue: fix cancel_work_sync() on ordered workqueues Alice Ryhl
2026-11-25  0:29   ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox