From patchwork Fri May 22 14:49:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 39353 X-Patchwork-Delegate: carlos@redhat.com 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 32237383F872; Fri, 22 May 2020 14:49:41 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2082.outbound.protection.outlook.com [40.107.22.82]) by sourceware.org (Postfix) with ESMTPS id DEE46385E830 for ; Fri, 22 May 2020 14:49:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DEE46385E830 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=VqO/7YOAX5QwY4CmBmQjGVEMWiYT2FAEbo41zcP0R48=; b=hk61FHi3gX2GQnxWx8IMgzPqhBpj4cRN59d7cKfhTojXrsVPZtTDP2m6zXir0mdVkwy0Agqzy9vz6/2YMjwjkWKcNxpyXxSCW9srfra3HFUbtDUWIsdG8uTX8DnD8ynz7vpy5RrDLdJyCZ5Lrj8gaNvul6Ph3NPuF38CbiDB55c= Received: from DB6PR0501CA0002.eurprd05.prod.outlook.com (2603:10a6:4:8f::12) by AM5PR0802MB2529.eurprd08.prod.outlook.com (2603:10a6:203:9e::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3021.23; Fri, 22 May 2020 14:49:32 +0000 Received: from DB5EUR03FT033.eop-EUR03.prod.protection.outlook.com (2603:10a6:4:8f:cafe::7f) by DB6PR0501CA0002.outlook.office365.com (2603:10a6:4:8f::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3021.23 via Frontend Transport; Fri, 22 May 2020 14:49:32 +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 DB5EUR03FT033.mail.protection.outlook.com (10.152.20.76) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3021.23 via Frontend Transport; Fri, 22 May 2020 14:49:32 +0000 Received: ("Tessian outbound 14e212f6ce41:v57"); Fri, 22 May 2020 14:49:32 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 31fde256ca19e9dd X-CR-MTA-TID: 64aa7808 Received: from 224b1bc58a04.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 331DB142-69CB-4681-B912-B957C5BF4083.1; Fri, 22 May 2020 14:49:26 +0000 Received: from EUR05-VI1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 224b1bc58a04.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 22 May 2020 14:49:26 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KCZ9i/xDHTXabsNhVDFIj6jJoy78N0AhlC0C8FpD2sjDHN1E8o/oX4wELmnvmUoVDvtm5XYP/IWIWj8UND6l20+5zwXC5Mv4asvF7BrXBp03wt3GI9X4kWBcSAsuSSxAaVyQmG5HuZ+NGRUN90m3u4tsgzbDwHuFTrIPIKHu0flzDYzh9AXF7cOp77sQJV2HMFnPs62Qj2sQYFlFGAyq6rX2RFYRrPBv0y3VuhuruAIN6+X5JphXSlpfljxMMbavjnf9TIHB53YPq9MMRNjQMFbnne6c7Uf7AsomHMGT47NzxJJCC430hLd6cgqiqAqU3iTxVyrgeh2rYNhKBXJwDA== 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=VqO/7YOAX5QwY4CmBmQjGVEMWiYT2FAEbo41zcP0R48=; b=KQdo3IoMo1NGOVbGygMN6I67k5txGcWR9tBHoNmC8NazM3TaJSHRWSLIDrqHZavZdvVEHQcAyPE/TrLPPpSJWh5yWuT+k/sQPQiOLnG8HoQmy+moww2kVbXAay8300T3UJHV+nU4P/F4eUrt39gXpdd3dhf2r6XN6qvHGNrcggQHSBzkLLw0o+rCn50c+6lxlJ+I3cu3VtnKIr9qUQBQ9FEfA3vSqVm15WiSim4gK2pwjIQR+HwrLbLLywMqc3YPScvYVvd6podhHx98D1vyDn4Ig0eCsKb2rx6/MPXiXZYCRGqLZxzjCoB4rxhC2SyviGtyKUmltiMVt0QuBneypQ== 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=VqO/7YOAX5QwY4CmBmQjGVEMWiYT2FAEbo41zcP0R48=; b=hk61FHi3gX2GQnxWx8IMgzPqhBpj4cRN59d7cKfhTojXrsVPZtTDP2m6zXir0mdVkwy0Agqzy9vz6/2YMjwjkWKcNxpyXxSCW9srfra3HFUbtDUWIsdG8uTX8DnD8ynz7vpy5RrDLdJyCZ5Lrj8gaNvul6Ph3NPuF38CbiDB55c= 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 AM6PR08MB3589.eurprd08.prod.outlook.com (2603:10a6:20b:46::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3021.24; Fri, 22 May 2020 14:49:23 +0000 Received: from AM6PR08MB3047.eurprd08.prod.outlook.com ([fe80::49fd:6ded:4da7:8862]) by AM6PR08MB3047.eurprd08.prod.outlook.com ([fe80::49fd:6ded:4da7:8862%7]) with mapi id 15.20.3021.020; Fri, 22 May 2020 14:49:23 +0000 From: Szabolcs Nagy To: libc-alpha@sourceware.org Subject: [PATCH] Avoid using up static TLS surplus for optimizations [BZ #25051] Date: Fri, 22 May 2020 15:49:17 +0100 Message-Id: <20200522144917.17999-1-szabolcs.nagy@arm.com> X-Mailer: git-send-email 2.17.1 X-ClientProxiedBy: LO2P265CA0263.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:8a::35) 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.55) by LO2P265CA0263.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:8a::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3021.23 via Frontend Transport; Fri, 22 May 2020 14:49:23 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [217.140.106.55] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: a2c89cf8-134f-41a8-53c0-08d7fe5f568f X-MS-TrafficTypeDiagnostic: AM6PR08MB3589:|AM5PR0802MB2529: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:9508;OLM:9508; X-Forefront-PRVS: 04111BAC64 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 0/ZpKgxrZ739GN7liGGw+0BYSoqeyVakSj4W2VcfwomHRpgy0o2rS2GxtsrckdbtNzm3sU4uAp7oKATKFxtooGysokhr+E1KOWYofHo2D0UNgCcMFIusoK3NXWJPBFAFHJclP3sqf135dzsUBnR1LyVpBuS2fEOFQk7kiTDPrK/FBsHdFwfYNDRfpckrPSvYQL1Lk8cjJyrIczVF4yEkNqJvnLBj7Y8HNmDyoWDXwuMT/witKYufRFHjdyUpqTFunVSeeM5CkMSTAfnfLA/ZXjARs41wXvEPUjtvNWJwyPrlJ8HI83cnY9jkB8/ZHwA1QmFmGkjjCPJVEbCFC6uCXFX7thjtl8RX1mmjksvQAD7Nz0igoDKq8Nx3F0NjuFC2nqgVmDh9P0+vwzeZiN/SUDjEJK0xv9huTa6Nw1yx/C/rbPymMnrl9/a/gqlqdgI03Lr4CqHEtvYH+IjJl/pN1CCvV38NhGhuszh3dJmPHe0= 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)(396003)(39860400002)(366004)(136003)(66946007)(66476007)(66556008)(69590400007)(44832011)(6486002)(52116002)(2616005)(8936002)(956004)(316002)(5660300002)(86362001)(186003)(1076003)(36756003)(6506007)(478600001)(6916009)(2906002)(26005)(6512007)(966005)(8676002)(16526019)(30864003)(6666004)(2004002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: h67ZNkqseX7NOCru/SitLox3ujVWaC/SVPYph1g8QsjQjVe2ssV9VPNV1kJLAwKTe2niNLJy4JfkhdVMDEXx+CVjTpsNps6bKN8PseD28jeCxX3jnOWdHw165t5v9PbBtAh2I5cc/GXyUEbMe/GiJPse9wKupR2coXCM+BDJVZ8gKm6wVhoYjndIbTY5NxKlmbLQBYI5QFTmrTCFXSzB7l2L+DaLzRiqrUavrc1b0BQDgadrQ6XjpRQ3TDxRvn8WZWBVecj820USKR5ADdXZWUd1YVWFRGjNzMMGHZekqjVWdL2pT/39gbIkTREvOlpkRa43XDYO2Q4a5B9PAs66zDIist7qF4V0N4EhG2XKhMvDM8FabCLTMVLCS0PkcqkkMdJwEZXjr1EjuNDZvxVinjmbTqHCPD9Tm+R0iiK+TRlwB/ds0rH/fuFKWyWFuoy0Q8U9f+ReYDfTSPiiWiOdIOUniY7dCKaqDdV28moK41y2zRNU1pPKmM7P0CvIMG7g X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB3589 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: DB5EUR03FT033.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)(346002)(376002)(39860400002)(136003)(46966005)(478600001)(966005)(6486002)(26005)(6512007)(30864003)(2616005)(8676002)(16526019)(44832011)(186003)(6506007)(1076003)(86362001)(956004)(70206006)(82310400002)(36756003)(356005)(6666004)(82740400003)(70586007)(81166007)(8936002)(5660300002)(6916009)(47076004)(316002)(336012)(69590400007)(2906002)(2004002); DIR:OUT; SFP:1101; X-MS-Office365-Filtering-Correlation-Id-Prvs: a2388d9b-2f66-4bf4-0a4b-08d7fe5f5114 X-Forefront-PRVS: 04111BAC64 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: QItqgog6VZElHS6IR3Dm6JcXz3o7Ml8PX5NKranB6hW2azJ7V7wIivdv0B3atRFV2UTOO7kHWJXlz60rOEUPK2xGeEF4PcASTWNGApCJiVJnB2zFwfDJcp2Wok+M4OCYQB6bzinSpsjpAfPmobu8kO12gTaHZhOwQkyK0ySq42lRklziAz5LwkpgGBFqfr/T+CJZjYOkFPLokSD7Zgtg/AAaC5PvMA+yl3PsuaAs6u/MuXqE50ZPbTrd+hRBgFsIpJ81pyYqFg74hXQsUQhl78RSZv47tDJlsdUt/wiR2gXiGPD9MJuexpZzzbbqP9IArl8TCOVUWFI6guijNgcXoDielVRRCW2BqRVQDCAt2XCJOsJ1OGxHsQ7bd194tjASlSlFtc+tvQAZTlrhIue20FhvQ7v6MZZ40QQUczSN/154s7Mo5xijYvdG+QtzLS7p2zbt5gqqnijKSp2myBDFA1vXKyxOzC0Uduk9bqBBbiZfuqCHG/94+QaBA/VCadI5hi3s6f6oWEMaUdL5RKWX3w== X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 May 2020 14:49:32.3716 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: a2c89cf8-134f-41a8-53c0-08d7fe5f568f 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: AM5PR0802MB2529 X-Spam-Status: No, score=-18.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, 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" On some targets static TLS surplus area can be used opportunistically for dynamically loaded modules such that the TLS access then becomes faster (TLSDESC and powerpc TLS optimization). However we don't want all surplus TLS to be used for this optimization because dynamically loaded modules with initial-exec model TLS can only use surplus TLS. Currently TLS_STATIC_SURPLUS is 1664 bytes which is not even enough to cover the IE TLS needed for libc.so when DL_NNS (== 16) namespaces are created, so to allow reliable use of dlmopen as well as dynamic TLS optimizations a new contract is specified for use of static TLS: - libc.so can have up to 192 bytes of IE TLS, - other system libraries together can have up to 144 bytes of IE TLS. - By default 512 bytes of "optional" static TLS is available for opportunistic use. - By default at most 4 dlmopen namespaces are supported. So the surplus TLS requirement is 3*192 + 4*144 + 512 = 1664 bytes with dynamic linking (i.e. the same as before: the externally visible behaviour is not changed, other than limiting static TLS use for optimizations on affected targets.) The optional TLS available for opportunistic use is now tunable (dl.optional_static_tls), so users can directly affect the allocated static TLS size. (Note that module unloading with dlclose does not reclaim static TLS. After the optional TLS runs out, TLS access is no longer optimized.) Since users may need more dlmopen namespaces (5 .. 16) the maximum supported number is now a tunable (dl.nns), when it is increased the static TLS allocation increases according to the contract. If users use more namespaces than the tunable setting, static TLS may run out. Or if users dynamically load libraries with IE TLS beyond what's allowed by the contract, static TLS may run out. These conditions are not checked or enforced, but the user's responsibility. Static linking used fixed 2048 bytes surplus TLS, this is changed so the same contract is used as for dynamic linking. However with static linking DL_NNS == 1 so dl.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. Tested-by: Carlos O'Donell --- 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-start.c | 2 + csu/libc-tls.c | 28 +++++------ elf/Makefile | 17 ++++++- elf/dl-reloc.c | 37 ++++++++++---- elf/dl-sysdep.c | 2 + elf/dl-tls.c | 56 +++++++++++++++++++-- elf/dl-tunables.h | 4 ++ elf/dl-tunables.list | 13 +++++ elf/dynamic-link.h | 5 +- elf/tst-tls-ie-mod.h | 40 +++++++++++++++ elf/tst-tls-ie-mod0.c | 4 ++ elf/tst-tls-ie-mod1.c | 4 ++ elf/tst-tls-ie-mod2.c | 4 ++ elf/tst-tls-ie-mod3.c | 4 ++ elf/tst-tls-ie-mod4.c | 4 ++ elf/tst-tls-ie-mod5.c | 4 ++ elf/tst-tls-ie.c | 100 +++++++++++++++++++++++++++++++++++++ manual/tunables.texi | 28 +++++++++++ sysdeps/generic/ldsodefs.h | 8 +++ 19 files changed, 332 insertions(+), 32 deletions(-) create mode 100644 elf/tst-tls-ie-mod.h create mode 100644 elf/tst-tls-ie-mod0.c create mode 100644 elf/tst-tls-ie-mod1.c create mode 100644 elf/tst-tls-ie-mod2.c create mode 100644 elf/tst-tls-ie-mod3.c create mode 100644 elf/tst-tls-ie-mod4.c create mode 100644 elf/tst-tls-ie-mod5.c create mode 100644 elf/tst-tls-ie.c diff --git a/csu/libc-start.c b/csu/libc-start.c index 4005caf84a..2396956266 100644 --- a/csu/libc-start.c +++ b/csu/libc-start.c @@ -190,6 +190,8 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), __tunables_init (__environ); + _dl_static_tls_tunables_init (); + ARCH_INIT_CPU_FEATURES (); /* Perform IREL{,A} relocations. */ diff --git a/csu/libc-tls.c b/csu/libc-tls.c index 73ade0fec5..62f0b0c8c3 100644 --- a/csu/libc-tls.c +++ b/csu/libc-tls.c @@ -46,13 +46,19 @@ 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; +/* Remaining amount of static TLS that may be used for optimizing + dynamic TLS access (e.g. with TLSDESC). */ +size_t _dl_tls_static_optional; /* Generation counter for the dtv. */ size_t _dl_tls_generation; @@ -81,10 +87,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; @@ -129,21 +133,17 @@ __libc_setup_tls (void) '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/Makefile b/elf/Makefile index 6fe1df90bb..b8bde1f47d 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -204,7 +204,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-dlopen-self tst-auditmany tst-initfinilazyfail tst-dlopenfail \ tst-dlopenfail-2 \ tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \ - tst-audit14 tst-audit15 tst-audit16 + tst-audit14 tst-audit15 tst-audit16 \ + tst-tls-ie # reldep9 tests-internal += loadtest unload unload2 circleload1 \ neededtest neededtest2 neededtest3 neededtest4 \ @@ -317,7 +318,10 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \ tst-dlopenfailmod1 tst-dlopenfaillinkmod tst-dlopenfailmod2 \ tst-dlopenfailmod3 tst-ldconfig-ld-mod \ tst-filterobj-flt tst-filterobj-aux tst-filterobj-filtee \ - tst-auditlogmod-1 tst-auditlogmod-2 tst-auditlogmod-3 + tst-auditlogmod-1 tst-auditlogmod-2 tst-auditlogmod-3 \ + tst-tls-ie-mod0 tst-tls-ie-mod1 tst-tls-ie-mod2 \ + tst-tls-ie-mod3 tst-tls-ie-mod4 tst-tls-ie-mod5 + # Most modules build with _ISOMAC defined, but those filtered out # depend on internal headers. modules-names-tests = $(filter-out ifuncmod% tst-libc_dlvsym-dso tst-tlsmod%,\ @@ -1748,3 +1752,12 @@ $(objpfx)tst-auxobj: $(objpfx)tst-filterobj-aux.so $(objpfx)tst-auxobj-dlopen: $(libdl) $(objpfx)tst-auxobj.out: $(objpfx)tst-filterobj-filtee.so $(objpfx)tst-auxobj-dlopen.out: $(objpfx)tst-filterobj-filtee.so + +$(objpfx)tst-tls-ie: $(libdl) $(shared-thread-library) +$(objpfx)tst-tls-ie.out: \ + $(objpfx)tst-tls-ie-mod0.so \ + $(objpfx)tst-tls-ie-mod1.so \ + $(objpfx)tst-tls-ie-mod2.so \ + $(objpfx)tst-tls-ie-mod3.so \ + $(objpfx)tst-tls-ie-mod4.so \ + $(objpfx)tst-tls-ie-mod5.so diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c index ffcc84d396..6d32e49467 100644 --- a/elf/dl-reloc.c +++ b/elf/dl-reloc.c @@ -39,13 +39,16 @@ /* We are trying to perform a static TLS relocation in MAP, but it was dynamically loaded. This can only work if there is enough surplus in the static TLS area already allocated for each running thread. If this - object's TLS segment is too big to fit, we fail. If it fits, - we set MAP->l_tls_offset and return. - This function intentionally does not return any value but signals error - directly, as static TLS should be rare and code handling it should - not be inlined as much as possible. */ + object's TLS segment is too big to fit, we fail with -1. If it fits, + we set MAP->l_tls_offset and return 0. + A portion of the surplus static TLS can be optionally used to optimize + dynamic TLS access (with TLSDESC or powerpc TLS optimizations). + If OPTIONAL is true then TLS is allocated for such optimization and + the caller must have a fallback in case the optional portion of surplus + TLS runs out. If OPTIONAL is false then the entire surplus TLS area is + considered and the allocation only fails if that runs out. */ int -_dl_try_allocate_static_tls (struct link_map *map) +_dl_try_allocate_static_tls (struct link_map *map, bool optional) { /* If we've already used the variable with dynamic access, or if the alignment requirements are too high, fail. */ @@ -68,8 +71,14 @@ _dl_try_allocate_static_tls (struct link_map *map) size_t n = (freebytes - blsize) / map->l_tls_align; - size_t offset = GL(dl_tls_static_used) + (freebytes - n * map->l_tls_align - - map->l_tls_firstbyte_offset); + /* Account optional static TLS surplus usage. */ + size_t use = freebytes - n * map->l_tls_align - map->l_tls_firstbyte_offset; + if (optional && use > GL(dl_tls_static_optional)) + goto fail; + else if (optional) + GL(dl_tls_static_optional) -= use; + + size_t offset = GL(dl_tls_static_used) + use; map->l_tls_offset = GL(dl_tls_static_used) = offset; #elif TLS_DTV_AT_TP @@ -83,6 +92,13 @@ _dl_try_allocate_static_tls (struct link_map *map) if (used > GL(dl_tls_static_size)) goto fail; + /* Account optional static TLS surplus usage. */ + size_t use = used - GL(dl_tls_static_used); + if (optional && use > GL(dl_tls_static_optional)) + goto fail; + else if (optional) + GL(dl_tls_static_optional) -= use; + map->l_tls_offset = offset; map->l_tls_firstbyte_offset = GL(dl_tls_static_used); GL(dl_tls_static_used) = used; @@ -110,12 +126,15 @@ _dl_try_allocate_static_tls (struct link_map *map) return 0; } +/* This function intentionally does not return any value but signals error + directly, as static TLS should be rare and code handling it should + not be inlined as much as possible. */ void __attribute_noinline__ _dl_allocate_static_tls (struct link_map *map) { if (map->l_tls_offset == FORCED_DYNAMIC_TLS_OFFSET - || _dl_try_allocate_static_tls (map)) + || _dl_try_allocate_static_tls (map, false)) { _dl_signal_error (0, map->l_name, NULL, N_("\ cannot allocate memory in static TLS block")); diff --git a/elf/dl-sysdep.c b/elf/dl-sysdep.c index 854570821c..68a780dcbd 100644 --- a/elf/dl-sysdep.c +++ b/elf/dl-sysdep.c @@ -222,6 +222,8 @@ _dl_sysdep_start (void **start_argptr, __tunables_init (_environ); + _dl_static_tls_tunables_init (); + #ifdef DL_SYSDEP_INIT DL_SYSDEP_INIT; #endif diff --git a/elf/dl-tls.c b/elf/dl-tls.c index fa03234610..740e33ea91 100644 --- a/elf/dl-tls.c +++ b/elf/dl-tls.c @@ -29,10 +29,55 @@ #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 dl +#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 dl.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 dl.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 +void +_dl_static_tls_tunables_init (void) +{ + size_t nns, opt_tls; + +#if HAVE_TUNABLES + nns = TUNABLE_GET (nns, size_t, NULL); + opt_tls = TUNABLE_GET (optional_static_tls, size_t, NULL); +#else + /* Default values of the tunables. */ + nns = 4; + opt_tls = 512; +#endif + if (nns > DL_NNS) + nns = DL_NNS; + GL(dl_tls_static_optional) = opt_tls; + GLRO(dl_tls_static_surplus) = ((nns - 1) * LIBC_IE_TLS + + nns * OTHER_IE_TLS + + opt_tls); +} /* Out-of-memory handler. */ static void @@ -218,7 +263,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 +308,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.h b/elf/dl-tunables.h index 969e50327b..678f447e09 100644 --- a/elf/dl-tunables.h +++ b/elf/dl-tunables.h @@ -128,4 +128,8 @@ tunable_is_name (const char *orig, const char *envname) } #endif + +/* Initializers of tunables in the dl tunable namespace. */ +void _dl_static_tls_tunables_init (void); + #endif diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list index 0d398dd251..ce46f28c7a 100644 --- a/elf/dl-tunables.list +++ b/elf/dl-tunables.list @@ -126,4 +126,17 @@ glibc { default: 3 } } + + dl { + nns { + type: SIZE_T + minval: 1 + maxval: 16 + default: 4 + } + optional_static_tls { + type: SIZE_T + default: 512 + } + } } diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h index bb7a66f4cd..6727233e1a 100644 --- a/elf/dynamic-link.h +++ b/elf/dynamic-link.h @@ -40,9 +40,10 @@ (__builtin_expect ((sym_map)->l_tls_offset \ != FORCED_DYNAMIC_TLS_OFFSET, 1) \ && (__builtin_expect ((sym_map)->l_tls_offset != NO_TLS_OFFSET, 1) \ - || _dl_try_allocate_static_tls (sym_map) == 0)) + || _dl_try_allocate_static_tls (sym_map, true) == 0)) -int _dl_try_allocate_static_tls (struct link_map *map) attribute_hidden; +int _dl_try_allocate_static_tls (struct link_map *map, bool optional) + attribute_hidden; #include diff --git a/elf/tst-tls-ie-mod.h b/elf/tst-tls-ie-mod.h new file mode 100644 index 0000000000..46b362a9b7 --- /dev/null +++ b/elf/tst-tls-ie-mod.h @@ -0,0 +1,40 @@ +/* Module with specified TLS size and model. + 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 + . */ + +/* This file is parameterized by macros N, SIZE and MODEL. */ + +#include +#include + +#define CONCATX(x, y) x ## y +#define CONCAT(x, y) CONCATX (x, y) +#define STRX(x) #x +#define STR(x) STRX (x) + +#define VAR CONCAT (var, N) + +__attribute__ ((aligned (8), tls_model (MODEL))) +__thread char VAR[SIZE]; + +void +CONCAT (access, N) (void) +{ + printf (STR (VAR) "[%d]:\t %p .. %p " MODEL "\n", SIZE, VAR, VAR + SIZE); + fflush (stdout); + memset (VAR, 1, SIZE); +} diff --git a/elf/tst-tls-ie-mod0.c b/elf/tst-tls-ie-mod0.c new file mode 100644 index 0000000000..2450686e40 --- /dev/null +++ b/elf/tst-tls-ie-mod0.c @@ -0,0 +1,4 @@ +#define N 0 +#define SIZE 480 +#define MODEL "global-dynamic" +#include "tst-tls-ie-mod.h" diff --git a/elf/tst-tls-ie-mod1.c b/elf/tst-tls-ie-mod1.c new file mode 100644 index 0000000000..849ff91e53 --- /dev/null +++ b/elf/tst-tls-ie-mod1.c @@ -0,0 +1,4 @@ +#define N 1 +#define SIZE 120 +#define MODEL "global-dynamic" +#include "tst-tls-ie-mod.h" diff --git a/elf/tst-tls-ie-mod2.c b/elf/tst-tls-ie-mod2.c new file mode 100644 index 0000000000..23915ab67b --- /dev/null +++ b/elf/tst-tls-ie-mod2.c @@ -0,0 +1,4 @@ +#define N 2 +#define SIZE 24 +#define MODEL "global-dynamic" +#include "tst-tls-ie-mod.h" diff --git a/elf/tst-tls-ie-mod3.c b/elf/tst-tls-ie-mod3.c new file mode 100644 index 0000000000..5395f844a5 --- /dev/null +++ b/elf/tst-tls-ie-mod3.c @@ -0,0 +1,4 @@ +#define N 3 +#define SIZE 16 +#define MODEL "global-dynamic" +#include "tst-tls-ie-mod.h" diff --git a/elf/tst-tls-ie-mod4.c b/elf/tst-tls-ie-mod4.c new file mode 100644 index 0000000000..93ac2eacae --- /dev/null +++ b/elf/tst-tls-ie-mod4.c @@ -0,0 +1,4 @@ +#define N 4 +#define SIZE 1024 +#define MODEL "initial-exec" +#include "tst-tls-ie-mod.h" diff --git a/elf/tst-tls-ie-mod5.c b/elf/tst-tls-ie-mod5.c new file mode 100644 index 0000000000..84b3fd285b --- /dev/null +++ b/elf/tst-tls-ie-mod5.c @@ -0,0 +1,4 @@ +#define N 5 +#define SIZE 128 +#define MODEL "initial-exec" +#include "tst-tls-ie-mod.h" diff --git a/elf/tst-tls-ie.c b/elf/tst-tls-ie.c new file mode 100644 index 0000000000..2f00a2936d --- /dev/null +++ b/elf/tst-tls-ie.c @@ -0,0 +1,100 @@ +/* Test dlopen of modules with initial-exec TLS. + Copyright (C) 2016-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 + . */ + +/* This test tries to check that surplus static TLS is not used up for + dynamic TLS optimizations and 3*192 + 4*144 = 1152 bytes of static + TLS is available for dlopening modules with initial-exec TLS. It + depends on dl.nns=4 and dl.optional_static_tls=512 tunable setting. */ + +#include +#include +#include +#include +#include + +static int do_test (void); +#include +#include +#include + +/* Have some big TLS in the main exe: should not use surplus TLS. */ +__thread char maintls[1000]; + +static pthread_barrier_t barrier; + +/* Forces multi-threaded behaviour. */ +static void * +blocked_thread_func (void *closure) +{ + xpthread_barrier_wait (&barrier); + /* TLS load and access tests run here in the main thread. */ + xpthread_barrier_wait (&barrier); + return NULL; +} + +static void * +load_and_access (const char *mod, const char *func) +{ + /* Load module with TLS. */ + void *p = xdlopen (mod, RTLD_NOW); + /* Access the TLS variable to ensure it is allocated. */ + void (*f) (void) = (void (*) (void))xdlsym (p, func); + f (); + return p; +} + +static int +do_test (void) +{ + void *mods[6]; + + { + int ret = pthread_barrier_init (&barrier, NULL, 2); + if (ret != 0) + { + errno = ret; + printf ("error: pthread_barrier_init: %m\n"); + exit (1); + } + } + + pthread_t blocked_thread = xpthread_create (NULL, blocked_thread_func, NULL); + xpthread_barrier_wait (&barrier); + + printf ("maintls[%zu]:\t %p .. %p\n", + sizeof maintls, maintls, maintls + sizeof maintls); + memset (maintls, 1, sizeof maintls); + + /* Load modules with dynamic TLS (may use surplus TLS opportunistically). */ + mods[0] = load_and_access ("tst-tls-ie-mod0.so", "access0"); + mods[1] = load_and_access ("tst-tls-ie-mod1.so", "access1"); + mods[2] = load_and_access ("tst-tls-ie-mod2.so", "access2"); + mods[3] = load_and_access ("tst-tls-ie-mod3.so", "access3"); + /* Load modules with initial-exec TLS (can only use surplus TLS). */ + mods[4] = load_and_access ("tst-tls-ie-mod4.so", "access4"); + mods[5] = load_and_access ("tst-tls-ie-mod5.so", "access5"); + + xpthread_barrier_wait (&barrier); + xpthread_join (blocked_thread); + + /* Close the modules. */ + for (int i = 0; i < 6; ++i) + xdlclose (mods[i]); + + return 0; +} diff --git a/manual/tunables.texi b/manual/tunables.texi index ec18b10834..437fdadff0 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,33 @@ 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 dl tunables + +@deftp {Tunable namespace} glibc.dl +Dynamic linker behavior can be modified by setting the +following tunables in the @code{dl} namespace: +@end deftp + +@deftp Tunable glibc.dl.nns +Sets the number of supported dynamic link namespaces for which enough +static TLS is allocated (see @code{dlmopen}). If more namespaces are +created then static TLS may run out at @code{dlopen} or @code{dlmopen} +time which is a non-recoverable failure. Currently this limit can be +set between 1 and 16 inclusive, the default is 4. If the limit is +increased then internally more static TLS is allocated to accomodate +system libraries with initial-exec TLS in all namespaces. +@end deftp + +@deftp Tunable glibc.dl.optional_static_tls +Sets the amount of surplus static TLS that may be used for optimizing +dynamic TLS access (only works on certain platforms, e.g. TLSDESC can +be optimized this way). The internal allocation of static TLS is +increased by this amount, the default is 512. +@end deftp + @node Elision Tunables @section Elision Tunables @cindex elision tunables diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 5ff4a2831b..cc7c682a81 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -442,6 +442,9 @@ struct rtld_global EXTERN size_t _dl_tls_static_used; /* Alignment requirement of the static TLS block. */ EXTERN size_t _dl_tls_static_align; + /* Remaining amount of static TLS that may be used for optimizing + dynamic TLS access (e.g. with TLSDESC). */ + EXTERN size_t _dl_tls_static_optional; /* Number of additional entries in the slotinfo array of each slotinfo list element. A large number makes it almost certain take we never @@ -583,6 +586,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. */