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: U2FsdGVkX19lCq4krJlp8iZAq1WBUCsqD2rxCLdJF4cASpu2wkOfM7k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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: 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