Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp331221ybl; Sat, 17 Aug 2019 01:31:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqxnnZLIq8SJce2rewpZCJCgaKUqlv+J6kBuL02GTQHMCeit+LLi6Xn8Tl1CSZ6fbnbQMUBe X-Received: by 2002:a17:902:4201:: with SMTP id g1mr13145696pld.300.1566030685678; Sat, 17 Aug 2019 01:31:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566030685; cv=none; d=google.com; s=arc-20160816; b=jzsfs58jvWdBEtiXlvbaOVZJ4z4BdXAD6y4TG1IBotPAuY+w0KkWp5562RM9BaVq6x em1JubWxhGZeVQ1BLqQHCxcMCwM2CUfhSvxhs4C6SMFXjoiErszc82w/umH1sznfY2t1 cQq5o/dR8X7VdFHhs/acjm4I2KbtQugv6xKjeGnh3h+FscyPlqjWDikPJt1PSJaNcKKP pu8lVbZDuVxf0hGbw/khAiHWkC7/OBVI/oz28bSEYLLkyTFMMdQzRR40J0IOi08BBqXE xV2dFB95LeDSGuYaMZsXBViqvmAnPY5A082ex944Tja54c1rbRIbxnIKNRWp5Wf2271T NaTQ== 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=XlKyh9+sAT5G8xYZnD/MbJfqU5oH3306V+QSOf7HE5Q=; b=jOQJRf6o6A4ZMotmd2sk6kBKccuiwtdW+HYOCKd+s86VU4Vdlf9DvW2VGzlR5CJ8Og yJ+bxCbvNIuasDBao5+bTJt4H0lDEFFYIHZDZ4012RyUSElK+vQatJTwRKYGaMvP3ROm XRaSHZKRnjWFN7qkjPwfzPtvoz+TyAgxudy5d8BEp2nkJxEZ3MAPM3nSild3Q22LMnfs PvB/1DulkvGCg6TgNcsWOV4V4SOwtn33WBT2CdJc4zkEcem8Avp5SgTyeHKi1a/ddE7+ k+0MgeXXtDyaZgPyEcPJLtVv7lSicxMhKOCD3MV/1YJVaxlmDPELDpADG3wvNIiXi0U6 OwFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=Dnmoym7Y; 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 b6si5978401pfd.29.2019.08.17.01.31.11; Sat, 17 Aug 2019 01:31:25 -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=Dnmoym7Y; 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 S1726239AbfHQI3I (ORCPT + 99 others); Sat, 17 Aug 2019 04:29:08 -0400 Received: from mail-lf1-f66.google.com ([209.85.167.66]:33755 "EHLO mail-lf1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726048AbfHQI3I (ORCPT ); Sat, 17 Aug 2019 04:29:08 -0400 Received: by mail-lf1-f66.google.com with SMTP id x3so5660397lfc.0 for ; Sat, 17 Aug 2019 01:29:06 -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=XlKyh9+sAT5G8xYZnD/MbJfqU5oH3306V+QSOf7HE5Q=; b=Dnmoym7YUYJQ1wGlX051HD2wM0ytNSunxqOj+teyHwLMGb5ME+5utfTxUo1n7F1qwn 5V8dilzCiJ/kQgv+Y0qZEOINx+uM9Zz1UhdCUzBHEII1soTuTmwsNgrKw/34CXstT/ee HjDHWyVuJmgTZRnehZ+wp1t1u1l25WThvTpYs= 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=XlKyh9+sAT5G8xYZnD/MbJfqU5oH3306V+QSOf7HE5Q=; b=O7eYpS+ffiizJOnvk4Hz2BGCH2eOLE7KwZ9ExUN9zBF/SmKZuGpJuF/kxFswW4XEe5 /dMCBqhzrgtkf6pvj5Wm9wJsQaV6sQcK0cHhyhe75aIIiZIXgiDU00C/T26/3vmhxIZL LBH0sid+8DSjYhm8zJfqjx2TTxHQLaQNxRGDFLnWj8rugFl5tctBAxpTx+GOg/HUmE4Y 5XgVqnbnFUPQEe9c9iAPPZnsdeShVeWgxW3XWi5jFaBZl8dyqQQaR4aiQhT8+9+V6ZD/ z0kib/N5m/RJB+/MQmlNglbgLh9P+X5kA7uLty/D3hwoMu2U3/a9oep0N2c9qJ2nhzNs l3QA== X-Gm-Message-State: APjAAAVsOdgFySurYIwfKvlnyba+Y3dE/smzS4C1lt/BUXESNBcBAOqq xXK989lBmiErWvXd4sgLyAdgMh+Be50= X-Received: by 2002:a05:6512:146:: with SMTP id m6mr7249784lfo.90.1566030545060; Sat, 17 Aug 2019 01:29:05 -0700 (PDT) Received: from mail-lf1-f45.google.com (mail-lf1-f45.google.com. [209.85.167.45]) by smtp.gmail.com with ESMTPSA id j4sm1302830lfm.19.2019.08.17.01.29.04 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Aug 2019 01:29:04 -0700 (PDT) Received: by mail-lf1-f45.google.com with SMTP id h28so5625190lfj.5 for ; Sat, 17 Aug 2019 01:29:04 -0700 (PDT) X-Received: by 2002:a19:c20b:: with SMTP id l11mr7275170lfc.106.1566030543835; Sat, 17 Aug 2019 01:29:03 -0700 (PDT) MIME-Version: 1.0 References: <241506096.21688.1565977319832.JavaMail.zimbra@efficios.com> <20190816205740.GF10481@google.com> <3c0cb8a2-eba2-7bea-8523-b948253a6804@arm.com> <20190817045217.GZ28441@linux.ibm.com> In-Reply-To: <20190817045217.GZ28441@linux.ibm.com> From: Linus Torvalds Date: Sat, 17 Aug 2019 01:28:48 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 1/1] Fix: trace sched switch start/stop racy updates To: "Paul E. McKenney" Cc: Valentin Schneider , Joel Fernandes , Thomas Gleixner , Alan Stern , Mathieu Desnoyers , rostedt , linux-kernel , Peter Zijlstra , Boqun Feng , Will Deacon , David Howells 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 Fri, Aug 16, 2019 at 9:52 PM Paul E. McKenney wrote: > > > I'd love to have a flag that says "all undefined behavior is treated > > as implementation-defined". There's a somewhat subtle - but very > > important - difference there. > > It would also be nice to downgrade some of the undefined behavior in > the standard itself. Compiler writers usually hate this because it > would require them to document what their compiler does in each case. > So they would prefer "unspecified" if the could not have "undefined". That certainly would sound very good to me. It's not the "documented behavior" that is important to me (since it is still going to be potentially different across different platforms), it's the "at least it's *some* consistent behavior for that platform". That's just _so_ much better than "undefined behavior" which basically says the compiler writer can do absolutely anything, whether it makes sense or not, and whether it might open a small bug up to absolutely catastrophic end results. > > if (a) > > global_var = 1 > > else > > global_var = 0 > > Me, I would still want to use WRITE_ONCE() in this case. I actually suspect that if we had this kind of code, and a real reason why readers would see it locklessly, then yes, WRITE_ONCE() makes sense. But most of the cases where people add WRITE_ONCE() aren't even the above kind of half-questionable cases. They are just literally WRITE_ONCE(flag, value); and since there is no real memory ordering implied by this, what's the actual value of that statement? What problem does it really solve wrt just writing it as flag = value; which is generally a lot easier to read. If the flag has semantic behavior wrt other things, and the write can race with a read (whether it's the read or the write that is unlocked is irrelevant), is still doesn't tend to make a lot of real difference. In the end, the most common reason for a WRITE_ONCE() is mostly just "to visually pair up with the non-synchronized read that uses READ_ONCE()" Put another way: a WRITE_ONCE() without a paired READ_ONCE() is almost certainly pointless. But the reverse is not really true. All a READ_ONCE() says is "I want either the old or the new value", and it can get that _without_ being paired with a WRITE_ONCE(). See? They just aren't equally important. > > And yes, reads are different from writes. Reads don't have the same > > kind of "other threads of execution can see them" effects, so a > > compiler turning a single read into multiple reads is much more > > realistic and not the same kind of "we need to expect a certain kind > > of sanity from the compiler" issue. > > Though each of those compiler-generated multiple reads might return a > different value, right? Right. See the examples I have in the email to Mathieu: unsigned int bits = some_global_value; ...test different bits in in 'bits' ... can easily cause multiple reads (particularly on a CPU that has a "test bits in memory" instruction and a lack of registers. So then doing it as unsigned int bits = READ_ONCE(some_global_value); .. test different bits in 'bits'... makes a real and obvious semantic difference. In ways that changing a one-time ptr->flag = true; to WRITE_ONCE(ptr->flag, true); does _not_ make any obvious semantic difference what-so-ever. Caching reads is also something that makes sense and is common, in ways that caching writes does not. So doing while (in_progress_global) /* twiddle your thumbs */; obviously trivially translates to an infinite loop with a single conditional in front of it, in ways that while (READ_ONCE(in_progress_global)) /* twiddle */; does not. So there are often _obvious_ reasons to use READ_ONCE(). I really do not find the same to be true of WRITE_ONCE(). There are valid uses, but they are definitely much less common, and much less obvious. Linus