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