Linux: Use rseq in sched_getcpu if available (v9)
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/3 glibc: Perform rseq registration at C startup and thread creation (v22)
Register rseq 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
Those are based on glibc master branch commit 3ee1e0ec5c. The rseq system call was merged into Linux 4.18.
The TLS_STATIC_SURPLUS define is increased to leave additional room for dlopen'd initial-exec TLS, which keeps elf/tst-auditmany working.
The increase (76 bytes) is larger than 32 bytes because it has not been increased in quite a while. The cost in terms of additional TLS storage is quite significant, but it will also obscure some initial-exec-related dlopen failures.
---
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.
Changes since v12:
- Remove __rseq_handled,
- Rely on OS implicit rseq unregistration on thread teardown,
- Register main thread in __libc_early_init ().
- Add Restartable Sequences entry to threads manual.
Changes since v13:
- Update following be/le abilist split for arm, microblaze, and sh.
- Update manual to add the __rseq_abi variable and RSEQ_SIG macro to generate manual index entries, and add missing "Restartable Sequences" menu entry to the threads chapter.
Changes since v14:
- Update copyright range to include 2020.
- Introduce __ASSUME_RSEQ defined for --enable-kernel=4.18.0 and higher.
- Use ifdef __ASSUME_RSEQ rather than ifdef __NR_rseq to discover rseq availability. This is necessary now that the system call numbers are integrated within glibc.
Changes since v15:
- Remove __ASSUME_RSEQ from kernel features.
- rseq internal: remove assume rseq
- remove assume rseq and struct rseq def from sysdeps/unix/sysv/linux/rseq-sym.c
- sys/rseq.h: detect rseq header, implement fallback
- sysdeps/unix/sysv/linux/sys/rseq.h include cdefs.h, add _Static_assert to validate struct rseq and struct rseq_cs alignment.
- sys/rseq.h: document that posix_memalign should be used rather than malloc if allocating struct rseq or struct rseq_cs on the heap. This is required to guarantee 32-byte alignement.
Changes since v16:
- Move rseq NEWS entry under 2.32.
- Move new __rseq_abi symbol to GLIBC_2.32.
Changes since v17:
- Change copyright year to 2020.
- Refer to GNU C Library manual rather than rseq manpage in NEWS.
- Use "initial" parameter from __libc_early_init ().
- Manual: rseq is Linux rather than GNU std.
- Remove rseq_unregister_current_thread () (unused).
- rseq_register_current_thread () returns void.
- Coding style fixes.
- sys/rseq.h: use "32" for alignment.
- Change http:// for https:// in comments.
- Add const struct rseq_cs * field to rseq_cs union.
Changes since v18:
- NEWS update.
- Manual update.
- Move misc/rseq-internal.h to sysdeps/generic/rseq-internal.h.
- Fix coding style in sysdeps/unix/sysv/linux/sys/rseq.h.
- Abort libc if __rseq_abi is already initialized in rseq_register_current_thread (), which is unexpected.
- Abort libc in rseq_register_current_thread () on all errno except EPERM (seccomp), EACCES (seccomp), and ENOSYS (not implemented).
Changes since v19:
- Take care of feedback from Florian.
- Update manual to remove uptr field, which is removed from sys/rseq.h.
- Rebase on current master branch.
Changes since v20:
- Rebase on current glibc master.
- Update TLS_STATIC_SURPLUS value.
- Use attribute __aligned__ to have same approach as Linux UAPI header.
- Update manual to include link to librseq rseq.2 system call man page.
Changes since v21:
- Update manual following feedback from Florian.
- Remove static assert and alignof from sys/rseq.h to align with current Linux UAPI header, based on feedback from Florian.
--- NEWS | 10 + elf/dl-tls.c | 2 +- elf/libc_early_init.c | 4 + manual/threads.texi | 64 ++++++ nptl/pthread_create.c | 13 ++ sysdeps/generic/rseq-internal.h | 26 +++ sysdeps/unix/sysv/linux/Makefile | 5 +- sysdeps/unix/sysv/linux/Versions | 1 + sysdeps/unix/sysv/linux/aarch64/bits/rseq.h | 43 ++++ sysdeps/unix/sysv/linux/aarch64/libc.abilist | 1 + sysdeps/unix/sysv/linux/alpha/libc.abilist | 1 + sysdeps/unix/sysv/linux/arm/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/arm/bits/rseq.h | 83 +++++++ sysdeps/unix/sysv/linux/arm/le/libc.abilist | 1 + sysdeps/unix/sysv/linux/bits/rseq.h | 29 +++ sysdeps/unix/sysv/linux/csky/libc.abilist | 1 + sysdeps/unix/sysv/linux/hppa/libc.abilist | 1 + sysdeps/unix/sysv/linux/i386/libc.abilist | 1 + sysdeps/unix/sysv/linux/ia64/libc.abilist | 1 + .../sysv/linux/m68k/coldfire/libc.abilist | 1 + .../unix/sysv/linux/m68k/m680x0/libc.abilist | 1 + .../sysv/linux/microblaze/be/libc.abilist | 1 + .../sysv/linux/microblaze/le/libc.abilist | 1 + sysdeps/unix/sysv/linux/mips/bits/rseq.h | 62 ++++++ .../sysv/linux/mips/mips32/fpu/libc.abilist | 1 + .../sysv/linux/mips/mips32/nofpu/libc.abilist | 1 + .../sysv/linux/mips/mips64/n32/libc.abilist | 1 + .../sysv/linux/mips/mips64/n64/libc.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 + .../powerpc/powerpc32/nofpu/libc.abilist | 1 + .../linux/powerpc/powerpc64/be/libc.abilist | 1 + .../linux/powerpc/powerpc64/le/libc.abilist | 1 + .../unix/sysv/linux/riscv/rv64/libc.abilist | 1 + sysdeps/unix/sysv/linux/rseq-internal.h | 73 +++++++ sysdeps/unix/sysv/linux/rseq-sym.c | 26 +++ sysdeps/unix/sysv/linux/s390/bits/rseq.h | 37 ++++ .../unix/sysv/linux/s390/s390-32/libc.abilist | 1 + .../unix/sysv/linux/s390/s390-64/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/be/libc.abilist | 1 + sysdeps/unix/sysv/linux/sh/le/libc.abilist | 1 + .../sysv/linux/sparc/sparc32/libc.abilist | 1 + .../sysv/linux/sparc/sparc64/libc.abilist | 1 + sysdeps/unix/sysv/linux/sys/rseq.h | 203 ++++++++++++++++++ sysdeps/unix/sysv/linux/x86/bits/rseq.h | 30 +++ .../unix/sysv/linux/x86_64/64/libc.abilist | 1 + .../unix/sysv/linux/x86_64/x32/libc.abilist | 1 + 48 files changed, 775 insertions(+), 3 deletions(-) create mode 100644 sysdeps/generic/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
NEWS +10 -0
@@ -31,6 +31,16 @@ Major new features: pthread_attr_getsigmask_np have been added. They allow applications to specify the signal mask of a thread created with pthread_create. +* Support for automatically registering threads with the Linux rseq + 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 support by using the documented ABI. + The GNU C Library manual has details on integration of Restartable + Sequences. + Deprecated and removed features, and other changes affecting compatibility: * The deprecated <sys/sysctl.h> header and the sysctl function have been
elf/dl-tls.c +1 -1
@@ -31,7 +31,7 @@/* Amount of excess space to allocate in the static TLS area to allow dynamic loading of modules defining IE-model TLS data. */ -#define TLS_STATIC_SURPLUS 64 + DL_NNS * 100 +#define TLS_STATIC_SURPLUS 64 + DL_NNS * 176/* Out-of-memory handler. */
elf/libc_early_init.c +4 -0
@@ -18,10 +18,14 @@#include <ctype.h> #include <libc-early-init.h> +#include <rseq-internal.h> void __libc_early_init (_Bool initial) {/* Initialize ctype data. */ __ctype_init (); +/* Register rseq ABI to the kernel for the main program's libc. */ +if (initial) +rseq_register_current_thread (); }
manual/threads.texi +64 -0
76 lines not on this page · raw message
nptl/pthread_create.c +13 -0
@@ -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>
@@ -384,6 +385,9 @@ START_THREAD_DEFN/* Initialize pointers to locale data. */ __ctype_init (); +/* Register rseq TLS to the kernel. */ +rseq_register_current_thread (); +#ifndef __ASSUME_SET_ROBUST_LIST if (__set_robust_list_avail >= 0)#endif
@@ -580,6 +584,15 @@ START_THREAD_DEFNprocess is really dead since 'clone' got passed the CLONE_CHILD_CLEARTID flag. The 'tid' field in the TCB will be set to zero. +rseq TLS is still registered at this point. Rely on implicit +unregistration performed by the kernel on thread teardown. This is not a +problem because the rseq TLS lives on the stack, and the stack outlives +the thread. If TCB allocation is ever changed, additional steps may be +required, such as performing explicit rseq unregistration before +reclaiming the rseq TLS area memory. It is NOT sufficient to block +signals because the kernel may write to the rseq area even without +signals. +The exit code is zero since in case all threads exit by calling 'pthread_exit' the exit status must be 0 (zero). */ __exit_thread ();
sysdeps/generic/rseq-internal.h new file +26 -0
@@ -0,0 +1,26 @@ +/* Restartable Sequences internal API. Stub version. +Copyright (C) 2020 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 +<https://www.gnu.org/licenses/>. */ + +#ifndef RSEQ_INTERNAL_H +#define RSEQ_INTERNAL_H + +static inline void +rseq_register_current_thread (void ) +{ +} + +#endif /* rseq-internal.h */
sysdeps/unix/sysv/linux/Makefile +3 -2
@@ -41,7 +41,7 @@ update-syscall-lists: arch-syscall.h endif ifeq ($(subdir),csu) -sysdep_routines += errno-loc +sysdep_routines += errno-loc rseq-sym endif ifeq ($(subdir),assert)
@@ -92,7 +92,8 @@ sysdep_headers += sys/mount.h sys/acct.h \ bits/termios-c_lflag.h bits/termios-tcflow.h \ bits/termios-misc.h \ bits/types/struct_semid_ds.h \ - bits/ipc-perm.h + bits/ipc-perm.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 +1 -0
@@ -178,6 +178,7 @@ libc { getdents64; gettid; tgkill; } GLIBC_2.32 { + __rseq_abi; } GLIBC_PRIVATE { # functions used in other libraries
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/libc.abilist +1 -0
@@ -2149,6 +2149,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/alpha/libc.abilist +1 -0
@@ -2231,6 +2231,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/arm/be/libc.abilist +1 -0
@@ -133,6 +133,7 @@ GLIBC_2.30 twalk_r F GLIBC_2.31 msgctl F GLIBC_2.31 semctl F GLIBC_2.31 shmctl F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np 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/le/libc.abilist +1 -0
@@ -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.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/bits/rseq.h new file +29 -0
@@ -0,0 +1,29 @@ +/* Restartable Sequences architecture header. Stub version. +Copyright (C) 2020 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 +<https://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/libc.abilist +1 -0
@@ -2093,6 +2093,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/hppa/libc.abilist +1 -0
@@ -2052,6 +2052,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/i386/libc.abilist +1 -0
@@ -2218,6 +2218,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/ia64/libc.abilist +1 -0
@@ -2084,6 +2084,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist +1 -0
@@ -134,6 +134,7 @@ GLIBC_2.30 twalk_r F GLIBC_2.31 msgctl F GLIBC_2.31 semctl F GLIBC_2.31 shmctl F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist +1 -0
@@ -2164,6 +2164,7 @@ GLIBC_2.30 twalk_r F GLIBC_2.31 msgctl F GLIBC_2.31 semctl F GLIBC_2.31 shmctl F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/microblaze/be/libc.abilist +1 -0
@@ -2144,6 +2144,7 @@ GLIBC_2.30 twalk_r F GLIBC_2.31 msgctl F GLIBC_2.31 semctl F GLIBC_2.31 shmctl F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/microblaze/le/libc.abilist +1 -0
@@ -2141,6 +2141,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
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
@@ -2135,6 +2135,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist +1 -0
@@ -2133,6 +2133,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist +1 -0
@@ -2141,6 +2141,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist +1 -0
@@ -2135,6 +2135,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/nios2/libc.abilist +1 -0
@@ -2182,6 +2182,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/powerpc/bits/rseq.h new file +37 -0
@@ -0,0 +1,37 @@ +/* Restartable Sequences Linux powerpc architecture header. +Copyright (C) 2020 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 +<https://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
@@ -2191,6 +2191,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist +1 -0
@@ -2224,6 +2224,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libc.abilist +1 -0
@@ -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.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libc.abilist +1 -0
@@ -2291,6 +2291,7 @@ GLIBC_2.32 __qecvtieee128_r F GLIBC_2.32 __qfcvtieee128 F GLIBC_2.32 __qfcvtieee128_r F GLIBC_2.32 __qgcvtieee128 F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 __scanfieee128 F GLIBC_2.32 __snprintf_chkieee128 F GLIBC_2.32 __snprintfieee128 F
sysdeps/unix/sysv/linux/riscv/rv64/libc.abilist +1 -0
@@ -2111,6 +2111,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/rseq-internal.h new file +73 -0
73 lines not on this page · raw message
sysdeps/unix/sysv/linux/rseq-sym.c new file +26 -0
@@ -0,0 +1,26 @@ +/* Restartable Sequences exported symbols. Linux Implementation. +Copyright (C) 2020 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 +<https://www.gnu.org/licenses/>. */ + +#include <sys/syscall.h> +#include <stdint.h> +#include <kernel-features.h> +#include <sys/rseq.h> + +__threadstruct rseq __rseq_abi = + { + .cpu_id =RSEQ_CPU_ID_UNINITIALIZED , + };
sysdeps/unix/sysv/linux/s390/bits/rseq.h new file +37 -0
@@ -0,0 +1,37 @@ +/* Restartable Sequences Linux s390 architecture header. +Copyright (C) 2020 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 +<https://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/libc.abilist +1 -0
@@ -2189,6 +2189,7 @@ GLIBC_2.30 twalk_r F GLIBC_2.31 msgctl F GLIBC_2.31 semctl F GLIBC_2.31 shmctl F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist +1 -0
@@ -2090,6 +2090,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/sh/be/libc.abilist +1 -0
@@ -2059,6 +2059,7 @@ GLIBC_2.30 twalk_r F GLIBC_2.31 msgctl F GLIBC_2.31 semctl F GLIBC_2.31 shmctl F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/sh/le/libc.abilist +1 -0
@@ -2056,6 +2056,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist +1 -0
@@ -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.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist +1 -0
@@ -2107,6 +2107,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/sys/rseq.h new file +203 -0
203 lines not on this page · raw message
sysdeps/unix/sysv/linux/x86/bits/rseq.h new file +30 -0
@@ -0,0 +1,30 @@ +/* Restartable Sequences Linux x86 architecture header. +Copyright (C) 2020 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 +<https://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/libc.abilist +1 -0
@@ -2065,6 +2065,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist +1 -0
@@ -2162,6 +2162,7 @@ GLIBC_2.30 getdents64 F GLIBC_2.30 gettid F GLIBC_2.30 tgkill F GLIBC_2.30 twalk_r F +GLIBC_2.32 __rseq_abi T 0x20 GLIBC_2.32 pthread_attr_getsigmask_np F GLIBC_2.32 pthread_attr_setaffinity_np F GLIBC_2.32 pthread_attr_setsigmask_np F
Signature · 1 line
2.17.1
Re: [PATCH 1/3] glibc: Perform rseq registration at C startup and thread creation (v22)
* Mathieu Desnoyers via Libc-alpha:
Quoted 9 lines
Register rseq 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/647Those are based on glibc master branch commit
3ee1e0ec5c. The rseq system call was merged into Linux 4.18.
Quoted 7 lines
The
TLS_STATIC_SURPLUSdefine is increased to leave additional room for dlopen'd initial-exec TLS, which keeps elf/tst-auditmany working.The increase (76 bytes) is larger than 32 bytes because it has not been increased in quite a while. The cost in terms of additional TLS storage is quite significant, but it will also obscure some initial-exec-related dlopen failures.
We need another change to get this working on most non-x86 architectures:
elf/dl-tls.c +6 -0
@@ -134,6 +134,12 @@ void_dl_determine_tlsoffset (void) {size_t max_align =TLS_TCB_ALIGN ; +/* libc.so with rseq has TLS with 32-byte alignment. Since TLS is +initialized before audit modules are loaded and slotinfo +information is available, this is not taken into account below in +the audit case. */ + max_align =MAX (max_align,32U ); +size_t freetop =0 ;size_t freebottom =0 ;
This isn't visible on x86-64 because TLS_TCB_ALIGN is already 64 there.
I plan to re-test with this fix and push the series.
Carlos, is it okay if I fold in the dl-tls.c change if testing looks good?
Thanks,
Florian
Re: [PATCH 1/3] glibc: Perform rseq registration at C startup and thread creation (v22)
Quoted 41 lines · Florian Weimer
* Mathieu Desnoyers via Libc-alpha:
Quoted 9 lines
Register rseq 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/647Those are based on glibc master branch commit
3ee1e0ec5c. The rseq system call was merged into Linux 4.18.Quoted 7 lines
The
TLS_STATIC_SURPLUSdefine is increased to leave additional room for dlopen'd initial-exec TLS, which keeps elf/tst-auditmany working.The increase (76 bytes) is larger than 32 bytes because it has not been increased in quite a while. The cost in terms of additional TLS storage is quite significant, but it will also obscure some initial-exec-related dlopen failures.
We need another change to get this working on most non-x86 architectures:
elf/dl-tls.c +6
-0@@ -134,6 +134,12 @@ void_dl_determine_tlsoffset (void) {size_t max_align =TLS_TCB_ALIGN ; +/* libc.so with rseq has TLS with 32-byte alignment. Since TLS is +initialized before audit modules are loaded and slotinfo +information is available, this is not taken into account below in +the audit case. */ + max_align =MAX (max_align,32U ); +size_t freetop =0 ;size_t freebottom =0 ;This isn't visible on x86-64 because
TLS_TCB_ALIGNis already 64 there.
Quoted 4 lines
I plan to re-test with this fix and push the series.
Carlos, is it okay if I fold in the
dl-tls.cchange if testing looks good?
I have reviewed the above and I think it is the correct *pragmatic* fix.
The reality is that to fix this fully you must use a two stage loading process to pre-examine all audit modules *before* setting the fundamental alignment of the TCB. This isn't easy with the current loader framework. Therefore the above is a good pragmatic solution.
There is always going to be a bit of a chicken and an egg situation. We want to provide a fundamental alignment requirement but we haven't yet seen all the requirements on alignment. So the best we could do is look over DT_NEEDED, DT_AUDIT, LD_PRELOAD, etc. get the best answer and then fail any subsequent dlopen's that load objects with higher fundamental requirements for alignment of the TCB.
The audit modules are problematic becuase they are loaded *before* anything else is loaded, *before* we've examined any of the actual objects we're about to load because they can influence the search paths. Again, this means the above solution is a perfect pragmatic choice. The real solution is to rearchitect the early audit module loading into two stages and that's work we can do later.
OK with the above change.
Signature · 2 lines
Cheers, Carlos.
PATCH 2/3 Linux: Use rseq in sched_getcpu if available (v9)
In reply to a message not in the archiveWhen 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)
---
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.
Changes since v5:
- Use __ASSUME_RSEQ to detect rseq availability.
Changes since v6:
- Remove use of __ASSUME_RSEQ.
Changes since v7:
- Fix incorrect merge with commit d0def09ff6 ("linux: Fix vDSO macros build with time64 interfaces")
Changes since v8:
- Update patch title.
- Add /* RSEQ_SIG */ for #else and #endif.
--- sysdeps/unix/sysv/linux/sched_getcpu.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
sysdeps/unix/sysv/linux/sched_getcpu.c +20 -2
@@ -18,10 +18,12 @@#include <errno.h> #include <sched.h> #include <sysdep.h> +#include <atomic.h> #include <sysdep-vdso.h> +#include <sys/rseq.h> -int -sched_getcpu (void ) +static int +vsyscall_sched_getcpu (void ) {unsigned int cpu;int r =-1 ;
@@ -32,3 +34,19 @@ sched_getcpu (void)#endif return r ==-1 ? r : cpu; } + +#ifdef 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 /* RSEQ_SIG */ +int +sched_getcpu (void ) +{ +return vsyscall_sched_getcpu (); +} +#endif /* RSEQ_SIG */
Signature · 1 line
2.17.1
* Mathieu Desnoyers:
Quoted 3 lines
When available, use the
cpu_idfield from__rseq_abion Linux to implementsched_getcpu(). Fall-back on the vgetcpu vDSO if unavailable.
I've pushed this to glibc master, but unfortunately it looks like this exposes a kernel bug related to affinity mask changes.
After building and testing glibc, this
for x in {1..2000} ; do posix/tst-affinity-static & done
produces some “error:” lines for me:
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 138, expected 0
error: Unexpected CPU 138, expected 0
error: Unexpected CPU 138, expected 0
error: Unexpected CPU 138, expected 0
“expected 0” is a result of how the test has been written, it bails out on the first failure, which happens with CPU ID 0.
Smaller systems can use a smaller count than 2000 to reproduce this. It also happens sporadically when running the glibc test suite itself (which is why it took further testing to reveal this issue).
I can reproduce this with the Debian 4.19.118-2+deb10u1 kernel, the Fedora 5.6.19-300.fc32 kernel, and the Red Hat Enterprise Linux kernel 4.18.0-193.el8 (all x86_64).
As to the cause, I'd guess that the exit path in the sched_setaffinity system call fails to update the rseq area, so that userspace can observe the outdated CPU ID there.
Thanks,
Florian
Quoted 31 lines · ----- On Jul 6, 2020, at 9:59 AM, Florian Weimer [email protected] wrote:
* Mathieu Desnoyers:
Quoted 3 lines
When available, use the
cpu_idfield from__rseq_abion Linux to implementsched_getcpu(). Fall-back on the vgetcpu vDSO if unavailable.I've pushed this to glibc master, but unfortunately it looks like this exposes a kernel bug related to affinity mask changes.
After building and testing glibc, this
for x in {1..2000} ; do posix/tst-affinity-static & done
produces some “error:” lines for me:
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 138, expected 0
error: Unexpected CPU 138, expected 0
error: Unexpected CPU 138, expected 0
error: Unexpected CPU 138, expected 0“expected 0” is a result of how the test has been written, it bails out on the first failure, which happens with CPU ID 0.
Smaller systems can use a smaller count than 2000 to reproduce this. It also happens sporadically when running the glibc test suite itself (which is why it took further testing to reveal this issue).
Quoted 7 lines
I can reproduce this with the Debian 4.19.118-2+deb10u1 kernel, the Fedora 5.6.19-300.fc32 kernel, and the Red Hat Enterprise Linux kernel 4.18.0-193.el8 (all
x86_64).As to the cause, I'd guess that the exit path in the
sched_setaffinitysystem call fails to update the rseq area, so that userspace can observe the outdated CPU ID there.
Hi Florian,
We have a similar test in Linux, see tools/testing/selftests/rseq/basic_test.c. That test does not trigger this issue, even when executed repeatedly.
I'll investigate further what is happening within the glibc test.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
Quoted 41 lines · ----- On Jul 6, 2020, at 10:49 AM, Mathieu Desnoyers…
Quoted 31 lines · ----- On Jul 6, 2020, at 9:59 AM, Florian Weimer [email protected] wrote:
* Mathieu Desnoyers:
Quoted 3 lines
When available, use the
cpu_idfield from__rseq_abion Linux to implementsched_getcpu(). Fall-back on the vgetcpu vDSO if unavailable.I've pushed this to glibc master, but unfortunately it looks like this exposes a kernel bug related to affinity mask changes.
After building and testing glibc, this
for x in {1..2000} ; do posix/tst-affinity-static & done
produces some “error:” lines for me:
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 2, expected 0
error: Unexpected CPU 138, expected 0
error: Unexpected CPU 138, expected 0
error: Unexpected CPU 138, expected 0
error: Unexpected CPU 138, expected 0“expected 0” is a result of how the test has been written, it bails out on the first failure, which happens with CPU ID 0.
Smaller systems can use a smaller count than 2000 to reproduce this. It also happens sporadically when running the glibc test suite itself (which is why it took further testing to reveal this issue).
Quoted 7 lines
I can reproduce this with the Debian 4.19.118-2+deb10u1 kernel, the Fedora 5.6.19-300.fc32 kernel, and the Red Hat Enterprise Linux kernel 4.18.0-193.el8 (all
x86_64).As to the cause, I'd guess that the exit path in the
sched_setaffinitysystem call fails to update the rseq area, so that userspace can observe the outdated CPU ID there.
Quoted 6 lines
Hi Florian,
We have a similar test in Linux, see
tools/testing/selftests/rseq/basic_test.c. That test does not trigger this issue, even when executed repeatedly.I'll investigate further what is happening within the glibc test.
Here are the missing kernel bits. Just adding those in wake_up_new_task() is enough to make the problem go away, but to err on the safe side I'm planning to add an rseq_migrate within sched_fork:
kernel/sched/core.c +2 -0
@@ -3043,6 +3043,7 @@ int sched_fork(unsigned long clone_flags, struct task_struct *p)* so use __set_task_cpu(). */ __set_task_cpu (p,smp_processor_id ()); +rseq_migrate (p);if (p->sched_class->task_fork) p->sched_class->task_fork (p);raw_spin_unlock_irqrestore (&p->pi_lock, flags);
@@ -3103,6 +3104,7 @@ void wake_up_new_task(struct task_struct *p)*/ p->recent_used_cpu =task_cpu (p);__set_task_cpu (p,select_task_rq (p,task_cpu (p),SD_BALANCE_FORK ,0 )); +rseq_migrate (p);#endif rq =__task_rq_lock (p, &rf);update_rq_clock (rq);
I'm not sure why your test catches it fairly quickly but ours does not. That's a good catch.
Now we need to discuss how we introduce that fix in a way that will allow user-space to trust the __rseq_abi.cpu_id field's content.
The usual approach to kernel bug fixing is typically to push the fix, mark it for stable kernels, and expect everyone to pick up the fixes. I wonder how comfortable glibc would be to replace its sched_getcpu implementation with a broken-until-fixed kernel rseq implementation without any mechanism in place to know whether it can trust the value of the cpu_id field. I am extremely reluctant to do so.
One possible way to introduce this fix would be to use the rseq flags argument to allow glibc to query whether the kernel implements a rseq with a cpu_id field it can trust. So glibc could do, for registration:
ret = INTERNAL_SYSCALL_CALL (rseq, &__rseq_abi, sizeof (struct rseq),
RSEQ_FLAG_REGISTER | RSEQ_FLAG_RELIABLE_CPU_ID, RSEQ_SIG);
(I'm open to bike-shedding the actual flag name)
So if the kernel does not implement the fix, the registration would return EINVAL. When getting EINVAL like this, we could then re-issue the rseq syscall:
ret = INTERNAL_SYSCALL_CALL (rseq, NULL, 0, RSEQ_FLAG_RELIABLE_CPU_ID, 0);
to check whether EINVAL has been caused by other invalid system call parameters, or it's really because the RSEQ_FLAG_RELIABLE_CPU_ID flag is unknown. Being able to distinguish between invalid parameters and unknown flags like this would end up requiring one extra system call on failed registration attempt on kernels which implement a broken rseq.
This also means glibc would only start really activating rseq on kernels implementing this fix.
Thoughts ?
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
* Mathieu Desnoyers:
Quoted 2 lines
Now we need to discuss how we introduce that fix in a way that will allow user-space to trust the __rseq_abi.cpu_id field's content.
I don't think that's necessary. We can mention it in the glibc distribution notes on the wiki.
Quoted 7 lines
The usual approach to kernel bug fixing is typically to push the fix, mark it for stable kernels, and expect everyone to pick up the fixes. I wonder how comfortable glibc would be to replace its
sched_getcpuimplementation with a broken-until-fixed kernel rseq implementation without any mechanism in place to know whether it can trust the value of thecpu_idfield. I am extremely reluctant to do so.
We have already had similar regressions in sched_getcpu, and we didn't put anything into glibc to deal with those.
Just queue the fix for the stable kernels. I expect that all distributions track stable kernel branches in some way, so just put into the kernel commit message that this commit is needed for a working sched_getcpu in glibc 2.32 and later.
Once the upstream fix is in Linus' tree, I'm going to file a request to backport the fix into the Red Hat Enterprise Linux 8.
Thanks for finding the root cause so quickly,
Florian
Quoted 16 lines · ----- On Jul 6, 2020, at 1:50 PM, Florian Weimer [email protected] wrote:
* Mathieu Desnoyers:
Quoted 2 lines
Now we need to discuss how we introduce that fix in a way that will allow user-space to trust the __rseq_abi.cpu_id field's content.
I don't think that's necessary. We can mention it in the glibc distribution notes on the wiki.
Quoted 7 lines
The usual approach to kernel bug fixing is typically to push the fix, mark it for stable kernels, and expect everyone to pick up the fixes. I wonder how comfortable glibc would be to replace its
sched_getcpuimplementation with a broken-until-fixed kernel rseq implementation without any mechanism in place to know whether it can trust the value of thecpu_idfield. I am extremely reluctant to do so.
Quoted 2 lines
We have already had similar regressions in
sched_getcpu, and we didn't put anything into glibc to deal with those.
Was that acceptable because having a wrong cpu number would never trigger corruption, only slowdowns ?
In the case of rseq, having the wrong cpu_id value is a real issue which will lead to corruption and crashes. So I maintain my reluctance to introduce the fix without any way for userspace to know whether the cpu_id field value is reliable.
What were the reasons why it was OK to have this kind of regression in sched_getcpu in the past, and are they still valid in the context of rseq ?
Thanks,
Mathieu
Quoted 11 lines
Just queue the fix for the stable kernels. I expect that all distributions track stable kernel branches in some way, so just put into the kernel commit message that this commit is needed for a working
sched_getcpuin glibc 2.32 and later.Once the upstream fix is in Linus' tree, I'm going to file a request to backport the fix into the Red Hat Enterprise Linux 8.
Thanks for finding the root cause so quickly,
Florian
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com
* Mathieu Desnoyers:
Quoted 18 lines
Quoted 16 lines · ----- On Jul 6, 2020, at 1:50 PM, Florian Weimer [email protected] wrote:
* Mathieu Desnoyers:
Quoted 2 lines
Now we need to discuss how we introduce that fix in a way that will allow user-space to trust the __rseq_abi.cpu_id field's content.
I don't think that's necessary. We can mention it in the glibc distribution notes on the wiki.
Quoted 7 lines
The usual approach to kernel bug fixing is typically to push the fix, mark it for stable kernels, and expect everyone to pick up the fixes. I wonder how comfortable glibc would be to replace its
sched_getcpuimplementation with a broken-until-fixed kernel rseq implementation without any mechanism in place to know whether it can trust the value of thecpu_idfield. I am extremely reluctant to do so.
Quoted 5 lines
Quoted 2 lines
We have already had similar regressions in
sched_getcpu, and we didn't put anything into glibc to deal with those.Was that acceptable because having a wrong cpu number would never trigger corruption, only slowdowns ?
First of all, it's a kernel bug. It's rare that we put workarounds for kernel bugs into glibc.
And yes, in pretty much all cases it's just a performance issue for sched_getcpu. When you know the CPU ID of a thread due to pinning to a single CPU, why would you call sched_getcpu? (That's the case where you could get corruption in theory.)
Quoted 4 lines
In the case of rseq, having the wrong
cpu_idvalue is a real issue which will lead to corruption and crashes. So I maintain my reluctance to introduce the fix without any way for userspace to know whether thecpu_idfield value is reliable.
Yes, for rseq itself, the scenario is somewhat different. Still, it's just another kernel bug. There will be others. 8-/
From a schedule point of view, it looks tough to get the magic flag into the mainline kernel in time for the upcoming glibc 2.32 release. If you insist on registering rseq only if the bug is not present, we'll probably have to back out some or all of the rseq changes.
Thanks,
Florian
Quoted 39 lines · ----- On Jul 6, 2020, at 2:11 PM, Florian Weimer [email protected] wrote:
* Mathieu Desnoyers:
Quoted 18 lines
Quoted 16 lines · ----- On Jul 6, 2020, at 1:50 PM, Florian Weimer [email protected] wrote:
* Mathieu Desnoyers:
Quoted 2 lines
Now we need to discuss how we introduce that fix in a way that will allow user-space to trust the __rseq_abi.cpu_id field's content.
I don't think that's necessary. We can mention it in the glibc distribution notes on the wiki.
Quoted 7 lines
The usual approach to kernel bug fixing is typically to push the fix, mark it for stable kernels, and expect everyone to pick up the fixes. I wonder how comfortable glibc would be to replace its
sched_getcpuimplementation with a broken-until-fixed kernel rseq implementation without any mechanism in place to know whether it can trust the value of thecpu_idfield. I am extremely reluctant to do so.Quoted 5 lines
Quoted 2 lines
We have already had similar regressions in
sched_getcpu, and we didn't put anything into glibc to deal with those.Was that acceptable because having a wrong cpu number would never trigger corruption, only slowdowns ?
First of all, it's a kernel bug. It's rare that we put workarounds for kernel bugs into glibc.
And yes, in pretty much all cases it's just a performance issue for
sched_getcpu. When you know the CPU ID of a thread due to pinning to a single CPU, why would you callsched_getcpu? (That's the case where you could get corruption in theory.)Quoted 4 lines
In the case of rseq, having the wrong
cpu_idvalue is a real issue which will lead to corruption and crashes. So I maintain my reluctance to introduce the fix without any way for userspace to know whether thecpu_idfield value is reliable.
Quoted 7 lines
Yes, for rseq itself, the scenario is somewhat different. Still, it's just another kernel bug. There will be others. 8-/
From a schedule point of view, it looks tough to get the magic flag into the mainline kernel in time for the upcoming glibc 2.32 release. If you insist on registering rseq only if the bug is not present, we'll probably have to back out some or all of the rseq changes.
I've just submitted the fix and a the new rseq flag as RFC to lkml:
https://lore.kernel.org/lkml/[email protected]/
Let's see how quickly we can come to an agreement on this on the kernel side.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com