e354a5
commit ffb17e7ba3a5ba9632cee97330b325072fbe41dd
e354a5
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
e354a5
Date:   Wed Jun 10 13:40:40 2020 +0100
e354a5
e354a5
    rtld: Avoid using up static TLS surplus for optimizations [BZ #25051]
e354a5
    
e354a5
    On some targets static TLS surplus area can be used opportunistically
e354a5
    for dynamically loaded modules such that the TLS access then becomes
e354a5
    faster (TLSDESC and powerpc TLS optimization). However we don't want
e354a5
    all surplus TLS to be used for this optimization because dynamically
e354a5
    loaded modules with initial-exec model TLS can only use surplus TLS.
e354a5
    
e354a5
    The new contract for surplus static TLS use is:
e354a5
    
e354a5
    - libc.so can have up to 192 bytes of IE TLS,
e354a5
    - other system libraries together can have up to 144 bytes of IE TLS.
e354a5
    - Some "optional" static TLS is available for opportunistic use.
e354a5
    
e354a5
    The optional TLS is now tunable: rtld.optional_static_tls, so users
e354a5
    can directly affect the allocated static TLS size. (Note that module
e354a5
    unloading with dlclose does not reclaim static TLS. After the optional
e354a5
    TLS runs out, TLS access is no longer optimized to use static TLS.)
e354a5
    
e354a5
    The default setting of rtld.optional_static_tls is 512 so the surplus
e354a5
    TLS is 3*192 + 4*144 + 512 = 1664 by default, the same as before.
e354a5
    
e354a5
    Fixes BZ #25051.
e354a5
    
e354a5
    Tested on aarch64-linux-gnu and x86_64-linux-gnu.
e354a5
    
e354a5
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
e354a5
e354a5
Conflicts:
e354a5
	elf/Makefile
e354a5
	  (Missing __libc_single_threaded downstream.)
e354a5
e354a5
diff --git a/csu/libc-tls.c b/csu/libc-tls.c
e354a5
index 6f2a47dc86222407..76aa1b98ea059a43 100644
e354a5
--- a/csu/libc-tls.c
e354a5
+++ b/csu/libc-tls.c
e354a5
@@ -62,6 +62,9 @@ size_t _dl_tls_static_align;
e354a5
    loaded modules with IE-model TLS or for TLSDESC optimization.
e354a5
    See comments in elf/dl-tls.c where it is initialized.  */
e354a5
 size_t _dl_tls_static_surplus;
e354a5
+/* Remaining amount of static TLS that may be used for optimizing
e354a5
+   dynamic TLS access (e.g. with TLSDESC).  */
e354a5
+size_t _dl_tls_static_optional;
e354a5
 
e354a5
 /* Generation counter for the dtv.  */
e354a5
 size_t _dl_tls_generation;
e354a5
diff --git a/elf/Makefile b/elf/Makefile
e354a5
index cbced7605ebe2443..8b96bfefd852b79f 100644
e354a5
--- a/elf/Makefile
e354a5
+++ b/elf/Makefile
e354a5
@@ -197,7 +197,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
e354a5
 	 tst-auditmany tst-initfinilazyfail \
e354a5
 	 tst-dlopenfail tst-dlopenfail-2 \
e354a5
 	 tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \
e354a5
-	 tst-audit14 tst-audit15 tst-audit16
e354a5
+	 tst-audit14 tst-audit15 tst-audit16 \
e354a5
+	 tst-tls-ie tst-tls-ie-dlmopen
e354a5
 #	 reldep9
e354a5
 tests-internal += loadtest unload unload2 circleload1 \
e354a5
 	 neededtest neededtest2 neededtest3 neededtest4 \
e354a5
@@ -313,7 +314,10 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
e354a5
 		tst-dlopenfailmod1 tst-dlopenfaillinkmod tst-dlopenfailmod2 \
e354a5
 		tst-dlopenfailmod3 \
e354a5
 		tst-filterobj-flt tst-filterobj-aux tst-filterobj-filtee \
e354a5
-		tst-auditlogmod-1 tst-auditlogmod-2 tst-auditlogmod-3
e354a5
+		tst-auditlogmod-1 tst-auditlogmod-2 tst-auditlogmod-3 \
e354a5
+		tst-tls-ie-mod0 tst-tls-ie-mod1 tst-tls-ie-mod2 \
e354a5
+		tst-tls-ie-mod3 tst-tls-ie-mod4 tst-tls-ie-mod5 \
e354a5
+		tst-tls-ie-mod6
e354a5
 
e354a5
 # Most modules build with _ISOMAC defined, but those filtered out
e354a5
 # depend on internal headers.
e354a5
@@ -1690,3 +1694,23 @@ $(objpfx)tst-auxobj: $(objpfx)tst-filterobj-aux.so
e354a5
 $(objpfx)tst-auxobj-dlopen: $(libdl)
e354a5
 $(objpfx)tst-auxobj.out: $(objpfx)tst-filterobj-filtee.so
e354a5
 $(objpfx)tst-auxobj-dlopen.out: $(objpfx)tst-filterobj-filtee.so
e354a5
+
e354a5
+$(objpfx)tst-tls-ie: $(libdl) $(shared-thread-library)
e354a5
+$(objpfx)tst-tls-ie.out: \
e354a5
+  $(objpfx)tst-tls-ie-mod0.so \
e354a5
+  $(objpfx)tst-tls-ie-mod1.so \
e354a5
+  $(objpfx)tst-tls-ie-mod2.so \
e354a5
+  $(objpfx)tst-tls-ie-mod3.so \
e354a5
+  $(objpfx)tst-tls-ie-mod4.so \
e354a5
+  $(objpfx)tst-tls-ie-mod5.so \
e354a5
+  $(objpfx)tst-tls-ie-mod6.so
e354a5
+
e354a5
+$(objpfx)tst-tls-ie-dlmopen: $(libdl) $(shared-thread-library)
e354a5
+$(objpfx)tst-tls-ie-dlmopen.out: \
e354a5
+  $(objpfx)tst-tls-ie-mod0.so \
e354a5
+  $(objpfx)tst-tls-ie-mod1.so \
e354a5
+  $(objpfx)tst-tls-ie-mod2.so \
e354a5
+  $(objpfx)tst-tls-ie-mod3.so \
e354a5
+  $(objpfx)tst-tls-ie-mod4.so \
e354a5
+  $(objpfx)tst-tls-ie-mod5.so \
e354a5
+  $(objpfx)tst-tls-ie-mod6.so
e354a5
diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c
e354a5
index afeace4d3e49180c..c6139b89d4ecddc8 100644
e354a5
--- a/elf/dl-reloc.c
e354a5
+++ b/elf/dl-reloc.c
e354a5
@@ -39,13 +39,16 @@
e354a5
 /* We are trying to perform a static TLS relocation in MAP, but it was
e354a5
    dynamically loaded.  This can only work if there is enough surplus in
e354a5
    the static TLS area already allocated for each running thread.  If this
e354a5
-   object's TLS segment is too big to fit, we fail.  If it fits,
e354a5
-   we set MAP->l_tls_offset and return.
e354a5
-   This function intentionally does not return any value but signals error
e354a5
-   directly, as static TLS should be rare and code handling it should
e354a5
-   not be inlined as much as possible.  */
e354a5
+   object's TLS segment is too big to fit, we fail with -1.  If it fits,
e354a5
+   we set MAP->l_tls_offset and return 0.
e354a5
+   A portion of the surplus static TLS can be optionally used to optimize
e354a5
+   dynamic TLS access (with TLSDESC or powerpc TLS optimizations).
e354a5
+   If OPTIONAL is true then TLS is allocated for such optimization and
e354a5
+   the caller must have a fallback in case the optional portion of surplus
e354a5
+   TLS runs out.  If OPTIONAL is false then the entire surplus TLS area is
e354a5
+   considered and the allocation only fails if that runs out.  */
e354a5
 int
e354a5
-_dl_try_allocate_static_tls (struct link_map *map)
e354a5
+_dl_try_allocate_static_tls (struct link_map *map, bool optional)
e354a5
 {
e354a5
   /* If we've already used the variable with dynamic access, or if the
e354a5
      alignment requirements are too high, fail.  */
e354a5
@@ -68,8 +71,14 @@ _dl_try_allocate_static_tls (struct link_map *map)
e354a5
 
e354a5
   size_t n = (freebytes - blsize) / map->l_tls_align;
e354a5
 
e354a5
-  size_t offset = GL(dl_tls_static_used) + (freebytes - n * map->l_tls_align
e354a5
-					    - map->l_tls_firstbyte_offset);
e354a5
+  /* Account optional static TLS surplus usage.  */
e354a5
+  size_t use = freebytes - n * map->l_tls_align - map->l_tls_firstbyte_offset;
e354a5
+  if (optional && use > GL(dl_tls_static_optional))
e354a5
+    goto fail;
e354a5
+  else if (optional)
e354a5
+    GL(dl_tls_static_optional) -= use;
e354a5
+
e354a5
+  size_t offset = GL(dl_tls_static_used) + use;
e354a5
 
e354a5
   map->l_tls_offset = GL(dl_tls_static_used) = offset;
e354a5
 #elif TLS_DTV_AT_TP
e354a5
@@ -83,6 +92,13 @@ _dl_try_allocate_static_tls (struct link_map *map)
e354a5
   if (used > GL(dl_tls_static_size))
e354a5
     goto fail;
e354a5
 
e354a5
+  /* Account optional static TLS surplus usage.  */
e354a5
+  size_t use = used - GL(dl_tls_static_used);
e354a5
+  if (optional && use > GL(dl_tls_static_optional))
e354a5
+    goto fail;
e354a5
+  else if (optional)
e354a5
+    GL(dl_tls_static_optional) -= use;
e354a5
+
e354a5
   map->l_tls_offset = offset;
e354a5
   map->l_tls_firstbyte_offset = GL(dl_tls_static_used);
e354a5
   GL(dl_tls_static_used) = used;
e354a5
@@ -110,12 +126,15 @@ _dl_try_allocate_static_tls (struct link_map *map)
e354a5
   return 0;
e354a5
 }
e354a5
 
e354a5
+/* This function intentionally does not return any value but signals error
e354a5
+   directly, as static TLS should be rare and code handling it should
e354a5
+   not be inlined as much as possible.  */
e354a5
 void
e354a5
 __attribute_noinline__
e354a5
 _dl_allocate_static_tls (struct link_map *map)
e354a5
 {
e354a5
   if (map->l_tls_offset == FORCED_DYNAMIC_TLS_OFFSET
e354a5
-      || _dl_try_allocate_static_tls (map))
e354a5
+      || _dl_try_allocate_static_tls (map, false))
e354a5
     {
e354a5
       _dl_signal_error (0, map->l_name, NULL, N_("\
e354a5
 cannot allocate memory in static TLS block"));
e354a5
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
e354a5
index cfda76f6de96df57..4f8c35b7d37bfc18 100644
e354a5
--- a/elf/dl-tls.c
e354a5
+++ b/elf/dl-tls.c
e354a5
@@ -60,8 +60,6 @@
e354a5
    This should be large enough to cover runtime libraries of the
e354a5
    compiler such as libgomp and libraries in libc other than libc.so.  */
e354a5
 #define OTHER_IE_TLS 144
e354a5
-/* Size of additional surplus TLS, placeholder for TLS optimizations.  */
e354a5
-#define OPT_SURPLUS_TLS 512
e354a5
 
e354a5
 /* Calculate the size of the static TLS surplus, when the given
e354a5
    number of audit modules are loaded.  Must be called after the
e354a5
@@ -69,13 +67,15 @@
e354a5
 void
e354a5
 _dl_tls_static_surplus_init (size_t naudit)
e354a5
 {
e354a5
-  size_t nns;
e354a5
+  size_t nns, opt_tls;
e354a5
 
e354a5
 #if HAVE_TUNABLES
e354a5
   nns = TUNABLE_GET (nns, size_t, NULL);
e354a5
+  opt_tls = TUNABLE_GET (optional_static_tls, size_t, NULL);
e354a5
 #else
e354a5
   /* Default values of the tunables.  */
e354a5
   nns = 4;
e354a5
+  opt_tls = 512;
e354a5
 #endif
e354a5
   if (nns > DL_NNS)
e354a5
     nns = DL_NNS;
e354a5
@@ -84,9 +84,10 @@ _dl_tls_static_surplus_init (size_t naudit)
e354a5
 		      (unsigned long) naudit, (unsigned long) (DL_NNS - nns));
e354a5
   nns += naudit;
e354a5
 
e354a5
+  GL(dl_tls_static_optional) = opt_tls;
e354a5
   GLRO(dl_tls_static_surplus) = ((nns - 1) * LIBC_IE_TLS
e354a5
 				 + nns * OTHER_IE_TLS
e354a5
-				 + OPT_SURPLUS_TLS);
e354a5
+				 + opt_tls);
e354a5
 }
e354a5
 
e354a5
 /* Out-of-memory handler.  */
e354a5
diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list
e354a5
index 7337fb85062c91a7..6408a8e5ae92d2c6 100644
e354a5
--- a/elf/dl-tunables.list
e354a5
+++ b/elf/dl-tunables.list
e354a5
@@ -134,5 +134,10 @@ glibc {
e354a5
       maxval: 16
e354a5
       default: 4
e354a5
     }
e354a5
+    optional_static_tls {
e354a5
+      type: SIZE_T
e354a5
+      minval: 0
e354a5
+      default: 512
e354a5
+    }
e354a5
   }
e354a5
 }
e354a5
diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h
e354a5
index 9e9d5a3b28bc06c5..2fc3c91b7defe84e 100644
e354a5
--- a/elf/dynamic-link.h
e354a5
+++ b/elf/dynamic-link.h
e354a5
@@ -40,9 +40,10 @@
e354a5
     (__builtin_expect ((sym_map)->l_tls_offset				\
e354a5
 		       != FORCED_DYNAMIC_TLS_OFFSET, 1)			\
e354a5
      && (__builtin_expect ((sym_map)->l_tls_offset != NO_TLS_OFFSET, 1)	\
e354a5
-	 || _dl_try_allocate_static_tls (sym_map) == 0))
e354a5
+	 || _dl_try_allocate_static_tls (sym_map, true) == 0))
e354a5
 
e354a5
-int _dl_try_allocate_static_tls (struct link_map *map) attribute_hidden;
e354a5
+int _dl_try_allocate_static_tls (struct link_map *map, bool optional)
e354a5
+  attribute_hidden;
e354a5
 
e354a5
 #include <elf.h>
e354a5
 
e354a5
diff --git a/elf/tst-tls-ie-dlmopen.c b/elf/tst-tls-ie-dlmopen.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..c7b5c688e362c861
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie-dlmopen.c
e354a5
@@ -0,0 +1,112 @@
e354a5
+/* Test dlopen of modules with initial-exec TLS after dlmopen.
e354a5
+   Copyright (C) 2016-2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+/* This test tries to check that surplus static TLS is not used up for
e354a5
+   dynamic TLS optimizations and 4*144 = 576 bytes of static TLS is
e354a5
+   still available for dlopening modules with initial-exec TLS after 3
e354a5
+   new dlmopen namespaces are created.  It depends on rtld.nns=4 and
e354a5
+   rtld.optional_static_tls=512 tunable settings.  */
e354a5
+
e354a5
+#include <errno.h>
e354a5
+#include <pthread.h>
e354a5
+#include <stdio.h>
e354a5
+#include <stdlib.h>
e354a5
+#include <string.h>
e354a5
+
e354a5
+static int do_test (void);
e354a5
+#include <support/xthread.h>
e354a5
+#include <support/xdlfcn.h>
e354a5
+#include <support/check.h>
e354a5
+#include <support/test-driver.c>
e354a5
+
e354a5
+/* Have some big TLS in the main exe: should not use surplus TLS.  */
e354a5
+__thread char maintls[1000];
e354a5
+
e354a5
+static pthread_barrier_t barrier;
e354a5
+
e354a5
+/* Forces multi-threaded behaviour.  */
e354a5
+static void *
e354a5
+blocked_thread_func (void *closure)
e354a5
+{
e354a5
+  xpthread_barrier_wait (&barrier);
e354a5
+  /* TLS load and access tests run here in the main thread.  */
e354a5
+  xpthread_barrier_wait (&barrier);
e354a5
+  return NULL;
e354a5
+}
e354a5
+
e354a5
+static void *
e354a5
+load_and_access (Lmid_t lmid, const char *mod, const char *func)
e354a5
+{
e354a5
+  /* Load module with TLS.  */
e354a5
+  void *p = xdlmopen (lmid, mod, RTLD_NOW);
e354a5
+  /* Access the TLS variable to ensure it is allocated.  */
e354a5
+  void (*f) (void) = (void (*) (void))xdlsym (p, func);
e354a5
+  f ();
e354a5
+  return p;
e354a5
+}
e354a5
+
e354a5
+static int
e354a5
+do_test (void)
e354a5
+{
e354a5
+  void *mods[5];
e354a5
+
e354a5
+  {
e354a5
+    int ret = pthread_barrier_init (&barrier, NULL, 2);
e354a5
+    if (ret != 0)
e354a5
+      {
e354a5
+        errno = ret;
e354a5
+        printf ("error: pthread_barrier_init: %m\n");
e354a5
+        exit (1);
e354a5
+      }
e354a5
+  }
e354a5
+
e354a5
+  pthread_t blocked_thread = xpthread_create (NULL, blocked_thread_func, NULL);
e354a5
+  xpthread_barrier_wait (&barrier);
e354a5
+
e354a5
+  printf ("maintls[%zu]:\t %p .. %p\n",
e354a5
+	   sizeof maintls, maintls, maintls + sizeof maintls);
e354a5
+  memset (maintls, 1, sizeof maintls);
e354a5
+
e354a5
+  /* Load modules with dynamic TLS (use surplus static TLS for libc
e354a5
+     in new namespaces and may be for TLS optimizations too).  */
e354a5
+  mods[0] = load_and_access (LM_ID_BASE, "tst-tls-ie-mod0.so", "access0");
e354a5
+  mods[1] = load_and_access (LM_ID_NEWLM, "tst-tls-ie-mod1.so", "access1");
e354a5
+  mods[2] = load_and_access (LM_ID_NEWLM, "tst-tls-ie-mod2.so", "access2");
e354a5
+  mods[3] = load_and_access (LM_ID_NEWLM, "tst-tls-ie-mod3.so", "access3");
e354a5
+  /* Load modules with initial-exec TLS (can only use surplus static TLS).  */
e354a5
+  mods[4] = load_and_access (LM_ID_BASE, "tst-tls-ie-mod6.so", "access6");
e354a5
+
e354a5
+  /* Here 576 bytes + 3 * libc use of surplus static TLS is in use so less
e354a5
+     than 1024 bytes are available (exact number depends on TLS optimizations
e354a5
+     and the libc TLS use).  */
e354a5
+  printf ("The next dlmopen should fail...\n");
e354a5
+  void *p = dlmopen (LM_ID_BASE, "tst-tls-ie-mod4.so", RTLD_NOW);
e354a5
+  if (p != NULL)
e354a5
+    FAIL_EXIT1 ("error: expected dlmopen to fail because there is "
e354a5
+		"not enough surplus static TLS.\n");
e354a5
+  printf ("...OK failed with: %s.\n", dlerror ());
e354a5
+
e354a5
+  xpthread_barrier_wait (&barrier);
e354a5
+  xpthread_join (blocked_thread);
e354a5
+
e354a5
+  /* Close the modules.  */
e354a5
+  for (int i = 0; i < 5; ++i)
e354a5
+    xdlclose (mods[i]);
e354a5
+
e354a5
+  return 0;
e354a5
+}
e354a5
diff --git a/elf/tst-tls-ie-mod.h b/elf/tst-tls-ie-mod.h
e354a5
new file mode 100644
e354a5
index 0000000000000000..46b362a9b783d214
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie-mod.h
e354a5
@@ -0,0 +1,40 @@
e354a5
+/* Module with specified TLS size and model.
e354a5
+   Copyright (C) 2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+/* This file is parameterized by macros N, SIZE and MODEL.  */
e354a5
+
e354a5
+#include <stdio.h>
e354a5
+#include <string.h>
e354a5
+
e354a5
+#define CONCATX(x, y) x ## y
e354a5
+#define CONCAT(x, y) CONCATX (x, y)
e354a5
+#define STRX(x) #x
e354a5
+#define STR(x) STRX (x)
e354a5
+
e354a5
+#define VAR CONCAT (var, N)
e354a5
+
e354a5
+__attribute__ ((aligned (8), tls_model (MODEL)))
e354a5
+__thread char VAR[SIZE];
e354a5
+
e354a5
+void
e354a5
+CONCAT (access, N) (void)
e354a5
+{
e354a5
+  printf (STR (VAR) "[%d]:\t %p .. %p " MODEL "\n", SIZE, VAR, VAR + SIZE);
e354a5
+  fflush (stdout);
e354a5
+  memset (VAR, 1, SIZE);
e354a5
+}
e354a5
diff --git a/elf/tst-tls-ie-mod0.c b/elf/tst-tls-ie-mod0.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..2450686e400e1141
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie-mod0.c
e354a5
@@ -0,0 +1,4 @@
e354a5
+#define N 0
e354a5
+#define SIZE 480
e354a5
+#define MODEL "global-dynamic"
e354a5
+#include "tst-tls-ie-mod.h"
e354a5
diff --git a/elf/tst-tls-ie-mod1.c b/elf/tst-tls-ie-mod1.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..849ff91e53b0a518
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie-mod1.c
e354a5
@@ -0,0 +1,4 @@
e354a5
+#define N 1
e354a5
+#define SIZE 120
e354a5
+#define MODEL "global-dynamic"
e354a5
+#include "tst-tls-ie-mod.h"
e354a5
diff --git a/elf/tst-tls-ie-mod2.c b/elf/tst-tls-ie-mod2.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..23915ab67bab0ada
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie-mod2.c
e354a5
@@ -0,0 +1,4 @@
e354a5
+#define N 2
e354a5
+#define SIZE 24
e354a5
+#define MODEL "global-dynamic"
e354a5
+#include "tst-tls-ie-mod.h"
e354a5
diff --git a/elf/tst-tls-ie-mod3.c b/elf/tst-tls-ie-mod3.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..5395f844a5999ea9
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie-mod3.c
e354a5
@@ -0,0 +1,4 @@
e354a5
+#define N 3
e354a5
+#define SIZE 16
e354a5
+#define MODEL "global-dynamic"
e354a5
+#include "tst-tls-ie-mod.h"
e354a5
diff --git a/elf/tst-tls-ie-mod4.c b/elf/tst-tls-ie-mod4.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..93ac2eacae292d86
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie-mod4.c
e354a5
@@ -0,0 +1,4 @@
e354a5
+#define N 4
e354a5
+#define SIZE 1024
e354a5
+#define MODEL "initial-exec"
e354a5
+#include "tst-tls-ie-mod.h"
e354a5
diff --git a/elf/tst-tls-ie-mod5.c b/elf/tst-tls-ie-mod5.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..84b3fd285b5b5a3e
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie-mod5.c
e354a5
@@ -0,0 +1,4 @@
e354a5
+#define N 5
e354a5
+#define SIZE 128
e354a5
+#define MODEL "initial-exec"
e354a5
+#include "tst-tls-ie-mod.h"
e354a5
diff --git a/elf/tst-tls-ie-mod6.c b/elf/tst-tls-ie-mod6.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..c736bf0684f3b08f
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie-mod6.c
e354a5
@@ -0,0 +1,4 @@
e354a5
+#define N 6
e354a5
+#define SIZE 576
e354a5
+#define MODEL "initial-exec"
e354a5
+#include "tst-tls-ie-mod.h"
e354a5
diff --git a/elf/tst-tls-ie.c b/elf/tst-tls-ie.c
e354a5
new file mode 100644
e354a5
index 0000000000000000..2dc0894480417389
e354a5
--- /dev/null
e354a5
+++ b/elf/tst-tls-ie.c
e354a5
@@ -0,0 +1,111 @@
e354a5
+/* Test dlopen of modules with initial-exec TLS.
e354a5
+   Copyright (C) 2016-2020 Free Software Foundation, Inc.
e354a5
+   This file is part of the GNU C Library.
e354a5
+
e354a5
+   The GNU C Library is free software; you can redistribute it and/or
e354a5
+   modify it under the terms of the GNU Lesser General Public
e354a5
+   License as published by the Free Software Foundation; either
e354a5
+   version 2.1 of the License, or (at your option) any later version.
e354a5
+
e354a5
+   The GNU C Library is distributed in the hope that it will be useful,
e354a5
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
e354a5
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
e354a5
+   Lesser General Public License for more details.
e354a5
+
e354a5
+   You should have received a copy of the GNU Lesser General Public
e354a5
+   License along with the GNU C Library; if not, see
e354a5
+   <https://www.gnu.org/licenses/>.  */
e354a5
+
e354a5
+/* This test tries to check that surplus static TLS is not used up for
e354a5
+   dynamic TLS optimizations and 3*192 + 4*144 = 1152 bytes of static
e354a5
+   TLS is available for dlopening modules with initial-exec TLS.  It
e354a5
+   depends on rtld.nns=4 and rtld.optional_static_tls=512 tunable setting.  */
e354a5
+
e354a5
+#include <errno.h>
e354a5
+#include <pthread.h>
e354a5
+#include <stdio.h>
e354a5
+#include <stdlib.h>
e354a5
+#include <string.h>
e354a5
+
e354a5
+static int do_test (void);
e354a5
+#include <support/xthread.h>
e354a5
+#include <support/xdlfcn.h>
e354a5
+#include <support/check.h>
e354a5
+#include <support/test-driver.c>
e354a5
+
e354a5
+/* Have some big TLS in the main exe: should not use surplus TLS.  */
e354a5
+__thread char maintls[1000];
e354a5
+
e354a5
+static pthread_barrier_t barrier;
e354a5
+
e354a5
+/* Forces multi-threaded behaviour.  */
e354a5
+static void *
e354a5
+blocked_thread_func (void *closure)
e354a5
+{
e354a5
+  xpthread_barrier_wait (&barrier);
e354a5
+  /* TLS load and access tests run here in the main thread.  */
e354a5
+  xpthread_barrier_wait (&barrier);
e354a5
+  return NULL;
e354a5
+}
e354a5
+
e354a5
+static void *
e354a5
+load_and_access (const char *mod, const char *func)
e354a5
+{
e354a5
+  /* Load module with TLS.  */
e354a5
+  void *p = xdlopen (mod, RTLD_NOW);
e354a5
+  /* Access the TLS variable to ensure it is allocated.  */
e354a5
+  void (*f) (void) = (void (*) (void))xdlsym (p, func);
e354a5
+  f ();
e354a5
+  return p;
e354a5
+}
e354a5
+
e354a5
+static int
e354a5
+do_test (void)
e354a5
+{
e354a5
+  void *mods[6];
e354a5
+
e354a5
+  {
e354a5
+    int ret = pthread_barrier_init (&barrier, NULL, 2);
e354a5
+    if (ret != 0)
e354a5
+      {
e354a5
+        errno = ret;
e354a5
+        printf ("error: pthread_barrier_init: %m\n");
e354a5
+        exit (1);
e354a5
+      }
e354a5
+  }
e354a5
+
e354a5
+  pthread_t blocked_thread = xpthread_create (NULL, blocked_thread_func, NULL);
e354a5
+  xpthread_barrier_wait (&barrier);
e354a5
+
e354a5
+  printf ("maintls[%zu]:\t %p .. %p\n",
e354a5
+	   sizeof maintls, maintls, maintls + sizeof maintls);
e354a5
+  memset (maintls, 1, sizeof maintls);
e354a5
+
e354a5
+  /* Load modules with dynamic TLS (may use surplus static TLS
e354a5
+     opportunistically).  */
e354a5
+  mods[0] = load_and_access ("tst-tls-ie-mod0.so", "access0");
e354a5
+  mods[1] = load_and_access ("tst-tls-ie-mod1.so", "access1");
e354a5
+  mods[2] = load_and_access ("tst-tls-ie-mod2.so", "access2");
e354a5
+  mods[3] = load_and_access ("tst-tls-ie-mod3.so", "access3");
e354a5
+  /* Load modules with initial-exec TLS (can only use surplus static TLS).  */
e354a5
+  mods[4] = load_and_access ("tst-tls-ie-mod4.so", "access4");
e354a5
+  mods[5] = load_and_access ("tst-tls-ie-mod5.so", "access5");
e354a5
+
e354a5
+  /* Here 1152 bytes of surplus static TLS is in use and at most 512 bytes
e354a5
+     are available (depending on TLS optimizations).  */
e354a5
+  printf ("The next dlopen should fail...\n");
e354a5
+  void *p = dlopen ("tst-tls-ie-mod6.so", RTLD_NOW);
e354a5
+  if (p != NULL)
e354a5
+    FAIL_EXIT1 ("error: expected dlopen to fail because there is "
e354a5
+		"not enough surplus static TLS.\n");
e354a5
+  printf ("...OK failed with: %s.\n", dlerror ());
e354a5
+
e354a5
+  xpthread_barrier_wait (&barrier);
e354a5
+  xpthread_join (blocked_thread);
e354a5
+
e354a5
+  /* Close the modules.  */
e354a5
+  for (int i = 0; i < 6; ++i)
e354a5
+    xdlclose (mods[i]);
e354a5
+
e354a5
+  return 0;
e354a5
+}
e354a5
diff --git a/manual/tunables.texi b/manual/tunables.texi
e354a5
index e6a3e9a2cf5c959c..bd737b5d57080462 100644
e354a5
--- a/manual/tunables.texi
e354a5
+++ b/manual/tunables.texi
e354a5
@@ -249,6 +249,23 @@ increase the per-thread memory usage as necessary, so this tunable does
e354a5
 not need to be changed to allow many audit modules e.g. via @env{LD_AUDIT}.
e354a5
 @end deftp
e354a5
 
e354a5
+@deftp Tunable glibc.rtld.optional_static_tls
e354a5
+Sets the amount of surplus static TLS in bytes to allocate at program
e354a5
+startup.  Every thread created allocates this amount of specified surplus
e354a5
+static TLS. This is a minimum value and additional space may be allocated
e354a5
+for internal purposes including alignment.  Optional static TLS is used for
e354a5
+optimizing dynamic TLS access for platforms that support such optimizations
e354a5
+e.g. TLS descriptors or optimized TLS access for POWER (@code{DT_PPC64_OPT}
e354a5
+and @code{DT_PPC_OPT}).  In order to make the best use of such optimizations
e354a5
+the value should be as many bytes as would be required to hold all TLS
e354a5
+variables in all dynamic loaded shared libraries.  The value cannot be known
e354a5
+by the dynamic loader because it doesn't know the expected set of shared
e354a5
+libraries which will be loaded.  The existing static TLS space cannot be
e354a5
+changed once allocated at process startup.  The default allocation of
e354a5
+optional static TLS is 512 bytes and is allocated in every thread.
e354a5
+@end deftp
e354a5
+
e354a5
+
e354a5
 @node Elision Tunables
e354a5
 @section Elision Tunables
e354a5
 @cindex elision tunables
e354a5
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
e354a5
index 293f3ab5a496afdf..37f1915b0c75a020 100644
e354a5
--- a/sysdeps/generic/ldsodefs.h
e354a5
+++ b/sysdeps/generic/ldsodefs.h
e354a5
@@ -441,6 +441,9 @@ struct rtld_global
e354a5
   EXTERN size_t _dl_tls_static_used;
e354a5
   /* Alignment requirement of the static TLS block.  */
e354a5
   EXTERN size_t _dl_tls_static_align;
e354a5
+  /* Remaining amount of static TLS that may be used for optimizing
e354a5
+     dynamic TLS access (e.g. with TLSDESC).  */
e354a5
+  EXTERN size_t _dl_tls_static_optional;
e354a5
 
e354a5
 /* Number of additional entries in the slotinfo array of each slotinfo
e354a5
    list element.  A large number makes it almost certain take we never