Skip to content

sched/setpriority: handle CPU affinity change for running task - #19687

Open
hujun260 wants to merge 1 commit into
apache:masterfrom
hujun260:apache_master_20260612
Open

sched/setpriority: handle CPU affinity change for running task#19687
hujun260 wants to merge 1 commit into
apache:masterfrom
hujun260:apache_master_20260612

Conversation

@hujun260

@hujun260 hujun260 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Summary

When a running task changes its CPU affinity on SMP and the new mask no longer includes its current CPU, nxsched_set_priority() requests an equal-priority reschedule on the current CPU. The task can be moved to g_readytorun without waking the idle target CPU, leaving it unable to run again.

Remove the task from its current CPU, add it back through nxsched_add_readytorun() so a suitable CPU is selected, and switch context unconditionally when the running task is no longer eligible for its current CPU.

Fixes #19680.

Impact

  • Existing SMP task-affinity behavior is corrected; no new feature or API is added.
  • No user adaptation is required.
  • No impact on the build process, hardware interfaces, documentation, security, or compatibility.
  • Non-SMP scheduling behavior is unchanged.

Testing

Build host: Ubuntu Linux, x86-64, GCC.

Target: QEMU ARM64, qemu-armv8a:nsh_smp, two Cortex-A53 CPUs.

Build command:

make distclean -j20
./tools/configure.sh -l qemu-armv8a:nsh_smp
make -j20

QEMU command:

qemu-system-aarch64 -cpu cortex-a53 -smp 2 -nographic \
  -machine virt,virtualization=on,gic-version=3 -net none \
  -chardev stdio,id=con,mux=on -serial chardev:con \
  -mon chardev=con,mode=readline -kernel ./nuttx

A local reproducer starts two equal-priority SCHED_RR threads on CPU1. T0 changes its affinity to CPU0 while CPU0 is idle and T1 remains runnable on CPU1.

Before this change, T0 never resumes and the test stops after:

orphan: T0 started on CPU1
orphan: T1 started on CPU1
orphan: both tasks are ready; releasing T0
orphan: T0 changes affinity CPU1 -> CPU0

After this change, T0 resumes on CPU0:

orphan: T0 started on CPU1
orphan: T1 started on CPU1
orphan: both tasks are ready; releasing T0
orphan: T0 changes affinity CPU1 -> CPU0
orphan: sched_setaffinity ret=0, T0 resumed on CPU0
orphan: T0 resumed on CPU0 (affinity ret=0)

Code and commit checks:

./tools/checkpatch.sh -g HEAD~1..HEAD
✔️ All checks pass.

PR verification Self-Check

  • This PR introduces only one functional change.
  • I have updated all required description fields above.
  • My PR adheres to the Contributing Guidelines and coding standard.
  • My PR is still work in progress (not ready for review).
  • My PR is ready for review and can be safely merged into the codebase.

When a running task changes its affinity on SMP and is no longer eligible to run on the current CPU, merely delivering an equal-priority scheduling request can leave the task in g_readytorun while the target CPU remains idle. The task then never runs again.

Remove the task from its current CPU, add it back to the ready-to-run list so a suitable CPU is selected, and perform the context switch unconditionally. This ensures the task is migrated according to its updated affinity.

Fixes: apache#19680

Assisted-by: GitHub Copilot:ppio/pa/gpt-5.6-sol

Signed-off-by: hujun5 <hujun5@xiaomi.com>
@hujun260
hujun260 requested a review from pkarashchenko as a code owner August 5, 2026 05:33
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: S The size of the change in this PR is small labels Aug 5, 2026
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] normal task became an orphan task when enable SMP after adjust task affinity (nsh64_smp)

3 participants