Received: by 10.213.65.68 with SMTP id h4csp1187115imn; Wed, 4 Apr 2018 14:17:39 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/JDzqYnOTavbJzH+vjOq5+7tqYk1UR5vSsym6X5J1F48/fIKwsxWFM9UfYtEkXCplhSw1z X-Received: by 10.98.201.70 with SMTP id k67mr14921786pfg.141.1522876659182; Wed, 04 Apr 2018 14:17:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522876659; cv=none; d=google.com; s=arc-20160816; b=rJAqWf7TkkvuTIiGnGcWA4m7HExCmn/VRHShPa02MjWy/ziU+zV29BznmozwOcLh3o sM4jVQgmcV3Hli8W1s9jy8Ebl8DTPpXb/mJHyOlC2Y8I6wTSHt8o4IE1c/+b4hWs4JVL wcf6yc4HS5VNkCu/F7KoEEqB7DOwVIyXJYXaZwfL1A+VuaOaJR31c86RVmq1OS6gf8+f 1+xQNPOquvmYFer5YlsyEzkdENoPBp7g/QVnVB/mg8wB7WCOMTRfX8Bqz6oDk/Yfp7GQ nQF+i1VITSwj4Nr90m0gsAJDdbCci8WcqhBR9lA0doLf0RMYgdhhPX11vMVXb3v8HZAb 24oQ== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:from:references:cc:to:subject :dkim-signature:arc-authentication-results; bh=kGQ9OWntKMXE7KgeqZFrAoOl/Xn0MpQsI2G/GBldegs=; b=CVvcwPMeVY9E8I9UOKguYBSy5wXJ4k/JlkUOiMnZeM7W1FNnILTEShVUh8QraoxHVC J9VPHw3evNfJ9wc7AjY0VfQvlY6D2yYSPEgB4hHejBJELF1VZZBGZdR6gICWCcmz+Tir 8DiW1DE+uzIc/h42k1GbjSVj5pMQllG9ygbJ48dNeqivSF7VnANWx9hue738UWelsUeL yboryEzRkUeyabg0CxLWtFqwJOXHwAiyoqG2wFyv17FSvEaokC4Wmqs9fyoKP6wO1Y3Y V66HypffmZWY1UVrVegN58WAXrtY21PB9+qzWM12LnjFezccIsTIVmoI/WcJY/M7t5qM /yng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2017-10-26 header.b=qrM0Sxli; 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=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b1si4325477pgn.572.2018.04.04.14.17.24; Wed, 04 Apr 2018 14:17:39 -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=@oracle.com header.s=corp-2017-10-26 header.b=qrM0Sxli; 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=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752141AbeDDVQT (ORCPT + 99 others); Wed, 4 Apr 2018 17:16:19 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:34502 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751749AbeDDVQS (ORCPT ); Wed, 4 Apr 2018 17:16:18 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w34L4aiM010813; Wed, 4 Apr 2018 21:15:14 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : to : cc : references : from : message-id : date : mime-version : in-reply-to : content-type : content-transfer-encoding; s=corp-2017-10-26; bh=kGQ9OWntKMXE7KgeqZFrAoOl/Xn0MpQsI2G/GBldegs=; b=qrM0SxliOxMoJyi19j5i6Pp6H9pskSyre0wCRpr/4rde9lKw+A/Dy+97qk9rnDk7FW3O RQCiPQ+sibN74ibmaQW2/XPO0nihrn8W9YQAYFwb3NEs4hg1KycCRg0kEnjw8X550A66 e3Xhh7G2JCtwxOzaucTmdpRrCr5fB3JZUe3NlN5XzfIhITLMUe/2TIandZesVFPe1YKl sHXSFDP9sXvjVYMYVWW/NWXtGbNrvrPc+0DyXlYM1qPGpDafQVq8dQsGmSs0OOZuj0tA tTRqjOhtAyKgBjNtz5bKcAgsTfXbKCOuAe4tkwkDJ7+96qs6ZUBs+U7ap1pnuikawjU6 6Q== Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by aserp2120.oracle.com with ESMTP id 2h56a5r1uk-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 04 Apr 2018 21:15:14 +0000 Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id w34LACY3001003 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 4 Apr 2018 21:10:12 GMT Received: from abhmp0002.oracle.com (abhmp0002.oracle.com [141.146.116.8]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w34LAAqp000747; Wed, 4 Apr 2018 21:10:10 GMT Received: from [10.152.33.180] (/10.152.33.180) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 04 Apr 2018 14:10:10 -0700 Subject: Re: Control dependency between prior load in while condition and later store? To: Alan Stern Cc: parri.andrea@gmail.com, will.deacon@arm.com, peterz@infradead.org, boqun.feng@gmail.com, npiggin@gmail.com, dhowells@redhat.com, j.alglave@ucl.ac.uk, luc.maranget@inria.fr, paulmck@linux.vnet.ibm.com, akiyks@gmail.com, linux-kernel@vger.kernel.org, Steven Sistare , Pasha Tatashin References: From: Daniel Jordan Organization: Oracle Message-ID: <4fa47ea8-e208-16d6-3b78-747049e3ee53@oracle.com> Date: Wed, 4 Apr 2018 17:10:05 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8853 signatures=668697 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=961 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1711220000 definitions=main-1804040204 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 04/04/2018 04:35 PM, 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. > >> I have my doubts because memory-barriers.txt doesn't talk much about >> loops and because of what that document says here: >> >> In addition, control dependencies apply only to the then-clause and >> else-clause of the if-statement in question. In particular, it does >> not necessarily apply to code following the if-statement: >> >> q = READ_ONCE(a); >> if (q) { >> WRITE_ONCE(b, 1); >> } else { >> WRITE_ONCE(b, 2); >> } >> WRITE_ONCE(c, 1); /* BUG: No ordering against the read from 'a'. */ > > This refers to situations where the two code paths meet up at the end > of the "if" statement. If they don't meet up (because one of the paths > branches away -- especially if it branches backward) then the > disclaimer doesn't apply, and everything following the "if" is > dependent. Ok, that's the part I wasn't getting: this is how the while loop changes the situation. > The reason is because the compiler knows that code following the "if" > statement will be executed unconditionally if the paths meet up, so it > can move that code back before the "if" (provided nothing else prevents > such motion). But if the paths don't meet up, the compiler can't > perform the code motion -- if it did then the program might end up > executing something that should not have been executed! > >> It's not obvious to me how the then-clause/else-clause idea maps onto >> loops, but if we think of the example at the top like this... >> >> while (1) { >> if (!READ_ONCE(a)) { >> WRITE_ONCE(c, 1); >> break; >> } >> } >> >> ...then the dependent store is within the then-clause. Viewed this way, >> it seems there would be a control dependency between a and c. >> >> Is that right? > > 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. That clears it up, thanks very much!