Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp5605478ybi; Tue, 4 Jun 2019 09:07:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqxjoGy7mLIZKoREJQK6y6xwOwjUkp412zWERnGynv/IFVZbO78EHCMcmnmYLD0N3ONjWlMb X-Received: by 2002:a63:dc09:: with SMTP id s9mr37066474pgg.425.1559664438678; Tue, 04 Jun 2019 09:07:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559664438; cv=none; d=google.com; s=arc-20160816; b=zPr9VdsRGD/OzoMJNemwpIZ+9qNjvFffAPjJ/Zi9Nd5dv/UnG4umHHxQ08FLXbXnEx 5Nywuv7QIqx9pKOu9qT5qn8f5dGtUtx4wCuGq3L0ropXzwNAhoM5HAj4bZxdCkO26G7T ot3XYgfm9Ykbx6B2i01L7myts42jM9ljeUwVTIyhh1hrIoQTTLpcgtqoi5gLZ1HFyEkJ xoMwzu/3L8x4JvoZ2KqvAbnnePXM0rghE8zBZEs/3quvu8tUqJ6szT509m6FSfBIFhLn iCK8XK41fkubRWJNT8FFr+TNi8cb+3hEjlutWbQ6eotYbyGydXMeHbi63j5nAyx1iWsN x+4w== 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; bh=yHKf0dBjWiln0w4ZthMmJnFg12c3Gmyiday/W+Ueeog=; b=BIzxFQUZF7DgbQAW1fSI0iMVwYFuE37iB/DW5H+vlashh1mgsNnffHi6Z1UCE/sBqU Wvd5Kmpca5q43tyKecqy3oMYAq5zhhSFlDYF1uvDnmgCrPMFQF2l1gUIqoJbrXFWelc6 8S0ek6Wa/KDd+vmTgzxGnxqbrbnWasgkjnQ/LvJu9mKBggnjgDzR5JHYwkWKuETRhirG I9YCgknjFixBfIv4ctbwhzuGvIG8H2DyzmQcZOP+B9g89fOL/Hr8RlEmGO0ebl5i32OT OYQMf/Kg6kn5edRoRInxj3sMcbAq+pjMOffp4ydo2eWtjE7rkoLCSVa/JHBF0CyNIfmo d6Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=P+lo3dX3; 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 r3si17661805pgj.319.2019.06.04.09.07.00; Tue, 04 Jun 2019 09:07:18 -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=P+lo3dX3; 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 S1728234AbfFDQFS (ORCPT + 99 others); Tue, 4 Jun 2019 12:05:18 -0400 Received: from mail-lf1-f43.google.com ([209.85.167.43]:43935 "EHLO mail-lf1-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728160AbfFDQFR (ORCPT ); Tue, 4 Jun 2019 12:05:17 -0400 Received: by mail-lf1-f43.google.com with SMTP id j29so2957366lfk.10 for ; Tue, 04 Jun 2019 09:05:16 -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=yHKf0dBjWiln0w4ZthMmJnFg12c3Gmyiday/W+Ueeog=; b=P+lo3dX3Y853XVVu/NErjTHy0y3uTOH5/1fFj4/+SdG8xmL9Vhgu3H+7epySB5/DoQ dE44yz0S2d21n4MIUQWM/Dq2DDY7x8hX/s3oEWxiond0RMSTcyQ7l8X3Q+Qh7jsMktff GFarG1OFqhP44+4kIN0kRqLqWQzDquNP2iWEM= 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=yHKf0dBjWiln0w4ZthMmJnFg12c3Gmyiday/W+Ueeog=; b=dmmzYcG2GyPFQB25NadWNLm/ExsBzm1H6LJ2QuC/7WdT1hSdxKHWeVWKbdRFCjAn0B dR/TEriaeXnfCMey41dlI9wg50RiK3oyO0bPSMcWndDWB/bj4cxmB4TUZgQkAmNunyHX UJYC1Om6JHIRx8Jb0rAsDX53YMHj0nXNVRrulfiAc3TyFPhYo+Nhy4cALbbAUWfGH2Cg zOcS3/2HXA7tONrui9qUCcYom9G0+apxH+MY7w7cAVT+Wl5A903yBLgyzNX+tHkb/aZD 4M0TiQQ8pvgkjjcofJgAXNcfsEi+/yfj8tJGHRwJFtPvX1uig+jJHsHm1IlzQNODAvVZ 1FgQ== X-Gm-Message-State: APjAAAV45rxTkG8lp7W9ucWKSUkdQ7lOJBy98kWXIvQternhKWjkNHL9 3OqqFxlj5uHjll3otHuQPkPI0vzPW9A= X-Received: by 2002:ac2:419a:: with SMTP id z26mr16557625lfh.122.1559664313554; Tue, 04 Jun 2019 09:05:13 -0700 (PDT) Received: from mail-lj1-f181.google.com (mail-lj1-f181.google.com. [209.85.208.181]) by smtp.gmail.com with ESMTPSA id m11sm3776406lfk.82.2019.06.04.09.05.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 04 Jun 2019 09:05:12 -0700 (PDT) Received: by mail-lj1-f181.google.com with SMTP id m23so1955978lje.12 for ; Tue, 04 Jun 2019 09:05:11 -0700 (PDT) X-Received: by 2002:a2e:9654:: with SMTP id z20mr4923630ljh.52.1559664311655; Tue, 04 Jun 2019 09:05:11 -0700 (PDT) MIME-Version: 1.0 References: <20190603200301.GM28207@linux.ibm.com> In-Reply-To: From: Linus Torvalds Date: Tue, 4 Jun 2019 09:04:55 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: rcu_read_lock lost its compiler barrier To: Alan Stern Cc: "Paul E. McKenney" , Boqun Feng , Herbert Xu , Frederic Weisbecker , Fengguang Wu , LKP , LKML , Netdev , "David S. Miller" , Andrea Parri , Luc Maranget , Jade Alglave 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 Tue, Jun 4, 2019 at 7:44 AM Alan Stern wrote: > > Even if you don't think the compiler will ever do this, the C standard > gives compilers the right to invent read accesses if a plain (i.e., > non-atomic and non-volatile) write is present. Note that for the kernel, it's not like we go strictly by what the standard says anyway. The reason for the "read for write" thing is that obviously you sometimes have broken architectures (*cough*alpha*cough*) that need to do some common writes are read-maskmodify-write accesses, and for bitfields you obviously *always* have that issue. In general, a sane compiler (as opposed to "we just read the standard") had better not add random reads, because people might have some mappings be write-only when the hardware supports it. And we do generally expect sanity from our compilers, and will add compiler flags to disable bad behavior if required - even if said behavior would be "technically allowed by the standard". > (Incidentally, regardless of whether the compiler will ever do this, I > have seen examples in the kernel where people did exactly this > manually, in order to avoid dirtying a cache line unnecessarily.) I really hope and expect that this is not something that the compiler ever does. If a compiler turns "x = y" into if (x != y) x = y" (like we do manually at times, as you say), that compiler is garbage that we cannot use for the kernel. It would break things like "smp_wmb()" ordering guarantees, I'm pretty sure. And as always, if we're doing actively stupid things, and the compiler then turns our stupid code into something we don't expect, the corollary is that then it's on _us_. IOW, if we do if (x != 1) { ... } x = 1; and the compiler goes "oh, we already checked that 'x == 1'" and moves that "unconditional" 'x = 1' into the conditional section like if (x != 1) { .. x = 1; } then that's not something we can then complain about. So our expectation is that the compiler is _sane_, not that it's some "C as assembler". Adding spurious reads is not ok. But if we already had reads in the code and the compiler combines them with other ops, that's on us. End result: in general, we do expect that the compiler turns a regular assignment into a single plain write when that's what the code does, and does not add extra logic over and beyond that. In fact, the alpha port was always subtly buggy exactly because of the "byte write turns into a read-and-masked-write", even if I don't think anybody ever noticed (we did fix cases where people _did_ notice, though, and we might still have some cases where we use 'int' for booleans because of alpha issues.). So I don't technically disagree with anything you say, I just wanted to point out that as far as the kernel is concerned, we do have higher quality expectations from the compiler than just "technically valid according to the C standard". Linus