Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1650069pxb; Thu, 28 Oct 2021 07:37:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwAwQmz8fLPJvy9O3fp6O6Oxo4L+hpZULpmoYgB8nbdGyoJfX544xo8sKK38UDNdGmxbc5n X-Received: by 2002:a50:da48:: with SMTP id a8mr6647981edk.146.1635431828994; Thu, 28 Oct 2021 07:37:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635431828; cv=none; d=google.com; s=arc-20160816; b=HrNHVZ95c4APbjyGpOonEXOp1wnU2sQJgnabG9rne9H0cnx744eaT1uCTtqGs0Yo2J TWOx3sNfNslPikWLyKb8AJULE6EZZ3sFrW5Sio6onXUrcx+9/b0GHX14IisTsMjBgHeN FSMbRdVdygbEM7zGDNO8ztAEH+SQZFPnDyqJJHBmtnsvNX54AUu/uQJY3iXqTr2NHxzt //wXi21bE9v08djOc+BmT07qK0Btkt2ZePT1lneqLZN3aqBQ7f6f/lINzolJhfooj0BL laN7RU1NpdaJKznk+fnt/EVXOzB2p822prF4AvRdoDiODa/H/hVXFdLFvzF9iLJHJMLg nKcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=TfbwwWTtjharS/1UyeTLx+FqvLRSgXq4sUuTm5+fXos=; b=NZE8jV6O/YCE6L1goLmHpfrj94j4qnU/ADF6/OlzuPKKOpKAhhQzc6ls0mxa7Ji+xB 5wgh3qAwFjVs8A6IRcs+aYvakp+1yEU3AKjruGr1Ad6qIbmOltWCq8LfaCB5FDFM6yee AG+4ta//Stwd1Sxqi1FBg5ECwteCVT7CbAaTk6Izp80PhWNx+yue92a0dARMtJnD6CQh E8rizBLLLajC3W2Ny6rb+hbcj1nF7OBKLDm5gm7MgUDC+DsM4XbP5mo2+g8JquKOumcQ 6WeiyMR0lm2Ygx+Y91l7dlBKXuPLLbji1zwuHUBhBUXpr+O91kszOJMdUeIzl3ai5H1z 2QZg== 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 fj2si5431225ejc.474.2021.10.28.07.36.45; Thu, 28 Oct 2021 07:37:08 -0700 (PDT) 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 S230493AbhJ1OhP (ORCPT + 99 others); Thu, 28 Oct 2021 10:37:15 -0400 Received: from netrider.rowland.org ([192.131.102.5]:48583 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S230435AbhJ1OhO (ORCPT ); Thu, 28 Oct 2021 10:37:14 -0400 Received: (qmail 1352277 invoked by uid 1000); 28 Oct 2021 10:34:46 -0400 Date: Thu, 28 Oct 2021 10:34:46 -0400 From: Alan Stern To: Paul =?iso-8859-1?Q?Heidekr=FCger?= Cc: paulmck@kernel.org, will@kernel.org, peterz@infradead.org, boqun.feng@gmail.com, parri.andrea@gmail.com, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, elver@google.com, charalampos.mainas@gmail.com, pramod.bhatotia@in.tum.de Subject: Re: Potentially Broken Address Dependency via test_bit() When Compiling With Clang Message-ID: <20211028143446.GA1351384@rowland.harvard.edu> References: <20211027142720.GB1319606@rowland.harvard.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Oct 28, 2021 at 02:37:47PM +0200, Paul Heidekr?ger wrote: > On Wed, Oct 27, 2021 at 10:27:20AM -0400, Alan Stern wrote: > > On Wed, Oct 27, 2021 at 12:19:48PM +0200, Paul Heidekr?ger wrote: > > > Address dependency in source code, lines 373 - 375 in fs/afs/addr_list.c: > > > > > > > [...] > > > > index = READ_ONCE(ac->alist->preferred); > > > > if (test_bit(index, &set)) > > > > goto selected; > > > > [...] > > > > > > where test_bit() expands to the following in > > > include/asm-generic/bitops/non-atomic.h, lines 115 - 122: > > > > > > > static __always_inline int > > > > arch_test_bit(unsigned int nr, const volatile unsigned long *addr) > > > > { > > > > return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); > > > > } > > > > #define test_bit arch_test_bit > > However, I can't follow the IR code. Can you please explain in ordinary > > English how the LLVM compiler manages to lose track of this dependency? > > > > Alan Stern > > Here's what we think might be going on: > - In 'arch_test_bit()', 'addr[BIT_WORD(nr)]' expands to 'addr[(nr) / 64]'. > - Since 'addr' points to an 'unsigned long', any other result than '0' for > '(nr) / 64' would be out of bounds and therefore undefined. > - We assume LLVM is able to figure this out and use it to get rid of the > address computation all together. Ah, that explains it. Yes, when set is a single unsigned long (or an array of length 1), the address dependency is only syntactic, not semantic. As a result, we should expect that compilers will sometimes not preserve it. The danger, of course, is that people relying on an ordering prescribed by the LKMM may get fooled because (unbeknownst to them) the dependency in question is not semantic. It would be great if a static checker could detect such things -- but this would require some way for us to inform the checker about when the code does rely on a dependency ordering. > We ran some experiments to see how optimisations behave when 'set' is in fact > an array and / or in global scope. > > 1. Insert a 'barrier()' in 'arch_test_bit()' before the 'return': > The dependency gets broken. > > 2. Make 'set' an 'unsigned long' array of size '42', keep local scope: > The dependency gets preserved. > > 3. Keep 'set' as 'unsigend long', move to global scope: > The dependency gets preserved. That one's a little strange. I don't see why the scope should make any difference, so long as the compiler knows the actual type and length. > 4. Make 'set' an 'unsigned long' array of size '42', move to global scope: > The dependency gets preserved. Alan