From patchwork Fri Jan 10 21:23:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 104544 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 819E6385828E for ; Fri, 10 Jan 2025 21:44:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 819E6385828E 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=YoVKWdb/ X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.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 DC01A3858427 for ; Fri, 10 Jan 2025 21:28:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DC01A3858427 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 DC01A3858427 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=1736544508; cv=none; b=Z8yVUAMzrmYuHRep0M9Pox2xwY47Vcu2JV3tZECfp4teHUQpvYqN33a//DSCUvl1c22OAFTo2jnotUAU7RxtoESFJj4EenpO6Px0+TxyILe/TKikmgQIdWYHTZm1uJSuapKv+RjUHiD8P2T9SdspWSLrJPl0OtTZVACH2mdSeHg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736544508; c=relaxed/simple; bh=/zSTl9J+VkQiLx/nJniKDmx33mK/zT3rtJi0IUgA3LI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=O6NVIpwbtclfvoU0VbFyG3n9V8LV0xD9/8ySt0WrKKvMOQzN01l1w6IV72UkePFyU6tMlscbjzxKiura0REHXC4wmkxnl7hZeGAokEA/83TtsO8B3g7UAZfYQ+sJ2KtViHefKjkeKAQpIYDfo7wFs5KbnfOttroEKhSOm0drRFE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DC01A3858427 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736544507; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SL6TjAxboyXln06LecGsXNYSMzDpPsgTIEIUtFy6bUA=; b=YoVKWdb/+rk6pLs+tOBZZAxyFIEZSARwT+OSFTGYgKaYGMN6S48L2XeVKbYGE/FBfLZaaV jyDFSxxnWcYWyYizfPv6Rod5Z7v0eKf6K3+V6b3CrUx7WWxBCic7d0koQ3UDUwxDISe0xT LN+OKeTKmuXzZmF+gXRQIXoxNnIPEQA= Received: from mx-prod-mc-01.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-225-g8RoXtAuOqe7M2bePM4mpw-1; Fri, 10 Jan 2025 16:28:26 -0500 X-MC-Unique: g8RoXtAuOqe7M2bePM4mpw-1 X-Mimecast-MFC-AGG-ID: g8RoXtAuOqe7M2bePM4mpw Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1ED911956070; Fri, 10 Jan 2025 21:28:25 +0000 (UTC) Received: from localhost (unknown [10.42.28.9]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7FF8A1944D10; Fri, 10 Jan 2025 21:28:24 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 05/10] libstdc++: Fix race condition in new atomic notify code Date: Fri, 10 Jan 2025 21:23:08 +0000 Message-ID: <20250110212810.832494-6-jwakely@redhat.com> In-Reply-To: <20250110212810.832494-1-jwakely@redhat.com> References: <20250110212810.832494-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: hvR5yaov8cvPDWvOhzVXyJt94ROQry4V0HaPp1e4Cs0_1736544505 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org When using a proxy object for atomic waiting and notifying operations, we need to ensure that the _M_ver value is always incremented by a notifying operation, even if we return early without doing the futex wake syscall. Otherwise we get missed wake-ups because the notifying thread doesn't modify the value that other threads are doing a futex wait on. libstdc++-v3/ChangeLog: * include/bits/atomic_wait.h (__notify_impl): Increment the proxy value before returning early for the uncontended case. --- libstdc++-v3/include/bits/atomic_wait.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/include/bits/atomic_wait.h b/libstdc++-v3/include/bits/atomic_wait.h index 29b83cad6e6c..4a9652ed8f1d 100644 --- a/libstdc++-v3/include/bits/atomic_wait.h +++ b/libstdc++-v3/include/bits/atomic_wait.h @@ -392,13 +392,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __wait_args __args{ *__a }; __waiter_pool_impl* __pool = nullptr; - if (__args & __wait_flags::__track_contention) - { - __pool = &__waiter_pool_impl::_S_impl_for(__addr); - if (!__pool->_M_waiting()) - return; - } - const __platform_wait_t* __wait_addr; if (__args & __wait_flags::__proxy_wait) { @@ -416,6 +409,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else // Use the atomic variable's own address. __wait_addr = __addr; + if (__args & __wait_flags::__track_contention) + { + __pool = &__waiter_pool_impl::_S_impl_for(__addr); + if (!__pool->_M_waiting()) + return; + } + #ifdef _GLIBCXX_HAVE_PLATFORM_WAIT __platform_notify(__wait_addr, __all); #else