Skip to contentSkip to conversation

glibc: Perform rseq(2) registration at C startup and thread creation (v12)

PATCH In review Mathieu Desnoyers 4 participants 11 messages Aug 7, 2019 – Sep 14, 2019 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/5 glibc: Perform rseq(2) registration at C startup and thread creation (v12)

Register rseq(2) TLS for each thread (including main), and unregister for each thread (excluding main). "rseq" stands for Restartable Sequences.

See the rseq(2) man page proposed here:
https://lkml.org/lkml/2018/9/19/647

This patch is based on glibc-2.30. The rseq(2) system call was merged into Linux 4.18.

CC Carlos O'Donell <[email protected]>
CC Florian Weimer <[email protected]>
CC Joseph Myers <[email protected]>
CC Szabolcs Nagy <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Ben Maurer <[email protected]>
CC Peter Zijlstra <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Boqun Feng <[email protected]>
CC Will Deacon <[email protected]>
CC Dave Watson <[email protected]>
CC Paul Turner <[email protected]>
CC Rich Felker <[email protected]>

---
Changes since v1:
- Move __rseq_refcount to an extra field at the end of __rseq_abi to eliminate one symbol.

All libraries/programs which try to register rseq (glibc, early-adopter applications, early-adopter libraries) should use the rseq refcount. It becomes part of the ABI within a user-space process, but it's not part of the ABI shared with the kernel per se.

- Restructure how this code is organized so glibc keeps building on non-Linux targets.

- Use non-weak symbol for __rseq_abi.

- Move rseq registration/unregistration implementation into its own nptl/rseq.c compile unit.

- Move __rseq_abi symbol under GLIBC_2.29.

Changes since v2:
- Move __rseq_refcount to its own symbol, which is less ugly than trying to play tricks with the rseq uapi.
- Move __rseq_abi from nptl to csu (C start up), so it can be used across glibc, including memory allocator and sched_getcpu(). The __rseq_refcount symbol is kept in nptl, because there is no reason to use it elsewhere in glibc.

Changes since v3:
- Set __rseq_refcount TLS to 1 on register/set to 0 on unregister because glibc is the first/last user.
- Unconditionally register/unregister rseq at thread start/exit, because glibc is the first/last user.
- Add missing abilist items.
- Rebase on glibc master commit a502c5294.
- Add NEWS entry.

Changes since v4:
- Do not use "weak" symbols for __rseq_abi and __rseq_refcount. Based on "System V Application Binary Interface", weak only affects the link editor, not the dynamic linker.
- Install a new sys/rseq.h system header on Linux, which contains the RSEQ_SIG definition, __rseq_abi declaration and __rseq_refcount declaration. Move those definition/declarations from rseq-internal.h to the installed sys/rseq.h header.
- Considering that rseq is only available on Linux, move csu/rseq.c to sysdeps/unix/sysv/linux/rseq-sym.c.
- Move __rseq_refcount from nptl/rseq.c to sysdeps/unix/sysv/linux/rseq-sym.c, so it is only defined on Linux.
- Move both ABI definitions for __rseq_abi and __rseq_refcount to sysdeps/unix/sysv/linux/Versions, so they only appear on Linux.
- Document __rseq_abi and __rseq_refcount volatile.
- Document the RSEQ_SIG signature define.
- Move registration functions from rseq.c to rseq-internal.h static inline functions. Introduce empty stubs in misc/rseq-internal.h, which can be overridden by architecture code in sysdeps/unix/sysv/linux/rseq-internal.h.
- Rename __rseq_register_current_thread and __rseq_unregister_current_thread to rseq_register_current_thread and rseq_unregister_current_thread, now that those are only visible as internal static inline functions.
- Invoke rseq_register_current_thread() from libc-start.c LIBC_START_MAIN rather than nptl init, so applications not linked against libpthread.so have rseq registered for their main() thread. Note that it is invoked separately for SHARED and !SHARED builds.

Changes since v5:
- Replace __rseq_refcount by __rseq_lib_abi, which contains two
uint32_t: register_state and refcount. The "register_state" field allows inhibiting rseq registration from signal handlers nested on top of glibc registration and occuring after rseq unregistration by glibc.
- Introduce enum rseq_register_state, which contains the states allowed for the struct rseq_lib_abi register_state field.

Changes since v6:
- Introduce bits/rseq.h to define RSEQ_SIG for each architecture. The generic bits/rseq.h does not define RSEQ_SIG, meaning that each architecture implementing rseq needs to implement bits/rseq.h.
- Rename enum item RSEQ_REGISTER_NESTED to RSEQ_REGISTER_ONGOING.
- Port to glibc-2.29.

Changes since v7:
- Remove __rseq_lib_abi symbol, including refcount and register_state fields.
- Remove reference counting and nested signals handling from registration/unregistration functions.
- Introduce new __rseq_handled exported symbol, which is set to 1 by glibc on C startup when it handles restartable sequences. This allows glibc to coexist with early adopter libraries and applications wishing to register restartable sequences when it is not handled by glibc.
- Introduce rseq_init (), which sets __rseq_handled to 1 from C startup.
- Update NEWS entry.
- Update comments at the beginning of new files.
- Registration depends on both __NR_rseq and RSEQ_SIG.
- Remove ARM, powerpc, MIPS RSEQ_SIG until we agree with maintainers on the signature choice.
- Update x86, s390 RSEQ_SIG based on discussion with arch maintainers.
- Remove rseq-internal.h from headers list of misc/Makefile, so it it not installed by make install.

Changes since v8:
- Introduce RSEQ_SIG_CODE and RSEQ_SIG_DATA on aarch64 to handle compiling with -mbig-endian.

Changes since v9:
- Update Changelog.
- Remove unneeded new file comment header newlines.

Changes since v10:
- Remove volatile from __rseq_abi declaration.
- Document that __rseq_handled is about library managing rseq registration, independently of whether rseq is available or not.
- Move __rseq_handled symbol to ld.so, initialize this symbol within the dynamic linker initialization for both shared (rtld.c) and static (dl-support.c) builds.
- Only register the rseq TLS on initialization once in multiple-libc scenarios. Use rtld_active () for this purpose.
- In the static libc case, register the rseq TLS after LD_PRELOAD constructors are run, so it matches the order of this initialization vs LD_PRELOAD contructors execution for the shared libc.
- Agreed on signature choice with powerpc and MIPS maintainers, re-adding those signatures,
- The main architecture still left out signature-wise is ARM32.

Changes since v11:
- Rebase on glibc 2.30.
- Re-introduce ARM RSEQ_SIG following feedback from Will Deacon.

CC Carlos O'Donell <[email protected]>
CC Florian Weimer <[email protected]>
CC Joseph Myers <[email protected]>
CC Szabolcs Nagy <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Ben Maurer <[email protected]>
CC Peter Zijlstra <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Boqun Feng <[email protected]>
CC Will Deacon <[email protected]>
CC Dave Watson <[email protected]>
CC Paul Turner <[email protected]>
CC Rich Felker <[email protected]>
---
 ChangeLog                                     | 78 ++++++++++++++++
 NEWS                                          | 15 ++++
 csu/libc-start.c                              | 14 ++-
 elf/dl-support.c                              | 22 +++++
 elf/rtld.c                                    | 26 ++++++
 misc/rseq-internal.h                          | 38 ++++++++
 nptl/pthread_create.c                         |  9 ++
 sysdeps/unix/sysv/linux/Makefile              |  4 +-
 sysdeps/unix/sysv/linux/Versions              | 10 +++
 sysdeps/unix/sysv/linux/aarch64/bits/rseq.h   | 43 +++++++++
 sysdeps/unix/sysv/linux/aarch64/ld.abilist    |  1 +
 sysdeps/unix/sysv/linux/aarch64/libc.abilist  |  1 +
 sysdeps/unix/sysv/linux/alpha/ld.abilist      |  1 +
 sysdeps/unix/sysv/linux/alpha/libc.abilist    |  1 +
 sysdeps/unix/sysv/linux/arm/bits/rseq.h       | 83 +++++++++++++++++
 sysdeps/unix/sysv/linux/arm/ld.abilist        |  1 +
 sysdeps/unix/sysv/linux/arm/libc.abilist      |  1 +
 sysdeps/unix/sysv/linux/bits/rseq.h           | 29 ++++++
 sysdeps/unix/sysv/linux/csky/ld.abilist       |  1 +
 sysdeps/unix/sysv/linux/csky/libc.abilist     |  1 +
 sysdeps/unix/sysv/linux/hppa/ld.abilist       |  1 +
 sysdeps/unix/sysv/linux/hppa/libc.abilist     |  1 +
 sysdeps/unix/sysv/linux/i386/ld.abilist       |  1 +
 sysdeps/unix/sysv/linux/i386/libc.abilist     |  1 +
 sysdeps/unix/sysv/linux/ia64/ld.abilist       |  1 +
 sysdeps/unix/sysv/linux/ia64/libc.abilist     |  1 +
 .../unix/sysv/linux/m68k/coldfire/ld.abilist  |  1 +
 .../sysv/linux/m68k/coldfire/libc.abilist     |  1 +
 .../unix/sysv/linux/m68k/m680x0/ld.abilist    |  1 +
 .../unix/sysv/linux/m68k/m680x0/libc.abilist  |  1 +
 sysdeps/unix/sysv/linux/microblaze/ld.abilist |  1 +
 .../unix/sysv/linux/microblaze/libc.abilist   |  1 +
 sysdeps/unix/sysv/linux/mips/bits/rseq.h      | 62 +++++++++++++
 .../sysv/linux/mips/mips32/fpu/libc.abilist   |  1 +
 .../unix/sysv/linux/mips/mips32/ld.abilist    |  1 +
 .../sysv/linux/mips/mips32/nofpu/libc.abilist |  1 +
 .../sysv/linux/mips/mips64/n32/ld.abilist     |  1 +
 .../sysv/linux/mips/mips64/n32/libc.abilist   |  1 +
 .../sysv/linux/mips/mips64/n64/ld.abilist     |  1 +
 .../sysv/linux/mips/mips64/n64/libc.abilist   |  1 +
 sysdeps/unix/sysv/linux/nios2/ld.abilist      |  1 +
 sysdeps/unix/sysv/linux/nios2/libc.abilist    |  1 +
 sysdeps/unix/sysv/linux/powerpc/bits/rseq.h   | 37 ++++++++
 .../linux/powerpc/powerpc32/fpu/libc.abilist  |  1 +
 .../sysv/linux/powerpc/powerpc32/ld.abilist   |  1 +
 .../powerpc/powerpc32/nofpu/libc.abilist      |  1 +
 .../linux/powerpc/powerpc64/be/ld.abilist     |  1 +
 .../linux/powerpc/powerpc64/be/libc.abilist   |  1 +
 .../linux/powerpc/powerpc64/le/ld.abilist     |  1 +
 .../linux/powerpc/powerpc64/le/libc.abilist   |  1 +
 sysdeps/unix/sysv/linux/riscv/rv64/ld.abilist |  1 +
 .../unix/sysv/linux/riscv/rv64/libc.abilist   |  1 +
 sysdeps/unix/sysv/linux/rseq-internal.h       | 88 +++++++++++++++++++
 sysdeps/unix/sysv/linux/rseq-sym.c            | 43 +++++++++
 sysdeps/unix/sysv/linux/s390/bits/rseq.h      | 37 ++++++++
 .../unix/sysv/linux/s390/s390-32/ld.abilist   |  1 +
 .../unix/sysv/linux/s390/s390-32/libc.abilist |  1 +
 .../unix/sysv/linux/s390/s390-64/ld.abilist   |  1 +
 .../unix/sysv/linux/s390/s390-64/libc.abilist |  1 +
 sysdeps/unix/sysv/linux/sh/ld.abilist         |  1 +
 sysdeps/unix/sysv/linux/sh/libc.abilist       |  1 +
 .../unix/sysv/linux/sparc/sparc32/ld.abilist  |  1 +
 .../sysv/linux/sparc/sparc32/libc.abilist     |  1 +
 .../unix/sysv/linux/sparc/sparc64/ld.abilist  |  1 +
 .../sysv/linux/sparc/sparc64/libc.abilist     |  1 +
 sysdeps/unix/sysv/linux/sys/rseq.h            | 52 +++++++++++
 sysdeps/unix/sysv/linux/x86/bits/rseq.h       | 30 +++++++
 sysdeps/unix/sysv/linux/x86_64/64/ld.abilist  |  1 +
 .../unix/sysv/linux/x86_64/64/libc.abilist    |  1 +
 sysdeps/unix/sysv/linux/x86_64/x32/ld.abilist |  1 +
 .../unix/sysv/linux/x86_64/x32/libc.abilist   |  1 +
 71 files changed, 768 insertions(+), 4 deletions(-)
 create mode 100644 misc/rseq-internal.h
 create mode 100644 sysdeps/unix/sysv/linux/aarch64/bits/rseq.h
 create mode 100644 sysdeps/unix/sysv/linux/arm/bits/rseq.h
 create mode 100644 sysdeps/unix/sysv/linux/bits/rseq.h
 create mode 100644 sysdeps/unix/sysv/linux/mips/bits/rseq.h
 create mode 100644 sysdeps/unix/sysv/linux/powerpc/bits/rseq.h
 create mode 100644 sysdeps/unix/sysv/linux/rseq-internal.h
 create mode 100644 sysdeps/unix/sysv/linux/rseq-sym.c
 create mode 100644 sysdeps/unix/sysv/linux/s390/bits/rseq.h
 create mode 100644 sysdeps/unix/sysv/linux/sys/rseq.h
 create mode 100644 sysdeps/unix/sysv/linux/x86/bits/rseq.h
ChangeLog +78 -0
index cdb9e14881..9a1930248f 100644
@@ -1,3 +1,81 @@
+2019-08-06  Mathieu Desnoyers <[email protected]>
+
+	* NEWS: Add Restartable Sequences feature description.
+	* csu/libc-start.c: Perform rseq(2) registration at C startup.
+	* elf/rtld.c: Initialize __rseq_handled at dynamic linker
+	startup for shared libc.
+	* elf/dl-support.c: Initialize __rseq_handled at dynamic linker
+	startup for static libc.
+	* nptl/pthread_create.c: Perform rseq(2) registration at thread
+	creation, unregistration at thread exit.
+	* sysdeps/unix/sysv/linux/Makefile: Add rseq-sym, sys/rseq.h,
+	bits/rseq.h.
+	* sysdeps/unix/sysv/linux/Versions: Export __rseq_abi from libc
+	and __rseq_handled from ld.
+	* sysdeps/unix/sysv/linux/aarch64/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/aarch64/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/alpha/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/alpha/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/arm/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/arm/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/csky/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/csky/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/hppa/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/hppa/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/i386/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/i386/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/ia64/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/ia64/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/m68k/coldfire/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/m68k/m680x0/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/microblaze/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/microblaze/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/mips/mips32/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/mips/mips64/n32/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/mips/mips64/n64/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/nios2/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/nios2/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/powerpc32/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/powerpc64/be/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/powerpc64/le/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/riscv/rv64/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/s390/s390-32/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/s390/s390-64/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/sh/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/sh/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/sparc/sparc32/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/sparc/sparc64/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/x86_64/64/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/x86_64/64/libc.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/x86_64/x32/ld.abilist: Likewise.
+	* sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist: Likewise.
+	* misc/rseq-internal.h: New file.
+	* sysdeps/unix/sysv/linux/rseq-internal.h: Likewise.
+	* sysdeps/unix/sysv/linux/rseq-sym.c: Likewise.
+	* sysdeps/unix/sysv/linux/sys/rseq.h: Likewise.
+	* sysdeps/unix/sysv/linux/bits/rseq.h: Likewise.
+	* sysdeps/unix/sysv/linux/aarch64/bits/rseq.h: Likewise.
+	* sysdeps/unix/sysv/linux/arm/bits/rseq.h: Likewise.
+	* sysdeps/unix/sysv/linux/mips/bits/rseq.h: Likewise.
+	* sysdeps/unix/sysv/linux/powerpc/bits/rseq.h: Likewise.
+	* sysdeps/unix/sysv/linux/s390/bits/rseq.h: Likewise.
+	* sysdeps/unix/sysv/linux/x86/bits/rseq.h: Likewise.
+
 2019-08-01  Carlos O'Donell <[email protected]>
 
 	* version.h (RELEASE): Set to "stable".
NEWS +15 -0
index ee9ed4de5a..5b2ee85da2 100644
@@ -5,6 +5,21 @@ See the end for copying conditions.
 Please send GNU C library bug reports via <https://sourceware.org/bugzilla/>
 using `glibc' in the "product" field.
 
+Version 2.31
+
+Major new features:
+
+* Support for automatically registering threads with the Linux rseq(2)
+  system call has been added.  This system call is implemented starting
+  from Linux 4.18.  The Restartable Sequences ABI accelerates user-space
+  operations on per-cpu data.  It allows user-space to perform updates
+  on per-cpu data without requiring heavy-weight atomic operations.
+  Automatically registering threads allows all libraries, including libc,
+  to make immediate use of the rseq(2) support by using the documented ABI.
+  See 'man 2 rseq' for the details of the ABI shared between libc and the
+  kernel.
+
+
 Version 2.30
 
 Major new features:
csu/libc-start.c +12 -2
index 5d9c3675fa..136d30ace9 100644
@@ -22,6 +22,8 @@
 #include <ldsodefs.h>
 #include <exit-thread.h>
 #include <libc-internal.h>
+#include <rseq-internal.h>
+#include <ldsodefs.h>
 
 #include <elf/dl-tunables.h>
 
@@ -140,7 +142,13 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
 
   __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
 
-#ifndef SHARED
+#ifdef SHARED
+  if (rtld_active ())
+    {
+      /* Register rseq ABI to the kernel.   */
+      (void) rseq_register_current_thread ();
+    }
+#else
   _dl_relocate_static_pie ();
 
   char **ev = &argv[argc + 1];
@@ -231,7 +239,9 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
   __pointer_chk_guard_local = pointer_chk_guard;
 # endif
 
-#endif /* !SHARED  */
+    /* Register rseq ABI to the kernel.   */
+    (void) rseq_register_current_thread ();
+#endif
 
   /* Register the destructor of the dynamic linker if there is any.  */
   if (__glibc_likely (rtld_fini != NULL))
elf/dl-support.c +22 -0
index 0a8b636d02..f717afed75 100644
@@ -34,6 +34,7 @@
 #include <unsecvars.h>
 #include <hp-timing.h>
 #include <stackinfo.h>
+#include <rseq-internal.h>
 
 extern char *__progname;
 char **_dl_argv = &__progname;	/* This is checked for some error messages.  */
@@ -215,6 +216,24 @@ __rtld_lock_define_initialized_recursive (, _dl_load_lock)
    that list.  */
 __rtld_lock_define_initialized_recursive (, _dl_load_write_lock)
 
+/* Advertise Restartable Sequences registration ownership across
+   application and shared libraries.
+
+   Libraries and applications must check whether this variable is zero or
+   non-zero if they wish to perform rseq registration on their own. If it
+   is zero, it means restartable sequence registration is not handled, and
+   the library or application is free to perform rseq registration. In
+   that case, the library or application is taking ownership of rseq
+   registration, and may set __rseq_handled to 1. It may then set it back
+   to 0 after it completes unregistering rseq.
+
+   If __rseq_handled is found to be non-zero, it means that another
+   library (or the application) is currently handling rseq registration.
+
+   Typical use of __rseq_handled is within library constructors and
+   destructors, or at program startup.  */
+
+int __rseq_handled;
 
 #ifdef HAVE_AUX_VECTOR
 int _dl_clktck;
@@ -375,6 +394,9 @@ _dl_non_dynamic_init (void)
 	  _dl_stack_flags = _dl_phdr[i].p_flags;
 	  break;
 	}
+
+  /* Publicize rseq registration ownership.  */
+  rseq_init ();
 }
 
 #ifdef DL_SYSINFO_IMPLEMENTATION
elf/rtld.c +26 -0
index c9490ff694..ef24c2faff 100644
@@ -43,6 +43,7 @@
 #include <stap-probe.h>
 #include <stackinfo.h>
 #include <not-cancel.h>
+#include <rseq-internal.h>
 
 #include <assert.h>
 
@@ -157,6 +158,25 @@ strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
 # define SECURE_PATH_LIMIT 1024
 #endif
 
+/* Advertise Restartable Sequences registration ownership across
+   application and shared libraries.
+
+   Libraries and applications must check whether this variable is zero or
+   non-zero if they wish to perform rseq registration on their own. If it
+   is zero, it means restartable sequence registration is not handled, and
+   the library or application is free to perform rseq registration. In
+   that case, the library or application is taking ownership of rseq
+   registration, and may set __rseq_handled to 1. It may then set it back
+   to 0 after it completes unregistering rseq.
+
+   If __rseq_handled is found to be non-zero, it means that another
+   library (or the application) is currently handling rseq registration.
+
+   Typical use of __rseq_handled is within library constructors and
+   destructors, or at program startup.  */
+
+int __rseq_handled;
+
 /* Check that AT_SECURE=0, or that the passed name does not contain
    directories and is not overly long.  Reject empty names
    unconditionally.  */
@@ -2314,6 +2334,12 @@ ERROR: '%s': cannot process note segment.\n", _dl_argv[0]);
       rtld_timer_accum (&relocate_time, start);
     }
 
+  /* Publicize rseq registration ownership.  This must be performed
+     after rtld re-relocation, before invoking constructors of
+     preloaded libraries.  IFUNC resolvers are called before this
+     initialization, so they may not observe the initialized state.  */
+  rseq_init ();
+
   /* Do any necessary cleanups for the startup OS interface code.
      We do these now so that no calls are made after rtld re-relocation
      which might be resolved to different functions than we expect.
misc/rseq-internal.h new file +38 -0
new file mode 100644
index 0000000000..ccad30bca5
@@ -0,0 +1,38 @@
+/* Restartable Sequences internal API. Stub version.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef RSEQ_INTERNAL_H
+#define RSEQ_INTERNAL_H
+
+static inline int
+rseq_register_current_thread (void)
+{
+  return -1;
+}
+
+static inline int
+rseq_unregister_current_thread (void)
+{
+  return -1;
+}
+
+static inline int
+rseq_init (void)
+{
+}
+
+#endif /* rseq-internal.h */
nptl/pthread_create.c +9 -0
index 18b7bbe765..f15dc8694f 100644
@@ -33,6 +33,7 @@
 #include <default-sched.h>
 #include <futex-internal.h>
 #include <tls-setup.h>
+#include <rseq-internal.h>
 #include "libioP.h"
 
 #include <shlib-compat.h>
@@ -378,6 +379,7 @@ __free_tcb (struct pthread *pd)
 START_THREAD_DEFN
 {
   struct pthread *pd = START_THREAD_SELF;
+  bool has_rseq = false;
 
   /* Initialize resolver state pointer.  */
   __resp = &pd->res;
@@ -389,6 +391,9 @@ START_THREAD_DEFN
   if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) == -2))
     futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE);
 
+  /* Register rseq TLS to the kernel. */
+  has_rseq = !rseq_register_current_thread ();
+
 #ifdef __NR_set_robust_list
 # ifndef __ASSUME_SET_ROBUST_LIST
   if (__set_robust_list_avail >= 0)
@@ -566,6 +571,10 @@ START_THREAD_DEFN
     }
 #endif
 
+  /* Unregister rseq TLS from kernel. */
+  if (has_rseq && rseq_unregister_current_thread ())
+    abort();
+
   advise_stack_range (pd->stackblock, pd->stackblock_size, (uintptr_t) pd,
 		      pd->guardsize);
 
sysdeps/unix/sysv/linux/Makefile +2 -2
index 1ab6bcbfc8..f2bbe25c02 100644
@@ -1,5 +1,5 @@
 ifeq ($(subdir),csu)
-sysdep_routines += errno-loc
+sysdep_routines += errno-loc rseq-sym
 endif
 
 ifeq ($(subdir),assert)
@@ -48,7 +48,7 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
 		  bits/termios-c_iflag.h bits/termios-c_oflag.h \
 		  bits/termios-baud.h bits/termios-c_cflag.h \
 		  bits/termios-c_lflag.h bits/termios-tcflow.h \
-		  bits/termios-misc.h
+		  bits/termios-misc.h sys/rseq.h bits/rseq.h
 
 tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
 	 tst-quota tst-sync_file_range tst-sysconf-iov_max tst-ttyname \
sysdeps/unix/sysv/linux/Versions +10 -0
index 1ca102a9e2..01639a81e8 100644
@@ -177,6 +177,9 @@ libc {
   GLIBC_2.30 {
     getdents64; gettid; tgkill;
   }
+  GLIBC_2.31 {
+    __rseq_abi;
+  }
   GLIBC_PRIVATE {
     # functions used in other libraries
     __syscall_rt_sigqueueinfo;
@@ -188,3 +191,10 @@ libc {
     __netlink_assert_response;
   }
 }
+
+ld {
+  # Symbols required by dynamic linker
+  GLIBC_2.31 {
+    __rseq_handled;
+  }
+}
sysdeps/unix/sysv/linux/aarch64/bits/rseq.h new file +43 -0

43 lines not on this page · raw message

sysdeps/unix/sysv/linux/aarch64/ld.abilist +1 -0
index 4ffe688649..89a7312e8c 100644
@@ -7,3 +7,4 @@ GLIBC_2.17 calloc F
 GLIBC_2.17 free F
 GLIBC_2.17 malloc F
 GLIBC_2.17 realloc F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/aarch64/libc.abilist +1 -0
index a4c31932cb..6784f13c09 100644
@@ -2145,3 +2145,4 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
sysdeps/unix/sysv/linux/alpha/ld.abilist +1 -0
index 98b66edabf..029176ab4c 100644
@@ -6,4 +6,5 @@ GLIBC_2.0 realloc F
 GLIBC_2.1 __libc_stack_end D 0x8
 GLIBC_2.1 _dl_mcount F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
 GLIBC_2.4 __stack_chk_guard D 0x8
sysdeps/unix/sysv/linux/alpha/libc.abilist +1 -0
index fe85a35620..8a1be3a6a2 100644
@@ -2220,6 +2220,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 _IO_fprintf F
 GLIBC_2.4 _IO_printf F
 GLIBC_2.4 _IO_sprintf F
sysdeps/unix/sysv/linux/arm/bits/rseq.h new file +83 -0

83 lines not on this page · raw message

sysdeps/unix/sysv/linux/arm/ld.abilist +1 -0
index a301c6ebc4..c65e9a7aa4 100644
@@ -1,3 +1,4 @@
+GLIBC_2.31 __rseq_handled D 0x4
 GLIBC_2.4 __libc_stack_end D 0x4
 GLIBC_2.4 __stack_chk_guard D 0x4
 GLIBC_2.4 __tls_get_addr F
sysdeps/unix/sysv/linux/arm/libc.abilist +1 -0
index bc3df8dcea..10137cfcee 100644
@@ -130,6 +130,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 _Exit F
 GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
 GLIBC_2.4 _IO_2_1_stdin_ D 0xa0
sysdeps/unix/sysv/linux/bits/rseq.h new file +29 -0
new file mode 100644
index 0000000000..a3c023f5c7
@@ -0,0 +1,29 @@
+/* Restartable Sequences architecture header. Stub version.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _SYS_RSEQ_H
+# error "Never use <bits/rseq.h> directly; include <sys/rseq.h> instead."
+#endif
+
+/* RSEQ_SIG is a signature required before each abort handler code.
+
+   It is a 32-bit value that maps to actual architecture code compiled
+   into applications and libraries. It needs to be defined for each
+   architecture. When choosing this value, it needs to be taken into
+   account that generating invalid instructions may have ill effects on
+   tools like objdump, and may also have impact on the CPU speculative
+   execution efficiency in some cases.  */
sysdeps/unix/sysv/linux/csky/ld.abilist +1 -0
index 71576160ed..e801578569 100644
@@ -7,3 +7,4 @@ GLIBC_2.29 calloc F
 GLIBC_2.29 free F
 GLIBC_2.29 malloc F
 GLIBC_2.29 realloc F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/csky/libc.abilist +1 -0
index 9b3cee65bb..b7ed346b1c 100644
@@ -2089,3 +2089,4 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
sysdeps/unix/sysv/linux/hppa/ld.abilist +1 -0
index 0387614d8f..88a5f35dc6 100644
@@ -6,4 +6,5 @@ GLIBC_2.2 free F
 GLIBC_2.2 malloc F
 GLIBC_2.2 realloc F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
 GLIBC_2.4 __stack_chk_guard D 0x4
sysdeps/unix/sysv/linux/hppa/libc.abilist +1 -0
index 75edece94a..37fd5f8525 100644
@@ -2041,6 +2041,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/i386/ld.abilist +1 -0
index edb7307228..a371a40db3 100644
@@ -7,3 +7,4 @@ GLIBC_2.1 __libc_stack_end D 0x4
 GLIBC_2.1 _dl_mcount F
 GLIBC_2.3 ___tls_get_addr F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/i386/libc.abilist +1 -0
index edeaf8e722..c7862ab47d 100644
@@ -2207,6 +2207,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/ia64/ld.abilist +1 -0
index 82042472c3..97d5962b13 100644
@@ -6,3 +6,4 @@ GLIBC_2.2 free F
 GLIBC_2.2 malloc F
 GLIBC_2.2 realloc F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/ia64/libc.abilist +1 -0
index b5d460eeb2..87b478c0fc 100644
@@ -2073,6 +2073,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/m68k/coldfire/ld.abilist +1 -0
index a301c6ebc4..c65e9a7aa4 100644
@@ -1,3 +1,4 @@
+GLIBC_2.31 __rseq_handled D 0x4
 GLIBC_2.4 __libc_stack_end D 0x4
 GLIBC_2.4 __stack_chk_guard D 0x4
 GLIBC_2.4 __tls_get_addr F
sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist +1 -0
index 05633b3cb8..444996f828 100644
@@ -131,6 +131,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 _Exit F
 GLIBC_2.4 _IO_2_1_stderr_ D 0x98
 GLIBC_2.4 _IO_2_1_stdin_ D 0x98
sysdeps/unix/sysv/linux/m68k/m680x0/ld.abilist +1 -0
index c9ec45cf1c..b314193557 100644
@@ -6,4 +6,5 @@ GLIBC_2.0 realloc F
 GLIBC_2.1 __libc_stack_end D 0x4
 GLIBC_2.1 _dl_mcount F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
 GLIBC_2.4 __stack_chk_guard D 0x4
sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist +1 -0
index 47eb7b4608..0f7ca57a45 100644
@@ -2150,6 +2150,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/microblaze/ld.abilist +1 -0
index aa0d71150a..8113f09614 100644
@@ -7,3 +7,4 @@ GLIBC_2.18 calloc F
 GLIBC_2.18 free F
 GLIBC_2.18 malloc F
 GLIBC_2.18 realloc F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/microblaze/libc.abilist +1 -0
index f7ced487f7..a2ce147dde 100644
@@ -2137,3 +2137,4 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
sysdeps/unix/sysv/linux/mips/bits/rseq.h new file +62 -0

62 lines not on this page · raw message

sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist +1 -0
index e49dc4272e..c4e49c839a 100644
@@ -2124,6 +2124,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/mips/mips32/ld.abilist +1 -0
index 55d48868e8..a28e9bf372 100644
@@ -6,4 +6,5 @@ GLIBC_2.0 realloc F
 GLIBC_2.2 __libc_stack_end D 0x4
 GLIBC_2.2 _dl_mcount F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
 GLIBC_2.4 __stack_chk_guard D 0x4
sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist +1 -0
index daa3b60c5b..b8b61569bc 100644
@@ -2122,6 +2122,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/mips/mips64/n32/ld.abilist +1 -0
index 55d48868e8..a28e9bf372 100644
@@ -6,4 +6,5 @@ GLIBC_2.0 realloc F
 GLIBC_2.2 __libc_stack_end D 0x4
 GLIBC_2.2 _dl_mcount F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
 GLIBC_2.4 __stack_chk_guard D 0x4
sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist +1 -0
index 457ce0b6f2..9d6c5cb025 100644
@@ -2130,6 +2130,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/mips/mips64/n64/ld.abilist +1 -0
index 44b345b7cf..6251bc8901 100644
@@ -6,4 +6,5 @@ GLIBC_2.0 realloc F
 GLIBC_2.2 __libc_stack_end D 0x8
 GLIBC_2.2 _dl_mcount F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
 GLIBC_2.4 __stack_chk_guard D 0x8
sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist +1 -0
index 63d5c03bfb..444c2f112a 100644
@@ -2124,6 +2124,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/nios2/ld.abilist +1 -0
index 110f1039fa..64d8acc083 100644
@@ -7,3 +7,4 @@ GLIBC_2.21 calloc F
 GLIBC_2.21 free F
 GLIBC_2.21 malloc F
 GLIBC_2.21 realloc F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/nios2/libc.abilist +1 -0
index 7fec0c9670..7b2ccbe953 100644
@@ -2178,3 +2178,4 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
sysdeps/unix/sysv/linux/powerpc/bits/rseq.h new file +37 -0
new file mode 100644
index 0000000000..bae8f4aaa1
@@ -0,0 +1,37 @@
+/* Restartable Sequences Linux powerpc architecture header.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _SYS_RSEQ_H
+# error "Never use <bits/rseq.h> directly; include <sys/rseq.h> instead."
+#endif
+
+/* RSEQ_SIG is a signature required before each abort handler code.
+
+   It is a 32-bit value that maps to actual architecture code compiled
+   into applications and libraries. It needs to be defined for each
+   architecture. When choosing this value, it needs to be taken into
+   account that generating invalid instructions may have ill effects on
+   tools like objdump, and may also have impact on the CPU speculative
+   execution efficiency in some cases.
+
+   RSEQ_SIG uses the following trap instruction:
+
+   powerpc-be:    0f e5 00 0b           twui   r5,11
+   powerpc64-le:  0b 00 e5 0f           twui   r5,11
+   powerpc64-be:  0f e5 00 0b           twui   r5,11  */
+
+#define RSEQ_SIG	0x0fe5000b
sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist +1 -0
index 9200a54309..76b2595f64 100644
@@ -2180,6 +2180,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 _IO_fprintf F
 GLIBC_2.4 _IO_printf F
 GLIBC_2.4 _IO_sprintf F
sysdeps/unix/sysv/linux/powerpc/powerpc32/ld.abilist +1 -0
index e8b0ea3a9b..b7589928de 100644
@@ -8,3 +8,4 @@ GLIBC_2.1 _dl_mcount F
 GLIBC_2.22 __tls_get_addr_opt F
 GLIBC_2.23 __parse_hwcap_and_convert_at_platform F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist +1 -0
index ef7779905f..1ce88283bc 100644
@@ -2213,6 +2213,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 _IO_fprintf F
 GLIBC_2.4 _IO_printf F
 GLIBC_2.4 _IO_sprintf F
sysdeps/unix/sysv/linux/powerpc/powerpc64/be/ld.abilist +1 -0
index edfc9ca56f..81f41a27ea 100644
@@ -8,3 +8,4 @@ GLIBC_2.3 calloc F
 GLIBC_2.3 free F
 GLIBC_2.3 malloc F
 GLIBC_2.3 realloc F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist +1 -0
index 2860df8ebc..a123a607f0 100644
@@ -2043,6 +2043,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 _IO_fprintf F
 GLIBC_2.4 _IO_printf F
 GLIBC_2.4 _IO_sprintf F
sysdeps/unix/sysv/linux/powerpc/powerpc64/le/ld.abilist +1 -0
index 37c8f6684b..d1f99b8d39 100644
@@ -8,3 +8,4 @@ GLIBC_2.17 malloc F
 GLIBC_2.17 realloc F
 GLIBC_2.22 __tls_get_addr_opt F
 GLIBC_2.23 __parse_hwcap_and_convert_at_platform F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist +1 -0
index 2229a1dcc0..608ad49593 100644
@@ -2247,3 +2247,4 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
sysdeps/unix/sysv/linux/riscv/rv64/ld.abilist +1 -0
index b411871d06..a1536257dd 100644
@@ -7,3 +7,4 @@ GLIBC_2.27 calloc F
 GLIBC_2.27 free F
 GLIBC_2.27 malloc F
 GLIBC_2.27 realloc F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +1 -0
index 31010e6cf7..c7657ce7f6 100644
@@ -2107,3 +2107,4 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
sysdeps/unix/sysv/linux/rseq-internal.h new file +88 -0

88 lines not on this page · raw message

sysdeps/unix/sysv/linux/rseq-sym.c new file +43 -0

43 lines not on this page · raw message

sysdeps/unix/sysv/linux/s390/bits/rseq.h new file +37 -0
new file mode 100644
index 0000000000..453250d761
@@ -0,0 +1,37 @@
+/* Restartable Sequences Linux s390 architecture header.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _SYS_RSEQ_H
+# error "Never use <bits/rseq.h> directly; include <sys/rseq.h> instead."
+#endif
+
+/* RSEQ_SIG is a signature required before each abort handler code.
+
+   It is a 32-bit value that maps to actual architecture code compiled
+   into applications and libraries. It needs to be defined for each
+   architecture. When choosing this value, it needs to be taken into
+   account that generating invalid instructions may have ill effects on
+   tools like objdump, and may also have impact on the CPU speculative
+   execution efficiency in some cases.
+
+   RSEQ_SIG uses the trap4 instruction. As Linux does not make use of the
+   access-register mode nor the linkage stack this instruction will always
+   cause a special-operation exception (the trap-enabled bit in the DUCT
+   is and will stay 0). The instruction pattern is
+       b2 ff 0f ff        trap4   4095(%r0)  */
+
+#define RSEQ_SIG	0xB2FF0FFF
sysdeps/unix/sysv/linux/s390/s390-32/ld.abilist +1 -0
index 0576c9575e..bea49478af 100644
@@ -6,3 +6,4 @@ GLIBC_2.0 realloc F
 GLIBC_2.1 __libc_stack_end D 0x4
 GLIBC_2.1 _dl_mcount F
 GLIBC_2.3 __tls_get_offset F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist +1 -0
index 576295deff..4ca893d263 100644
@@ -2175,6 +2175,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 _IO_fprintf F
 GLIBC_2.4 _IO_printf F
 GLIBC_2.4 _IO_sprintf F
sysdeps/unix/sysv/linux/s390/s390-64/ld.abilist +1 -0
index 1fbb890d1d..f11ac862b0 100644
@@ -6,3 +6,4 @@ GLIBC_2.2 free F
 GLIBC_2.2 malloc F
 GLIBC_2.2 realloc F
 GLIBC_2.3 __tls_get_offset F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist +1 -0
index abf0473683..2af0d8b13c 100644
@@ -2079,6 +2079,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 _IO_fprintf F
 GLIBC_2.4 _IO_printf F
 GLIBC_2.4 _IO_sprintf F
sysdeps/unix/sysv/linux/sh/ld.abilist +1 -0
index 0387614d8f..88a5f35dc6 100644
@@ -6,4 +6,5 @@ GLIBC_2.2 free F
 GLIBC_2.2 malloc F
 GLIBC_2.2 realloc F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
 GLIBC_2.4 __stack_chk_guard D 0x4
sysdeps/unix/sysv/linux/sh/libc.abilist +1 -0
index 41977f6e9c..eec216c8d1 100644
@@ -2045,6 +2045,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/sparc/sparc32/ld.abilist +1 -0
index fd0b33f86d..cac31f73af 100644
@@ -6,3 +6,4 @@ GLIBC_2.0 realloc F
 GLIBC_2.1 __libc_stack_end D 0x4
 GLIBC_2.1 _dl_mcount F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist +1 -0
index 3d2f00ca52..bc59a9ee5c 100644
@@ -2169,6 +2169,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 _IO_fprintf F
 GLIBC_2.4 _IO_printf F
 GLIBC_2.4 _IO_sprintf F
sysdeps/unix/sysv/linux/sparc/sparc64/ld.abilist +1 -0
index 82042472c3..97d5962b13 100644
@@ -6,3 +6,4 @@ GLIBC_2.2 free F
 GLIBC_2.2 malloc F
 GLIBC_2.2 realloc F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist +1 -0
index 2f20643e8e..9489cf146e 100644
@@ -2096,6 +2096,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/sys/rseq.h new file +52 -0

52 lines not on this page · raw message

sysdeps/unix/sysv/linux/x86/bits/rseq.h new file +30 -0
new file mode 100644
index 0000000000..a2918c4617
@@ -0,0 +1,30 @@
+/* Restartable Sequences Linux x86 architecture header.
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#ifndef _SYS_RSEQ_H
+# error "Never use <bits/rseq.h> directly; include <sys/rseq.h> instead."
+#endif
+
+/* RSEQ_SIG is a signature required before each abort handler code.
+
+   RSEQ_SIG is used with the following reserved undefined instructions, which
+   trap in user-space:
+
+   x86-32:    0f b9 3d 53 30 05 53      ud1    0x53053053,%edi
+   x86-64:    0f b9 3d 53 30 05 53      ud1    0x53053053(%rip),%edi  */
+
+#define RSEQ_SIG	0x53053053
sysdeps/unix/sysv/linux/x86_64/64/ld.abilist +1 -0
index 0dc9430611..910127139e 100644
@@ -6,3 +6,4 @@ GLIBC_2.2.5 free F
 GLIBC_2.2.5 malloc F
 GLIBC_2.2.5 realloc F
 GLIBC_2.3 __tls_get_addr F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/x86_64/64/libc.abilist +1 -0
index 59f85d9373..da4e2a95d7 100644
@@ -2054,6 +2054,7 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
 GLIBC_2.4 __confstr_chk F
 GLIBC_2.4 __fgets_chk F
 GLIBC_2.4 __fgets_unlocked_chk F
sysdeps/unix/sysv/linux/x86_64/x32/ld.abilist +1 -0
index 80f3161586..e866e0a653 100644
@@ -6,3 +6,4 @@ GLIBC_2.16 calloc F
 GLIBC_2.16 free F
 GLIBC_2.16 malloc F
 GLIBC_2.16 realloc F
+GLIBC_2.31 __rseq_handled D 0x4
sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist +1 -0
index 67a4e238d6..d152b4d217 100644
@@ -2153,3 +2153,4 @@ GLIBC_2.30 getdents64 F
 GLIBC_2.30 gettid F
 GLIBC_2.30 tgkill F
 GLIBC_2.30 twalk_r F
+GLIBC_2.31 __rseq_abi T 0x20
Signature · 1 line
2.17.1
#2 lore raw

* Mathieu Desnoyers:

csu/libc-start.c LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), · line 151

Quoted 7 lines
csu/libc-start.c +7 -0
+#ifdef SHARED
+  if (rtld_active ())
+    {
+      /* Register rseq ABI to the kernel.   */
+      (void) rseq_register_current_thread ();
+    }
+#else

I think this will need *another* check for the inner libc in an audit module. See what we do in malloc. __libc_multiple_libcs is supposed to cover that, but it's unfortunately not reliable.

I believe without that additional check, the first audit module we load will claim rseq, and the main program will not have control over the rseq area. Reversed roles would be desirable here.

In October, I hope to fix __libc_multiple_libcs, and then you can use just that. (We have a Fedora patch that is supposed to fix it, I need to documen the mechanism and upstream it.)

Quoted 8 lines
elf/dl-support.c +8 -0
+/* Advertise Restartable Sequences registration ownership across
+   application and shared libraries.
+
+   Libraries and applications must check whether this variable is zero or
+   non-zero if they wish to perform rseq registration on their own. If it
+   is zero, it means restartable sequence registration is not handled, and
+   the library or application is free to perform rseq registration. In
+   that case, the library or application is taking ownership of rseq

elf/dl-support.c __rtld_lock_define_initialized_recursive (, _dl_load_lock) · line 236

Quoted 10 lines
elf/dl-support.c +10 -0
+   registration, and may set __rseq_handled to 1. It may then set it back
+   to 0 after it completes unregistering rseq.
+
+   If __rseq_handled is found to be non-zero, it means that another
+   library (or the application) is currently handling rseq registration.
+
+   Typical use of __rseq_handled is within library constructors and
+   destructors, or at program startup.  */
+
+int __rseq_handled;

Are there any programs that use __rseq_handled *today*?

I'm less convinced that we actually need this. I don't think we have ever done anything like that before, and I don't think it's necessary. Any secondary rseq library just needs to note if it could perform registration, and if it failed to do so, do not perform unregistration in a pthread destructor callback.

Sure, there's the matter of pthread destructor ordering, but that problem is not different from any other singleton (thread-local or not), and the fix for the last time this has come up (TLS destructors vs dlclose) was to upgrade glibc.

Thanks,
Florian

#3 lore raw
Quoted 14 lines · Florian Weimer

* Mathieu Desnoyers:

csu/libc-start.c LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), · line 151

Quoted 7 lines
csu/libc-start.c +7 -0
+#ifdef SHARED
+  if (rtld_active ())
+    {
+      /* Register rseq ABI to the kernel.   */
+      (void) rseq_register_current_thread ();
+    }
+#else

I think this will need *another* check for the inner libc in an audit module. See what we do in malloc. __libc_multiple_libcs is supposed to cover that, but it's unfortunately not reliable.

Quoted 7 lines

I believe without that additional check, the first audit module we load will claim rseq, and the main program will not have control over the rseq area. Reversed roles would be desirable here.

In October, I hope to fix __libc_multiple_libcs, and then you can use just that. (We have a Fedora patch that is supposed to fix it, I need to documen the mechanism and upstream it.)

This is a technical issue we can resolve.

Quoted 27 lines
Quoted 8 lines
elf/dl-support.c +8 -0
+/* Advertise Restartable Sequences registration ownership across
+   application and shared libraries.
+
+   Libraries and applications must check whether this variable is zero or
+   non-zero if they wish to perform rseq registration on their own. If it
+   is zero, it means restartable sequence registration is not handled, and
+   the library or application is free to perform rseq registration. In
+   that case, the library or application is taking ownership of rseq

elf/dl-support.c __rtld_lock_define_initialized_recursive (, _dl_load_lock) · line 236

Quoted 10 lines
elf/dl-support.c +10 -0
+   registration, and may set __rseq_handled to 1. It may then set it back
+   to 0 after it completes unregistering rseq.
+
+   If __rseq_handled is found to be non-zero, it means that another
+   library (or the application) is currently handling rseq registration.
+
+   Typical use of __rseq_handled is within library constructors and
+   destructors, or at program startup.  */
+
+int __rseq_handled;

Are there any programs that use __rseq_handled *today*?

I'm less convinced that we actually need this. I don't think we have ever done anything like that before, and I don't think it's necessary. Any secondary rseq library just needs to note if it could perform registration, and if it failed to do so, do not perform unregistration in a pthread destructor callback.

Quoted 4 lines

Sure, there's the matter of pthread destructor ordering, but that problem is not different from any other singleton (thread-local or not), and the fix for the last time this has come up (TLS destructors vs dlclose) was to upgrade glibc.

This is a braoder issue.

Mathieu,

It would be easier to merge the patch set if it were just an unconditional registration like we do for set_robust_list().

What's your thought there?

Signature · 2 lines
Cheers,
Carlos.
#4 lore raw

* Carlos O'Donell:

Quoted 2 lines

It would be easier to merge the patch set if it were just an unconditional registration like we do for set_robust_list().

Note that this depends on the in-tree system call numbers list, which I still need to finish according to Joseph's specifications.

(We have something that should work for us as long as we can get large machines from the lab, but I agree that it's not very useful if glibc bot-cycle time is roughly one business day.)

Thanks,
Florian

#5 lore raw
Quoted 7 lines · Florian Weimer

* Carlos O'Donell:

Quoted 2 lines

It would be easier to merge the patch set if it were just an unconditional registration like we do for set_robust_list().

Note that this depends on the in-tree system call numbers list, which I still need to finish according to Joseph's specifications.

Which work is this? Do you have a URL reference to WIP?

Quoted 3 lines

(We have something that should work for us as long as we can get large machines from the lab, but I agree that it's not very useful if glibc bot-cycle time is roughly one business day.)

Yeah, we have to discuss how to accelerate this.

Signature · 2 lines
Cheers,
Carlos.
#6 lore raw

* Carlos O'Donell:

Quoted 10 lines · Florian Weimer
Quoted 7 lines

* Carlos O'Donell:

Quoted 2 lines

It would be easier to merge the patch set if it were just an unconditional registration like we do for set_robust_list().

Note that this depends on the in-tree system call numbers list, which I still need to finish according to Joseph's specifications.

Which work is this? Do you have a URL reference to WIP?

<https://sourceware.org/ml/libc-alpha/2019-05/msg00630.html>
<https://sourceware.org/ml/libc-alpha/2019-06/msg00015.html>

I think realistically this is needed for the Y2038 work as well if we want to support building glibc with older kernel headers. “glibc 2.31 will have Y2038 support and rseq support, but only if it runs on a current and it happens to have been built against sufficiently recent kernel headers” is a bit difficult to explain. The current kernel part is easy enough to understand, but the impact of the kernel headers on the feature set has always been tough to explain. Especially if you factor in vendor kernels with system call backports.

Thanks,
Florian

#7 lore raw

* Florian Weimer:

Quoted 8 lines

* Carlos O'Donell:

Quoted 6 lines · Florian Weimer
Quoted 5 lines

* Carlos O'Donell:

Quoted 2 lines

It would be easier to merge the patch set if it were just an unconditional registration like we do for set_robust_list().

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Note that this depends on the in-tree system call numbers list, which I still need to finish according to Joseph's specifications.

Which work is this? Do you have a URL reference to WIP?

<https://sourceware.org/ml/libc-alpha/2019-05/msg00630.html>
<https://sourceware.org/ml/libc-alpha/2019-06/msg00015.html>

Sorry, there was also this:

<https://sourceware.org/ml/libc-alpha/2019-05/msg00629.html>

I also posted a build-many-glibcs.py patch at some point with an automatic table update, but that still had the massive bot-cycle time.

My current line of thinking is to implement some --use-compilers flag, so that you can build a fresh glibc checkout against an old, pre-built compilers for the system call tables update, and then use the patched glibc sources for one (and hopefully final) bot-cycle.

Thanks,
Florian

#8 lore raw
In reply to Florian Weimer
Quoted 16 lines · Florian Weimer

* Carlos O'Donell:

Quoted 10 lines · Florian Weimer
Quoted 7 lines

* Carlos O'Donell:

Quoted 2 lines

It would be easier to merge the patch set if it were just an unconditional registration like we do for set_robust_list().

Note that this depends on the in-tree system call numbers list, which I still need to finish according to Joseph's specifications.

Which work is this? Do you have a URL reference to WIP?

<https://sourceware.org/ml/libc-alpha/2019-05/msg00630.html>
<https://sourceware.org/ml/libc-alpha/2019-06/msg00015.html>

Quoted 8 lines

I think realistically this is needed for the Y2038 work as well if we want to support building glibc with older kernel headers. “glibc 2..31 will have Y2038 support and rseq support, but only if it runs on a current and it happens to have been built against sufficiently recent kernel headers” is a bit difficult to explain. The current kernel part is easy enough to understand, but the impact of the kernel headers on the feature set has always been tough to explain. Especially if you factor in vendor kernels with system call backports.

I'm in favor of in-tree syscall numbers list. If you don't want O(n) per-arch work, though, you could just define the 'base number' for each arch and use the fact that all the new syscalls share a common numbering (i.e. base + constant depending only on syscall). I think including the list with glibc is more robust though, and would eliminate the need to check for definitions of older (pre-unification) syscalls glibc wants to use.

Rich

#9 lore raw
In reply to Carlos O'Donell
Quoted 19 lines · ----- On Sep 11, 2019, at 3:00 PM, carlos [email protected] wrote:
Quoted 18 lines · Florian Weimer

* Mathieu Desnoyers:

csu/libc-start.c LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), · line 151

Quoted 7 lines
csu/libc-start.c +7 -0
+#ifdef SHARED
+  if (rtld_active ())
+    {
+      /* Register rseq ABI to the kernel.   */
+      (void) rseq_register_current_thread ();
+    }
+#else

I think this will need *another* check for the inner libc in an audit module. See what we do in malloc. __libc_multiple_libcs is supposed to cover that, but it's unfortunately not reliable.

I believe without that additional check, the first audit module we load will claim rseq, and the main program will not have control over the rseq area. Reversed roles would be desirable here.

Quoted 5 lines
Quoted 3 lines

In October, I hope to fix __libc_multiple_libcs, and then you can use just that. (We have a Fedora patch that is supposed to fix it, I need to documen the mechanism and upstream it.)

This is a technical issue we can resolve.

I'm unsure whether there are changes I need to do in my rseq patchset, or if this is a separate issue that will be fixed separately before glibc 2.31 is out, which would then update the rseq bits accordingly ?

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
elf/dl-support.c +12 -0
+/* Advertise Restartable Sequences registration ownership across
+   application and shared libraries.
+
+   Libraries and applications must check whether this variable is zero or
+   non-zero if they wish to perform rseq registration on their own. If it
+   is zero, it means restartable sequence registration is not handled, and
+   the library or application is free to perform rseq registration. In
+   that case, the library or application is taking ownership of rseq
+   registration, and may set __rseq_handled to 1. It may then set it back
+   to 0 after it completes unregistering rseq.
+
+   If __rseq_handled is found to be non-zero, it means that another
Quoted 8 lines
Quoted 8 lines

elf/dl-support.c __rtld_lock_define_initialized_recursive (, _dl_load_lock) · line 236

Quoted 6 lines
elf/dl-support.c +6 -0
+   library (or the application) is currently handling rseq registration.
+
+   Typical use of __rseq_handled is within library constructors and
+   destructors, or at program startup.  */
+
+int __rseq_handled;

Are there any programs that use __rseq_handled *today*?

No, because I told all open source project developers asking whether they can use rseq to wait until we agree on _this_ precise user-level ABI (__rseq_abi and __rseq_handled). Until we agree on this, there _can_ be no users, unless they are willing to suffer conflicts when their application/program is linked against an updated glibc.

Quoted 6 lines
Quoted 6 lines

I'm less convinced that we actually need this. I don't think we have ever done anything like that before, and I don't think it's necessary. Any secondary rseq library just needs to note if it could perform registration, and if it failed to do so, do not perform unregistration in a pthread destructor callback.

If that secondary rseq library happens to try to perform registration within its library constructor (before glibc has performed the __rseq_abi TLS registration), we end up in a situation where the secondary library takes ownership of rseq, even though libc would require ownership. This is a scenario we want to avoid.

Making sure libc reserves ownership through __rseq_handled (which is a non-TLS variable that can be accessed early in the program lifetime) protects against this.

Quoted 6 lines
Quoted 5 lines

Sure, there's the matter of pthread destructor ordering, but that problem is not different from any other singleton (thread-local or not), and the fix for the last time this has come up (TLS destructors vs dlclose) was to upgrade glibc.

Quoted 8 lines

This is a braoder issue.

Mathieu,

It would be easier to merge the patch set if it were just an unconditional registration like we do for set_robust_list().

What's your thought there?

I don't expect set_robust_list was really useful without glibc support. In the current case, rseq can be used by applications and libraries even with older glibc. My goal is to enable such use and not wait for years before end-users upgrade their glibc before rseq can be used.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com
#10 lore raw

* Mathieu Desnoyers:

Quoted 3 lines

I'm unsure whether there are changes I need to do in my rseq patchset, or if this is a separate issue that will be fixed separately before glibc 2.31 is out, which would then update the rseq bits accordingly ?

Someone else (perhaps me) has to fix __libc_multiple_libcs. Then you can use it instead/in addition to the rtld_active check (depending on the semantics we agree upon for __libc_multiple_libcs).

Fixing __libc_multiple_libcs may also address the early initialization issue because for that to be always correct, we need to run the initialization code before ELF constructors.

Quoted 11 lines
Quoted 5 lines
Quoted 5 lines

I'm less convinced that we actually need this. I don't think we have ever done anything like that before, and I don't think it's necessary. Any secondary rseq library just needs to note if it could perform registration, and if it failed to do so, do not perform unregistration in a pthread destructor callback.

If that secondary rseq library happens to try to perform registration within its library constructor (before glibc has performed the __rseq_abi TLS registration), we end up in a situation where the secondary library takes ownership of rseq, even though libc would require ownership. This is a scenario we want to avoid.

We can avoid that if we run the glibc initialization before user code (except IFUNC resolvers). glibc itself doesn't have to do the initialization from an ELF constructor.

Quoted 3 lines

Making sure libc reserves ownership through __rseq_handled (which is a non-TLS variable that can be accessed early in the program lifetime) protects against this.

If that's it's only purpose, I don't think it's necessary. If the kernel can fail the second registration attempt, that would be all the information the alternative rseq implementation needs (plus the matter of destruction).

Thanks,
Florian

#11 lore raw

PATCH 2/5 glibc: sched_getcpu(): use rseq cpu_id TLS on Linux (v5)

In reply to a message not in the archive

When available, use the cpu_id field from __rseq_abi on Linux to implement sched_getcpu(). Fall-back on the vgetcpu vDSO if unavailable.

Benchmarks:

x86-64: Intel E5-2630 [email protected], 16-core, hyperthreading

glibc sched_getcpu():                     13.7 ns (baseline)
glibc sched_getcpu() using rseq:           2.5 ns (speedup:  5.5x)

inline load cpuid from __rseq_abi TLS: 0.8 ns (speedup: 17.1x)

CC Carlos O'Donell <[email protected]>
CC Florian Weimer <[email protected]>
CC Joseph Myers <[email protected]>
CC Szabolcs Nagy <[email protected]>
CC Thomas Gleixner <[email protected]>
CC Ben Maurer <[email protected]>
CC Peter Zijlstra <[email protected]>
CC "Paul E. McKenney" <[email protected]>
CC Boqun Feng <[email protected]>
CC Will Deacon <[email protected]>
CC Dave Watson <[email protected]>
CC Paul Turner <[email protected]>

---
Changes since v1:
- rseq is only used if both __NR_rseq and RSEQ_SIG are defined.

Changes since v2:
- remove duplicated __rseq_abi extern declaration.

Changes since v3:
- update ChangeLog.

Changes since v4:
- Use atomic_load_relaxed to load the __rseq_abi.cpu_id field, a consequence of the fact that __rseq_abi is not volatile anymore.
- Include atomic.h which provides atomic_load_relaxed.

---
 ChangeLog                              |  5 +++++
 sysdeps/unix/sysv/linux/sched_getcpu.c | 25 +++++++++++++++++++++++--
 2 files changed, 28 insertions(+), 2 deletions(-)
ChangeLog +5 -0
index 9a1930248f..128a7b4baa 100644
@@ -1,3 +1,8 @@
+2019-08-06  Mathieu Desnoyers <[email protected]>
+
+	* sysdeps/unix/sysv/linux/sched_getcpu.c: use rseq cpu_id TLS on
+	Linux.
+
 2019-08-06  Mathieu Desnoyers <[email protected]>
 
 	* NEWS: Add Restartable Sequences feature description.
sysdeps/unix/sysv/linux/sched_getcpu.c +23 -2
index fb0d317f83..01a818f5b0 100644
@@ -18,14 +18,15 @@
 #include <errno.h>
 #include <sched.h>
 #include <sysdep.h>
+#include <atomic.h>
 
 #ifdef HAVE_GETCPU_VSYSCALL
 # define HAVE_VSYSCALL
 #endif
 #include <sysdep-vdso.h>
 
-int
-sched_getcpu (void)
+static int
+vsyscall_sched_getcpu (void)
 {
 #ifdef __NR_getcpu
   unsigned int cpu;
@@ -37,3 +38,23 @@ sched_getcpu (void)
   return -1;
 #endif
 }
+
+#ifdef __NR_rseq
+#include <sys/rseq.h>
+#endif
+
+#if defined __NR_rseq && defined RSEQ_SIG
+int
+sched_getcpu (void)
+{
+  int cpu_id = atomic_load_relaxed (&__rseq_abi.cpu_id);
+
+  return cpu_id >= 0 ? cpu_id : vsyscall_sched_getcpu ();
+}
+#else
+int
+sched_getcpu (void)
+{
+  return vsyscall_sched_getcpu ();
+}
+#endif
Signature · 1 line
2.17.1