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 5C7A8CF648D for ; Fri, 27 Sep 2024 23:11:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E209D6B0156; Fri, 27 Sep 2024 19:11:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D59D36B0157; Fri, 27 Sep 2024 19:11:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B85696B0158; Fri, 27 Sep 2024 19:11:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9083E6B0156 for ; Fri, 27 Sep 2024 19:11:05 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0EFA0ACF59 for ; Fri, 27 Sep 2024 23:11:05 +0000 (UTC) X-FDA: 82612065690.03.2A74D09 Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) by imf12.hostedemail.com (Postfix) with ESMTP id DE92A40006 for ; Fri, 27 Sep 2024 23:11:02 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="I34P/sQ1"; spf=pass (imf12.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.222.174 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=1727478499; 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=vpoTAtzsVeeK1uuREm1KOA1rviE8CgrfOlB6HO554Gk=; b=DD5kNQ7ewx8v79Gpmx3jmTQrTCWQSUSEGCqx4gc6f3aXIrLS5+FzO5JL7WxwiPf4+b5EX0 UTX+Dhiw6BMO6yHHBuYohKR4R5fQhtuWfKvaimlsF20jZTBVoimkpXGjMYi0vJtX3tKgcN PNDE5Bgfjpy1dtAKm0YF9YL9D10yQa4= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="I34P/sQ1"; spf=pass (imf12.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.222.174 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=1727478499; a=rsa-sha256; cv=none; b=gSAHd6hkTdEMV75B1eiBHh/fwBgFuqUaXSnkrCks4F8GVKmHc6PTeWY470BxHL8OGPb7Fr tZ3eU38ZW+ajf+sZpJN4iE0F5F0+ImGW6bBh92J94ifW14Sg2jobYh8gCeQNdg42Tnj5+o YHMBzFDqwijQh6Wt0wsxkR9vlAn8O3E= Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-7a9b049251eso185685885a.2 for ; Fri, 27 Sep 2024 16:11:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727478662; x=1728083462; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=vpoTAtzsVeeK1uuREm1KOA1rviE8CgrfOlB6HO554Gk=; b=I34P/sQ1kIyoEhLWPq9Gf16Z0b92aMikPS/jdPeTQuil1ZDX3eqjfrgBeJCC3vUagg Fc0QqgqG275w2xH+zBo6oCrrjBv9qYe2jMPpkVFlN7AIHAjMIPpAMdjDiGW5SSFS9CtR agE0wCp2ya9rfBm6QL/sxOya/jQDdyIXW4smNtkwyCvkBWdOgEGfR9FQRS/rcf/ZqSa2 ukNjJ7RrajQA3DbmGxiLyqHQHWZnGrDPE1WKUvQRrOUgI2Xe6v3lgBc5Y5TqY2JxPQri CelCUFdDCRKWU+j8lOJ8cyoB13XLHelyBDu4GGMNJG8R+EpeyigdsbmB4aVHuLBO0e8R A7LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727478662; x=1728083462; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vpoTAtzsVeeK1uuREm1KOA1rviE8CgrfOlB6HO554Gk=; b=BWjYldHlelUSqzPejH0ZwPTVPOJIIk9JGox8BJcriqjHSF7+oKSG7K0xScJZ/srk7Z 2SFzIAVImhQO5KYkZnDwEMrW+BV8FlRiC02OIr8jqjvWLZ+Znoi8hcyhSU0q29xzdPbX 6dqkircfBORvPRxuAieNZPBgzkqaIO8W2lT5saFSPIFW9RBo3rwkSZJNN72r0BRxpBN3 ghjuOWM9173Tt1laIZEyiU3EXjL5pcOntkBWgnmGRAW0SDt8FI5pdSzSNQ2GzalQ/N0e DzmIb+wSPiWtzwq++KWjVo+oWYo6NhgWT1ZbVDeJACU1nYvlDX6wU1ZqFAMG5SCnNjiT HCWQ== X-Forwarded-Encrypted: i=1; AJvYcCVf1bVpr1d1LXYOknnjALcWBTM18I5z360uDUvyKbOqId/2UaZs/tO5N7gMFGqPilXzmJsHagc0JA==@kvack.org X-Gm-Message-State: AOJu0YyU9NhJNMUnjKT4ssE5xJi+yNSB1nH02nLgCNr8qJ5g/+AaTV8C eWNFs2szYwqmWCrJyyv+0pNj6NJhS3xCQu8nJj9ypMbcsLl5EAhL X-Google-Smtp-Source: AGHT+IFBDTW5qDL4IO8+urSC0PfeHvBtwa+yXuuYMnvjq5EwYU75Ii6opIDkOZR/QjLSpzSZoio8og== X-Received: by 2002:a05:620a:414d:b0:7ab:3311:f0e with SMTP id af79cd13be357-7ae378562bfmr670455385a.33.1727478661811; Fri, 27 Sep 2024 16:11:01 -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 af79cd13be357-7ae377d773asm136809785a.35.2024.09.27.16.11.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Sep 2024 16:11:01 -0700 (PDT) Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfauth.phl.internal (Postfix) with ESMTP id 7372A120006A; Fri, 27 Sep 2024 19:11:00 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-04.internal (MEProxy); Fri, 27 Sep 2024 19:11:00 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddutddgvddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvden ucfhrhhomhepuehoqhhunhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrd gtohhmqeenucggtffrrghtthgvrhhnpeehudfgudffffetuedtvdehueevledvhfelleei vedtgeeuhfegueevieduffeivdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhi thihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmh grihhlrdgtohhmsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthhtohepfedtpdhmohgu vgepshhmthhpohhuthdprhgtphhtthhopehmrghthhhivghurdguvghsnhhohigvrhhsse gvfhhfihgtihhoshdrtghomhdprhgtphhtthhopehtohhrvhgrlhgusheslhhinhhugidq fhhouhhnuggrthhiohhnrdhorhhgpdhrtghpthhtoheplhhinhhugidqkhgvrhhnvghlse hvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepghhrvghgkhhhsehlihhnuhig fhhouhhnuggrthhiohhnrdhorhhgpdhrtghpthhtohepsghighgvrghshieslhhinhhuth hrohhnihigrdguvgdprhgtphhtthhopehprghulhhmtghksehkvghrnhgvlhdrohhrghdp rhgtphhtthhopeifihhllheskhgvrhhnvghlrdhorhhgpdhrtghpthhtohepphgvthgvrh iisehinhhfrhgruggvrggurdhorhhgpdhrtghpthhtohepshhtvghrnhesrhhofihlrghn ugdrhhgrrhhvrghrugdrvgguuh X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 27 Sep 2024 19:10:59 -0400 (EDT) Date: Fri, 27 Sep 2024 16:10:09 -0700 From: Boqun Feng To: Mathieu Desnoyers Cc: Linus Torvalds , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Sebastian Andrzej Siewior , "Paul E. McKenney" , Will Deacon , Peter Zijlstra , Alan Stern , 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 , rcu@vger.kernel.org, linux-mm@kvack.org, lkmm@lists.linux.dev, Gary Guo Subject: Re: [RFC PATCH] compiler.h: Introduce ptr_eq() to preserve address dependency Message-ID: References: <20240927203334.976821-1-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240927203334.976821-1-mathieu.desnoyers@efficios.com> X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: DE92A40006 X-Stat-Signature: jy5c4asas1phow1easxpnbgn81otjfqa X-Rspam-User: X-HE-Tag: 1727478662-147450 X-HE-Meta: U2FsdGVkX1/3l+AgbBpPcHtn6F2eJUqB1SzQpBhIXV25eBzI/IZoI/gnfJbI99oYfuqLG3wPnRias20eq3WnQZGkmjm8sLJET2KZwiI9vIHfAgM/YsZDzbDVyF99H92dWonw/dr4aNToOCeyomUe4vDz74XvmCxZAZJSUL4/QhdMESlO7NCqjyoeal8K/gyZDOx1TNI89lEEuxk2wCIVt7WN517snBBISbBhwm9pwjzbuP2yw6Kv6hRZpegrCkJ968v/jUm5iVCEa3/IIny5vVndVL8WMJz556kPMgwNuGHWZaErWz7Ygn2rrTPJiFX2HzhLFsi5iPnWYuG96g1CYFxKpiEqXcewMmAH0M5POn82pXXTu4na7mbIMRCw/H1RlBkdP6Z4c2Ml6MI4u6xE633zpHkbW3oGQuspvEI02CL+kAOJl+iSzPuY9kh6ux2JfVqhCwPeYX+3F0EwyEfjwqtv9AMilhdHiLvdWBGHGHQccGlU7zORkcZFureuFXNIZbBG5DkyPVhy+35fwHjvO7NdgsCV6DO00pxaYCmgygALruwT/Jqs7vZfsIqeHhwWOvLfZTBdBGzEjELInmo/LdtgVa2ZgnU8WAPBq2qtjIy/I3ALp9iG90mbs1uesd+C8aSUQoz/McnLTCn+NG0+8yMITB39kfalqYEdNdOOPWeYSZxtHjZpN3q88uxXzQs9mq24P40wnntkaVCwlI41Tp40MeyBLtS8fJeW2DNlLBgiWVTAYulotjSTJ+jSXDVooJqAEXKse6jdi80jsTxOEmq+w9bAx7WLjFT1emdCsi8rQlhci2W++fq0Z5k1OFnEcFXu/YE3MsC9DU30ZpGcabfTT7uTx6UVwnS8nZz4lnR5ypxRpVdWwN5VxHz1pyNpNKLqAEROWqKtJ/Wb7uSyzWt8nQRrYmkzzLj0Pw+YHqtLk0rFZ37f9mS6w00cv1i0KTRW6mWa5lcH5oMhT+y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S5INPl6qh5wbLZb+Kp1kunnsnGrJe8aaoaCrXOImf66oHtaKV+vIjcKz2xoXD/0ltYNXOHWh/ECsfDAufaM1ysUJJ449A3qIIFCNZvpDg8ULUEJFYiYbmV3F92gR1BMh7kzoHkXx2M968= 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: [Cc Gary] On Fri, Sep 27, 2024 at 04:33:34PM -0400, Mathieu Desnoyers wrote: > Compiler CSE and SSA GVN optimizations can cause the address dependency > of addresses returned by rcu_dereference to be lost when comparing those > pointers with either constants or previously loaded pointers. > > Introduce ptr_eq() to compare two addresses while preserving the address > dependencies for later use of the address. It should be used when > comparing an address returned by rcu_dereference(). > > This is needed to prevent the compiler CSE and SSA GVN optimizations > from replacing the registers holding @a or @b based on their > equality, which does not preserve address dependencies and allows the > following misordering speculations: > > - If @b is a constant, the compiler can issue the loads which depend > 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. > > The same logic applies with @a and @b swapped. > > The compiler barrier() is ineffective at fixing this issue. > It does not prevent the compiler CSE from losing the address dependency: > > int fct_2_volatile_barriers(void) > { > int *a, *b; > > do { > a = READ_ONCE(p); > asm volatile ("" : : : "memory"); > b = READ_ONCE(p); > } while (a != b); > asm volatile ("" : : : "memory"); <----- barrier() > return *b; > } > > With gcc 14.2 (arm64): > > fct_2_volatile_barriers: > adrp x0, .LANCHOR0 > add x0, x0, :lo12:.LANCHOR0 > .L2: > ldr x1, [x0] <------ x1 populated by first load. > ldr x2, [x0] > cmp x1, x2 > bne .L2 > ldr w0, [x1] <------ x1 is used for access which should depend on b. > ret > > On weakly-ordered architectures, this lets CPU speculation use the > result from the first load to speculate "ldr w0, [x1]" before > "ldr x2, [x0]". > Based on the RCU documentation, the control dependency does not prevent > the CPU from speculating loads. > > Suggested-by: Linus Torvalds > Suggested-by: Boqun Feng > Signed-off-by: Mathieu Desnoyers > Cc: Greg Kroah-Hartman > Cc: Sebastian Andrzej Siewior > Cc: "Paul E. McKenney" > Cc: Will Deacon > Cc: Peter Zijlstra > Cc: Boqun Feng > Cc: Alan Stern > Cc: John Stultz > Cc: Neeraj Upadhyay > Cc: Linus Torvalds > Cc: Boqun Feng > Cc: Frederic Weisbecker > Cc: Joel Fernandes > Cc: Josh Triplett > Cc: Uladzislau Rezki > Cc: Steven Rostedt > Cc: Lai Jiangshan > Cc: Zqiang > Cc: Ingo Molnar > Cc: Waiman Long > Cc: Mark Rutland > Cc: Thomas Gleixner > Cc: Vlastimil Babka > Cc: maged.michael@gmail.com > Cc: Mateusz Guzik > Cc: rcu@vger.kernel.org > Cc: linux-mm@kvack.org > Cc: lkmm@lists.linux.dev > --- > include/linux/compiler.h | 62 ++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 62 insertions(+) > > diff --git a/include/linux/compiler.h b/include/linux/compiler.h > index 2df665fa2964..f26705c267e8 100644 > --- a/include/linux/compiler.h > +++ b/include/linux/compiler.h > @@ -186,6 +186,68 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val, > __asm__ ("" : "=r" (var) : "0" (var)) > #endif > > +/* > + * Compare two addresses while preserving the address dependencies for > + * later use of the address. It should be used when comparing an address > + * returned by rcu_dereference(). > + * > + * This is needed to prevent the compiler CSE and SSA GVN optimizations > + * from replacing the registers holding @a or @b based on their > + * equality, which does not preserve address dependencies and allows the > + * following misordering speculations: > + * > + * - If @b is a constant, the compiler can issue the loads which depend > + * 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. > + * > + * The same logic applies with @a and @b swapped. > + * > + * Return value: true if pointers are equal, false otherwise. > + * > + * The compiler barrier() is ineffective at fixing this issue. It does > + * not prevent the compiler CSE from losing the address dependency: > + * > + * int fct_2_volatile_barriers(void) > + * { > + * int *a, *b; > + * > + * do { > + * a = READ_ONCE(p); > + * asm volatile ("" : : : "memory"); > + * b = READ_ONCE(p); > + * } while (a != b); > + * asm volatile ("" : : : "memory"); <-- barrier() > + * return *b; > + * } > + * > + * With gcc 14.2 (arm64): > + * > + * fct_2_volatile_barriers: > + * adrp x0, .LANCHOR0 > + * add x0, x0, :lo12:.LANCHOR0 > + * .L2: > + * ldr x1, [x0] <-- x1 populated by first load. > + * ldr x2, [x0] > + * cmp x1, x2 > + * bne .L2 > + * ldr w0, [x1] <-- x1 is used for access which should depend on b. > + * ret > + * > + * On weakly-ordered architectures, this lets CPU speculation use the > + * result from the first load to speculate "ldr w0, [x1]" before > + * "ldr x2, [x0]". > + * Based on the RCU documentation, the control dependency does not > + * prevent the CPU from speculating loads. > + */ > +static __always_inline > +int ptr_eq(const volatile void *a, const volatile void *b) > +{ > + OPTIMIZER_HIDE_VAR(a); > + OPTIMIZER_HIDE_VAR(b); > + return a == b; > +} > + This is better than what I proposed, thank you! Reviewed-by: Boqun Feng Could you also make some documentation changes at the "Be very careful about comparing pointers obtained from..." paragraph in Documentation/RCU/rcu_dereference.rst? Since 'ptr_eq' is a good tool in those cases mentioned there. Thanks. Regards, Boqun > #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__) > > /** > -- > 2.39.2 >