[v5,00/22] Fix Race conditions in pthread cancellation [BZ#12683]

Message ID 20230410195907.4123869-1-adhemerval.zanella@linaro.org
Headers
Series Fix Race conditions in pthread cancellation [BZ#12683] |

Message

Adhemerval Zanella Netto April 10, 2023, 7:58 p.m. UTC
  This updated version from previous one [1] is simpler due the
changes already in done during past releases.  It only requires
changes on cancellation entrypoint, the SIGCANCEL handler, and
minor changes on pthread_cancel.  It also take in consideration
the recent regressions like 404656009b, where some syscall can
not be interrupted if not async mode is not enabled.

Each cancellable entrypoint is now a just tail call to internal
__syscall_cancel, with the nont required arguments set to 0 (it also
allows some code size optimization).  The single-thread or pthread
cancellation disable is handled at __syscall_cancel as well.

The first patch is the generic bits, while the rest is arch-specific
syscall_cancel.S entry points and/or arch-specific bits (like
7 argument support for mips or argument handling for x32).  So
it can not be installed separately, since the generic version does
not work correctly on all ABIs (another option might to move
the fix itself to be the last patch).

I have checked on x86_64, i686, aarch64, arm, powerpc, powerpc64,
powerpc64le, sparc64, and mips.  For the rest of the implementation
I manually crafted the syscall_cancel.S from the compiler generated
built of the generic syscall_cancel.c.

Changes from v4:
  - Rebased against master, added or1k and loongarch.
  - Make SYSCALL_CANCEL and INTERNAL_SYSCALL_CANCEL macros to work
    better as tail call, to do not require argument suffling.
  - Fixed x32 to use TYPEFY and ARGIFY macros.
  - Fixed m68k to use names registers.
  - Fixed typos from last version.

[1] https://sourceware.org/pipermail/libc-alpha/2020-April/112517.html

Adhemerval Zanella (22):
  nptl: Fix Race conditions in pthread cancellation [BZ#12683]
  x86_64: Fix Race conditions in pthread cancellation [BZ#12683]
  x32: Fix Race conditions in pthread cancellation [BZ#12683]
  i386: Fix Race conditions in pthread cancellation [BZ#12683]
  aarch64: Fix Race conditions in pthread cancellation [BZ#12683]
  arm: Fix Race conditions in pthread cancellation [BZ#12683]
  powerpc: Fix Race conditions in pthread cancellation [BZ#12683]
  sparc: Fix Race conditions in pthread cancellation [BZ#12683]
  s390: Fix Race conditions in pthread cancellation [BZ#12683]
  riscv: Fix Race conditions in pthread cancellation [BZ#12683]
  arc: Fix Race conditions in pthread cancellation [BZ #12683]
  ia64: Fix Race conditions in pthread cancellation [BZ#12683]
  sh: Fix Race conditions in pthread cancellation [BZ#12683]
  nios2: Fix Race conditions in pthread cancellation [BZ#12683]
  microblaze: Fix Race conditions in pthread cancellation [BZ#12683]
  hppa: Fix Race conditions in pthread cancellation [BZ#12683]
  m68k: Fix Race conditions in pthread cancellation [BZ#12683]
  alpha: Fix Race conditions in pthread cancellation [BZ#12683]
  csky: Fix Race conditions in pthread cancellation [BZ#12683]
  mips: Fix Race conditions in pthread cancellation [BZ#12683]
  or1k: Fix Race conditions in pthread cancellation [BZ#12683]
  loongarch: Fix Race conditions in pthread cancellation [BZ#12683]

 elf/Makefile                                  |   5 +-
 nptl/Makefile                                 |  11 +-
 nptl/cancellation.c                           | 115 ++++++------
 nptl/cleanup_defer.c                          |   5 +-
 nptl/descr-const.sym                          |   6 +
 nptl/descr.h                                  |  18 ++
 nptl/libc-cleanup.c                           |   5 +-
 nptl/pthread_cancel.c                         |  78 +++-----
 nptl/pthread_exit.c                           |   4 +-
 nptl/pthread_setcancelstate.c                 |   2 +-
 nptl/pthread_setcanceltype.c                  |   2 +-
 nptl/pthread_testcancel.c                     |   5 +-
 nptl/tst-cancel31.c                           | 100 ++++++++++
 sysdeps/generic/syscall_types.h               |  25 +++
 sysdeps/nptl/cancellation-pc-check.h          |  54 ++++++
 sysdeps/nptl/lowlevellock-futex.h             |  20 +-
 sysdeps/nptl/pthreadP.h                       |  11 +-
 sysdeps/powerpc/powerpc32/sysdep.h            |   3 +
 sysdeps/powerpc/powerpc64/sysdep.h            |  19 ++
 sysdeps/sh/sysdep.h                           |   1 +
 sysdeps/unix/sysdep.h                         | 173 ++++++++++++++----
 .../unix/sysv/linux/aarch64/syscall_cancel.S  |  59 ++++++
 .../unix/sysv/linux/alpha/syscall_cancel.S    |  80 ++++++++
 sysdeps/unix/sysv/linux/arc/syscall_cancel.S  |  56 ++++++
 sysdeps/unix/sysv/linux/arm/syscall_cancel.S  |  78 ++++++++
 sysdeps/unix/sysv/linux/csky/syscall_cancel.S | 114 ++++++++++++
 sysdeps/unix/sysv/linux/hppa/syscall_cancel.S |  81 ++++++++
 sysdeps/unix/sysv/linux/i386/syscall_cancel.S | 103 +++++++++++
 .../sysv/linux/ia64/cancellation-pc-check.h   |  48 +++++
 sysdeps/unix/sysv/linux/ia64/syscall_cancel.S |  81 ++++++++
 .../sysv/linux/loongarch/syscall_cancel.S     |  54 ++++++
 sysdeps/unix/sysv/linux/m68k/syscall_cancel.S |  84 +++++++++
 .../sysv/linux/microblaze/syscall_cancel.S    |  61 ++++++
 .../sysv/linux/mips/mips32/syscall_cancel.S   | 128 +++++++++++++
 sysdeps/unix/sysv/linux/mips/mips32/sysdep.h  |   4 +
 .../linux/mips/mips64/n32/syscall_types.h     |  28 +++
 .../sysv/linux/mips/mips64/syscall_cancel.S   | 108 +++++++++++
 sysdeps/unix/sysv/linux/mips/mips64/sysdep.h  |  52 +++---
 .../unix/sysv/linux/nios2/syscall_cancel.S    |  95 ++++++++++
 sysdeps/unix/sysv/linux/or1k/syscall_cancel.S |  63 +++++++
 .../unix/sysv/linux/powerpc/syscall_cancel.S  |  65 +++++++
 .../unix/sysv/linux/riscv/syscall_cancel.S    |  67 +++++++
 .../sysv/linux/s390/s390-32/syscall_cancel.S  |  62 +++++++
 .../sysv/linux/s390/s390-64/syscall_cancel.S  |  62 +++++++
 sysdeps/unix/sysv/linux/sh/syscall_cancel.S   | 126 +++++++++++++
 sysdeps/unix/sysv/linux/socketcall.h          |  35 +++-
 .../sysv/linux/sparc/sparc32/syscall_cancel.S |  71 +++++++
 .../sysv/linux/sparc/sparc64/syscall_cancel.S |  74 ++++++++
 sysdeps/unix/sysv/linux/syscall_cancel.c      |  73 ++++++++
 sysdeps/unix/sysv/linux/sysdep-cancel.h       |  12 --
 .../unix/sysv/linux/x86_64/syscall_cancel.S   |  57 ++++++
 .../sysv/linux/x86_64/x32/syscall_types.h     |  34 ++++
 sysdeps/x86_64/nptl/tcb-offsets.sym           |   3 -
 53 files changed, 2551 insertions(+), 229 deletions(-)
 create mode 100644 nptl/descr-const.sym
 create mode 100644 nptl/tst-cancel31.c
 create mode 100644 sysdeps/generic/syscall_types.h
 create mode 100644 sysdeps/nptl/cancellation-pc-check.h
 create mode 100644 sysdeps/unix/sysv/linux/aarch64/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/alpha/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/arc/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/arm/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/csky/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/hppa/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/i386/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/ia64/cancellation-pc-check.h
 create mode 100644 sysdeps/unix/sysv/linux/ia64/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/loongarch/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/m68k/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/microblaze/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/mips/mips32/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/mips/mips64/n32/syscall_types.h
 create mode 100644 sysdeps/unix/sysv/linux/mips/mips64/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/nios2/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/or1k/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/powerpc/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/riscv/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/s390/s390-32/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/s390/s390-64/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/sh/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/sparc/sparc32/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/sparc/sparc64/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/syscall_cancel.c
 create mode 100644 sysdeps/unix/sysv/linux/x86_64/syscall_cancel.S
 create mode 100644 sysdeps/unix/sysv/linux/x86_64/x32/syscall_types.h