From patchwork Mon Jun 22 16:21:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 1314609 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces@sourceware.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-armh-onmicrosoft-com header.b=vTYc/lbq; dkim=pass (1024-bit key) header.d=armh.onmicrosoft.com header.i=@armh.onmicrosoft.com header.a=rsa-sha256 header.s=selector2-armh-onmicrosoft-com header.b=vTYc/lbq; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49rF5s2SjVz9sQt for ; Tue, 23 Jun 2020 02:21:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A3FA0389040A; Mon, 22 Jun 2020 16:21:18 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-eopbgr80044.outbound.protection.outlook.com [40.107.8.44]) by sourceware.org (Postfix) with ESMTPS id A7ABC3840C3C for ; Mon, 22 Jun 2020 16:21:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A7ABC3840C3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=Szabolcs.Nagy@arm.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5EZWJyDbfI2icG6CY66cfFiCmS+vfPodQj+FrHoZenw=; b=vTYc/lbq1Uo4r4+5vPz1k1UlHt3o5x/OxF1CsAhf2NZNcb+YuIzcG20bUI9c/vKlyP4/IRoYLQvdmASx7hqbuNEbTkYCNjduC+YN6mbTZ465fPsRieSmrn51UsxdXsTcmmzaBVdiAY8psmVoOAomc58y4Ygotu7T5cklBpcLni4= Received: from MR2P264CA0040.FRAP264.PROD.OUTLOOK.COM (2603:10a6:500::28) by VI1PR08MB2797.eurprd08.prod.outlook.com (2603:10a6:802:25::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3109.21; Mon, 22 Jun 2020 16:21:12 +0000 Received: from VE1EUR03FT026.eop-EUR03.prod.protection.outlook.com (2603:10a6:500:0:cafe::ed) by MR2P264CA0040.outlook.office365.com (2603:10a6:500::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3109.22 via Frontend Transport; Mon, 22 Jun 2020 16:21:12 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; sourceware.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com; sourceware.org; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by VE1EUR03FT026.mail.protection.outlook.com (10.152.18.148) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3109.22 via Frontend Transport; Mon, 22 Jun 2020 16:21:12 +0000 Received: ("Tessian outbound 147ff5d152c1:v59"); Mon, 22 Jun 2020 16:21:12 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: b2fbd32047a8f1a1 X-CR-MTA-TID: 64aa7808 Received: from 1fcc22e33099.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id BCC42429-1F8C-44BC-B861-2CAD0B0865FC.1; Mon, 22 Jun 2020 16:21:06 +0000 Received: from EUR01-DB5-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 1fcc22e33099.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Mon, 22 Jun 2020 16:21:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=R2svSmAMGbWKDdZNZs0deGysPdWADBz1j0gaaHjQX+yEJYjw4VPDvUkYpZMaHx5UzAGHzsLH901f/XE7lPVKBbM/DzoD4fTYySOlsVArwEWocQEncrJ7R+6XZl6NXZkINcGDnynQyxokDzsoov2aauQ8bJBb059Hec/gPh17gurT9+7rWcllejCy7O0r6BtDA1LDFaCGc2KN1ATKXmr/w0doTZ2Jou8ITyFxstIJ9HLviMcDMTT7tW/+GT22udXwcQIusskWEMMuDJm60ITDN3pcsnyiXxOOWikGtUCDoRY/07R69vv/YshExLk92WVAwcKQt1MkgHdFUq9BWyXCIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5EZWJyDbfI2icG6CY66cfFiCmS+vfPodQj+FrHoZenw=; b=lTpjBBI8TtvF0OGaA/nw8bFNWJvcQAp+Pp7swKgP3k4bi5olVajITXV8lj/2AjY2ujQuYwFp6gdLAqYyvXSrGH+5ARVD6ngIOSH3MQa+56MXfy3dCZFqLybfe9krJNmuIwKfdoWII2fWTQRXwK/WvROb2JQBR30iPk6Ut3YDq957QG/lcQAJzkB2Lrphjc03ixrR7I7rFXLJmB7l9dNjyZ5zF+MONGQAVL/DlCQgUzAHlxn1wBHInzmYxqWu9BX8JRaGjmq6R6pNuPV0xoStiSMJKaj8a3uPT1M7xust8OKpCkhXPwQDJcfCmDsqwgEYv53t038GeEMrhxk4anURtA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5EZWJyDbfI2icG6CY66cfFiCmS+vfPodQj+FrHoZenw=; b=vTYc/lbq1Uo4r4+5vPz1k1UlHt3o5x/OxF1CsAhf2NZNcb+YuIzcG20bUI9c/vKlyP4/IRoYLQvdmASx7hqbuNEbTkYCNjduC+YN6mbTZ465fPsRieSmrn51UsxdXsTcmmzaBVdiAY8psmVoOAomc58y4Ygotu7T5cklBpcLni4= Authentication-Results-Original: sourceware.org; dkim=none (message not signed) header.d=none;sourceware.org; dmarc=none action=none header.from=arm.com; Received: from AM6PR08MB3047.eurprd08.prod.outlook.com (2603:10a6:209:4c::23) by AM6PR08MB5239.eurprd08.prod.outlook.com (2603:10a6:20b:e6::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3109.21; Mon, 22 Jun 2020 16:21:06 +0000 Received: from AM6PR08MB3047.eurprd08.prod.outlook.com ([fe80::2404:de9f:78c0:313c]) by AM6PR08MB3047.eurprd08.prod.outlook.com ([fe80::2404:de9f:78c0:313c%6]) with mapi id 15.20.3109.027; Mon, 22 Jun 2020 16:21:06 +0000 From: Szabolcs Nagy To: libc-alpha@sourceware.org Subject: [PATCH v5 1/2] rtld: Add rtld.nns tunable for the number of supported namespaces Date: Mon, 22 Jun 2020 17:21:00 +0100 Message-Id: <9e6146dfb8a23058a086b9cef668b6a4c610ec14.1592841472.git.szabolcs.nagy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0064.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:60::28) To AM6PR08MB3047.eurprd08.prod.outlook.com (2603:10a6:209:4c::23) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (217.140.106.53) by LO2P265CA0064.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:60::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3109.25 via Frontend Transport; Mon, 22 Jun 2020 16:21:05 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [217.140.106.53] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 04af6edd-e5ac-4ccf-5798-08d816c847b4 X-MS-TrafficTypeDiagnostic: AM6PR08MB5239:|VI1PR08MB2797: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:9508;OLM:9508; X-Forefront-PRVS: 0442E569BC X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 3/rFiZU0bbZVc7HisgScgD/s8VSlc44fIex/ejDx0QH0hg01MQenUy9iOl48cN11p9N78uPK133yAfzgzP9XN/kqu9K5rLEcLCEFhcxsnVoMO4y5FTqPEoiQcS6k/GxjNgIPXgbhox3lOzPYQbYlRKIUh1szAZlc6VrSanTkhpTv82vWDamV6ogZxbdWyxlTlJko0VsRZSBX7KIdJkFC/YFu9nqP599RoLTTf1sTUw/CIq1bn96GpJ9UpQ1q6+K5sSE36SJNQVhDHLgObfXQQbuZjAKwyyir2DoAwGjDCIb4lkQkYIKBHqYVErCjHqBUfYkJ1yWqfv09J3vfWDdE4bZB5/qT0Yrf6LDSNU1quFRuj6GWd7p7npA6Luftb6x2C/UJlp6KJOs+T4PAvxaa+B7F+KFGiWohpPrT4L8APOIudqMinKBtWNw3iPNTgrz16d6Dv8TyY9HH+oy3AvqAgRQUFfWPnRKGlsnQazvPOmWMTNJ72qgnIwJuCmdmi03i X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM6PR08MB3047.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFTY:; SFS:(4636009)(346002)(376002)(136003)(39860400002)(366004)(396003)(52116002)(2906002)(44832011)(30864003)(6486002)(966005)(36756003)(316002)(86362001)(83380400001)(2616005)(478600001)(6666004)(6506007)(8936002)(6512007)(956004)(69590400007)(186003)(16526019)(66476007)(66946007)(8676002)(66556008)(5660300002)(6916009)(26005)(136400200001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: oahb8OXvuaPiOHY09DTxOKFOwCsPiHPbyj5XEwBNEQQ7x65AsAgxKnMf0CtSUQnA8fA/tGr9TAkb6d76eoL1wVU6OUpSEu52zHg61HffIUz2WhQjFiv7hQSZc0P61ZzJf+lv7JhntkU0z5evAhp745O0PxeGTIa3QYixImCFgYBjzNmxFEL0P1MMnN+1GziOVgiVj25SztaqLbsW7b6okI3O5vsGl15go5uJlbySvM0e1a8jIGw7guqnkKARa4Vuj7mwNRqAHH2N2lHvcYXcvEjEunL6y8/4VjrkPt85JAOFyoBIZTixYgpnnMwlkoBFFQch+Lz3CnGWNyQPlJtCTYtV3wm/aRVqN/ci5a6mtOpsBnTpf6B0weNyFd/m8vW5IjiZZFrFSyeuIDfjtN3Di9nK4/m6RIHt3CHP1pzrpT8B7aN1xqBh43mO0IKCx9d+nK8GC3v7RWQO00bnCOxeLt/jYIuB9oC4s+nFWR17j0cZgwktzsUQzBrYsF+nSHAC X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB5239 Original-Authentication-Results: sourceware.org; dkim=none (message not signed) header.d=none; sourceware.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: VE1EUR03FT026.eop-EUR03.prod.protection.outlook.com X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFTY:; SFS:(4636009)(396003)(136003)(39860400002)(376002)(346002)(46966005)(8936002)(82310400002)(44832011)(2616005)(6916009)(47076004)(82740400003)(336012)(356005)(2906002)(81166007)(956004)(83380400001)(8676002)(6506007)(26005)(70586007)(36756003)(70206006)(6486002)(966005)(69590400007)(186003)(6666004)(16526019)(86362001)(36906005)(478600001)(316002)(30864003)(6512007)(5660300002)(136400200001); DIR:OUT; SFP:1101; X-MS-Office365-Filtering-Correlation-Id-Prvs: 1c5a698d-366c-464a-300d-08d816c843b7 X-Forefront-PRVS: 0442E569BC X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JySZ8JCh0Ab4nQkaqXXRebRARLkz7hQlGJgiqawLV0UvZOntmTaK7LXNJd6bTAmU3n8c8Zna6VunhX3Ho7WD/0SYI/42n7k7o0aECXBmyy/g/H4QaMsVYM/Cf0Jza5arqUUMiB+1ddF5Nx10S/X0NQ+JLJx/gzTGrbjn0x+gXre8s7nfqU/ZX+0XjtBu7Ds9892lsCBV1diwCeUjgcn/4iGZoTixYzIvZoS5iLz/8pZc/kovhLPYboAk0Lm7hPbe6byVTGUYPbm3Js0NKq9ZtMZHNthPkPfuyXMdhWlRbePyGJvVu1Sb6kDsEternATZw+4VRAlDhIrPYC7dHVOx11rQkkMxlMSXhQ3rUSBP88NJQP0vWGFr4Psuz2P/0snqgitnh/129rXCgMVBpSezX/dZXyIsZUFm2Xpe0kYyygxQTYpzegmivO/0iVHLzhUXNaW9mKZ5RyQVXBiFsw/oWyyhVYv3mxHO2oBL9ss9BZKms/ByfSD+VrRTMSoGplYzu9nKjExnXP2pZlF4QxCZthcoHqI3X4kXR1Dnk2lZEFo= X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Jun 2020 16:21:12.3657 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 04af6edd-e5ac-4ccf-5798-08d816c847b4 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB2797 X-Spam-Status: No, score=-16.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY 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: , Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" TLS_STATIC_SURPLUS is 1664 bytes currently which is not enough to support DL_NNS (== 16) number of dynamic link namespaces, if we assume 192 bytes of TLS are reserved for libc use and 144 bytes are reserved for other system libraries that use IE TLS. A new tunable is introduced to control the number of supported namespaces and to adjust the surplus static TLS size as follows: surplus_tls = 192 * (rtld.nns-1) + 144 * rtld.nns + 512 The default is rtld.nns == 4 and then the surplus TLS size is the same as before, so the behaviour is unchanged by default. If an application creates more namespaces than the rtld.nns setting allows, then it is not guaranteed to work, but the limit is not checked. So existing usage will continue to work, but in the future if an application creates more than 4 dynamic link namespaces then the tunable will need to be set. In this patch DL_NNS is a fixed value and provides a maximum to the rtld.nns setting. Static linking used fixed 2048 bytes surplus TLS, this is changed so the same contract is used as for dynamic linking. With static linking DL_NNS == 1 so rtld.nns tunable is forced to 1, so by default the surplus TLS is reduced to 144 + 512 = 656 bytes. This change is not expected to cause problems. Tested on aarch64-linux-gnu and x86_64-linux-gnu. Reviewed-by: Carlos O'Donell --- v5: - Split the patch into two: rtld.nns and tls optimization parts. - Use rtld.nns instead of dl.nns. - Renamed the init function and moved the calls close to the first use of surplus tls size. - Updated the tunable documentation. v4: - Rebased and moved this log out of the commit message. - Minor commit message wording changes. v3: - archived at https://sourceware.org/pipermail/libc-alpha/2020-March/111660.html - Replace TLS_STATIC_SURPLUS with GLRO(dl_tls_static_surplus) and simplify related logic. - In case of static linking, replace GL(dl_tls_static_size) with GLRO(dl_tls_static_surplus) in the code paths before the GL(dl_tls_static_size) value is actually computed. - Update comments and the test code. - Document the new tunables. - Update description, mention static linking. v2: - Add dl.nns tunable. - Add dl.optional_static_tls tunable. - New surplus TLS usage contract that works reliably up to dl.nns namespaces. --- csu/libc-tls.c | 28 +++++++++---------- elf/dl-tls.c | 55 ++++++++++++++++++++++++++++++++++---- elf/dl-tunables.list | 9 +++++++ elf/rtld.c | 3 +++ manual/tunables.texi | 21 +++++++++++++++ sysdeps/generic/ldsodefs.h | 8 ++++++ 6 files changed, 105 insertions(+), 19 deletions(-) diff --git a/csu/libc-tls.c b/csu/libc-tls.c index 73ade0fec5..e2603157e8 100644 --- a/csu/libc-tls.c +++ b/csu/libc-tls.c @@ -46,13 +46,16 @@ bool _dl_tls_dtv_gaps; struct dtv_slotinfo_list *_dl_tls_dtv_slotinfo_list; /* Number of modules in the static TLS block. */ size_t _dl_tls_static_nelem; -/* Size of the static TLS block. Giving this initialized value - preallocates some surplus bytes in the static TLS area. */ -size_t _dl_tls_static_size = 2048; +/* Size of the static TLS block. */ +size_t _dl_tls_static_size; /* Size actually allocated in the static TLS block. */ size_t _dl_tls_static_used; /* Alignment requirement of the static TLS block. */ size_t _dl_tls_static_align; +/* Size of surplus space in the static TLS area for dynamically + loaded modules with IE-model TLS or for TLSDESC optimization. + See comments in elf/dl-tls.c where it is initialized. */ +size_t _dl_tls_static_surplus; /* Generation counter for the dtv. */ size_t _dl_tls_generation; @@ -81,10 +84,8 @@ init_slotinfo (void) static void init_static_tls (size_t memsz, size_t align) { - /* That is the size of the TLS memory for this object. The initialized - value of _dl_tls_static_size is provided by dl-open.c to request some - surplus that permits dynamic loading of modules with IE-model TLS. */ - GL(dl_tls_static_size) = roundup (memsz + GL(dl_tls_static_size), + /* That is the size of the TLS memory for this object. */ + GL(dl_tls_static_size) = roundup (memsz + GLRO(dl_tls_static_surplus), TLS_TCB_ALIGN); #if TLS_TCB_AT_TP GL(dl_tls_static_size) += TLS_TCB_SIZE; @@ -125,25 +126,24 @@ __libc_setup_tls (void) break; } + /* Calculate the size of the static TLS surplus. */ + _dl_tls_static_surplus_init (); + /* We have to set up the TCB block which also (possibly) contains 'errno'. Therefore we avoid 'malloc' which might touch 'errno'. Instead we use 'sbrk' which would only uses 'errno' if it fails. In this case we are right away out of memory and the user gets - what she/he deserves. - - The initialized value of _dl_tls_static_size is provided by dl-open.c - to request some surplus that permits dynamic loading of modules with - IE-model TLS. */ + what she/he deserves. */ #if TLS_TCB_AT_TP /* Align the TCB offset to the maximum alignment, as _dl_allocate_tls_storage (in elf/dl-tls.c) does using __libc_memalign and dl_tls_static_align. */ - tcb_offset = roundup (memsz + GL(dl_tls_static_size), max_align); + tcb_offset = roundup (memsz + GLRO(dl_tls_static_surplus), max_align); tlsblock = __sbrk (tcb_offset + TLS_INIT_TCB_SIZE + max_align); #elif TLS_DTV_AT_TP tcb_offset = roundup (TLS_INIT_TCB_SIZE, align ?: 1); tlsblock = __sbrk (tcb_offset + memsz + max_align - + TLS_PRE_TCB_SIZE + GL(dl_tls_static_size)); + + TLS_PRE_TCB_SIZE + GLRO(dl_tls_static_surplus)); tlsblock += TLS_PRE_TCB_SIZE; #else /* In case a model with a different layout for the TCB and DTV diff --git a/elf/dl-tls.c b/elf/dl-tls.c index fa03234610..2201a1cc1d 100644 --- a/elf/dl-tls.c +++ b/elf/dl-tls.c @@ -29,10 +29,54 @@ #include #include -/* Amount of excess space to allocate in the static TLS area - to allow dynamic loading of modules defining IE-model TLS data. */ -#define TLS_STATIC_SURPLUS 64 + DL_NNS * 100 +#define TUNABLE_NAMESPACE rtld +#include + +/* Surplus static TLS, GLRO(dl_tls_static_surplus), is used for + + - IE TLS in libc.so for all dlmopen namespaces except in the initial + one where libc.so is not loaded dynamically but at startup time, + - IE TLS in other libraries which may be dynamically loaded even in the + initial namespace, + - and optionally for optimizing dynamic TLS access. + + The maximum number of namespaces is DL_NNS, but to support that many + namespaces correctly the static TLS allocation should be significantly + increased, which may cause problems with small thread stacks due to the + way static TLS is accounted (bug 11787). + + So there is a rtld.nns tunable limit on the number of supported namespaces + that affects the size of the static TLS and by default it's small enough + not to cause problems with existing applications. The limit is not + enforced or checked: it is the user's responsibility to increase rtld.nns + if more dlmopen namespaces are used. */ + +/* Size of initial-exec TLS in libc.so. */ +#define LIBC_IE_TLS 192 +/* 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 +/* Size of additional surplus TLS, placeholder for TLS optimizations. */ +#define OPT_SURPLUS_TLS 512 +void +_dl_tls_static_surplus_init (void) +{ + size_t nns; + +#if HAVE_TUNABLES + nns = TUNABLE_GET (nns, size_t, NULL); +#else + /* Default values of the tunables. */ + nns = 4; +#endif + if (nns > DL_NNS) + nns = DL_NNS; + GLRO(dl_tls_static_surplus) = ((nns - 1) * LIBC_IE_TLS + + nns * OTHER_IE_TLS + + OPT_SURPLUS_TLS); +} /* Out-of-memory handler. */ static void @@ -218,7 +262,8 @@ _dl_determine_tlsoffset (void) } GL(dl_tls_static_used) = offset; - GL(dl_tls_static_size) = (roundup (offset + TLS_STATIC_SURPLUS, max_align) + GL(dl_tls_static_size) = (roundup (offset + GLRO(dl_tls_static_surplus), + max_align) + TLS_TCB_SIZE); #elif TLS_DTV_AT_TP /* The TLS blocks start right after the TCB. */ @@ -262,7 +307,7 @@ _dl_determine_tlsoffset (void) } GL(dl_tls_static_used) = offset; - GL(dl_tls_static_size) = roundup (offset + TLS_STATIC_SURPLUS, + GL(dl_tls_static_size) = roundup (offset + GLRO(dl_tls_static_surplus), TLS_TCB_ALIGN); #else # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 0d398dd251..b07742d7b3 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -126,4 +126,13 @@ glibc { default: 3 } } + + rtld { + nns { + type: SIZE_T + minval: 1 + maxval: 16 + default: 4 + } + } } diff --git a/elf/rtld.c b/elf/rtld.c index f4c2602d65..f339f6894f 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -780,6 +780,9 @@ init_tls (void) } assert (i == GL(dl_tls_max_dtv_idx)); + /* Calculate the size of the static TLS surplus. */ + _dl_tls_static_surplus_init (); + /* Compute the TLS offsets for the various blocks. */ _dl_determine_tlsoffset (); diff --git a/manual/tunables.texi b/manual/tunables.texi index ec18b10834..978e08f4fb 100644 --- a/manual/tunables.texi +++ b/manual/tunables.texi @@ -31,6 +31,7 @@ their own namespace. @menu * Tunable names:: The structure of a tunable name * Memory Allocation Tunables:: Tunables in the memory allocation subsystem +* Dynamic Linking Tunables:: Tunables in the dynamic linking subsystem * Elision Tunables:: Tunables in elision subsystem * POSIX Thread Tunables:: Tunables in the POSIX thread subsystem * Hardware Capability Tunables:: Tunables that modify the hardware @@ -226,6 +227,26 @@ pointer, so add 4 on 32-bit systems or 8 on 64-bit systems to the size passed to @code{malloc} for the largest bin size to enable. @end deftp +@node Dynamic Linking Tunables +@section Dynamic Linking Tunables +@cindex dynamic linking tunables +@cindex rtld tunables + +@deftp {Tunable namespace} glibc.rtld +Dynamic linker behavior can be modified by setting the +following tunables in the @code{rtld} namespace: +@end deftp + +@deftp Tunable glibc.rtld.nns +Sets the number of supported dynamic link namespaces (see @code{dlmopen}). +Currently this limit can be set between 1 and 16 inclusive, the default is 4. +Each link namespace consumes some memory in all thread, and thus raising the +limit will increase the amount of memory each thread uses. Raising the limit +is useful when your application uses more than 4 dynamic linker audit modules +e.g. LD_AUDIT, or will use more than 4 dynamic link namespaces as created +by @code{dlmopen} with an lmid argument of @code{LM_ID_NEWLM}. +@end deftp + @node Elision Tunables @section Elision Tunables @cindex elision tunables diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index c525ffa12c..3b0c6d9620 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -583,6 +583,11 @@ struct rtld_global_ro binaries, don't honor for PIEs). */ EXTERN ElfW(Addr) _dl_use_load_bias; + /* Size of surplus space in the static TLS area for dynamically + loaded modules with IE-model TLS or for TLSDESC optimization. + See comments in elf/dl-tls.c where it is initialized. */ + EXTERN size_t _dl_tls_static_surplus; + /* Name of the shared object to be profiled (if any). */ EXTERN const char *_dl_profile; /* Filename of the output file. */ @@ -1101,6 +1106,9 @@ extern size_t _dl_count_modids (void) attribute_hidden; /* Calculate offset of the TLS blocks in the static TLS block. */ extern void _dl_determine_tlsoffset (void) attribute_hidden; +/* Calculate the size of the static TLS surplus. */ +void _dl_tls_static_surplus_init (void) attribute_hidden; + #ifndef SHARED /* Set up the TCB for statically linked applications. This is called early during startup because we always use TLS (for errno and the