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