[PATCH] cpukit/smp: Prevent premature thread dispatch

Kinsey Moore kinsey.moore at oarcorp.com
Thu Sep 16 20:50:18 UTC 2021


There is currently a narrow window between the CPU state being set to UP
and the dispatch disable flag being set. It is possible to perform a
cross-processor thread dispatch in this window which catches the CPU in
a state which is not actually fully ready for that type of thread
dispatch.

This moves the CPU state change to just before the CPU's first thread
dispatch and later than the dispatch disable flag change which closes
the window for the race condition.
---
 cpukit/score/src/threadstartmultitasking.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/cpukit/score/src/threadstartmultitasking.c b/cpukit/score/src/threadstartmultitasking.c
index 094a535394..0c0951a243 100644
--- a/cpukit/score/src/threadstartmultitasking.c
+++ b/cpukit/score/src/threadstartmultitasking.c
@@ -29,8 +29,6 @@ void _Thread_Start_multitasking( void )
   Thread_Control  *heir;
 
 #if defined(RTEMS_SMP)
-  _Per_CPU_State_change( cpu_self, PER_CPU_STATE_UP );
-
   /*
    * Threads begin execution in the _Thread_Handler() function.   This
    * function will set the thread dispatch disable level to zero.
@@ -44,6 +42,8 @@ void _Thread_Start_multitasking( void )
 
 #if defined(RTEMS_SMP)
   _CPU_SMP_Prepare_start_multitasking();
+
+  _Per_CPU_State_change( cpu_self, PER_CPU_STATE_UP );
 #endif
 
 #if defined(_CPU_Start_multitasking)
-- 
2.30.2



More information about the devel mailing list