Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3890793iog; Tue, 21 Jun 2022 07:59:32 -0700 (PDT) X-Google-Smtp-Source: AGRyM1taN2A4mwEmL81IV1grlleZPqllc0VFJOQUs1i2Xjf2SW/nMWWINlLVsf3fIGtU7FWcRZyT X-Received: by 2002:a17:903:186:b0:167:62b0:a556 with SMTP id z6-20020a170903018600b0016762b0a556mr29427580plg.122.1655823572628; Tue, 21 Jun 2022 07:59:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655823572; cv=none; d=google.com; s=arc-20160816; b=Sy8orkoDNkIQ5lyRUR4VvoCtrriPmfp0C4YELVij6VIb5UbQAkC6hOdjMAmB5BuUOW AECXDnVH3GgMdA4HL6PbSwEiU9DQG0IgkTJfhW7HZDqGp0JmBzLxlbIkLnFL91MQ+qhe GG2pniI67Fpd4UWKD/Uc9/GOXBZkX25xMh/ljOoI2S9q0OlqkZLRi1wvUlG133MxEobt aiZp5lKWZ++LtBw7HmtEjVCtGlzDDLW5leTXSzZPGqbIo4hDgvD05X0PxIGM+SY9A64C yy4whWJNvZ7FCM9JYneVADupxFAMy6+z5OIYCsaZYW35yG9kr9KPgejGyX0zrlKrjEAb MOqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=OUdo2YEXD/p7ijTOXlInxiaivHwm5C2jW3M3e0Tgycc=; b=M4Md6y4DPau9n43D8kmbWjKft3tO7NRtuQ7TJwJkNFWWUExwFvZbEj104Lickaax6G +H3Tur5oo43pQQzuey+q+DpVHIF6kx+JQhNMTAWsT2WkMM8fwdPTC/YK6qJbm7m19sGB MYi7i/3sGk8sOUrq6fx8dBbeazg4hu7sGzMoJjG31g94BrEgwxfJGsTn0CxJSGHrimSB N3pP/7ErAiqaJmPp2CK4YFmRqJdSCnmsQ0H9Ovls6rN1aMQnPCAWcK9KNT3y+VNvSoS7 nw9LVFKCjasP9bYorOgMAQIlVN5GeobCPjDOlQjg26sH+JdIMiECqs3/w6Z99STaJtsQ ySDA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q3-20020a056a00084300b0051bfad76f55si22299246pfk.348.2022.06.21.07.59.19; Tue, 21 Jun 2022 07:59:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351422AbiFUOYU (ORCPT + 99 others); Tue, 21 Jun 2022 10:24:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349421AbiFUOYT (ORCPT ); Tue, 21 Jun 2022 10:24:19 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 3FB7D186E1 for ; Tue, 21 Jun 2022 07:24:18 -0700 (PDT) Received: (qmail 876587 invoked by uid 1000); 21 Jun 2022 10:24:17 -0400 Date: Tue, 21 Jun 2022 10:24:17 -0400 From: Alan Stern To: Paul =?iso-8859-1?Q?Heidekr=FCger?= Cc: llvm@lists.linux.dev, linux-toolchains@vger.kernel.org, Andrea Parri , Will Deacon , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Nathan Chancellor , Nick Desaulniers , Tom Rix , Palmer Dabbelt , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, Marco Elver , Charalampos Mainas , Pramod Bhatotia , Soham Chakraborty , Martin Fink Subject: Re: [PATCH RFC] tools/memory-model: Adjust ctrl dependency definition Message-ID: References: <20220615114330.2573952-1-paul.heidekrueger@in.tum.de> <50B9D7C1-B11D-4583-9814-BFFF2C80D8CA@in.tum.de> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <50B9D7C1-B11D-4583-9814-BFFF2C80D8CA@in.tum.de> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jun 21, 2022 at 01:59:27PM +0200, Paul Heidekr?ger wrote: > OK. So, LKMM limits the scope of control dependencies to its arm(s), hence > there is a control dependency from the last READ_ONCE() before the loop > exists to the WRITE_ONCE(). > > But then what about the following: > > > int *x, *y; > > > > int foo() > > { > > /* More code */ > > > > if(READ_ONCE(x)) > > return 42; > > > > /* More code */ > > > > WRITE_ONCE(y, 42); > > > > /* More code */ > > > > return 0; > > } > > The READ_ONCE() determines whether the WRITE_ONCE() will be executed at all, > but the WRITE_ONCE() doesn't lie in the if condition's arm. So in this case the LKMM would not recognize that there's a control dependency, even though it clearly exists. > However, by > "inverting" the if, we get the following equivalent code: > > > if(!READ_ONCE(x)) { > > /* More code */ > > > > WRITE_ONCE(y, 42); > > > > /* More code */ > > > > return 0; > > } > > > > return 42; > > Now, the WRITE_ONCE() is in the if's arm, and there is clearly a control > dependency. Correct. > Similar cases: > > > if(READ_ONCE()) > > foo(); /* WRITE_ONCE() in foo() */ > > return 42; > > or > > > if(READ_ONCE()) > > goto foo; /* WRITE_ONCE() after foo */ > > return 42; > > In both cases, the WRITE_ONCE() again isn't in the if's arm syntactically > speaking, but again, with rewriting, you can end up with a control > dependency; in the first case via inlining, in the second case by simply > copying the code after the "foo" marker. Again, correct. The LKMM isn't always consistent, and it behaves this way to try to avoid presuming too much about the optimizations that compilers may apply. Alan