Skip to content

schedule: use k_thread_cpu_pin() for CPU affinity#10953

Open
jsarha wants to merge 1 commit into
thesofproject:mainfrom
jsarha:schedule_pin_only_fix
Open

schedule: use k_thread_cpu_pin() for CPU affinity#10953
jsarha wants to merge 1 commit into
thesofproject:mainfrom
jsarha:schedule_pin_only_fix

Conversation

@jsarha

@jsarha jsarha commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Replace the k_thread_cpu_mask_clear() + k_thread_cpu_mask_enable() sequence with a single k_thread_cpu_pin() call.

The clear-then-enable pattern momentarily leaves the thread with a zero CPU mask, which is invalid under CONFIG_SCHED_CPU_MASK_PIN_ONLY and triggers an assertion after Zephyr commit 7798570a031d ("kernel: sched: enforce non-zero CPU mask invariant in PIN_ONLY mode").

k_thread_cpu_pin() atomically sets exactly one bit in the mask, avoiding the transient zero-mask state.

This PR together with zephyrproject-rtos/zephyr#111955 should make SOF boot again with latest Zephyr and asserts enabled.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates SOF’s Zephyr scheduling-related thread affinity setup to avoid a transient zero CPU mask state that can assert under Zephyr’s CONFIG_SCHED_CPU_MASK_PIN_ONLY invariant enforcement, by switching from k_thread_cpu_mask_clear() + k_thread_cpu_mask_enable() to k_thread_cpu_pin().

Changes:

  • Replace clear+enable CPU-mask sequences with k_thread_cpu_pin() in EDF workqueue init and LL/DMA domain thread creation.
  • (Intended) Eliminate transient zero-mask state that can trigger Zephyr assertions with newer scheduler checks.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
zephyr/edf_schedule.c Pins the EDF workqueue thread using k_thread_cpu_pin() instead of clear+enable.
src/schedule/zephyr_domain.c Pins the LL domain thread using k_thread_cpu_pin() in the kernel-space path.
src/schedule/zephyr_dma_domain.c Pins the DMA domain thread using k_thread_cpu_pin() instead of clear+enable.

Comment thread zephyr/edf_schedule.c
Comment on lines 116 to 118
#ifdef CONFIG_SCHED_CPU_MASK
k_thread_cpu_mask_clear(thread);
k_thread_cpu_mask_enable(thread, PLATFORM_PRIMARY_CORE_ID);
k_thread_cpu_pin(thread, PLATFORM_PRIMARY_CORE_ID);
#endif

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That is annother fix, not the one this PR is fixing. And the only reason that may cause k_thread_cpu_pin() to fail is that thread is already running, which is obviously not the case here, so check is quite futile.

Comment on lines 220 to 222
#ifdef CONFIG_SCHED_CPU_MASK
k_thread_cpu_mask_clear(thread);
k_thread_cpu_mask_enable(thread, core);
k_thread_cpu_pin(thread, core);
#endif

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

see above

Comment on lines 220 to 222
#ifdef CONFIG_SCHED_CPU_MASK
k_thread_cpu_mask_clear(thread);
k_thread_cpu_mask_enable(thread, core);
k_thread_cpu_pin(thread, core);
#endif

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 464 to 466
#ifdef CONFIG_SCHED_CPU_MASK
k_thread_cpu_mask_clear(thread);
k_thread_cpu_mask_enable(thread, core);
k_thread_cpu_pin(thread, core);
#endif

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

see above

Replace the k_thread_cpu_mask_clear() + k_thread_cpu_mask_enable()
sequence with a single k_thread_cpu_pin() call.

The clear-then-enable pattern momentarily leaves the thread with a
zero CPU mask, which is invalid under CONFIG_SCHED_CPU_MASK_PIN_ONLY
and triggers an assertion after Zephyr commit 7798570a031d ("kernel:
sched: enforce non-zero CPU mask invariant in PIN_ONLY mode").

k_thread_cpu_pin() atomically sets exactly one bit in the mask,
avoiding the transient zero-mask state.

Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
@jsarha jsarha force-pushed the schedule_pin_only_fix branch from 1801dbe to 2779791 Compare June 24, 2026 15:14
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.

3 participants