Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp665070ybl; Wed, 21 Aug 2019 03:47:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqyxSIBOfOuu3L1HGXkECdMYRew4zHw18wXVZwQcoYHsGPtX85vRyASdAMZC7okMAl8VtIPg X-Received: by 2002:a17:902:1c7:: with SMTP id b65mr27356932plb.313.1566384420354; Wed, 21 Aug 2019 03:47:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566384420; cv=none; d=google.com; s=arc-20160816; b=Sf/p0knRU9xKGU0S0vYiZjOB/MWBJInRjPBZWci4ExVtTcBDPfxClMY5Q6v5JmBibC 3/PhivrAP4eigUP2ngefM/WrQoimDgyCI+Kocbe1spuo/xQS1TomqI288WIhyVRmK0mw IyoqoCHy8HGYoFw3MJxwsoLA+zs5yQvkfqd0fgK9/gxOdlhI99LsEQaN7SNfwTVm9eY5 2qM6P1KUoVoJPWLAvbq30+hBLLTGT3+Kkqvy0gwVJet0FDYBDa6UZDHENkKOIJxXAz5j /61/BSiwJZi55be4LCX+fBoesAJcOm0uSxSMO8OKBlMvQ084HsudolFGCgMe13Ulij0R udMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=r2oaiaSIt+g2dbhMn6ox4560MBskLwpJ6xTp7Idgsoo=; b=IYB/1E2Ha2HkUvYoV2PRl7BnFq9NIcpo0ohhJytibf0RvMTaV16I51iqSJ1pumnPfy X0Wff5FCPUYC6hOufLehdGtuqT0m8VrjAGIs/fOVzhDvlBPrdhbHKtWEP5LdfBktXhHj ZdAzAaUKHmLRwExDaJdUHSV1YtBX2sQ1bWaJlOwR0T4qEqaZ6yjRcTwKZUPvrSA69Uvh jD3lfYifETQ1SpxVmEUyAnd2b8XbUrzH0DJyEJH49GC6knuFZOhkCDgBbJj+ewW1V3tH vT/2E7pSnoFrbHEhBsohn28caXvDy8XVPPt8ED/ElIRv1isX5kzvNxFF5qB4fVjY4q7g 3S0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wnfiH+LP; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t23si14310322plr.108.2019.08.21.03.46.45; Wed, 21 Aug 2019 03:47:00 -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=@kernel.org header.s=default header.b=wnfiH+LP; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728092AbfHUKcI (ORCPT + 99 others); Wed, 21 Aug 2019 06:32:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:54112 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727318AbfHUKcI (ORCPT ); Wed, 21 Aug 2019 06:32:08 -0400 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id A32F42332A; Wed, 21 Aug 2019 10:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566383526; bh=A+iM2fcbvw+d9Qr+hJyFjUYYb+TAcMjZYCiuBg29XNw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=wnfiH+LPw4EVdrkSmLcIk7Zd2olnASwh6NoDlI6zKtQxRw5kfxtMR3PVfxGyny/pC evcfutPLvZng5VSBB79v3dl2qNSkKxRxQnodbUN/R6qCuIQdRUsr3u97Sm0Lq1RbYC A9kK9RJCZRFQv8ny+jlfOn1MB6QczAxJbSmNO8II= Date: Wed, 21 Aug 2019 11:32:01 +0100 From: Will Deacon To: "Paul E. McKenney" Cc: Peter Zijlstra , Linus Torvalds , Mathieu Desnoyers , Thomas Gleixner , Joel Fernandes , Alan Stern , rostedt , Valentin Schneider , linux-kernel , Boqun Feng , Will Deacon , David Howells Subject: Re: [PATCH 1/1] Fix: trace sched switch start/stop racy updates Message-ID: <20190821103200.kpufwtviqhpbuv2n@willie-the-truck> References: <241506096.21688.1565977319832.JavaMail.zimbra@efficios.com> <1642847744.23403.1566005809759.JavaMail.zimbra@efficios.com> <20190820135612.GS2332@hirez.programming.kicks-ass.net> <20190820202932.GW28441@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190820202932.GW28441@linux.ibm.com> User-Agent: NeoMutt/20170113 (1.7.2) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 20, 2019 at 01:29:32PM -0700, Paul E. McKenney wrote: > On Tue, Aug 20, 2019 at 03:56:12PM +0200, Peter Zijlstra wrote: > > On Sat, Aug 17, 2019 at 01:08:02AM -0700, Linus Torvalds wrote: > > > > > 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. > > > > Paulmck actually has an example of that somewhere; ISTR that particular > > case actually got fixed by GCC, but I'd really _love_ for some compiler > > people (both GCC and LLVM) to state that their respective compilers will > > not do load/store tearing for machine word sized load/stores. > > I do very much recall such an example, but I am now unable to either > find it or reproduce it. :-/ > > If I cannot turn it up in a few days, I will ask the LWN editors to > make appropriate changes to the "Who is afraid" article. > > > Without this written guarantee (which supposedly was in older GCC > > manuals but has since gone missing), I'm loathe to rely on it. > > > > Yes, it is very rare, but it is a massive royal pain to debug if/when it > > does do happen. > > But from what I can see, Linus is OK with use of WRITE_ONCE() for data > races on any variable for which there is at least one READ_ONCE(). > So we can still use WRITE_ONCE() as we would like in our own code. > Yes, you or I might be hit by someone else's omission of WRITE_ONCE(), > it is better than the proverbial kick in the teeth. > > Of course, if anyone knows of a compiler/architecture combination that > really does tear stores of 32-bit constants, please do not keep it > a secret! After all, it would be good to get that addressed easily > starting now rather than after a difficult and painful series of > debugging sessions. It's not quite what you asked for, but if you look at the following silly code: typedef unsigned long long u64; struct data { u64 arr[1023]; u64 flag; }; void foo(struct data *x) { int i; for (i = 0; i < 1023; ++i) x->arr[i] = 0; x->flag = 0; } void bar(u64 *x) { *x = 0xabcdef10abcdef10; } Then arm64 clang (-O2) generates the following for foo: foo: // @foo stp x29, x30, [sp, #-16]! // 16-byte Folded Spill orr w2, wzr, #0x2000 mov w1, wzr mov x29, sp bl memset ldp x29, x30, [sp], #16 // 16-byte Folded Reload ret and so the store to 'flag' has become part of the memset, which could easily be bytewise in terms of atomicity (and this isn't unlikely given we have a DC ZVA instruction which only guaratees bytewise atomicity). GCC (also -O2) generates the following for bar: bar: mov w1, 61200 movk w1, 0xabcd, lsl 16 stp w1, w1, [x0] ret and so it is using a store-pair instruction to reduce the complexity in the immediate generation. Thus, the 64-bit store will only have 32-bit atomicity. In fact, this is scary because if I change bar to: void bar(u64 *x) { *(volatile u64 *)x = 0xabcdef10abcdef10; } then I get: bar: mov w1, 61200 movk w1, 0xabcd, lsl 16 str w1, [x0] str w1, [x0, 4] ret so I'm not sure that WRITE_ONCE would even help :/ It's worth noting that: void baz(atomic_long *x) { atomic_store_explicit(x, 0xabcdef10abcdef10, memory_order_relaxed) } does the right thing: baz: mov x1, 61200 movk x1, 0xabcd, lsl 16 movk x1, 0xef10, lsl 32 movk x1, 0xabcd, lsl 48 str x1, [x0] ret Whilst these examples may be contrived, I do thing they illustrate that we can't simply say "stores to aligned, word-sized pointers are atomic". Will