From patchwork Sun Sep 29 11:16:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 13815062 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 55C43CF6499 for ; Sun, 29 Sep 2024 11:18:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DFAEE8D000D; Sun, 29 Sep 2024 07:18:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DA9E08D000C; Sun, 29 Sep 2024 07:18:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B84F58D000D; Sun, 29 Sep 2024 07:18:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 91FF98D000C for ; Sun, 29 Sep 2024 07:18:26 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 470A9419A4 for ; Sun, 29 Sep 2024 11:18:26 +0000 (UTC) X-FDA: 82617527412.06.03E442F Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by imf28.hostedemail.com (Postfix) with ESMTP id AAA61C0004 for ; Sun, 29 Sep 2024 11:18:23 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=NNkAdE6i; spf=pass (imf28.hostedemail.com: domain of mathieu.desnoyers@efficios.com designates 167.114.26.122 as permitted sender) smtp.mailfrom=mathieu.desnoyers@efficios.com; dmarc=pass (policy=none) header.from=efficios.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727608665; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=XwwKmLYREfYfy45CeOtjYBqVSb3De3yQP9JU5FkNWjM=; b=ersV/WAdOYjZahQGVR+k6kFJj8kt02XoPmeOjXYplB7Py+a3x7vYRjH8otJvybhvHV1Zat WW2JicudmafCch2Yq5aDcOXw3obro50IEEYZFJSDhH3OzAuHmxctWT8c7Buj1kBFXJfQlX I+GUWKgsA/ZOtczaZ1HNbd1UtMl/WRA= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=NNkAdE6i; spf=pass (imf28.hostedemail.com: domain of mathieu.desnoyers@efficios.com designates 167.114.26.122 as permitted sender) smtp.mailfrom=mathieu.desnoyers@efficios.com; dmarc=pass (policy=none) header.from=efficios.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727608665; a=rsa-sha256; cv=none; b=UWIN6i305ArW5E82scAc3wEPb/J7G3EgJuSwYoyWiQ00XHCXg++KHyJgJSsgJ1HzPVALzB QrEppJOoyXOWdE83FGxO7uoOmEhncuupya1x7SiyG/9ZkY12Y/yG9pXfenXS/bAo3Hmq2u /3u4N0QW0FvyWZ+EfyFTEP8/bTV/yCw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1727608702; bh=Y5j1aIqMovkS/FInHPWE1mFExvGy7kLegXx8btwyZ4E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NNkAdE6i9u/f3JyH2cAFuOiH4BVyA3T9LasBQAM1KhNDRZpwxJ/LPHiFfYkMC1Zo4 A+mkLLwvAuIqf9FiSDoCMBEV1ltpOmSN1s/UU44YbBp3+AljsjV54ldX92HW6B7hQ0 MMU4jw71FqGLD9AekGjwv5vPqpaDFPHZxzJcDrLKRe6NEXLi7mzcEtyFAxq7C49QvO WR/TbkJVldbtfR3K6eCdXtURg8fgZuKSV3TnhBNb9ApRCd41bFecdWD9ZJagtaPKzA NAr9dZ6JzdUt8qAZHGlb7ICsbw+39SglcNZXRPcd1PEIByl2wi3jj9taF/vY8ykkD9 9NTicTxZw7xaA== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4XGhXG4Hftz696; Sun, 29 Sep 2024 07:18:22 -0400 (EDT) From: Mathieu Desnoyers To: Linus Torvalds Cc: linux-kernel@vger.kernel.org, Mathieu Desnoyers , Greg Kroah-Hartman , Sebastian Andrzej Siewior , "Paul E. McKenney" , Will Deacon , Peter Zijlstra , Boqun Feng , 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 , Gary Guo , Jonas Oberhauser , rcu@vger.kernel.org, linux-mm@kvack.org, lkmm@lists.linux.dev, Nikita Popov , llvm@lists.linux.dev Subject: [PATCH v1 1/2] compiler.h: Introduce ptr_eq() to preserve address dependency Date: Sun, 29 Sep 2024 07:16:07 -0400 Message-Id: <20240929111608.1016757-2-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240929111608.1016757-1-mathieu.desnoyers@efficios.com> References: <20240929111608.1016757-1-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: fgtnyqkpqsdejoun17c8ht96tb47cho8 X-Rspamd-Queue-Id: AAA61C0004 X-Rspamd-Server: rspam11 X-HE-Tag: 1727608703-258963 X-HE-Meta: U2FsdGVkX18xwJAQb2xObf003bnYwaYUD12vx6kGo06Hl6nuczW4QRO3edABqy+t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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: 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 using @a (or @b) in places where the source refers to @b (or @a) based on the fact that after the comparison, the two are known to be equal, 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. Suggested-by: Linus Torvalds Suggested-by: Boqun Feng Signed-off-by: Mathieu Desnoyers Reviewed-by: Boqun Feng Acked-by: "Paul E. McKenney" 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: Gary Guo Cc: Jonas Oberhauser Cc: rcu@vger.kernel.org Cc: linux-mm@kvack.org Cc: lkmm@lists.linux.dev Cc: Nikita Popov Cc: llvm@lists.linux.dev --- Changes since v0: - Include feedback from Alan Stern. --- include/linux/compiler.h | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index 2df665fa2964..75a378ae7af1 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -186,6 +186,69 @@ 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 using @a (or @b) in places where the source refers to @b (or @a) + * based on the fact that after the comparison, the two are known to be + * equal, 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; +} + #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__) /** From patchwork Sun Sep 29 11:16:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 13815060 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 24F09CF6499 for ; Sun, 29 Sep 2024 11:18:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6596D8E0001; Sun, 29 Sep 2024 07:18:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E0928D000C; Sun, 29 Sep 2024 07:18:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 40D778E0001; Sun, 29 Sep 2024 07:18:26 -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 1DB398D0009 for ; Sun, 29 Sep 2024 07:18:26 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8A1A041C52 for ; Sun, 29 Sep 2024 11:18:25 +0000 (UTC) X-FDA: 82617527370.01.12E56B4 Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by imf14.hostedemail.com (Postfix) with ESMTP id EF50C100008 for ; Sun, 29 Sep 2024 11:18:23 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b="bKR/jLng"; dmarc=pass (policy=none) header.from=efficios.com; spf=pass (imf14.hostedemail.com: domain of mathieu.desnoyers@efficios.com designates 167.114.26.122 as permitted sender) smtp.mailfrom=mathieu.desnoyers@efficios.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727608602; a=rsa-sha256; cv=none; b=xYNXCvk0WF44IUxPrg9w5uqOnTWM4xfQVbnSVoG2Q2llLkiHw7koBc1LoMk1BtVvVFSU/S +V592XaXaA6QcsxsifKZ7WHlpwKgWDO2//dr1PTwMi4pGUu6Uz+2+8PAHTMPbC/UM9ZDkg MEOz7IyLlJuqHtmF5v1Dwtl/UFKkHeM= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b="bKR/jLng"; dmarc=pass (policy=none) header.from=efficios.com; spf=pass (imf14.hostedemail.com: domain of mathieu.desnoyers@efficios.com designates 167.114.26.122 as permitted sender) smtp.mailfrom=mathieu.desnoyers@efficios.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1727608602; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aJsbfRPNvPne4qjdnMqBlIya67U284rcXlFHaMDYg4M=; b=EvEnErFNDMGf0tCG9Oq3tgkFIXwxcsRo17s3LEZ/knetcesrkL06Ylt4APS2llEC/cjPpb Zp65cAcjW7xU+DqPgejO6RTwnPITpu3iKhPJdEZnMBMfkbuGSHe/jrKYHq92CiJsCi7Ug9 NySM56eVUa13y6v3E4B2vC9CZ84n4M8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1727608703; bh=g1ERcjuxIIQfzyAE04JgYpLXWJLghnUv7XhUlDPv9Y4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bKR/jLngSMnpfkC+0RATww72JqtH1SoYAn6K35TxeOozZBSsXRNV0CBRN0ZfegI/2 r/+8JtcIu/mnr+vyisFGHKYQCpbqhJzMgwf5KlY/WMQTlaVbWtVsjsmL2bw8G+Dv2B pdEAXppYbeIKbMF/A/Lq9/2RM3PMl2mHH92429U4/RbFXYOImIqKcKX+P5JpCYD+fL VT9cR8Io1Swpe+OhuwkP7Kvleoz191Ei4HHvUI7vSqAhUtSmCQWen7eNllMpbAZLQn wSfP0muk5IrqHAezuL9nH6FMnDjRQcxiSAEpRUQAXqftJHa5kuYzP6VAJBEeGwGEQq 0xmbgO178/+Iw== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4XGhXG75M3z6Ht; Sun, 29 Sep 2024 07:18:22 -0400 (EDT) From: Mathieu Desnoyers To: Linus Torvalds Cc: linux-kernel@vger.kernel.org, Mathieu Desnoyers , Greg Kroah-Hartman , Sebastian Andrzej Siewior , "Paul E. McKenney" , Will Deacon , Peter Zijlstra , Boqun Feng , 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 , Gary Guo , Jonas Oberhauser , rcu@vger.kernel.org, linux-mm@kvack.org, lkmm@lists.linux.dev, Nikita Popov , llvm@lists.linux.dev Subject: [PATCH v1 2/2] Documentation: RCU: Refer to ptr_eq() Date: Sun, 29 Sep 2024 07:16:08 -0400 Message-Id: <20240929111608.1016757-3-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240929111608.1016757-1-mathieu.desnoyers@efficios.com> References: <20240929111608.1016757-1-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: EF50C100008 X-Stat-Signature: 7m8bmp7im5iqnbgxf1f1cdxk959jpfti X-Rspam-User: X-HE-Tag: 1727608703-47435 X-HE-Meta: U2FsdGVkX19lCq4krJlp8iZAq1WBUCsqD2rxCLdJF4cASpu2wkOfM7kcBUBBzjsDWDHeFoVfqSrEyU9l+9pUm074dtpWiaAq3tInebJalDxP/CcvPEm/k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ner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Refer to ptr_eq() in the rcu_dereference() documentation. ptr_eq() is a mechanism that preserves address dependencies when comparing pointers, and should be favored when comparing a pointer obtained from rcu_dereference() against another pointer. 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: Gary Guo Cc: Jonas Oberhauser Cc: rcu@vger.kernel.org Cc: linux-mm@kvack.org Cc: lkmm@lists.linux.dev Cc: Nikita Popov Cc: llvm@lists.linux.dev Acked-by: Paul E. McKenney --- Changes since v0: - Include feedback from Alan Stern. --- Documentation/RCU/rcu_dereference.rst | 32 ++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/Documentation/RCU/rcu_dereference.rst b/Documentation/RCU/rcu_dereference.rst index 2524dcdadde2..9ef97b7ca74d 100644 --- a/Documentation/RCU/rcu_dereference.rst +++ b/Documentation/RCU/rcu_dereference.rst @@ -104,11 +104,12 @@ readers working properly: after such branches, but can speculate loads, which can again result in misordering bugs. -- Be very careful about comparing pointers obtained from - rcu_dereference() against non-NULL values. As Linus Torvalds - explained, if the two pointers are equal, the compiler could - substitute the pointer you are comparing against for the pointer - obtained from rcu_dereference(). For example:: +- Use operations that preserve address dependencies (such as + "ptr_eq()") to compare pointers obtained from rcu_dereference() + against non-NULL pointers. As Linus Torvalds explained, if the + two pointers are equal, the compiler could substitute the + pointer you are comparing against for the pointer obtained from + rcu_dereference(). For example:: p = rcu_dereference(gp); if (p == &default_struct) @@ -125,6 +126,23 @@ readers working properly: On ARM and Power hardware, the load from "default_struct.a" can now be speculated, such that it might happen before the rcu_dereference(). This could result in bugs due to misordering. + Performing the comparison with "ptr_eq()" ensures the compiler + does not perform such transformation. + + If the comparison is against another pointer, the compiler is + allowed to use either pointer for the following accesses, which + loses the address dependency and allows weakly-ordered + architectures such as ARM and PowerPC to speculate the + address-dependent load before rcu_dereference(). For example:: + + p1 = READ_ONCE(gp); + p2 = rcu_dereference(gp); + if (p1 == p2) + do_default(p2->a); + + The compiler can use p1->a rather than p2->a, destroying the + address dependency. Performing the comparison with "ptr_eq()" + ensures the compiler preserves the address dependencies. However, comparisons are OK in the following cases: @@ -204,6 +222,10 @@ readers working properly: comparison will provide exactly the information that the compiler needs to deduce the value of the pointer. + When in doubt, use operations that preserve address dependencies + (such as "ptr_eq()") to compare pointers obtained from + rcu_dereference() against non-NULL pointers. + - Disable any value-speculation optimizations that your compiler might provide, especially if you are making use of feedback-based optimizations that take data collected from prior runs. Such