Received: by 10.213.65.68 with SMTP id h4csp2016029imn; Thu, 5 Apr 2018 07:37:17 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+zBiMVzv4hR6uS8tzSjwMhyAYM+qPZFBm4B9cu0P8QhX3Kvfxbh0Cj8zMzvU/dW/UiP2iL X-Received: by 2002:a17:902:22a:: with SMTP id 39-v6mr23431079plc.128.1522939037870; Thu, 05 Apr 2018 07:37:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522939037; cv=none; d=google.com; s=arc-20160816; b=VZzYzSdoWEi9tp4mpaBXCBwq+fT9PlfRIhpEr6w1sPFSaC5qAklHD3IC2x7y1bLFHx wodxwNd+HUJtlugwxi2RGGfhDtZg5T1aT5cWpNWsBB/8qXTq+Phszqh5ZBc7VAMqRt1Q EzTf2G3pVurJEMAGzbV8vxJvgGHQZ99HuyG4wXDnZvJnQCLP1R5aD1lrfAscxM6ddWLy jyp6bnGC1ouVJyT34CGgj/mELYq0ZGAD9aQl/VnDkQoGEBArk4oM+Vv29bInYzWzRlqz CWthFb3Axt73lIN84etcwqn/2VH4M/3sejjWUFqP7ivS6EGM6wd6n4CO3L+FwSOy0wG7 /7xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:in-reply-to:subject:cc:to:from:date :arc-authentication-results; bh=8OYsWtj8aJBwdYvDR+W+x67QMMAuXEAdE7S1eU1tROw=; b=nGAJykE+Mpjxy63opdCU9QAdU2ozylpiuxZAs+dVlhoCZ/hgkazO4UmMRKmPR/OfpN CkDnCEP0BmsPyNq4uYySS2EUc4BxqLVNublPWOzkmbImHw6Q/FNuY/OwkaznE33ZPeed neO4xKJI73mxoXNxELXmYDUX2/zE1ERoWnUF0cNZxSmIhpfWiegCmMNi8yu1gmEfjCvC FtqvRBtubkE/qByBKnSSxQjiltVhxQZoVNKjDOOLTcUlrDoAs94Ukz4bRvfOFTBkKL8I FGrRAafVa+9AZ4aQU+tFtsa5aOTHOgHySdsZ0S0AJwp7LKLekHCZs4kuPdr66CeXZam1 KuVA== ARC-Authentication-Results: i=1; mx.google.com; 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 l75si6185926pfj.375.2018.04.05.07.37.03; Thu, 05 Apr 2018 07:37:17 -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; 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 S1751563AbeDEOfY (ORCPT + 99 others); Thu, 5 Apr 2018 10:35:24 -0400 Received: from iolanthe.rowland.org ([192.131.102.54]:47078 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1751195AbeDEOfX (ORCPT ); Thu, 5 Apr 2018 10:35:23 -0400 Received: (qmail 6136 invoked by uid 2102); 5 Apr 2018 10:35:22 -0400 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 5 Apr 2018 10:35:22 -0400 Date: Thu, 5 Apr 2018 10:35:22 -0400 (EDT) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: Peter Zijlstra cc: Daniel Jordan , , , , , , , , , , , Steven Sistare , Pasha Tatashin Subject: Re: Control dependency between prior load in while condition and later store? In-Reply-To: <20180405073204.GP4043@hirez.programming.kicks-ass.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=UTF-8 Content-Transfer-Encoding: 8BIT Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 5 Apr 2018, Peter Zijlstra wrote: > On Wed, Apr 04, 2018 at 04:35:32PM -0400, Alan Stern wrote: > > On Wed, 4 Apr 2018, Daniel Jordan wrote: > > > > > A question for memory-barriers.txt aficionados. > > > > > > Is there a control dependency between the prior load of 'a' and the > > > later store of 'c'?: > > > > > > while (READ_ONCE(a)); > > > WRITE_ONCE(c, 1); > > > > I would say that yes, there is. > > Indeed. > > > Yes, except that a more accurate view of the object code would be > > something like this: > > > > Loop: r1 = READ_ONCE(a); > > if (r1) > > goto Loop; > > else > > ; // Do nothing > > WRITE_ONCE(c, 1); > > > > Here you can see that one path branches backward, so everything > > following the "if" is dependent on the READ_ONCE. > > Agreed, and I think I even have code that relies on such a pattern > somewhere.. Ah.. yes, see smp_cond_load_acquire(). One does have to be very careful when talking about compiler behavior. This happens to be a particularly delicate point. My old copy of the C++11 draft standard says (section 1.10 paragraph 24): The implementation may assume that any thread will eventually do one of the following: ?- terminate, ?- make a call to a library I/O function, ?- access or modify a volatile object, or ?- perform a synchronization operation or an atomic operation. [ Note: This is intended to allow compiler transformations such as removal of empty loops, even when termination cannot be proven. - end note ] In this example, READ_ONCE() is in fact a volatile access, so we're okay. But if it weren't, the compiler might decide to assume the loop will eventually terminate, meaning that the WRITE_ONCE() would always be executed eventually. Then there would be nothing to prevent the compiler from moving the WRITE_ONCE() up before the start of the loop, which would of course destroy the control dependency. Alan