Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp436604imw; Fri, 8 Jul 2022 05:47:09 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sHdcnUknydBcSF/tnoY8bpyipBUgv2mnp9cFDPERP7N8LK2Zi0tUGTZy96i8cgGN+kOCOd X-Received: by 2002:a17:903:404b:b0:16c:28fd:54a with SMTP id n11-20020a170903404b00b0016c28fd054amr1105209pla.133.1657284429086; Fri, 08 Jul 2022 05:47:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657284429; cv=none; d=google.com; s=arc-20160816; b=qMpIwx2sZLhl5eaWyOMlrbrmRmBYLVYlcO19wCN5G1eed3tcS2tGYZgXqv4YOewy3w 1rxLwGCf+bRw0+C/VKUay924FG/znT3g7M6NKLHntZrqWJW4jokr4fgg5KqLIefU5CHn DFKXUudZ6n8qMpg5cuJj1FTT1pg330WejjqP4f2iEsx38U+BDu+4GHk9YIZm5fgC3QFi PO75w/zvDyj5bA41tqEUB4vvdZRWMNVfzhMZIDRBxMjM/VUretLKkdBcHwAXqplECm3K 4SneUvJBwmtyOjU2sC+NY0L8DcqTChlgcPeJlF0LM2zeOT91/AhTTR+WtfxK0Q48/YTH YUFw== 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:dkim-signature; bh=iNABZ+iRiUdeEgm98v4CN7DQd/touY+ubNiQ+SHp/1I=; b=0yGc8N84nl+X0xEAhiAOCq2VJYA/UDPLsHebZ1uSKMBOUCN9UmiT84cZp0V10avCsw 8zauqa7oIJQ9wkT1SHjglC4uR+PwosF4KkMbYyseDbAv+MGYY7RIKepYCtIVBBYOYS0X le685JXQ3sUf2jnmhV7k3vW14r5dV6+GtsQa3BOvYlJzltSoLpzm7kjXnQBv9CSfOa45 lh0xRypBE+RIZAFkgwkRHVt2qVpJG2NjFvjqEEEzfoyAMd3y0zyLo6gmLCSwX4dR5dKP Os0iWqVeogTiSCJLF2Izapdvbo+hKvNVdMVbgXcqLjC7E+6s/KA0erkBjRw99fzuzgcj i5yQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joelfernandes.org header.s=google header.b=F18GnCBI; 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 u18-20020a170903125200b00163fc5e39a0si5192002plh.227.2022.07.08.05.46.56; Fri, 08 Jul 2022 05:47:09 -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; dkim=pass header.i=@joelfernandes.org header.s=google header.b=F18GnCBI; 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 S237952AbiGHMOx (ORCPT + 99 others); Fri, 8 Jul 2022 08:14:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42442 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231287AbiGHMOu (ORCPT ); Fri, 8 Jul 2022 08:14:50 -0400 Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DF4FA958E for ; Fri, 8 Jul 2022 05:14:49 -0700 (PDT) Received: by mail-qt1-x836.google.com with SMTP id i11so26905128qtr.4 for ; Fri, 08 Jul 2022 05:14:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joelfernandes.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=iNABZ+iRiUdeEgm98v4CN7DQd/touY+ubNiQ+SHp/1I=; b=F18GnCBIiGap9h2kUdgSNdeFQWCvNTEy4Q7oyKH5zxsjLSDT/H/JIRauf/QdXv+DCg d3TWT4BZTEuz4/xnlh6Bic9EJdXSa5JUUnUZ445uLaXo27DLEyl47xZOy50Z48ZIp42f aibfHJ2/gcmd06dR3uz/b8L30DUJhvi/jRw/w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=iNABZ+iRiUdeEgm98v4CN7DQd/touY+ubNiQ+SHp/1I=; b=rR8sfvv+3UmaIaWQ6Xnm0qP3bRi9lUa7Y1m53xCAhIJyhfTymCQla7X/6SO+bxgsoS T0zY4mEdUezyWS055hOK71St+oqPQ42J13AMofOfRcQzQFyCizEDfihspGXWyFjUP3Vb p8a1xVFyHR2y5C0ap1s449ksMyUiMSkqzt3ihQ5XVWRHGHS/Wzbr/3iRr+Ma2BHug4pe UoFZS9J4MxBqd49lwVsZXHdEh57K1nh6pxgqOht1gczptrTsNDEUWDWgF/73Qn0TF92K i3jZHSadmlPq+EcGAf5vbFJER2gEKwO6gmreZ2U72veoskhUl5YB6MK7PicRElwPO59O BWdg== X-Gm-Message-State: AJIora8Lyx2h2dVUmLoVwQ/Xd6h6+xZf+4SQkEfeS3s4YbIjrYmp179F as59DeUT8KZNinFYLfA5bZII0Q== X-Received: by 2002:ac8:5f51:0:b0:31d:2909:bf56 with SMTP id y17-20020ac85f51000000b0031d2909bf56mr2632574qta.73.1657282487558; Fri, 08 Jul 2022 05:14:47 -0700 (PDT) Received: from localhost (228.221.150.34.bc.googleusercontent.com. [34.150.221.228]) by smtp.gmail.com with ESMTPSA id bq30-20020a05620a469e00b006a785ba0c25sm25214448qkb.77.2022.07.08.05.14.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Jul 2022 05:14:47 -0700 (PDT) Date: Fri, 8 Jul 2022 12:14:46 +0000 From: Joel Fernandes To: Paul =?iso-8859-1?Q?Heidekr=FCger?= Cc: Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, Marco Elver , Charalampos Mainas , Pramod Bhatotia , Soham Chakraborty , Martin Fink Subject: Re: [PATCH v2] tools/memory-model: Clarify LKMM's limitations in litmus-tests.txt Message-ID: References: <20220614154812.1870099-1-paul.heidekrueger@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: <20220614154812.1870099-1-paul.heidekrueger@in.tum.de> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 14, 2022 at 03:48:11PM +0000, Paul Heidekr?ger wrote: > As discussed, clarify LKMM not recognizing certain kinds of orderings. > In particular, highlight the fact that LKMM might deliberately make > weaker guarantees than compilers and architectures. > > Link: https://lore.kernel.org/all/YpoW1deb%2FQeeszO1@ethstick13.dse.in.tum.de/T/#u > Signed-off-by: Paul Heidekr?ger > Co-developed-by: Alan Stern > Cc: Marco Elver > Cc: Charalampos Mainas > Cc: Pramod Bhatotia > Cc: Soham Chakraborty > Cc: Martin Fink > --- Reviewed-by: Joel Fernandes (Google) thanks, - Joel > v2: > - Incorporate Alan Stern's feedback. > - Add suggested text by Alan Stern to clearly state how the branch and the > smp_mb() affect ordering. > - Add "Co-developed-by: Alan Stern " based on the > above. > > .../Documentation/litmus-tests.txt | 37 ++++++++++++++----- > 1 file changed, 27 insertions(+), 10 deletions(-) > > diff --git a/tools/memory-model/Documentation/litmus-tests.txt b/tools/memory-model/Documentation/litmus-tests.txt > index 8a9d5d2787f9..cc355999815c 100644 > --- a/tools/memory-model/Documentation/litmus-tests.txt > +++ b/tools/memory-model/Documentation/litmus-tests.txt > @@ -946,22 +946,39 @@ Limitations of the Linux-kernel memory model (LKMM) include: > carrying a dependency, then the compiler can break that dependency > by substituting a constant of that value. > > - Conversely, LKMM sometimes doesn't recognize that a particular > - optimization is not allowed, and as a result, thinks that a > - dependency is not present (because the optimization would break it). > - The memory model misses some pretty obvious control dependencies > - because of this limitation. A simple example is: > + Conversely, LKMM will sometimes overestimate the amount of > + reordering compilers and CPUs can carry out, leading it to miss > + some pretty obvious cases of ordering. A simple example is: > > r1 = READ_ONCE(x); > if (r1 == 0) > smp_mb(); > WRITE_ONCE(y, 1); > > - There is a control dependency from the READ_ONCE to the WRITE_ONCE, > - even when r1 is nonzero, but LKMM doesn't realize this and thinks > - that the write may execute before the read if r1 != 0. (Yes, that > - doesn't make sense if you think about it, but the memory model's > - intelligence is limited.) > + The WRITE_ONCE() does not depend on the READ_ONCE(), and as a > + result, LKMM does not claim ordering. However, even though no > + dependency is present, the WRITE_ONCE() will not be executed before > + the READ_ONCE(). There are two reasons for this: > + > + The presence of the smp_mb() in one of the branches > + prevents the compiler from moving the WRITE_ONCE() > + up before the "if" statement, since the compiler has > + to assume that r1 will sometimes be 0 (but see the > + comment below); > + > + CPUs do not execute stores before po-earlier conditional > + branches, even in cases where the store occurs after the > + two arms of the branch have recombined. > + > + It is clear that it is not dangerous in the slightest for LKMM to > + make weaker guarantees than architectures. In fact, it is > + desirable, as it gives compilers room for making optimizations. > + For instance, suppose that a 0 value in r1 would trigger undefined > + behavior elsewhere. Then a clever compiler might deduce that r1 > + can never be 0 in the if condition. As a result, said clever > + compiler might deem it safe to optimize away the smp_mb(), > + eliminating the branch and any ordering an architecture would > + guarantee otherwise. > > 2. Multiple access sizes for a single variable are not supported, > and neither are misaligned or partially overlapping accesses. > -- > 2.35.1 >