From patchwork Fri Oct 1 15:24:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 45689 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 B31D33857804 for ; Fri, 1 Oct 2021 15:25:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B31D33857804 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633101927; bh=WCWjagK+TemXUmkjEu2hchU12OBKKNMbWGd3U4Q+92w=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ICRtslLrdDhRJitqrC/xMrT05y7yB6A0Y4mwcWpJev+fMqS9ZPupLsZ7ur+DVQD6Q 35BraxRWUpsOJFDXFL8RZeFKqu53i/8SNbPiV4+y0j+l6LjyRDb5J3qq3Trp2j+f6f xHUGz4pZ9Apy0Fq6fE9ayXxpjycyKw2vhP8MyU8Y= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2062.outbound.protection.outlook.com [40.107.22.62]) by sourceware.org (Postfix) with ESMTPS id 8BBCC3858406 for ; Fri, 1 Oct 2021 15:25:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8BBCC3858406 Received: from AM6P191CA0085.EURP191.PROD.OUTLOOK.COM (2603:10a6:209:8a::26) by VI1PR08MB3583.eurprd08.prod.outlook.com (2603:10a6:803:84::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4544.15; Fri, 1 Oct 2021 15:24:58 +0000 Received: from VE1EUR03FT054.eop-EUR03.prod.protection.outlook.com (2603:10a6:209:8a:cafe::e2) by AM6P191CA0085.outlook.office365.com (2603:10a6:209:8a::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.14 via Frontend Transport; Fri, 1 Oct 2021 15:24:58 +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 VE1EUR03FT054.mail.protection.outlook.com (10.152.19.64) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.14 via Frontend Transport; Fri, 1 Oct 2021 15:24:58 +0000 Received: ("Tessian outbound c21c48fbc857:v103"); Fri, 01 Oct 2021 15:24:57 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 894bef1c6c03507f X-CR-MTA-TID: 64aa7808 Received: from f5172cbb129c.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id A979C188-2E86-4ADC-A17E-1050F69E466A.1; Fri, 01 Oct 2021 15:24:44 +0000 Received: from EUR05-DB8-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id f5172cbb129c.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Fri, 01 Oct 2021 15:24:44 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mjfnynO8mvZBv8Slcles3V2JRZICiWv9mSEAn6b63Df1R0Frix01gwm3mE/UPY55LhZ8TaTRkfgv7DktqAUXUNQoVFuShesq8yLwiUrX6MRznUXi4u4l4GggknIkYyXbQwl8KsTA+TKiHiMXCFK/O/EjSK2nczKrfO9Kurd5wVA4AIzZ3WYcPzCVvNWqJqdWP8jrrgXdiIeHmap1shO8jxn5UZVX8lgyAg0HIdmiW+L6T+6/voKkGrQa1csxhTtc+fmakObPKJDYG0MpJGy23d/Siis2bblHa0C4hlAwCXOeXZxBr8xzLZA14YNZdkBBH4ehEfs3lZdBtgPH32PGYw== 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=WCWjagK+TemXUmkjEu2hchU12OBKKNMbWGd3U4Q+92w=; b=KUv7zXPm4hZQKxz/X+uxLuV03XipgrfSYeQviPuN9ydSrjoIh8pTeXdCNZ174YxMc4HEqwTjibjRBE3NJwwI42we136D6SDwXGIYam/xtdsHvSmQ97job/O3N9xQZ4ZwG4s6S2+LSCp1MAwm0OgaNP6xgLqKD+Qs6oTvnx+QSOcVmm/4Ha67+fcmsl4sxoaLb0FIDtBKE+GCd40cS7oAO5qrAurQFXntUPwPdq+23kwrTaF19JL29uMoMQ/SUfd9TnhQ8ST8LvUTuqnA8djJLZ64YRtToqtQZWBTvozq25mUNsh2SNqcnDfIfqCHPYf1mL4pvfhNRVlMrsVqMmtclg== 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 VI1PR0801MB1854.eurprd08.prod.outlook.com (2603:10a6:800:5c::15) by VI1PR0801MB1870.eurprd08.prod.outlook.com (2603:10a6:800:85::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.13; Fri, 1 Oct 2021 15:24:41 +0000 Received: from VI1PR0801MB1854.eurprd08.prod.outlook.com ([fe80::a592:9d43:f9d3:bad7]) by VI1PR0801MB1854.eurprd08.prod.outlook.com ([fe80::a592:9d43:f9d3:bad7%5]) with mapi id 15.20.4544.022; Fri, 1 Oct 2021 15:24:41 +0000 To: libc-alpha@sourceware.org Subject: [PATCH v2] elf: Avoid deadlock between pthread_create and ctors [BZ #28357] Date: Fri, 1 Oct 2021 16:24:34 +0100 Message-Id: <20211001152434.2359690-1-szabolcs.nagy@arm.com> X-Mailer: git-send-email 2.25.1 X-ClientProxiedBy: LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) To VI1PR0801MB1854.eurprd08.prod.outlook.com (2603:10a6:800:5c::15) MIME-Version: 1.0 Received: from armchair.cambridge.arm.com (217.140.106.50) by LO2P265CA0379.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.15 via Frontend Transport; Fri, 1 Oct 2021 15:24:40 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 03e1fbaa-8ab9-48bd-1442-08d984efa14a X-MS-TrafficTypeDiagnostic: VI1PR0801MB1870:|VI1PR08MB3583: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:7691;OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: wUlJU9YYBMKut+I+AcsgqgZg16wzRNR0ARM7EK394oRGh5tPfzHzwg0nahk8IKZrt+srj7ZFM0OJD4QHYcwUwz0vdpuxtSpOy6VbKEjJl+RyB282LxWWWtI1Fjkek5Kxh6rBZG/jv3eYSssJsOrxjqMH/6hyuLCGr/gKY83lfN+oC6WLneHOkt2+qcpRsFJ/1oZTHmdppqXOjM0ePsVAUcB9T/sXnKEO/9sV7jLS7IYUOHO4Axmx3+M9979dWINrEMLP34Efuts4qH3womVg6Ige22y6W6mSdQqOljjP0A3/jbpYl/NnGxNcQMRVxVVkxDpUGCa6SQa3qUfeyhQ380T4A/OXvXyyATcWNC9tkUPqr/aBpPW9vnxK8UFqaLkY3YLYjSIKtqqvwkm/xCPkYm92payqzpeuSMZjhwehrRY+oULQ+YhWNVtmviYwnFmi3uQFoYX7aDcckG1doKfzzQoCbsWbYE6j3nuoOTt7q6bp39EwSz8+Un8dthGr+qpS88WefcuI9ReV9ZF8kOCzgZPoHbJDlSMiPn1uMImJyGMWtk0SXWud1WDgy7RIpA09Wqo9Shjrw2VZiSolwphGfA91F1Qc6QsLQ8I3ulicBSTZjSrAS4yIunPFkgFWw5fljU7Ijoz2VJc7KxyFhZxBdquXojL7hXpGQDAn3yaC7MjW6temSLioh+3sVkVuPaJNXo9nly5waqzyUtXNiqTc92NmgG8uSnnZeo0DGivGV6xFRDeu5ZUJyvyBuGbuL6CnbBgZINWfymUbvU3+D/wyJGqg03axl1gQxOSM4RrZP8mQr702br3R7VfWMhAvWNmwAeK1xoNfPsyldCx/u2oT1g== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:VI1PR0801MB1854.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(66946007)(44832011)(5660300002)(956004)(86362001)(4326008)(36756003)(8936002)(26005)(83380400001)(66476007)(66556008)(186003)(8676002)(6666004)(30864003)(2616005)(2906002)(38350700002)(38100700002)(6486002)(52116002)(7696005)(1076003)(316002)(508600001)(6916009)(2004002)(473944003); DIR:OUT; SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0801MB1870 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: VE1EUR03FT054.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 1a68678e-59fb-44e3-95c8-08d984ef96be X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: k7zr+CsSjIPPko/WjGhG9Bo/s4hwdXtssbK342iPIkDqyaC044JLu4y06O0lWVorU7U7trrJLvt5UiTO+XlKF0OcpYjDl+kIXFgXEhYUXQRbKshV3F/I4E1LcbJeGiFoGBfLSQhpntSXzPx1YjfSTCXyuVsqLSWC8VtxZoNCtQgIOt+RlfDBTtVcRr4mJIwkHU3Kr6c4ylsWlME70JRSWAjZy08HPGkQ5cSUOX/4zkbWcEFxukvPvXQMucWQTDkHH8krPUcLov1jwkWbidcUkGqHT8vj9/skfJWvl8fYEs4tSONq5CR2uly9n9mZfNBo+Eyu6tL8ZrG0tUNJgZazwADfA+3WNiCDx+QpTI/pWVI20ugEEm0loRAPdtd/7ffowUNoS8gL2MW4eM8j+EspEbtJUXwdlrf80aM13Zb5DLTanEWpdtjy8pJc/RPgsng52NicJE6dao3DQgfpIo8ZxnhkjVgsfACWs443ZKgEcor5Kz/MfzyreGP17JxYi8hMNZhvswXRobLNqir1o2EjDcjP3I11rvH1I32JOEFfzkb48f+xXq2ZluY3pIZoaGNQWU3iRZgjtJXBZUvQnDC9Y0CIyOA8fAZ++7iaWPAA7YMZrzGN6To7v42+q3SG5TXQUIZiikaf8B85liC66IefHqhwv+x9FJ5+EIwrT7inssbRf6nRN2eB1yXpJ0KGuA5smgNvZEezRlB9lsY5zr6B+1l58uNiBe2OMpDOCEP/uCN+yi6LNjSCcEW2rRbhvKEKC9DxzSdsxxUNOmMqR+E+mtkV1UXbds/iUnQqzXKL79253+zG1CmPk/NEpsBTbUteAwwlZ2fkaUyUw8gAg8iSmQ== 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)(86362001)(8936002)(36860700001)(508600001)(7696005)(8676002)(4326008)(36756003)(107886003)(1076003)(70206006)(316002)(26005)(70586007)(956004)(2906002)(82310400003)(356005)(6486002)(336012)(30864003)(186003)(2616005)(47076005)(6916009)(44832011)(83380400001)(5660300002)(81166007)(6666004)(2004002)(473944003); DIR:OUT; SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 01 Oct 2021 15:24:58.6609 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 03e1fbaa-8ab9-48bd-1442-08d984efa14a 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: VE1EUR03FT054.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB3583 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, 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 --- i will commit this next week if there are no further comments. v2: - include test code. - fix whitespace/style issues. --- 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 ++- sysdeps/pthread/Makefile | 10 ++- sysdeps/pthread/tst-create1.c | 120 +++++++++++++++++++++++++++++++ sysdeps/pthread/tst-create1mod.c | 41 +++++++++++ 10 files changed, 236 insertions(+), 12 deletions(-) create mode 100644 sysdeps/pthread/tst-create1.c create mode 100644 sysdeps/pthread/tst-create1mod.c 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 5295e931b0..6ea5dd2457 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 8d2bba3d43..9642eb9c92 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 d49529da0d..225c8bb309 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -369,6 +369,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; @@ -1268,7 +1275,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. */ diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index d4bd2d4e3e..0e137a873f 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -151,15 +151,17 @@ tests += tst-cancelx2 tst-cancelx3 tst-cancelx6 tst-cancelx8 tst-cancelx9 \ tst-cleanupx0 tst-cleanupx1 tst-cleanupx2 tst-cleanupx3 ifeq ($(build-shared),yes) -tests += tst-atfork2 tst-pt-tls4 tst-_res1 tst-fini1 +tests += tst-atfork2 tst-pt-tls4 tst-_res1 tst-fini1 tst-create1 tests-nolibpthread += tst-fini1 endif modules-names += tst-atfork2mod tst-tls4moda tst-tls4modb \ - tst-_res1mod1 tst-_res1mod2 tst-fini1mod + tst-_res1mod1 tst-_res1mod2 tst-fini1mod \ + tst-create1mod test-modules = $(addprefix $(objpfx),$(addsuffix .so,$(modules-names))) tst-atfork2mod.so-no-z-defs = yes +tst-create1mod.so-no-z-defs = yes ifeq ($(build-shared),yes) # Build all the modules even when not actually running test programs. @@ -278,4 +280,8 @@ LDFLAGS-tst-join7mod.so = -Wl,-soname,tst-join7mod.so CFLAGS-tst-unwind-thread.c += -funwind-tables +LDFLAGS-tst-create1 = -Wl,-export-dynamic +$(objpfx)tst-create1: $(shared-thread-library) +$(objpfx)tst-create1.out: $(objpfx)tst-create1mod.so + endif diff --git a/sysdeps/pthread/tst-create1.c b/sysdeps/pthread/tst-create1.c new file mode 100644 index 0000000000..44d1c0fc34 --- /dev/null +++ b/sysdeps/pthread/tst-create1.c @@ -0,0 +1,120 @@ +/* Verify that pthread_create does not deadlock when ctors take locks. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +/* +Check if ctor and pthread_create deadlocks in + +thread 1: dlopen -> ctor -> lock(user_lock) +thread 2: lock(user_lock) -> pthread_create + +or in + +thread 1: dlclose -> dtor -> lock(user_lock) +thread 2: lock(user_lock) -> pthread_create +*/ + +static pthread_barrier_t bar_ctor; +static pthread_barrier_t bar_dtor; +static pthread_mutex_t user_lock = PTHREAD_MUTEX_INITIALIZER; + +void +ctor (void) +{ + xpthread_barrier_wait (&bar_ctor); + dprintf (1, "thread 1: in ctor: started.\n"); + xpthread_mutex_lock (&user_lock); + dprintf (1, "thread 1: in ctor: locked user_lock.\n"); + xpthread_mutex_unlock (&user_lock); + dprintf (1, "thread 1: in ctor: unlocked user_lock.\n"); + dprintf (1, "thread 1: in ctor: done.\n"); +} + +void +dtor (void) +{ + xpthread_barrier_wait (&bar_dtor); + dprintf (1, "thread 1: in dtor: started.\n"); + xpthread_mutex_lock (&user_lock); + dprintf (1, "thread 1: in dtor: locked user_lock.\n"); + xpthread_mutex_unlock (&user_lock); + dprintf (1, "thread 1: in dtor: unlocked user_lock.\n"); + dprintf (1, "thread 1: in dtor: done.\n"); +} + +static void * +thread3 (void *a) +{ + dprintf (1, "thread 3: started.\n"); + dprintf (1, "thread 3: done.\n"); + return 0; +} + +static void * +thread2 (void *a) +{ + pthread_t t3; + dprintf (1, "thread 2: started.\n"); + + xpthread_mutex_lock (&user_lock); + dprintf (1, "thread 2: locked user_lock.\n"); + xpthread_barrier_wait (&bar_ctor); + t3 = xpthread_create (0, thread3, 0); + xpthread_mutex_unlock (&user_lock); + dprintf (1, "thread 2: unlocked user_lock.\n"); + xpthread_join (t3); + + xpthread_mutex_lock (&user_lock); + dprintf (1, "thread 2: locked user_lock.\n"); + xpthread_barrier_wait (&bar_dtor); + t3 = xpthread_create (0, thread3, 0); + xpthread_mutex_unlock (&user_lock); + dprintf (1, "thread 2: unlocked user_lock.\n"); + xpthread_join (t3); + + dprintf (1, "thread 2: done.\n"); + return 0; +} + +static void +thread1 (void) +{ + dprintf (1, "thread 1: started.\n"); + xpthread_barrier_init (&bar_ctor, NULL, 2); + xpthread_barrier_init (&bar_dtor, NULL, 2); + pthread_t t2 = xpthread_create (0, thread2, 0); + void *p = xdlopen ("tst-create1mod.so", RTLD_NOW | RTLD_GLOBAL); + dprintf (1, "thread 1: dlopen done.\n"); + xdlclose (p); + dprintf (1, "thread 1: dlclose done.\n"); + xpthread_join (t2); + dprintf (1, "thread 1: done.\n"); +} + +int +do_test (void) +{ + thread1 (); + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-create1mod.c b/sysdeps/pthread/tst-create1mod.c new file mode 100644 index 0000000000..62c9006961 --- /dev/null +++ b/sysdeps/pthread/tst-create1mod.c @@ -0,0 +1,41 @@ +/* Verify that pthread_create does not deadlock when ctors take locks. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +/* Require TLS setup for the module. */ +__thread int tlsvar; + +void ctor (void); +void dtor (void); + +static void __attribute__ ((constructor)) +do_init (void) +{ + dprintf (1, "constructor started: %d.\n", tlsvar++); + ctor (); + dprintf (1, "constructor done: %d.\n", tlsvar++); +} + +static void __attribute__ ((destructor)) +do_end (void) +{ + dprintf (1, "destructor started: %d.\n", tlsvar++); + dtor (); + dprintf (1, "destructor done: %d.\n", tlsvar++); +}