446cf2
commit 07ed32f920f0bcb1ddb400e4ed606104756dee32
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Mon Jul 20 13:30:45 2020 +0200
446cf2
446cf2
    elf: Change TLS static surplus default back to 1664
446cf2
    
446cf2
    Make the computation in elf/dl-tls.c more transparent, and add
446cf2
    an explicit test for the historic value.
446cf2
    
446cf2
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
446cf2
446cf2
Conflicts:
446cf2
	elf/Makefile
446cf2
	  (Missing test backports.)
446cf2
	elf/dl-tls.c
446cf2
	  (Missing backport of rseq and its revert.)
446cf2
446cf2
diff --git a/elf/Makefile b/elf/Makefile
446cf2
index 8b96bfefd852b79f..82b5b4a07495c805 100644
446cf2
--- a/elf/Makefile
446cf2
+++ b/elf/Makefile
446cf2
@@ -204,7 +204,7 @@ tests-internal += loadtest unload unload2 circleload1 \
446cf2
 	 neededtest neededtest2 neededtest3 neededtest4 \
446cf2
 	 tst-tls3 tst-tls6 tst-tls7 tst-tls8 tst-dlmopen2 \
446cf2
 	 tst-ptrguard1 tst-stackguard1 tst-libc_dlvsym \
446cf2
-	 tst-create_format1
446cf2
+	 tst-create_format1 tst-tls-surplus
446cf2
 tests-container += tst-pldd
446cf2
 ifeq ($(build-hardcoded-path-in-tests),yes)
446cf2
 tests += tst-dlopen-aout
446cf2
@@ -1714,3 +1714,5 @@ $(objpfx)tst-tls-ie-dlmopen.out: \
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-surplus: $(libdl)
446cf2
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
446cf2
index 4f8c35b7d37bfc18..cccf74b33481b866 100644
446cf2
--- a/elf/dl-tls.c
446cf2
+++ b/elf/dl-tls.c
446cf2
@@ -54,13 +54,37 @@
446cf2
    Audit modules use their own namespaces, they are not included in rtld.nns,
446cf2
    but come on top when computing the number of namespaces.  */
446cf2
 
446cf2
-/* Size of initial-exec TLS in libc.so.  */
446cf2
-#define LIBC_IE_TLS 192
446cf2
+/* Size of initial-exec TLS in libc.so.  This should be the maximum of
446cf2
+   observed PT_GNU_TLS sizes across all architectures.  Some
446cf2
+   architectures have lower values due to differences in type sizes
446cf2
+   and link editor capabilities.  */
446cf2
+#define LIBC_IE_TLS 144
446cf2
+
446cf2
 /* Size of initial-exec TLS in libraries other than libc.so.
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
 
446cf2
+/* Default number of namespaces.  */
446cf2
+#define DEFAULT_NNS 4
446cf2
+
446cf2
+/* Default for dl_tls_static_optional.  */
446cf2
+#define OPTIONAL_TLS 512
446cf2
+
446cf2
+/* Compute the static TLS surplus based on the namespace count and the
446cf2
+   TLS space that can be used for optimizations.  */
446cf2
+static inline int
446cf2
+tls_static_surplus (int nns, int opt_tls)
446cf2
+{
446cf2
+  return (nns - 1) * LIBC_IE_TLS + nns * OTHER_IE_TLS + opt_tls;
446cf2
+}
446cf2
+
446cf2
+/* This value is chosen so that with default values for the tunables,
446cf2
+   the computation of dl_tls_static_surplus in
446cf2
+   _dl_tls_static_surplus_init yields the historic value 1664, for
446cf2
+   backwards compatibility.  */
446cf2
+#define LEGACY_TLS (1664 - tls_static_surplus (DEFAULT_NNS, OPTIONAL_TLS))
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
    number of audit modules is known and before static TLS allocation.  */
446cf2
@@ -74,8 +98,8 @@ _dl_tls_static_surplus_init (size_t naudit)
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
+  nns = DEFAULT_NNS;
446cf2
+  opt_tls = OPTIONAL_TLS;
446cf2
 #endif
446cf2
   if (nns > DL_NNS)
446cf2
     nns = DL_NNS;
446cf2
@@ -85,9 +109,8 @@ _dl_tls_static_surplus_init (size_t naudit)
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_tls);
446cf2
+  assert (LEGACY_TLS >= 0);
446cf2
+  GLRO(dl_tls_static_surplus) = tls_static_surplus (nns, opt_tls) + LEGACY_TLS;
446cf2
 }
446cf2
 
446cf2
 /* Out-of-memory handler.  */
446cf2
diff --git a/elf/tst-tls-surplus.c b/elf/tst-tls-surplus.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..b0dea0b5ee178ddd
446cf2
--- /dev/null
446cf2
+++ b/elf/tst-tls-surplus.c
446cf2
@@ -0,0 +1,42 @@
446cf2
+/* Test size of the static TLS surplus reservation for backwards compatibility.
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
+#include <stdio.h>
446cf2
+#include <support/check.h>
446cf2
+#include <support/xdlfcn.h>
446cf2
+
446cf2
+static int do_test (void);
446cf2
+#include <support/test-driver.c>
446cf2
+
446cf2
+/* This hack results in a definition of struct rtld_global_ro.  Do
446cf2
+   this after all the other header inclusions, to minimize the
446cf2
+   impact.  */
446cf2
+#define SHARED
446cf2
+#include <ldsodefs.h>
446cf2
+
446cf2
+static
446cf2
+int do_test (void)
446cf2
+{
446cf2
+  /* Avoid introducing a copy relocation due to the hidden alias in
446cf2
+     ld.so.  */
446cf2
+  struct rtld_global_ro *glro = xdlsym (NULL, "_rtld_global_ro");
446cf2
+  printf ("info: _dl_tls_static_surplus: %zu\n", glro->_dl_tls_static_surplus);
446cf2
+  /* Hisoric value: 16 * 100 + 64.  */
446cf2
+  TEST_VERIFY (glro->_dl_tls_static_surplus >= 1664);
446cf2
+  return 0;
446cf2
+}