Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1586629iog; Tue, 14 Jun 2022 08:54:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwBBF8jer5XinY5jhCUIKr8fQGIGz7emTQ1VO5oA8PrYLbRZUUrnj7UGIoG9De1xkGCNqwG X-Received: by 2002:a05:6402:d05:b0:425:b7ab:776e with SMTP id eb5-20020a0564020d0500b00425b7ab776emr6768133edb.142.1655222092835; Tue, 14 Jun 2022 08:54:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655222092; cv=none; d=google.com; s=arc-20160816; b=rmk2i6/bvq2Mv4T/Q6Od3AviCZq3Ai6dcaS7IlS4wNKw6MfSYzcHFklXaIjf+DMxzQ Vv7KMBvwZ5836I+ZLdakrgpE0/9PdHsDlBI5WaioGNAweg2dJcVZrf7kZ0AL83akU4v2 YDBzmYsHFyw2iQpmtqd6zCSJG5a+cYXjIhw6f7ntnqck2LtyipNgx7U/DBy3rfRtxA/C 9ezp6a5jTsMHnbPWkukujU89u+GvuPpWBnKRQEjmRseCJjWN61W2TkB0VxZmdnf2WrMz jnh/hh6HdBGB2dI+m82UpeR2sdDYxFKaa6auoHhJ08iGIyKNeRFNqQeVdjc8TClrRqE0 sEQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1YhOTVDbcaz5aNnY1gjxQfdQ/am6Ukomt+EudTq2P2M=; b=SFyaQ6hvDj8RjEfRAfD/9t5bHFITm0ZoKp08cqQZ3D42zat8m+P7V5IbO/oDVVLqjd IO16j3VV8WHDpOiSuY5ZPlvcD0u+XKokJbQBsfSdrQ0OMaObb92kanGl9i/Rb8MJ2QO3 HIhNPJEe1x50Yn5RvobnVW6ZLsJbNHVqRceLboiAxpot7puRpZGoFTeAr+a9IbStUB1b BiZp8w3VA518X5/mVSq40uiYxQmv+IXX00PEGTPkUPpM5Mdy6opacoggM++9g83X9gcF BLWnFKM9o8/Hb5KmcT9BwGnb0853AGWBUkMFLZES+fIPJYQxWewzZgXc7zlBmKtP6uO/ tsaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@in.tum.de header.s=20220209 header.b=MnPhEdQq; 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=NONE sp=NONE dis=NONE) header.from=tum.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h7-20020a05640250c700b0042ac1cffe67si493034edb.413.2022.06.14.08.54.27; Tue, 14 Jun 2022 08:54:52 -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=@in.tum.de header.s=20220209 header.b=MnPhEdQq; 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=NONE sp=NONE dis=NONE) header.from=tum.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343944AbiFNPte (ORCPT + 99 others); Tue, 14 Jun 2022 11:49:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344263AbiFNPt1 (ORCPT ); Tue, 14 Jun 2022 11:49:27 -0400 Received: from mailout1.rbg.tum.de (mailout1.rbg.tum.de [131.159.0.201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0546A2EA14 for ; Tue, 14 Jun 2022 08:49:25 -0700 (PDT) Received: from mailrelay1.rbg.tum.de (mailrelay1.in.tum.de [IPv6:2a09:80c0:254::14]) by mailout1.rbg.tum.de (Postfix) with ESMTPS id 501D6B1B; Tue, 14 Jun 2022 17:49:14 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=in.tum.de; s=20220209; t=1655221754; bh=1YhOTVDbcaz5aNnY1gjxQfdQ/am6Ukomt+EudTq2P2M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MnPhEdQqyQ1dj/LdIOb1/ik+eBP2cXlELw5VAJ4ZScoIVFbkwpvTRn4lRYr9nd95z bpi6zH4cmJ97p4VIqa5mB0GIttqcjxsuTSiO+aIXn1VSnSIEXP7h3cyFw2XsZAcj+m lTguT6BeX1U1HrhmptlwHBWZFb294oWbroWDzjeHypXsZzZHDFzEZvJYXqWRF/Wktp Sls1uXGCT1+n6uKXHFwOYWNp41+59ziA9diqr+Xolumj0ybiuw47RUBp0qq9Tt7elg LoyBZwSwT5TZQi1GnEiKcTFWcgBrIJZ6i0c5w/Tuao76wl5F5LahW+IjfrJjw7MiQZ Hw02rTSxcCSDQ== Received: by mailrelay1.rbg.tum.de (Postfix, from userid 112) id 4C06C28B; Tue, 14 Jun 2022 17:49:14 +0200 (CEST) Received: from mailrelay1.rbg.tum.de (localhost [127.0.0.1]) by mailrelay1.rbg.tum.de (Postfix) with ESMTP id 1D39E28A; Tue, 14 Jun 2022 17:49:14 +0200 (CEST) Received: from mail.in.tum.de (mailproxy.in.tum.de [IPv6:2a09:80c0::78]) by mailrelay1.rbg.tum.de (Postfix) with ESMTPS id 17A50286; Tue, 14 Jun 2022 17:49:14 +0200 (CEST) Received: by mail.in.tum.de (Postfix, from userid 112) id 13CFB4A01EC; Tue, 14 Jun 2022 17:49:14 +0200 (CEST) Received: (Authenticated sender: heidekrp) by mail.in.tum.de (Postfix) with ESMTPSA id B21584A013B; Tue, 14 Jun 2022 17:49:13 +0200 (CEST) (Extended-Queue-bit xtech_hi@fff.in.tum.de) From: =?UTF-8?q?Paul=20Heidekr=C3=BCger?= To: 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 , =?UTF-8?q?Paul=20Heidekr=C3=BCger?= , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: Marco Elver , Charalampos Mainas , Pramod Bhatotia , Soham Chakraborty , Martin Fink Subject: [PATCH v2] tools/memory-model: Clarify LKMM's limitations in litmus-tests.txt Date: Tue, 14 Jun 2022 15:48:11 +0000 Message-Id: <20220614154812.1870099-1-paul.heidekrueger@in.tum.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 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 --- 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