xiaoxiang781216 commented on code in PR #19562:
URL: https://github.com/apache/nuttx/pull/19562#discussion_r3741422658
##########
arch/arm/src/common/gnu/fork.S:
##########
@@ -88,6 +86,7 @@
up_fork:
/* Create a stack frame */
+ mov r2, r0 /* Preserve the vfork flag */
Review Comment:
let keep r0 unchanged and remove this and line 111
##########
arch/x86_64/src/common/fork.S:
##########
@@ -91,14 +88,19 @@
* | [r15] | i
* | [r14] | n
* | [r13] | g
- * | [r12] | <- rsp before calling x86_64_fork, rdi = rsp
+ * | [r12] | <- rsp before calling x86_64_fork, rsi = rsp
* | ......... |
*/
.globl up_fork
.type up_fork, @function
up_fork:
+ /* %rdx is call-clobbered and is not part of the snapshot, so it carries
+ * the vfork flag across the sequence below, which clobbers %rdi.
+ */
+
+ movq %rdi, %rdx
Review Comment:
don't change rdi too
##########
sched/task/task_fork.c:
##########
@@ -268,65 +453,127 @@ FAR struct tcb_s *nxtask_setup_fork(start_t retaddr)
* Name: nxtask_start_fork
*
* Description:
- * The fork() function has the same effect as fork(), except that the
- * behavior is undefined if the process created by fork() either modifies
- * any data other than a variable of type pid_t used to store the return
- * value from fork(), or returns from the function in which fork() was
- * called, or calls any other function before successfully calling _exit()
- * or one of the exec family of functions.
- *
- * This function provides one step in the overall fork() sequence: It
- * starts execution of the previously initialized TCB. The overall
- * sequence is:
- *
- * 1) User code calls fork()
- * 2) Architecture-specific code provides fork()and calls
- * nxtask_setup_fork().
- * 3) nxtask_setup_fork() allocates and configures the child task's TCB.
- * This consists of:
- * - Allocation of the child task's TCB.
- * - Initialization of file descriptors and streams
- * - Configuration of environment variables
- * - Allocate and initialize the stack
- * - Setup the input parameters for the task.
- * - Initialization of the TCB (including call to up_initial_state())
- * 4) fork() provides any additional operating context. fork must:
- * - Initialize special values in any CPU registers that were not
- * already configured by up_initial_state()
- * 5) fork() then calls nxtask_start_fork()
- * 6) nxtask_start_fork() then executes the child thread.
+ * The last step of both primitives: finish the child and run it. The
+ * architecture-specific code calls this once it has built the child's
+ * register context and stack.
+ *
+ * A vfork() additionally suspends the caller until the child calls _exit()
+ * or one of the exec family of functions. The suspension lives here, in
+ * the kernel primitive, rather than in a libc waitpid() as it once did.
+ * Two things follow from that. The parent is released when the child's
+ * TCB is torn down (see nxtask_resume_vfork()), which for an exec()ing
+ * child is immediately after exec_swap() has handed the child's pid to the
+ * program it loaded -- so the parent resumes at exec(), holding a pid that
+ * names the running program, as POSIX requires. And vfork() no longer
+ * depends on CONFIG_SCHED_WAITPID.
*
* Input Parameters:
- * child - The tcb_s struct instance that created by
- * nxtask_setup_fork() method
- * wait_child - whether need to wait until the child is running finished
+ * child - The tcb_s struct instance created by nxtask_setup_fork()
+ * vfork - true for vfork(), false for fork()
*
* Returned Value:
- * Upon successful completion, fork() returns 0 to the child process and
- * returns the process ID of the child process to the parent process.
- * Otherwise, -1 is returned to the parent, no child process is created,
- * and errno is set to indicate the error.
+ * The process ID of the child, or ERROR on failure.
*
****************************************************************************/
-pid_t nxtask_start_fork(FAR struct tcb_s *child)
+pid_t nxtask_start_fork(FAR struct tcb_s *child, bool vfork)
{
+#ifdef CONFIG_ARCH_HAVE_VFORK
+ struct vfork_s rel;
+ int ret;
+#endif
pid_t pid;
- sinfo("Starting Child TCB=%p\n", child);
+ sinfo("Starting Child TCB=%p vfork=%d\n", child, vfork);
DEBUGASSERT(child);
+#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_HAVE_FORK)
+ /* The architecture code has finished writing the child's image, so put the
+ * parent back in its own address environment. See nxtask_setup_fork().
+ */
+
+ if (!vfork &&
+ (child->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
+ {
+ fork_restore_parent_env();
+ }
+#endif
+
/* Get the assigned pid before we start the task */
pid = child->pid;
+#ifdef CONFIG_ARCH_HAVE_VFORK
+ if (vfork)
+ {
+ /* The rendezvous lives in this frame. We are about to block in it and
+ * will not leave until the child has posted, so it outlives every use.
+ */
+
+ nxsem_init(&rel.sem, 0, 0);
+ rel.released = false;
+ child->vfork_rel = &rel;
+ }
+#endif
+
/* Activate the task */
nxtask_activate(child);
+#ifdef CONFIG_ARCH_HAVE_VFORK
+ if (vfork)
+ {
+ /* Wait for the child to _exit() or exec(). This is not a cancellation
+ * point and must not be interrupted by a signal: the child may be
+ * running on our stack, so returning early would corrupt it.
+ */
+
+ do
+ {
+ ret = nxsem_wait_uninterruptible(&rel.sem);
+ }
+ while (ret == -EINTR);
+
+ nxsem_destroy(&rel.sem);
+ }
+#endif
+
return pid;
}
+#ifdef CONFIG_ARCH_HAVE_VFORK
+/****************************************************************************
+ * Name: nxtask_resume_vfork
+ *
+ * Description:
+ * Release the vfork() parent suspended on this child, if there is one.
+ *
+ * Called from nxsched_release_tcb(), the last point in the child's life,
+ * by which time an exec()ing child has already handed its pid to the
+ * program it loaded. nxtask_abort_fork() reaches it too, so a fork that
+ * fails after the rendezvous also releases the parent.
+ *
+ * Input Parameters:
+ * child - The TCB being torn down
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void nxtask_resume_vfork(FAR struct tcb_s *child)
+{
+ FAR struct vfork_s *rel = child->vfork_rel;
+
+ if (rel != NULL && !rel->released)
+ {
+ rel->released = true;
+ child->vfork_rel = NULL;
Review Comment:
why need both released and sem? keep sem is enough.
##########
arch/Kconfig:
##########
@@ -11,7 +11,7 @@ config ARCH_ARM
bool "ARM"
select ARCH_HAVE_BACKTRACE
select ARCH_HAVE_INTERRUPTSTACK
- select ARCH_HAVE_FORK
+ select ARCH_HAVE_VFORK if !BUILD_KERNEL
Review Comment:
why need check BUILD_KERNEL
##########
arch/Kconfig:
##########
@@ -29,7 +29,7 @@ config ARCH_ARM64
select ARCH_64BIT
select ARCH_HAVE_BACKTRACE
select ARCH_HAVE_INTERRUPTSTACK
- select ARCH_HAVE_FORK if !BUILD_KERNEL && !BUILD_PROTECTED
+ select ARCH_HAVE_VFORK if !BUILD_KERNEL && !BUILD_PROTECTED
Review Comment:
ditto
##########
arch/risc-v/src/common/fork.S:
##########
@@ -94,12 +84,15 @@
up_fork:
#ifdef CONFIG_LIB_SYSCALL
- /* When coming via system call, everything is in place already */
+ /* When coming via system call, everything is in place already: a0 already
+ * holds the vfork flag, and riscv_fork() takes its snapshot from the TCB.
+ */
tail riscv_fork
#else
/* Create a stack frame */
+ mv a2, a0 /* Preserve the vfork flag */
Review Comment:
could we avoid change a0
##########
include/nuttx/sched.h:
##########
@@ -1136,34 +1159,55 @@ void nxtask_startup(main_t entrypt, int argc, FAR char
*argv[]);
#endif
/****************************************************************************
- * Internal fork support. The overall sequence is:
- *
- * 1) User code calls fork(). fork() is provided in architecture-specific
- * code.
- * 2) fork()and calls nxtask_setup_fork().
+ * Internal support for the two cloning primitives, vfork() and fork(). The
+ * sequence below is common to both, and `vfork' says which one was called:
+ *
+ * vfork() the child shares the parent's memory and the parent is
+ * suspended until the child _exit()s or exec()s.
+ * fork() the child gets its own copy of the parent's memory at the same
+ * virtual addresses, and both run.
+ *
+ * 1) User code calls vfork() or fork(). Both are libc wrappers around
+ * up_fork(), which is provided in architecture-specific code.
+ * 2) The architecture-specific code snapshots the caller's registers and
+ * calls nxtask_setup_fork().
* 3) nxtask_setup_fork() allocates and configures the child task's TCB.
* This consists of:
* - Allocation of the child task's TCB.
* - Initialization of file descriptors and streams
* - Configuration of environment variables
- * - Allocate and initialize the stack
+ * - Establishing the child's address environment: joined to the parent's
+ * for vfork(), duplicated from it for fork()
+ * - Allocating the stack, or inheriting the parent's for fork()
* - Setup the input parameters for the task.
* - Initialization of the TCB (including call to up_initial_state())
- * 4) fork() provides any additional operating context. fork must:
+ * 4) The architecture-specific code provides any additional operating
+ * context:
* - Initialize special values in any CPU registers that were not
* already configured by up_initial_state()
- * 5) fork() then calls nxtask_start_fork()
+ * - Relocate the copied stack, unless the child shares the parent's
+ * 5) It then calls nxtask_start_fork(), which for vfork() additionally
+ * suspends the caller.
* 6) nxtask_start_fork() then executes the child thread.
*
* nxtask_abort_fork() may be called if an error occurs between
* steps 3 and 6.
*
+ * nxtask_resume_vfork() releases a suspended vfork() parent. It is called
+ * from nxsched_release_tcb(), the last point in the child's life -- by which
+ * time the child is off the ready-to-run list and an exec()ing child has
+ * already handed its pid to the program it loaded.
+ *
****************************************************************************/
-FAR struct tcb_s *nxtask_setup_fork(start_t retaddr);
-pid_t nxtask_start_fork(FAR struct tcb_s *child);
+FAR struct tcb_s *nxtask_setup_fork(start_t retaddr, bool vfork);
+pid_t nxtask_start_fork(FAR struct tcb_s *child, bool vfork);
void nxtask_abort_fork(FAR struct tcb_s *child, int errcode);
+#ifdef CONFIG_ARCH_HAVE_VFORK
+void nxtask_resume_vfork(FAR struct tcb_s *child);
Review Comment:
move to sched/sched.h
##########
libs/libc/libc.csv:
##########
@@ -348,6 +348,7 @@
"usleep","unistd.h","","int","useconds_t"
"vasprintf","stdio.h","","int","FAR char **","FAR const IPTR char *","va_list"
"versionsort","dirent.h","","int","FAR const struct dirent **","FAR const
struct dirent **"
+"vfork","unistd.h","!defined(CONFIG_BUILD_KERNEL) &&
defined(CONFIG_ARCH_HAVE_VFORK)","pid_t"
Review Comment:
why check CONFIG_BUILD_KERNEL
##########
arch/Kconfig:
##########
@@ -147,7 +147,7 @@ config ARCH_X86_64
select PCI_LATE_DRIVERS_REGISTER if PCI
select ARCH_TOOLCHAIN_GNU
select ARCH_HAVE_BACKTRACE
- select ARCH_HAVE_FORK if !BUILD_KERNEL
+ select ARCH_HAVE_VFORK if !BUILD_KERNEL
Review Comment:
ditto
--
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]