From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31E0CCF6D39 for ; Wed, 2 Oct 2024 15:25:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC29A4401B4; Wed, 2 Oct 2024 11:25:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A74004401B3; Wed, 2 Oct 2024 11:25:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8034C4401B4; Wed, 2 Oct 2024 11:25:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C612F4401AF for ; Wed, 2 Oct 2024 11:25:42 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 640461C6582 for ; Wed, 2 Oct 2024 15:25:42 +0000 (UTC) X-FDA: 82629036924.12.1221F7C Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by imf14.hostedemail.com (Postfix) with ESMTP id 6076A100010 for ; Wed, 2 Oct 2024 15:25:38 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727882700; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N3UKLPWHlh2RX5LaWCCM2KYQX80CSVNv7YMw7hKc72U=; b=J9Ac2m/j90MotRVoEEMIc7r0xWVf6WUsLdVTm4dw7t0QZfq7ZV/gAENwY8XXxk5VTGkIAj aowSKqP6+kx1+qR7fJQMs7UP1DBCc/GKLwbGp/SFncIxEZVvIDUq8Lfs0mxa18rZiIr55T /nfCwqoAdv5l58IOSg6YaLpXb8agJnw= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; spf=pass (imf14.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727882700; a=rsa-sha256; cv=none; b=yX5VAMVb5hY8AGlbtn7EqILoXsEMq3dcco12hohRSjK6gpkhPWn5h8cU+YQYzXYOc32ge5 cUHzRHhRJzi6vhCHKtJLs5cEzXndaNKx3U6t29UexGkc8xJ9uWe+JEs4CACW4SI+YogngD l5FETZarrnfOF1uNweO9F0AXb7Tbz/Q= Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-319-0qh1U1F9OE-o__CYQVvx_A-1; Wed, 02 Oct 2024 16:25:35 +0100 X-MC-Unique: 0qh1U1F9OE-o__CYQVvx_A-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 2 Oct 2024 16:24:46 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Wed, 2 Oct 2024 16:24:46 +0100 From: David Laight To: 'Alan Stern' CC: Jonas Oberhauser , Mathieu Desnoyers , Linus Torvalds , "linux-kernel@vger.kernel.org" , Greg Kroah-Hartman , Sebastian Andrzej Siewior , "Paul E. McKenney" , Will Deacon , Peter Zijlstra , Boqun Feng , John Stultz , Neeraj Upadhyay , Frederic Weisbecker , "Joel Fernandes" , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Lai Jiangshan , Zqiang , Ingo Molnar , Waiman Long , "Mark Rutland" , Thomas Gleixner , Vlastimil Babka , "maged.michael@gmail.com" , Mateusz Guzik , Gary Guo , "rcu@vger.kernel.org" , "linux-mm@kvack.org" , "lkmm@lists.linux.dev" Subject: RE: [PATCH 1/2] compiler.h: Introduce ptr_eq() to preserve address dependency Thread-Topic: [PATCH 1/2] compiler.h: Introduce ptr_eq() to preserve address dependency Thread-Index: AQHbE2njI9qt6nx11kSrjhsE5O+AlrJyIkGggABQqYCAAKZugIAAWd+AgAAhswA= Date: Wed, 2 Oct 2024 15:24:45 +0000 Message-ID: References: <20240928135128.991110-1-mathieu.desnoyers@efficios.com> <20240928135128.991110-2-mathieu.desnoyers@efficios.com> <02c63e79-ec8c-4d6a-9fcf-75f0e67ea242@rowland.harvard.edu> <9539c551-5c91-42db-8ac1-cff1d6d7c293@huaweicloud.com> <2cdda043-1ad9-40cf-a157-0c16a0ffb046@rowland.harvard.edu> <5d7d8a59-57f5-4125-95bb-fda9c193b9cf@huaweicloud.com> <82e97ad5-17ad-418d-8791-22297acc7af4@rowland.harvard.edu> <2b1caba3-48fa-43b9-bd44-cf60b9a141d7@rowland.harvard.edu> <22638e2fe1274eb0834fa3e43b44184e@AcuMS.aculab.com> In-Reply-To: Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Stat-Signature: wjycxsh64mynozqffzojwoau3wa9bhw5 X-Rspamd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WsNqK4nBwqVFFUDJAt7dC7Jl9BVIvUfFnDA3Cww6j7YVBNsCAPCt/rOXPvzcipj5wUzEhCMS87/1bw8Niydr3Luc7MB8QBQUl0G3d6cmhL3647uoEUgKaBm2XG3oVzY7fAB+wMNfYc9D/I9zsuyTrtxfWIcFXe6F568QTZNcgzXOtoJt59vVv06Ha5jhEY03AKxDkmw7Jd8WDeuo6elxc9tEUjamdOWud2qHaXBh2m2lEFipamwNNxCc06g5zd+1yrTZOCGHLo+Uadu61mtVUGgW8w+Zef1FZE7+0fOVxmP5DCqvhIZTMAPYEsxRGtcC5mOwmJ4GMCEK+0EioHr0rU20O/Rk4Rh3B595ZWTa6+3TDSiCa7QVq9pUxqB1cmd/GlXewbbA2LEH5FV8ZvGdVyA0aYOrCrMA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: 'Alan Stern' > Sent: 02 October 2024 15:15 >=20 > On Wed, Oct 02, 2024 at 08:13:15AM +0000, David Laight wrote: > > From: 'Alan Stern' > > > Sent: 01 October 2024 23:57 > > > > > > On Tue, Oct 01, 2024 at 05:11:05PM +0000, David Laight wrote: > > > > From: Alan Stern > > > > > Sent: 30 September 2024 19:53 > > > > > > > > > > On Mon, Sep 30, 2024 at 07:05:06PM +0200, Jonas Oberhauser wrote: > > > > > > > > > > > > > > > > > > Am 9/30/2024 um 6:43 PM schrieb Alan Stern: > > > > > > > On Mon, Sep 30, 2024 at 01:26:53PM +0200, Jonas Oberhauser wr= ote: > > > > > > > > > > > > > > > > > > > > > > > > Am 9/28/2024 um 4:49 PM schrieb Alan Stern: > > > > > > > > > > > > > > > > I should also point out that it is not enough to prevent th= e compiler from > > > > > > > > using @a instead of @b. > > > > > > > > > > > > > > > > It must also be prevented from assigning @b=3D@a, which it = is often allowed to > > > > > > > > do after finding @a=3D=3D@b. > > > > > > > > > > > > > > Wouldn't that be a bug? > > > > > > > > > > > > That's why I said that it is often allowed to do it. In your ca= se it > > > > > > wouldn't, but it is often possible when a and b are non-atomic = & > > > > > > non-volatile (and haven't escaped, and I believe sometimes even= then). > > > > > > > > > > > > It happens for example here with GCC 14.1.0 -O3: > > > > > > > > > > > > int fct_hide(void) > > > > > > { > > > > > > int *a, *b; > > > > > > > > > > > > do { > > > > > > a =3D READ_ONCE(p); > > > > > > asm volatile ("" : : : "memory"); > > > > > > b =3D READ_ONCE(p); > > > > > > } while (a !=3D b); > > > > > > OPTIMIZER_HIDE_VAR(b); > > > > > > return *b; > > > > > > } > > > > > > > > > > > > > > > > > > > > > > > > ldr r1, [r2] > > > > > > ldr r3, [r2] > > > > > > cmp r1, r3 > > > > > > bne .L6 > > > > > > mov r3, r1 // nay... > > > > > > > > > > A totally unnecessary instruction, which accomplishes nothing oth= er than > > > > > to waste time, space, and energy. But nonetheless, allowed -- I = agree. > > > > > > > > > > The people in charge of GCC's optimizer might like to hear about = this, > > > > > if they're not already aware of it... > > > > > > > > > > > ldr r0, [r3] // yay! > > > > > > bx lr > > > > > > > > > > One could argue that in this example the compiler _has_ used *a i= nstead > > > > > of *b. However, such an argument would have more force if we had > > > > > described what we are talking about more precisely. > > > > > > > > The 'mov r3, r1' has nothing to do with 'a'. > > > > > > What do you mean by that? At this point in the program, a is the > > > variable whose value is stored in r1 and b is the variable whose valu= e > > > is stored in r3. "mov r3, r1" copies the value from r1 into r3 and i= s > > > therefore equivalent to executing "b =3D a". (That is why I said one > > > could argue that the "return *b" statement uses the value of *a.) Th= us > > > it very much does have something to do with "a". > > > > After the cmp and bne r1 and r3 have the same value. > > The compiler tracks that and will use either register later. > > That can never matter. >=20 > The whole point of this thread is that sometimes it _does_ matter. Not > on x86, but on weakly ordered architectures where using the wrong > register will bypass a dependency and allow the CPU to speculatively > load values earlier than the programmer wants it to. >=20 > > Remember the compiler tracks values (in pseudo/internal registers) > > not variables. > > > > > > It is a more general problem that OPTIMISER_HIDE_VAR() pretty much > > > > always ends up allocating a different internal 'register' for the > > > > output and then allocating a separate physical rehgister. > > > > > > What output are you referring to? Does OPTIMISER_HIDE_VAR() have an > > > output? If it does, the source program above ignores it, discarding = any > > > returned value. > > > > Look up OPTIMISER_HIDE_VAR(x) it basically x =3D f(x) where f() is > > the identity operation: > > =09asm ("" : "+r"(x)) > > I'll bet that gcc allocates a separate internal/pseudo register > > for the result so wants to do y =3D f(x). > > Probably generating y =3D x; y =3D f(y); > > (The 'mov' might be after the asm, but I think that would get > > optimised away - the listing file might help.) > > > > So here the compiler has just decided to reuse the register that > > held the other of a/b for the extra temporary. >=20 > I think you've got this backward. As mentioned above, a is originally > in r1 and b is in r3. The source says OPTIMIZER_HIDE_VAR(b), so you're > saying that gcc should be copying r3 into a separate internal/pseudo > register. But instead it's copying r1. I think I know what you are trying to do, and you just fail. Whether something can work is another matter, but that code can't ever work. Inside if (a =3D=3D b) the compiler will always use the same register for references to a and b - because it knows they have the same value. Possibly something like: =09c =3D b; =09OPTIMISER_HIDE_VAR(c); =09if (a =3D=3D c) { =09=09*b will ensure that there isn't a speculative load from *a. You'll get at least one register-register move - but they are safe. Otherwise you'll need to put the condition inside an asm block. =09David >=20 > Alan - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)