Repository navigation
sched: split tcb->flags by writer - #21804
Conversation
c1924f5 to
05f50bd
Compare
05f50bd to
e10360b
Compare
b8054fe to
6cb69ba
Compare
fdc7a2b to
4e39d99
Compare
4e39d99 to
80ffe69
Compare
jerpelea
left a comment
There was a problem hiding this comment.
please split the PR in 2 logical separated PRs:
sched: split tcb->flags by writer
sched: make the shared part atomic
@jerpelea |
|
@zyfeier please fix the conflict |
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. |
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>
Summary
Why. The flags of one task are written from more than one CPU.
nxsched_suspend(),nxsig_queue_action()andnxtask_restart()setTCB_FLAG_CPU_LOCKEDon a task that runs on another CPU, andsched_setscheduler()changes the scheduling policy of any task, while that task changes its own bits without the critical section, for exampleTCB_FLAG_SYSCALLon system call entry and exit. A plain read-modify-write oftcb->flagscan 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->flagskeeps the bits that have a single writer: the thread type andTCB_FLAG_FREE_TCB, set by the creator before the task runs,TCB_FLAG_FREE_STACKandTCB_FLAG_EXIT_COUNTED, which change only while the task does not run or when it exits, andTCB_FLAG_SYSCALL,TCB_FLAG_SIGNAL_ACTION,TCB_FLAG_SIGMASK_SAVED,TCB_FLAG_PREEMPT_SCHEDandTCB_FLAG_UNINTERRUPTIBLE, which only the task itself changes. It keeps its name and theTCB_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 auint16_tin the hole afterinit_priority, with the remaining bits renumbered to 0-9 in their original order.tcb->shared_flags, anatomic_tin 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_CANCELandTCB_FLAG_JOIN_COMPLETED. They keep their names and are accessed withatomic_or(),atomic_and()andatomic_read(). The policy is read withnxsched_get_policy()and changed withnxsched_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_LOCKEDinnxsched_suspend(),nxtask_restart(),nxsig_queue_action()and the backtrace path,EXIT_PROCESSINGinnxtask_terminate()), the value returned byatomic_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 auint16_t, so bits 16-19 were cleared. It now clearsCPU_LOCKEDonly, and only if it set it.Impact
struct tcb_sis kernel-internal. Out-of-tree code that tests the scheduling policy orTCB_FLAG_CPU_LOCKED,TCB_FLAG_EXIT_PROCESSING,TCB_FLAG_HEAP_CHECK,TCB_FLAG_DETACHED,TCB_FLAG_FORCED_CANCELorTCB_FLAG_JOIN_COMPLETEDmust usetcb->shared_flagswithatomic_*(); the names are unchanged, so such code still builds and must be checked by hand.tools/pynuttx/nxgdb/thread.pyreadsshared_flags.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).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 -gpasses.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):Runtime, ostest
user_main: Exitingandostest_main: Exiting with status 0, with no fault in the log:PREEMPT_SCHEDinsched_unlock()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_1is unresolved (Error 22) becausesigaltstack()is not implemented.The runtime results were measured on the previous revision f850d22, which differs only in keeping
TCB_FLAG_EXIT_COUNTEDinshared_flags.