* [PATCH hazptr v2 2/4] ptreq.h: Introduce ptr_eq() to preserve address dependency [not found] <20261009182142.6311-1-mathieu.desnoyers@efficios.com> @ 2026-10-09 18:21 ` Mathieu Desnoyers 2026-10-09 19:03 ` Boqun Feng 2026-10-09 21:55 ` Gary Guo 2026-10-09 18:21 ` [PATCH hazptr v2 3/4] Documentation: RCU: Refer to ptr_eq() Mathieu Desnoyers 1 sibling, 2 replies; 6+ messages in thread From: Mathieu Desnoyers @ 2026-10-09 18:21 UTC (permalink / raw) To: Paul E . McKenney Cc: linux-kernel, Mathieu Desnoyers, Linus Torvalds, Boqun Feng, Greg Kroah-Hartman, Sebastian Andrzej Siewior, Will Deacon, Peter Zijlstra, Alan Stern, John Stultz, 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, Mateusz Guzik, Gary Guo, rcu, linux-mm, lkmm, Nikita Popov, llvm, Lian Wang, Kunwu Chan 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. The header "linux/ptreq.h" is the expected include target. It contains the documentation of the ptr_eq() API. An architecture specific implementation of the pointer comparison can be implemented by each architecture as asm/ptreq.h. The x86 implementation is provided initially. If no implementation header is present for the architecture, an arch-agnostic fallback based on OPTIMIZER_HIDE_VAR is included from asm-generic. Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> Suggested-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Cc: "Paul E. McKenney" <paulmck@kernel.org> Cc: Will Deacon <will@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Alan Stern <stern@rowland.harvard.edu> Cc: John Stultz <jstultz@google.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Frederic Weisbecker <frederic@kernel.org> Cc: Joel Fernandes <joel@joelfernandes.org> Cc: Josh Triplett <josh@joshtriplett.org> Cc: Uladzislau Rezki <urezki@gmail.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Lai Jiangshan <jiangshanlai@gmail.com> Cc: Zqiang <qiang.zhang1211@gmail.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Waiman Long <longman@redhat.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: maged.michael@gmail.com Cc: Mateusz Guzik <mjguzik@gmail.com> Cc: Gary Guo <gary@garyguo.net> Cc: rcu@vger.kernel.org Cc: linux-mm@kvack.org Cc: lkmm@lists.linux.dev Cc: Nikita Popov <github@npopov.com> Cc: llvm@lists.linux.dev Cc: Lian Wang <lianux.mm@gmail.com> Cc: Kunwu Chan <kunwu.chan@gmail.com> --- Changes since v1: - Move to linux/ptreq.h, implement x86-specific comparison and asm-generic header (fallback). Changes since v0: - Include feedback from Alan Stern. --- arch/x86/include/asm/ptreq.h | 16 +++++++++ include/asm-generic/Kbuild | 1 + include/asm-generic/ptreq.h | 16 +++++++++ include/linux/ptreq.h | 63 ++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 arch/x86/include/asm/ptreq.h create mode 100644 include/asm-generic/ptreq.h create mode 100644 include/linux/ptreq.h diff --git a/arch/x86/include/asm/ptreq.h b/arch/x86/include/asm/ptreq.h new file mode 100644 index 000000000000..593b45f80451 --- /dev/null +++ b/arch/x86/include/asm/ptreq.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_PTREQ_H +#define _ASM_X86_PTREQ_H + +#include <linux/types.h> +#include <asm/asm.h> + +static __always_inline +bool ptr_eq(const volatile void *a, const volatile void *b) +{ + bool ret; + asm(__ASM_SIZE(cmp) " %1,%2" : "=@ccz" (ret) : "r" (a), "r" (b)); + return ret; +} + +#endif /* _ASM_X86_PTREQ_H */ diff --git a/include/asm-generic/Kbuild b/include/asm-generic/Kbuild index 2bc00c67dc54..e1d95dea09d5 100644 --- a/include/asm-generic/Kbuild +++ b/include/asm-generic/Kbuild @@ -47,6 +47,7 @@ mandatory-y += percpu.h mandatory-y += percpu_types.h mandatory-y += pgalloc.h mandatory-y += preempt.h +mandatory-y += ptreq.h mandatory-y += rqspinlock.h mandatory-y += runtime-const.h mandatory-y += rwonce.h diff --git a/include/asm-generic/ptreq.h b/include/asm-generic/ptreq.h new file mode 100644 index 000000000000..db5a88628b1e --- /dev/null +++ b/include/asm-generic/ptreq.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ASM_GENERIC_PTREQ_H +#define __ASM_GENERIC_PTREQ_H + +#include <linux/types.h> +#include <linux/compiler.h> + +static __always_inline +bool ptr_eq(const volatile void *a, const volatile void *b) +{ + OPTIMIZER_HIDE_VAR(a); + OPTIMIZER_HIDE_VAR(b); + return a == b; +} + +#endif /* __ASM_GENERIC_PTREQ_H */ diff --git a/include/linux/ptreq.h b/include/linux/ptreq.h new file mode 100644 index 000000000000..5499f65da30a --- /dev/null +++ b/include/linux/ptreq.h @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PTREQ_H +#define _LINUX_PTREQ_H + +/* + * 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. + */ + +#include <asm/ptreq.h> + +#endif /* _LINUX_PTREQ_H */ -- 2.43.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH hazptr v2 2/4] ptreq.h: Introduce ptr_eq() to preserve address dependency 2026-10-09 18:21 ` [PATCH hazptr v2 2/4] ptreq.h: Introduce ptr_eq() to preserve address dependency Mathieu Desnoyers @ 2026-10-09 19:03 ` Boqun Feng 2026-10-09 19:07 ` Mathieu Desnoyers 2026-10-09 21:55 ` Gary Guo 1 sibling, 1 reply; 6+ messages in thread From: Boqun Feng @ 2026-10-09 19:03 UTC (permalink / raw) To: Mathieu Desnoyers Cc: Paul E . McKenney, linux-kernel, Linus Torvalds, Boqun Feng, Greg Kroah-Hartman, Sebastian Andrzej Siewior, Will Deacon, Peter Zijlstra, Alan Stern, John Stultz, 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, Mateusz Guzik, Gary Guo, rcu, linux-mm, lkmm, Nikita Popov, llvm, Lian Wang, Kunwu Chan On Fri, Oct 09, 2026 at 02:21:33PM -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 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. > > The header "linux/ptreq.h" is the expected include target. It > contains the documentation of the ptr_eq() API. > > An architecture specific implementation of the pointer comparison > can be implemented by each architecture as asm/ptreq.h. The x86 > implementation is provided initially. If no implementation header is > present for the architecture, an arch-agnostic fallback based on > OPTIMIZER_HIDE_VAR is included from asm-generic. > > Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> > Suggested-by: Boqun Feng <boqun.feng@gmail.com> > Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> > Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> > Cc: "Paul E. McKenney" <paulmck@kernel.org> > Cc: Will Deacon <will@kernel.org> > Cc: Peter Zijlstra <peterz@infradead.org> > Cc: Boqun Feng <boqun.feng@gmail.com> > Cc: Alan Stern <stern@rowland.harvard.edu> > Cc: John Stultz <jstultz@google.com> > Cc: Linus Torvalds <torvalds@linux-foundation.org> > Cc: Boqun Feng <boqun.feng@gmail.com> > Cc: Frederic Weisbecker <frederic@kernel.org> > Cc: Joel Fernandes <joel@joelfernandes.org> > Cc: Josh Triplett <josh@joshtriplett.org> > Cc: Uladzislau Rezki <urezki@gmail.com> > Cc: Steven Rostedt <rostedt@goodmis.org> > Cc: Lai Jiangshan <jiangshanlai@gmail.com> > Cc: Zqiang <qiang.zhang1211@gmail.com> > Cc: Ingo Molnar <mingo@redhat.com> > Cc: Waiman Long <longman@redhat.com> > Cc: Mark Rutland <mark.rutland@arm.com> > Cc: Thomas Gleixner <tglx@linutronix.de> > Cc: Vlastimil Babka <vbabka@suse.cz> > Cc: maged.michael@gmail.com > Cc: Mateusz Guzik <mjguzik@gmail.com> > Cc: Gary Guo <gary@garyguo.net> > Cc: rcu@vger.kernel.org > Cc: linux-mm@kvack.org > Cc: lkmm@lists.linux.dev > Cc: Nikita Popov <github@npopov.com> > Cc: llvm@lists.linux.dev > Cc: Lian Wang <lianux.mm@gmail.com> > Cc: Kunwu Chan <kunwu.chan@gmail.com> > --- > Changes since v1: > - Move to linux/ptreq.h, implement x86-specific comparison I prefer the name ptr_eq.h over ptreq.h :) With that name fix or there is a strong reason of ptreq.h: Reviewed-by: Boqun Feng <boqun@kernel.org> Regards, Boqun > and asm-generic header (fallback). > > Changes since v0: > - Include feedback from Alan Stern. > --- > arch/x86/include/asm/ptreq.h | 16 +++++++++ > include/asm-generic/Kbuild | 1 + > include/asm-generic/ptreq.h | 16 +++++++++ > include/linux/ptreq.h | 63 ++++++++++++++++++++++++++++++++++++ > 4 files changed, 96 insertions(+) > create mode 100644 arch/x86/include/asm/ptreq.h > create mode 100644 include/asm-generic/ptreq.h > create mode 100644 include/linux/ptreq.h > > diff --git a/arch/x86/include/asm/ptreq.h b/arch/x86/include/asm/ptreq.h > new file mode 100644 > index 000000000000..593b45f80451 > --- /dev/null > +++ b/arch/x86/include/asm/ptreq.h > @@ -0,0 +1,16 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef _ASM_X86_PTREQ_H > +#define _ASM_X86_PTREQ_H > + > +#include <linux/types.h> > +#include <asm/asm.h> > + > +static __always_inline > +bool ptr_eq(const volatile void *a, const volatile void *b) > +{ > + bool ret; > + asm(__ASM_SIZE(cmp) " %1,%2" : "=@ccz" (ret) : "r" (a), "r" (b)); > + return ret; > +} > + > +#endif /* _ASM_X86_PTREQ_H */ > diff --git a/include/asm-generic/Kbuild b/include/asm-generic/Kbuild > index 2bc00c67dc54..e1d95dea09d5 100644 > --- a/include/asm-generic/Kbuild > +++ b/include/asm-generic/Kbuild > @@ -47,6 +47,7 @@ mandatory-y += percpu.h > mandatory-y += percpu_types.h > mandatory-y += pgalloc.h > mandatory-y += preempt.h > +mandatory-y += ptreq.h > mandatory-y += rqspinlock.h > mandatory-y += runtime-const.h > mandatory-y += rwonce.h > diff --git a/include/asm-generic/ptreq.h b/include/asm-generic/ptreq.h > new file mode 100644 > index 000000000000..db5a88628b1e > --- /dev/null > +++ b/include/asm-generic/ptreq.h > @@ -0,0 +1,16 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef __ASM_GENERIC_PTREQ_H > +#define __ASM_GENERIC_PTREQ_H > + > +#include <linux/types.h> > +#include <linux/compiler.h> > + > +static __always_inline > +bool ptr_eq(const volatile void *a, const volatile void *b) > +{ > + OPTIMIZER_HIDE_VAR(a); > + OPTIMIZER_HIDE_VAR(b); > + return a == b; > +} > + > +#endif /* __ASM_GENERIC_PTREQ_H */ > diff --git a/include/linux/ptreq.h b/include/linux/ptreq.h > new file mode 100644 > index 000000000000..5499f65da30a > --- /dev/null > +++ b/include/linux/ptreq.h > @@ -0,0 +1,63 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef _LINUX_PTREQ_H > +#define _LINUX_PTREQ_H > + > +/* > + * 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. > + */ > + > +#include <asm/ptreq.h> > + > +#endif /* _LINUX_PTREQ_H */ > -- > 2.43.0 > ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH hazptr v2 2/4] ptreq.h: Introduce ptr_eq() to preserve address dependency 2026-10-09 19:03 ` Boqun Feng @ 2026-10-09 19:07 ` Mathieu Desnoyers 0 siblings, 0 replies; 6+ messages in thread From: Mathieu Desnoyers @ 2026-10-09 19:07 UTC (permalink / raw) To: Boqun Feng Cc: Paul E . McKenney, linux-kernel, Linus Torvalds, Boqun Feng, Greg Kroah-Hartman, Sebastian Andrzej Siewior, Will Deacon, Peter Zijlstra, Alan Stern, John Stultz, 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, Mateusz Guzik, Gary Guo, rcu, linux-mm, lkmm, Nikita Popov, llvm, Lian Wang, Kunwu Chan On 2026-10-09 15:03, Boqun Feng wrote: > On Fri, Oct 09, 2026 at 02:21:33PM -0400, Mathieu Desnoyers wrote: [...] >> >> An architecture specific implementation of the pointer comparison >> can be implemented by each architecture as asm/ptreq.h. The x86 >> implementation is provided initially. If no implementation header is >> present for the architecture, an arch-agnostic fallback based on >> OPTIMIZER_HIDE_VAR is included from asm-generic. >> [...] > > I prefer the name ptr_eq.h over ptreq.h :) > > With that name fix or there is a strong reason of ptreq.h: > > Reviewed-by: Boqun Feng <boqun@kernel.org> I took rwonce.h (implementing READ_ONCE()/WRITE_ONCE()) as an inspiration for the file name. I'm open to change it to whatever people think best. I have no strong preference for either ptreq.h or ptr_eq.h. I'll let people chime in before respinning an updated series if a file name change is needed. Thanks! Mathieu -- Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH hazptr v2 2/4] ptreq.h: Introduce ptr_eq() to preserve address dependency 2026-10-09 18:21 ` [PATCH hazptr v2 2/4] ptreq.h: Introduce ptr_eq() to preserve address dependency Mathieu Desnoyers 2026-10-09 19:03 ` Boqun Feng @ 2026-10-09 21:55 ` Gary Guo 2026-10-11 19:42 ` Mathieu Desnoyers 1 sibling, 1 reply; 6+ messages in thread From: Gary Guo @ 2026-10-09 21:55 UTC (permalink / raw) To: Mathieu Desnoyers, Paul E . McKenney Cc: linux-kernel, Linus Torvalds, Boqun Feng, Greg Kroah-Hartman, Sebastian Andrzej Siewior, Will Deacon, Peter Zijlstra, Alan Stern, John Stultz, 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, Mateusz Guzik, Gary Guo, rcu, linux-mm, lkmm, Nikita Popov, llvm, Lian Wang, Kunwu Chan On Fri Oct 9, 2026 at 7:21 PM BST, 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 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. > > The header "linux/ptreq.h" is the expected include target. It > contains the documentation of the ptr_eq() API. > > An architecture specific implementation of the pointer comparison > can be implemented by each architecture as asm/ptreq.h. The x86 > implementation is provided initially. If no implementation header is > present for the architecture, an arch-agnostic fallback based on > OPTIMIZER_HIDE_VAR is included from asm-generic. > > Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> > Suggested-by: Boqun Feng <boqun.feng@gmail.com> > Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> > Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> > Cc: "Paul E. McKenney" <paulmck@kernel.org> > Cc: Will Deacon <will@kernel.org> > Cc: Peter Zijlstra <peterz@infradead.org> > Cc: Boqun Feng <boqun.feng@gmail.com> > Cc: Alan Stern <stern@rowland.harvard.edu> > Cc: John Stultz <jstultz@google.com> > Cc: Linus Torvalds <torvalds@linux-foundation.org> > Cc: Boqun Feng <boqun.feng@gmail.com> > Cc: Frederic Weisbecker <frederic@kernel.org> > Cc: Joel Fernandes <joel@joelfernandes.org> > Cc: Josh Triplett <josh@joshtriplett.org> > Cc: Uladzislau Rezki <urezki@gmail.com> > Cc: Steven Rostedt <rostedt@goodmis.org> > Cc: Lai Jiangshan <jiangshanlai@gmail.com> > Cc: Zqiang <qiang.zhang1211@gmail.com> > Cc: Ingo Molnar <mingo@redhat.com> > Cc: Waiman Long <longman@redhat.com> > Cc: Mark Rutland <mark.rutland@arm.com> > Cc: Thomas Gleixner <tglx@linutronix.de> > Cc: Vlastimil Babka <vbabka@suse.cz> > Cc: maged.michael@gmail.com > Cc: Mateusz Guzik <mjguzik@gmail.com> > Cc: Gary Guo <gary@garyguo.net> > Cc: rcu@vger.kernel.org > Cc: linux-mm@kvack.org > Cc: lkmm@lists.linux.dev > Cc: Nikita Popov <github@npopov.com> > Cc: llvm@lists.linux.dev > Cc: Lian Wang <lianux.mm@gmail.com> > Cc: Kunwu Chan <kunwu.chan@gmail.com> > --- > Changes since v1: > - Move to linux/ptreq.h, implement x86-specific comparison > and asm-generic header (fallback). > > Changes since v0: > - Include feedback from Alan Stern. > --- > arch/x86/include/asm/ptreq.h | 16 +++++++++ > include/asm-generic/Kbuild | 1 + > include/asm-generic/ptreq.h | 16 +++++++++ I agree with Boqun that this should be ptr_eq. There is no need to save that character :) > include/linux/ptreq.h | 63 ++++++++++++++++++++++++++++++++++++ > 4 files changed, 96 insertions(+) > create mode 100644 arch/x86/include/asm/ptreq.h > create mode 100644 include/asm-generic/ptreq.h > create mode 100644 include/linux/ptreq.h > > diff --git a/arch/x86/include/asm/ptreq.h b/arch/x86/include/asm/ptreq.h > new file mode 100644 > index 000000000000..593b45f80451 > --- /dev/null > +++ b/arch/x86/include/asm/ptreq.h > @@ -0,0 +1,16 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef _ASM_X86_PTREQ_H > +#define _ASM_X86_PTREQ_H > + > +#include <linux/types.h> > +#include <asm/asm.h> > + > +static __always_inline > +bool ptr_eq(const volatile void *a, const volatile void *b) > +{ > + bool ret; > + asm(__ASM_SIZE(cmp) " %1,%2" : "=@ccz" (ret) : "r" (a), "r" (b)); > + return ret; > +} > + > +#endif /* _ASM_X86_PTREQ_H */ > diff --git a/include/asm-generic/Kbuild b/include/asm-generic/Kbuild > index 2bc00c67dc54..e1d95dea09d5 100644 > --- a/include/asm-generic/Kbuild > +++ b/include/asm-generic/Kbuild > @@ -47,6 +47,7 @@ mandatory-y += percpu.h > mandatory-y += percpu_types.h > mandatory-y += pgalloc.h > mandatory-y += preempt.h > +mandatory-y += ptreq.h > mandatory-y += rqspinlock.h > mandatory-y += runtime-const.h > mandatory-y += rwonce.h > diff --git a/include/asm-generic/ptreq.h b/include/asm-generic/ptreq.h > new file mode 100644 > index 000000000000..db5a88628b1e > --- /dev/null > +++ b/include/asm-generic/ptreq.h > @@ -0,0 +1,16 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +#ifndef __ASM_GENERIC_PTREQ_H > +#define __ASM_GENERIC_PTREQ_H > + > +#include <linux/types.h> > +#include <linux/compiler.h> > + > +static __always_inline > +bool ptr_eq(const volatile void *a, const volatile void *b) I suppose this is `const volatile` to take advantage of implicit conversion that adds cv-qualifiers to be added implicitly. Might worth a mention in somewhere (commit message maybe) because I was confused for a second on why this needs to be volatile. > +{ > + OPTIMIZER_HIDE_VAR(a); > + OPTIMIZER_HIDE_VAR(b); > + return a == b; > +} One thought on inline assembly. For RISC-V, branching with comparison is a single instruction, but there's no instruction for obtaining the boolean for a == b. One option is to do static __always_inline bool ptr_eq_likely(const volatile void *a, const volatile void *b) { asm goto ("bne %0, %1, %l2" : : "r" (a), "r" (b) :: not_eq); return true; not_eq: return likely(false); } and then the optimizer can turn a ptr_eq call + a branch to a single instruction. This, however, would require likely + unlikely variants. Alternatively, we can just not have all the complexity, and just emit one extra instruction: static __always_inline bool ptr_eq(const volatile void *a, const volatile void *b) { unsigned long diff; asm("xor %0, %1, %2" : "=r" (diff) : "r" (a), "r" (b)); return diff == 0; } Thoughts? Best, Gary ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH hazptr v2 2/4] ptreq.h: Introduce ptr_eq() to preserve address dependency 2026-10-09 21:55 ` Gary Guo @ 2026-10-11 19:42 ` Mathieu Desnoyers 0 siblings, 0 replies; 6+ messages in thread From: Mathieu Desnoyers @ 2026-10-11 19:42 UTC (permalink / raw) To: Gary Guo, Paul E . McKenney Cc: linux-kernel, Linus Torvalds, Boqun Feng, Greg Kroah-Hartman, Sebastian Andrzej Siewior, Will Deacon, Peter Zijlstra, Alan Stern, John Stultz, 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, Mateusz Guzik, rcu, linux-mm, lkmm, Nikita Popov, llvm, Lian Wang, Kunwu Chan On 2026-10-09 17:55, Gary Guo wrote: > On Fri Oct 9, 2026 at 7:21 PM BST, 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 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. >> >> The header "linux/ptreq.h" is the expected include target. It >> contains the documentation of the ptr_eq() API. >> >> An architecture specific implementation of the pointer comparison >> can be implemented by each architecture as asm/ptreq.h. The x86 >> implementation is provided initially. If no implementation header is >> present for the architecture, an arch-agnostic fallback based on >> OPTIMIZER_HIDE_VAR is included from asm-generic. >> >> Suggested-by: Linus Torvalds <torvalds@linux-foundation.org> >> Suggested-by: Boqun Feng <boqun.feng@gmail.com> >> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> >> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> >> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> >> Cc: "Paul E. McKenney" <paulmck@kernel.org> >> Cc: Will Deacon <will@kernel.org> >> Cc: Peter Zijlstra <peterz@infradead.org> >> Cc: Boqun Feng <boqun.feng@gmail.com> >> Cc: Alan Stern <stern@rowland.harvard.edu> >> Cc: John Stultz <jstultz@google.com> >> Cc: Linus Torvalds <torvalds@linux-foundation.org> >> Cc: Boqun Feng <boqun.feng@gmail.com> >> Cc: Frederic Weisbecker <frederic@kernel.org> >> Cc: Joel Fernandes <joel@joelfernandes.org> >> Cc: Josh Triplett <josh@joshtriplett.org> >> Cc: Uladzislau Rezki <urezki@gmail.com> >> Cc: Steven Rostedt <rostedt@goodmis.org> >> Cc: Lai Jiangshan <jiangshanlai@gmail.com> >> Cc: Zqiang <qiang.zhang1211@gmail.com> >> Cc: Ingo Molnar <mingo@redhat.com> >> Cc: Waiman Long <longman@redhat.com> >> Cc: Mark Rutland <mark.rutland@arm.com> >> Cc: Thomas Gleixner <tglx@linutronix.de> >> Cc: Vlastimil Babka <vbabka@suse.cz> >> Cc: maged.michael@gmail.com >> Cc: Mateusz Guzik <mjguzik@gmail.com> >> Cc: Gary Guo <gary@garyguo.net> >> Cc: rcu@vger.kernel.org >> Cc: linux-mm@kvack.org >> Cc: lkmm@lists.linux.dev >> Cc: Nikita Popov <github@npopov.com> >> Cc: llvm@lists.linux.dev >> Cc: Lian Wang <lianux.mm@gmail.com> >> Cc: Kunwu Chan <kunwu.chan@gmail.com> >> --- >> Changes since v1: >> - Move to linux/ptreq.h, implement x86-specific comparison >> and asm-generic header (fallback). >> >> Changes since v0: >> - Include feedback from Alan Stern. >> --- >> arch/x86/include/asm/ptreq.h | 16 +++++++++ >> include/asm-generic/Kbuild | 1 + >> include/asm-generic/ptreq.h | 16 +++++++++ > > I agree with Boqun that this should be ptr_eq. There is no need to save that > character :) > >> include/linux/ptreq.h | 63 ++++++++++++++++++++++++++++++++++++ >> 4 files changed, 96 insertions(+) >> create mode 100644 arch/x86/include/asm/ptreq.h >> create mode 100644 include/asm-generic/ptreq.h >> create mode 100644 include/linux/ptreq.h >> >> diff --git a/arch/x86/include/asm/ptreq.h b/arch/x86/include/asm/ptreq.h >> new file mode 100644 >> index 000000000000..593b45f80451 >> --- /dev/null >> +++ b/arch/x86/include/asm/ptreq.h >> @@ -0,0 +1,16 @@ >> +/* SPDX-License-Identifier: GPL-2.0 */ >> +#ifndef _ASM_X86_PTREQ_H >> +#define _ASM_X86_PTREQ_H >> + >> +#include <linux/types.h> >> +#include <asm/asm.h> >> + >> +static __always_inline >> +bool ptr_eq(const volatile void *a, const volatile void *b) >> +{ >> + bool ret; >> + asm(__ASM_SIZE(cmp) " %1,%2" : "=@ccz" (ret) : "r" (a), "r" (b)); >> + return ret; >> +} >> + >> +#endif /* _ASM_X86_PTREQ_H */ >> diff --git a/include/asm-generic/Kbuild b/include/asm-generic/Kbuild >> index 2bc00c67dc54..e1d95dea09d5 100644 >> --- a/include/asm-generic/Kbuild >> +++ b/include/asm-generic/Kbuild >> @@ -47,6 +47,7 @@ mandatory-y += percpu.h >> mandatory-y += percpu_types.h >> mandatory-y += pgalloc.h >> mandatory-y += preempt.h >> +mandatory-y += ptreq.h >> mandatory-y += rqspinlock.h >> mandatory-y += runtime-const.h >> mandatory-y += rwonce.h >> diff --git a/include/asm-generic/ptreq.h b/include/asm-generic/ptreq.h >> new file mode 100644 >> index 000000000000..db5a88628b1e >> --- /dev/null >> +++ b/include/asm-generic/ptreq.h >> @@ -0,0 +1,16 @@ >> +/* SPDX-License-Identifier: GPL-2.0 */ >> +#ifndef __ASM_GENERIC_PTREQ_H >> +#define __ASM_GENERIC_PTREQ_H >> + >> +#include <linux/types.h> >> +#include <linux/compiler.h> >> + >> +static __always_inline >> +bool ptr_eq(const volatile void *a, const volatile void *b) > > I suppose this is `const volatile` to take advantage of implicit conversion that > adds cv-qualifiers to be added implicitly. Might worth a mention in somewhere > (commit message maybe) because I was confused for a second on why this needs > to be volatile. > >> +{ >> + OPTIMIZER_HIDE_VAR(a); >> + OPTIMIZER_HIDE_VAR(b); >> + return a == b; >> +} > > One thought on inline assembly. For RISC-V, branching with comparison is a > single instruction, but there's no instruction for obtaining the boolean for > a == b. > > One option is to do > > static __always_inline > bool ptr_eq_likely(const volatile void *a, const volatile void *b) { > asm goto ("bne %0, %1, %l2" : : "r" (a), "r" (b) :: not_eq); > return true; > not_eq: > return likely(false); > } > > and then the optimizer can turn a ptr_eq call + a branch to a single > instruction. This, however, would require likely + unlikely variants. > > Alternatively, we can just not have all the complexity, and just emit one extra > instruction: > > static __always_inline > bool ptr_eq(const volatile void *a, const volatile void *b) { > unsigned long diff; > asm("xor %0, %1, %2" : "=r" (diff) : "r" (a), "r" (b)); > return diff == 0; > } > > Thoughts? If architectures end up needing the likely/unlikely variants we can do that if it saves instructions on the hot path. How about we do this change when we introduce optimized support for riscv ? Thanks, Mathieu -- Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH hazptr v2 3/4] Documentation: RCU: Refer to ptr_eq() [not found] <20261009182142.6311-1-mathieu.desnoyers@efficios.com> 2026-10-09 18:21 ` [PATCH hazptr v2 2/4] ptreq.h: Introduce ptr_eq() to preserve address dependency Mathieu Desnoyers @ 2026-10-09 18:21 ` Mathieu Desnoyers 1 sibling, 0 replies; 6+ messages in thread From: Mathieu Desnoyers @ 2026-10-09 18:21 UTC (permalink / raw) To: Paul E . McKenney Cc: linux-kernel, Mathieu Desnoyers, Alan Stern, Joel Fernandes, Greg Kroah-Hartman, Sebastian Andrzej Siewior, Will Deacon, Peter Zijlstra, Boqun Feng, John Stultz, Linus Torvalds, Frederic Weisbecker, Josh Triplett, Uladzislau Rezki, Steven Rostedt, Lai Jiangshan, Zqiang, Ingo Molnar, Waiman Long, Mark Rutland, Thomas Gleixner, Vlastimil Babka, maged.michael, Mateusz Guzik, Gary Guo, rcu, linux-mm, lkmm, Nikita Popov, llvm, Lian Wang, Kunwu Chan 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 <mathieu.desnoyers@efficios.com> Acked-by: Alan Stern <stern@rowland.harvard.edu> Acked-by: Paul E. McKenney <paulmck@kernel.org> Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Cc: "Paul E. McKenney" <paulmck@kernel.org> Cc: Will Deacon <will@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Alan Stern <stern@rowland.harvard.edu> Cc: John Stultz <jstultz@google.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Frederic Weisbecker <frederic@kernel.org> Cc: Joel Fernandes <joel@joelfernandes.org> Cc: Josh Triplett <josh@joshtriplett.org> Cc: Uladzislau Rezki <urezki@gmail.com> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Lai Jiangshan <jiangshanlai@gmail.com> Cc: Zqiang <qiang.zhang1211@gmail.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Waiman Long <longman@redhat.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: maged.michael@gmail.com Cc: Mateusz Guzik <mjguzik@gmail.com> Cc: Gary Guo <gary@garyguo.net> Cc: rcu@vger.kernel.org Cc: linux-mm@kvack.org Cc: lkmm@lists.linux.dev Cc: Nikita Popov <github@npopov.com> Cc: llvm@lists.linux.dev Cc: Lian Wang <lianux.mm@gmail.com> Cc: Kunwu Chan <kunwu.chan@gmail.com> --- Changes since v1: - Include feedback from Paul E. McKenney. Changes since v0: - Include feedback from Alan Stern. --- 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 5bc3785ebfc2..85b5f79ebf14 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 -- 2.43.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-10-11 19:42 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20261009182142.6311-1-mathieu.desnoyers@efficios.com>
2026-10-09 18:21 ` [PATCH hazptr v2 2/4] ptreq.h: Introduce ptr_eq() to preserve address dependency Mathieu Desnoyers
2026-10-09 19:03 ` Boqun Feng
2026-10-09 19:07 ` Mathieu Desnoyers
2026-10-09 21:55 ` Gary Guo
2026-10-11 19:42 ` Mathieu Desnoyers
2026-10-09 18:21 ` [PATCH hazptr v2 3/4] Documentation: RCU: Refer to ptr_eq() Mathieu Desnoyers
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).