posix: New Linux posix_spawn{p} implementation
commit9ff72da471a509a8c19791efe469f47fa6977410
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 19 Jan 2016 19:33:32 +0000 (19 17:33 -0200)
committerAdhemerval Zanella <adhemerval.zanella@linaro.com>
Mon, 7 Mar 2016 04:53:47 +0000 (7 11:53 +0700)
tree8c5f0563c5141f1401c5dd43d4255cde78085194
parent1eb8930608705702d5746e5491bab4e4429fcb83
posix: New Linux posix_spawn{p} implementation

This patch implements a new posix_spawn{p} implementation for Linux.  The main
difference is it uses the clone syscall directly with CLONE_VM and CLONE_VFORK
flags and a direct allocated stack.  The new stack and start function solves
most the vfork limitation (possible parent clobber due stack spilling).  The
remaning issue are related to signal handling:

  1. That no signal handlers must run in child context, to avoid corrupt
     parent's state.
  2. Child must synchronize with parent to enforce stack deallocation and
     to possible return execv issues.

The first one is solved by blocking all signals in child, even NPTL-internal
ones (SIGCANCEL and SIGSETXID).  The second issue is done by a stack allocation
in parent and a synchronization with using a pipe or waitpid (in case or error).
The pipe has the advantage of allowing the child signal an exec error (checked
with new tst-spawn2 test).

There is an inherent race condition in pipe2 usage for architectures that do not
support the syscall directly.  In such cases the a pipe plus fctnl is used
instead and it may lead to file descriptor leak in parent (as decribed by fcntl
documentation).

The child process stack is allocate with a mmap with MAP_STACK flag using
default architecture stack size.  Although it is slower than use a stack buffer
from parent, it allows some slack for the compatibility code to run scripts
with no shebang (which may use a buffer with size depending of argument list
count).

Performance should be similar to the vfork default posix implementation and
way faster than fork path (vfork on mostly linux ports are basically
clone with CLONE_VM plus CLONE_VFORK).  The only difference is the syscalls
required for the stack allocation/deallocation.

It fixes BZ#10354, BZ#14750, and BZ#18433.

Tested on i386, x86_64, powerpc64le, and aarch64.

[BZ #14750]
[BZ #10354]
[BZ #18433]
* include/sched.h (__clone): Add hidden prototype.
(__clone2): Likewise.
* include/unistd.h (__dup): Likewise.
* posix/Makefile (tests): Add tst-spawn2.
* posix/tst-spawn2.c: New file.
* sysdeps/posix/dup.c (__dup): Add hidden definition.
* sysdeps/unix/sysv/linux/aarch64/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/alpha/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/arm/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/hppa/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/i386/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/ia64/clone2.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/m68k/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/microblaze/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/mips/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/nios2/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S (__clone):
Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S (__clone):
Likewise.
* sysdeps/unix/sysv/linux/s390/s390-32/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/s390/s390-64/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/sh/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc32/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc64/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/tile/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/x86_64/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/nptl-signals.h
(____nptl_is_internal_signal): New function.
* sysdeps/unix/sysv/linux/spawni.c: New file.
27 files changed:
ChangeLog
include/sched.h
include/unistd.h
posix/Makefile
posix/tst-spawn2.c [new file with mode: 0644]
sysdeps/posix/dup.c
sysdeps/unix/sysv/linux/aarch64/clone.S
sysdeps/unix/sysv/linux/alpha/clone.S
sysdeps/unix/sysv/linux/arm/clone.S
sysdeps/unix/sysv/linux/hppa/clone.S
sysdeps/unix/sysv/linux/i386/clone.S
sysdeps/unix/sysv/linux/ia64/clone2.S
sysdeps/unix/sysv/linux/m68k/clone.S
sysdeps/unix/sysv/linux/microblaze/clone.S
sysdeps/unix/sysv/linux/mips/clone.S
sysdeps/unix/sysv/linux/nios2/clone.S
sysdeps/unix/sysv/linux/nptl-signals.h
sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S
sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S
sysdeps/unix/sysv/linux/s390/s390-32/clone.S
sysdeps/unix/sysv/linux/s390/s390-64/clone.S
sysdeps/unix/sysv/linux/sh/clone.S
sysdeps/unix/sysv/linux/sparc/sparc32/clone.S
sysdeps/unix/sysv/linux/sparc/sparc64/clone.S
sysdeps/unix/sysv/linux/spawni.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/tile/clone.S
sysdeps/unix/sysv/linux/x86_64/clone.S