From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id A5B5C3858288 for ; Tue, 10 Oct 2023 13:43:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A5B5C3858288 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from r6.localdomain (82-217-174-174.cable.dynamic.v4.ziggo.nl [82.217.174.174]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 238963022F1C; Tue, 10 Oct 2023 15:43:35 +0200 (CEST) Received: by r6.localdomain (Postfix, from userid 1000) id 370B53403F7; Tue, 10 Oct 2023 15:43:34 +0200 (CEST) From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: hsm2@rice.edu, Mark Wielaard Subject: [PATCH 12/16] libdw: Make libdw_find_split_unit thread-safe Date: Tue, 10 Oct 2023 15:42:56 +0200 Message-ID: <20231010134300.53830-12-mark@klomp.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231010134300.53830-1-mark@klomp.org> References: <301fac87e83ebbbd677750579ae9a3429b461bdf.camel@klomp.org> <20231010134300.53830-1-mark@klomp.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3033.6 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,RCVD_IN_BARRACUDACENTRAL,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Heather McIntyre * (try_split_file): Use eu_tsearch. Add lock for cu->split. Signed-off-by: Heather S. McIntyre Signed-off-by: Mark Wielaard --- libdw/libdw_find_split_unit.c | 43 +++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/libdw/libdw_find_split_unit.c b/libdw/libdw_find_split_unit.c index 533f8072..a288e9bd 100644 --- a/libdw/libdw_find_split_unit.c +++ b/libdw/libdw_find_split_unit.c @@ -34,13 +34,19 @@ #include "libelfP.h" #include -#include +#include #include #include #include #include #include +/* __libdw_link_skel_split() modifies "skel->split = split" + and "split->split = skel". + "cu->split" is read at multiple locations and conditionally updated. + Mutual exclusion is enforced to prevent a race. */ +rwlock_define(static, cu_split_lock); + static void try_split_file (Dwarf_CU *cu, const char *dwo_path) { @@ -57,7 +63,7 @@ try_split_file (Dwarf_CU *cu, const char *dwo_path) if (split->unit_type == DW_UT_split_compile && cu->unit_id8 == split->unit_id8) { - if (tsearch (split->dbg, &cu->dbg->split_tree, + if (eu_tsearch (split->dbg, &cu->dbg->split_tree, __libdw_finddbg_cb) == NULL) { /* Something went wrong. Don't link. */ @@ -65,9 +71,13 @@ try_split_file (Dwarf_CU *cu, const char *dwo_path) break; } + rwlock_wrlock(cu_split_lock); + /* Link skeleton and split compile units. */ __libdw_link_skel_split (cu, split); + rwlock_unlock(cu_split_lock); + /* We have everything we need from this ELF file. And we are going to close the fd to not run out of file descriptors. */ @@ -75,8 +85,13 @@ try_split_file (Dwarf_CU *cu, const char *dwo_path) break; } } + + rwlock_rdlock(cu_split_lock); + if (cu->split == (Dwarf_CU *) -1) dwarf_end (split_dwarf); + + rwlock_unlock(cu_split_lock); } /* Always close, because we don't want to run out of file descriptors. See also the elf_fcntl ELF_C_FDDONE call @@ -89,9 +104,13 @@ Dwarf_CU * internal_function __libdw_find_split_unit (Dwarf_CU *cu) { + rwlock_rdlock(cu_split_lock); + Dwarf_CU *cu_split_local = cu->split; + rwlock_unlock(cu_split_lock); + /* Only try once. */ - if (cu->split != (Dwarf_CU *) -1) - return cu->split; + if (cu_split_local != (Dwarf_CU *) -1) + return cu_split_local; /* We need a skeleton unit with a comp_dir and [GNU_]dwo_name attributes. The split unit will be the first in the dwo file and should have the @@ -116,7 +135,11 @@ __libdw_find_split_unit (Dwarf_CU *cu) free (dwo_path); } - if (cu->split == (Dwarf_CU *) -1) + rwlock_rdlock(cu_split_lock); + cu_split_local = cu->split; + rwlock_unlock(cu_split_lock); + + if (cu_split_local == (Dwarf_CU *) -1) { /* Try compdir plus dwo_name. */ Dwarf_Attribute compdir; @@ -138,9 +161,15 @@ __libdw_find_split_unit (Dwarf_CU *cu) } } + rwlock_wrlock(cu_split_lock); + /* If we found nothing, make sure we don't try again. */ if (cu->split == (Dwarf_CU *) -1) - cu->split = NULL; + { + cu->split = NULL; + cu_split_local = cu->split; + } - return cu->split; + rwlock_unlock(cu_split_lock); + return cu_split_local; } -- 2.41.0