Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1281766pxj; Fri, 4 Jun 2021 10:16:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxH0+9hxW8BV0dc7iHJHm/XDLhEuQ+hz5ahoy7rehWVdyiHgP/izyJh1yrBJrCX7zL8f1i6 X-Received: by 2002:a17:906:f184:: with SMTP id gs4mr5144033ejb.420.1622826978274; Fri, 04 Jun 2021 10:16:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622826978; cv=none; d=google.com; s=arc-20160816; b=wtSqBWNjp23Nf804FqL4artReDYLWboeQiL0SDeEku/ojRriTd42yZ9LdJYTZ8L3Nh T8ktzEXalYXzlIpttiMMALQ3a2z4ceBZQ95DP5YQgOQR2af9mGd3btAzCMfqhjGpCaSs GUlRFWWy8kFA3s9GbIG1KFUUpcUhlc3ILQvzieQCS7AzGE5ZYiNk6PJWx14cu3djP2uA Z3nj3wpd295KoMrpoaQX+BCmEKc5DthuMga+RAWGQoadH6m1jf73D/CdzSLUibJnQPI7 dc69Ey9+onYkjdsCGAiZZV8SWt1zabyI3DGuXcj01uhjDVyl0KTgbpCOyQBiNC7No9bs fI5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=zetK+W/bHVw7XwdBevEYPYV360JBbDIWAi5v9HH+5Ds=; b=DTQb2iAXT/X2DHex0ld8waIrkFX1v82NzjAcB34kDUx0j2nGcID71+hrowReA8eiAL 5ZXxgg7b2m4pGQ8IgMXMv+aXN5gCfOSgmOWdUVtdNCPrGJUq88G6Lrj9eSG14v1bndks ANtsfwh5hTBVbODvEJUjL0KC7iiTFZBnnsuK0AOan+xIotJlXMkwrinmMkdz+oVtLARR WZoHDpzNjALK2JHOoTvlFR0pkjRrwjDNeSAI3Ndp3SwqvQ82fkJ12DWNB0MRYvf1aRHY vM6ZeP+qcxAKeKNoHzHehSdrxd2oVUm2PVrybC9j85Emv7XxOHd7DJE4h9uI5MWI2ugI NHGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=USDxZZGj; 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 f11si4918092ejh.536.2021.06.04.10.15.54; Fri, 04 Jun 2021 10:16:18 -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; dkim=pass header.i=@linux-foundation.org header.s=google header.b=USDxZZGj; 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 S230035AbhFDRNr (ORCPT + 99 others); Fri, 4 Jun 2021 13:13:47 -0400 Received: from mail-lf1-f51.google.com ([209.85.167.51]:44682 "EHLO mail-lf1-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230169AbhFDRNq (ORCPT ); Fri, 4 Jun 2021 13:13:46 -0400 Received: by mail-lf1-f51.google.com with SMTP id r198so11788157lff.11 for ; Fri, 04 Jun 2021 10:11:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=zetK+W/bHVw7XwdBevEYPYV360JBbDIWAi5v9HH+5Ds=; b=USDxZZGjVEj4Wb7i/ZRsFz5JtQRbDOnr2QyyE0iZ3zBXnjTge18snA9ewptL6QC41y MN48LNtNkLXYStjRT0tMHo7geGbVSu7/fna5H5O2cPPqvlxYMg2Aa5UhNk3jWKr6FKwG k2O7oCKHoalMvnZ6FPpaxbCWmcjgFuxd6O41E= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=zetK+W/bHVw7XwdBevEYPYV360JBbDIWAi5v9HH+5Ds=; b=gpAEI72LxnljFf7c43PcLPFzh4V+x1r/6/1q8U/tCTk7PZWGGn47Mx9k8QbZdpLS+t FtWUM8uekLBicnxWaakjj8MQKYhWgIxRGkhITtzN8icPJz8kZko+0pl+G4MvpCKUlzN2 VbayOa2fAMXCMpmuC/Eyc/M0g8vgrFgk/H2egTWzTx2uyO230X3zbgFDCT/4EJckO/JP wVlSTzdfzke9BXnM9MQFGEsXTjcctHv6RVkJRGaO0Av0x9vs3taLXU8LXD/lugrPVNT0 16IlwRjPeUgnpvo3aY0RK3uKtUQHTCXZjPLiwGfHI1XnVhtaAlSmqSVVP6O8SSkmm6d2 eI2g== X-Gm-Message-State: AOAM532+8mfCRHc4G3auLR2wd9a8V6JPZusSTaYckPs99yZBshK/jR72 UJ9+OOcszADgxc+4ibKxVhT9SYlHYXJTd9JP X-Received: by 2002:ac2:4c2c:: with SMTP id u12mr3293101lfq.209.1622826647464; Fri, 04 Jun 2021 10:10:47 -0700 (PDT) Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com. [209.85.167.44]) by smtp.gmail.com with ESMTPSA id r202sm659214lff.251.2021.06.04.10.10.45 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 04 Jun 2021 10:10:46 -0700 (PDT) Received: by mail-lf1-f44.google.com with SMTP id i9so15083013lfe.13 for ; Fri, 04 Jun 2021 10:10:45 -0700 (PDT) X-Received: by 2002:a05:6512:374b:: with SMTP id a11mr3293378lfs.377.1622826645571; Fri, 04 Jun 2021 10:10:45 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Linus Torvalds Date: Fri, 4 Jun 2021 10:10:29 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC] LKMM: Add volatile_if() To: Peter Zijlstra Cc: Will Deacon , "Paul E. McKenney" , Alan Stern , 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 Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. The compiler *cannot* just say "oh, I'll do that 'volatile asm barrier' whether the condition is true or not". That would be a fundamental compiler bug. It's as if we wrote if (x) y++; and the compiler went "Oh, I'll just increment 'y' unconditionally by one, I'm sure the programmer doesn't mind, the conditional on 'x' is immaterial". No. That's not a C compiler. That's a stinking piece of buggy shit. The compiler has to honor the conditional. In that "y++" case, a compiler can decide to do it without a branch, and basically rewrite the above as y += !!x; but with a "volatile asm", that would be a bug. Of course, we might want to make sure that the compiler doesn't go "oh, empty asm, I can ignore it", but if that's the case then it's not about "volatile_if()" any more, at that point it's "oh, the compiler broke our 'barrier()' implementation", and we have bigger issues. Linus