Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp54600ybl; Fri, 16 Aug 2019 18:37:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqxpHDvDzed4Kur8LSCUmnIo6mwDgCFzGCM4SqArjQDxY6Cok1W4RRaB4IW1ZGILsKxJ2j1N X-Received: by 2002:a17:902:654f:: with SMTP id d15mr11118217pln.253.1566005876987; Fri, 16 Aug 2019 18:37:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566005876; cv=none; d=google.com; s=arc-20160816; b=go4RbsH4ZLdQD7bMeRrG044aHzrib6mm3rglUbp2Ye/ddqPVrMe1vddOix+q1+CicQ aawsQfuj+l+sk2lyzvB+2cEWe25jt2bXGWOza2fYoi6Bgsx3kyr3sVuhWfZ5A2wqcHpz zgRjSHYeAKaxAM7/+bpWLAPPIAggaCxMh2MLdU25/9wUW6dV2dAXbbdHCgVUIPEk6wkN s/OYrbA5/HVoBGuoZqDnBvjGmE9c7fo8HnDLEaGRUtdtVGQZDnvxJkMzBNjDodEVJpt8 1BB3WUQnqbi3nqhMeaRK2BQGkCcolmB0pj0Xdo5XHtaz4h9Ic9en16Hc8MQpKKgWNGrn zdew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:thread-index:thread-topic :content-transfer-encoding:mime-version:subject:references :in-reply-to:message-id:cc:to:from:date:dkim-signature:dkim-filter; bh=96IAPoPyhNwCRUJnxRSwtU1kXmraj/p1+Xr9UEL1eek=; b=lqkE9ZVq24B3XFtiCRRbOF7IB8KCa3FF74bbHfDRD0spwvu5skdJc7dr9pK8latj6c Ms9JCQWpG3Q2gSy0vm8V1xHEJ/ulPCvWZORIZk21PNYkjYLF65x+4RNCeLYtv/2dnpe/ n2smPS7msMP1q08HErKGRDlRGFqNt6ZzOV3x1wn0JBT43RQ0KSzdlUKlcKwCj/jssUux AB053mZVmMDveCwUBZ0yYUpgH/eYEFP1Eq3iHoZp7AoFHMine9LXYTIw+aAIxOY890Cp 7nnkpzBGCzu6Ppb4BCJW9UwuG77TKlv2oomxr3+w3/JEjar7QbxPHXDIAVyUqjtnsYwE y+6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@efficios.com header.s=default header.b=CM9xBLyW; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y13si5451437pfc.251.2019.08.16.18.37.42; Fri, 16 Aug 2019 18:37:56 -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=@efficios.com header.s=default header.b=CM9xBLyW; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=efficios.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726067AbfHQBgw (ORCPT + 99 others); Fri, 16 Aug 2019 21:36:52 -0400 Received: from mail.efficios.com ([167.114.142.138]:57410 "EHLO mail.efficios.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725988AbfHQBgw (ORCPT ); Fri, 16 Aug 2019 21:36:52 -0400 Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id A3A642C8933; Fri, 16 Aug 2019 21:36:50 -0400 (EDT) Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id fF-kLxWnN6pU; Fri, 16 Aug 2019 21:36:50 -0400 (EDT) Received: from localhost (ip6-localhost [IPv6:::1]) by mail.efficios.com (Postfix) with ESMTP id 205ED2C8930; Fri, 16 Aug 2019 21:36:50 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 mail.efficios.com 205ED2C8930 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficios.com; s=default; t=1566005810; bh=96IAPoPyhNwCRUJnxRSwtU1kXmraj/p1+Xr9UEL1eek=; h=Date:From:To:Message-ID:MIME-Version; b=CM9xBLyWwxkLSO/6MqFwv941L7m4F3uxH24Q0TjmrgpW14EoOySOtudTDpLTWQXXB obhOseuxl+r8Y4SkAseInWd/o8mpJz8BVR4VTK3FX1UIYTsuuyvAFwnL9kuK9IxKO5 BrpZWxAbeT1QsH1VwdCU9/dDNDic61uPJ4fPuxQDNKsbcZNVh50IHOm3BlJY8go4Wk RtlN+fbzxnrIsR9aHhuNTOsF3stWDlcEI/wBVqL41cas0QcKcSZk5Qxa3wpguoF9xG kI1CaYXgk9Nk9lucTXFyfq0hoA5Va76IHe1s07kVpjZB4Ev9aqTAiaY1UmeRclN9XW A8mamxa4KL+Hw== X-Virus-Scanned: amavisd-new at efficios.com Received: from mail.efficios.com ([IPv6:::1]) by localhost (mail02.efficios.com [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id 58D9Cft72E0O; Fri, 16 Aug 2019 21:36:50 -0400 (EDT) Received: from mail02.efficios.com (mail02.efficios.com [167.114.142.138]) by mail.efficios.com (Postfix) with ESMTP id 008D82C8926; Fri, 16 Aug 2019 21:36:49 -0400 (EDT) Date: Fri, 16 Aug 2019 21:36:49 -0400 (EDT) From: Mathieu Desnoyers To: Linus Torvalds Cc: Thomas Gleixner , "Joel Fernandes, Google" , Alan Stern , rostedt , Valentin Schneider , linux-kernel , Peter Zijlstra , paulmck , Boqun Feng , Will Deacon , David Howells Message-ID: <1642847744.23403.1566005809759.JavaMail.zimbra@efficios.com> In-Reply-To: References: <241506096.21688.1565977319832.JavaMail.zimbra@efficios.com> Subject: Re: [PATCH 1/1] Fix: trace sched switch start/stop racy updates MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Originating-IP: [167.114.142.138] X-Mailer: Zimbra 8.8.15_GA_3829 (ZimbraWebClient - FF68 (Linux)/8.8.15_GA_3829) Thread-Topic: trace sched switch start/stop racy updates Thread-Index: FtzrAEkFElsTg1M7nrKVRqIiganZVA== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ----- On Aug 16, 2019, at 5:04 PM, Linus Torvalds torvalds@linux-foundation.org wrote: > On Fri, Aug 16, 2019 at 1:49 PM Thomas Gleixner wrote: >> >> Can we finally put a foot down and tell compiler and standard committee >> people to stop this insanity? > > It's already effectively done. > > Yes, values can be read from memory multiple times if they need > reloading. So "READ_ONCE()" when the value can change is a damn good > idea. > > But it should only be used if the value *can* change. Inside a locked > region it is actively pointless and misleading. > > Similarly, WRITE_ONCE() should only be used if you have a _reason_ for > using it (notably if you're not holding a lock). > > If people use READ_ONCE/WRITE_ONCE when there are locks that prevent > the values from changing, they are only making the code illegible. > Don't do it. I agree with your argument in the case where both read-side and write-side are protected by the same lock: READ/WRITE_ONCE are useless then. However, in the scenario we have here, only the write-side is protected by the lock against concurrent updates, but reads don't take any lock. 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. 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 ? In terms of code change, should we favor only introducing WRITE_ONCE in new code, or should existing code matching those conditions be moved to WRITE_ONCE as bug fixes ? Thanks, Mathieu > > But in the *absence* of locking, READ_ONCE/WRITE_ONCE is usually a > good thing. The READ_ONCE actually tends to matter, because even if > the value is used only once at a source level, the compiler *could* > decide to do something else. > > The WRITE_ONCE() may or may not matter (afaik, thanks to concurrency, > modern C standard does not allow optimistic writes anyway, and we > wouldn't really accept such a compiler option if it did). > > But if the write is done without locking, it's good practice just to > show you are aware of the whole "done without locking" part. > > Linus -- Mathieu Desnoyers EfficiOS Inc. http://www.efficios.com