From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36172C4743D for ; Sat, 5 Jun 2021 03:14:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0D66261414 for ; Sat, 5 Jun 2021 03:14:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231282AbhFEDPw (ORCPT ); Fri, 4 Jun 2021 23:15:52 -0400 Received: from netrider.rowland.org ([192.131.102.5]:59555 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S229998AbhFEDPv (ORCPT ); Fri, 4 Jun 2021 23:15:51 -0400 Received: (qmail 1701512 invoked by uid 1000); 4 Jun 2021 23:14:03 -0400 Date: Fri, 4 Jun 2021 23:14:03 -0400 From: Alan Stern To: Linus Torvalds Cc: Peter Zijlstra , Will Deacon , "Paul E. McKenney" , Andrea Parri , Boqun Feng , Nick Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Linux Kernel Mailing List , linux-toolchains@vger.kernel.org, linux-arch Subject: Re: [RFC] LKMM: Add volatile_if() Message-ID: <20210605031403.GA1701165@rowland.harvard.edu> References: <20210604134422.GA2793@willie-the-truck> <20210604151356.GC2793@willie-the-truck> <20210604155154.GG1676809@rowland.harvard.edu> <20210604182708.GB1688170@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-arch@vger.kernel.org On Fri, Jun 04, 2021 at 12:09:26PM -0700, Linus Torvalds wrote: > Side note: it is worth noting that my version of "volatile_if()" has > an added little quirk: it _ONLY_ orders the stuff inside the > if-statement. > > I do think it's worth not adding new special cases (especially that > "asm goto" hack that will generate worse code than the compiler could > do), but it means that > > x = READ_ONCE(ptr); > volatile_if (x > 0) > WRITE_ONCE(*z, 42); > > has an ordering, but if you write it as > > x = READ_ONCE(ptr); > volatile_if (x <= 0) > return; > WRITE_ONCE(*z, 42); > > then I could in theory see teh compiler doing that WRITE_ONCE() as > some kind of non-control dependency. This may be a minor point, but can that loophole be closed as follows? define volatile_if(x) \ if ((({ _Bool __x = (x); BUILD_BUG_ON(__builtin_constant_p(__x)); __x; }) && \ ({ barrier(); 1; })) || ({ barrier(); 0; })) (It's now a little later at night than when I usually think about this sort of thing, so my brain isn't firing on all its cylinders. Forgive me if this is a dumb question.) Alan