From patchwork Mon Jan 20 03:20:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Merey X-Patchwork-Id: 105073 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 3A9453858408 for ; Mon, 20 Jan 2025 03:22:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3A9453858408 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=OTbFSPk/ X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 8B9F53858D21 for ; Mon, 20 Jan 2025 03:20:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8B9F53858D21 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8B9F53858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1737343248; cv=none; b=Pfk50CaBWM5xvUcrPib1hFcO2AiUrYS34Wbca06qEVnc93jD9wlUUuVLcqARIpsoIBwOPeD+7L2Mm8HADqSmZLUoLOPkb4wMb7JHalOLT/sOZgo19HrHOQlSJx5LbVI4gIkILHpb7Maa2lquBuawzAfSHIZqSOcMK9sioC/asxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1737343248; c=relaxed/simple; bh=bkqnpc4Sh8bXfgXL9XKfsmhEpw/h4TkrfeKeYE63FbU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EcR5YoKMXMXcPExwZV0RqURnQJxjSfYE+sGRGFt3415TLzznIjnRunSp7SAa1WOIOhJsdy3dkPWFnbXZAB2ugHqS+hMmT8UgPVUSKs+cm1Psd+gw7UBofrH3GXmBAmP+93ykf0cUbKaCSLTSvFix05JWCn6TyQa4u0SpDwTrhAc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B9F53858D21 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1737343248; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SWpVNdlNK7ZLOTtNoJe4pX/2Ev+TNWZa5j+Sq1u0S8c=; b=OTbFSPk/tnY3BjD+WQDTMgH22AFPIVw/XVeVLL/X1JWrSTMPB9TJI8D+UxnZYqfI6oERMt 21LhYFmIUh6Hp7HGVoqfAzUJjjsDG5r9tJx3Xpo2noCRKGubKWtmVMVtZKIvxHHaNWYZ3/ WBzP0mpKqyCWhtGcznPi4DLIZrGrxh4= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-133-42mdLT_lN5mT2n4v2lB2xg-1; Sun, 19 Jan 2025 22:20:46 -0500 X-MC-Unique: 42mdLT_lN5mT2n4v2lB2xg-1 X-Mimecast-MFC-AGG-ID: 42mdLT_lN5mT2n4v2lB2xg Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 011D81955DCC for ; Mon, 20 Jan 2025 03:20:46 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.22.80.25]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 54986195608A; Mon, 20 Jan 2025 03:20:45 +0000 (UTC) From: Aaron Merey To: elfutils-devel@sourceware.org Cc: Aaron Merey Subject: [PATCH 02/15] Change type of dwarf_lock from rwlock to mutex Date: Sun, 19 Jan 2025 22:20:28 -0500 Message-ID: <20250120032041.280173-2-amerey@redhat.com> In-Reply-To: <20250120032041.280173-1-amerey@redhat.com> References: <20250120032041.280173-1-amerey@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Nlqn69ssy5gQjj7y0hG_Y7kMh8ovtU8QP0kNvmxzzM4_1737343246 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: elfutils-devel-bounces~patchwork=sourceware.org@sourceware.org Change type of dwarf_lock to mutex in order to take advantage of built-in support for recursive locking. * lib/locks.h: Add macros for locking, unlocking, initializing and destroying mutexes. * libdw/dwarf_begin_elf.c (dwarf_end): Replace rwlock macro with mutex macro. * libdw/dwarf_formref_die.c (dwarf_formref_die): Ditto. * libdw/dwarf_getalt.c (dwarf_getalt): Ditto. * libdw/dwarf_setalt.c (dwarf_setalt): Ditto. * libdw/libdwP.h (struct Dwarf): Ditto. * libdw/libdw_findcu.c (__libdw_findcu): Ditto. Signed-off-by: Aaron Merey --- If performance suffers too much from serializing all accesses to Dwarf objects, we could instead consider using an rwlock with a custom wrapper that implements recursive locking. lib/locks.h | 16 ++++++++++++++++ libdw/dwarf_begin_elf.c | 2 +- libdw/dwarf_end.c | 2 +- libdw/dwarf_formref_die.c | 4 ++-- libdw/dwarf_getalt.c | 10 +++++----- libdw/dwarf_setalt.c | 4 ++-- libdw/libdwP.h | 2 +- libdw/libdw_findcu.c | 4 ++-- 8 files changed, 30 insertions(+), 14 deletions(-) diff --git a/lib/locks.h b/lib/locks.h index 90fe3f1b..9c17eca7 100644 --- a/lib/locks.h +++ b/lib/locks.h @@ -43,6 +43,17 @@ # define rwlock_rdlock(lock) RWLOCK_CALL (rdlock (&lock)) # define rwlock_wrlock(lock) RWLOCK_CALL (wrlock (&lock)) # define rwlock_unlock(lock) RWLOCK_CALL (unlock (&lock)) +# define mutex_define(class,name) class pthread_mutex_t name +# define MUTEX_CALL(call) \ + ({ int _err = pthread_mutex_ ## call; assert_perror (_err); }) +# define mutex_init(lock) \ + ({ pthread_mutexattr_t _attr; \ + pthread_mutexattr_init (&_attr); \ + pthread_mutexattr_settype (&_attr, PTHREAD_MUTEX_RECURSIVE); \ + MUTEX_CALL (init (&lock, &_attr)); }) +# define mutex_lock(_lock) MUTEX_CALL (lock (&_lock)) +# define mutex_unlock(lock) MUTEX_CALL (unlock (&lock)) +# define mutex_fini(lock) MUTEX_CALL (destroy (&lock)) # define once(once_control, init_routine) \ ONCE_CALL (once (&once_control, init_routine)) #else @@ -55,6 +66,11 @@ # define rwlock_rdlock(lock) ((void) (lock)) # define rwlock_wrlock(lock) ((void) (lock)) # define rwlock_unlock(lock) ((void) (lock)) +# define mutex_define(class,name) class int name +# define mutex_init(lock) ((void) (lock)) +# define mutex_lock(lock) ((void) (lock)) +# define mutex_unlock(lock) ((void) (lock)) +# define mutex_fini(lock) ((void) (lock)) # define once_define(class,name) # define once(once_control, init_routine) init_routine() #endif /* USE_LOCKS */ diff --git a/libdw/dwarf_begin_elf.c b/libdw/dwarf_begin_elf.c index c8292913..8a3a939b 100644 --- a/libdw/dwarf_begin_elf.c +++ b/libdw/dwarf_begin_elf.c @@ -579,7 +579,7 @@ dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp) __libdw_seterrno (DWARF_E_NOMEM); /* no memory. */ return NULL; } - rwlock_init (result->dwarf_lock); + mutex_init (result->dwarf_lock); eu_search_tree_init (&result->cu_tree); eu_search_tree_init (&result->tu_tree); eu_search_tree_init (&result->split_tree); diff --git a/libdw/dwarf_end.c b/libdw/dwarf_end.c index ee3ed74e..92389c07 100644 --- a/libdw/dwarf_end.c +++ b/libdw/dwarf_end.c @@ -129,7 +129,7 @@ dwarf_end (Dwarf *dwarf) if (dwarf->mem_tails != NULL) free (dwarf->mem_tails); pthread_rwlock_destroy (&dwarf->mem_rwl); - rwlock_fini (dwarf->dwarf_lock); + mutex_fini (dwarf->dwarf_lock); /* Free the pubnames helper structure. */ free (dwarf->pubnames_sets); diff --git a/libdw/dwarf_formref_die.c b/libdw/dwarf_formref_die.c index 69d1bf1c..85c484f6 100644 --- a/libdw/dwarf_formref_die.c +++ b/libdw/dwarf_formref_die.c @@ -92,9 +92,9 @@ dwarf_formref_die (Dwarf_Attribute *attr, Dwarf_Die *result) bool scan_debug_types = false; do { - rwlock_wrlock(attr->cu->dbg->dwarf_lock); + mutex_lock (attr->cu->dbg->dwarf_lock); cu = __libdw_intern_next_unit (attr->cu->dbg, scan_debug_types); - rwlock_unlock(attr->cu->dbg->dwarf_lock); + mutex_unlock (attr->cu->dbg->dwarf_lock); if (cu == NULL) { diff --git a/libdw/dwarf_getalt.c b/libdw/dwarf_getalt.c index 26433b81..f4b8ad02 100644 --- a/libdw/dwarf_getalt.c +++ b/libdw/dwarf_getalt.c @@ -170,12 +170,12 @@ dwarf_getalt (Dwarf *main) if (main == NULL) return NULL; - rwlock_wrlock(main->dwarf_lock); + mutex_lock (main->dwarf_lock); /* Only try once. */ if (main->alt_dwarf == (void *) -1) { - rwlock_unlock (main->dwarf_lock); + mutex_unlock (main->dwarf_lock); return NULL; } @@ -185,7 +185,7 @@ dwarf_getalt (Dwarf *main) if (main->alt_dwarf != NULL) { result = main->alt_dwarf; - rwlock_unlock (main->dwarf_lock); + mutex_unlock (main->dwarf_lock); return result; } @@ -195,12 +195,12 @@ dwarf_getalt (Dwarf *main) if (main->alt_dwarf == NULL) { main->alt_dwarf = (void *) -1; - rwlock_unlock (main->dwarf_lock); + mutex_unlock (main->dwarf_lock); return NULL; } result = main->alt_dwarf; - rwlock_unlock (main->dwarf_lock); + mutex_unlock (main->dwarf_lock); return result; } diff --git a/libdw/dwarf_setalt.c b/libdw/dwarf_setalt.c index f98a457c..d8790fd3 100644 --- a/libdw/dwarf_setalt.c +++ b/libdw/dwarf_setalt.c @@ -35,7 +35,7 @@ void dwarf_setalt (Dwarf *main, Dwarf *alt) { - rwlock_wrlock (main->dwarf_lock); + mutex_lock (main->dwarf_lock); if (main->alt_fd != -1) { @@ -45,6 +45,6 @@ dwarf_setalt (Dwarf *main, Dwarf *alt) } main->alt_dwarf = alt; - rwlock_unlock (main->dwarf_lock); + mutex_unlock (main->dwarf_lock); } INTDEF (dwarf_setalt) diff --git a/libdw/libdwP.h b/libdw/libdwP.h index d6bab606..db5abeec 100644 --- a/libdw/libdwP.h +++ b/libdw/libdwP.h @@ -266,7 +266,7 @@ struct Dwarf /* The dwarf_lock is a read-write lock designed to ensure thread-safe access and modification of an entire Dwarf object. */ - rwlock_define(, dwarf_lock); + mutex_define(, dwarf_lock); /* Internal memory handling. This is basically a simplified thread-local reimplementation of obstacks. Unfortunately the standard obstack diff --git a/libdw/libdw_findcu.c b/libdw/libdw_findcu.c index bbbbee5d..613f61c8 100644 --- a/libdw/libdw_findcu.c +++ b/libdw/libdw_findcu.c @@ -249,7 +249,7 @@ __libdw_findcu (Dwarf *dbg, Dwarf_Off start, bool v4_debug_types) if (found != NULL) return *found; - rwlock_wrlock (dbg->dwarf_lock); + mutex_lock (dbg->dwarf_lock); if (start < *next_offset) __libdw_seterrno (DWARF_E_INVALID_DWARF); @@ -276,7 +276,7 @@ __libdw_findcu (Dwarf *dbg, Dwarf_Off start, bool v4_debug_types) } } - rwlock_unlock (dbg->dwarf_lock); + mutex_unlock (dbg->dwarf_lock); return result; }