zyfeier opened a new pull request, #22238: URL: https://github.com/apache/nuttx/pull/22238
## Summary **Why.** Five bits of `tcb->flags` are changed only by the task itself: `TCB_FLAG_SYSCALL`, `TCB_FLAG_SIGNAL_ACTION`, `TCB_FLAG_SIGMASK_SAVED`, `TCB_FLAG_PREEMPT_SCHED` and `TCB_FLAG_UNINTERRUPTIBLE`. They are also the only bits that the system call, signal delivery, semaphore wait, `sched_unlock()` and timer tick paths change or test. The rest of the word is written from other CPUs as well: `nxsched_suspend()`, `nxsig_queue_action()` and `nxtask_restart()` set `TCB_FLAG_CPU_LOCKED` on a task that is running elsewhere. As long as both groups share one word, a plain read-modify-write on either side can lose the other side's update, and making the word atomic puts atomic operations on every one of those hot paths. **What.** Move the self-written bits to a byte of their own: - `tcb->exec_flags` (`uint8_t`): `TCB_EXEC_SYSCALL`, `TCB_EXEC_SIGNAL_ACTION`, `TCB_EXEC_SIGMASK_SAVED`, `TCB_EXEC_PREEMPT_SCHED`, `TCB_EXEC_UNINTERRUPTIBLE`. Only the task itself changes them, so plain loads and stores are enough. The thread type (`TCB_FLAG_TTYPE_*`) moves here too: the creator of the TCB sets it before the task runs and it never changes, so it is read with a plain load. - `tcb->sched_policy` (`uint8_t`): holds `SCHED_FIFO`, `SCHED_RR`, `SCHED_SPORADIC` or `SCHED_OTHER` directly. Other tasks change it, but always as a whole value. The two bytes fill the 2-byte hole after `init_priority`, so `struct tcb_s` keeps its size. A plain read-modify-write of `exec_flags` cannot lose an update: - no other task and no interrupt handler writes it: `sched_unlock()` returns early in interrupt context, and a system call is raised by the task itself; - a signal action that runs between the load and the store of such a change restores every bit it sets before it returns; - a byte of its own is a separate memory location, so writing it cannot disturb an update of `tcb->flags` on another CPU. With this change a cross-CPU write of `TCB_FLAG_CPU_LOCKED` can no longer clobber the hot-path bits. The bits left in `tcb->flags` are made atomic in #21804, which depends on this PR. **How.** 1. `sched, arch: fix nxstyle errors in files touched by the tcb flags change`: whitespace only, in files that this PR and #21804 change. 2. `sched: move the per-task hot-path flags out of tcb->flags`: adds `exec_flags` and `sched_policy` and converts their users. Removes `TCB_FLAG_SYSCALL`, `TCB_FLAG_SIGNAL_ACTION`, `TCB_FLAG_SIGMASK_SAVED`, `TCB_FLAG_PREEMPT_SCHED`, `TCB_FLAG_UNINTERRUPTIBLE`, `TCB_FLAG_POLICY_*` and `TCB_FLAG_SCHED_*`, so that out-of-tree code still using them fails to build instead of reading the wrong bits. 3. `sched: move the thread type into tcb->exec_flags`: the type moves to bits 0-1 of `exec_flags`. `TCB_FLAG_TTYPE_MASK` becomes `TCB_EXEC_TTYPE_MASK`, so that code still reading the type from `tcb->flags` fails to build; the `TCB_FLAG_TTYPE_TASK/PTHREAD/KERNEL` values keep their names and values, since they are also passed as the `ttype` argument of `up_create_stack()` and friends. ## Impact - Users / API: none visible to applications. `struct tcb_s` is kernel-internal; out-of-tree code that uses the removed `TCB_FLAG_*` macros must switch to `exec_flags` / `sched_policy`, and read the thread type as `tcb->exec_flags & TCB_EXEC_TTYPE_MASK`. `tools/pynuttx/nxgdb/thread.py` reads `sched_policy`. - Build: none. - Hardware: none. `sizeof(struct tcb_s)` is unchanged (checked with `ptype /o` on stm32l0538-disco:nsh 184, mps3-an547:knsh 248, qemu-armv7a:knsh_smp 272, qemu-armv8a:nsh_smp 360, rv-virt:ksmp64 416, sim:ostest 416 bytes). - Documentation: `Documentation/guides/concurrency/fork_vfork_migration.rst` and `Documentation/os/scheduling/index.rst` use the new names. - Security: none. - Compatibility: see Users / API. ## Testing Host: Ubuntu x86_64, CI image `ghcr.io/apache/nuttx/apache-nuttx-ci-linux`, master c32466a8737, apps 8ff87439bf4 (clean worktree) for the build. The runtime results below were measured on master 960459c6503: on the first two commits of this PR, and again on the whole series of #21804, which includes the third commit. They will be refreshed on the current base. **Build** (`make EXTRAFLAGS="-Wno-cpp -Werror"`): 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 on every commit. **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-armv8a:knsh | arm64 kernel build, system call path | | 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. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
