Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1329082pxj; Fri, 4 Jun 2021 11:27:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJztHjaJf0GRS3e2dfi9M1OmzTD0VQqdUvmiS1FL04ekb11TVQ4FqHl/X9Zr4lSHZ4iCjjMq X-Received: by 2002:a17:907:7201:: with SMTP id dr1mr5554521ejc.19.1622831234838; Fri, 04 Jun 2021 11:27:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622831234; cv=none; d=google.com; s=arc-20160816; b=pV4nXMqMr+Ptd1YR5oFpx2auqz1Fbr1UwXfZ9CSif4kkL6QrWlUm/pAX/ZoaOb9kec XPi8q2gErjQsYD/LeN+JZUWsU/yp6l+zBxjvA8VAuRqsHtcJQWADXVIZcNqPn29B58+G djyuVTL8d5GPHLAs0dVPnqUMtXwZHPvyyHqXpdoTu5VfjXC0G6uEmgdjEzHIQmuVQwEY eVVmMfAIX4tuHiMhnwbKrcX6q4c+uaCcufUKZygjMnHB/0zoO4xoUOLuiNupirjnrgnX lz0+jeTCjIEh5gq21cb8Ay+PP+hzhTeI3mB1+/bJD8irkM9qjI9DK/HBnJawZlUDTqA8 jUJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=iAj6eUybdANJ2/4EsZg6IKf15vG5XfiA+WPoqOOoC5w=; b=Q44JaUMhIajnEhFjcTEMI/lFKB7vYCSoj4ZpdoCp5zKegaR5ANrjcX21vUAazsujLH CDN+9dNEQj2he9q/emEaxJ1G6TQCHOrt+cWpWJ852CgXBxvoJDsvzh2QqhNDrg25XcyQ 94B8gtpQy5b0JZScCIZWEsmjKVIi1/Ak0Mbd3qjcFJ8xUthCukorzMbEJZyVn6btx1as /jBFLKhvXkeBZGmSs6NUJgcpZyhgE6tv+bp1t6AJjx6PRoyjVG6X15SAuMVOHkOCg0wd O0QYW1fzheVm3s2e+ooaaV2Yuc7i/QM1X427iNF3iGIwVXHRWhnCIdTQeaeOeKveZGht BhUg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d26si6072735edr.567.2021.06.04.11.26.51; Fri, 04 Jun 2021 11:27:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229726AbhFDSZo (ORCPT + 99 others); Fri, 4 Jun 2021 14:25:44 -0400 Received: from netrider.rowland.org ([192.131.102.5]:45547 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S229995AbhFDSZo (ORCPT ); Fri, 4 Jun 2021 14:25:44 -0400 Received: (qmail 1689062 invoked by uid 1000); 4 Jun 2021 14:23:57 -0400 Date: Fri, 4 Jun 2021 14:23:57 -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: <20210604182357.GA1688170@rowland.harvard.edu> References: 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-kernel@vger.kernel.org On Fri, Jun 04, 2021 at 10:10:29AM -0700, Linus Torvalds wrote: > On Fri, Jun 4, 2021 at 9:37 AM Peter Zijlstra wrote: > > > > > > > > Why is "volatile_if()" not just > > > > > > #define barier_true() ({ barrier(); 1; }) > > > > > > #define volatile_if(x) if ((x) && barrier_true()) > > > > Because we weren't sure compilers weren't still allowed to optimize the > > branch away. > > This isn't about some "compiler folks think". > > The above CANNOT be compiled any other way than with a branch. > > A compiler that optimizes a branch away is simply broken. > > Of course, the actual condition (ie "x" above) has to be something > that the compiler cannot statically determine is a constant, but since > the whole - and only - point is that there will be a READ_ONCE() or > similar there, that's not an issue. In fact there is one weird case where it is an issue (mentioned in memory-barriers.txt): If some obscure arch-specific header file does: #define FOO 1 and an unwitting programmer writes: volatile_if (READ_ONCE(*y) % FOO == 0) WRITE_ONCE(*z, 5); then the compiler _can_ statically determine that the condition is a constant, in spite of the READ_ONCE, but this fact isn't apparent to the programmer. The generated object code will include both the read and the write, but there won't necessarily be any ordering between them. I don't know if cases like this exist in the kernel. It wouldn't be surprising if they did though, particularly in situations where a feature (like multi-level page tables) may be compiled away. Alan