From patchwork Wed Sep 22 07:50:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 45279 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 1BD143858D39 for ; Wed, 22 Sep 2021 07:51:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1BD143858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1632297092; bh=BMC2S9HkSUGXuXSInWTSD7Horce/80hIAERXilgYZhM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=n2NQvpMMFD5s6v1dKzqp4V5ZMPBn9SAnGWFZI6d0oumFU2SIo61sHxQ5+h6sYp8CC i+ZluXZpg/6VrS2/t1/Kiv5zibCVKWwIURA/2m4nhg6yWuuLXIIxWN1fH3apQc7Wkg /LabRSgL1daTjaCHiU8IbBcfYOwbw4uScr5RTQTs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR02-AM5-obe.outbound.protection.outlook.com (mail-eopbgr00057.outbound.protection.outlook.com [40.107.0.57]) by sourceware.org (Postfix) with ESMTPS id 2D8B93858D39 for ; Wed, 22 Sep 2021 07:51:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2D8B93858D39 Received: from AM5PR0101CA0018.eurprd01.prod.exchangelabs.com (2603:10a6:206:16::31) by AM9PR08MB7227.eurprd08.prod.outlook.com (2603:10a6:20b:412::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.14; Wed, 22 Sep 2021 07:51:06 +0000 Received: from AM5EUR03FT027.eop-EUR03.prod.protection.outlook.com (2603:10a6:206:16:cafe::3a) by AM5PR0101CA0018.outlook.office365.com (2603:10a6:206:16::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.14 via Frontend Transport; Wed, 22 Sep 2021 07:51:06 +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=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; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM5EUR03FT027.mail.protection.outlook.com (10.152.16.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.13 via Frontend Transport; Wed, 22 Sep 2021 07:51:06 +0000 Received: ("Tessian outbound f1898412aff1:v103"); Wed, 22 Sep 2021 07:51:05 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 424a75dd853e9d29 X-CR-MTA-TID: 64aa7808 Received: from 93f83d637a9a.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id 5AAAA23B-190A-4C20-AF17-802FE0C744B5.1; Wed, 22 Sep 2021 07:50:58 +0000 Received: from EUR04-HE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id 93f83d637a9a.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Wed, 22 Sep 2021 07:50:58 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Lu2Gs731O88xRkCaR4M9bcUTSV9omvOxlC7cpg9Q6GPa1ymrGNM3KF2MRcatHniAEANTGXYjOjUfGLxnfjiVBuva2sJTAuxUUxYF4XacMrmguTesruOhTVhBEgxA5aPkh719l91frofO53SVQqDAshTq/8K9FkwPFVyckWoYL6NEl9oFgaXj37RhJf8pwRwVgHGgWP5ewS2NZgA1yNWg/pOhy1uY0OWkNxXDi3mdaZl0Oh/KPNWCQ+9dlSlNxR7WCzpxsDc5rD5YpaNdtsMUw4RJplfTBhuQoVUvJPmDzAWE/U+OsEU60K5nva+9+y2yhBxS72SDKvjpM1keq/qT3A== 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; bh=BMC2S9HkSUGXuXSInWTSD7Horce/80hIAERXilgYZhM=; b=RKV4PADmDCUYmWzJNZg3eDGzBCST8HDq//gkBUaQGoiW1kgH+4K00cDgnVLgg/MiDsQ7IfzaswixF4lQTl+IjE8EKGYzXdoXJHRSb1dhStFqjaBfYuMPfhyI9HyN1ffxzQJ2eizZ1onDRflWBTgxFDk+vV/L/tbfWexlZXaXxlAuFaxLhNzxH3/evb51enre8LUYfWsiKA83WokTJ74kCW3CMPAf0dEFViKrCSDSYAmpjcopsHZ671tekjbWGKQb6pujDpKOODZjogh5Rre7XucLJhbMiK7F7Et9JKZEL2KLSJWLEaDoEkZbqFFZpYTzQiY/IqdyZTGPpJEjvmPM2g== 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 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 PA4PR08MB6320.eurprd08.prod.outlook.com (2603:10a6:102:e5::9) by PAXPR08MB6878.eurprd08.prod.outlook.com (2603:10a6:102:139::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.16; Wed, 22 Sep 2021 07:50:55 +0000 Received: from PA4PR08MB6320.eurprd08.prod.outlook.com ([fe80::118b:49d3:ac4a:966f]) by PA4PR08MB6320.eurprd08.prod.outlook.com ([fe80::118b:49d3:ac4a:966f%8]) with mapi id 15.20.4544.013; Wed, 22 Sep 2021 07:50:55 +0000 To: libc-alpha@sourceware.org Subject: [PATCH 1/2] elf: Avoid deadlock between pthread_create and ctors [BZ #28357] Date: Wed, 22 Sep 2021 08:50:42 +0100 Message-Id: <20210922075042.20132-1-szabolcs.nagy@arm.com> X-Mailer: git-send-email 2.17.1 X-ClientProxiedBy: SN7P222CA0013.NAMP222.PROD.OUTLOOK.COM (2603:10b6:806:124::11) To PA4PR08MB6320.eurprd08.prod.outlook.com (2603:10a6:102:e5::9) MIME-Version: 1.0 Received: from localhost.localdomain (217.140.106.55) by SN7P222CA0013.NAMP222.PROD.OUTLOOK.COM (2603:10b6:806:124::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.13 via Frontend Transport; Wed, 22 Sep 2021 07:50:53 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8f25e938-7280-47fd-f0e1-08d97d9dbbb5 X-MS-TrafficTypeDiagnostic: PAXPR08MB6878:|AM9PR08MB7227: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:8882;OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: uH5A6HNGK31ldUzyuNa/wCc0SpUlcYVjTP0FaM/+FBqw2ywcX/PDFKQKKdD7tmI5lWX9SqDsJSHhyQhYKMLs0Wbf0lmt0EnZqNBTfCObyxgdiGKXdPdQXUeSDunQObq/jTwIe3QxEZiZMGFt3Z+zxAzLl1tTGAdLzXVnaeOKwPXZOJ1v/FhJS1tQByrQu3dEZZPEYEEKxOUKCdA+a15Rubj119WmsCSsNXp7P60aRXX3cAoUrZKFRIYWfXPEuW3kbG3B33WmkXYDyFkq/n2eYehTO7DCvNOw4cXjJDtk46iGokUpDCWVIahvjn6wcRMBb26VU62Ex3eDztt5ImhI3w+i3bNLWIK602GtbIetHGUa5P+7S0UTdd81sdFJqBbd2aYZJFrzi8ONU6Z1+3IEY52waBwMh8Wjb2uqoACTox9fuaWzAOdW8yC6RS1ZuXeC5JcSutgtVOQneRPkBcOvvZBXK5qb7mSnypRBAOMLqddPFHIKUd9biSG41L7kNLGJ7XdjZcHEDz3JGcNjnXBINamK88nTggDmwyVe9eQmPnB4P5MPWfXfugBPaZcoMn+QeLwzdgiFFmVEzgXX9LhTN3hWwjCBfJO7l+KehrZsOgtl2y2ilwNmxTbMrAM+XjhAygYvvBck+dXAEHEdY9HwlzmeumMibbfDTJYn89oL02yg5qdwKvq01IrRAbZA9K37iBEcM+B6nCFGxobO4O8zOw== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PA4PR08MB6320.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(316002)(6512007)(36756003)(6666004)(83380400001)(1076003)(86362001)(5660300002)(38100700002)(2906002)(38350700002)(186003)(8676002)(6916009)(66946007)(6486002)(6506007)(508600001)(26005)(8936002)(52116002)(44832011)(2616005)(66476007)(956004)(66556008); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: PAXPR08MB6878 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: AM5EUR03FT027.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 7d8e6122-a13c-42b6-141d-08d97d9db4c3 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: x44eBlVjmWzVXFUVLxGok7NoBL7PCifffTJflgx525gSebAgvQA4h5F7cszH419iceEXMgYgvjQ5hWFeZl+b0hIh8ynBJO7AbHZA4/rYXUF0w5IewacF+UrwyhoRMZw6v9zXbdaJrFoRbNd8DQUM/srriCJg1DibWtL73rewXCvcLrqI5s023hirNFiIv0UEcXLZdvqChhexbAeWosQvED4Mbbe1FyqdMmjfkQ+Pfvntme/6mS1K/UcPgRnYSOinJDNBltpwytbvylOw5IXQaZi0f+cYx8+oCFGTnlFk41mHG2cxtpzxPwJZAAmj5rfSOMBxedmbf1MSw/982SKt/Sstv/Y/JFQ/umJQ6Q+fOKkqpJDcNR1zQWWridksOxovRyK3R003XQA+J9uSDR20Oljoy6agJNbk9hlb27w3MLAnSq1kc6liQtJVBb3dXS7qYpp2Jno3zVNzglds+nHDq/qDFXSJjZ5ycgTQkL57TZ61ixnFcHTXyWLwSzaRIvlv+LBCqoEIYmEjW0JN4OES3xK8eutNSzvSe3x3+dlbDabpQFDqoKm/8VCx09mkXDuvIH97ZnhqshPmfVPVphpMDVSttlu/6VkVesakdqdLZEIPfYpg+qviro+Cva9318wkRPJojttzHuqwTENTQvPZPpSA4ReI9JMNnGREWKghE7ubuXmOHrjelJW3fByk6vht9T/RjjpCFQ5C7k+QJzb5gg== 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:(4636009)(46966006)(36840700001)(70206006)(70586007)(36860700001)(44832011)(5660300002)(47076005)(6506007)(508600001)(316002)(356005)(1076003)(26005)(82310400003)(86362001)(6512007)(6916009)(83380400001)(81166007)(2906002)(186003)(8936002)(956004)(336012)(6486002)(6666004)(8676002)(36756003)(2616005); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Sep 2021 07:51:06.1356 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8f25e938-7280-47fd-f0e1-08d97d9dbbb5 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: AM5EUR03FT027.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM9PR08MB7227 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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" The fix for bug 19329 caused a regression such that pthread_create can deadlock when concurrent ctors from dlopen are waiting for it to finish. Use a new GL(dl_load_tls_lock) in pthread_create that is not taken around ctors in dlopen. The new lock is also used in __tls_get_addr instead of GL(dl_load_lock). The new lock is held in _dl_open_worker and _dl_close_worker around most of the logic before/after the init/fini routines. When init/fini routines are running then TLS is in a consistent, usable state. In _dl_open_worker the new lock requires catching and reraising dlopen failures that happen in the critical section. The new lock is reinitialized in a fork child, to keep the existing behaviour and it is kept recursive in case malloc interposition or tls access from signal handlers can retake it. It is not obvious if this is necessary or helps, but avoids changing the preexisting behaviour. The new lock may be more appropriate for dl_iterate_phdr too than GL(dl_load_write_lock), since TLS state of an incompletely loaded module may be accessed. If the new lock can replace the old one, that can be a separate change. Fixes bug 28357. Reviewed-by: Adhemerval Zanella --- elf/dl-close.c | 6 ++++++ elf/dl-open.c | 35 ++++++++++++++++++++++++++++++++++- elf/dl-support.c | 7 +++++++ elf/dl-tls.c | 16 ++++++++-------- elf/rtld.c | 1 + posix/fork.c | 3 +++ sysdeps/generic/ldsodefs.h | 9 ++++++++- 7 files changed, 67 insertions(+), 10 deletions(-) diff --git a/elf/dl-close.c b/elf/dl-close.c index 93ff5c96e9..cfe0f1c0c9 100644 --- a/elf/dl-close.c +++ b/elf/dl-close.c @@ -549,6 +549,9 @@ _dl_close_worker (struct link_map *map, bool force) size_t tls_free_end; tls_free_start = tls_free_end = NO_TLS_OFFSET; + /* Protects global and module specitic TLS state. */ + __rtld_lock_lock_recursive (GL(dl_load_tls_lock)); + /* We modify the list of loaded objects. */ __rtld_lock_lock_recursive (GL(dl_load_write_lock)); @@ -784,6 +787,9 @@ _dl_close_worker (struct link_map *map, bool force) GL(dl_tls_static_used) = tls_free_start; } + /* TLS is cleaned up for the unloaded modules. */ + __rtld_lock_unlock_recursive (GL(dl_load_tls_lock)); + #ifdef SHARED /* Auditing checkpoint: we have deleted all objects. */ if (__glibc_unlikely (do_audit)) diff --git a/elf/dl-open.c b/elf/dl-open.c index a25443f6d1..071b678bf6 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -66,6 +66,9 @@ struct dl_open_args libc_map value in the namespace in case of a dlopen failure. */ bool libc_already_loaded; + /* Set to true if the end of dl_open_worker_begin was reached. */ + bool worker_continue; + /* Original parameters to the program and the current environment. */ int argc; char **argv; @@ -482,7 +485,7 @@ call_dl_init (void *closure) } static void -dl_open_worker (void *a) +dl_open_worker_begin (void *a) { struct dl_open_args *args = a; const char *file = args->file; @@ -774,6 +777,36 @@ dl_open_worker (void *a) _dl_call_libc_early_init (libc_map, false); } + args->worker_continue = true; +} + +static void +dl_open_worker (void *a) +{ + struct dl_open_args *args = a; + + args->worker_continue = false; + + { + /* Protects global and module specific TLS state. */ + __rtld_lock_lock_recursive (GL(dl_load_tls_lock)); + + struct dl_exception ex; + int err = _dl_catch_exception (&ex, dl_open_worker_begin, args); + + __rtld_lock_unlock_recursive (GL(dl_load_tls_lock)); + + if (__glibc_unlikely (ex.errstring != NULL)) + /* Reraise the error. */ + _dl_signal_exception (err, &ex, NULL); + } + + if (!args->worker_continue) + return; + + int mode = args->mode; + struct link_map *new = args->map; + /* Run the initializer functions of new objects. Temporarily disable the exception handler, so that lazy binding failures are fatal. */ diff --git a/elf/dl-support.c b/elf/dl-support.c index 02e2ed72f5..2a2d42a09f 100644 --- a/elf/dl-support.c +++ b/elf/dl-support.c @@ -228,6 +228,13 @@ __rtld_lock_define_initialized_recursive (, _dl_load_lock) list of loaded objects while an object is added to or removed from that list. */ __rtld_lock_define_initialized_recursive (, _dl_load_write_lock) + /* This lock protects global and module specific TLS related data. + E.g it is held in dlopen and dlclose when GL(dl_tls_generation), + GL(dl_tls_max_dtv_idx) or GL(dl_tls_dtv_slotinfo_list) are + accessed and when TLS related relocations are processed for + a module. It was introduced to keep pthread_create accessing + TLS state that is being set up. */ +__rtld_lock_define_initialized_recursive (, _dl_load_tls_lock) #ifdef HAVE_AUX_VECTOR diff --git a/elf/dl-tls.c b/elf/dl-tls.c index d554ae4497..9260d2d696 100644 --- a/elf/dl-tls.c +++ b/elf/dl-tls.c @@ -532,7 +532,7 @@ _dl_allocate_tls_init (void *result) size_t maxgen = 0; /* Protects global dynamic TLS related state. */ - __rtld_lock_lock_recursive (GL(dl_load_lock)); + __rtld_lock_lock_recursive (GL(dl_load_tls_lock)); /* Check if the current dtv is big enough. */ if (dtv[-1].counter < GL(dl_tls_max_dtv_idx)) @@ -606,7 +606,7 @@ _dl_allocate_tls_init (void *result) listp = listp->next; assert (listp != NULL); } - __rtld_lock_unlock_recursive (GL(dl_load_lock)); + __rtld_lock_unlock_recursive (GL(dl_load_tls_lock)); /* The DTV version is up-to-date now. */ dtv[0].counter = maxgen; @@ -745,7 +745,7 @@ _dl_update_slotinfo (unsigned long int req_modid) Here the dtv needs to be updated to new_gen generation count. - This code may be called during TLS access when GL(dl_load_lock) + 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 @@ -867,11 +867,11 @@ tls_get_addr_tail (GET_ADDR_ARGS, dtv_t *dtv, struct link_map *the_map) if (__glibc_unlikely (the_map->l_tls_offset != FORCED_DYNAMIC_TLS_OFFSET)) { - __rtld_lock_lock_recursive (GL(dl_load_lock)); + __rtld_lock_lock_recursive (GL(dl_load_tls_lock)); if (__glibc_likely (the_map->l_tls_offset == NO_TLS_OFFSET)) { the_map->l_tls_offset = FORCED_DYNAMIC_TLS_OFFSET; - __rtld_lock_unlock_recursive (GL(dl_load_lock)); + __rtld_lock_unlock_recursive (GL(dl_load_tls_lock)); } else if (__glibc_likely (the_map->l_tls_offset != FORCED_DYNAMIC_TLS_OFFSET)) @@ -883,7 +883,7 @@ tls_get_addr_tail (GET_ADDR_ARGS, dtv_t *dtv, struct link_map *the_map) #else # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" #endif - __rtld_lock_unlock_recursive (GL(dl_load_lock)); + __rtld_lock_unlock_recursive (GL(dl_load_tls_lock)); dtv[GET_ADDR_MODULE].pointer.to_free = NULL; dtv[GET_ADDR_MODULE].pointer.val = p; @@ -891,7 +891,7 @@ tls_get_addr_tail (GET_ADDR_ARGS, dtv_t *dtv, struct link_map *the_map) return (char *) p + GET_ADDR_OFFSET; } else - __rtld_lock_unlock_recursive (GL(dl_load_lock)); + __rtld_lock_unlock_recursive (GL(dl_load_tls_lock)); } struct dtv_pointer result = allocate_and_init (the_map); dtv[GET_ADDR_MODULE].pointer = result; @@ -962,7 +962,7 @@ _dl_tls_get_addr_soft (struct link_map *l) return NULL; dtv_t *dtv = THREAD_DTV (); - /* This may be called without holding the GL(dl_load_lock). Reading + /* This may be called without holding the GL(dl_load_tls_lock). Reading arbitrary gen value is fine since this is best effort code. */ size_t gen = atomic_load_relaxed (&GL(dl_tls_generation)); if (__glibc_unlikely (dtv[0].counter != gen)) diff --git a/elf/rtld.c b/elf/rtld.c index 742c413c48..096d1cc172 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -322,6 +322,7 @@ struct rtld_global _rtld_global = #ifdef _LIBC_REENTRANT ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER, ._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER, + ._dl_load_tls_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER, #endif ._dl_nns = 1, ._dl_ns = diff --git a/posix/fork.c b/posix/fork.c index c471f7b15f..021691b9b7 100644 --- a/posix/fork.c +++ b/posix/fork.c @@ -99,6 +99,9 @@ __libc_fork (void) /* Reset the lock the dynamic loader uses to protect its data. */ __rtld_lock_initialize (GL(dl_load_lock)); + /* Reset the lock protecting dynamic TLS related data. */ + __rtld_lock_initialize (GL(dl_load_tls_lock)); + reclaim_stacks (); /* Run the handlers registered for the child. */ diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 6e50fcd7cd..f946744e5d 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -372,6 +372,13 @@ struct rtld_global list of loaded objects while an object is added to or removed from that list. */ __rtld_lock_define_recursive (EXTERN, _dl_load_write_lock) + /* This lock protects global and module specific TLS related data. + E.g it is held in dlopen and dlclose when GL(dl_tls_generation), + GL(dl_tls_max_dtv_idx) or GL(dl_tls_dtv_slotinfo_list) are + accessed and when TLS related relocations are processed for + a module. It was introduced to keep pthread_create accessing + TLS state that is being set up. */ + __rtld_lock_define_recursive (EXTERN, _dl_load_tls_lock) /* Incremented whenever something may have been added to dl_loaded. */ EXTERN unsigned long long _dl_load_adds; @@ -1271,7 +1278,7 @@ extern int _dl_scope_free (void *) attribute_hidden; /* Add module to slot information data. If DO_ADD is false, only the required memory is allocated. Must be called with GL - (dl_load_lock) acquired. If the function has already been called + (dl_load_tls_lock) acquired. If the function has already been called for the link map L with !do_add, then this function will not raise an exception, otherwise it is possible that it encounters a memory allocation failure. */