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 --- 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 --- Changes in v2: - Drop patch 2 ("workqueue: doc: clarify cancel_work_sync() cross-workqueue behavior"), already applied to wq/for-7.5 by Tejun Heo. - Use list_entry_rcu(pwq->wq->pwqs.next, ...)->pool in try_to_grab_pending() without the redundant NULL check, per Tejun Heo. - Link to v1: https://lore.kernel.org/r/20261118-ordered-wq-cancel-v1-0-916e01f9affe@google.com --- kernel/workqueue.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/kernel/workqueue.c b/kernel/workqueue.c index af845214e78b..9566e8822686 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,18 @@ 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)) + offq_pool = list_entry_rcu(pwq->wq->pwqs.next, struct pool_workqueue, + pwqs_node)->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); --- base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3 change-id: 20261118-ordered-wq-cancel-63b02e0feac4 Best regards, -- Alice Ryhl