Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp859573pxb; Fri, 28 Jan 2022 11:38:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJwa0CH9zgZ2dHZiY4mXM9QiP5+3u9flQdDSa6VHmhwDiaQ4v+Xgk80hEAOGySerfL5H/z/r X-Received: by 2002:a17:907:a409:: with SMTP id sg9mr8031680ejc.219.1643398710414; Fri, 28 Jan 2022 11:38:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643398710; cv=none; d=google.com; s=arc-20160816; b=rt7KFk3H/XeHHRKjs73dBNZbHiiQqnpuGdQu6m84suQYJPmMqTXqv86Bb52cTPuTy6 h3ulai+SeQaTJx4nEpyNXLnhfMHL99WIgA3jPK5MA0LV07Hvm7i6qLEAWiF+d10oud1q 4KzeQ3uPmkKySzNYZ+NPtFYe3BdXTI3G6Xsmp0XkfRfULz1tRdqHAMVTAGojYSmiCLEK g4675KXw6PtmwqXlgSV7fzt3YNsdfCcO+nb18r68NvXm+DAqoBWUiIQQt3G/pXRF5/u5 v+kWilJxuCkAy8QRdq1UBTm+NZvaHFOp23ufNqFjXoLqsJuxQoTh3UdgQTLvRQGQMewO 1YuQ== 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=aUXPI+vQ5heE3YwpaNkdPf7nfYkqKxhPx+5PzESQ1xY=; b=YgVTX3eGlMD6arQeCDrXcuy+iq1jYoIuynECS7AtTEugukCS25811xeD41ZP5vU+Ia orIChBp6gQY0XTkcei0nCDXe+o1hW2M/MYXXAV/h8DYxvdmjc0aEjezpl+rx5vpZZiAj 9aVyV7tk+1RX26pxlBcTR9iOCW4J9/uXb/FXCicyQXB07rAPIXS1BcWGnb42tcFPyxRh BVxWKtsvAqPE7+x3cd1gdj19CKcnG+OaR2jyVWDkWXN2V1nLwP90nUCp2npPzjkMQEp+ ahva6XJ36e45/FeiE06WTpEURONuMxn0zi2QN/SiQwUUQAHmw/ghhg2WS8OUvv9VOiAa LZ+A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hp3si3909920ejc.847.2022.01.28.11.38.05; Fri, 28 Jan 2022 11:38:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344221AbiA0VLy (ORCPT + 99 others); Thu, 27 Jan 2022 16:11:54 -0500 Received: from netrider.rowland.org ([192.131.102.5]:36195 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1344178AbiA0VLu (ORCPT ); Thu, 27 Jan 2022 16:11:50 -0500 Received: (qmail 185924 invoked by uid 1000); 27 Jan 2022 16:11:48 -0500 Date: Thu, 27 Jan 2022 16:11:48 -0500 From: Alan Stern To: Paul =?iso-8859-1?Q?Heidekr=FCger?= Cc: 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 , =?iso-8859-1?Q?Bj=F6rn_T=F6pel?= , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, Marco Elver , Charalampos Mainas , Pramod Bhatotia Subject: [PATCH] tools/memory-model: Explain syntactic and semantic dependencies Message-ID: References: <20220125172819.3087760-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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Paul Heidekr?ger pointed out that the Linux Kernel Memory Model documentation doesn't mention the distinction between syntactic and semantic dependencies. This is an important difference, because the compiler can easily break dependencies that are only syntactic, not semantic. This patch adds a few paragraphs to the LKMM documentation explaining these issues and illustrating how they can matter. Suggested-by: Paul Heidekr?ger Signed-off-by: Alan Stern --- [as1970] tools/memory-model/Documentation/explanation.txt | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+) Index: usb-devel/tools/memory-model/Documentation/explanation.txt =================================================================== --- usb-devel.orig/tools/memory-model/Documentation/explanation.txt +++ usb-devel/tools/memory-model/Documentation/explanation.txt @@ -485,6 +485,53 @@ have R ->po X. It wouldn't make sense f somehow on a value that doesn't get loaded from shared memory until later in the code! +Here's a trick question: When is a dependency not a dependency? Answer: +When it is purely syntactic rather than semantic. We say a dependency +between two accesses is purely syntactic if the second access doesn't +actually depend on the result of the first. Here is a trivial example: + + r1 = READ_ONCE(x); + WRITE_ONCE(y, r1 * 0); + +There appears to be a data dependency from the load of x to the store of +y, since the value to be stored is computed from the value that was +loaded. But in fact, the value stored does not really depend on +anything since it will always be 0. Thus the data dependency is only +syntactic (it appears to exist in the code) but not semantic (the second +access will always be the same, regardless of the value of the first +access). Given code like this, a compiler could simply eliminate the +load from x, which would certainly destroy any dependency. + +(It's natural to object that no one in their right mind would write code +like the above. However, macro expansions can easily give rise to this +sort of thing, in ways that generally are not apparent to the +programmer.) + +Another mechanism that can give rise to purely syntactic dependencies is +related to the notion of "undefined behavior". Certain program behaviors +are called "undefined" in the C language specification, which means that +when they occur there are no guarantees at all about the outcome. +Consider the following example: + + int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +Access beyond the end or before the beginning of an array is one kind of +undefined behavior. Therefore the compiler doesn't have to worry about +what will happen if r1 is nonzero, and it can assume that r1 will always +be zero without actually loading anything from i. (If the assumption +turns out to be wrong, the resulting behavior will be undefined anyway +so the compiler doesn't care!) Thus the load from i can be eliminated, +breaking the address dependency. + +The LKMM is unaware that purely syntactic dependencies are different +from semantic dependencies and therefore mistakenly predicts that the +accesses in the two examples above will be ordered. This is another +example of how the compiler can undermine the memory model. Be warned. + THE READS-FROM RELATION: rf, rfi, and rfe -----------------------------------------