Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp380407imw; Fri, 8 Jul 2022 04:49:50 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tJR+c1RwnU/msd16Z3Z+geTPNdJi7+HIJhI7BJjVKlFHIhOzYFMxLhQ/9KXd6dp1/L1DD7 X-Received: by 2002:a17:906:5d08:b0:6ff:8ed:db63 with SMTP id g8-20020a1709065d0800b006ff08eddb63mr3252755ejt.408.1657280989776; Fri, 08 Jul 2022 04:49:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657280989; cv=none; d=google.com; s=arc-20160816; b=hYrsjtoCFO2hA2SpG41Yntv0r+WhPoxHxiwuUKau4I4bhiBPwz4n2rZr0Ed2W7/Zq4 hvaSRAs6+IYVicDmN5lPqWYH/w/xy+qdEkNOzsbTfp6Trre0OOX3wnXNo6Tx/gHg220R S0gNO/Qd1g8vfgFKph6g18bJdek+dqD1HIrmgJlA9mFbE6HtROXxCQdZnL4Pr9SFPMbf MAnQQonfKnV73wfaJaQoRVMJBRZEAhtYWKJjPKxLk1ry4HV0vve1uC06pnXvJ84TBjQc j4S5odctTCEi6N9odZ4Z9cC3tp+icYe05ESbQZ4JGdo0iF3mYzkadoaAvkP5Maf5/sSL wwTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=tm11KlnnIXGr7nvHs0RfeJdMMwEgYtIpJrEKcFo1S38=; b=iL5JBjl0j5VdWVqCtYTZXZlxIZ1518Yul5LDloZstvTSFrOzKaeF6pbRKWZZuH4CO3 +hzcXuNG4/KKgpPf6eGZgHjGIhZ6NMjea48zar5HByAM5ohuYSfYJ3ngP4SHzDNTMgq9 hxPd8txqdrdbBekDv8lXkavp1++mMD0qDLaUZdBjWiW7OTzFAp2ie+cEkePQAVrLQ/DV HZ2laMJJbkdLoQv95PowrEQ8Nm0wAn/GseaSlsvlM1t1GCACA36RXcfXJw1HhhnGLB/1 PUsEQkVWaYawpGPXXa3QAb2u9BZBcndseGs9M4d+5/jLW6K09mmAI9osPMCBykrJ1aKV zlTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=rWcYb2tP; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i7-20020a05640242c700b0043aad2d4f73si1930527edc.444.2022.07.08.04.49.22; Fri, 08 Jul 2022 04:49:49 -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=@google.com header.s=20210112 header.b=rWcYb2tP; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237474AbiGHLoq (ORCPT + 99 others); Fri, 8 Jul 2022 07:44:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237559AbiGHLoo (ORCPT ); Fri, 8 Jul 2022 07:44:44 -0400 Received: from mail-yw1-x1135.google.com (mail-yw1-x1135.google.com [IPv6:2607:f8b0:4864:20::1135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6E3D77D1F3 for ; Fri, 8 Jul 2022 04:44:43 -0700 (PDT) Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-3137316bb69so195603897b3.10 for ; Fri, 08 Jul 2022 04:44:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=tm11KlnnIXGr7nvHs0RfeJdMMwEgYtIpJrEKcFo1S38=; b=rWcYb2tPMqjko19vnw97Xy75z7EjAz6RBmh89eGBLfmWyDs5ModNXPj7JLs08ESwti +P9qMDacx9NPWCzo1qvPCcqALe3Tv8TG17MwtGf6CntF5sNU/oO9fVYBahbKsEZFpdn/ 3AS1PfZBvgy7ZBGgTy1/yqKSA7oyPxZwxPOU0tGQjIuvhCtoHFl4LtEQ2iNATROVVHTr zc716yBryWj2deyUOFPTs3AZ8BY/DxP0SJNsbaLEMVmLxMPYHtWYvmJzahK+txzIFcdm ig7msRn8p3hWMA71b1/Vy6rhwS8zYwUWpf2L0NWvBveIFCAfaKzCilTFZ62+sD76TbOw MfSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=tm11KlnnIXGr7nvHs0RfeJdMMwEgYtIpJrEKcFo1S38=; b=IwYzbvH9Jstj+pFCOdCDqTOuIlFS9D8ebjZA70SdsQbDgYA2LPJjQ32cf3qgaY2jEZ RMpTm/yJ5n6198owJVP18oEb2HqXOqYNofk8idISgNXQQLFzNQbomgTBPuqFYLHfR+BT 0YtUzirRNCOvkAhlRTQ8Jljze1JKvc4lH7p17D/0q1xZ4hu5MWxA8HkbClZUZm6WJxQa 4oLPuEGsn08v//4Hu4NDMDwWusKRXxhucYHoOkIUoLUISSWH0ogW6Fg2DK1bgthOMA+o URtqaVjnUt3CfGgR9Dx8X9htY/anlyEUHsUU3CSrpmCfhrmkdopmBmxGf11pizvjvgCg uOlw== X-Gm-Message-State: AJIora9Vr+M5EEvZ8o+FvFTeMc9mKfQjprDZna3FZ5esa/a+UJWBpf7U KTPrQAyC+VqyCkIE3vpjNUYigKYDeWJ77f4txE0/ZA== X-Received: by 2002:a0d:cf07:0:b0:31d:17cb:ec11 with SMTP id r7-20020a0dcf07000000b0031d17cbec11mr3579407ywd.264.1657280682042; Fri, 08 Jul 2022 04:44:42 -0700 (PDT) MIME-Version: 1.0 References: <20220614154812.1870099-1-paul.heidekrueger@in.tum.de> In-Reply-To: <20220614154812.1870099-1-paul.heidekrueger@in.tum.de> From: Marco Elver Date: Fri, 8 Jul 2022 13:44:06 +0200 Message-ID: Subject: Re: [PATCH v2] tools/memory-model: Clarify LKMM's limitations in litmus-tests.txt To: =?UTF-8?Q?Paul_Heidekr=C3=BCger?= 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 , Joel Fernandes , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, Charalampos Mainas , Pramod Bhatotia , Soham Chakraborty , Martin Fink Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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, 14 Jun 2022 at 17:49, Paul Heidekr=C3=BCger 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.tu= m.de/T/#u > Signed-off-by: Paul Heidekr=C3=BCger > Co-developed-by: Alan Stern Reviewed-by: Marco Elver However with the Co-developed-by, this is missing Alan's SOB. > Cc: Marco Elver > Cc: Charalampos Mainas > Cc: Pramod Bhatotia > Cc: Soham Chakraborty > Cc: Martin Fink > --- > > v2: > - Incorporate Alan Stern's feedback. > - Add suggested text by Alan Stern to clearly state how the branch and th= e > 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/me= mory-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 dependenc= y > 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 i= t). > - 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 =3D READ_ONCE(x); > if (r1 =3D=3D 0) > smp_mb(); > WRITE_ONCE(y, 1); > > - There is a control dependency from the READ_ONCE to the WRITE_ONC= E, > - even when r1 is nonzero, but LKMM doesn't realize this and thinks > - that the write may execute before the read if r1 !=3D 0. (Yes, t= hat > - 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 befo= re > + 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 undefine= d > + 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 >