From patchwork Wed Feb 9 00:55:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Rodgers X-Patchwork-Id: 50934 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 155603858019 for ; Wed, 9 Feb 2022 00:56:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 155603858019 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1644368186; bh=pW1T0BDCjN7vaQYkbofaUGoufuvygev0BvS5ixv6uCk=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=s0OdKgwoSJCA/AuExz/hF3MBZpRuIgGEOBT3dv9l2mz7Fu1PhVXLWXYx0ivlYcK6T rcO5iSvy29Bl6DamhEb370ODsl3CyOiYB8mIDnj50dVDp4Q7bmjTi1zSBh47DyiOZ2 elKlUMMw1JZ/kTYutZTSZUrUap9D+KjZo4FLmWEU= 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 ESMTPS id 5C81F3858D1E for ; Wed, 9 Feb 2022 00:55:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5C81F3858D1E Received: from mail-oo1-f72.google.com (mail-oo1-f72.google.com [209.85.161.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-59-uG1SyGy7NhGflrsiyY4ubQ-1; Tue, 08 Feb 2022 19:55:54 -0500 X-MC-Unique: uG1SyGy7NhGflrsiyY4ubQ-1 Received: by mail-oo1-f72.google.com with SMTP id f8-20020a4aa688000000b002ede70d37dbso354209oom.16 for ; Tue, 08 Feb 2022 16:55:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=KX1pcZwI0vugTr53kycyylyGywm+XECSAsNntKl0uWk=; b=JjGTzla+nR3mLjVYgdAF9w8OZUBkLNmo7yp7mfK7XvmPFqCRF3YFQN58BBSvaZHx+G LRy0kvp7qwUfvqU0xKLOoBYoM7QC999g3+zbhi42tOxPfkTFUV7IwvlTjrQz7IxlHO4r LFIorpbM9gkSz0WNxWMvvYlHe6MTx/3PxVkXguv44V6YebtEE+GjUxBngIze1/dJhom6 pJ0UWDeRs+bffeOBylKiugJaXCvHP1wZgBXLoh3QFh2ZqJUGnVyBWmCCnrUmWZoGF80X Ex13xRuIzM/mgVV4kqctVCK2o6A3gpSMnjq4v+c7cYEtnAJKsr3G0++uACPJFKBtDy9F LhFA== X-Gm-Message-State: AOAM530ja5JzLOYKSMAD70NMaedQRNp8EP/uYH4Kbhs5Odv6DNrd0m+F qKARJoi4cDSdEqj4D0GWDTMtE4TMLC+BIj7H66NGcJsx4TTttS+8X4pmhY+1XFV+KoKUg0VqCqG nRGkluT2d9VZNjbdwmDz6iQNlIapkvCVKGg== X-Received: by 2002:a05:6870:118f:: with SMTP id 15mr4192oau.105.1644368153601; Tue, 08 Feb 2022 16:55:53 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3XnKUmt+AXQvna9mvzp2SSbomCYxtqTXXZI4SRtnH5WEbyroqtPsVm2//JtxzZu3kA+ReI/9RH57toh8ZTfw= X-Received: by 2002:a05:6870:118f:: with SMTP id 15mr4186oau.105.1644368153333; Tue, 08 Feb 2022 16:55:53 -0800 (PST) MIME-Version: 1.0 Date: Tue, 8 Feb 2022 16:55:42 -0800 Message-ID: Subject: [PATCH] libstdc++: Fix deadlock in atomic wait [PR104442] To: "libstdc++" X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, URI_HEX 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-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Thomas Rodgers via Gcc-patches From: Thomas Rodgers Reply-To: Thomas Rodgers Cc: gcc Patches Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This issue was observed as a deadloack in 29_atomics/atomic/wait_notify/100334.cc on vxworks. When a wait is "laundered" (e.g. type T* does not suffice as a waitable address for the platform's native waiting primitive), the address waited is that of the _M_ver member of __waiter_pool_base, so several threads may wait on the same address for unrelated atomic's. As noted in the PR, the implementation correctly exits the wait for the thread who's data changed, but not for any other threads waiting on the same address. As noted in the PR the __waiter::_M_do_wait_v member was correctly exiting but the other waiters were not reloaded the value of _M_ver before re-entering the wait. Moving the spin call inside the loop accomplishes this, and is consistent with the predicate accepting version of __waiter::_M_do_wait. From ee66736beca3dce4bc09350c5407a2ac7219fbec Mon Sep 17 00:00:00 2001 From: Thomas Rodgers Date: Tue, 8 Feb 2022 16:33:36 -0800 Subject: [PATCH] libstdc++: Fix deadlock in atomic wait [PR104442] This issue was observed as a deadloack in 29_atomics/atomic/wait_notify/100334.cc on vxworks. When a wait is "laundered" (e.g. type T* does not suffice as a waitable address for the platform's native waiting primitive), the address waited is that of the _M_ver member of __waiter_pool_base, so several threads may wait on the same address for unrelated atomic's. As noted in the PR, the implementation correctly exits the wait for the thread who's data changed, but not for any other threads waiting on the same address. As noted in the PR the __waiter::_M_do_wait_v member was correctly exiting but the other waiters were not reloaded the value of _M_ver before re-entering the wait. Moving the spin call inside the loop accomplishes this, and is consistent with the predicate accepting version of __waiter::_M_do_wait. libstdc++-v3/ChangeLog: PR libstdc++/104442 * include/bits/atomic_wait.h (__waiter::_M_do_wait_v): Move spin loop inside do loop so that threads failing the wait, reload _M_ver. --- libstdc++-v3/include/bits/atomic_wait.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/include/bits/atomic_wait.h b/libstdc++-v3/include/bits/atomic_wait.h index d7de0d7eb9e..33ce26ade1b 100644 --- a/libstdc++-v3/include/bits/atomic_wait.h +++ b/libstdc++-v3/include/bits/atomic_wait.h @@ -332,7 +332,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } else { - __atomic_load(__addr, &__val, __ATOMIC_RELAXED); + __atomic_load(__addr, &__val, __ATOMIC_SEQ_CST); } return __atomic_spin(__pred, __spin); } @@ -388,12 +388,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void _M_do_wait_v(_Tp __old, _ValFn __vfn) { - __platform_wait_t __val; - if (__base_type::_M_do_spin_v(__old, __vfn, __val)) - return; - do { + __platform_wait_t __val; + if (__base_type::_M_do_spin_v(__old, __vfn, __val)) + return; __base_type::_M_w._M_do_wait(__base_type::_M_addr, __val); } while (__detail::__atomic_compare(__old, __vfn())); -- 2.34.1