Repository navigation
Conversation
The next change converts the tcb->flags users in these files. They carry style errors that nxstyle reports for any change to the file: missing blank lines after declarations, a mis-indented DEBUGASSERT in irq_dispatch.c and mis-indented switch cases in minerva_swint.c. Fix them first, so that the conversion itself stays easy to review. No functional change. Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
Five bits of tcb->flags are changed only by the task itself: SYSCALL, SIGNAL_ACTION, SIGMASK_SAVED, PREEMPT_SCHED and UNINTERRUPTIBLE. The scheduling policy is written by other tasks, but always as a whole value. These are also the only bits that the system call, signal delivery, semaphore wait, sched_unlock() and timer tick paths change or test. Move the five bits into a new byte, tcb->exec_flags, and the policy into tcb->sched_policy, which holds SCHED_FIFO, SCHED_RR, SCHED_SPORADIC or SCHED_OTHER directly. Both bytes fill the existing 2-byte hole after init_priority, so struct tcb_s keeps its size on 32-bit and 64-bit targets. Since no other task and no interrupt handler writes exec_flags, a plain read-modify-write of it cannot lose an update; a signal action that runs in the middle of such a write restores every bit it sets before it returns. A byte of its own is a separate memory location, so changing exec_flags or sched_policy cannot disturb a concurrent atomic update of tcb->flags on another CPU. This leaves the remaining tcb->flags bits, which are written from more than one context, to be made atomic without adding atomic operations to the paths above. The TCB_FLAG_SYSCALL, TCB_FLAG_SIGNAL_ACTION, TCB_FLAG_SIGMASK_SAVED, TCB_FLAG_PREEMPT_SCHED, TCB_FLAG_UNINTERRUPTIBLE and TCB_FLAG_POLICY_* / TCB_FLAG_SCHED_* definitions are removed, so out-of-tree code that still uses them fails to build instead of reading the wrong bits. Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
The next commit makes the bits of tcb->flags atomic, because other CPUs change some of them. The thread type does not need that. The creator of the TCB sets it before the task runs, and it never changes after that. It is kept in tcb->flags only because the bits used to be packed together, but it is read on many paths: task creation and exit, group and signal handling, fault handling and the file systems. Move the type to bits 0-1 of tcb->exec_flags and the TCB_EXEC_* bits to bits 2-6. Nothing else writes exec_flags while the creator sets the type: the task does not run yet and no other task writes exec_flags. After that, the task's own updates of exec_flags write the unchanged type back, so the type can be read with a plain load on any CPU. TCB_FLAG_TTYPE_MASK is renamed TCB_EXEC_TTYPE_MASK, so that code which still reads the type from tcb->flags fails to build. The type values keep their names and values: they are also passed as the ttype argument of up_create_stack() and friends and match CHILD_FLAG_TTYPE_*. Signed-off-by: zhangyuan29 <zhangyuan29@xiaomi.com>
zyfeier
requested review from
Donny9,
GUIDINGLI,
Ouss4,
anchao,
davids5,
gustavonihei,
hartmannathan,
jerpelea,
johannes-nivus,
masayuki2009,
pkarashchenko,
pussuw,
simbit18,
tmedicci,
xiaoxiang781216,
yamt and
yf13
as code owners
November 24, 2026 05:21
|
|
||
| #ifdef CONFIG_BUILD_KERNEL | ||
| if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL) | ||
| if ((tcb->exec_flags & TCB_EXEC_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL) |
Contributor
There was a problem hiding this comment.
can we keep exec_flags and macro name as before to reduce the chanage
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Why. Five bits of
tcb->flagsare changed only by the task itself:TCB_FLAG_SYSCALL,TCB_FLAG_SIGNAL_ACTION,TCB_FLAG_SIGMASK_SAVED,TCB_FLAG_PREEMPT_SCHEDandTCB_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()andnxtask_restart()setTCB_FLAG_CPU_LOCKEDon 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): holdsSCHED_FIFO,SCHED_RR,SCHED_SPORADICorSCHED_OTHERdirectly. Other tasks change it, but always as a whole value.The two bytes fill the 2-byte hole after
init_priority, sostruct tcb_skeeps its size.A plain read-modify-write of
exec_flagscannot lose an update:sched_unlock()returns early in interrupt context, and a system call is raised by the task itself;tcb->flagson another CPU.With this change a cross-CPU write of
TCB_FLAG_CPU_LOCKEDcan no longer clobber the hot-path bits. The bits left intcb->flagsare made atomic in #21804, which depends on this PR.How.
sched, arch: fix nxstyle errors in files touched by the tcb flags change: whitespace only, in files that this PR and sched: split tcb->flags by writer #21804 change.sched: move the per-task hot-path flags out of tcb->flags: addsexec_flagsandsched_policyand converts their users. RemovesTCB_FLAG_SYSCALL,TCB_FLAG_SIGNAL_ACTION,TCB_FLAG_SIGMASK_SAVED,TCB_FLAG_PREEMPT_SCHED,TCB_FLAG_UNINTERRUPTIBLE,TCB_FLAG_POLICY_*andTCB_FLAG_SCHED_*, so that out-of-tree code still using them fails to build instead of reading the wrong bits.sched: move the thread type into tcb->exec_flags: the type moves to bits 0-1 ofexec_flags.TCB_FLAG_TTYPE_MASKbecomesTCB_EXEC_TTYPE_MASK, so that code still reading the type fromtcb->flagsfails to build; theTCB_FLAG_TTYPE_TASK/PTHREAD/KERNELvalues keep their names and values, since they are also passed as thettypeargument ofup_create_stack()and friends.Impact
struct tcb_sis kernel-internal; out-of-tree code that uses the removedTCB_FLAG_*macros must switch toexec_flags/sched_policy, and read the thread type astcb->exec_flags & TCB_EXEC_TTYPE_MASK.tools/pynuttx/nxgdb/thread.pyreadssched_policy.sizeof(struct tcb_s)is unchanged (checked withptype /oon 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/guides/concurrency/fork_vfork_migration.rstandDocumentation/os/scheduling/index.rstuse the new names.Testing
Host: Ubuntu x86_64, CI image
ghcr.io/apache/nuttx/apache-nuttx-ci-linux, master c32466a, apps 8ff87439bf4 (clean worktree) for the build. The runtime results below were measured on master 960459c: 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 -gpasses on every commit.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.