From patchwork Wed Oct 2 01:02:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 13819080 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 41D02CF318A for ; Wed, 2 Oct 2024 01:04:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8212C680044; Tue, 1 Oct 2024 21:04:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E5F7680046; Tue, 1 Oct 2024 21:04:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5FA8B680044; Tue, 1 Oct 2024 21:04:43 -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 3E19768002B for ; Tue, 1 Oct 2024 21:04:43 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D23671C6D1E for ; Wed, 2 Oct 2024 01:04:42 +0000 (UTC) X-FDA: 82626867204.03.669E7D1 Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by imf22.hostedemail.com (Postfix) with ESMTP id 4643DC0013 for ; Wed, 2 Oct 2024 01:04:41 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=KaNB6lo6; spf=pass (imf22.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=1727831041; 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=oPX59bvwXibOxKyRX3ygAz7qh/sEJEaoE98t1UlQ1Ug=; b=Gs+SE2g/2fyf0JzsvYPLGxlVGL+owzIxunVmYBRB0u4IFW+UiVy18M3IkHThIYJbm8sVpt iTtwdtv+Nsgdk3mbTrEhRrJR6JsZtW5zjaT6voDIKfnnElfR1Kd4tKSx/Hpi2rMqa4zXVH n2YeGMgtJT2b7W81udG0MHXxaJXBJ+U= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=KaNB6lo6; spf=pass (imf22.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=1727831041; a=rsa-sha256; cv=none; b=2a1Bq6zv1fpqoSEuIhA70yyWbTPwHDr8n0c4IfRxL0pzOtgOY1x5KgqydYeMlkHP0G8GcQ 79ZLvDX20OJqufsnBiSvRsaRNWEKLbt0WL5HcqzdEfrFaJl2hoUO8dHj96jlrXsJ+NzLZ7 Lkjom2j8P7rnWLXl7N1hHhhuVp5/YxU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1727831080; bh=59DRX32mdLXQysZ3Sgtuo0FR/eOuDpT4eLg0JUW2fnk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KaNB6lo6ISQgXrUO78AXmLuIFN7BV4iBsZNXSWA69948Uvn3PU3kuggbxHOfc1Bfr Go+PfaP7fQGthexDGSC868GPoIcmsFTEWMoY9EQ8P6KvKgJEkdkZIyz7LE0gl/HfA4 NhscuTm3+Mm2QXJl/Z4u8q0cZcv2gEbkICH8v5niYGxOuNxbfKaUtBOjbd/oqGJNmf 6b8CRKo0JDTFyJI5khNY5JTt9tfWi2S7tio8yv92TBnzA5vRf5mpZfS+jalbV7fyug CVVlALHEBSgjOpOv7+oGP6sdMJywq7HeJR7POyhGrpyIdPnLq+A1gsjvO2sVYKHdpf 2voJad+AJIX3w== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4XJGmm1Q4jzjd1; Tue, 1 Oct 2024 21:04:40 -0400 (EDT) From: Mathieu Desnoyers To: Linus Torvalds , Andrew Morton , Peter Zijlstra Cc: linux-kernel@vger.kernel.org, Mathieu Desnoyers , Nicholas Piggin , Michael Ellerman , Greg Kroah-Hartman , Sebastian Andrzej Siewior , "Paul E. McKenney" , Will Deacon , 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 , Jonas Oberhauser , rcu@vger.kernel.org, linux-mm@kvack.org, lkmm@lists.linux.dev, Gary Guo , Nikita Popov , llvm@lists.linux.dev Subject: [RFC PATCH 2/4] Documentation: RCU: Refer to ptr_eq() Date: Tue, 1 Oct 2024 21:02:03 -0400 Message-Id: <20241002010205.1341915-3-mathieu.desnoyers@efficios.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20241002010205.1341915-1-mathieu.desnoyers@efficios.com> References: <20241002010205.1341915-1-mathieu.desnoyers@efficios.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 7f1ay33i61a9i4rn13kns3x776jw4jx6 X-Rspamd-Queue-Id: 4643DC0013 X-Rspamd-Server: rspam11 X-HE-Tag: 1727831081-721621 X-HE-Meta: U2FsdGVkX1/6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BVfjh7oCk= 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: 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 Acked-by: Alan Stern 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. Changes since v1: - Include feedback from Paul E. McKenney. --- Documentation/RCU/rcu_dereference.rst | 38 +++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/Documentation/RCU/rcu_dereference.rst b/Documentation/RCU/rcu_dereference.rst index 2524dcdadde2..de6175bf430f 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,29 @@ 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) /* BUGGY!!! */ + 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. + Corrected code:: + + p1 = READ_ONCE(gp); + p2 = rcu_dereference(gp); + if (ptr_eq(p1, p2)) + do_default(p2->a); However, comparisons are OK in the following cases: @@ -204,6 +228,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