nuttx/include
Marco Casaroli 70c2ef5911 !sched/arch/libc: Give fork() and vfork() their real, separate semantics.
NuttX implemented fork() and vfork() as the same function.  Both were libc
wrappers around a single up_fork() syscall; vfork() differed only by a
trailing waitpid().  Underneath, the child joined the parent's address
environment -- the same addrenv_join() that pthread_create() uses -- and got
a private copy of the stack.  So the child shared .data, .bss and the heap
with its parent and ran concurrently with it.

That is not fork().  It is vfork()-with-a-private-stack under fork()'s name,
and the history says so: today's fork() is NuttX's old vfork(), renamed in
c33d1c9c97 (2023) without any change of behaviour.  The failure was silent --
a program written against POSIX fork() compiled, ran, and had its child's
writes land in the parent's variables.

Separate them into two primitives, chosen by which function the caller
called rather than by what the hardware happens to be:

  fork()   child gets its own copy of the parent's memory at the same
           virtual addresses; runs concurrently.  Only where an address
           environment can be duplicated -- elsewhere it is not declared at
           all, so calling it is a build error naming the function.
  vfork()  child shares the parent's memory; parent suspended until the
           child _exit()s or exec()s.  Implementable everywhere.

Below libc there is still one syscall.  up_fork() gains a bool saying which
primitive the caller used, since the per-architecture register snapshot is
the same for both, and passes it to nxtask_setup_fork(), which is the single
place the memory semantics are decided.  The argument arrives in the first
argument register and is never touched:  each architecture's snapshot takes
some other call-clobbered register for its scratch, so the flag is simply
still there when the C worker is called.

The vfork() parent suspension moves out of libc into nxtask_start_fork(),
released from nxsched_release_tcb() by nxtask_resume_vfork().  Two things
follow: the parent is resumed at exec(), since exec_swap() has already handed
the child's pid to the loaded program by the time the vfork stub exits, and
vfork() no longer depends on CONFIG_SCHED_WAITPID.

Releasing there requires one fix in nxtask_exit().  It raises rtcb->lockcount
directly rather than through sched_lock() while it tears the TCB down, so the
nxsem_post() that wakes the vfork() parent leaves it queued where a blocked
task collects while pre-emption is off -- g_pendingtasks, or g_readytorun on
SMP -- and the matching raw lockcount-- does not publish it the way
sched_unlock() would, leaving the parent stranded with nothing to move it on.
The fix mirrors sched_unlock() for each case:  nxsched_merge_pending(), or
nxsched_deliver_task() under CONFIG_SMP.  Both are no-ops while pre-emption is
still disabled, and up_exit() re-reads this_task() afterwards, so a change of
the ready-to-run head is honoured.  Without it vfork() deadlocks wherever no
other task happens to call sched_unlock() afterwards -- rv-virt:nsh64 and
rv-virt:pnsh64, where NSH is blocked in waitpid() holding the lock, and
qemu-armv8a:citest_smp, which hangs the moment the vfork() test runs.

fork() is built on a new addrenv_fork(), backed by an up_addrenv_fork() hook
that duplicates an address environment into freshly allocated pages mapped at
the same virtual addresses -- unlike up_addrenv_clone(), which copies only
the representation and leaves both pointing at the same page tables.  The
child then adopts the parent's stack geometry rather than being given a
relocated copy: a pointer to a stack local taken before fork() must name the
same object in the child that it named in the parent, and the parent's stack
is already in the duplicate, with its contents, at the parent's address.

No architecture implements up_addrenv_fork() yet, so this commit leaves
fork() unavailable everywhere.  That is the intended state.  It withdraws
fork() from ARCH_ARM, flat ARCH_ARM64, ARCH_RISCV, ARCH_SIM and ARCH_X86_64,
where until now it named the sharing primitive; per-architecture patches
restore it, with POSIX semantics, as up_addrenv_fork() lands.  In the
meantime the sharing primitive is still there under the name that describes
it: vfork() for a child that runs a program, pthread_create() for a second
flow of control that shares memory, posix_spawn() for both at once.

Kconfig: ARCH_HAVE_VFORK inherits ARCH_HAVE_FORK's select lines, conditions
included, so no configuration gains machinery; ARCH_HAVE_FORK is redefined to
mean "can provide POSIX fork() semantics" and now depends on ARCH_ADDRENV.

There is one deliberate departure from "verbatim".  ARCH_ARM selected the
fork family unconditionally, BUILD_KERNEL included, and that has never
worked:  on a kernel build the architecture's fork entry point sees the
kernel's return address and stack pointer rather than the caller's, so the
child resumes at a kernel address.  On qemu-armv7a:knsh master faults in
ostest's fork case with "Child did not run" and then a data abort; without
the condition this change faults the same way through vfork().  ARCH_ARM64
and ARCH_X86_64 already carried "if !BUILD_KERNEL" for exactly this reason --
ARM was the outlier.  Conditioning it turns a runtime fault into an honest
absence, which is the whole point of the change; arch/arm takes the condition
off again in the patch that adds its saved-syscall-frame path.  Only the
MMU-capable ARM ports are affected, since Cortex-M cannot build BUILD_KERNEL
at all.

Also fixes two latent syntax errors found on the way: a missing comma in
riscv_fork.c and mips_fork.c, both in *_FRAMEPOINTER && !SAVE_GP branches
that are never compiled today.

BREAKING CHANGE: fork() is withdrawn from every architecture.  It is no
longer declared in unistd.h, so code that calls it fails to build with an error
naming the function, and the sharing behaviour it used to have is gone rather
than renamed.  CONFIG_ARCH_HAVE_FORK no longer means "fork() exists"; it means
"this configuration can provide POSIX fork() semantics", and no architecture
selects it yet.

Quick fix, chosen by why the call was made:

  to run a program                vfork() + exec*(), or better posix_spawn()
  a second flow of control that   pthread_create()
  shares the caller's memory
  a genuinely independent copy    keep fork(), and wait for the per-arch patch
  of the process                  that implements up_addrenv_fork() and selects
                                  CONFIG_ARCH_HAVE_FORK

Out-of-tree code that tests CONFIG_ARCH_HAVE_FORK to decide whether a
fork-then-exec path is available wants CONFIG_ARCH_HAVE_VFORK instead, which is
selected in exactly the places CONFIG_ARCH_HAVE_FORK used to be.  The full
migration guide is Documentation/guides/fork_vfork_migration.rst.

Assisted-by: Claude Code:claude-opus-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-08-10 08:57:30 -03:00
..
android
arpa
crypto crypto: add CRYPTO_AES_CTR_SSH variant (128-bit big-endian counter) 2026-07-16 15:42:10 +08:00
cxx include/cxx/ctime: Add localtime to std namespace. 2026-06-09 11:33:40 -03:00
net forward: Add IFF_NOSRC_FORWARD and IFF_NODST_FORWARD flags. 2026-01-31 02:37:58 +08:00
netinet netinet/in.h: Rename imr_interface to imr_address in struct ip_mreqn. 2026-01-16 09:42:12 +08:00
netpacket net/packet: add PACKET_<ADD|DROP>_MEMBERSHIP support 2025-12-25 10:01:43 +08:00
nuttx !sched/arch/libc: Give fork() and vfork() their real, separate semantics. 2026-08-10 08:57:30 -03:00
ssp
sys !sched/arch/libc: Give fork() and vfork() their real, separate semantics. 2026-08-10 08:57:30 -03:00
.gitignore ci: add stdbit.h test 2026-06-29 14:44:17 +02:00
aio.h
alloca.h
assert.h
byteswap.h
ctype.h
debug.h style: fix checkpatch issues after debug.h move 2026-04-07 07:50:06 -03:00
dirent.h fs/dirent: add d_ino member to struct dirent 2026-06-26 10:45:33 -04:00
dlfcn.h
dsp.h libs/libdsp: Add Matrix operations 2026-07-11 14:55:59 -03:00
dspb16.h style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
elf.h arch:use ARCH_64BIT to mark arch is support 64bit 2025-12-18 22:05:12 +08:00
elf32.h
elf64.h
endian.h
err.h
errno.h include/errno.h: skip set_errno in interrupt context 2026-05-03 17:23:40 -03:00
execinfo.h
fcntl.h !include/fcntl.h: align open flags with Linux values 2026-06-30 13:43:44 +08:00
fixedmath.h !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
fnmatch.h
ftw.h
gcov.h gcov: Refactoring the implementation framework of gcov 2025-07-03 00:17:58 +08:00
getopt.h
glob.h
grp.h libc/grp: add getgrouplist() 2026-06-23 23:09:22 +08:00
hex2bin.h
iconv.h
ifaddrs.h
imx_container.h arch/arm64/imx9: Add ROMAPI and AHAB support for i.MX9 2025-05-09 19:21:17 +08:00
inttypes.h !sched/clock: remove CONFIG_SYSTEM_TIME64 and always use 64-bit time 2026-05-19 16:21:28 +08:00
iso646.h
langinfo.h
libgen.h
libintl.h
limits.h !sys/types.h: change time_t and clock_t to int64_t to align with other OSes 2026-05-19 16:21:28 +08:00
locale.h style/FAR: remove FAR qualifier 2025-11-10 10:56:29 +01:00
lzf.h lzf: prevent lzf header struct optimization 2026-02-13 11:58:50 +01:00
malloc.h
mqueue.h
netdb.h
nl_types.h
nxflat.h !arch/arm: Use r9 as the PIC base register. 2026-08-06 01:38:23 +08:00
obstack.h
poll.h
pthread.h include/pthread : initialize wait_count in PTHREAD_COND_INITIALIZER 2026-06-16 19:12:30 -03:00
pty.h
pwd.h
regex.h
resolv.h
sched.h sched: remove csection and reduce the interrupt disabling time in sched_[un]lock 2025-01-23 19:58:49 +08:00
search.h LICENSE: update NuttX-PublicDomain SPDX identifier 2025-12-26 19:46:12 +08:00
semaphore.h sched/semaphore: add support to customize semaphore max allowed value 2025-09-04 09:48:34 +08:00
shadow.h
signal.h sched/timer: Fix MISRA Rule 10.4 2026-01-22 22:14:00 +08:00
spawn.h sched/spawn: Fix MISRA C 2012 Rule 10.4 violations 2026-02-02 20:39:36 +08:00
stdbool.h
stddef.h !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
stdint.h arm64_head.S: fix the asm code build error 2026-01-13 21:20:52 +08:00
stdio.h stdio.h: Update TMP_MAX definition to match Linux. 2026-01-14 17:28:30 -08:00
stdlib.h fs/binfmt: close symlink TOCTOU and harden setuid/setgid exec hygiene 2026-08-01 15:32:03 -03:00
stdnoreturn.h
string.h include/string.h: mark memset and memcpy as used_code 2026-02-12 13:19:06 -05:00
strings.h !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
syscall.h
syslog.h drivers/syslog: Add RFC 5424 protocol support 2025-06-13 20:26:57 +08:00
termios.h include: modify for support socat, so need add some macro same as linux 2025-12-10 23:44:14 +08:00
threads.h include/threads.h: Replace thrd_ defines by actual function definitions 2024-12-19 21:56:02 +08:00
time.h
ulimit.h ulimit: add ulimit implementation 2026-01-16 10:03:53 +08:00
unistd.h !sched/arch/libc: Give fork() and vfork() their real, separate semantics. 2026-08-10 08:57:30 -03:00
utime.h
uuid.h
wait.h wait: add wait.h header file 2024-12-20 11:46:14 -03:00
wchar.h fix misspelled names in locally scoped code 2025-05-15 10:12:12 +08:00
wctype.h