From patchwork Fri Jul 17 11:41:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 40133 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 88E22393B04E; Fri, 17 Jul 2020 11:41:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 88E22393B04E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1594986101; bh=xQdoSMzOzQ01Kn6dYikqJrZgmFsr1vOj/nTq73ncAbo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Oisk0PRt8b3QwinxH8xn6xKlJ/r/NlclAPZenRCdzw+KQ04qFHLhBmTZshZ6pgtk+ nBvXg69PJKlbQTStUNll91DclKX0cS8tNHr/HOPK7rniKTNmGbu/btnA/RmrkqWSLt pN74glqFONwyjR0BJAdQ71BHrDEl8wnkzwoh73oc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by sourceware.org (Postfix) with ESMTP id E938F393A077 for ; Fri, 17 Jul 2020 11:41:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E938F393A077 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-190-h2azEV6-MZquR_8WNli9sA-1; Fri, 17 Jul 2020 07:41:34 -0400 X-MC-Unique: h2azEV6-MZquR_8WNli9sA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9E9DC1923768; Fri, 17 Jul 2020 11:41:33 +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 C840A724C2; Fri, 17 Jul 2020 11:41:32 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH v2] elf: Change TLS static surplus default back to 1664 Date: Fri, 17 Jul 2020 13:41:31 +0200 Message-ID: <87eepaqsgk.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.79 on 10.5.11.11 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_H2, 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. Reviewed-by: Carlos O'Donell --- elf/Makefile | 4 +++- elf/dl-tls.c | 37 ++++++++++++++++++++++++++++++------- elf/tst-tls-surplus.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 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..9fa62f5d22 100644 --- a/elf/dl-tls.c +++ b/elf/dl-tls.c @@ -54,13 +54,37 @@ 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. This should be the maximum of + observed PT_GNU_TLS sizes across all architectures. Some + architectures have lower values due to differences in type sizes + and link editor capabilities. */ +#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 +98,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 +109,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; +}