public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3 0/4] Simplify internal single-threaded usage
@ 2022-06-21 20:32 Adhemerval Zanella
  2022-06-21 20:32 ` [PATCH v3 1/4] misc: Optimize internal usage of __libc_single_threaded Adhemerval Zanella
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Adhemerval Zanella @ 2022-06-21 20:32 UTC (permalink / raw)
  To: libc-alpha, Wilco Dijkstra, Fangrui Song

The glibc currently has three different internal ways to check if a
process is single-threaded: the exported global variable
__libc_single_threaded, the internal-only __libc_multiple_threads, and
the variant used by some architectures and allocated on TCB, 
multiple_threads.  Also each port can define SINGLE_THREAD_BY_GLOBAL
to either use __libc_multiple_threads or multiple_threads.

The __libc_single_threaded and __libc_multiple_threads have essentially
the same semantic: both are global variables where the value is not
reset if/when the process becomes multi-threaded.  The issue of using
__libc_single_threaded internally is since it is accessed through copy
relocation, both values must be updated.  This is fixed in the first
patch.

The second patch replaces __libc_multiple_threads with
__libc_single_threaded, while also fixing a bug where architectures that
define SINGLE_THREAD_BY_GLOBAL does not enable the optimization.

The third patch replaces multiple_threads with __libc_single_threaded,
to simplify a possible single-thread lock optimization.  On most
architectures, accessing an internal global variable should be as fast
as through the TCB (it seems that only legacy ABIs that require extra
code sequence to materialize global access, such as i686 and sparc,
using the TCB would be faster, however it is mitigated when the
SINGLE_THREAD_P is accessed in large code blocks).

The x86 is only architecture that optimizes the lock access directly by
reimplementing atomic operations.  In this case, the affected
implementations are rewritten to use SINGLE_THREAD_P macro, while some
unused macros are just removed (for instance atomic_add_zero).  The idea
is to just phase out this specific atomic implementation in favor of
compiler builtins and move the single-thread optimization to be
arch-neutral.

In the last patch just remove the single-thread.h header and move the
definition to internal sys/single_threaded.h, so now there is only
one place to add such optimization.

v3:
* Replace __libc_dlsym with a new macro to define both internal and
  external symbol (STV_HIDDEN and STV_DEFAULT), so pthread_create
  can update both.
* Add more SINGLE_THREAD_P usage now that it is based on
  __libc_single_threaded on all architectures.
* Fixed i686 compare and swap macro refactor.

v2:
* Add RTLD_DEFAULT support for __libc_dlsym and use it instead of
  ___dlsym.
* Simplify the x86 atomic macros.

Adhemerval Zanella (4):
  misc: Optimize internal usage of __libc_single_threaded
  Replace __libc_multiple_threads with __libc_single_threaded
  Remove usage of TLS_MULTIPLE_THREADS_IN_TCB
  Remove single-thread.h

 elf/libc_early_init.c                       |   2 +-
 include/libc-symbols.h                      |  22 +
 include/sys/single_threaded.h               |  20 +-
 misc/single_threaded.c                      |   2 +
 misc/tst-atomic.c                           |   1 +
 nptl/Makefile                               |   1 -
 nptl/allocatestack.c                        |  12 -
 nptl/descr.h                                |  17 +-
 nptl/libc_multiple_threads.c                |  28 --
 nptl/pthread_cancel.c                       |   9 +-
 nptl/pthread_create.c                       |  10 +-
 posix/fork.c                                |   2 +-
 sysdeps/generic/single-thread.h             |  25 -
 sysdeps/i386/htl/tcb-offsets.sym            |   1 -
 sysdeps/i386/nptl/tcb-offsets.sym           |   1 -
 sysdeps/i386/nptl/tls.h                     |   4 +-
 sysdeps/ia64/nptl/tcb-offsets.sym           |   1 -
 sysdeps/ia64/nptl/tls.h                     |   2 -
 sysdeps/mach/hurd/i386/tls.h                |   4 +-
 sysdeps/mach/hurd/sysdep-cancel.h           |   5 -
 sysdeps/nios2/nptl/tcb-offsets.sym          |   1 -
 sysdeps/nptl/setxid.h                       |   2 +-
 sysdeps/or1k/nptl/tls.h                     |   2 -
 sysdeps/powerpc/nptl/tcb-offsets.sym        |   3 -
 sysdeps/powerpc/nptl/tls.h                  |   3 -
 sysdeps/s390/nptl/tcb-offsets.sym           |   1 -
 sysdeps/s390/nptl/tls.h                     |   6 +-
 sysdeps/sh/nptl/tcb-offsets.sym             |   1 -
 sysdeps/sh/nptl/tls.h                       |   2 -
 sysdeps/sparc/nptl/tcb-offsets.sym          |   1 -
 sysdeps/sparc/nptl/tls.h                    |   2 +-
 sysdeps/unix/sysdep.h                       |   2 +-
 sysdeps/unix/sysv/linux/aarch64/sysdep.h    |   2 -
 sysdeps/unix/sysv/linux/alpha/sysdep.h      |   2 -
 sysdeps/unix/sysv/linux/arc/sysdep.h        |   2 -
 sysdeps/unix/sysv/linux/arm/sysdep.h        |   2 -
 sysdeps/unix/sysv/linux/hppa/sysdep.h       |   2 -
 sysdeps/unix/sysv/linux/microblaze/sysdep.h |   2 -
 sysdeps/unix/sysv/linux/s390/sysdep.h       |   3 -
 sysdeps/unix/sysv/linux/single-thread.h     |  44 --
 sysdeps/unix/sysv/linux/x86_64/sysdep.h     |   2 -
 sysdeps/x86/atomic-machine.h                | 488 ++++++--------------
 sysdeps/x86_64/nptl/tcb-offsets.sym         |   1 -
 sysdeps/x86_64/nptl/tls.h                   |   2 +-
 44 files changed, 199 insertions(+), 548 deletions(-)
 delete mode 100644 nptl/libc_multiple_threads.c
 delete mode 100644 sysdeps/generic/single-thread.h
 delete mode 100644 sysdeps/unix/sysv/linux/single-thread.h

-- 
2.34.1


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-07-05 11:43 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-21 20:32 [PATCH v3 0/4] Simplify internal single-threaded usage Adhemerval Zanella
2022-06-21 20:32 ` [PATCH v3 1/4] misc: Optimize internal usage of __libc_single_threaded Adhemerval Zanella
2022-06-23  4:15   ` Fangrui Song
2022-06-23 14:05     ` Adhemerval Zanella
2022-06-21 20:32 ` [PATCH v3 2/4] Replace __libc_multiple_threads with __libc_single_threaded Adhemerval Zanella
2022-06-22 11:04   ` Yann Droneaud
2022-06-22 12:24     ` Adhemerval Zanella
2022-06-22 13:53       ` Yann Droneaud
2022-07-05 11:34   ` Wilco Dijkstra
2022-06-21 20:32 ` [PATCH v3 3/4] Remove usage of TLS_MULTIPLE_THREADS_IN_TCB Adhemerval Zanella
2022-06-21 20:32 ` [PATCH v3 4/4] Remove single-thread.h Adhemerval Zanella
2022-07-05 11:43   ` Wilco Dijkstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).