Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update task notification scheduler suspension usage #982

Conversation

Dazza0
Copy link
Contributor

@Dazza0 Dazza0 commented Feb 5, 2024

Description

#612 fixed an issue with ulTaskGenericNotifyTake() and xTaskGenericNotifyWait() being non deterministic by ensuring that those functions call prvAddCurrentTaskToDelayedList() while the scheduler was suspended. The merged fix would suspend the scheduler while still in a critical section, then exit the critical section while the scheduler was suspended:

taskENTER_CRITICAL();
vTaskSuspendAll();
taskEXIT_CRITICAL();
prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
xTaskResumeAll();

The method above technically works but is a bit unintuitive due to interleaving critical sections and scheduler suspension.

This PR updates the behavior described above by wrapping the critical section in a scheduler suspension block:

vTaskSuspendAll();
taskENTER_CRITICAL();
// Check if block required
taskEXIT_CRITICAL();
if (blocking) {
    prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
}
xTaskResumeAll();

This method is more intuitive. As an added bonus, it also allows the SMP implementation of vTaskSuspendAll() to be simplified (as we can assume the vTaskSuspendAll() is now never called in a critical section). As such, an extra assert has been added to vTaskSuspendAll() to ensure that it is never called in a critical section.

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@Dazza0 Dazza0 requested a review from a team as a code owner February 5, 2024 07:32
@Dazza0 Dazza0 force-pushed the change/ulTaskGenericNotifyTake_scheduler_suspension branch 2 times, most recently from 371657e to 0fe4f0f Compare February 5, 2024 15:32
Dazza0 added a commit to Dazza0/FreeRTOS that referenced this pull request Feb 5, 2024
This commit updates the CMock unit tests according to the changes introduced
to FreeRTOS/FreeRTOS-Kernel#982.
Previously ulTaskGenericNotifyTake() and xTaskGenericNotifyWait() would suspend
the scheduler while inside a critical section.

This commit changes the order by wrapping the critical sections in a scheduler
suspension block. This logic is more inuitive and allows the SMP scheduler
suspension logic to be simplified.
@Dazza0 Dazza0 force-pushed the change/ulTaskGenericNotifyTake_scheduler_suspension branch from 0fe4f0f to 8cd19b4 Compare February 5, 2024 16:51
@Dazza0
Copy link
Contributor Author

Dazza0 commented Feb 5, 2024

@chinglee-iot I've updated the CMock unit tests as well. PTAL.

paulbartell
paulbartell previously approved these changes Feb 5, 2024
tasks.c Outdated
Comment on lines 3811 to 3812
/* This must never be called from inside a critical section */
configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aggarg @RichardBarry

Is this change alright? I know some ports such as the ARM_CRx_No_GIC push the critical nesting count as part of the task context:

    /* Push the critical nesting count. */
    LDR     R2, ulCriticalNestingConst
    LDR     R1, [R2]
    PUSH    {R1}

I'm worried that this change might be a breaking change for some ports/applications and was hoping for a deeper look into this PR from you two

Copy link
Member

@chinglee-iot chinglee-iot Feb 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using portGET_CRITICAL_NESTING_COUNT() here is okay to me. Sharing my observation here.
Critical nesting count should only be modified in critical section and context switch inside a critical section is not allowed in SMP. Use the following scenarios for discussion:

  1. portGET_CRITICAL_NESTING_COUNT() is greater than 1. This task is in a critical section and can read or update the critical nesting count without problem.
  2. portGET_CRITICAL_NESTING_COUNT() is 0. The task A may get preempted by task B while reading critical nesting count. Critical nesting count will be restore to 0 when the task B left critical section.

chinglee-iot
chinglee-iot previously approved these changes Feb 6, 2024
tasks.c Outdated Show resolved Hide resolved
tasks.c Outdated Show resolved Hide resolved
tasks.c Outdated Show resolved Hide resolved
@chinglee-iot chinglee-iot dismissed stale reviews from paulbartell and themself via 07cb29d February 6, 2024 13:11
chinglee-iot
chinglee-iot previously approved these changes Feb 6, 2024
aggarg
aggarg previously approved these changes Feb 6, 2024
chinglee-iot added a commit to FreeRTOS/FreeRTOS that referenced this pull request Feb 7, 2024
This commit updates the CMock unit tests according to the changes introduced
to FreeRTOS/FreeRTOS-Kernel#982.

Co-authored-by: chinglee-iot <[email protected]>
Co-authored-by: Rahul Kar <[email protected]>
* Prevent potential NULL pointer access when scheduler is not running
@chinglee-iot chinglee-iot dismissed stale reviews from aggarg and themself via 63c8d73 February 7, 2024 04:40
Copy link

sonarcloud bot commented Feb 7, 2024

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

2 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

codecov bot commented Feb 7, 2024

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (57a5ed7) 93.55% compared to head (63c8d73) 93.52%.

Files Patch % Lines
tasks.c 89.65% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #982      +/-   ##
==========================================
- Coverage   93.55%   93.52%   -0.03%     
==========================================
  Files           6        6              
  Lines        3197     3199       +2     
  Branches      887      889       +2     
==========================================
+ Hits         2991     2992       +1     
  Misses         92       92              
- Partials      114      115       +1     
Flag Coverage Δ
unittests 93.52% <89.65%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@chinglee-iot chinglee-iot merged commit 7284d84 into FreeRTOS:main Feb 7, 2024
16 of 18 checks passed
Dazza0 added a commit to Dazza0/FreeRTOS-Kernel that referenced this pull request Mar 4, 2024
* Update task notification scheduler suspension

Previously ulTaskGenericNotifyTake() and xTaskGenericNotifyWait() would suspend
the scheduler while inside a critical section.

This commit changes the order by wrapping the critical sections in a scheduler
suspension block. This logic is more inuitive and allows the SMP scheduler
suspension logic to be simplified.

* tasks.c: Fix typo

* Use a complete sentence in comment

* Check portGET_CRITICAL_NESTING_COUNT when scheduler is running

* Prevent potential NULL pointer access when scheduler is not running

---------

Co-authored-by: Paul Bartell <[email protected]>
Co-authored-by: chinglee-iot <[email protected]>
Co-authored-by: Ching-Hsin Lee <[email protected]>
laroche pushed a commit to laroche/FreeRTOS-Kernel that referenced this pull request Apr 18, 2024
* Add reg tests for CORTEX_MPU_M3_NUCLEO_L152RE

Signed-off-by: Gaurav Aggarwal <[email protected]>
Co-authored-by: kar-rahul-aws <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants