From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 ED42038582A4 for ; Wed, 14 Sep 2022 22:28:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ED42038582A4 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663194526; 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: in-reply-to:in-reply-to:references:references; bh=ltKPh/UvFM3UgE6VzXbveCbNameOH187SBt9+GqNV8I=; b=LNk1szoRC8oln6xLYJb4owVzyv59sLtGMOyQXQ/t4v5NuUhYYIv31rN7AKPkyVt8vBilgZ vmgZwcwU1Oh9KqQjJ8gkod1tLmM3UrLX0Qr+gUYgSoIcDk7/8D5s51VmrRXQWS53byI7iu mfZQYavKzL5Zabh7t6ORb5kp73Dhoss= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-288-QHAYEkcINayFZp-0Y5zZSg-1; Wed, 14 Sep 2022 18:28:45 -0400 X-MC-Unique: QHAYEkcINayFZp-0Y5zZSg-1 Received: by mail-qv1-f71.google.com with SMTP id i10-20020ad45c6a000000b004a25d0fea96so11372919qvh.3 for ; Wed, 14 Sep 2022 15:28:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date; bh=ltKPh/UvFM3UgE6VzXbveCbNameOH187SBt9+GqNV8I=; b=mOm4SoWc5lAlQKVFoxjcwP1dnpFrUSv9sWCj9okHDYTta96qPsUdSOcgvCmnAlMM/3 oMkONpGU3PByXNKzzD013iPoG63rDiylweziV7hgh5Sr8p+6Qu1+khe/BabTK8NtVpP4 a8S7xEEFvCvECbnDJAe/QbiOkLTfAzK483Rbtt4zv38WiR+gZxf9XVvN1quqij+QPoaj EG0dTmfXBI0G9D2Kcln5T4PE/rKIQieb6AewRMOaBpkFl+x/mh7U1advkzsHUR40zsWM 2O1BKdKiKT7kKHYp+gvDwGEBW/3BVTDbQWQkFqt9/BFuRm9O+IYfQFoN1XRRh67ISpv4 8zxg== X-Gm-Message-State: ACgBeo2fqitCC0mnrCRCvwX/0jCQA+qvrZ0VkXDM+AtY5xUcjLjzzfww JJXPiSPkgYVLy9zgyPb7imd0bPK5RUTNQVHsBaBpG3EjG3036iTfjxP3vskigixKTdHWLUpuvNn 2jp8fIZQ6GPd82sr1PIOUZM9/8Puojlk= X-Received: by 2002:a05:622a:180d:b0:35b:d283:7e65 with SMTP id t13-20020a05622a180d00b0035bd2837e65mr9955347qtc.106.1663194525056; Wed, 14 Sep 2022 15:28:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR7gQa1fTnPnyRr/nqjsQitYzQxdSxffbpLUh7dJPMDj8uTA8PUlpNrBdswBx2dXW58XueH1qm9kUio5I2EtLXA= X-Received: by 2002:a05:622a:180d:b0:35b:d283:7e65 with SMTP id t13-20020a05622a180d00b0035bd2837e65mr9955329qtc.106.1663194524787; Wed, 14 Sep 2022 15:28:44 -0700 (PDT) MIME-Version: 1.0 References: <20220914220449.276340-1-jwakely@redhat.com> In-Reply-To: From: Jonathan Wakely Date: Wed, 14 Sep 2022 23:28:34 +0100 Message-ID: Subject: Re: [committed] libstdc++: Add TSan annotations to std::atomic> To: Jonathan Wakely Cc: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" 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,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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: On Wed, 14 Sept 2022 at 23:25, Jonathan Wakely wrote: > > On Wed, 14 Sept 2022 at 23:05, Jonathan Wakely via Libstdc++ > wrote: > > > > Tested powerpc64le-linux, pushed to trunk. > > > > -- >8 -- > > > > This adds annotations to std::atomic> to enable TSan to > > understand the custom locking. Without this, TSan reports data races for > > accesses to the _M_ptr member, even though those are correctly > > synchronized using atomic operations on the tagged pointer. > > > > libstdc++-v3/ChangeLog: > > > > * include/bits/shared_ptr_atomic.h (_GLIBCXX_TSAN_MUTEX_DESTROY) > > (_GLIBCXX_TSAN_MUTEX_PRE_LOCK, _GLIBCXX_TSAN_MUTEX_POST_LOCK) > > (_GLIBCXX_TSAN_MUTEX_PRE_UNLOCK, _GLIBCXX_TSAN_MUTEX_POST_UNLOCK) > > (_GLIBCXX_TSAN_MUTEX_PRE_SIGNAL, _GLIBCXX_TSAN_MUTEX_POST_SIGNAL): > > Define macros for TSan annotation functions. > > (_Sp_atomic::_Atomic_count): Add annotations. > > --- > > libstdc++-v3/include/bits/shared_ptr_atomic.h | 38 +++++++++++++++++++ > > 1 file changed, 38 insertions(+) > > > > diff --git a/libstdc++-v3/include/bits/shared_ptr_atomic.h b/libstdc++-v3/include/bits/shared_ptr_atomic.h > > index d4bd712fc7d..4580807f42c 100644 > > --- a/libstdc++-v3/include/bits/shared_ptr_atomic.h > > +++ b/libstdc++-v3/include/bits/shared_ptr_atomic.h > > @@ -32,6 +32,30 @@ > > > > #include > > > > +#if defined _GLIBCXX_TSAN && __has_include() > > +#include > > +#define _GLIBCXX_TSAN_MUTEX_DESTROY(X) \ > > + __tsan_mutex_destroy(X, __tsan_mutex_not_static) > > +#define _GLIBCXX_TSAN_MUTEX_PRE_LOCK(X) \ > > + __tsan_mutex_pre_lock(X, __tsan_mutex_not_static) > > +#define _GLIBCXX_TSAN_MUTEX_POST_LOCK(X) \ > > + __tsan_mutex_post_lock(X, __tsan_mutex_not_static, 0) > > +#define _GLIBCXX_TSAN_MUTEX_PRE_UNLOCK(X) \ > > + __tsan_mutex_pre_unlock(X, __tsan_mutex_not_static) > > +#define _GLIBCXX_TSAN_MUTEX_POST_UNLOCK(X) \ > > + __tsan_mutex_post_unlock(X, __tsan_mutex_not_static) > > +#define _GLIBCXX_TSAN_MUTEX_PRE_SIGNAL(X) __tsan_mutex_pre_signal(X, 0) > > +#define _GLIBCXX_TSAN_MUTEX_POST_SIGNAL(X) __tsan_mutex_post_signal(X, 0) > > +#else > > +#define _GLIBCXX_TSAN_MUTEX_DESTROY(X) > > +#define _GLIBCXX_TSAN_MUTEX_PRE_LOCK(X) > > +#define _GLIBCXX_TSAN_MUTEX_POST_LOCK(X) > > +#define _GLIBCXX_TSAN_MUTEX_PRE_UNLOCK(X) > > +#define _GLIBCXX_TSAN_MUTEX_POST_UNLOCK(X) > > +#define _GLIBCXX_TSAN_MUTEX_PRE_SIGNAL(X) > > +#define _GLIBCXX_TSAN_MUTEX_POST_SIGNAL(X) > > +#endif > > + > > namespace std _GLIBCXX_VISIBILITY(default) > > { > > _GLIBCXX_BEGIN_NAMESPACE_VERSION > > @@ -377,6 +401,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > > ~_Atomic_count() > > { > > auto __val = _M_val.load(memory_order_relaxed); > > + _GLIBCXX_TSAN_MUTEX_DESTROY(&_M_val); > > After further thought, I'm not sure this is right. This tells tsan > that the "mutex" at &_M_val cannot be locked or unlocked again after > this. But what happens if the address is reused by a different > atomic> which happens to be at the same memory address? > Will tsan think that's an invalid use of the original "mutex" after > its destruction? We can't easily add a call to __tsan_mutex_create, which would begin the lifetime of a new object at that address, because the default constructor is constexpr, and the create function isn't. > > I will investigate. > > We might need to stop using the __tsan_mutex_destroy call, and if so, > we can stop using the __tsan_mutex_not_static flag too. The pre/post > lock/unlock/signal pairs are still valuable without the lifetime > checking.