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 C46FACF6491 for ; Sat, 28 Sep 2024 23:55:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 17FD76B0092; Sat, 28 Sep 2024 19:55:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E09380007; Sat, 28 Sep 2024 19:55:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E4E6E80009; Sat, 28 Sep 2024 19:55:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C0D0280007 for ; Sat, 28 Sep 2024 19:55:48 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 696A8141658 for ; Sat, 28 Sep 2024 23:55:48 +0000 (UTC) X-FDA: 82615807176.07.F41E631 Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) by imf22.hostedemail.com (Postfix) with ESMTP id 4900FC0009 for ; Sat, 28 Sep 2024 23:55:46 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KN9mdy4e; spf=pass (imf22.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.219.53 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727567581; 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:dkim-signature; bh=xdlFz25Zf+8U2eqLlBb5vpxPfbLxBGZjcVw0lkrPLz0=; b=vstIxAmKtFMx+bKcC8qw2m1B6Fbl85KEvGBbaQisnIV+ufyF3Nyifm3JGbhRrz5UuMfI1N QFqLkuXMfe13maYR5Xs8ycoI0xrSKbtHFATbU88YrGizxtcelT/wlXt6jJdBmbxx8Jpyzj qXul+K+swmpdwyIrD/2yMyXnG5KWaQ0= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=KN9mdy4e; spf=pass (imf22.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.219.53 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727567581; a=rsa-sha256; cv=none; b=kZeG8BV+WH/eFTKvII2c7FasEHr9OIAbWFqEnNKP1CfYjpHtaqwjS+krlUjLHsVM+AVpT5 /khpiVP8ZKhaZjXvGZptP9CU4F5Cq5dd/f6cxMqJP+6dU7lOv4pLtXXf495OhEATtG7Ocd 4Qpa1CekyjTxTxDTQ5BT7rXjbIY4RD4= Received: by mail-qv1-f53.google.com with SMTP id 6a1803df08f44-6cb37b34a87so21443616d6.0 for ; Sat, 28 Sep 2024 16:55:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727567745; x=1728172545; darn=kvack.org; h=content-transfer-encoding:subject:references:in-reply-to:message-id :cc:to:from:date:mime-version:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=xdlFz25Zf+8U2eqLlBb5vpxPfbLxBGZjcVw0lkrPLz0=; b=KN9mdy4emHzd+fVof1EgQbhjw16B0Sl1ODNbdGxH0lAEE/ldV8sYOaP5DrT7t4V17h UZQI/Zo/mjxaVq7ua5S9KoQ6jA//VrY9Rsi+AoSauTEIk2BuLhh9lojn8OnEZd1VlSHm 5ki988DdYd1evJd6MnHMg7+20S6tGXfOWXL/IzeXvuxTtEiFDmg/2t5Nh4bYsWqb4GHS PROJOSSP8FPWQpT1cGFHauK4lvEE61r71xWL3NsIDnKWszw8Mwjb5SplMc1HvmUU8c+e yiPCyuQuJwGFogfZGGl3U1U688fGkgyA+ytW/lHPONjfhp4eJyJjdQ3qyHEpF6kxMCVT 5NYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727567745; x=1728172545; h=content-transfer-encoding:subject:references:in-reply-to:message-id :cc:to:from:date:mime-version:feedback-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=xdlFz25Zf+8U2eqLlBb5vpxPfbLxBGZjcVw0lkrPLz0=; b=Mb0mLPnrkoxgV2n4/5ncvcWvRUIM6uTwQL3B7Oz2ooI2GREuK+o+izt9IdwhVd7jzL BXGDKZ9n4XktB12BqCBmzGI2KCfnJO8IBP1dudUZYMUXhub1r6bTx+7cxnVRlfllSvJ9 O48cIoH0InZLs37kM5x9PRfpjJ35jduKUhTB7KBxXMmGEeAq3WasgQCoW1t2rMPIqW6B isVMHKyHzMP8xCRS19TV+LEZOE2JB+wuvFk9orhY5wnoKLuCDWSKZOkh73e86NQGM6bp bdQwXWseQoSm3MT2LxGk6XlMsvvhG4DLRpJ49ZOftUeIcfvtqSVkZ7/aERfIZQhD2c/q 3Usg== X-Forwarded-Encrypted: i=1; AJvYcCVTDuLR98Fjij/JStDX68Mgu4lNuRmsLnUTmpCYicLLO8tOfi3HeHt7O/xBtFgsN/73ShlCLDBNow==@kvack.org X-Gm-Message-State: AOJu0YyFiZfpCE/v0+pfdQqn+uaB4uRRjUWDSJX5O8kKiBzzJfDZ4iWW 1mP7cfzKo4k/FW5tniDZ+wsPt6xcMimjj0DK2mQFqN8/NU+67L9g X-Google-Smtp-Source: AGHT+IE2EP5102rhFTu960BDjMPWsqQa3LU5oIL8pQz1/lS6aaFDHTp4mKl1437B+0bJhgqnpN7+mQ== X-Received: by 2002:a05:6214:590a:b0:6cb:4ca3:f0ce with SMTP id 6a1803df08f44-6cb4ca3f5b9mr72919926d6.42.1727567745269; Sat, 28 Sep 2024 16:55:45 -0700 (PDT) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6cb3b66ceb7sm24589906d6.90.2024.09.28.16.55.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Sep 2024 16:55:44 -0700 (PDT) Received: from phl-compute-12.internal (phl-compute-12.phl.internal [10.202.2.52]) by mailfauth.phl.internal (Postfix) with ESMTP id F1B231200068; Sat, 28 Sep 2024 19:55:43 -0400 (EDT) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-12.internal (MEProxy); Sat, 28 Sep 2024 19:55:44 -0400 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdduvddgvdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepofggfffhvfevkfgjfhfutgfgsehtqhertdertdej necuhfhrohhmpedfuehoqhhunhcuhfgvnhhgfdcuoegsohhquhhnrdhfvghnghesghhmrg hilhdrtghomheqnecuggftrfgrthhtvghrnhepvdeggeelleekjeejgefhuddvgfehkedt hffhieektedugfeiiefffeetfeejvdeinecuffhomhgrihhnpegvfhhfihgtihhoshdrtg homhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegs ohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdeige dqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihig mhgvrdhnrghmvgdpnhgspghrtghpthhtohepfedupdhmohguvgepshhmthhpohhuthdprh gtphhtthhopehnvggvrhgrjhdruhhprgguhhihrgihsegrmhgurdgtohhmpdhrtghpthht ohepmhgrrhhkrdhruhhtlhgrnhgusegrrhhmrdgtohhmpdhrtghpthhtohepmhgrthhhih gvuhdruggvshhnohihvghrshesvghffhhitghiohhsrdgtohhmpdhrtghpthhtohepghgr rhihsehgrghrhihguhhordhnvghtpdhrtghpthhtohepjhhirghnghhshhgrnhhlrghise hgmhgrihhlrdgtohhmpdhrtghpthhtohepmhgrghgvugdrmhhitghhrggvlhesghhmrghi lhdrtghomhdprhgtphhtthhopehmjhhguhiiihhksehgmhgrihhlrdgtohhmpdhrtghpth htohepmhhmphhgohhurhhiuggvsehgmhgrihhlrdgtohhmpdhrtghpthhtohepqhhirghn ghdriihhrghnghduvdduudesghhmrghilhdrtghomh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id C3CB4B00069; Sat, 28 Sep 2024 19:55:43 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 Date: Sun, 29 Sep 2024 07:55:23 +0800 From: "Boqun Feng" To: "Alan Huang" , "Mathieu Desnoyers" Cc: "Alan Stern" , "Linus Torvalds" , LKML , "Greg Kroah-Hartman" , "Sebastian Andrzej Siewior" , "Paul E. McKenney" , "Will Deacon" , "Peter Zijlstra" , "John Stultz" , "Neeraj Upadhyay" , "Frederic Weisbecker" , "Joel Fernandes" , "Josh Triplett" , "Uladzislau Rezki (Sony)" , rostedt , "Lai Jiangshan" , Zqiang , "Ingo Molnar" , "Waiman Long" , "Mark Rutland" , "Thomas Gleixner" , "Vlastimil Babka" , maged.michael@gmail.com, "Mateusz Guzik" , "Gary Guo" , "Jonas Oberhauser" , RCU , linux-mm@kvack.org, lkmm@lists.linux.dev Message-Id: <60123be5-ae24-4426-b9ca-6f39e64ab76b@app.fastmail.com> In-Reply-To: References: <20240928135128.991110-1-mathieu.desnoyers@efficios.com> <20240928135128.991110-2-mathieu.desnoyers@efficios.com> <02c63e79-ec8c-4d6a-9fcf-75f0e67ea242@rowland.harvard.edu> <2091628c-2d96-4492-99d9-0f6a61b08d1d@efficios.com> Subject: Re: [PATCH 1/2] compiler.h: Introduce ptr_eq() to preserve address dependency Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 4900FC0009 X-Stat-Signature: wx8ojgctdb8au6nu3dhidzbwihfdgfjy X-Rspam-User: X-HE-Tag: 1727567746-922336 X-HE-Meta: U2FsdGVkX19pxrNbWxPYAnFThtL1IlL4BqGeYnGiiypURRFLrsfUIpwNDCc95Inle4D9mKzc6fHyienePMAD92/W1FBW/HaADub5tIws+5nbGvCRTCfro6BekAdZ87PNTuu7xbxW+mXOomHMZI/nmuDYycbQQuVr0czFdjQ+PJvRmedEpB+ckMwK0MrwOssD69X+sMYzHF5rVhozsMqgaMAb3FgSFrt/PkhWf6u9tt37GiICnL2tP6CYslhLTb/0RbsShOU19RAbcSkSelVLAG8gT6r9mBJYCGhwdQX6ehQGG5RORSDTsg8ceUAtf3j7PtNHt3x9Fp/6Yy6HYmCFjlisZIoSq8WZWvI/VxTA4dUs1zqUKbc9DjbU9llW/Ky+cxoyWuaGrF3t8AcvccZiXVYO4Wp39p4dmdiEOKlClAOTW6IgAKcFenc0hhRURBIZZwz7BcHnG6FITX9paDznZsu34NGxmHKqWrpQBaC9Go8YpqRI/QbcA/iQWb0L3rXjz0YjHZnSSolZL+kpCUq4NjmVKsm1EclQAk//teRqEiv6Q05MR5QCJZAs579FT15wkEREZrvMwndp2KNYqWorJcb6U72GFec7uUOIgXCBYkRsOFBVn6UOqNoobPtt/HwXpdOmLvfL3gCPouNnCQN7XXeQEqsQuUgbFycbVjvW3oOGxallstF5ujuT61RQKVmJgpraSFsOghMihG2yDHM9AGdt5rNNZaw6/ZeJNXXBWuwBrSLGL7l0nV4wuGHJii77KxTvCLkoGQhOydv3T2W3YPKfeZf3CrgdQSPsbZ5PJITxli22pBhIBZbZ0DsOIiu7H3sI91bNiFSmJ20j/8MbDkeDcJF1k9A6rC+RuL28FVAVDq5J6WSa8KbdPj6BulUH+hR7uf0mfd3g/vGPsvYqCraj3XvexKUCd/xJDhEOGhfmla5rNU8s8S4SJWzoYAJeoCcXQnbcGxDMKBzb5S5 fWsy6E4p lZRs83aAW15AViGY65tYdin37GZLHguALYPa3J21+2IyfAc/2/JyXKh5srVWRM2RsOFJchDRNQjqbZU92wjs9nWIbDWAxNuLik61zOqJ4yZm3Dx+v3onJofZ+TEmjAtciYVvtr6NZ76TGuJwMBooQdVmJSwkZcKxEYSYWpaHqcB22DgB+Q49j9PQTIsit1v2JBq/zYVdkWuRCVlunp50XcMy6uOrLrj4JQXD2ip9sKwLO3+mduXAwI7IuokIv3b8tbuyDvPVZ+qS0QYucSxE/k74dg0o4UwXwus7qkpy5SvTdLRp9Q4C9/qsNVie7j4zksPjjzgPniuSfV5GlbVu4/05YZ8Al7LCIDnzZ5AeZ3wcJfm6RK1n8DFcfDprCL0JZ7l+rm/ntOPe8ddyNf7JB+uXmSU0V3OEsKN20IYBXBGD7IZQ3F8rWjdHCykJzuuMMgkt9ukgSbmeqP1jy6/fnUrsvcMl1zlNqD4DBboUVOsc9K5hNS9y3uN3M/eE9tGNbpIxeSVKZg+Ls+TGHdg3HRZURriKzLceL50nFTtZ9KV1QnIPKQbU5Y5xn9g== 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 Sun, Sep 29, 2024, at 6:26 AM, Alan Huang wrote: > 2024=E5=B9=B49=E6=9C=8828=E6=97=A5 23:55=EF=BC=8CMathieu Desnoyers wrote=EF=BC=9A >>=20 >> On 2024-09-28 17:49, Alan Stern wrote: >>> On Sat, Sep 28, 2024 at 11:32:18AM -0400, Mathieu Desnoyers wrote: >>>> On 2024-09-28 16:49, Alan Stern wrote: >>>>> On Sat, Sep 28, 2024 at 09:51:27AM -0400, Mathieu Desnoyers wrote: >>>>>> equality, which does not preserve address dependencies and allows= the >>>>>> following misordering speculations: >>>>>>=20 >>>>>> - If @b is a constant, the compiler can issue the loads which dep= end >>>>>> on @a before loading @a. >>>>>> - If @b is a register populated by a prior load, weakly-ordered >>>>>> CPUs can speculate loads which depend on @a before loading @a. >>>>>=20 >>>>> It shouldn't matter whether @a and @b are constants, registers, or >>>>> anything else. All that matters is that the compiler uses the wro= ng >>>>> one, which allows weakly ordered CPUs to speculate loads you would= n't >>>>> expect it to, based on the source code alone. >>>>=20 >>>> I only partially agree here. >>>>=20 >>>> On weakly-ordered architectures, indeed we don't care whether the >>>> issue is caused by the compiler reordering the code (constant) >>>> or the CPU speculating the load (registers). >>>>=20 >>>> However, on strongly-ordered architectures, AFAIU, only the constant >>>> case is problematic (compiler reordering the dependent load), becau= se >>> I thought you were trying to prevent the compiler from using one poi= nter >>> instead of the other, not trying to prevent it from reordering anyth= ing. >>> Isn't this the point the documentation wants to get across when it s= ays >>> that comparing pointers can be dangerous? >>=20 >> The motivation for introducing ptr_eq() is indeed because the >> compiler barrier is not sufficient to prevent the compiler from >> using one pointer instead of the other. > > barrier_data(&b) prevents that. > It prevents that because it acts as barrier() + OPTIMIZER_HIDE_VAR(b). I don=E2=80=99t see much value of using that since we can resolve the pr= oblem with OPTIMIZER_HIDE_VAR() alone. Regards, Boqun >>=20 >> But it turns out that ptr_eq() is also a good tool to prevent the >> compiler from reordering loads in case where the comparison is >> done against a constant. >>=20 >>>> CPU speculating the loads across the control dependency is not an >>>> issue. >>>>=20 >>>> So am I tempted to keep examples that clearly state whether >>>> the issue is caused by compiler reordering instructions, or by >>>> CPU speculation. >>> Isn't it true that on strongly ordered CPUs, a compiler barrier is >>> sufficient to prevent the rcu_dereference() problem? So the whole i= dea >>> behind ptr_eq() is that it prevents the problem on all CPUs. >>=20 >> Correct. But given that we have ptr_eq(), it's good to show how it >> equally prevents the compiler from reordering address-dependent loads >> (comparison with constant) *and* prevents the compiler from using >> one pointer rather than the other (comparison between two non-constant >> pointers) which affects speculation on weakly-ordered CPUs. >>=20 >>> You can make your examples as specific as you like, but the fact rem= ains >>> that ptr_eq() is meant to prevent situations where both: >>> The compiler uses the wrong pointer for a load, and >>> The CPU performs the load earlier than you want. >>> If either one of those doesn't hold then the problem won't arise. >>=20 >> Correct. >>=20 >> Thanks, >>=20 >> Mathieu >>=20 >>=20 >> --=20 >> Mathieu Desnoyers >> EfficiOS Inc. >> https://www.efficios.com >>=20 >>