Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2318778imm; Thu, 7 Jun 2018 08:42:24 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIwkESFaPAIuhaVk18/P9KM6W7fN14bgtr6dZp4NanZFCVXUk3xXGZgw6PNuaKY9gm6h5EQ X-Received: by 2002:a62:3a59:: with SMTP id h86-v6mr2251105pfa.209.1528386144625; Thu, 07 Jun 2018 08:42:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528386144; cv=none; d=google.com; s=arc-20160816; b=pqC6HwUFF/549qHxucXLennfBshRIUD8DAtITXgvwGhQKMp59q8xNPgf2GncDn6zTw 1M4joEWov+6NczaI1gKrNu136p9JA4UL1ZC2V7RE0A4qyj6Qh46FYA6AVqA0/y7CbH48 H9WDHf43MY0z7G474fx3qznOJEeA4+7h/oZz9I/A/yKczjdTA6bfuiWuQJ8dB/SBNZxQ 5fXDpIok+uSzWwt+88ZPi/AlRR8MBPZubH887Nv/axzbKIzHmdJOKbuXAxQbgbh1Zpzy gkRoQRBKq4Ls0EIvSZpPcH6uzocTSK3xsbXfXUc3bqItd3mp9NZOFj0pvGnTKiZ5+F9T nPPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=f7wI1NzODy9bK01WezJ2uFwIVzrX85WEH/rGD5qMJBI=; b=hzNmHxvZPpAUuj1mcV5LwV9V9EyRgjIDHtfBq8C4043B4UqnuGhXAIOgXufm8q3maN qgcoI/cV90UNcoXgtr1BaHML1smeJjxykr02errnWZyzs1zgQgqv6wxbMkdoUKew2b4i PgvTjp0+K+1cvjF7DFhAA59VxeQTTLEC+vRPQ+/F3dROZEyGN6qkMgq9LR+4hIxmcuET xMan3rwnJzlYj67lMECsZY+9Vg+2vlnqII921ql9cuxhuWb0r+dzBOa9WHK6+LWYSVb+ NgHOQeC80sld+iWYfJnkX1CH7e4ij1AnpjCZgC7Lmxq/rh9/trgDvf+rHWJ8yX3xiGUr iorw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=cMSxAp+k; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q6-v6si43568605pgs.451.2018.06.07.08.42.10; Thu, 07 Jun 2018 08:42:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=cMSxAp+k; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934283AbeFGPlG (ORCPT + 99 others); Thu, 7 Jun 2018 11:41:06 -0400 Received: from mail-io0-f172.google.com ([209.85.223.172]:38225 "EHLO mail-io0-f172.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933177AbeFGPlC (ORCPT ); Thu, 7 Jun 2018 11:41:02 -0400 Received: by mail-io0-f172.google.com with SMTP id l19-v6so12334802ioj.5; Thu, 07 Jun 2018 08:41:02 -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=f7wI1NzODy9bK01WezJ2uFwIVzrX85WEH/rGD5qMJBI=; b=cMSxAp+kf3mVsRKGIWQ4l6RHZR7hxdqIE/w8bW0TMHhJzwU93xfoYn2UNrE9gF/k+z eqjF4BokyOCtWcWJxlLwR5CavcjiJNUd/NS5gtO96h4fQWe9aW0YKErhB8NsKLDGaQjf YaAcxwDq9bTlYwnHYOrXGi8cmxU8mOsXgS8Kg= 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=f7wI1NzODy9bK01WezJ2uFwIVzrX85WEH/rGD5qMJBI=; b=Dq/0UEuDRhCLhdyZJfZ9RcYaFkafo4WS+EkvdYQIMHYTX+24k+4FuV2pTfejKBitcG iyvc+861vKyNVkOQSR7k+I4h+0AGRCQPXGhlKd8EJTzfnzWvTiTfBY5cfwIDXG7amtYm jy93/SNKCcfllzTPcM2exiVkuSlH2fnqvUNUjZWUx0luMzWy/fjQ0aOuew2W85hHqeZ/ fgO92s6rGPASe6JUtqzWSd4pSnK6JQ8JFqUZ80F7rm2r07+7WD0Cg6WKdEdz7LtHPGQ0 tVt5WxJj0JpYFeMIUATxwo9WybbfP+eyFf5I6/Bf3XQmcyZCoxFcbFYCYezHg+RSOclX Tx7Q== X-Gm-Message-State: APt69E0geTf1t0KQWOi8veGZ52n6FIWxKZIQcIlHljZywd21r1qx1biz 9hpt0qBQfEKICtRV9Ov98olcZk32hjnES6xc22E= X-Received: by 2002:a6b:1b90:: with SMTP id b138-v6mr2051938iob.48.1528386061869; Thu, 07 Jun 2018 08:41:01 -0700 (PDT) MIME-Version: 1.0 References: <20180607094314.GF3593@linux.vnet.ibm.com> In-Reply-To: From: Linus Torvalds Date: Thu, 7 Jun 2018 08:40:49 -0700 Message-ID: Subject: Re: LKMM litmus test for Roman Penyaev's rcu-rr To: Alan Stern Cc: Paul McKenney , Roman Pen , Linux Kernel Mailing List , linux-arch , andrea.parri@amarulasolutions.com, Will Deacon , Peter Zijlstra , Boqun Feng , Nick Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Ingo Molnar Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 7, 2018 at 7:57 AM Alan Stern wrote: > > You can look at a memory model from three points of view: > > 1. To a programmer, the model provides both guarantees (a certain > code snippet will never yield a particular undesired result) > and warnings (another snippet might yield an undesired result). > > 2. To a CPU designer, the model provides limits on what the > hardware should be allowed to do (e.g., never execute a store > before the condition of a preceding conditional branch has been > determined -- not even if the CPU knows that the store would be > executed on both legs of the branch). > > 3. To a compiler writer, the model provides limits on what code > manipulations should be allowed. > > Linus's comments mostly fall under viewpoint 2 (AFAICT), and a lot of > our thinking to date has been in viewpoint 1. But viewpoint 3 is the > one most relevant to the code we have been discussing here. Hmm. Yes. I've been ignoring it because we've historically just "solved" it by having barriers (either "asm volatile" with a memory clobber, or just a volatile access) for the cases under discussion. There isn't a lot of room for compiler games there. We've had issues with compilers _elsewhere_, but when it comes to memory models, we've always solved it with "hey, let's make that access volatile", or "we will force the compiler to use *this* instruction and nothing else". So we've never actually had much worry about what a compiler could do for these particular issues. In fact, one of the reasons I personally doubt we will ever use the C++ memory model things is that I simply don't trust the stanards body to do the right thing. The discussion about control dependencies vs data dependencies made me 100% convinced that there is no way in hell that the C++ standards body model can ever work reliably. Writing the requirements down as English, and writing them down as an "abstract machine" that had a lot of syntax caused untold and completely unnecessary verbiage, and made the end result incomprehensible to any normal human. Very much so to a compiler writer. So honestly, I've given up on the C++ memory model. We've done quite well with inline asm and "volatile", and since I do not believe we will ever get rid of either, the C++ memory model isn't much of an improvement. And notice that this is very much a fundamental doubt about the whole approach that the standards language is taking. I'm not doubting that a memory model cannot be defined. I'm doubting that it can sanely be described with the approach that the C standards body has. Honestly, if the C++ standards body had taken a completely different approach of actually *definining* a virtual machine, and defining the actual code generation (ie the language would actually *define* what every operation did in the virtual machine), and defining what the memory ordering within that virtual machine was, I would give it a chance in hell of working. But that's not what the C++ standards body did. Instead, it tried to define things in terms of "dependency chains" and other very high-level abstractions. So I do not believe for a second in the C++ memory ordering. It's broken. It's broken for fundamental reasons, and I doubt we'll ever use it because no compiler writer will ever get it remotely right. > Saying that a control dependency should extend beyond the end of an > "if" statement is basically equivalent to saying that compiler writers > are forbidden to implement certain optimizations. Honestly, what I think the standard should do is *define* that if (a) B else C is defined to generate the code load a test je label1; B jmp label2 label1: C label2: in the virtual machine. And then each operation (like "load" vs "load_acquire" vs "load_volatile") would have well-defined semantics in that virtual machine. And then a compiler writer would be able to do any optimization he dam,n well please, as long as it is semantically 100% equivalent. That would actually give a program well-defined semantics, and I guarantee it would make compiler writers happier too, because they'd have a definition they can follow and think about. The memory ordering requirements would fall out from the memory ordering defined for the virtual machine, and then that machine could be described sanely. In the above kind of situation, I believe we could have described what "control vs data dependency" means, because you could have specified it for the virtual machine, and then the compiler writers could determine from that well-defined flow (and the kind of load they had) whether they could turn a data dependency into a control dependency during optimization or not. But that's not how the C standard is written, and it's not how the people involved *want* to write it. They want to write it at a high level, not a low level. End result: I really don't trust what the C++ memory ordering semantics are. It's all much too wishy-washy, and I don't think a compiler writer can ever understand it. > Now, my experience > has been that compiler writers are loathe to give up an optimization > unless someone can point to a specific part of the language spec and > show that the proposed optimization would violate it. I agree. Which is why we sometimes use the big hammer approach and just say "don't do this optimization at all". But it's also why the language definition being unclear is so horrible. It's sometimes simply not all that obvious what the standard actually allows and what it doesn't, and memory ordering is going to be way more fragile than even the existing confusion. So I agree 100% with you that the LKMM cases are not likely to affect the C standard, and not affect compiler writers. But I also think that's largely irrelevant, because I don't think it's worth worrying about. I don't believe we will be using the standard memory ordering, because it's not going to end up usable, and we'll continue to roll our own. I did hope a few years ago that that wouldn't be the case. But the discussions I was involved with convinced me that there's no hope. Linus