From patchwork Fri Jul 17 09:53:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 40131 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 99B4A3861922; Fri, 17 Jul 2020 09:54:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 99B4A3861922 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1594979647; bh=I7ZvIaSlAX9y7GlTrjjMCBNxI0cPpSIOdIXimRryeVo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=viKCv3/LwOx9ba1wbZNB+/Ypw7TlE+KZhlYpSmWlqfFc81WtY0wXIwm915GqN1N3y +6tobx47FF++ZXmA001JCnlhjOMQulLBhcWOVmR+nTs7NbL9sPbb08Ez/FPut8CsFE RzdxPRizdrLX2sR/e3kaGcBeF0OUu3zNYy0WL6l4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by sourceware.org (Postfix) with ESMTP id BDB313857C59 for ; Fri, 17 Jul 2020 09:54:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BDB313857C59 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-411-vi2ErAoZN32x1fKFNvDhWQ-1; Fri, 17 Jul 2020 05:53:59 -0400 X-MC-Unique: vi2ErAoZN32x1fKFNvDhWQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6B8C3800597 for ; Fri, 17 Jul 2020 09:53:58 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-112-136.ams2.redhat.com [10.36.112.136]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BEA1E1A918 for ; Fri, 17 Jul 2020 09:53:57 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH] elf: Change TLS static surplus default back to 1664 Date: Fri, 17 Jul 2020 11:53:56 +0200 Message-ID: <87imemsc0b.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Make the computation in elf/dl-tls.c more transparent, and add an explicit test for the historic value. Tested on powerpc64le-linux-gnu and x86_64-linux-gnu. Reviewed-by: Szabolcs Nagy --- elf/Makefile | 4 +++- elf/dl-tls.c | 35 ++++++++++++++++++++++++++++------- elf/tst-tls-surplus.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 8 deletions(-) diff --git a/elf/Makefile b/elf/Makefile index a2c3b12007..0b78721848 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -216,7 +216,7 @@ tests-internal += loadtest unload unload2 circleload1 \ neededtest neededtest2 neededtest3 neededtest4 \ tst-tls3 tst-tls6 tst-tls7 tst-tls8 tst-dlmopen2 \ tst-ptrguard1 tst-stackguard1 tst-libc_dlvsym \ - tst-create_format1 + tst-create_format1 tst-tls-surplus tests-container += tst-pldd tst-dlopen-tlsmodid-container \ tst-dlopen-self-container test-srcs = tst-pathopt @@ -1794,3 +1794,5 @@ $(objpfx)tst-tls-ie-dlmopen.out: \ $(objpfx)tst-tls-ie-mod4.so \ $(objpfx)tst-tls-ie-mod5.so \ $(objpfx)tst-tls-ie-mod6.so + +$(objpfx)tst-tls-surplus: $(libdl) diff --git a/elf/dl-tls.c b/elf/dl-tls.c index 9a17427047..462b0f46c1 100644 --- a/elf/dl-tls.c +++ b/elf/dl-tls.c @@ -54,13 +54,35 @@ Audit modules use their own namespaces, they are not included in rtld.nns, but come on top when computing the number of namespaces. */ -/* Size of initial-exec TLS in libc.so. */ -#define LIBC_IE_TLS 160 +/* Size of initial-exec TLS in libc.so. May be smaller than that on + some architectures. */ +#define LIBC_IE_TLS 144 + /* Size of initial-exec TLS in libraries other than libc.so. This should be large enough to cover runtime libraries of the compiler such as libgomp and libraries in libc other than libc.so. */ #define OTHER_IE_TLS 144 +/* Default number of namespaces. */ +#define DEFAULT_NNS 4 + +/* Default for dl_tls_static_optional. */ +#define OPTIONAL_TLS 512 + +/* Compute the static TLS surplus based on the namespace count and the + TLS space that can be used for optimizations. */ +static inline int +tls_static_surplus (int nns, int opt_tls) +{ + return (nns - 1) * LIBC_IE_TLS + nns * OTHER_IE_TLS + opt_tls; +} + +/* This value is chosen so that with default values for the tunables, + the computation of dl_tls_static_surplus in + _dl_tls_static_surplus_init yields the historic value 1664, for + backwards compatibility. */ +#define LEGACY_TLS (1664 - tls_static_surplus (DEFAULT_NNS, OPTIONAL_TLS)) + /* Calculate the size of the static TLS surplus, when the given number of audit modules are loaded. Must be called after the number of audit modules is known and before static TLS allocation. */ @@ -74,8 +96,8 @@ _dl_tls_static_surplus_init (size_t naudit) opt_tls = TUNABLE_GET (optional_static_tls, size_t, NULL); #else /* Default values of the tunables. */ - nns = 4; - opt_tls = 512; + nns = DEFAULT_NNS; + opt_tls = OPTIONAL_TLS; #endif if (nns > DL_NNS) nns = DL_NNS; @@ -85,9 +107,8 @@ _dl_tls_static_surplus_init (size_t naudit) nns += naudit; GL(dl_tls_static_optional) = opt_tls; - GLRO(dl_tls_static_surplus) = ((nns - 1) * LIBC_IE_TLS - + nns * OTHER_IE_TLS - + opt_tls); + assert (LEGACY_TLS >= 0); + GLRO(dl_tls_static_surplus) = tls_static_surplus (nns, opt_tls) + LEGACY_TLS; } /* Out-of-memory handler. */ diff --git a/elf/tst-tls-surplus.c b/elf/tst-tls-surplus.c new file mode 100644 index 0000000000..b0dea0b5ee --- /dev/null +++ b/elf/tst-tls-surplus.c @@ -0,0 +1,42 @@ +/* Test size of the static TLS surplus reservation for backwards compatibility. + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +static int do_test (void); +#include + +/* This hack results in a definition of struct rtld_global_ro. Do + this after all the other header inclusions, to minimize the + impact. */ +#define SHARED +#include + +static +int do_test (void) +{ + /* Avoid introducing a copy relocation due to the hidden alias in + ld.so. */ + struct rtld_global_ro *glro = xdlsym (NULL, "_rtld_global_ro"); + printf ("info: _dl_tls_static_surplus: %zu\n", glro->_dl_tls_static_surplus); + /* Hisoric value: 16 * 100 + 64. */ + TEST_VERIFY (glro->_dl_tls_static_surplus >= 1664); + return 0; +}