* [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
2026-11-18 17:09 ` [PATCH 2/2] workqueue: doc: clarify cancel_work_sync() cross-workqueue behavior Alice Ryhl
0 siblings, 2 replies; 5+ 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] 5+ 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 2026-11-18 17:09 ` [PATCH 2/2] workqueue: doc: clarify cancel_work_sync() cross-workqueue behavior Alice Ryhl 1 sibling, 1 reply; 5+ 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] 5+ 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; 5+ 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] 5+ messages in thread
* [PATCH 2/2] workqueue: doc: clarify cancel_work_sync() cross-workqueue behavior 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-18 17:09 ` Alice Ryhl 2026-11-25 3:25 ` Tejun Heo 1 sibling, 1 reply; 5+ 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, Alice Ryhl The kernel-doc comment for cancel_work_sync() claims that it waits for @work to finish even if @work "migrates to another workqueue". However, this has not been true since commit 606a5020b9bd ("workqueue: gut flush[_delayed]_work_sync()") replaced the all-pools scan with start_flush_work(). Consider the following scenario: 1. @work starts executing on wq A (pool A). 2. While @work is still executing on pool A, it is queued onto a different workqueue wq B (backed by pool B). Because __queue_work() only redirects to last_pool when re-queueing onto the same workqueue, @work is queued on pool B (where it may even start executing concurrently with pool A) and work->data is overwritten with wq B's pwq / pool B. 3. flush_work(), cancel_work_sync(), or disable_work_sync() is called on @work. 4. start_flush_work() only inspects the single pool recorded in work->data (pool B) and returns once @work is no longer pending or executing on pool B, while @work is still executing on pool A. Queueing an in-flight work item onto another workqueue already violates the documented non-reentrance conditions in Documentation/core-api/workqueue.rst. Thus, update the kernel-doc comments for flush_work(), cancel_work_sync(), and disable_work_sync() along with workqueue.rst to stop claiming that cross-workqueue migration is supported, and link directly to the non-reentrance conditions section. Fixes: 606a5020b9bd ("workqueue: gut flush[_delayed]_work_sync()") Signed-off-by: Alice Ryhl <aliceryhl@google.com> --- Documentation/core-api/workqueue.rst | 7 ++++++- kernel/workqueue.c | 17 ++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Documentation/core-api/workqueue.rst b/Documentation/core-api/workqueue.rst index bb770f556568..007dee698ede 100644 --- a/Documentation/core-api/workqueue.rst +++ b/Documentation/core-api/workqueue.rst @@ -769,6 +769,8 @@ The work item's function should be trivially visible in the stack trace. +.. _workqueue_non_reentrance: + Non-reentrance Conditions ========================= @@ -784,7 +786,10 @@ executed by at most one worker system-wide at any given time. Note that requeuing the work item (to the same queue) in the self function doesn't break these conditions, so it's safe to do. Otherwise, caution is -required when breaking the conditions inside a work function. +required when breaking the conditions inside a work function; in particular, +flush_work(), cancel_work_sync(), and disable_work_sync() only track the last +queued instance and may return while an earlier execution on another workqueue +is still running. Kernel Inline Documentations Reference diff --git a/kernel/workqueue.c b/kernel/workqueue.c index d7acedfb8b27..5c7018ea0b48 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c @@ -4634,7 +4634,9 @@ static bool __flush_work(struct work_struct *work, bool from_cancel) * @work: the work to flush * * Wait until @work has finished execution. @work is guaranteed to be idle - * on return if it hasn't been requeued since flush started. + * on return if it hasn't been requeued since flush started and the + * :ref:`non-reentrance conditions <workqueue_non_reentrance>` in + * Documentation/core-api/workqueue.rst hold. * * Return: * %true if flush_work() waited for the work to finish execution, @@ -4764,9 +4766,14 @@ EXPORT_SYMBOL(cancel_work); * @work: the work to cancel * * Cancel @work and wait for its execution to finish. This function can be used - * even if the work re-queues itself or migrates to another workqueue. On return - * from this function, @work is guaranteed to be not pending or executing on any - * CPU as long as there aren't racing enqueues. + * even if the work re-queues itself. On return from this function, @work is + * guaranteed to be not pending or executing on any CPU as long as there aren't + * racing enqueues. + * + * Care must be taken if :ref:`the non-reentrance conditions <workqueue_non_reentrance>` + * in Documentation/core-api/workqueue.rst are not upheld for this work item. + * In that case, this function only waits for execution on the last queued + * workqueue. * * cancel_work_sync(&delayed_work->work) must not be used for delayed_work's. * Use cancel_delayed_work_sync() instead. @@ -4843,7 +4850,7 @@ EXPORT_SYMBOL_GPL(disable_work); * @work: work item to disable * * Similar to disable_work() but also wait for @work to finish if currently - * executing. + * executing; see cancel_work_sync(). * * Must be called from a sleepable context if @work was last queued on a non-BH * workqueue. Can also be called from non-hardirq atomic contexts including BH -- 2.56.1.703.gf848ed807c-goog ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] workqueue: doc: clarify cancel_work_sync() cross-workqueue behavior 2026-11-18 17:09 ` [PATCH 2/2] workqueue: doc: clarify cancel_work_sync() cross-workqueue behavior Alice Ryhl @ 2026-11-25 3:25 ` Tejun Heo 0 siblings, 0 replies; 5+ messages in thread From: Tejun Heo @ 2026-11-25 3:25 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, Corey Minyard, openipmi-developer, Alasdair Kergon, Mike Snitzer, Mikulas Patocka, Benjamin Marzinski, dm-devel, Dmitry Torokhov, linux-input, Rafael J. Wysocki, Viresh Kumar, linux-arm-msm, linux-pm, D. Wythe, Dust Li, Sidraya Jayagond, Mahanta Jambigi, Tony Lu, Wen Gu, David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman, linux-rdma, linux-s390, netdev, Oder Chiou, Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai, linux-sound, Michael S. Tsirkin, Jason Wang, Eugenio Pérez, Xuan Zhuo, virtualization Applied to wq/for-7.5. Note that cancel_work_sync() and friends haven't been waiting for a work item which is still running from a different workqueue since 606a5020b9bd ("workqueue: gut flush[_delayed]_work_sync()") which went in in v3.7. This has been broken for well over a decade and the documentation now says so. The following queue the same work item on more than one workqueue and then sync-cancel or flush it. As cancel_work_sync() doesn't guarantee that the work item is quiescent in that case, they need to make sure in some other way, e.g. by flushing the workqueues that are involved. Note that system_wq and system_percpu_wq, which schedule_work() uses, are different workqueues. cc'ing the maintainers. - drivers/char/ipmi/ipmi_msghandler.c: smi_work - drivers/md/dm-mpath.c: trigger_event - net/smc/smc_llc.c: llc_event_work - sound/soc/codecs/rt5651.c: jack_detect_work - sound/soc/codecs/rt5682s.c: jack_detect_work - sound/soc/soc-jack.c: snd_soc_jack_gpio.work - drivers/input/misc/palmas-pwrbutton.c: input_work - drivers/cpufreq/qcom-cpufreq-hw.c: throttle_work - drivers/vdpa/vdpa_user/vduse_dev.c: vduse_virtqueue.inject Thanks. -- tejun ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-25 3:25 UTC | newest] Thread overview: 5+ 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 2026-11-18 17:09 ` [PATCH 2/2] workqueue: doc: clarify cancel_work_sync() cross-workqueue behavior Alice Ryhl 2026-11-25 3:25 ` Tejun Heo
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox