Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp317334ybl; Sat, 17 Aug 2019 01:10:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqzQr2aLk+1urgl+5uU9Qfn2HoxlSRoVPV6tjOG+CcGrbB5Xk5tf5JzB052EgnMwWdMu0EPo X-Received: by 2002:a17:90a:db06:: with SMTP id g6mr11156238pjv.60.1566029407680; Sat, 17 Aug 2019 01:10:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566029407; cv=none; d=google.com; s=arc-20160816; b=LcZAjzDkwow//y4tbHldO2hv+JzXJdSZakfZTR93Q49UQFtqNayx03HajFjmlvc6Vz 8/ECQvaO4aQajtu30FghvEliYBggA07Zgf9rfyK1Xecp/Hi0Va0jJ1pC4Hyalb+Orn+t PTFbOeOCJIqP8pPr6O+hgWptVPkj6p9+MS2riMdTQKMxs+h/+7xFuHQXEGyVJ3Xlia0G WcQYl/A/vpIrC1zsN+K7PEgF2TcGI/d8mH0QEQYT/B5aa2Jb+W3S70DXIh9GErPo/zg6 gxTVwn/UnTPo3PpA+36UeyA2X399dMN2CLiO8rTzqNYqUprWeytE6h63p1ZQLbG7Udyi 6KAA== 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=vp+MGTABPU/puudI66VKq7mS0MIIOOVXXSvOEmbq4Vc=; b=VKgroS0E1z5O8shoN2OID5exqxuanApUsb5oXhKNra0MN/IGW+8PXEcmpIbiknzrzF sm+uZ0KvfU7lP1QIpg5goQ6Xb0Rg7yt6reONX4bCmyjCyB+B1xTMYJswDjPWxKgd/8/x s+0OyFqo9+hCxg7BNRM/IxKldy/1oRWhCAh3VkOhnghFP21iWOWWZDVplu2GX4hvtU39 noBCkv6VfwTNrEHtFzemPxhXDn3qi70wq6oxmSl0/xEb71qkhlhzEu0TzTPF00a197Av n5faPO0qV6HAgMv2+eRzmAYzRY3ix8QvvA5mgRGptPWslj/iNDDUft2qRVZqpwkhsGLU hpdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=XnXHa9lO; 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 ce18si4655234pjb.70.2019.08.17.01.09.43; Sat, 17 Aug 2019 01:10:07 -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=XnXHa9lO; 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 S1726002AbfHQIIX (ORCPT + 99 others); Sat, 17 Aug 2019 04:08:23 -0400 Received: from mail-lj1-f193.google.com ([209.85.208.193]:46159 "EHLO mail-lj1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725784AbfHQIIX (ORCPT ); Sat, 17 Aug 2019 04:08:23 -0400 Received: by mail-lj1-f193.google.com with SMTP id f9so7236113ljc.13 for ; Sat, 17 Aug 2019 01:08:21 -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=vp+MGTABPU/puudI66VKq7mS0MIIOOVXXSvOEmbq4Vc=; b=XnXHa9lOHjOFvPMmo8A8mnN5Zy8EDYnLSw8sZsPPtKuv7Bh7u/15iBwKR/WjCmHXcn Yk56GYQUBunYG0pnq61rRscJeJ3JCbUHjtLp8xJhXdP0Jrc4dWN17Uw/8YK2ImuCjx8i g6bnklRqS4rVL3SLNkQwZO9zW+9JgoLLdy0OY= 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=vp+MGTABPU/puudI66VKq7mS0MIIOOVXXSvOEmbq4Vc=; b=Ru/cVllH+3ai0CqBVQgwO5CIkMexT3TBQZUbsNZy6tj6CQyA1iWAwpAgx/T7gPjyqm i3RHT8MAqZoOR94OlXHIF0o+3kDAz2ElFxY2lurDRwdpPji10sUnmhrchmlXvYnOyPow oOejiXVRvam7TFNRPmC15ibRyZJNd6i+498AsS0DrCIewPEP80lNVV4w4WUV88HptlaP h0tqpmUu5Vvxc5GtRFfj29TNm/lDLs408VyoKqLtNUyZR/otokoyWwouXDoxBInsZsjS xNRZMsZdv/0xC1PbTy5feK/kJVY68UX+Ep5qBxIhV/+JI1MmdH2Lhb2kugBLiyoEUVDb E63A== X-Gm-Message-State: APjAAAVSBiaoxJ00xhspBiRg9o6PI40k/EVEzptn8lPuWlKhZWvM1EyQ r4FVAWruiHBTHGSO36aBJX0C+qchPu0= X-Received: by 2002:a2e:63cd:: with SMTP id s74mr3866199lje.3.1566029300348; Sat, 17 Aug 2019 01:08:20 -0700 (PDT) Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com. [209.85.167.46]) by smtp.gmail.com with ESMTPSA id s20sm1337116ljg.88.2019.08.17.01.08.18 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 17 Aug 2019 01:08:19 -0700 (PDT) Received: by mail-lf1-f46.google.com with SMTP id j17so5623111lfp.3 for ; Sat, 17 Aug 2019 01:08:18 -0700 (PDT) X-Received: by 2002:ac2:48b8:: with SMTP id u24mr7116353lfg.170.1566029298663; Sat, 17 Aug 2019 01:08:18 -0700 (PDT) MIME-Version: 1.0 References: <241506096.21688.1565977319832.JavaMail.zimbra@efficios.com> <1642847744.23403.1566005809759.JavaMail.zimbra@efficios.com> In-Reply-To: <1642847744.23403.1566005809759.JavaMail.zimbra@efficios.com> From: Linus Torvalds Date: Sat, 17 Aug 2019 01:08:02 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 1/1] Fix: trace sched switch start/stop racy updates To: Mathieu Desnoyers Cc: Thomas Gleixner , Joel Fernandes , Alan Stern , rostedt , Valentin Schneider , linux-kernel , Peter Zijlstra , paulmck , 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, 18:36 Mathieu Desnoyers wrote: > > If WRITE_ONCE has any use at all (protecting against store tearing and > invented stores), it should be used even with a lock held in this > scenario, because the lock does not prevent READ_ONCE() from observing > transient states caused by lack of WRITE_ONCE() for the update. The thing is, we really haven't requred WRITE_ONCE() to protect against store tearing. We have lots of traditional code that does stuff along the lines of .. set of data structure .. smp_wmb(); *ptr = newdata; and we simply *depend* on the compiler doing the "*ptr" as a single write. We've simply always done that. Even on UP we've had the "interrupts will see old value or new value but not some random half-way value", going all the way back to the original kernel sources. The data tearing issue is almost a non-issue. We're not going to add WRITE_ONCE() to these kinds of places for no good reason. > So why does WRITE_ONCE exist in the first place ? Is it for documentation > purposes only or are there actual situations where omitting it can cause > bugs with real-life compilers ? WRITE_ONCE should be seen mainly as (a) documentation and (b) for new code. Although I suspect often you'd be better off using smb_load_acquire() and smp_store_release() when you have code sequences where you have unlocked READ_ONCE/WRITE_ONCE and memory ordering. WRITE_ONCE() doesn't even protect against data tearing. If you do a "WRITE_ONCE()" on a type larger than 8 bytes, it will fall back to __builtin_memcpy(). So honestly, WRITE_ONCE() is often more documentation than protecting against something, but overdoing it doesn't help document anything, it just obscures the point. Yeah, yeah, it will use a "volatile" access for the proper normal types, but even then that won't protect against data tearing on 64-bit writes on a 32-bit machine, for example. It doesn't even give ordering guarantees for the sub-words. So you still end up with the almost the same basic rule: a natural byte/word write will be atomic. But really, it's going to be so even without the WRITE_ONCE(), so... It does ostensibly protect against the compiler re-ordering the write against other writes (note: *compiler*, not CPU), and it does make sure the write only happens once. But it's really hard to see a valid compiler optimization that wouldn't do that anyway. Because of the compiler ordering of WRITE_ONCE against other WRITE_ONCE cases, it could be used for irq-safe ordering on the local cpu, for example. If that matters. Although I suspect any such case is practically going to use per-cpu variables anyway. End result: it's *mostly* about documentation. Just do a grep for WRITE_ONCE() vs READ_ONCE(). You'll find a lot more users of the latter. And quite frankly, I looked at some of the WRITE_ONCE() users and a lot of them were kind of pointless. Somebody tell me just exactly how they expect the WRITE_ONCE() cases in net/tls/tls_sw.c to matter, for example (and that was literally a random case I happened to look at). It's not clear what they do, since they certainly don't imply any memory ordering. They do imply a certain amount of compiler re-ordering due to the volatile, but that's pretty limited too. Only wrt _other_ things with side effects, not the normal code around them anyway. In contrast, READ_ONCE() has very practical examples of mattering, because unlike writes, compilers really can reasonably split reads. For example, if you're testing multiple bits in the same word, and you want to make sure they are coherent, you should very much do val = READ_ONCE(mem); .. test different bits in val .. because without the READ_ONCE(), the compiler could end up just doing the read multiple times. Similarly, even if you only use a value once, this is actually something a compiler can do: if (a) { lock(); B() unlock(); } else B(); and a compiler might end up generating that as if (a) lock(); B(); if (a) unlock(); instead. So doing "if (READ_ONCE(a))" actually makes a difference - it guarantees that 'a' is only read once, even if that value was _literally_ only used on a source level, the compiler really could have decided "let's read it twice". See how duplicating a read is fundamentally different from duplicating a write? Duplicating or splitting a read is not visible to other threads. Notice how nothiing like the above is reasonable for a write. That said, the most common case really is none of the above half-way subtle cases. It's literally the whole "write pointer once". Making existing code that does that use WRITE_ONCE() is completely pointless. It's not going to really change or fix anything at all. Linus