From patchwork Fri Jan 6 18:52:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 62807 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 6C8C53858D28 for ; Fri, 6 Jan 2023 18:53:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6C8C53858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673031207; bh=x+3W/rfWL1sk0l1+9yRfXa8/OCBoQM9nt52WeGPRMUA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=fgkHASSW6d36BRKcUm1f/Puz4XW+Ty4lnVQlwEcPsJV45XYZMsTNdm82ft5Z10zrj qqHg2sO+nvZ5/pp3SZTfiAHMNyfsFxPFR2Sekm4c2d7jFx/oHEPoTZ/hgO2MQGtkol OSF4NKmwmC1+4t/OCi+IYL5WXtvOXnfqYjWcodvc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-db5eur01on2048.outbound.protection.outlook.com [40.107.15.48]) by sourceware.org (Postfix) with ESMTPS id F0F593858D28 for ; Fri, 6 Jan 2023 18:53:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F0F593858D28 Received: from DB6P195CA0015.EURP195.PROD.OUTLOOK.COM (2603:10a6:4:cb::25) by GV2PR08MB9949.eurprd08.prod.outlook.com (2603:10a6:150:ba::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5986.14; Fri, 6 Jan 2023 18:53:00 +0000 Received: from DBAEUR03FT047.eop-EUR03.prod.protection.outlook.com (2603:10a6:4:cb:cafe::9d) by DB6P195CA0015.outlook.office365.com (2603:10a6:4:cb::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5986.15 via Frontend Transport; Fri, 6 Jan 2023 18:52:59 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass 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; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DBAEUR03FT047.mail.protection.outlook.com (100.127.143.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5944.17 via Frontend Transport; Fri, 6 Jan 2023 18:52:59 +0000 Received: ("Tessian outbound 0d7b2ab0f13d:v132"); Fri, 06 Jan 2023 18:52:59 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 946ee622eec2c3fe X-CR-MTA-TID: 64aa7808 Received: from 9d44ccaada05.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id EB4F228D-E7C5-431C-8CD6-9BB0FE52790C.1; Fri, 06 Jan 2023 18:52:53 +0000 Received: from EUR02-DB5-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 9d44ccaada05.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 06 Jan 2023 18:52:53 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HnlpobPF16s5TJutib35W6rvc7bbV08KN1IsmIdv3+61chQn6rDkwEgDK5wtQWQeB+xE/8qr52LpvhvHCufZBskVRr0qC07+VkEci+9ZgoA20L4itgpP4akT0X4VweymMd2jWlfKFPsi2d7C0g181X6kLryPq5HEIe4TBz3XE6G6w+oDyvSzyCJVtgqRCWnf+Tp5EKqUb6jZG/b/WyT8V7YFpnu2BzFt0x7eOcpcv2KMhokBru+OkCTVFJwlpzJOfkKRjvn0KNSbdXBwNsBP42hwtp/ux7XiEQYzA7uHUXxJWz7w6sQjLQIF17cyTzX5mNm5a8ptSotANqhlUJz30A== 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-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=x+3W/rfWL1sk0l1+9yRfXa8/OCBoQM9nt52WeGPRMUA=; b=K07yVkCJal4UtkvchepImRXXi2DEtUjTDSwmWXht//HC6NDHzQli3a16MFmVejIWAcC7v+tK//SLLmGkpMXmFQ+419QtnvT+njz1ZDF1j+3pY/jlWgHjE1gN4GiL4a8oqtjkeuU3l4xyYp5j09HVsQjVHCyFyToRqLlhKEX8uaVYVjg4IT6ofUPnxzoBaiIhKlfuKnYKTsGj18wpBJFlGH031K/XiFJy1v5ivqZcInrK14043NtBpF4YNHLqVzfsVp/U+MxMAvwKlZQt/8gA34Qduglh5zMHcgBIwPOJYGvheYZ05ufCsNIxvlgofhfxuLVixml5xMqL+EboT46RSA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=sourceware.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=none (message not signed); arc=none Received: from DU2PR04CA0288.eurprd04.prod.outlook.com (2603:10a6:10:28c::23) by DU0PR08MB8686.eurprd08.prod.outlook.com (2603:10a6:10:402::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5986.9; Fri, 6 Jan 2023 18:52:51 +0000 Received: from DBAEUR03FT027.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:28c:cafe::7a) by DU2PR04CA0288.outlook.office365.com (2603:10a6:10:28c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5986.15 via Frontend Transport; Fri, 6 Jan 2023 18:52:51 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; pr=C Received: from nebula.arm.com (40.67.248.234) by DBAEUR03FT027.mail.protection.outlook.com (100.127.142.237) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5944.16 via Frontend Transport; Fri, 6 Jan 2023 18:52:50 +0000 Received: from AZ-NEU-EX03.Arm.com (10.251.24.31) by AZ-NEU-EX04.Arm.com (10.251.24.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Fri, 6 Jan 2023 18:52:50 +0000 Received: from armchair.cambridge.arm.com (10.2.80.71) by mail.arm.com (10.251.24.31) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Fri, 6 Jan 2023 18:52:50 +0000 To: Subject: [PATCH v3] elf: Fix slow tls access after dlopen [BZ #19924] Date: Fri, 6 Jan 2023 18:52:50 +0000 Message-ID: <20230106185250.2936935-1-szabolcs.nagy@arm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: DBAEUR03FT027:EE_|DU0PR08MB8686:EE_|DBAEUR03FT047:EE_|GV2PR08MB9949:EE_ X-MS-Office365-Filtering-Correlation-Id: e84f8941-6b03-4723-5643-08daf0173b6e x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: 0LYUFEXXBK+SOYSvWWtRMbL3iPUik4mUGSdeALFu/Afa5tVrmayMnA/q6UzgtZc/yy9hbqpaD58qZwdIQxv3UszOIohET5lyOED/OsH8F67xxH80P/pt6CkgL3GazkLvEzbt/wD6/1FZxI7ZwqDs6nvdShPlMAft55CpBilg68lvONgh0siwvN7bMWguRnODD1xOjAskbeuvdqQvBEk43dlA/OHIDDLEJUe1uKSS61ASbXpTBUcuIG5HQJCiENVUBf3mqbx4Sh6IBhe3idFo+myea9LpNJXPoVuAaxugGkqi5yGIRVhNBvI47hRFXw+jSuDgmR0DKrZcBNiyfKAZjQGG6DimC1ZZBquDgfuLkwuLewteY/w+AiQvCIYWsC44PyUlPAoIeCkYcnovl3H51DXsqgANAFjIstS+vL0QlVyYDrTwJf/srbYXy+qkWQCHsio9/3Q4Bbn0TProFHWKkfEINv2VvRye4AlDQ6monlg+5+EgQa6NFMcmkfU8LAkJQpGAfgmdAZ4MhT3cL+JE+5U0+Y1MtDO37WPcGa5rZkOS9tVv7gV+B6nvP2lGFT9QxmDaeDwSvq4nY+YwtNHu4qibzqtwmrS3AYtE6KnBgRH2Y2NNbsEngwkwz9YadjvPDcW8p3ywiterqRAkVGL+av/XV1ohdblcG8FDRne9hj8eSAUF15P6FD5S191Q7WsI6aQEgdtc/p74bPB7nb3NOo6hOWOjwzEaEWF4LnDGdY8= X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:nebula.arm.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230022)(4636009)(396003)(376002)(346002)(39860400002)(136003)(451199015)(36840700001)(46966006)(86362001)(40480700001)(36756003)(82310400005)(26005)(186003)(81166007)(478600001)(7696005)(8676002)(8936002)(70206006)(41300700001)(6916009)(70586007)(44832011)(5660300002)(316002)(30864003)(2906002)(2616005)(36860700001)(1076003)(426003)(82740400003)(47076005)(336012)(356005)(66899015)(83380400001)(21314003)(36900700001); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: DU0PR08MB8686 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DBAEUR03FT047.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 2ad9f37d-bc36-4b56-f919-08daf017362a X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 15+TcOHdr5vxAUdCnFIsaQraOFIStmAs9Ao+uNi0pkF1smumc57kX5WJ1teYiceTNze2J5YlfDRnXsrmg1ZEFHI/LhHEX2LkMCyyHZUdG7edPWD3HxWyB5VOSJSh4gB5x2CKu5HbytlOaNxRSmcppGKPKNnNV3nPz8EgAjLlx0H1MnNCTN0el6znoeozi0QHT5hI/2N+q737tme5U3vVHoGWw/yewzYhuVU/CFC8XSIzX3bNxZWTQPx4LKfdXFXMd8Nup56YAch0pZDqytZGSvvbL9Nihz0pQCagIzYPljNzKzUdN8I1S3H0bQ+37Yu8foA+hsKVh7gSZtniX1leI9810lNi1wNqL9tS4vg/syWqWScgoZTTtITktfDC28fgwq4qxwrSGhMKMRkZfh//P50dUcdIIQG5P9heHfm3DNfhelw4PULGXh93pGYKKkfKPr7LjoMjPNrgsK2wa0k+4tSCQE7Kmp5B+Xh+JALPgh0wNYbukKvq44wudpjUH+2Hwk8LQKzy+Oq0hwaPLFBGoeKvziE3lv3MjRGhxRmoijbLBoqE18Tc+QrJTCdV/mKjDJ9VHmZaviGH7ET9s9l8foIxVKANmpWRCYlhWNK5//B7A+ZmKPN3wUmOeg+qf90ur9IsweQooXo9m9hnKwy8usyOtsLRZdXFrmez/PMAJH+3xjHMdLbaJ7+W9/bNVfdp7GPH1iuau8sCdhIkudZw2qV0hfLsFNz7fQyymKyoJ0U= 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; SFS:(13230022)(4636009)(376002)(396003)(346002)(39860400002)(136003)(451199015)(40470700004)(36840700001)(46966006)(47076005)(40480700001)(36860700001)(7696005)(2906002)(426003)(44832011)(86362001)(30864003)(82740400003)(83380400001)(5660300002)(478600001)(316002)(40460700003)(82310400005)(2616005)(8676002)(186003)(26005)(1076003)(336012)(70586007)(70206006)(6916009)(66899015)(81166007)(8936002)(41300700001)(36756003)(21314003); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jan 2023 18:52:59.8191 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e84f8941-6b03-4723-5643-08daf0173b6e 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-AuthSource: DBAEUR03FT047.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: GV2PR08MB9949 X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, KAM_DMARC_NONE, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Szabolcs Nagy via Libc-alpha From: Szabolcs Nagy Reply-To: Szabolcs Nagy Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" In short: __tls_get_addr checks the global generation counter and if the current dtv is older then _dl_update_slotinfo updates dtv up to the generation of the accessed module. So if the global generation is newer than geneneration of the module then __tls_get_addr keeps hitting the slow dtv update path. The dtv update path includes a number of checks to see if any update is needed and this already causes measurable tls access slow down after dlopen. It may be possible to detect up-to-date dtv faster. But if there are many modules loaded (> TLS_SLOTINFO_SURPLUS) then this requires at least walking the slotinfo list. This patch tries to update the dtv to the global generation instead, so after a dlopen the tls access slow path is only hit once. The modules with larger generation than the accessed one were not necessarily synchronized before, so additional synchronization is needed. This patch uses acquire/release synchronization when accessing the generation counter. Note: in the x86_64 version of dl-tls.c the generation is only loaded once, since relaxed mo is not faster than acquire mo load. I have not benchmarked this yet. --- v3: updated concurrency notes. v2: rebased and updated the commit message a bit. still RFC quality. --- elf/dl-close.c | 2 +- elf/dl-open.c | 8 +-- elf/dl-reloc.c | 4 +- elf/dl-tls.c | 117 ++++++++++++++++++++----------------- sysdeps/generic/ldsodefs.h | 3 +- sysdeps/x86_64/dl-tls.c | 4 +- 6 files changed, 72 insertions(+), 66 deletions(-) diff --git a/elf/dl-close.c b/elf/dl-close.c index 14deca2e2b..ef909d8c66 100644 --- a/elf/dl-close.c +++ b/elf/dl-close.c @@ -703,7 +703,7 @@ _dl_close_worker (struct link_map *map, bool force) if (__glibc_unlikely (newgen == 0)) _dl_fatal_printf ("TLS generation counter wrapped! Please report as described in "REPORT_BUGS_TO".\n"); /* Can be read concurrently. */ - atomic_store_relaxed (&GL(dl_tls_generation), newgen); + atomic_store_release (&GL(dl_tls_generation), newgen); if (tls_free_end == GL(dl_tls_static_used)) GL(dl_tls_static_used) = tls_free_start; diff --git a/elf/dl-open.c b/elf/dl-open.c index e7db5e9642..734a0bee4e 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -405,7 +405,7 @@ update_tls_slotinfo (struct link_map *new) _dl_fatal_printf (N_("\ TLS generation counter wrapped! Please report this.")); /* Can be read concurrently. */ - atomic_store_relaxed (&GL(dl_tls_generation), newgen); + atomic_store_release (&GL(dl_tls_generation), newgen); /* We need a second pass for static tls data, because _dl_update_slotinfo must not be run while calls to @@ -422,8 +422,8 @@ TLS generation counter wrapped! Please report this.")); now, but we can delay updating the DTV. */ imap->l_need_tls_init = 0; #ifdef SHARED - /* Update the slot information data for at least the - generation of the DSO we are allocating data for. */ + /* Update the slot information data for the current + generation. */ /* FIXME: This can terminate the process on memory allocation failure. It is not possible to raise @@ -431,7 +431,7 @@ TLS generation counter wrapped! Please report this.")); _dl_update_slotinfo would have to be split into two operations, similar to resize_scopes and update_scopes above. This is related to bug 16134. */ - _dl_update_slotinfo (imap->l_tls_modid); + _dl_update_slotinfo (imap->l_tls_modid, newgen); #endif dl_init_static_tls (imap); diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c index 756bf950f6..e7b16a31ca 100644 --- a/elf/dl-reloc.c +++ b/elf/dl-reloc.c @@ -114,9 +114,7 @@ _dl_try_allocate_static_tls (struct link_map *map, bool optional) #ifdef SHARED if (__builtin_expect (THREAD_DTV()[0].counter != GL(dl_tls_generation), 0)) - /* Update the slot information data for at least the generation of - the DSO we are allocating data for. */ - (void) _dl_update_slotinfo (map->l_tls_modid); + (void) _dl_update_slotinfo (map->l_tls_modid, GL(dl_tls_generation)); #endif dl_init_static_tls (map); diff --git a/elf/dl-tls.c b/elf/dl-tls.c index 093cdddb7e..01dace09ca 100644 --- a/elf/dl-tls.c +++ b/elf/dl-tls.c @@ -721,57 +721,57 @@ allocate_and_init (struct link_map *map) struct link_map * -_dl_update_slotinfo (unsigned long int req_modid) +_dl_update_slotinfo (unsigned long int req_modid, size_t new_gen) { struct link_map *the_map = NULL; dtv_t *dtv = THREAD_DTV (); - /* The global dl_tls_dtv_slotinfo array contains for each module - index the generation counter current when the entry was created. + /* CONCURRENCY NOTES: + + The global dl_tls_dtv_slotinfo array contains for each module + index the generation counter current when that entry was updated. This array never shrinks so that all module indices which were - valid at some time can be used to access it. Before the first - use of a new module index in this function the array was extended - appropriately. Access also does not have to be guarded against - modifications of the array. It is assumed that pointer-size - values can be read atomically even in SMP environments. It is - possible that other threads at the same time dynamically load - code and therefore add to the slotinfo list. This is a problem - since we must not pick up any information about incomplete work. - The solution to this is to ignore all dtv slots which were - created after the one we are currently interested. We know that - dynamic loading for this module is completed and this is the last - load operation we know finished. */ - unsigned long int idx = req_modid; + valid at some time can be used to access it. Concurrent loading + and unloading of modules can update slotinfo entries or extend + the array. The updates happen under the GL(dl_load_tls_lock) and + finish with the release store of the generation counter to + GL(dl_tls_generation) which is synchronized with the load of + new_gen in the caller. So updates up to new_gen are synchronized + but updates for later generations may not be. + + Here we update the thread dtv from old_gen (== dtv[0].counter) to + new_gen generation. For this each dtv[i] entry is either set to + an unallocated state (set), or left unmodified (nop). Where (set) + may resize the dtv first if modid i >= dtv[-1].counter. The rules + for the decision between (set) and (nop) are + + (1) If slotinfo entry i is concurrently updated then either (set) + or (nop) is valid: TLS access cannot use dtv[i] unless it is + synchronized with a generation > new_gen. + + Otherwise if the generation of slotinfo entry i is gen and the + loaded module for this entry is map then + + (2) If gen <= old_gen then do (nop). + + (3) If old_gen < gen <= new_gen then + (3.1) if map != 0 then (set) + (3.2) if map == 0 then either (set) or (nop). + + Note that (1) cannot be reliably detected, but since both actions + are valid it does not have to be. Only (2) and (3.1) cases need + to be distinguished for which relaxed mo access of gen and map is + enough: their value is synchronized when it matters. + + Note that a relaxed mo load may give an out-of-thin-air value since + it is used in decisions that can affect concurrent stores. But this + should only happen if the OOTA value causes UB that justifies the + concurrent store of the value. This is not expected to be an issue + in practice. */ struct dtv_slotinfo_list *listp = GL(dl_tls_dtv_slotinfo_list); - while (idx >= listp->len) + if (dtv[0].counter < new_gen) { - idx -= listp->len; - listp = listp->next; - } - - if (dtv[0].counter < listp->slotinfo[idx].gen) - { - /* CONCURRENCY NOTES: - - Here the dtv needs to be updated to new_gen generation count. - - This code may be called during TLS access when GL(dl_load_tls_lock) - is not held. In that case the user code has to synchronize with - dlopen and dlclose calls of relevant modules. A module m is - relevant if the generation of m <= new_gen and dlclose of m is - synchronized: a memory access here happens after the dlopen and - before the dlclose of relevant modules. The dtv entries for - relevant modules need to be updated, other entries can be - arbitrary. - - This e.g. means that the first part of the slotinfo list can be - accessed race free, but the tail may be concurrently extended. - Similarly relevant slotinfo entries can be read race free, but - other entries are racy. However updating a non-relevant dtv - entry does not affect correctness. For a relevant module m, - max_modid >= modid of m. */ - size_t new_gen = listp->slotinfo[idx].gen; size_t total = 0; size_t max_modid = atomic_load_relaxed (&GL(dl_tls_max_dtv_idx)); assert (max_modid >= req_modid); @@ -784,31 +784,33 @@ _dl_update_slotinfo (unsigned long int req_modid) { size_t modid = total + cnt; - /* Later entries are not relevant. */ + /* Case (1) for all later modids. */ if (modid > max_modid) break; size_t gen = atomic_load_relaxed (&listp->slotinfo[cnt].gen); + /* Case (1). */ if (gen > new_gen) - /* Not relevant. */ continue; - /* If the entry is older than the current dtv layout we - know we don't have to handle it. */ + /* Case (2) or (1). */ if (gen <= dtv[0].counter) continue; + /* Case (3) or (1). */ + /* If there is no map this means the entry is empty. */ struct link_map *map = atomic_load_relaxed (&listp->slotinfo[cnt].map); /* Check whether the current dtv array is large enough. */ if (dtv[-1].counter < modid) { + /* Case (3.2) or (1). */ if (map == NULL) continue; - /* Resize the dtv. */ + /* Resizing the dtv aborts on failure: bug 16134. */ dtv = _dl_resize_dtv (dtv, max_modid); assert (modid <= dtv[-1].counter); @@ -819,7 +821,7 @@ _dl_update_slotinfo (unsigned long int req_modid) } /* If there is currently memory allocate for this - dtv entry free it. */ + dtv entry free it. Note: this is not AS-safe. */ /* XXX Ideally we will at some point create a memory pool. */ free (dtv[modid].pointer.to_free); @@ -914,9 +916,9 @@ tls_get_addr_tail (GET_ADDR_ARGS, dtv_t *dtv, struct link_map *the_map) static struct link_map * __attribute_noinline__ -update_get_addr (GET_ADDR_ARGS) +update_get_addr (GET_ADDR_ARGS, size_t gen) { - struct link_map *the_map = _dl_update_slotinfo (GET_ADDR_MODULE); + struct link_map *the_map = _dl_update_slotinfo (GET_ADDR_MODULE, gen); dtv_t *dtv = THREAD_DTV (); void *p = dtv[GET_ADDR_MODULE].pointer.val; @@ -946,12 +948,17 @@ __tls_get_addr (GET_ADDR_ARGS) dtv_t *dtv = THREAD_DTV (); /* Update is needed if dtv[0].counter < the generation of the accessed - module. The global generation counter is used here as it is easier - to check. Synchronization for the relaxed MO access is guaranteed - by user code, see CONCURRENCY NOTES in _dl_update_slotinfo. */ + module, but the global generation counter is easier to check (which + must be synchronized up to the generation of the accessed module by + user code doing the TLS access so relaxed mo read is enough). */ size_t gen = atomic_load_relaxed (&GL(dl_tls_generation)); if (__glibc_unlikely (dtv[0].counter != gen)) - return update_get_addr (GET_ADDR_PARAM); + { + /* Update DTV up to the global generation, see CONCURRENCY NOTES + in _dl_update_slotinfo. */ + gen = atomic_load_acquire (&GL(dl_tls_generation)); + return update_get_addr (GET_ADDR_PARAM, gen); + } void *p = dtv[GET_ADDR_MODULE].pointer.val; diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 9dae72b1ed..20c5017878 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1266,7 +1266,8 @@ extern void _dl_add_to_slotinfo (struct link_map *l, bool do_add) /* Update slot information data for at least the generation of the module with the given index. */ -extern struct link_map *_dl_update_slotinfo (unsigned long int req_modid) +extern struct link_map *_dl_update_slotinfo (unsigned long int req_modid, + size_t gen) attribute_hidden; /* Look up the module's TLS block as for __tls_get_addr, diff --git a/sysdeps/x86_64/dl-tls.c b/sysdeps/x86_64/dl-tls.c index 24a6e643b0..f74b20dd8d 100644 --- a/sysdeps/x86_64/dl-tls.c +++ b/sysdeps/x86_64/dl-tls.c @@ -40,9 +40,9 @@ __tls_get_addr_slow (GET_ADDR_ARGS) { dtv_t *dtv = THREAD_DTV (); - size_t gen = atomic_load_relaxed (&GL(dl_tls_generation)); + size_t gen = atomic_load_acquire (&GL(dl_tls_generation)); if (__glibc_unlikely (dtv[0].counter != gen)) - return update_get_addr (GET_ADDR_PARAM); + return update_get_addr (GET_ADDR_PARAM, gen); return tls_get_addr_tail (GET_ADDR_PARAM, dtv, NULL); }