From 7a6cd1dc4d814e2f99bf75ac16a9fb93d15bb7e4 Mon Sep 17 00:00:00 2001 From: Kai Vehmanen Date: Wed, 20 Oct 2021 15:21:30 +0300 Subject: [PATCH] zephyr: ll-schedule: fix a race between task cancel and new schedule As task cancellation is an asynchronous operation, it is possible that the task is in SOF_TASK_STATE_CANCEL state when zephyr_ll_task_schedule() is called. With current code, the cancel takes precedence and task is not scheduled again. This shows up as errors in SOF audio test case for multiple-pause-resume where a set of pipelines is repeatedly paused and unpaused. If the race is hit, a request to pause is successful (and pipeline task is requested to be cancelled) but the subsequent release will not get schedueld at all leading to an IPC timeout. BugLink: https://github.com/thesofproject/sof/issues/4742 Signed-off-by: Kai Vehmanen --- src/schedule/zephyr_ll.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/schedule/zephyr_ll.c b/src/schedule/zephyr_ll.c index 59258d397cd1..f68bb8ad590a 100644 --- a/src/schedule/zephyr_ll.c +++ b/src/schedule/zephyr_ll.c @@ -316,11 +316,15 @@ static int zephyr_ll_task_schedule(void *data, struct task *task, uint64_t start list_for_item(list, &sch->tasks) { task_iter = container_of(list, struct task, list); - /* - * keep original start. TODO: this shouldn't be happening. - * Remove after verification - */ if (task_iter == task) { + /* if cancelled, reschedule the task */ + if (task->state == SOF_TASK_STATE_CANCEL) + break; + + /* + * keep original start. TODO: this shouldn't be happening. + * Remove after verification + */ spin_unlock_irq(&sch->lock, flags); tr_warn(&ll_tr, "task %p (%pU) already scheduled", task, task->uid); @@ -333,6 +337,13 @@ static int zephyr_ll_task_schedule(void *data, struct task *task, uint64_t start sch->ll_domain->next_tick > task->start) task->start = sch->ll_domain->next_tick; + if (task->state == SOF_TASK_STATE_CANCEL) { + /* do not queue the same task again */ + task->state = SOF_TASK_STATE_QUEUED; + spin_unlock_irq(&sch->lock, flags); + return 0; + } + zephyr_ll_task_insert_unlocked(sch, task); sch->n_tasks++;