From 79d1e58b5c2f4ba62e37c26f9a68d60a8ecfeee1 Mon Sep 17 00:00:00 2001 From: seteiro <276597748+cms19859230182-lang@users.noreply.github.com> Date: Fri, 27 Nov 2026 13:16:16 +0800 Subject: [PATCH 1/3] [drivers][workqueue] stop delayed timer when resubmitting with 0 Fix #8477 When a delayed work item (submitted with ticks > 0) is resubmitted with ticks == 0 before its delay expires, the delayed state must be cleared and the item detached from the previous delayed list before it is inserted into the immediate work list. Serialize the ownership hand-off of a work item with a module spinlock that is always taken before any queue->spinlock: _workqueue_submit_work(), rt_workqueue_urgent_work() and _workqueue_cancel_work(). Two threads may both see an idle (or stale) owner and then each take only their own queue lock, so one of them would unlink the node from the other queue's list. The owner is now snapshot once, re-checked under that queue's lock before the node is detached, and only one queue lock is held at a time, so two queues cannot deadlock on each other. rt_workqueue_urgent_work() also records work->workqueue = queue. A work item owned by another queue can no longer be unlinked from here: rt_workqueue_cancel_work() reports -RT_EBUSY for it, and rt_workqueue_cancel_work_sync() passes that result on instead of returning RT_EOK. Reporting success made callers (aio_cancel(), rt_work_cancel() users) believe the callback could no longer run while it still could. The synchronous variant still waits for the callback when the item is executed by its own queue. --- components/drivers/ipc/workqueue.c | 140 +++++++++++++++++++++++++---- 1 file changed, 122 insertions(+), 18 deletions(-) diff --git a/components/drivers/ipc/workqueue.c b/components/drivers/ipc/workqueue.c index 30b779e4dfc5..a86b8ad6ea64 100644 --- a/components/drivers/ipc/workqueue.c +++ b/components/drivers/ipc/workqueue.c @@ -112,15 +112,48 @@ static void _workqueue_thread_entry(void *parameter) } } +/* lock order: _workqueue_owner_lock first, then at most one queue->spinlock */ +static RT_DEFINE_SPINLOCK(_workqueue_owner_lock); + +static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work); +/* same as _workqueue_cancel_work() but queue->spinlock must be held */ +static rt_err_t _workqueue_cancel_work_locked(struct rt_workqueue *queue, struct rt_work *work); +static rt_err_t _workqueue_cancel_work_ex(struct rt_workqueue *queue, struct rt_work *work, + rt_bool_t *migrated); + static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t ticks) { rt_base_t level; + rt_base_t owner_level; rt_err_t err = RT_EOK; struct rt_work *work_tmp; rt_list_t *list_tmp; + struct rt_workqueue *owner; - level = rt_spin_lock_irqsave(&(queue->spinlock)); + owner_level = rt_spin_lock_irqsave(&_workqueue_owner_lock); + + for (;;) + { + owner = work->workqueue; + if (owner != RT_NULL && owner != queue) + { + level = rt_spin_lock_irqsave(&(owner->spinlock)); + if (work->workqueue == owner) + { + _workqueue_cancel_work_locked(owner, work); + } + rt_spin_unlock_irqrestore(&(owner->spinlock), level); + } + + level = rt_spin_lock_irqsave(&(queue->spinlock)); + if (work->workqueue == RT_NULL || work->workqueue == queue) + { + break; + } + + rt_spin_unlock_irqrestore(&(queue->spinlock), level); + } /* remove list */ rt_list_remove(&(work->list)); work->flags = 0; @@ -162,23 +195,62 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, err = -RT_ERROR; } rt_spin_unlock_irqrestore(&(queue->spinlock), level); + rt_spin_unlock_irqrestore(&_workqueue_owner_lock, owner_level); + return err; } -static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) +static rt_err_t _workqueue_cancel_work_locked(struct rt_workqueue *queue, struct rt_work *work) { - rt_base_t level; - rt_err_t err; + rt_err_t err; - level = rt_spin_lock_irqsave(&(queue->spinlock)); rt_list_remove(&(work->list)); - work->flags = 0; - err = queue->work_current != work ? RT_EOK : -RT_EBUSY; + work->flags = 0; + err = queue->work_current != work ? RT_EOK : -RT_EBUSY; work->workqueue = RT_NULL; + return err; +} + +static rt_err_t _workqueue_cancel_work_ex(struct rt_workqueue *queue, struct rt_work *work, + rt_bool_t *migrated) +{ + rt_base_t level; + rt_base_t owner_level; + rt_err_t err; + + if (migrated != RT_NULL) + { + *migrated = RT_FALSE; + } + + owner_level = rt_spin_lock_irqsave(&_workqueue_owner_lock); + level = rt_spin_lock_irqsave(&(queue->spinlock)); + + if ((work->workqueue != RT_NULL) && (work->workqueue != queue)) + { + /* the list node belongs to another queue, its lock is not held here */ + err = -RT_EBUSY; + if (migrated != RT_NULL) + { + *migrated = RT_TRUE; + } + } + else + { + err = _workqueue_cancel_work_locked(queue, work); + } + rt_spin_unlock_irqrestore(&(queue->spinlock), level); + rt_spin_unlock_irqrestore(&_workqueue_owner_lock, owner_level); + return err; } +static rt_err_t _workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) +{ + return _workqueue_cancel_work_ex(queue, work, RT_NULL); +} + /** * @brief Initialize a work item, binding with a callback function. * @@ -315,17 +387,34 @@ rt_err_t rt_workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *wo rt_err_t rt_workqueue_urgent_work(struct rt_workqueue *queue, struct rt_work *work) { rt_base_t level; + rt_base_t owner_level; + struct rt_workqueue *owner; RT_ASSERT(queue != RT_NULL); RT_ASSERT(work != RT_NULL); + owner_level = rt_spin_lock_irqsave(&_workqueue_owner_lock); + + owner = work->workqueue; + if (owner != RT_NULL && owner != queue) + { + level = rt_spin_lock_irqsave(&(owner->spinlock)); + if (work->workqueue == owner) + { + _workqueue_cancel_work_locked(owner, work); + } + rt_spin_unlock_irqrestore(&(owner->spinlock), level); + } + level = rt_spin_lock_irqsave(&(queue->spinlock)); /* NOTE: the work MUST be initialized firstly */ rt_list_remove(&(work->list)); rt_list_insert_after(&queue->work_list, &(work->list)); + work->workqueue = queue; rt_completion_done(&(queue->wakeup_completion)); rt_spin_unlock_irqrestore(&(queue->spinlock), level); + rt_spin_unlock_irqrestore(&_workqueue_owner_lock, owner_level); return RT_EOK; } @@ -338,7 +427,8 @@ rt_err_t rt_workqueue_urgent_work(struct rt_workqueue *queue, struct rt_work *wo * @param work is a pointer to the work item object. * * @return RT_EOK Success. - * -RT_EBUSY This work item is executing. + * -RT_EBUSY This work item is executing, or it is owned by another + * work queue and was therefore not cancelled. */ rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *work) { @@ -356,24 +446,38 @@ rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *wo * @param work is a pointer to the work item object. * * @return RT_EOK Success. + * -RT_EBUSY This work item is owned by another work queue, so it was + * not cancelled and may still run. */ rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work) { + rt_err_t err; + rt_bool_t migrated; + RT_ASSERT(queue != RT_NULL); RT_ASSERT(work != RT_NULL); - if (queue->work_current == work) /* it's current work in the queue */ - { - /* wait for work completion */ - rt_sem_take(&(queue->sem), RT_WAITING_FOREVER); - /* Note that because work items are automatically deleted after execution, they do not need to be deleted again */ - } - else + for (;;) { - _workqueue_cancel_work(queue, work); - } + if (queue->work_current == work) /* it's current work in the queue */ + { + /* wait for work completion */ + rt_sem_take(&(queue->sem), RT_WAITING_FOREVER); + /* Note that because work items are automatically deleted after execution, they do not need to be deleted again */ + return RT_EOK; + } - return RT_EOK; + err = _workqueue_cancel_work_ex(queue, work, &migrated); + if (err != -RT_EBUSY) + { + return err; + } + + if (migrated) + { + return -RT_EBUSY; + } + } } /** From aecd0f6a26ea117d50cc078c8e04de24ec1c0daa Mon Sep 17 00:00:00 2001 From: seteiro Date: Wed, 2 Dec 2026 09:14:14 +0000 Subject: [PATCH 2/3] =?UTF-8?q?ci-bot:=20#12059=20round=201/3:=20=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=20workqueue.c=20=E5=A3=B0=E6=98=8E=E5=AF=B9=E9=BD=90?= =?UTF-8?q?=E4=BB=A5=E8=BF=87=20clang-format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 去掉 owner_level 前多余空格,满足 clang-format 对改动行的要求。 纯格式调整,无逻辑变化。 --- components/drivers/ipc/workqueue.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/drivers/ipc/workqueue.c b/components/drivers/ipc/workqueue.c index a86b8ad6ea64..94c08f0688df 100644 --- a/components/drivers/ipc/workqueue.c +++ b/components/drivers/ipc/workqueue.c @@ -125,7 +125,7 @@ static rt_err_t _workqueue_submit_work(struct rt_workqueue *queue, struct rt_work *work, rt_tick_t ticks) { rt_base_t level; - rt_base_t owner_level; + rt_base_t owner_level; rt_err_t err = RT_EOK; struct rt_work *work_tmp; rt_list_t *list_tmp; From 401e91621d0f1b9ecf19a4942ad4d4c678e5b051 Mon Sep 17 00:00:00 2001 From: seteiro Date: Thu, 3 Dec 2026 16:41:17 +0800 Subject: [PATCH 3/3] [drivers][workqueue] report -RT_EBUSY when cancel_sync finds the item handed over A work item's callback may submit the same item to another queue while the callback is still running. rt_workqueue_cancel_work_sync() waited for that callback and then reported RT_EOK without checking the owner again, so the caller believed the cancellation succeeded while the item was still queued (or running) on the other queue. After waiting, read the owner under _workqueue_owner_lock and report -RT_EBUSY when the item belongs to another queue. An item that is free or still belongs to this queue keeps returning RT_EOK, so an item re-queued on its own queue is not unlinked behind the callback's back. --- components/drivers/ipc/workqueue.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/components/drivers/ipc/workqueue.c b/components/drivers/ipc/workqueue.c index 94c08f0688df..f2c1cf2ac496 100644 --- a/components/drivers/ipc/workqueue.c +++ b/components/drivers/ipc/workqueue.c @@ -451,6 +451,7 @@ rt_err_t rt_workqueue_cancel_work(struct rt_workqueue *queue, struct rt_work *wo */ rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_work *work) { + rt_base_t owner_level; rt_err_t err; rt_bool_t migrated; @@ -463,8 +464,13 @@ rt_err_t rt_workqueue_cancel_work_sync(struct rt_workqueue *queue, struct rt_wor { /* wait for work completion */ rt_sem_take(&(queue->sem), RT_WAITING_FOREVER); + /* Note that because work items are automatically deleted after execution, they do not need to be deleted again */ - return RT_EOK; + owner_level = rt_spin_lock_irqsave(&_workqueue_owner_lock); + migrated = (work->workqueue != RT_NULL) && (work->workqueue != queue); + rt_spin_unlock_irqrestore(&_workqueue_owner_lock, owner_level); + + return migrated ? -RT_EBUSY : RT_EOK; } err = _workqueue_cancel_work_ex(queue, work, &migrated);