Skip to content

sched: split tcb->flags by writer - #21804

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
zyfeier:sched-atomic-locks
Nov 25, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
zyfeier:sched-atomic-locks

Conversation

@zyfeier

@zyfeier zyfeier commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Why. The flags of one task are written from more than one CPU. nxsched_suspend(), nxsig_queue_action() and nxtask_restart() set TCB_FLAG_CPU_LOCKED on a task that runs on another CPU, and sched_setscheduler() changes the scheduling policy of any task, while that task changes its own bits without the critical section, for example TCB_FLAG_SYSCALL on system call entry and exit. A plain read-modify-write of tcb->flags can then lose one of the two updates.

An earlier version of this PR made the whole word atomic. That put atomic operations on the system call, signal delivery and semaphore wait paths, on UP builds as well.

What. Split the word by who writes it:

  • tcb->flags keeps the bits that have a single writer: the thread type and TCB_FLAG_FREE_TCB, set by the creator before the task runs, TCB_FLAG_FREE_STACK and TCB_FLAG_EXIT_COUNTED, which change only while the task does not run or when it exits, and TCB_FLAG_SYSCALL, TCB_FLAG_SIGNAL_ACTION, TCB_FLAG_SIGMASK_SAVED, TCB_FLAG_PREEMPT_SCHED and TCB_FLAG_UNINTERRUPTIBLE, which only the task itself changes. It keeps its name and the TCB_FLAG_* names and is still accessed with plain loads and stores, so the arch code and the system call, signal delivery and semaphore wait paths are not changed. It becomes a uint16_t in the hole after init_priority, with the remaining bits renumbered to 0-9 in their original order.
  • The scheduling policy and the bits that other tasks change move to tcb->shared_flags, an atomic_t in the place of the old field: TCB_FLAG_POLICY_*, TCB_FLAG_CPU_LOCKED, TCB_FLAG_EXIT_PROCESSING, TCB_FLAG_HEAP_CHECK, TCB_FLAG_DETACHED, TCB_FLAG_FORCED_CANCEL and TCB_FLAG_JOIN_COMPLETED. They keep their names and are accessed with atomic_or(), atomic_and() and atomic_read(). The policy is read with nxsched_get_policy() and changed with nxsched_set_policy(), a compare-and-exchange loop, so that other CPUs never see a half-written policy.

Where a flag was tested and then set in two steps (CPU_LOCKED in nxsched_suspend(), nxtask_restart(), nxsig_queue_action() and the backtrace path, EXIT_PROCESSING in nxtask_terminate()), the value returned by atomic_or() is used, so that two callers cannot both see the flag clear.

This also fixes nxtask_terminate(). It saved the flags before the SMP call that removes a running task from its CPU and wrote the whole word back afterwards, so a bit that the task or another task changed in the meantime (FORCED_CANCEL, JOIN_COMPLETED, DETACHED) was lost; the copy was also a uint16_t, so bits 16-19 were cleared. It now clears CPU_LOCKED only, and only if it set it.

Impact

  • Users / API: none visible to applications. struct tcb_s is kernel-internal. Out-of-tree code that tests the scheduling policy or TCB_FLAG_CPU_LOCKED, TCB_FLAG_EXIT_PROCESSING, TCB_FLAG_HEAP_CHECK, TCB_FLAG_DETACHED, TCB_FLAG_FORCED_CANCEL or TCB_FLAG_JOIN_COMPLETED must use tcb->shared_flags with atomic_*(); the names are unchanged, so such code still builds and must be checked by hand. tools/pynuttx/nxgdb/thread.py reads shared_flags.
  • Build: none.
  • Hardware: arch code changes only where it sets FORCED_CANCEL (arm, arm64, risc-v, x86_64). sizeof(struct tcb_s) is unchanged on every configuration below that has debug symbols (stm32l0538-disco:nsh 184, mps2-an521:nsh 216, mps3-an547:knsh 248, raspberrypi-pico-2:smp 224, qemu-armv7a:nsh 224, qemu-armv7a:knsh_smp 272, qemu-armv8a:nsh 344, qemu-armv8a:knsh 400, qemu-armv8a:nsh_smp 360, fvp-armv8r:pnsh_smp 416, rv-virt:knsh 248, rv-virt:ksmp64 416, sim:ostest 416, verdin-mx8mp:rpmsg 216 bytes).
  • Documentation: none.
  • Security: none.
  • Compatibility: see Users / API.

Testing

Host: Ubuntu x86_64, CI image ghcr.io/apache/nuttx/apache-nuttx-ci-linux, master 67e49fa, apps 8ff87439bf4 (clean worktree).

Build (make EXTRAFLAGS="-Wno-cpp -Werror"), master and this PR, all 20 pass: stm32l0538-disco:nsh, stm32f4discovery:nsh, stm32f4discovery:sporadic, stm32l476vg-disco:knsh, nucleo-g431rb:nsh, nucleo-g431rb:cansock, mps2-an521:nsh, mps3-an547:knsh, raspberrypi-pico-2:smp, qemu-armv7a:nsh, qemu-armv7a:knsh_smp, qemu-armv8a:nsh, qemu-armv8a:knsh, qemu-armv8a:nsh_smp, fvp-armv8r:pnsh_smp, rv-virt:knsh, rv-virt:ksmp64, sim:ostest, esp32-devkitc:nsh, verdin-mx8mp:rpmsg. tools/checkpatch.sh -c -u -m -g passes.

Hot paths. Atomic, barrier and interrupt-disable instructions (dmb, ldrex/strex, ldaex/stlex, ldaxr/stlxr, LSE, __aarch64_* calls, cpsid/PRIMASK, amoor/amoand, lr/sc) in the bodies of 26 hot-path functions (system call, IRQ dispatch, context switch, ready-to-run list, timer tick, sched_lock/sched_unlock, semaphore, signal delivery): the count is the same as master in all 336 function/configuration pairs.

Code size (.text, bytes):

Configuration Core master this PR delta
stm32l0538-disco:nsh Cortex-M0+ 60707 60847 +140
stm32f4discovery:nsh Cortex-M4 95716 95860 +144
mps3-an547:knsh Cortex-M55, kernel build 289239 289879 +640
qemu-armv7a:knsh_smp Cortex-A7, SMP 133080 133440 +360
qemu-armv8a:nsh_smp Cortex-A53, SMP 690364 690656 +292
rv-virt:ksmp64 RV64, SMP 146203 146295 +92

Runtime, ostest user_main: Exiting and ostest_main: Exiting with status 0, with no fault in the log:

Configuration Covers
qemu-armv8a:nsh_smp_tickless (4 CPUs) PREEMPT_SCHED in sched_unlock()
qemu-armv8a:citest_smp (4 CPUs) the configuration CI runs
qemu-armv7a:knsh armv7-a kernel build, system call path
sim:ostest full run

qemu-intel64:citest_smp (4 CPUs), LTP open POSIX signal tests, 30 pass: kill_1_1, kill_2_1, killpg_1_1, killpg_2_1, sigaction_1_1, sigwait_1_1, sigpending_1_1, pthread_kill_1_1, sigaddset_1_1, raise_1_1, sigprocmask 5_1 6_1 7_1 8_1 8_2 8_3 9_1 10_1 12_1 15_1 17_1, sigqueue 2_1 2_2 4_1 5_1 6_1 7_1 8_1 10_1 11_1. sigaltstack_1_1 is unresolved (Error 22) because sigaltstack() is not implemented.

The runtime results were measured on the previous revision f850d22, which differs only in keeping TCB_FLAG_EXIT_COUNTED in shared_flags.

GUIDINGLI
GUIDINGLI previously approved these changes Nov 12, 2026
@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: avr Issues related to all AVR(8-bit or 32-bit) architectures Arch: ceva Issues related to CEVA architecture Arch: hc Issues related to HC architecture Arch: mips Issues related to the MIPS architecture Arch: openrisc Issues related to the OpenRISC architecture Arch: renesas Issues related to the Renesas chips labels Nov 12, 2026
@github-actions github-actions Bot added Size: L The size of the change in this PR is large and removed Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. Board: arm labels Nov 17, 2026
@zyfeier
zyfeier force-pushed the sched-atomic-locks branch 5 times, most recently from fdc7a2b to 4e39d99 Compare November 17, 2026 07:03
@zyfeier zyfeier changed the title sched/signal: remove the big critical sections from the signal path sched: make the TCB and group flags atomic and drop the big critical sections Nov 17, 2026
@zyfeier
zyfeier marked this pull request as draft November 17, 2026 11:18
@zyfeier zyfeier changed the title sched: make the TCB and group flags atomic and drop the big critical sections sched: make tcb->flags atomic Nov 18, 2026
GUIDINGLI
GUIDINGLI previously approved these changes Nov 19, 2026
GUIDINGLI
GUIDINGLI previously approved these changes Nov 19, 2026

@jerpelea jerpelea 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.

please split the PR in 2 logical separated PRs:
sched: split tcb->flags by writer
sched: make the shared part atomic

@GUIDINGLI

Copy link
Copy Markdown
Contributor

please split the PR in 2 logical separated PRs: sched: split tcb->flags by writer sched: make the shared part atomic

@jerpelea
I suppose there is no need split to 2 PRs, they are already splitted to 5 paches.

GUIDINGLI
GUIDINGLI previously approved these changes Nov 23, 2026
Comment thread sched/sched/sched_setscheduler.c Resolved
Comment thread include/nuttx/sched.h Resolved
Comment thread arch/arm/src/armv7-a/arm_userfault.c Resolved
Comment thread arch/arm/src/common/arm_createstack.c Resolved
Comment thread sched/task/task_terminate.c Resolved
@zyfeier

zyfeier commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@jerpelea Split as requested: "sched: split tcb->flags by writer" is now #22238 (first three commits). This PR keeps the atomic part (last two commits) and depends on #22238; it will be rebased once #22238 is merged.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@zyfeier please fix the conflict

@jerpelea

Copy link
Copy Markdown
Contributor

@jerpelea Split as requested: "sched: split tcb->flags by writer" is now #22238 (first three commits). This PR keeps the atomic part (last two commits) and depends on #22238; it will be rebased once #22238 is merged.

#22238 was closed

@zyfeier

zyfeier commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@jerpelea Split as requested: "sched: split tcb->flags by writer" is now #22238 (first three commits). This PR keeps the atomic part (last two commits) and depends on #22238; it will be rebased once #22238 is merged.

#22238 was closed

Hi, @jerpelea, closed in favor of #21804. After the rework, moving the cross-task bits (including the scheduling policy) out of tcb->flags and making them atomic is one step, so the change is now a single commit there.

Comment thread arch/arm/src/arm_a_r/arm_userfault.c Resolved
Comment thread include/nuttx/sched.h Resolved
The flags of one task are written from more than one CPU.
nxsched_suspend(), nxsig_queue_action() and nxtask_restart() set
TCB_FLAG_CPU_LOCKED on a task that runs on another CPU, and
sched_setscheduler() changes the scheduling policy of any task, while
that task changes its own bits without the critical section, for
example TCB_FLAG_SYSCALL on system call entry and exit.  A plain
read-modify-write of tcb->flags can then lose one of the two updates.

Split the word by who writes it:

- tcb->flags keeps the bits that have a single writer: the thread type
  and TCB_FLAG_FREE_TCB, set by the creator before the task runs,
  TCB_FLAG_FREE_STACK and TCB_FLAG_EXIT_COUNTED, which change only
  while the task does not run or when it exits, and the bits that only
  the task itself changes.  It keeps its name and the TCB_FLAG_* names,
  the remaining bits are renumbered to bits 0-9 in their original
  order, and it is still accessed with plain loads and stores, so the
  arch code and the system call, signal delivery and semaphore wait
  paths are not changed.  It becomes a uint16_t in the hole after
  init_priority.
- The scheduling policy and the bits that other tasks change move to
  tcb->shared_flags, an atomic_t in the place of the old field, and are
  accessed with atomic_or(), atomic_and(), atomic_read() and, for the
  policy, a compare-and-exchange loop in nxsched_set_policy().  They
  keep their TCB_FLAG_* names, but are used only with shared_flags.

Where a flag was tested and then set in two steps (TCB_FLAG_CPU_LOCKED
in nxsched_suspend(), nxtask_restart(), nxsig_queue_action() and the
backtrace path, TCB_FLAG_EXIT_PROCESSING in nxtask_terminate()), use the
value returned by atomic_or() instead, so that two callers cannot both
see the flag clear.

This also fixes nxtask_terminate().  It saved the flags before the SMP
call that removes a running task from its CPU and wrote the whole word
back afterwards.  The task keeps running on the other CPU until that
call is handled, so a bit that it or another task changed in the
meantime, such as TCB_FLAG_FORCED_CANCEL, TCB_FLAG_JOIN_COMPLETED or
TCB_FLAG_DETACHED, was lost.  The copy was also a uint16_t, so writing
it back cleared bits 16-19.  Now it clears TCB_FLAG_CPU_LOCKED only,
and only if it set it.

Fix the nxstyle errors that the touched files carry.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: x86_64 Issues related to the x86_64 architecture Area: File System File System issues Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants