From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.2 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 71EB11F55B for ; Wed, 27 May 2020 11:55:04 +0000 (UTC) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6DFB6396EC49; Wed, 27 May 2020 11:54:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6DFB6396EC49 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1590580452; bh=8JQMvx2zWac3MOD/saWvywt/Mw8doQrRKm0qoksTuk4=; h=To:Subject:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=LYmjL7Ot3COOKfUDB8NQGAEXFGNQA8W2BiXaBMYB0aLQCjcKA+AyvI6Sv/7r8T2ek HsjXcd/W5ESy2MFwxT4xkG1YHcgQynTie+K4PBsxEiF+lWffIECAoyNo/E5Krkw4QZ i/g2lc4vnegdhA5kUErfFmI/u4z1vXJAQ8hYyS8I= Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by sourceware.org (Postfix) with ESMTP id 9156C3887019 for ; Wed, 27 May 2020 11:54:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9156C3887019 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-513-owaXrCHbPayoWPuVLpMQ7Q-1; Wed, 27 May 2020 07:54:05 -0400 X-MC-Unique: owaXrCHbPayoWPuVLpMQ7Q-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4D5C0461; Wed, 27 May 2020 11:54:04 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-113-106.ams2.redhat.com [10.36.113.106]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 85EA998A3F; Wed, 27 May 2020 11:54:03 +0000 (UTC) To: Rich Felker Subject: Re: [PATCH 2/2] manual: Document __libc_single_threaded References: <20200522161413.GU1079@brightrain.aerifal.cx> <871rnb3nue.fsf@oldenburg2.str.redhat.com> <20200522172826.GW1079@brightrain.aerifal.cx> <87h7w727i4.fsf@oldenburg2.str.redhat.com> <20200522174932.GY1079@brightrain.aerifal.cx> <20200522192249.GC24880@arm.com> <20200522195326.GB1079@brightrain.aerifal.cx> <20200523064941.GD26190@port70.net> <20200523160202.GG1079@brightrain.aerifal.cx> <87o8qczb0n.fsf@oldenburg2.str.redhat.com> <20200525172149.GQ1079@brightrain.aerifal.cx> Date: Wed, 27 May 2020 13:54:01 +0200 In-Reply-To: <20200525172149.GQ1079@brightrain.aerifal.cx> (Rich Felker's message of "Mon, 25 May 2020 13:21:49 -0400") Message-ID: <87mu5tk2zq.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain 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: , From: Florian Weimer via Libc-alpha Reply-To: Florian Weimer Cc: libc-alpha@sourceware.org Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" * Rich Felker: > On Mon, May 25, 2020 at 10:08:37AM +0200, Florian Weimer via Libc-alpha wrote: >> * Rich Felker: >> >> >> this can allow earlier single threaded detection than only >> >> considering pthread_join: e.g. stdio, malloc etc may do a >> >> check and update the global after an acquire barrier, however >> >> the compiler must not cache globals across libc calls for this >> >> to work. >> > >> > It can't cache globals across non-pure functions whose definitions it >> > cant't see (and if it saw the definition it would know the global is >> > modified). >> >> Sorry about that, hit C-c C-c while I thought I was in a terminal. 8-/ >> >> For most standard C functions, it is well-known to which global >> variables (if any) they write. Of course, compilers exploit this fact. >> >> > malloc is something of a special case where clang treats it >> > not as a function but having "pure malloc semantics", but even then I >> > don't think it matters if it caches it; >> >> And of course malloc is the most common example of a standard function >> that has observable side effects beyond those specified in the standard: >> most implementations have a statistics interface. >> >> > at worst you see the old value of __libc_single_threaded (false) >> > rather than the new one (true) and that direction is safe. >> >> It's still a data race. The compiler can easily generate invalid code >> if it incorrectly assumes that __libc_single_threaded remains stable. I >> don't know if Clang will do this. But I think the C library >> implementation should be rather conservative here. > > If this is an issue, and even regardless of whether it is, I think the > type of __libc_single_threaded should be volatile qualified. This > ensures that it cannot be cached in any way that might be invalid. > That's not just a hack; it's the correct way to model that the value > is able to change asynchronously (such as by an operation that the > compiler would otherwise assume can't have side effects). I think it makes more sense not to declare the object as volatile and make sure that only libc functions which imply the required barrier write to __libc_single_threaded. For instance, I expect that this will allow compilers to generate tighter code around multiple (implied) reference count updates. Thanks, Florian