Fix critical SMP scheduler issues with CPU selection and ready-to-run list management #18241
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses critical issues in the SMP scheduler's CPU selection and task management logic:
nxsched_select_cpu()preventing valid CPU selectionnxsched_remove_self()operationThese changes ensure correct task placement on SMP systems and proper scheduler behavior during task removal.
Motivation and Problem
Issue 1: Invalid CPU Selection Initialization
nxsched_select_cpu()initializescpu = CONFIG_SMP_NCPUS(invalid sentinel value)Issue 2: Lock-Check Preventing Optimal Scheduling
!nxsched_islocked_tcb(rtcb)prevented selection of CPU-locked tasksIssue 3: Redundant Condition in Ready-to-Run Addition
if (target_cpu < CONFIG_SMP_NCPUS)was unnecessary after CPU selectionnxsched_select_cpu()already guarantees valid CPU return (now with assertion)Issue 4: Incorrect Task Cleanup Sequencing
nxsched_switch_running()called innxsched_remove_self()after function returnnxsched_remove_running()for correct sequencingChanges Made
File: sched/sched/sched.h
Line 580: Changed CPU initialization from
CONFIG_SMP_NCPUSto0xff(invalid sentinel)Line 603-604: Removed lock check from CPU selection condition
else if (rtcb->sched_priority <= minprio && !nxsched_islocked_tcb(rtcb))else if (rtcb->sched_priority <= minprio)Line 613: Added
DEBUGASSERT(cpu != 0xff)before returnFile: sched/sched/sched_addreadytorun.c
Line 166: Added priority check to switch_equal condition
if (switch_equal)if (switch_equal && sched_priority > 0)Line 258: Moved
tcbdeclaration outside conditional blockFAR struct tcb_s *tcb = current_task(target_cpu);Lines 267-276: Removed
if (target_cpu < CONFIG_SMP_NCPUS)conditionalbtcb->cpu = target_cpu;assignment for CPU trackingif (tcb->sched_priority < btcb->sched_priority)Line 267: Added comment explaining CPU assignment
File: sched/sched/sched_process_delivered.c
if (target_cpu < CONFIG_SMP_NCPUS && target_cpu != cpu && ...)if (target_cpu != cpu && ...)File: sched/sched/sched_removereadytorun.c
nxsched_switch_running(tcb->cpu, false)callnxsched_remove_self()tonxsched_remove_running()Impact
Verification Checklist
Testing Scenarios
SMP Task Placement
Load Balancing
CPU-Locked Tasks
Task Removal
Edge Cases
Technical Notes
CPU Selection Algorithm:
Ready-to-Run Addition:
btcb->cpu = target_cpu) critical for affinity trackingTask Removal Sequencing:
nxsched_switch_running()must execute while task TCB validnxsched_remove_running()prevents post-exit accessBuild Information
Compiler: ARM GCC 10.x and later
Architectures: ARMv7-A, ARMv7-R, ARM64, ARM Cortex-M series
Configurations: CONFIG_SMP enabled (also compatible with single-CPU)
Flags:
-Wall -Wextra -Werror