Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp5199995pxj; Wed, 9 Jun 2021 11:29:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3EUHaGNYZpR4ha2r0gthK5KaWB5SlLCUD4mXKmA0q8EjU8cSorxpOHfiRD7TFmwgcpExn X-Received: by 2002:aa7:dc4f:: with SMTP id g15mr712961edu.277.1623263375294; Wed, 09 Jun 2021 11:29:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623263375; cv=none; d=google.com; s=arc-20160816; b=NqDqJrOINRNwoOFw7ShI4swhojTZoclk6uT9YyoLNWXF180ysx+8wMxxWxUP6/+cLW bW57AKEK5ANKFs2vXIu49k96cjSlE4EOJyLo93zom46OioJS7584QgB36L9f4j7YAyHv 7jYf60Fxx9qZYVedtypwTI2p+W/JTdZ5PD6OabWruXzf7rxTesUTjH3PwPnGMe3UZgs/ HcvxwM0n6fnjLqbnGC+JxU3BetvtCTgwj/ULawqErNSKjhrNJtRIXY8yMJO0kBrBGOih W0WCgQxaGmQEdTJHi+8a8U6Cw0PBVAZt4NiH2nC3FMGfyoDIpUmr/hU5YEItyWI+KS/k Rdvw== 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=bFQd3zAD8lpUyxGScL5+vdciPUdoe8anCwZ/Pot9vr8=; b=Wf7HxkWTS+T80wWCVWgi64ZzlbbCoHi4umAveb4Hf5C8aBKAQufQzKrvJYVI0zcyiw ycg8mqGPMcbUu4zy+xvMjdUKNy8VFPn2/5Ot+XQkB94Klr3QNvm7QM5BzbVmWVMTpSIR q/U7ASDLS0cIcJw5a5oCfwGrL8UbDq89M0VkqhHymT5rUyrmuGtgLcnUYVQVGoYUk5rL SyMaICDK4Zka+AVBYqIBj9uh+qZEaI3EQorZ5+dc0GL8KRBJHgMbaQ7aAGIe/vQqF2ws k5JBcfWURUVu0BRTImhYd9FNt+GVxmcRes8g+htZdo9btpTYjxBY0+FNXdOxe7Ve+/iK BHyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=buypirlx; 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 be13si326471edb.104.2021.06.09.11.29.10; Wed, 09 Jun 2021 11:29:35 -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=buypirlx; 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 S229542AbhFIS2s (ORCPT + 99 others); Wed, 9 Jun 2021 14:28:48 -0400 Received: from mail-lj1-f173.google.com ([209.85.208.173]:33361 "EHLO mail-lj1-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229472AbhFIS2s (ORCPT ); Wed, 9 Jun 2021 14:28:48 -0400 Received: by mail-lj1-f173.google.com with SMTP id r16so1177458ljc.0 for ; Wed, 09 Jun 2021 11:26:42 -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=bFQd3zAD8lpUyxGScL5+vdciPUdoe8anCwZ/Pot9vr8=; b=buypirlxci94sxOGbi+7kSMs1iSYy0ZicxwYsW9OVNC15G9EZTp5mpVkZytFoVCSqf KVWjrEyyIi3Cg3hAC/szqXXHzHz7n+fyN6GsW0ii+AaZ+ugC2LJF9qadg07T+AKyMKVR M56q0HKD/t0ucenNnyisNBKXGXANZ8TGiRLew= 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=bFQd3zAD8lpUyxGScL5+vdciPUdoe8anCwZ/Pot9vr8=; b=WohXZfb81AgQQShxCateal9Oe227bTxqplM9rWaas49pTaMxuZ1QGlk1YBp83qdtDW SakpLN5w4QkrYXvMjVfLn41wPtHZeAAU4/ZWbLoNox7xp3JNry61oatNVxgTBSkAgo4D GSOSw5Q8U0ZC1LlE4X/yDhiJLuPBmBTrl66TtIbbMAjyVbvi/KNrCC/XEKc295IwF9RH b/s+6m/6IRPDprFU1+DhUmvb3w2w+gvk84uRMrIpjCI95vKCOVydoyyWPVyB/TBiXc9C IlA+ep4nlU5kVe7ra9ApivoqsolsA43B9MuOQxQOeY1TEpDLggkLzeBhcvbu5PZEv1of v1xw== X-Gm-Message-State: AOAM5303EpQIS+2q63rrxmENV76vHb4y+N2/uJO8NsAb5FtpcpKGgjGR stIbBzhA32n0qruFk1udFU49M/ibYFNKSljQ X-Received: by 2002:a2e:2a86:: with SMTP id q128mr872004ljq.119.1623263141846; Wed, 09 Jun 2021 11:25:41 -0700 (PDT) Received: from mail-lf1-f50.google.com (mail-lf1-f50.google.com. [209.85.167.50]) by smtp.gmail.com with ESMTPSA id s9sm53145ljp.62.2021.06.09.11.25.40 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 09 Jun 2021 11:25:41 -0700 (PDT) Received: by mail-lf1-f50.google.com with SMTP id m21so23807870lfg.13 for ; Wed, 09 Jun 2021 11:25:40 -0700 (PDT) X-Received: by 2002:a05:6512:3f82:: with SMTP id x2mr460411lfa.421.1623263140500; Wed, 09 Jun 2021 11:25:40 -0700 (PDT) MIME-Version: 1.0 References: <20210607152806.GS4397@paulmck-ThinkPad-P17-Gen-1> <20210608152851.GX18427@gate.crashing.org> <20210609153133.GF18427@gate.crashing.org> In-Reply-To: From: Linus Torvalds Date: Wed, 9 Jun 2021 11:25:24 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC] LKMM: Add volatile_if() To: Marco Elver Cc: Segher Boessenkool , Peter Zijlstra , "Paul E. McKenney" , Alexander Monakov , Jakub Jelinek , Alan Stern , Will Deacon , 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 Wed, Jun 9, 2021 at 9:13 AM Marco Elver wrote: > > I had a longer discussion with someone offline about it, and the > problem with a builtin is similar to the "memory_order_consume > implementation problem" The "memory_order_consume" problem is *entirely* artificial, and due to the C standards body incompetence. Really. I was there. Only very peripherally, but I was involved enough to know what the problem was. And the problem wasn't the concept of 'consume'. The problem was entirely and 100% the incorrect model that the C standards people used to describe the problem. The C standards people took a "syntax and type based" approach to the whole thing, and it was an utter disaster. It's the wrong model entirely, because it became very very hard to describe the issue in terms of optimizations of expressions and ordering at a syntactic level. What the standard _should_ have done, is to describe it in the same terms that "volatile" is described - make all memory accesses "visible in the virtual machine", and then specify the memory ordering requirements within that virtual machine. We have successful examples of that from other languages. I'm sorry if this hurts some C language lawyers fragile ego, but Christ, Java did it better. Java! A language that a lot of people love to piss on. But it did memory ordering fundamentally better. And it's not like it would even have been a new concept. The notion of "volatile" has been there since the very beginning of C. Yes, yes, the C++ people screwed it up mightily and confused themselves about what an "access" means. But "volatile" is actually a lot better specified than the memory ordering requirements were, and the specifications are (a) simpler and (b) much *much* easier for a compiler person to understand. Plus with memory ordering described as an operation - rather than as a type - even the C++ confusion of volatile would have gone away. So the very thing that likely made people want to avoid the "visible access in the virtual machine" model didn't even _exist_ in the first place. So the language committee pointlessly said "volatile is bad, we need to do something else", and came up with something that was an order of magnitude worse than volatile, and that simply _couldn't_ possibly sanely handle that "problem of consume". But the problem was always purely about the model used to _describe_ the issue being bad, not the issue itself. The "consume" memory ordering is actually very easy to describe in the "as if" virtual machine memory model (well, as easy as _any_ memory ordering is). If the C standards committee hadn't picked the wrong way to describe things, the problem simply would not exist. Really. And I guarantee you that compiler writes would have had an easier time with that "virtual memory model" approach too. No, memory ordering sure as hell isn't simple to understand for *anybody*, but it got about a million times worse by using the wrong abstraction layer to try to "explain" it. It really is fairly easy to explain what "acquire" is at a virtual machine model level. About as easy as memory ordering gets. For a compiler writer, it basically turns into "you have to do the actual access using XYZ, and then you can't move later memory operations to before it". End of story. So you can actually describe these things in fairly straighforward manner if you actually do it at that virtual machine level, because that's literally the language that the hardware itself works at. And then you could easily have defined "consume" as being the same thing as "acquire", except that you can drop the special XYZ access (fence, ld.acq, whatever) and replace it with a plain load if there are only data dependencies on the loaded value (assuming, of course, that your target hardware then supports that ordering requirements: alpha would _always_ need the barrier). That could literally have been done as a peephole optimization, and a compiler writer would never have had to even really worry about it. Easy peasy. 99% of all compiler writers would not have to know anything about the issue, there would be just one very special optimization at the end that allows you to drop a barrier (or turn a "ld.acq" into just an "ld") once you see all the uses of that loaded value. A trivial peephole will handle 99% of all cases, and then for the rest you just keep it as acquire. So anybody who tells you that "consume is complicated" is wrong. Consume is *not* complicated. They've just chosen the wrong model to describe it. Look, memory ordering pretty much _is_ the rocket science of CS, but the C standards committee basically made it a ton harder by specifying "we have to make the rocket out of duct tape and bricks, and only use liquid hydrogen as a propellant". Linus