This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 13d5c83cd8cd457e419f28218e2bfec2d7172f1e Author: Marco Casaroli <[email protected]> AuthorDate: Wed Nov 18 01:11:33 2026 +0100 sched/task: Change the child count of a task group under tg_lock. Without CONFIG_SCHED_CHILD_STATUS, a task group counts its children in tg_nchildren. The creation of a child (nxtask_save_parent()), the exit of a child (nxtask_sigchild()) and task_reparent() change the count with no lock or only in a critical section. On SMP, a child that exits on one CPU and a new child that the parent creates on another CPU can change the count of the parent at the same time, and one change is lost. A lost decrement keeps waitpid() waiting for a child that is gone; a lost increment makes it return ECHILD while a child runs. Change the count under tg_lock of the group, as group_add_child() and group_remove_child() change the child list with CONFIG_SCHED_CHILD_STATUS. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- sched/task/task_exithook.c | 4 ++++ sched/task/task_reparent.c | 32 ++++++++++++++++++++++++++++---- sched/task/task_setup.c | 5 +++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/sched/task/task_exithook.c b/sched/task/task_exithook.c index c11e993feec..cb177eb60de 100644 --- a/sched/task/task_exithook.c +++ b/sched/task/task_exithook.c @@ -245,8 +245,12 @@ static inline void nxtask_sigchild(FAR struct tcb_s *ptcb, * children from this parent. */ + irqstate_t flags; + DEBUGASSERT(ptcb->group != NULL && ptcb->group->tg_nchildren > 0); + flags = spin_lock_irqsave(&ptcb->group->tg_lock); ptcb->group->tg_nchildren--; + spin_unlock_irqrestore(&ptcb->group->tg_lock, flags); #endif /* CONFIG_SCHED_CHILD_STATUS */ diff --git a/sched/task/task_reparent.c b/sched/task/task_reparent.c index ce82b04687b..9b92c293cd7 100644 --- a/sched/task/task_reparent.c +++ b/sched/task/task_reparent.c @@ -69,6 +69,9 @@ int task_reparent(pid_t ppid, pid_t chpid) FAR struct task_group_s *pgrp; FAR struct tcb_s *tcb; irqstate_t flags; +#ifndef CONFIG_SCHED_CHILD_STATUS + irqstate_t tgflags; +#endif pid_t opid; int ret; @@ -186,8 +189,17 @@ int task_reparent(pid_t ppid, pid_t chpid) DEBUGASSERT(ogrp->tg_nchildren > 0); - ogrp->tg_nchildren--; /* The original parent now has one few children */ - pgrp->tg_nchildren++; /* The new parent has one additional child */ + /* The original parent now has one few children */ + + tgflags = spin_lock_irqsave(&ogrp->tg_lock); + ogrp->tg_nchildren--; + spin_unlock_irqrestore(&ogrp->tg_lock, tgflags); + + /* The new parent has one additional child */ + + tgflags = spin_lock_irqsave(&pgrp->tg_lock); + pgrp->tg_nchildren++; + spin_unlock_irqrestore(&pgrp->tg_lock, tgflags); ret = OK; #endif /* CONFIG_SCHED_CHILD_STATUS */ @@ -212,6 +224,9 @@ int task_reparent(pid_t ppid, pid_t chpid) FAR struct tcb_s *otcb; pid_t opid; irqstate_t flags; +#ifndef CONFIG_SCHED_CHILD_STATUS + irqstate_t tgflags; +#endif int ret; /* Disable interrupts so that nothing can change in the relationship of @@ -310,8 +325,17 @@ int task_reparent(pid_t ppid, pid_t chpid) DEBUGASSERT(otcb->group != NULL && otcb->group->tg_nchildren > 0); - otcb->group->tg_nchildren--; /* The original parent now has one few children */ - ptcb->group->tg_nchildren++; /* The new parent has one additional child */ + /* The original parent now has one few children */ + + tgflags = spin_lock_irqsave(&otcb->group->tg_lock); + otcb->group->tg_nchildren--; + spin_unlock_irqrestore(&otcb->group->tg_lock, tgflags); + + /* The new parent has one additional child */ + + tgflags = spin_lock_irqsave(&ptcb->group->tg_lock); + ptcb->group->tg_nchildren++; + spin_unlock_irqrestore(&ptcb->group->tg_lock, tgflags); ret = OK; #endif /* CONFIG_SCHED_CHILD_STATUS */ diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c index b9e9be6f3ac..875a7dd5ad2 100644 --- a/sched/task/task_setup.c +++ b/sched/task/task_setup.c @@ -262,6 +262,9 @@ static inline void nxtask_save_parent(FAR struct tcb_s *tcb, uint8_t ttype) /* Get the TCB of the parent task. In this case, the calling task. */ FAR struct tcb_s *rtcb = this_task(); +#ifndef CONFIG_SCHED_CHILD_STATUS + irqstate_t flags; +#endif DEBUGASSERT(rtcb != NULL && rtcb->group != NULL); @@ -320,7 +323,9 @@ static inline void nxtask_save_parent(FAR struct tcb_s *tcb, uint8_t ttype) */ DEBUGASSERT(rtcb->group->tg_nchildren < UINT16_MAX); + flags = spin_lock_irqsave(&rtcb->group->tg_lock); rtcb->group->tg_nchildren++; + spin_unlock_irqrestore(&rtcb->group->tg_lock, flags); #endif /* CONFIG_SCHED_CHILD_STATUS */ }
