From patchwork Mon Jan 10 10:10:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 49794 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 C86DE389245B for ; Mon, 10 Jan 2022 10:12:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C86DE389245B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1641809538; bh=ZMnhPpU9bgY/GjgqhohjqV0opts7owOwN4yMZ8LKvAk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=f5f8dJ/hhawpeFfv26zAPDZDe0oiCZ1G+nXjv5VgLezzYP5BPatnF201sNF0zBBeP vbjEE0INGKkEz2ECQEdM1cnipL4Sj1TiwnpTGRtyiFd3FILZgD7U3TbIZ/07T32F/F a90lu88M/syaOVUHDyNcrm1mUq06XtUvtXRO/Z5k= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4EC833892445 for ; Mon, 10 Jan 2022 10:10:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4EC833892445 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-436-A2Y_pS60NPOijtSHJaiwQg-1; Mon, 10 Jan 2022 05:10:24 -0500 X-MC-Unique: A2Y_pS60NPOijtSHJaiwQg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3935D83DD22; Mon, 10 Jan 2022 10:10:23 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.102]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3B4C06F95C; Mon, 10 Jan 2022 10:10:21 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH] elf: Simplify software TM implementation in _dl_find_object Date: Mon, 10 Jan 2022 11:10:20 +0100 Message-ID: <8735lv296b.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Cc: Szabolcs Nagy Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" With the current set of fences, the version update at the start of the TM write operation is redundant, and the version update at the end does not need to use an atomic read-modify-write operation. Also use relaxed MO stores during the dlclose update, and skip any version changes there. Suggested-by: Szabolcs Nagy Reviewed-by: Szabolcs Nagy --- v2: Use __atomic_wide_counter_add_relaxed. This has been running over the weekend without any failures. The reasoning for only one version update (it becomes visible before subsequent updates to the data that has been read) makes sense to me. elf/dl-find_object.c | 56 +++++++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/elf/dl-find_object.c b/elf/dl-find_object.c index 0eb8607edd..2b8df2fd67 100644 --- a/elf/dl-find_object.c +++ b/elf/dl-find_object.c @@ -123,9 +123,9 @@ struct dlfo_mappings_segment /* To achieve async-signal-safety, two copies of the data structure are used, so that a signal handler can still use this data even if - dlopen or dlclose modify the other copy. The the MSB in - _dlfo_loaded_mappings_version determines which array element is the - currently active region. */ + dlopen or dlclose modify the other copy. The the least significant + bit in _dlfo_loaded_mappings_version determines which array element + is the currently active region. */ static struct dlfo_mappings_segment *_dlfo_loaded_mappings[2]; /* Returns the number of actually used elements in all segments @@ -248,7 +248,7 @@ _dlfo_read_start_version (void) { /* Acquire MO load synchronizes with the fences at the beginning and end of the TM update region in _dlfo_mappings_begin_update, - _dlfo_mappings_end_update, _dlfo_mappings_end_update_no_switch. */ + _dlfo_mappings_end_update. */ return __atomic_wide_counter_load_acquire (&_dlfo_loaded_mappings_version); } @@ -261,35 +261,25 @@ _dlfo_read_version_locked (void) } /* Update the version to reflect that an update is happening. This - does not change the bit that controls the active segment chain. - Returns the index of the currently active segment chain. */ -static inline unsigned int + does not change the bit that controls the active segment chain. */ +static inline void _dlfo_mappings_begin_update (void) { - /* The store synchronizes with loads in _dlfo_read_start_version + /* The fence synchronizes with loads in _dlfo_read_start_version (also called from _dlfo_read_success). */ atomic_thread_fence_release (); - return __atomic_wide_counter_fetch_add_relaxed - (&_dlfo_loaded_mappings_version, 2); } /* Installs the just-updated version as the active version. */ static inline void _dlfo_mappings_end_update (void) { - /* The store synchronizes with loads in _dlfo_read_start_version + /* The fence synchronizes with loads in _dlfo_read_start_version (also called from _dlfo_read_success). */ atomic_thread_fence_release (); - __atomic_wide_counter_fetch_add_relaxed (&_dlfo_loaded_mappings_version, 1); -} -/* Completes an in-place update without switching versions. */ -static inline void -_dlfo_mappings_end_update_no_switch (void) -{ - /* The store synchronizes with loads in _dlfo_read_start_version - (also called from _dlfo_read_success). */ - atomic_thread_fence_release (); - __atomic_wide_counter_fetch_add_relaxed (&_dlfo_loaded_mappings_version, 2); + /* No atomic read-modify-write update needed because of the loader + lock. */ + __atomic_wide_counter_add_relaxed (&_dlfo_loaded_mappings_version, 1); } /* Return true if the read was successful, given the start @@ -302,10 +292,11 @@ _dlfo_read_success (uint64_t start_version) the TM region are not ordered past the version check below. */ atomic_thread_fence_acquire (); - /* Synchronizes with stores in _dlfo_mappings_begin_update, - _dlfo_mappings_end_update, _dlfo_mappings_end_update_no_switch. - It is important that all stores from the last update have been - visible, and stores from the next updates are not. + /* Synchronizes with the fences in _dlfo_mappings_begin_update, + _dlfo_mappings_end_update. It is important that all stores from + the last update have become visible, and stores from the next + update to this version are not before the version number is + updated. Unlike with seqlocks, there is no check for odd versions here because we have read the unmodified copy (confirmed to be @@ -839,17 +830,10 @@ _dl_find_object_dlclose (struct link_map *map) issues around __libc_freeres. */ return; - /* The update happens in-place, but given that we do not use - atomic accesses on the read side, update the version around - the update to trigger re-validation in concurrent - readers. */ - _dlfo_mappings_begin_update (); - - /* Mark as closed. */ - obj->map_end = obj->map_start; - obj->map = NULL; - - _dlfo_mappings_end_update_no_switch (); + /* Mark as closed. This does not change the overall data + structure, so no TM cycle is needed. */ + atomic_store_relaxed (&obj->map_end, obj->map_start); + atomic_store_relaxed (&obj->map, NULL); return; } }