From 112b8cf47099330366de1e4ecb2ef7f97753bc86 Mon Sep 17 00:00:00 2001 From: Ville Juven Date: Tue, 22 Oct 2024 16:06:38 +0300 Subject: [PATCH] sched/task_exit.c: Refresh current CPU instead of relying on stale value The comment about the CPU index remaining stable is incorrect. There is no guarantee the task does not yield during the exit process, meaning the CPU can most definitely change. Also, there is no reason why it should not be allowed to change. This fixes a full system crash during process exit when the CPU changes and we query the current task from the old CPU. --- sched/task/task_exit.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/sched/task/task_exit.c b/sched/task/task_exit.c index a4efb2bc839..b4bb1b84019 100644 --- a/sched/task/task_exit.c +++ b/sched/task/task_exit.c @@ -76,17 +76,11 @@ int nxtask_exit(void) FAR struct tcb_s *rtcb; int ret; #ifdef CONFIG_SMP - int cpu; - - /* Get the current CPU. By assumption, we are within a critical section - * and, hence, the CPU index will remain stable. - * - * Avoid using this_task() because it may assume a state that is not + /* Avoid using this_task() because it may assume a state that is not * appropriate for an exiting task. */ - cpu = this_cpu(); - dtcb = current_task(cpu); + dtcb = current_task(this_cpu()); #else dtcb = this_task(); #endif @@ -111,7 +105,7 @@ int nxtask_exit(void) /* Get the new task at the head of the ready to run list */ #ifdef CONFIG_SMP - rtcb = current_task(cpu); + rtcb = current_task(this_cpu()); #else rtcb = this_task(); #endif