score: Fix context switch extensions (SMP)

In uniprocessor and SMP configurations, the context switch extensions
were called during _Thread_Do_dispatch():

void _Thread_Do_dispatch( Per_CPU_Control *cpu_self, ISR_Level level )
{
  Thread_Control *executing;

  executing = cpu_self->executing;

  ...
  do {
    Thread_Control *heir;

    heir = _Thread_Get_heir_and_make_it_executing( cpu_self );
    ...
    _User_extensions_Thread_switch( executing, heir );
    ...
    _Context_Switch( &executing->Registers, &heir->Registers );
    ...
  } while ( cpu_self->dispatch_necessary );
  ...
}

In uniprocessor configurations, this is fine and the context switch
extensions are called for all thread switches except the very first
thread switch to the initialization thread.  However, in SMP
configurations, the context switch may be invalidated and updated in the
low-level _Context_Switch() routine.  See:

  https://docs.rtems.org/branches/master/c-user/symmetric_multiprocessing_services.html#thread-dispatch-details

In case such an update happens, a thread will execute on the processor
which was not seen in the previous call of the context switch
extensions.  This can confuse for example event record consumers which
use events generated by a context switch extension.

Fixing this is not straight forward.  The context switch extensions call
must move after the low-level context switch.  The problem here is that
we may end up in _Thread_Handler().  Adding the context switch
extensions call to _Thread_Handler() covers now also the thread switch
to the initialization thread.  We also have to save the last executing
thread (ancestor) of the processor.  Registers or the stack cannot be
used for this purpose.  We have to add it to the per-processor
information.  Existing extensions may be affected, since now context
switch extensions use the stack of the heir thread.  The stack checker
is affected by this.

Calling the thread switch extensions in the low-level context switch is
difficult since at this point an intermediate stack is used which is
only large enough to enable servicing of interrupts.

Update #3885.
This commit is contained in:
Sebastian Huber
2020-02-28 07:50:19 +01:00
parent 2220a536e3
commit fcb11510c6
8 changed files with 78 additions and 3 deletions
+7
View File
@@ -528,6 +528,13 @@ typedef struct Per_CPU_Control {
struct _Thread_Control *idle_if_online_and_unused;
} Scheduler;
/**
* @brief The ancestor of the executing thread.
*
* This member is used by _User_extensions_Thread_switch().
*/
struct _Thread_Control *ancestor;
/**
* @brief Begin of the per-CPU data area.
*
+11
View File
@@ -386,7 +386,16 @@ static inline void _User_extensions_Thread_switch(
_ISR_lock_ISR_disable( &lock_context );
_Per_CPU_Acquire( cpu_self, &lock_context );
executing = cpu_self->ancestor;
cpu_self->ancestor = heir;
node = _Chain_Immutable_first( chain );
/*
* An executing thread equal to the heir thread may happen in two
* situations. Firstly, in case context switch extensions are created after
* system initialization. Secondly, during a thread self restart.
*/
if ( executing != heir ) {
#endif
while ( node != tail ) {
@@ -398,6 +407,8 @@ static inline void _User_extensions_Thread_switch(
}
#if defined(RTEMS_SMP)
}
_Per_CPU_Release( cpu_self, &lock_context );
_ISR_lock_ISR_enable( &lock_context );
#endif
+23 -3
View File
@@ -295,8 +295,8 @@ static void Stack_check_report_blown_task(
* rtems_stack_checker_switch_extension
*/
void rtems_stack_checker_switch_extension(
Thread_Control *running RTEMS_UNUSED,
Thread_Control *heir RTEMS_UNUSED
Thread_Control *running,
Thread_Control *heir
)
{
bool sp_ok;
@@ -306,6 +306,22 @@ void rtems_stack_checker_switch_extension(
/*
* Check for an out of bounds stack pointer or an overwrite
*/
#if defined(RTEMS_SMP)
sp_ok = Stack_check_Frame_pointer_in_range( heir );
if ( !sp_ok ) {
pattern_ok = Stack_check_Is_sanity_pattern_valid(
&heir->Start.Initial_stack
);
Stack_check_report_blown_task( heir, pattern_ok );
}
pattern_ok = Stack_check_Is_sanity_pattern_valid( &running->Start.Initial_stack );
if ( !pattern_ok ) {
Stack_check_report_blown_task( running, pattern_ok );
}
#else
sp_ok = Stack_check_Frame_pointer_in_range( running );
pattern_ok = Stack_check_Is_sanity_pattern_valid( &running->Start.Initial_stack );
@@ -313,6 +329,7 @@ void rtems_stack_checker_switch_extension(
if ( !sp_ok || !pattern_ok ) {
Stack_check_report_blown_task( running, pattern_ok );
}
#endif
stack = &Stack_check_Interrupt_stack[ _SMP_Get_current_processor() ];
@@ -329,7 +346,10 @@ void rtems_stack_checker_switch_extension(
*/
bool rtems_stack_checker_is_blown( void )
{
rtems_stack_checker_switch_extension( _Thread_Get_executing(), NULL );
Thread_Control *executing;
executing = _Thread_Get_executing();
rtems_stack_checker_switch_extension( executing, executing );
/*
* The Stack Pointer and the Pattern Area are OK so return false.
+3
View File
@@ -75,6 +75,9 @@ static void _Thread_Create_idle_for_CPU( Per_CPU_Control *cpu )
*/
cpu->heir =
cpu->executing = idle;
#if defined(RTEMS_SMP)
cpu->ancestor = idle;
#endif
idle->is_idle = true;
idle->Start.Entry.adaptor = _Thread_Entry_adaptor_idle;
+5
View File
@@ -297,10 +297,15 @@ void _Thread_Do_dispatch( Per_CPU_Control *cpu_self, ISR_Level level )
_ISR_Local_enable( level );
#if !defined(RTEMS_SMP)
_User_extensions_Thread_switch( executing, heir );
#endif
_Thread_Save_fp( executing );
_Context_Switch( &executing->Registers, &heir->Registers );
_Thread_Restore_fp( executing );
#if defined(RTEMS_SMP)
_User_extensions_Thread_switch( NULL, executing );
#endif
/*
* We have to obtain this value again after the context switch since the
+4
View File
@@ -97,6 +97,10 @@ void _Thread_Handler( void )
*/
_Thread_Restore_fp( executing );
#if defined(RTEMS_SMP)
_User_extensions_Thread_switch( NULL, executing );
#endif
/*
* Do not use the level of the thread control block, since it has a
* different format.
+21
View File
@@ -20,8 +20,28 @@
#endif
#include <rtems/score/userextimpl.h>
#include <rtems/score/smp.h>
#include <rtems/score/percpu.h>
static void _User_extensions_Set_ancestors( void )
{
#if defined(RTEMS_SMP)
if ( _Chain_Is_empty( &_User_extensions_Switches_list ) ) {
uint32_t cpu_max;
uint32_t cpu_index;
cpu_max = _SMP_Get_processor_maximum();
for ( cpu_index = 0 ; cpu_index < cpu_max ; ++cpu_index ) {
Per_CPU_Control *cpu;
cpu = _Per_CPU_Get_by_index( cpu_index );
cpu->ancestor = cpu->executing;
}
}
#endif
}
void _User_extensions_Add_set(
User_extensions_Control *the_extension
)
@@ -45,6 +65,7 @@ void _User_extensions_Add_set(
the_extension->Callouts.thread_switch;
_Per_CPU_Acquire_all( &lock_context );
_User_extensions_Set_ancestors();
_Chain_Initialize_node( &the_extension->Switch.Node );
_Chain_Append_unprotected(
&_User_extensions_Switches_list,
+4
View File
@@ -453,8 +453,12 @@ static void test(void)
#endif
active_extensions = 4;
#ifdef RTEMS_SMP
assert(counter == 12);
#else
assert(counter == 10);
counter = 12;
#endif
sc = rtems_task_create(
rtems_build_name('W', 'O', 'R', 'K'),