From patchwork Wed Oct 2 01:02:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 13819081 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 E1EF4CF3189 for ; Wed, 2 Oct 2024 01:04:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6A73680047; Tue, 1 Oct 2024 21:04:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C1D76680045; 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 9C956680047; Tue, 1 Oct 2024 21:04:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 74ADB68002B for ; Tue, 1 Oct 2024 21:04:43 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E6F4C14159C for ; Wed, 2 Oct 2024 01:04:42 +0000 (UTC) X-FDA: 82626867204.27.033F831 Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by imf11.hostedemail.com (Postfix) with ESMTP id 58E2140005 for ; Wed, 2 Oct 2024 01:04:41 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=kThGxzqm; spf=pass (imf11.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=1727830953; 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=8KrjyG7UJVLDweqHlvxnqxw3ltq+4n3/lbHvzWngHaI=; b=kCnUyAI8qiNH0nBG0cO1d9Cf6h7lmk24PZHa/DFO+IBUtbJo7N99E7VfM+8w0CeuuRfV2N AxbM3ux4A6/3JukfoMuORFUjuKLVjQLz2Kd/v2kmIWMCyWBqGBy7cshiEyTzzQ+h9AuHVO 365xIGdfwAYmbGkLHb5Zj2DVyt2xXEg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727830953; a=rsa-sha256; cv=none; b=WXVYAEno9gHgnbYvnGCQC5ezwmNcXLDwnvMQ78gx6OetVfGUaUiDerlNffGBttrux0R6Mr 4af9HS74nvwTzmWy9bQFleYEvy/V8Jz/u/UDQM4EAx5BEnPObAKuRTxdPYksiuGSw3/BZz QlG6hy1hpUaN8q9vq8kMwnxF+4FyxTE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=kThGxzqm; spf=pass (imf11.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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1727831080; bh=eumWImPFsrX/ZuZnIIsDwO32qp7NYwQGZnsiXAMqNR4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kThGxzqmna0mbJ4PbssiWmX/BbPYXO8M5axEv9WOQMzumAQ53j1/oVSMNu8gKySqp f+TKwzph7C9tRlgVpbFQ6lCs9Ff9rZARWu8/iQPDOAVjYuK0zZ+1GiS7rFrQtGe9R2 /t3iyg4We7QxR8Fg56xcGBNAl/Ueuuu7bRaPTUf92dq2RF9drLyD2fw4QSD1uM1TSF sd3nShCSpSLLNu5F6KUlIPwxhB9tW/1sT1SlXOUK40eRgr/jhs0jQq6+ky1RlaLnW7 sQ1sAvKd64Dhjpdnt8bg3LsKMM59l4xzoSg50SNIjgVljv5h7VUY9GOJRzweBq+8lD E+8mHfbUTVkWA== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4XJGml5tn7zj4J; Tue, 1 Oct 2024 21:04:39 -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 1/4] compiler.h: Introduce ptr_eq() to preserve address dependency Date: Tue, 1 Oct 2024 21:02:02 -0400 Message-Id: <20241002010205.1341915-2-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-Stat-Signature: mhraw3o3g8npmsiz5caoaxypwww3tc68 X-Rspamd-Queue-Id: 58E2140005 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1727831081-78177 X-HE-Meta: U2FsdGVkX1/lW/pmUwecUHpj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osity: 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: 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" Acked-by: Alan Stern 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 Reviewed-by: Joel Fernandes (Google) Tested-by: Joel Fernandes (Google) --- 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 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: U2Fs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bdBW75kDQkBVfjh7oCk= 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 From patchwork Wed Oct 2 01:02:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 13819082 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 BE814CF3189 for ; Wed, 2 Oct 2024 01:04:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1498F680049; Tue, 1 Oct 2024 21:04:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D8898680046; 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 B066F680046; Tue, 1 Oct 2024 21:04:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7836D680045 for ; Tue, 1 Oct 2024 21:04:43 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 215C41A175B for ; Wed, 2 Oct 2024 01:04:43 +0000 (UTC) X-FDA: 82626867246.11.EB5DDA9 Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by imf29.hostedemail.com (Postfix) with ESMTP id 7F2AC120014 for ; Wed, 2 Oct 2024 01:04:41 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=XncHOzB1; spf=pass (imf29.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=1727831016; a=rsa-sha256; cv=none; b=7pgc9g/gbVjLFtrsCNaCa+J7k6VDwbqjRWYM2JuF37NwQ+J3pB67Yrto5JTrIXSoWcOXMn XDuflfCFt1msrqip59xiWSr0K6WMcQWrosHHd3NtobJXRfJoU+B7CUqGITyWNzCNdzxdm7 nNB92udijz0VexdoiCcTyesQlwpgCVw= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b=XncHOzB1; spf=pass (imf29.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=1727831016; 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=WORQPmDatvD9PC4GTa8jptInYC1tBLWJzTXr1yuYzuk=; b=vCqwoasL9UQeio3h0QBKJxPZNTR9LR4dcJgWL4VVn6sUfsL087AsAiDszttS6rRnfJEsVz YIuc3eQDbTn4u4IziRr16cXjNjeRXD8in1L16FK2uporjJUQTwZIIv+QHZxLhHVQc6441E Dt34lrP8jljAQ8NwtxcqlmVxXYAgSXM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1727831080; bh=YpbszxPGSpf5EjU2PeRKfD+htiAvIE0R8ZYJQ/cohsw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XncHOzB1ONmQdzo1CB6cDxvEeibW7m6gIOfIsJN79bRDRsVr/1W6+xQ8cu7+188fh yGWbXG7QwcT+NOta8ICmSr6WVbmtMwOdoyjTz1jrOML2BIsdUEkqEZGycHFzIvpwog 6pZ25Vygfk+mDNfoaU4BTQzgKc4FDcCoTJF9zZWrJ2E9QhMuDgpJSL78H5eQEyaNVm L1vZVHILlCgU/IF2UgR10te5GEB0WtBUM8PZd1AyvlwokiY0dWqus6U6wwF1vB5ee0 SHFAVY5FRF7TMlsPgZqiNE8bJGZJBmIKRuqoR0lz+n1Dbj7qyEXeOMfMhR/KQ2f9tM J2ek7gvRKsE3Q== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4XJGmm40c6zjRK; 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 Subject: [RFC PATCH 3/4] hp: Implement Hazard Pointers Date: Tue, 1 Oct 2024 21:02:04 -0400 Message-Id: <20241002010205.1341915-4-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-Stat-Signature: by7wcm3cu49kdksd4rb79hbwgz8p3xbf X-Rspamd-Queue-Id: 7F2AC120014 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1727831081-873666 X-HE-Meta: U2FsdGVkX19CnYvGuT0MnsIAnFD8qdeC9yRSvzsgV5napI6bjXGg1RMDTLJCNP9TXdS0Esb/h8jQS+JhX/2qp4XPx0d2dyHD0KubRPWep5mXUq8r0Co7JJob90pHSiqUlfOsPVDLovQhLhQP7bo97FvGEIuVHHy20O1XRNbRQA94DS6yLlO2mULZlEIbQ6YuRYO0dtM6mYx57EMNIdVFUwRJ5Jc97998rnYMftEyV0YrByZC9V91JNZ1wlgqtks+VyX0nzTy0FnUziOWOE3FeY0RF4LTaJWeiJ3nTjZobiin9KnKmCilAkO4HRHHoTdo7QuGpaua1YqgxbjTW5Qz2gRYYXzy01+mU2JNbDqSYeql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HSxFd8mbuiL0BiM65gW/tLCP71siZsaVsiJuWvVthdVO9ySsi8oz6F9q+w4uBGtHG66ae7+wBpilv7mttl712aPlOQVKb12i+eRVi2nEHiuD3D74ibGRo8U5cyqzhOMWrDkoqSzPcYcnIpXsJwj5/FX5UkexlkSuaMvmwSorMQgtLHcTCZkRI5CrsYYj4YQqaZaXgSb9Obp1faV0wGyxnApdSSSwd1dPbDwq8bk+vMtjpaSDXd6a7eGNW5ASi79oD8fzeCgYd2z1G8nWTPQn4aLeAVB7tl83SF+7c6x2zMTSyo7RQFM6FrPd8++5L020dCivCRZ0MY9CekmIwoY7KwoyCidLTCt3mQDok+adDXwycwGknSXyN7uFpiDF3XDSoDglDB4OjUU4mihriygfjqKdZ0hyibWTtwbHK5oWF1UWBoz040HQ5gd94DitlAZrUA5nG7O96pl5ba62RgnuVHEdHKXSbIW5aXJuEpDwKLk57zq+hFso+svMadIUis/fgLXkTI+N6WSgFebDUpE7lfypIh hxFdJNi3 V542lnqLOIKaAnEM2DLocrlTsSJ9ZwH6TFUaju6sPPNHCi2O9zmgH/ADMzqV6FTY883DTQGsyx5OQJzIWG8L1/lxiES73sbpAQ9QtvVjU4KV/DpqS3RGSQ== 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: This API provides existence guarantees of objects through Hazard Pointers (HP). Each HP domain defines a fixed number of hazard pointer slots (nr_cpus) across the entire system. Its main benefit over RCU is that it allows fast reclaim of HP-protected pointers without needing to wait for a grace period. It also allows the hazard pointer scan to call a user-defined callback to retire a hazard pointer slot immediately if needed. This callback may, for instance, issue an IPI to the relevant CPU. There are a few possible use-cases for this in the Linux kernel: - Improve performance of mm_count by replacing lazy active mm by HP. - Guarantee object existence on pointer dereference to use refcount: - replace locking used for that purpose in some drivers, - replace RCU + inc_not_zero pattern, - rtmutex: Improve situations where locks need to be taken in reverse dependency chain order by guaranteeing existence of first and second locks in traversal order, allowing them to be locked in the correct order (which is reverse from traversal order) rather than try-lock+retry on nested lock. References: [1]: M. M. Michael, "Hazard pointers: safe memory reclamation for lock-free objects," in IEEE Transactions on Parallel and Distributed Systems, vol. 15, no. 6, pp. 491-504, June 2004 Link: https://lore.kernel.org/lkml/j3scdl5iymjlxavomgc6u5ndg3svhab6ga23dr36o4f5mt333w@7xslvq6b6hmv/ Link: https://lpc.events/event/18/contributions/1731/ Signed-off-by: Mathieu Desnoyers Cc: Nicholas Piggin Cc: Michael Ellerman 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: Andrew Morton 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: Jonas Oberhauser Cc: rcu@vger.kernel.org Cc: linux-mm@kvack.org Cc: lkmm@lists.linux.dev --- include/linux/hp.h | 154 +++++++++++++++++++++++++++++++++++++++++++++ kernel/Makefile | 2 +- kernel/hp.c | 46 ++++++++++++++ 3 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 include/linux/hp.h create mode 100644 kernel/hp.c diff --git a/include/linux/hp.h b/include/linux/hp.h new file mode 100644 index 000000000000..929e8685a0fd --- /dev/null +++ b/include/linux/hp.h @@ -0,0 +1,154 @@ +// SPDX-FileCopyrightText: 2024 Mathieu Desnoyers +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef _LINUX_HP_H +#define _LINUX_HP_H + +/* + * HP: Hazard Pointers + * + * This API provides existence guarantees of objects through hazard + * pointers. + * + * It uses a fixed number of hazard pointer slots (nr_cpus) across the + * entire system for each HP domain. + * + * Its main benefit over RCU is that it allows fast reclaim of + * HP-protected pointers without needing to wait for a grace period. + * + * It also allows the hazard pointer scan to call a user-defined callback + * to retire a hazard pointer slot immediately if needed. This callback + * may, for instance, issue an IPI to the relevant CPU. + * + * References: + * + * [1]: M. M. Michael, "Hazard pointers: safe memory reclamation for + * lock-free objects," in IEEE Transactions on Parallel and + * Distributed Systems, vol. 15, no. 6, pp. 491-504, June 2004 + */ + +#include + +/* + * Hazard pointer slot. + */ +struct hp_slot { + void *addr; +}; + +/* + * Hazard pointer context, returned by hp_use(). + */ +struct hp_ctx { + struct hp_slot *slot; + void *addr; +}; + +/* + * hp_scan: Scan hazard pointer domain for @addr. + * + * Scan hazard pointer domain for @addr. + * If @retire_cb is NULL, wait to observe that each slot contains a value + * that differs from @addr. + * If @retire_cb is non-NULL, invoke @callback for each slot containing + * @addr. + */ +void hp_scan(struct hp_slot __percpu *percpu_slots, void *addr, + void (*retire_cb)(int cpu, struct hp_slot *slot, void *addr)); + +/* Get the hazard pointer context address (may be NULL). */ +static inline +void *hp_ctx_addr(struct hp_ctx ctx) +{ + return ctx.addr; +} + +/* + * hp_allocate: Allocate a hazard pointer. + * + * Allocate a hazard pointer slot for @addr. The object existence should + * be guaranteed by the caller. + * + * Returns a hazard pointer context. + */ +static inline +struct hp_ctx hp_allocate(struct hp_slot __percpu *percpu_slots, void *addr) +{ + struct hp_slot *slot; + struct hp_ctx ctx; + + if (!addr) + goto fail; + slot = this_cpu_ptr(percpu_slots); + /* + * A single hazard pointer slot per CPU is available currently. + * Other hazard pointer domains can eventually have a different + * configuration. + */ + if (READ_ONCE(slot->addr)) + goto fail; + WRITE_ONCE(slot->addr, addr); /* Store B */ + ctx.slot = slot; + ctx.addr = addr; + return ctx; + +fail: + ctx.slot = NULL; + ctx.addr = NULL; + return ctx; +} + +/* + * hp_dereference_allocate: Dereference and allocate a hazard pointer. + * + * Returns a hazard pointer context. + */ +static inline +struct hp_ctx hp_dereference_allocate(struct hp_slot __percpu *percpu_slots, void * const * addr_p) +{ + struct hp_slot *slot; + void *addr, *addr2; + struct hp_ctx ctx; + + addr = READ_ONCE(*addr_p); +retry: + ctx = hp_allocate(percpu_slots, addr); + if (!hp_ctx_addr(ctx)) + goto fail; + /* Memory ordering: Store B before Load A. */ + smp_mb(); + /* + * Use RCU dereference without lockdep checks, because + * lockdep is not aware of HP guarantees. + */ + addr2 = rcu_access_pointer(*addr_p); /* Load A */ + /* + * If @addr_p content has changed since the first load, + * clear the hazard pointer and try again. + */ + if (!ptr_eq(addr2, addr)) { + WRITE_ONCE(slot->addr, NULL); + if (!addr2) + goto fail; + addr = addr2; + goto retry; + } + ctx.slot = slot; + ctx.addr = addr2; + return ctx; + +fail: + ctx.slot = NULL; + ctx.addr = NULL; + return ctx; +} + +/* Retire the hazard pointer in @ctx. */ +static inline +void hp_retire(const struct hp_ctx ctx) +{ + smp_store_release(&ctx.slot->addr, NULL); +} + +#endif /* _LINUX_HP_H */ diff --git a/kernel/Makefile b/kernel/Makefile index 3c13240dfc9f..ec16de96fa80 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -7,7 +7,7 @@ obj-y = fork.o exec_domain.o panic.o \ cpu.o exit.o softirq.o resource.o \ sysctl.o capability.o ptrace.o user.o \ signal.o sys.o umh.o workqueue.o pid.o task_work.o \ - extable.o params.o \ + extable.o params.o hp.o \ kthread.o sys_ni.o nsproxy.o \ notifier.o ksysfs.o cred.o reboot.o \ async.o range.o smpboot.o ucount.o regset.o ksyms_common.o diff --git a/kernel/hp.c b/kernel/hp.c new file mode 100644 index 000000000000..b2447bf15300 --- /dev/null +++ b/kernel/hp.c @@ -0,0 +1,46 @@ +// SPDX-FileCopyrightText: 2024 Mathieu Desnoyers +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +/* + * HP: Hazard Pointers + */ + +#include +#include + +/* + * hp_scan: Scan hazard pointer domain for @addr. + * + * Scan hazard pointer domain for @addr. + * If @retire_cb is non-NULL, invoke @callback for each slot containing + * @addr. + * Wait to observe that each slot contains a value that differs from + * @addr before returning. + */ +void hp_scan(struct hp_slot __percpu *percpu_slots, void *addr, + void (*retire_cb)(int cpu, struct hp_slot *slot, void *addr)) +{ + int cpu; + + /* + * Store A precedes hp_scan(): it unpublishes addr (sets it to + * NULL or to a different value), and thus hides it from hazard + * pointer readers. + */ + + if (!addr) + return; + /* Memory ordering: Store A before Load B. */ + smp_mb(); + /* Scan all CPUs slots. */ + for_each_possible_cpu(cpu) { + struct hp_slot *slot = per_cpu_ptr(percpu_slots, cpu); + + if (retire_cb && smp_load_acquire(&slot->addr) == addr) /* Load B */ + retire_cb(cpu, slot, addr); + /* Busy-wait if node is found. */ + while ((smp_load_acquire(&slot->addr)) == addr) /* Load B */ + cpu_relax(); + } +} From patchwork Wed Oct 2 01:02:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mathieu Desnoyers X-Patchwork-Id: 13819084 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 ECCD0CF3189 for ; Wed, 2 Oct 2024 01:04:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6A7B468002B; Tue, 1 Oct 2024 21:04:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2F41A680048; Tue, 1 Oct 2024 21:04:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 148F5680045; Tue, 1 Oct 2024 21:04:44 -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 C9077680048 for ; Tue, 1 Oct 2024 21:04:43 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 71F411C6D1E for ; Wed, 2 Oct 2024 01:04:43 +0000 (UTC) X-FDA: 82626867246.26.91DFA62 Received: from smtpout.efficios.com (smtpout.efficios.com [167.114.26.122]) by imf07.hostedemail.com (Postfix) with ESMTP id E6A544000B for ; Wed, 2 Oct 2024 01:04:41 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b="HkrAj/ua"; spf=pass (imf07.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=1727830954; 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=6f1pQa0MZKxKcjeKUtMHjt0Q4k4o+ayrZgYrd0KgBGc=; b=IbI0/LfFuUqumbdVBHX7idyhD1q96f5dCDJNp20SzEe4z4uRRrtasjTxonnP1g79vBWYur Nlf/7iMXuVjOb0ZPuzAseOppguF0882e0S0HeAxP9+T21Ms9K9QUSsM2PLMnCCeSa+E4Dd bLj1waNwH7+X/0/rZ++c6hG2EuB2i0E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727830954; a=rsa-sha256; cv=none; b=6FsOrRC2viSt9lSozPDR9OVcNfk99xew+MwbQ6PUP/POrrIkjhy05JHBmtYgaJOPJ6uLTo au9XoLggORzhA6ib2HeOT95/mQuyyX+ihFyOAo4wNIu5rds4HWvIu0FWBFtQIcbavm93E1 ENZrsZUpKIogAIt0ttMpvBehRaXZuC0= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=efficios.com header.s=smtpout1 header.b="HkrAj/ua"; spf=pass (imf07.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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=efficios.com; s=smtpout1; t=1727831081; bh=KhCgq8/VW/73gqkVz00pGJVD+ke1nSXtEyCBARYvylk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HkrAj/uaDf9kJ0Wx95CfYdHab3m/D6/qZeMTmPxsJAPWkP9HrS5GyF7WU8mEQnr0C wkgOwNFa6gGPpnkhFoKH4lc5p7qQ4UWB8d84TOb6NocY1xbcEvfSNf5kHnwXnVbybq 5o8VS18IhqJFexDKc9urmTu0OHHSmsfMdPJ7XpcBzr22uwz0FAGSJur3cAg0lb0EkC u197UJunNMnHIFIQ7qKskh3Y+fd9+rDuh9adRab7e6Ti8itqFyuvY68pyXAcONGTRI n+Omd8oSuTGhKVKEDPOAa4LXEE9OAxse5ohUnwJXfRADrOAjqZXpry4GqyzeLBZ6ih SKY00Omhfzgjw== Received: from thinkos.internal.efficios.com (unknown [IPv6:2606:6d00:100:4000:cacb:9855:de1f:ded2]) by smtpout.efficios.com (Postfix) with ESMTPSA id 4XJGmm6WCqzj09; 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 Subject: [RFC PATCH 4/4] sched+mm: Use hazard pointers to track lazy active mm existence Date: Tue, 1 Oct 2024 21:02:05 -0400 Message-Id: <20241002010205.1341915-5-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-Stat-Signature: d4yniamzym3wc3ftes5xp8enwbomxaua X-Rspamd-Queue-Id: E6A544000B X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1727831081-35100 X-HE-Meta: U2FsdGVkX1+W+TUF5i/N9phvXJMvv5iC3x9aZNbazHb7Kq+H9AgIsYcloIQV2qVH2PfuI8zgAY+CsHRBd1HTVJ0kVktMJR1LHAXNS39y/ntlkn9fAePMDBUzi9aQuMV9KYEqARQnXuTJ6NPuICMyJXt8WMLV0MUa/kQyOB9WbeXkRF0zy/31N7UCox7FaYKGRT/RGOUZHjje1VdsAmZaNlNCVykypW9L3IG0+8oWXKwm2CvGPYDGHoRU83uPVFWYnmcHMSytl6fFvRO66MKBmU1QGzh6jF+q47UYhWh28+86Ly2dJW2NhIalghR8N7yflemg6DLgjkeX7U3UruCKHcsGaEqGj1a/8AIPqn+50EclZTXDcb/ToIfzIOapPTo/PUnst8PIS3NKJxZAcT+A6C/68RAOeEpVhW5cfliIXC6V0OQ5nYqm0AnKbp0peuN3F1qmfP1GUOZQO0EZnqeLn6cg1bpktXJvz3FetH28HuZVGRAcFq04BKMjNOEEpmWuhCQHIbXUErMHb6SCKuEko+KBhUFWqUaQBrQCOHWZBxWkaD3Nss6s3D9/RjphG+ClEIg6Bo4F8nDebYsyUOhqQCYocOlx3t5bU+ALsZGmePjokpTlWfbDHe+k5JUC0AfTwIbwJfGqckGwIm7tf48Fhp2P7X0lfg2auFYVLclsHuUnrui5JfWMfCZ5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i2HmQ4lMZpXML5NmTpKjDBRdONyh/0tP6ArA4PWSt8IImd3grHVkxUx2eA81qZER7vqke0zhrTuLJN9IlihQXb2KELac9L/W/RyMUdipjEZtH89ytm58mGIDEWZ8F6yH6YxwMRONpx1auD60RPQnH0azz3Ax2LjzHbNeMnqqj/TTWI7GPHvqiuluzBLu9u/v5FhlVH3m0PWShB6c o2aP1x7v A5TTkmjvVcgzke/iFA3W2wBoeLs73Ed4aWW7yyV2cco= 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: Replace lazy active mm existence tracking with hazard pointers. This removes the following implementations and their associated config options: - MMU_LAZY_TLB_REFCOUNT - MMU_LAZY_TLB_SHOOTDOWN - This removes the call_rcu delayed mm drop for RT. It leverages the fact that each CPU only ever have at most one single lazy active mm. This makes it a very good fit for a hazard pointer domain implemented with one hazard pointer slot per CPU. * Benchmarks: will-it-scale context_switch1_threads nr threads (-t) speedup 24 +3% 48 +12% 96 +21% 192 +28% Methodology: Each test is the average of 20 iterations. Use median result of 3 test runs. Test hardware: CPU(s): 384 On-line CPU(s) list: 0-383 Vendor ID: AuthenticAMD Model name: AMD EPYC 9654 96-Core Processor CPU family: 25 Model: 17 Thread(s) per core: 2 Core(s) per socket: 96 Socket(s): 2 Stepping: 1 Frequency boost: enabled CPU(s) scaling MHz: 100% CPU max MHz: 3709.0000 CPU min MHz: 400.0000 BogoMIPS: 4799.75 Memory: 768 GB ram. Signed-off-by: Mathieu Desnoyers Cc: Nicholas Piggin Cc: Michael Ellerman 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: Andrew Morton 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: Jonas Oberhauser Cc: rcu@vger.kernel.org Cc: linux-mm@kvack.org Cc: lkmm@lists.linux.dev --- Documentation/mm/active_mm.rst | 9 ++-- arch/Kconfig | 32 ------------- arch/powerpc/Kconfig | 1 - arch/powerpc/mm/book3s64/radix_tlb.c | 23 +-------- include/linux/mm_types.h | 3 -- include/linux/sched/mm.h | 71 +++++++++++----------------- kernel/exit.c | 4 +- kernel/fork.c | 47 +++++------------- kernel/sched/sched.h | 8 +--- lib/Kconfig.debug | 10 ---- 10 files changed, 49 insertions(+), 159 deletions(-) diff --git a/Documentation/mm/active_mm.rst b/Documentation/mm/active_mm.rst index d096fc091e23..c225cac49c30 100644 --- a/Documentation/mm/active_mm.rst +++ b/Documentation/mm/active_mm.rst @@ -2,11 +2,10 @@ Active MM ========= -Note, the mm_count refcount may no longer include the "lazy" users -(running tasks with ->active_mm == mm && ->mm == NULL) on kernels -with CONFIG_MMU_LAZY_TLB_REFCOUNT=n. Taking and releasing these lazy -references must be done with mmgrab_lazy_tlb() and mmdrop_lazy_tlb() -helpers, which abstract this config option. +Note, the mm_count refcount no longer include the "lazy" users (running +tasks with ->active_mm == mm && ->mm == NULL) Taking and releasing these +lazy references must be done with mmgrab_lazy_tlb() and mmdrop_lazy_tlb() +helpers, which are implemented with hazard pointers. :: diff --git a/arch/Kconfig b/arch/Kconfig index 975dd22a2dbd..d4261935f8dc 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -475,38 +475,6 @@ config ARCH_WANT_IRQS_OFF_ACTIVATE_MM irqs disabled over activate_mm. Architectures that do IPI based TLB shootdowns should enable this. -# Use normal mm refcounting for MMU_LAZY_TLB kernel thread references. -# MMU_LAZY_TLB_REFCOUNT=n can improve the scalability of context switching -# to/from kernel threads when the same mm is running on a lot of CPUs (a large -# multi-threaded application), by reducing contention on the mm refcount. -# -# This can be disabled if the architecture ensures no CPUs are using an mm as a -# "lazy tlb" beyond its final refcount (i.e., by the time __mmdrop frees the mm -# or its kernel page tables). This could be arranged by arch_exit_mmap(), or -# final exit(2) TLB flush, for example. -# -# To implement this, an arch *must*: -# Ensure the _lazy_tlb variants of mmgrab/mmdrop are used when manipulating -# the lazy tlb reference of a kthread's ->active_mm (non-arch code has been -# converted already). -config MMU_LAZY_TLB_REFCOUNT - def_bool y - depends on !MMU_LAZY_TLB_SHOOTDOWN - -# This option allows MMU_LAZY_TLB_REFCOUNT=n. It ensures no CPUs are using an -# mm as a lazy tlb beyond its last reference count, by shooting down these -# users before the mm is deallocated. __mmdrop() first IPIs all CPUs that may -# be using the mm as a lazy tlb, so that they may switch themselves to using -# init_mm for their active mm. mm_cpumask(mm) is used to determine which CPUs -# may be using mm as a lazy tlb mm. -# -# To implement this, an arch *must*: -# - At the time of the final mmdrop of the mm, ensure mm_cpumask(mm) contains -# at least all possible CPUs in which the mm is lazy. -# - It must meet the requirements for MMU_LAZY_TLB_REFCOUNT=n (see above). -config MMU_LAZY_TLB_SHOOTDOWN - bool - config ARCH_HAVE_NMI_SAFE_CMPXCHG bool diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index d7b09b064a8a..b1e25e75baab 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -291,7 +291,6 @@ config PPC select MMU_GATHER_PAGE_SIZE select MMU_GATHER_RCU_TABLE_FREE select MMU_GATHER_MERGE_VMAS - select MMU_LAZY_TLB_SHOOTDOWN if PPC_BOOK3S_64 select MODULES_USE_ELF_RELA select NEED_DMA_MAP_STATE if PPC64 || NOT_COHERENT_CACHE select NEED_PER_CPU_EMBED_FIRST_CHUNK if PPC64 diff --git a/arch/powerpc/mm/book3s64/radix_tlb.c b/arch/powerpc/mm/book3s64/radix_tlb.c index 9e1f6558d026..ff0d4f28cf52 100644 --- a/arch/powerpc/mm/book3s64/radix_tlb.c +++ b/arch/powerpc/mm/book3s64/radix_tlb.c @@ -1197,28 +1197,7 @@ void radix__tlb_flush(struct mmu_gather *tlb) * See the comment for radix in arch_exit_mmap(). */ if (tlb->fullmm) { - if (IS_ENABLED(CONFIG_MMU_LAZY_TLB_SHOOTDOWN)) { - /* - * Shootdown based lazy tlb mm refcounting means we - * have to IPI everyone in the mm_cpumask anyway soon - * when the mm goes away, so might as well do it as - * part of the final flush now. - * - * If lazy shootdown was improved to reduce IPIs (e.g., - * by batching), then it may end up being better to use - * tlbies here instead. - */ - preempt_disable(); - - smp_mb(); /* see radix__flush_tlb_mm */ - exit_flush_lazy_tlbs(mm); - __flush_all_mm(mm, true); - - preempt_enable(); - } else { - __flush_all_mm(mm, true); - } - + __flush_all_mm(mm, true); } else if ( (psize = radix_get_mmu_psize(page_size)) == -1) { if (!tlb->freed_tables) radix__flush_tlb_mm(mm); diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 485424979254..db5f13554485 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -975,9 +975,6 @@ struct mm_struct { atomic_t tlb_flush_batched; #endif struct uprobes_state uprobes_state; -#ifdef CONFIG_PREEMPT_RT - struct rcu_head delayed_drop; -#endif #ifdef CONFIG_HUGETLB_PAGE atomic_long_t hugetlb_usage; #endif diff --git a/include/linux/sched/mm.h b/include/linux/sched/mm.h index 91546493c43d..0fecd1a3311d 100644 --- a/include/linux/sched/mm.h +++ b/include/linux/sched/mm.h @@ -9,6 +9,10 @@ #include #include #include +#include + +/* Sched lazy mm hazard pointer domain. */ +DECLARE_PER_CPU(struct hp_slot, hp_domain_sched_lazy_mm); /* * Routines for handling mm_structs @@ -55,61 +59,42 @@ static inline void mmdrop(struct mm_struct *mm) __mmdrop(mm); } -#ifdef CONFIG_PREEMPT_RT -/* - * RCU callback for delayed mm drop. Not strictly RCU, but call_rcu() is - * by far the least expensive way to do that. - */ -static inline void __mmdrop_delayed(struct rcu_head *rhp) +/* Helpers for lazy TLB mm refcounting */ +static inline void mmgrab_lazy_tlb(struct mm_struct *mm) { - struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop); + struct hp_ctx ctx; - __mmdrop(mm); -} + /* + * mmgrab_lazy_tlb must provide a full memory barrier, see the + * membarrier comment finish_task_switch which relies on this. + */ + smp_mb(); -/* - * Invoked from finish_task_switch(). Delegates the heavy lifting on RT - * kernels via RCU. - */ -static inline void mmdrop_sched(struct mm_struct *mm) -{ - /* Provides a full memory barrier. See mmdrop() */ - if (atomic_dec_and_test(&mm->mm_count)) - call_rcu(&mm->delayed_drop, __mmdrop_delayed); -} -#else -static inline void mmdrop_sched(struct mm_struct *mm) -{ - mmdrop(mm); -} -#endif + /* + * The caller guarantees existence of mm. Allocate a hazard + * pointer to chain this existence guarantee to a hazard + * pointer. + */ + ctx = hp_allocate(&hp_domain_sched_lazy_mm, mm); -/* Helpers for lazy TLB mm refcounting */ -static inline void mmgrab_lazy_tlb(struct mm_struct *mm) -{ - if (IS_ENABLED(CONFIG_MMU_LAZY_TLB_REFCOUNT)) - mmgrab(mm); + /* There is only a single lazy mm per CPU at any time. */ + WARN_ON_ONCE(!hp_ctx_addr(ctx)); } static inline void mmdrop_lazy_tlb(struct mm_struct *mm) { - if (IS_ENABLED(CONFIG_MMU_LAZY_TLB_REFCOUNT)) { - mmdrop(mm); - } else { - /* - * mmdrop_lazy_tlb must provide a full memory barrier, see the - * membarrier comment finish_task_switch which relies on this. - */ - smp_mb(); - } + /* + * mmdrop_lazy_tlb must provide a full memory barrier, see the + * membarrier comment finish_task_switch which relies on this. + */ + smp_mb(); + WRITE_ONCE(this_cpu_ptr(&hp_domain_sched_lazy_mm)->addr, NULL); } static inline void mmdrop_lazy_tlb_sched(struct mm_struct *mm) { - if (IS_ENABLED(CONFIG_MMU_LAZY_TLB_REFCOUNT)) - mmdrop_sched(mm); - else - smp_mb(); /* see mmdrop_lazy_tlb() above */ + smp_mb(); /* see mmdrop_lazy_tlb() above */ + WRITE_ONCE(this_cpu_ptr(&hp_domain_sched_lazy_mm)->addr, NULL); } /** diff --git a/kernel/exit.c b/kernel/exit.c index 7430852a8571..cb4ace06c0f0 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -545,8 +545,6 @@ static void exit_mm(void) if (!mm) return; mmap_read_lock(mm); - mmgrab_lazy_tlb(mm); - BUG_ON(mm != current->active_mm); /* more a memory barrier than a real lock */ task_lock(current); /* @@ -561,6 +559,8 @@ static void exit_mm(void) */ smp_mb__after_spinlock(); local_irq_disable(); + mmgrab_lazy_tlb(mm); + BUG_ON(mm != current->active_mm); current->mm = NULL; membarrier_update_current_mm(NULL); enter_lazy_tlb(mm, current); diff --git a/kernel/fork.c b/kernel/fork.c index cc760491f201..42c652ec39b5 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -149,6 +149,9 @@ DEFINE_PER_CPU(unsigned long, process_counts) = 0; __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ +/* Sched lazy mm hazard pointer domain. */ +DEFINE_PER_CPU(struct hp_slot, hp_domain_sched_lazy_mm); + #ifdef CONFIG_PROVE_RCU int lockdep_tasklist_lock_is_held(void) { @@ -855,50 +858,24 @@ static void do_shoot_lazy_tlb(void *arg) WARN_ON_ONCE(current->mm); current->active_mm = &init_mm; switch_mm(mm, &init_mm, current); + WRITE_ONCE(this_cpu_ptr(&hp_domain_sched_lazy_mm)->addr, NULL); } } -static void cleanup_lazy_tlbs(struct mm_struct *mm) +static void retire_lazy_mm_hp(int cpu, struct hp_slot *slot, void *addr) { - if (!IS_ENABLED(CONFIG_MMU_LAZY_TLB_SHOOTDOWN)) { - /* - * In this case, lazy tlb mms are refounted and would not reach - * __mmdrop until all CPUs have switched away and mmdrop()ed. - */ - return; - } + smp_call_function_single(cpu, do_shoot_lazy_tlb, addr, 1); + smp_call_function_single(cpu, do_check_lazy_tlb, addr, 1); +} +static void cleanup_lazy_tlbs(struct mm_struct *mm) +{ /* - * Lazy mm shootdown does not refcount "lazy tlb mm" usage, rather it - * requires lazy mm users to switch to another mm when the refcount + * Require lazy mm users to switch to another mm when the refcount * drops to zero, before the mm is freed. This requires IPIs here to * switch kernel threads to init_mm. - * - * archs that use IPIs to flush TLBs can piggy-back that lazy tlb mm - * switch with the final userspace teardown TLB flush which leaves the - * mm lazy on this CPU but no others, reducing the need for additional - * IPIs here. There are cases where a final IPI is still required here, - * such as the final mmdrop being performed on a different CPU than the - * one exiting, or kernel threads using the mm when userspace exits. - * - * IPI overheads have not found to be expensive, but they could be - * reduced in a number of possible ways, for example (roughly - * increasing order of complexity): - * - The last lazy reference created by exit_mm() could instead switch - * to init_mm, however it's probable this will run on the same CPU - * immediately afterwards, so this may not reduce IPIs much. - * - A batch of mms requiring IPIs could be gathered and freed at once. - * - CPUs store active_mm where it can be remotely checked without a - * lock, to filter out false-positives in the cpumask. - * - After mm_users or mm_count reaches zero, switching away from the - * mm could clear mm_cpumask to reduce some IPIs, perhaps together - * with some batching or delaying of the final IPIs. - * - A delayed freeing and RCU-like quiescing sequence based on mm - * switching to avoid IPIs completely. */ - on_each_cpu_mask(mm_cpumask(mm), do_shoot_lazy_tlb, (void *)mm, 1); - if (IS_ENABLED(CONFIG_DEBUG_VM_SHOOT_LAZIES)) - on_each_cpu(do_check_lazy_tlb, (void *)mm, 1); + hp_scan(&hp_domain_sched_lazy_mm, mm, retire_lazy_mm_hp); } /* diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 4c36cc680361..d883c2aa3518 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3527,12 +3527,8 @@ static inline void switch_mm_cid(struct rq *rq, if (!next->mm) { // to kernel /* * user -> kernel transition does not guarantee a barrier, but - * we can use the fact that it performs an atomic operation in - * mmgrab(). - */ - if (prev->mm) // from user - smp_mb__after_mmgrab(); - /* + * we can use the fact that mmgrab() has a full barrier. + * * kernel -> kernel transition does not change rq->curr->mm * state. It stays NULL. */ diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index a30c03a66172..1cb9dab361c9 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -803,16 +803,6 @@ config DEBUG_VM If unsure, say N. -config DEBUG_VM_SHOOT_LAZIES - bool "Debug MMU_LAZY_TLB_SHOOTDOWN implementation" - depends on DEBUG_VM - depends on MMU_LAZY_TLB_SHOOTDOWN - help - Enable additional IPIs that ensure lazy tlb mm references are removed - before the mm is freed. - - If unsure, say N. - config DEBUG_VM_MAPLE_TREE bool "Debug VM maple trees" depends on DEBUG_VM