Received: by 10.213.65.68 with SMTP id h4csp1153787imn; Wed, 4 Apr 2018 13:37:03 -0700 (PDT) X-Google-Smtp-Source: AIpwx48sOval+giYsLtzotTliNvCVd7Dlv5OuPvmZWqljOyyr2WCHjyWRcpWU5KbgfpWOzf99JqB X-Received: by 10.98.13.151 with SMTP id 23mr15022530pfn.120.1522874223827; Wed, 04 Apr 2018 13:37:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522874223; cv=none; d=google.com; s=arc-20160816; b=ZB5QnIMGhfmyJMAChNI9wGJMNRXjC8dHiBKgJhjWIvYlLJUs/EfzdeG4s9G9loQPn/ f3W7DyC+iTMDAiaW2kQN7ybAFyVjq4IPb8tuSzacgD5hXpu588Y/it1MU9xCdKxhtylm PG+IpyXo/o5S/PcGeVN9bRVI/xXTUOGg4Xw4o+XQkXLDDTlZlrzvU3EdbbTaERkDmOOb rjSg7EM/7RrO4bypbTobINE5cT1ZdjP5BBg9G01Tk8P5eIIFixzdTnwtNCQmkO2ptV9v hLjtfzrbOr7EDkgJj0AgVf+Mh+kK0Orq2FOxduYSLnVjhGawkqPN2LP90oRHvBMPlQQ9 43fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:in-reply-to :subject:cc:to:from:date:arc-authentication-results; bh=+n1iMaKvHgENXOFrYQt/YWA0GHEcqfBBTQJguten4Ig=; b=wX5aqWQPEeOl4g79O9S8PFW6czdgIw6G3IU3GEvBlABFeQ22+Of2hhx5S23PUzao26 R4clWYXI/2CPtssNqWorg559b3h+eRrovyQyX1IoNvKWy3qOWK+mIeiJnidq44flQuEr 3zMjySENX17t8t0dO22uraoFbFRH2UeHUpbEFAOat7gXTjD4FwT222evNl997KyFD0oz ZRwCYbVpPAdWa20M2IvJI5VVRmg3lQVErsDxnhcNCWmNyUkl4WHRFSFpJ9mQXtuEVuC5 PWDnE/fG6g9y9/9/S+4lvvyoj6FbWMCexEqE/axDxvua/hdRpc2affljSEUrm0Bm70nP zJTg== 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 i3-v6si3866598pld.241.2018.04.04.13.36.50; Wed, 04 Apr 2018 13:37:03 -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 S1752248AbeDDUff (ORCPT + 99 others); Wed, 4 Apr 2018 16:35:35 -0400 Received: from iolanthe.rowland.org ([192.131.102.54]:57910 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1751654AbeDDUfe (ORCPT ); Wed, 4 Apr 2018 16:35:34 -0400 Received: (qmail 7720 invoked by uid 2102); 4 Apr 2018 16:35:32 -0400 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 4 Apr 2018 16:35:32 -0400 Date: Wed, 4 Apr 2018 16:35:32 -0400 (EDT) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: Daniel Jordan cc: parri.andrea@gmail.com, , , , , , , , , , , Steven Sistare , Pasha Tatashin Subject: Re: Control dependency between prior load in while condition and later store? In-Reply-To: <087a5ca4-e788-60ee-9145-3a078781cf05@oracle.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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. Alan