Skip to content

Commit

Permalink
fix(freertos/smp): Prevent prvCheckForRunStateChange() in a nested cr…
Browse files Browse the repository at this point in the history
…itical section

This commit adds a check in vTaskSuspendAll() to avoid calling
prvCheckForRunStateChange() if we are in a nested critical section. This
can happen if we have aquired a lock in another data group (such as
queue) and then we take the kernel lock via the vTaskSuspendAll() call.
  • Loading branch information
sudeep-mohanty committed Oct 9, 2024
1 parent 3216174 commit d034956
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,15 +529,15 @@ PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ conf

/* Helper macros to lock (critical section) the kernel data group . */
#if ( ( portUSING_GRANULAR_LOCKS == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
#define taskLOCK_KERNEL_DATA_GROUP() \
portDISABLE_INTERRUPTS(); \
portGET_SPINLOCK( &xTaskSpinlock ); \
#define taskLOCK_KERNEL_DATA_GROUP() \
portDISABLE_INTERRUPTS(); \
portGET_SPINLOCK( &xTaskSpinlock ); \
if( ( xSchedulerRunning != pdFALSE ) && ( portGET_CRITICAL_NESTING_COUNT() == 0U ) ) \
{ \
prvCheckForRunStateChange(); \
} \
portINCREMENT_CRITICAL_NESTING_COUNT(); \
portGET_SPINLOCK( &xISRSpinlock ); \
{ \
prvCheckForRunStateChange(); \
} \
portINCREMENT_CRITICAL_NESTING_COUNT(); \
portGET_SPINLOCK( &xISRSpinlock ); \
portINCREMENT_CRITICAL_NESTING_COUNT();

#define taskUNLOCK_KERNEL_DATA_GROUP() \
Expand Down Expand Up @@ -4044,7 +4044,11 @@ void vTaskSuspendAll( void )
/* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
* purpose is to prevent altering the variable when fromISR APIs are readying
* it. */
if( uxSchedulerSuspended == 0U )
if( ( uxSchedulerSuspended == 0U )
#if ( portUSING_GRANULAR_LOCKS == 1 )
&& ( portGET_CRITICAL_NESTING_COUNT() == 0U )
#endif /* #if ( portUSING_GRANULAR_LOCKS == 1 ) */
)
{
prvCheckForRunStateChange();
}
Expand Down Expand Up @@ -4305,7 +4309,7 @@ BaseType_t xTaskResumeAll( void )
#if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
&& ( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == 0U )
#endif
)
)
{
#if ( configUSE_PREEMPTION != 0 )
{
Expand Down

0 comments on commit d034956

Please sign in to comment.