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 4E65FCF6D2C for ; Wed, 2 Oct 2024 14:14:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBF7E6B00A4; Wed, 2 Oct 2024 10:14:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B6E9B6B00B4; Wed, 2 Oct 2024 10:14:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E8526B00B2; Wed, 2 Oct 2024 10:14:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 744CA6B0309 for ; Wed, 2 Oct 2024 10:14:42 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 06CE3A0726 for ; Wed, 2 Oct 2024 14:14:42 +0000 (UTC) X-FDA: 82628858004.28.344407F Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) by imf01.hostedemail.com (Postfix) with ESMTP id E95DE4000E for ; Wed, 2 Oct 2024 14:14:39 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=rowland.harvard.edu header.s=google header.b=PzuyzNO4; dmarc=pass (policy=none) header.from=rowland.harvard.edu; spf=pass (imf01.hostedemail.com: domain of stern@g.harvard.edu designates 209.85.222.176 as permitted sender) smtp.mailfrom=stern@g.harvard.edu ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727878440; a=rsa-sha256; cv=none; b=meuj6ZXktrZ1yX6vuEuVUbHx09JdP0Fziy7GdLBBSY8PPhDQMpMiYXbfKY8ievSbm/vSvc 0DjrVgc5otXOF53BlvMaRmvLv3gzeR6A+9N7rBwd0sXo0LIaHKYlq1bnqoXZAgISKNO1An jah0adraHKdtviswBVxczNMhv4cebec= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=rowland.harvard.edu header.s=google header.b=PzuyzNO4; dmarc=pass (policy=none) header.from=rowland.harvard.edu; spf=pass (imf01.hostedemail.com: domain of stern@g.harvard.edu designates 209.85.222.176 as permitted sender) smtp.mailfrom=stern@g.harvard.edu ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727878440; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gQ/povkWh1vgNmBfuGLgKT44hfzyi9J+3u5A1F7slgc=; b=Nf60HY7ebPRyXwV18XvbWRgxD6fP2pHHEalgE5j1JXb23heNHznPx8Bxg5euMt4VSzsp8D r9O0TElwZceUtWbgFX+nuKyIp0iACXkLK4pCOQyQfDl9XGPVab6mIU6+gjb1OrD8HHaAzG a4vfYznAgQdV5Wngl+WshL32F0oU+S4= Received: by mail-qk1-f176.google.com with SMTP id af79cd13be357-7a9aa913442so632207385a.1 for ; Wed, 02 Oct 2024 07:14:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rowland.harvard.edu; s=google; t=1727878479; x=1728483279; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=gQ/povkWh1vgNmBfuGLgKT44hfzyi9J+3u5A1F7slgc=; b=PzuyzNO4ewIohBb9BSyANV48wEnVY8buqwQNvcIUVNmMu2Ku7cRu9A8TvkiNxOaOOM LFfWqp1gcv3bbfn9BFm1tFObOWFWHGrxyx0CrFyVKi0CBOgLVQ1R46VHuMk/qTH/QGfN Qryz/Bf+8VXueWzc+UMA/7yU67GMQ2WwfarGSxwwf/BWM6F+rUi0OreG6tLGjmGRiZH+ Iku46Riq8IPl0jz93gKNnBkkUQ7vY0nCbTJRZC4dcFTcY7l1jk3L7uizSfaLtY5jHs6H EPqGq+JertcmcRy8B7DNW5KyaNQ/NJ53W6phm/N7yeLuVrYPPx2vmgj8wqldquuFE4Rq 5Rrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727878479; x=1728483279; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=gQ/povkWh1vgNmBfuGLgKT44hfzyi9J+3u5A1F7slgc=; b=A7DwAc3zK5XOOjhWPlK17mV0JKU5ycLOpJeb7l24D+1ihWeIq6x6Doo4a61vTXcf4M H4ukQXMD3tTAMe8PqiHRlJglIZopeAKlCF7DXUz2bkYndirAj/LQT50l9tUSMoXwBoXy 6hY+ERWXr4JgcqefmWRxV6fl18SjlM2e1kLMtB6QkBgz9q/idXIVklrxvNcRDW9znFI6 1gyMJ6AKpLiD+RNTnt0JLESDJuF6e7LP8kJq8+bu5tk8XrYPKY9jh2Lc/O+SjoFa5PP1 B5gBLkNTtDvMBWOvnfYUSoNyn1BluZZvUSbfyIRM1xJTmcGhV4ndQrDi3iRwglnv7vov tbYg== X-Forwarded-Encrypted: i=1; AJvYcCUBqIdxRzMsPlWhosr8KnqgbvXHPjNOYtFih8dRpw6ivXRClPSE59j7XWd9Yw+XTKHttfFvIWzjDg==@kvack.org X-Gm-Message-State: AOJu0YwZ/iLd8Jb74LmjOxcANrXa2vpVW+thFHZO2Zh8Tx/6pZ0dsCzT hM34/rWeiXiWny2lbakmfZxprGNd/EDpfXJ96dBwnscZoYm3lRaX6qTn7aLfrA== X-Google-Smtp-Source: AGHT+IG/o8TFevNqXUuDX3vJqZ8KPl51exlwA0PQIaotbs7GdJJp0vmE7UIcGFQNu+8WwPq2nynC8Q== X-Received: by 2002:a05:620a:bc6:b0:7a9:b4d2:9d70 with SMTP id af79cd13be357-7ae626bdc64mr557972185a.14.1727878478749; Wed, 02 Oct 2024 07:14:38 -0700 (PDT) Received: from rowland.harvard.edu ([2601:19b:681:fd10::1a9d]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7ae377f09bcsm616133885a.65.2024.10.02.07.14.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Oct 2024 07:14:38 -0700 (PDT) Date: Wed, 2 Oct 2024 10:14:33 -0400 From: 'Alan Stern' To: David Laight 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 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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <22638e2fe1274eb0834fa3e43b44184e@AcuMS.aculab.com> X-Rspam-User: X-Rspamd-Queue-Id: E95DE4000E X-Rspamd-Server: rspam01 X-Stat-Signature: jjznm418asysu11th43xeg58zj5dhaae X-HE-Tag: 1727878479-54478 X-HE-Meta: U2FsdGVkX19rrBJMbV81YeB5O/z1PCMhCLNKwJhRbJXjhi/bXKIP9RR5WwLpjVtc3zeVq4pQEF059VAw+sE157t646f7KKRwmlxVOlrCZUHUcU3g1IWYaFa5YQbZHzAgQ4C23lsF8w0le5acN25hA7Y5HrpcysEVNuXQCfwsjQxt7jt006K580rwm1VSBhNBGAWzMU5TBQDNnd3feHV3ZSiQ09cz+Mc8gdB1yRs0wDXmEitwRSKjKDkRrtgjAHpNp3oByPyqda3NdbVrZqd0mFpHwDDLbMh9oyGZo0M1CNn1vg/HUwBVJT6xZLaV64L5yecoupLS7MBtt7pEnPK9BxttwLfdut0oofT5FjU/3YO724sepTBKWwj88Oe5TxH+dyra+Xyy95V6FxKjXngCToB5AHham5Wi5KgSB393cDUE7La3JnZFedN/DWBydKXlpoE7Sufuq76Wqq5cUGST9SnpMpf3w42o4MnoEB925/whylzlJsK08ASjrzTMtaq+n8NjyPCo004VLcJ1LZVszmoNzURzrdKdir+YmL2uI8ktytmPhzJXj8+8ENLGr/y1SFt7CFxmgiWkvNaG7c9jivojgubCMV6rzEjXEryo9j3QeQ8/84o/JjBF0AJbYVLITGDLrss4kOvxcU7ysqnjFMZqw+YykdawRnGlQewNumxifDdX6Lv10xjc9nr2BlkG7Cu7OROzBRoh4huRvwKkBxxTfmAdI+bFVhyqJU5ij68t8xhqdX9N80KTRGI5Bvd3BkQe0Llc6Cf1/DDSiDNgXMqL4bYfSrbcAX81FjzNqqaA+gWpyHkjc/iY7pl7qKf8jD6LmcuuGMgs1ZBurLmc61jeOnfPr2i52B1WJHOVuCo0HqEaL7RHg9w6jQQXFDAhYgS3/J4POSm/DzddIugECGEAASTsFKtrmGxBCrX95HMHstHhpzvBNHba2nUkskJGSkfCzsOyTs4VEvx5R0E d4WczOlm ZnqmE0aPr4OI92lOpQ4kaCe6gx25j6Plr/ipccnfRGWqGVXUWHZa6UMb4GVEpIy60TdV/K/lwppnR5uaywAMfz7B0eix9gdOAa9BIpecmaNRi5VzBig19BBwiAGLyKiqjyHLZo381aSC2MoJ9RzckftZrrh9v5THaXYpsUdKrsTC00IN0Y6E9raxbSt2mMI7shVYZcf0S34+9A4U+GpFtt0Qx0t/ToiXPL8bVvKA8h91gM/JgmZW7b5KLbf7oz/rpvdSwPrS7g+diZokcCah2Fb0XnUTWSme+YhD7ikt0tsIvnNmWiDBkrU6zwpWrNo8U54ShiA/gadGBHFE2GvzIJ1B0EJwEFii3k4agerc/iYKilJ7v0z+hRu6Novq6RnTbyQnTKr98gSsl987cbcV72PP3IVt5vUbR1K7xkH4Nih5wl0sggAUlHtqgHd4/SUmdBuc86PLjuQIdl5whiW5WsueCtYJNUpE9HhqFrzQsy7WTilJxGdAeJMhK6VbwZtdAs1Nz5MlK6IV5Baxtgu0Pftck+SWbCcxRHCVIh5CXS4RM/C+TiAC11Di7r3GTnaFt8hzdycj5uBmw/r9f/CZaIv9giRVuT387ER75n+Q4lal9qM1JBBO0tuxDZlZyrX2SEWi+pJ0hIO7i56X6ZRpo3Aj6tgQpsRcnbIRbXQjSl79xQE6A8bIDYO0nwP6hM4Gy/HrHyFUEkJRXUf8= 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: 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 wrote: > > > > > > > > > > > > > > > > > > > > > Am 9/28/2024 um 4:49 PM schrieb Alan Stern: > > > > > > > > > > > > > > I should also point out that it is not enough to prevent the compiler from > > > > > > > using @a instead of @b. > > > > > > > > > > > > > > It must also be prevented from assigning @b=@a, which it is often allowed to > > > > > > > do after finding @a==@b. > > > > > > > > > > > > Wouldn't that be a bug? > > > > > > > > > > That's why I said that it is often allowed to do it. In your case 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 = READ_ONCE(p); > > > > > asm volatile ("" : : : "memory"); > > > > > b = READ_ONCE(p); > > > > > } while (a != 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 other 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 instead > > > > 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 value > > is stored in r3. "mov r3, r1" copies the value from r1 into r3 and is > > therefore equivalent to executing "b = a". (That is why I said one > > could argue that the "return *b" statement uses the value of *a.) Thus > > 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. 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. > 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 = f(x) where f() is > the identity operation: > asm ("" : "+r"(x)) > I'll bet that gcc allocates a separate internal/pseudo register > for the result so wants to do y = f(x). > Probably generating y = x; y = 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. 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. Alan