Skip to contentSkip to conversation

Hazard pointer updates

PATCH In review Mathieu Desnoyers 5 participants 18 messages Oct 11 – Oct 27

Subsystems (6)
lore
Share & tools
lore

Mathieu Desnoyers

author reviewer
#0 lore raw

v3 cover Hazard pointer updates

Hi Paul,

This series applies on top of "hazptr: handle NULL address in hazptr_detach" you have in your rcu dev tree.

This first patch addresses a race identified by Boqun Feng in the two-phase wildcard scheme. (no change since the last series)

Patches 2-3 are prerequisites for using ptr_eq() in the 4th patch. Those were discussed at length in a prior version of hazard pointer patches.

Since the last series, I have renamed ptreq.h to ptr_eq.h, addressing the feedback received from Boqun and Gary.

Patch 4 introduces a "try acquire" helper to allow the fast path to not rely on wildcards, while keeping the wildcard forward progress guarantees in the acquire slow path, used on fast path failure.

Thanks,

Mathieu

Mathieu Desnoyers (4):
hazptr: Fix two-phase hazptr_synchronize race with detach ptr_eq.h: Introduce ptr_eq() to preserve address dependency
Documentation: RCU: Refer to ptr_eq()
hazptr: Introduce "try acquire" fast path, fallback to overflow list

Cc Paul E. McKenney <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Bradley Morgan <[email protected]>
Cc Gary Guo <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>
 Documentation/RCU/rcu_dereference.rst |  38 +++++++-
 arch/x86/include/asm/ptr_eq.h         |  16 +++
 include/asm-generic/Kbuild            |   1 +
 include/asm-generic/ptr_eq.h          |  16 +++
 include/linux/hazptr.h                |  48 +++++----
 include/linux/ptr_eq.h                |  63 ++++++++++++
 kernel/hazptr.c                       | 135 +++++++++++++++-----------
 7 files changed, 237 insertions(+), 80 deletions(-)
 create mode 100644 arch/x86/include/asm/ptr_eq.h
 create mode 100644 include/asm-generic/ptr_eq.h
 create mode 100644 include/linux/ptr_eq.h
Signature · 1 line
2.43.0
author reviewer
#1 lore raw

PATCH v3 1/4 hazptr: Fix two-phase hazptr_synchronize race with detach

Boqun Feng pointed out that a detach happening concurrently with hazptr_synchronize can miss a slot. Indeed, scanning the per-CPU slots needs to be done *before* scanning the overflow lists. Fix the implementation accordingly.

---
Changes since v0:
- Load the new overflow list phase word in "hazptr_chain_backup_slot".
- Rename the lock to hazptr_phase_lock to make it clear that it protects both the wildcard and the overflow list phases.

---
 kernel/hazptr.c | 50 +++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 40 insertions(+), 10 deletions(-)
kernel/hazptr.c +40 -10
index d3d1050d92cf..13faa5ba7677 100644
@@ -13,6 +13,8 @@
 #include <linux/list.h>
 #include <linux/export.h>
 
+static DEFINE_MUTEX(hazptr_phase_lock);	/* Protect the wildcard and list phase flip. */
+
 /*
  * The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee
  * hazptr_synchronize forward progress even with a steady stream of readers.
@@ -20,10 +22,12 @@
  * This also affects the overflow list selection: the current list used by
  * readers is array[(unsigned long) hazptr_wildcard - 1].
  */
-static DEFINE_MUTEX(hazptr_wildcard_lock);	/* Protect the wildcard flip. */
 void *hazptr_wildcard = (void *) 1UL;
 EXPORT_SYMBOL_GPL(hazptr_wildcard);
 
+/* The current overflow list phase. */
+static unsigned int hazptr_overflow_list_phase;
+
 struct hazptr_overflow_list {
 	raw_spinlock_t lock;		/* Lock protecting overflow list and list generation. */
 	struct hlist_head head;		/* Overflow list head. */
@@ -53,6 +57,12 @@ void *flip_wildcard(void *wildcard)
 	return ((unsigned long) wildcard == 1UL) ? (void *) 2UL : (void *) 1UL;
 }
 
+static
+unsigned int flip_list_phase(unsigned int phase)
+{
+	return 1 - phase;
+}
+
 static
 bool is_wildcard(void *addr)
 {
@@ -178,15 +188,12 @@ void hazptr_synchronize_cpu_slots(int cpu, void *addr, void *scan_wildcard)
 }
 
 static
-void hazptr_scan_period(void *addr, void *scan_wildcard)
+void hazptr_scan_cpu_slots_period(void *addr, void *scan_wildcard)
 {
-	unsigned int scan_idx = (unsigned long) scan_wildcard - 1;
 	int cpu;
 
 	/* Scan all CPUs slots. */
 	for_each_possible_cpu(cpu) {
-		struct hazptr_overflow_list_flip *overflow_list_flip = per_cpu_ptr(&percpu_overflow_list_flip, cpu);
-
 		/*
 		 * Scan CPU slots.
 		 * Forward progress against recurring wildcards is guaranteed
@@ -199,6 +206,17 @@ void hazptr_scan_period(void *addr, void *scan_wildcard)
 		 * to acquire that same hazard pointer value.
 		 */
 		hazptr_synchronize_cpu_slots(cpu, addr, scan_wildcard);
+	}
+}
+
+static
+void hazptr_scan_overflow_list_period(void *addr, unsigned int scan_idx)
+{
+	int cpu;
+
+	/* Scan all CPUs overflow lists. */
+	for_each_possible_cpu(cpu) {
+		struct hazptr_overflow_list_flip *overflow_list_flip = per_cpu_ptr(&percpu_overflow_list_flip, cpu);
 
 		/*
 		 * Scan backup slots in percpu overflow lists.
@@ -218,6 +236,7 @@ void hazptr_scan_period(void *addr, void *scan_wildcard)
  */
 void hazptr_synchronize(void *addr)
 {
+	unsigned int scan_list_phase;
 	void *scan_wildcard;
 
 	/*
@@ -235,18 +254,29 @@ void hazptr_synchronize(void *addr)
 	/* Memory ordering: Store A before Load B. */
 	smp_mb();
 
-	guard(mutex)(&hazptr_wildcard_lock);
+	guard(mutex)(&hazptr_phase_lock);
+
+	/* Scan per-CPU slots. */
 	scan_wildcard = flip_wildcard(hazptr_wildcard);
-	hazptr_scan_period(addr, scan_wildcard);
-	WRITE_ONCE(hazptr_wildcard, scan_wildcard);	/* Flip the current wildcard. */
-	hazptr_scan_period(addr, flip_wildcard(scan_wildcard));
+	hazptr_scan_cpu_slots_period(addr, scan_wildcard);
+	WRITE_ONCE(hazptr_wildcard, scan_wildcard);			/* Flip the current wildcard. */
+	hazptr_scan_cpu_slots_period(addr, flip_wildcard(scan_wildcard));
+
+	/*
+	 * Scan overflow lists *after* scanning per-CPU slots. See
+	 * hazptr_promote_to_backup_slot() for scan ordering requirement.
+	 */
+	scan_list_phase = flip_list_phase(hazptr_overflow_list_phase);
+	hazptr_scan_overflow_list_period(addr, scan_list_phase);
+	WRITE_ONCE(hazptr_overflow_list_phase, scan_list_phase);	/* Flip the current list phase. */
+	hazptr_scan_overflow_list_period(addr, flip_list_phase(scan_list_phase));
 }
 EXPORT_SYMBOL_GPL(hazptr_synchronize);
 
 struct hazptr_slot *hazptr_chain_backup_slot(struct hazptr_ctx *ctx)
 {
 	struct hazptr_overflow_list_flip *overflow_list_flip = this_cpu_ptr(&percpu_overflow_list_flip);
-	unsigned int list_idx = (unsigned long) READ_ONCE(hazptr_wildcard) - 1;
+	unsigned int list_idx = READ_ONCE(hazptr_overflow_list_phase);
 	struct hazptr_overflow_list *overflow_list = &overflow_list_flip->array[list_idx];
 	struct hazptr_slot *slot = &ctx->backup_slot.slot;
 
Signature · 1 line
2.43.0
Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH hazptr v3 1/4] hazptr: Fix two-phase hazptr_synchronize race with detach

Hi Mathieu,

Scanning all per-CPU slots before the overflow lists addresses the race where a backup slot can be moved to a list that has already been scanned.

Using a separate overflow-list phase also decouples list selection from the wildcard phase.

Thanks,
Kunwu

Quoted 146 lines · Mathieu Desnoyers

Boqun Feng pointed out that a detach happening concurrently with hazptr_synchronize can miss a slot. Indeed, scanning the per-CPU slots needs to be done *before* scanning the overflow lists. Fix the implementation accordingly.

---
Changes since v0:
- Load the new overflow list phase word in "hazptr_chain_backup_slot".
- Rename the lock to hazptr_phase_lock to make it clear that it protects both the wildcard and the overflow list phases.

---
 kernel/hazptr.c | 50 +++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 40 insertions(+), 10 deletions(-)
kernel/hazptr.c +40 -11
index d3d1050d92cf..13faa5ba7677 100644
@@ -13,6 +13,8 @@
 #include <linux/list.h>
 #include <linux/export.h>
 
+static DEFINE_MUTEX(hazptr_phase_lock);        /* Protect the wildcard and list phase flip. */
+
 /*
  * The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee
  * hazptr_synchronize forward progress even with a steady stream of readers.
@@ -20,10 +22,12 @@
  * This also affects the overflow list selection: the current list used by
  * readers is array[(unsigned long) hazptr_wildcard - 1].
  */
-static DEFINE_MUTEX(hazptr_wildcard_lock);     /* Protect the wildcard flip. */
 void *hazptr_wildcard = (void *) 1UL;
 EXPORT_SYMBOL_GPL(hazptr_wildcard);
 
+/* The current overflow list phase. */
+static unsigned int hazptr_overflow_list_phase;
+
 struct hazptr_overflow_list {
        raw_spinlock_t lock;            /* Lock protecting overflow list and list generation. */
        struct hlist_head head;         /* Overflow list head. */
@@ -53,6 +57,12 @@ void *flip_wildcard(void *wildcard)
        return ((unsigned long) wildcard == 1UL) ? (void *) 2UL : (void *) 1UL;
 }
 
+static
+unsigned int flip_list_phase(unsigned int phase)
+{
+       return 1 - phase;
+}
+
 static
 bool is_wildcard(void *addr)
 {
@@ -178,15 +188,12 @@ void hazptr_synchronize_cpu_slots(int cpu, void *addr, void *scan_wildcard)
 }
 
 static
-void hazptr_scan_period(void *addr, void *scan_wildcard)
+void hazptr_scan_cpu_slots_period(void *addr, void *scan_wildcard)
 {
-       unsigned int scan_idx = (unsigned long) scan_wildcard - 1;
        int cpu;
 
        /* Scan all CPUs slots. */
        for_each_possible_cpu(cpu) {
-               struct hazptr_overflow_list_flip *overflow_list_flip = per_cpu_ptr(&percpu_overflow_list_flip, cpu);
-
                /*
                 * Scan CPU slots.
                 * Forward progress against recurring wildcards is guaranteed
@@ -199,6 +206,17 @@ void hazptr_scan_period(void *addr, void *scan_wildcard)
                 * to acquire that same hazard pointer value.
                 */
                hazptr_synchronize_cpu_slots(cpu, addr, scan_wildcard);
+       }
+}
+
+static
+void hazptr_scan_overflow_list_period(void *addr, unsigned int scan_idx)
+{
+       int cpu;
+
+       /* Scan all CPUs overflow lists. */
+       for_each_possible_cpu(cpu) {
+               struct hazptr_overflow_list_flip *overflow_list_flip = per_cpu_ptr(&percpu_overflow_list_flip, cpu);
 
                /*
                 * Scan backup slots in percpu overflow lists.
@@ -218,6 +236,7 @@ void hazptr_scan_period(void *addr, void *scan_wildcard)
  */
 void hazptr_synchronize(void *addr)
 {
+       unsigned int scan_list_phase;
        void *scan_wildcard;
 
        /*
@@ -235,18 +254,29 @@ void hazptr_synchronize(void *addr)
        /* Memory ordering: Store A before Load B. */
        smp_mb();
 
-       guard(mutex)(&hazptr_wildcard_lock);
+       guard(mutex)(&hazptr_phase_lock);
+
+       /* Scan per-CPU slots. */
        scan_wildcard = flip_wildcard(hazptr_wildcard);
-       hazptr_scan_period(addr, scan_wildcard);
-       WRITE_ONCE(hazptr_wildcard, scan_wildcard);     /* Flip the current wildcard. */
-       hazptr_scan_period(addr, flip_wildcard(scan_wildcard));
+       hazptr_scan_cpu_slots_period(addr, scan_wildcard);
+       WRITE_ONCE(hazptr_wildcard, scan_wildcard);                     /* Flip the current wildcard. */
+       hazptr_scan_cpu_slots_period(addr, flip_wildcard(scan_wildcard));
+
+       /*
+        * Scan overflow lists *after* scanning per-CPU slots. See
+        * hazptr_promote_to_backup_slot() for scan ordering requirement.
+        */
+       scan_list_phase = flip_list_phase(hazptr_overflow_list_phase);
+       hazptr_scan_overflow_list_period(addr, scan_list_phase);
+       WRITE_ONCE(hazptr_overflow_list_phase, scan_list_phase);        /* Flip the current list phase. */
+       hazptr_scan_overflow_list_period(addr, flip_list_phase(scan_list_phase));
 }
 EXPORT_SYMBOL_GPL(hazptr_synchronize);
 
 struct hazptr_slot *hazptr_chain_backup_slot(struct hazptr_ctx *ctx)
 {
        struct hazptr_overflow_list_flip *overflow_list_flip = this_cpu_ptr(&percpu_overflow_list_flip);
-       unsigned int list_idx = (unsigned long) READ_ONCE(hazptr_wildcard) - 1;
+       unsigned int list_idx = READ_ONCE(hazptr_overflow_list_phase);
        struct hazptr_overflow_list *overflow_list = &overflow_list_flip->array[list_idx];
        struct hazptr_slot *slot = &ctx->backup_slot.slot;
 
--

2.43.0

author reviewer
#3 lore raw

Re: [PATCH hazptr v3 1/4] hazptr: Fix two-phase hazptr_synchronize race with detach

Quoted 11 lines · KunWu Chan

Hi Mathieu,

Scanning all per-CPU slots before the overflow lists addresses the race where a backup slot can be moved to a list that has already been scanned.

Using a separate overflow-list phase also decouples list selection from the wildcard phase.

Thanks for the reviews ! I've picked them up in my own local branches, but it would be best if Paul can add those 3 review tags into the commits he recently picked up.

Thanks,

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
maintainer
#4 lore raw

Re: [PATCH hazptr v3 1/4] hazptr: Fix two-phase hazptr_synchronize race with detach

Quoted 11 lines · Mathieu Desnoyers
Quoted 10 lines · KunWu Chan

Hi Mathieu,

Scanning all per-CPU slots before the overflow lists addresses the race where a backup slot can be moved to a list that has already been scanned.

Using a separate overflow-list phase also decouples list selection from the wildcard phase.

Quoted 5 lines
Quoted 1 line

Thanks for the reviews ! I've picked them up in my own local branches, but it would be best if Paul can add those 3 review tags into the commits he recently picked up.

I will apply them (1/4, 2/4, and 4/4) on my next rebase, thank you both!

Thanx, Paul

author reviewer
#5 lore raw

PATCH v3 2/4 ptr_eq.h: Introduce ptr_eq() to preserve address dependency

In reply to Mathieu Desnoyers

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/ptr_eq.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/ptr_eq.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.

The arguments to ptr_eq() are const volatile to take advantage of implicit conversion that adds cv-qualifiers, which are thus added implicitly.

Cc Greg Kroah-Hartman <[email protected]>
Cc Sebastian Andrzej Siewior <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Will Deacon <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Alan Stern <[email protected]>
Cc John Stultz <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Uladzislau Rezki <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mateusz Guzik <[email protected]>
Cc Gary Guo <[email protected]>
Cc Nikita Popov <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>

---
Changes since v2:
- Rename ptreq.h to ptr_eq.h (x86, asm-generic and linux headers). Adapt the include guards accordingly.

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/ptr_eq.h | 16 +++++++++
 include/asm-generic/Kbuild    |  1 +
 include/asm-generic/ptr_eq.h  | 16 +++++++++
 include/linux/ptr_eq.h        | 63 +++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+)
 create mode 100644 arch/x86/include/asm/ptr_eq.h
 create mode 100644 include/asm-generic/ptr_eq.h
 create mode 100644 include/linux/ptr_eq.h
arch/x86/include/asm/ptr_eq.h new file +16 -0
new file mode 100644
index 000000000000..8e521cabe24b
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_PTR_EQ_H
+#define _ASM_X86_PTR_EQ_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_PTR_EQ_H */
include/asm-generic/Kbuild +1 -0
index 2bc00c67dc54..86cee0dfd47f 100644
@@ -47,6 +47,7 @@ mandatory-y += percpu.h
 mandatory-y += percpu_types.h
 mandatory-y += pgalloc.h
 mandatory-y += preempt.h
+mandatory-y += ptr_eq.h
 mandatory-y += rqspinlock.h
 mandatory-y += runtime-const.h
 mandatory-y += rwonce.h
include/asm-generic/ptr_eq.h new file +16 -0
new file mode 100644
index 000000000000..2bd1c3b8a56f
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_GENERIC_PTR_EQ_H
+#define __ASM_GENERIC_PTR_EQ_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_PTR_EQ_H */
include/linux/ptr_eq.h new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v3 2/4
#6 lore raw

Re: [PATCH hazptr v3 2/4] ptr_eq.h: Introduce ptr_eq() to preserve address dependency

Hi Mathieu,

I checked both implementations of ptr_eq().

The x86 version uses inline asm for the comparison, so the compiler cannot reason about it as an ordinary C pointer comparison.

The generic version uses OPTIMIZER_HIDE_VAR() before the comparison to prevent the compiler from propagating the original pointer values through it. I verified this with GCC 13.3 on ARM64: the generated code uses the second load's result for the dependent access, rather than reusing the first load's result.

Thanks,
Kunwu

Quoted 220 lines · Mathieu Desnoyers

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/ptr_eq.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/ptr_eq.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.

The arguments to ptr_eq() are const volatile to take advantage of implicit conversion that adds cv-qualifiers, which are thus added implicitly.

Cc Greg Kroah-Hartman <[email protected]>
Cc Sebastian Andrzej Siewior <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Will Deacon <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Alan Stern <[email protected]>
Cc John Stultz <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Uladzislau Rezki <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mateusz Guzik <[email protected]>
Cc Gary Guo <[email protected]>
Cc Nikita Popov <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>

---
Changes since v2:
- Rename ptreq.h to ptr_eq.h (x86, asm-generic and linux headers). Adapt the include guards accordingly.

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/ptr_eq.h | 16 +++++++++
 include/asm-generic/Kbuild    |  1 +
 include/asm-generic/ptr_eq.h  | 16 +++++++++
 include/linux/ptr_eq.h        | 63 +++++++++++++++++++++++++++++++++++
 4 files changed, 96 insertions(+)
 create mode 100644 arch/x86/include/asm/ptr_eq.h
 create mode 100644 include/asm-generic/ptr_eq.h
 create mode 100644 include/linux/ptr_eq.h
arch/x86/include/asm/ptr_eq.h new file +16 -0
new file mode 100644
index 000000000000..8e521cabe24b
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_X86_PTR_EQ_H
+#define _ASM_X86_PTR_EQ_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_PTR_EQ_H */
include/asm-generic/Kbuild +1 -0
index 2bc00c67dc54..86cee0dfd47f 100644
@@ -47,6 +47,7 @@ mandatory-y += percpu.h
 mandatory-y += percpu_types.h
 mandatory-y += pgalloc.h
 mandatory-y += preempt.h
+mandatory-y += ptr_eq.h
 mandatory-y += rqspinlock.h
 mandatory-y += runtime-const.h
 mandatory-y += rwonce.h
include/asm-generic/ptr_eq.h new file +16 -0
new file mode 100644
index 000000000000..2bd1c3b8a56f
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __ASM_GENERIC_PTR_EQ_H
+#define __ASM_GENERIC_PTR_EQ_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_PTR_EQ_H */
include/linux/ptr_eq.h new file +63 -1
new file mode 100644
index 000000000000..7a2bbf6e64b3
@@ -0,0 +1,63 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_PTR_EQ_H
+#define _LINUX_PTR_EQ_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/ptr_eq.h>
+
+#endif /* _LINUX_PTR_EQ_H */
--

2.43.0

author reviewer
#7 lore raw

PATCH v3 3/4 Documentation: RCU: Refer to ptr_eq()

In reply to Mathieu Desnoyers

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.

Cc Greg Kroah-Hartman <[email protected]>
Cc Sebastian Andrzej Siewior <[email protected]>
Cc "Paul E. McKenney" <[email protected]>
Cc Will Deacon <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Alan Stern <[email protected]>
Cc John Stultz <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Joel Fernandes <[email protected]>
Cc Josh Triplett <[email protected]>
Cc Uladzislau Rezki <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Lai Jiangshan <[email protected]>
Cc Zqiang <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Waiman Long <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Mateusz Guzik <[email protected]>
Cc Gary Guo <[email protected]>
Cc Nikita Popov <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>

---
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(-)
Documentation/RCU/rcu_dereference.rst +33 -5
index 5bc3785ebfc2..85b5f79ebf14 100644
@@ -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
Signature · 1 line
2.43.0
author reviewer
#8 lore raw

PATCH v3 4/4 hazptr: Introduce "try acquire" fast path, fallback to overflow list

In reply to Mathieu Desnoyers

Introduce a "try acquire" hazard pointer fast path, which performs an early load of the address to store it into the hazard pointer slot, and then re-loads that address after a barrier to check whether it has changed meanwhile.

On comparison failure, rather than re-try, guarantee forward progress by falling back to the __hazptr_acquire slow path on failure.

The acquire slow path attempts a try-acquire for any available per-CPU slot. If that fails, it chains the backup slot into the overflow list, therefore guaranteeing forward progress for both hazard pointer read-side and synchronize:

- Readers set the wildcard, and then proceed to set the more specific address to replace the wildcard.

- One synchronize alternates between two overflow list periods, scanning each one while readers are added to the other period, thus preventing a steady flow of readers from preventing synchronize forward progress.

With this change, the scan on per-CPU slots don't need to expect a wildcard anymore, because none can be produced by readers. Wildcards are only expected within overflow lists.

---
Changes since v2:
- Adapt include to rename from ptreq.h to ptr_eq.h.

---
 include/linux/hazptr.h |  48 +++++++++++--------
 kernel/hazptr.c        | 103 ++++++++++++++++++++---------------------
 2 files changed, 77 insertions(+), 74 deletions(-)
include/linux/hazptr.h +28 -20
index d1670121947a..90d8d5a3d244 100644
@@ -25,13 +25,11 @@
 #include <linux/types.h>
 #include <linux/cleanup.h>
 #include <linux/sched.h>
+#include <linux/ptr_eq.h>
 
 /* 4 slots (each sizeof(hazptr_slot_item)) fit in a single 64-byte cache line. */
 #define NR_HAZPTR_PERCPU_SLOTS	4
 
-/* The current hazard pointer wildcard. */
-extern void *hazptr_wildcard;
-
 /*
  * Hazard pointer slot.
  */
@@ -190,6 +188,31 @@ void hazptr_note_context_switch(void)
 	}
 }
 
+/* Try hazard pointer protection. */
+static inline
+void *__hazptr_try_acquire(struct hazptr_ctx *ctx, void * const *addr_p, struct hazptr_slot *slot)
+{
+	void *early_addr, *addr;
+
+	if (unlikely(slot->addr))
+		return NULL;
+	early_addr = READ_ONCE(*addr_p);	/* Early load. */
+	WRITE_ONCE(slot->addr, early_addr);	/* Store B */
+	/* Memory ordering: Store B before Load A. */
+	smp_mb();
+	addr = READ_ONCE(*addr_p);		/* Load A */
+	/*
+	 * Validate that address did not change between Early load and Load A.
+	 * Use ptr_eq() to make sure that result from Load A is returned to the
+	 * caller to preserve address dependency.
+	 */
+	if (unlikely(!ptr_eq(addr, early_addr))) {
+		WRITE_ONCE(slot->addr, NULL);
+		return NULL;
+	}
+	return addr;
+}
+
 /**
  * hazptr_acquire - Load pointer at address and protect with hazard pointer.
  *
@@ -245,24 +268,9 @@ void *hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
 	ctx->acquire_cpu = smp_processor_id();
 	ctx->acquire_caller = _THIS_IP_;
 #endif
-	if (unlikely(slot->addr))
+	addr = __hazptr_try_acquire(ctx, addr_p, slot);
+	if (unlikely(!addr))
 		return __hazptr_acquire(ctx, addr_p);
-	WRITE_ONCE(slot->addr, READ_ONCE(hazptr_wildcard));	/* Store B */
-
-	/* Memory ordering: Store B before Load A. */
-	smp_mb();
-
-	/*
-	 * Load @addr_p after storing wildcard to the hazard pointer slot.
-	 */
-	addr = READ_ONCE(*addr_p);	/* Load A */
-
-	/*
-	 * We don't care about ordering of Store C. It will simply
-	 * replace the wildcard by a more specific address. If addr is
-	 * NULL, we simply store NULL into the slot.
-	 */
-	WRITE_ONCE(slot->addr, addr);	/* Store C */
 	slot_item->ctx.ctx = ctx;
 	ctx->slot = slot;
 	return addr;
kernel/hazptr.c +49 -54

206 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v3 4/4
#9 lore raw

Re: [PATCH hazptr v3 4/4] hazptr: Introduce "try acquire" fast path, fallback to overflow list

Quoted 21 lines · Mathieu Desnoyers

Introduce a "try acquire" hazard pointer fast path, which performs an early load of the address to store it into the hazard pointer slot, and then re-loads that address after a barrier to check whether it has changed meanwhile.

On comparison failure, rather than re-try, guarantee forward progress by falling back to the __hazptr_acquire slow path on failure.

The acquire slow path attempts a try-acquire for any available per-CPU slot. If that fails, it chains the backup slot into the overflow list, therefore guaranteeing forward progress for both hazard pointer read-side and synchronize:

- Readers set the wildcard, and then proceed to set the more specific address to replace the wildcard.

- One synchronize alternates between two overflow list periods, scanning each one while readers are added to the other period, thus preventing a steady flow of readers from preventing synchronize forward progress.

Quoted 8 lines

With this change, the scan on per-CPU slots don't need to expect a wildcard anymore, because none can be produced by readers. Wildcards are only expected within overflow lists.

Hi, my R-B still stands from

https://lore.kernel.org/all/[email protected]/

Quoted 308 lines
Cc Paul E. McKenney <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Bradley Morgan <[email protected]>
Cc Gary Guo <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>

---
Changes since v2:
- Adapt include to rename from ptreq.h to ptr_eq.h.
---
include/linux/hazptr.h | 48 +++++++++++--------
kernel/hazptr.c | 103 ++++++++++++++++++++---------------------

2 files changed, 77 insertions(+), 74 deletions(-)
include/linux/hazptr.h +1 -0
index d1670121947a..90d8d5a3d244 100644
@@ -25,13 +25,11 @@
 #include <linux/types.h>
 #include <linux/cleanup.h>
 #include <linux/sched.h>
+#include <linux/ptr_eq.h>
 
 /* 4 slots (each sizeof(hazptr_slot_item)) fit in a single 64-byte cache

line. */
#define NR_HAZPTR_PERCPU_SLOTS 4

include/linux/hazptr.h +0 -3
-/* The current hazard pointer wildcard. */
-extern void *hazptr_wildcard;
-

/*
* Hazard pointer slot.
*/

include/linux/hazptr.h +27 -17
@@ -190,6 +188,31 @@ void hazptr_note_context_switch(void)
 	}
 }
 
+/* Try hazard pointer protection. */
+static inline
+void *__hazptr_try_acquire(struct hazptr_ctx *ctx, void * const *addr_p, struct hazptr_slot *slot)
+{
+	void *early_addr, *addr;
+
+	if (unlikely(slot->addr))
+		return NULL;
+	early_addr = READ_ONCE(*addr_p);	/* Early load. */
+	WRITE_ONCE(slot->addr, early_addr);	/* Store B */
+	/* Memory ordering: Store B before Load A. */
+	smp_mb();
+	addr = READ_ONCE(*addr_p);		/* Load A */
+	/*
+	 * Validate that address did not change between Early load and Load A.
+	 * Use ptr_eq() to make sure that result from Load A is returned to the
+	 * caller to preserve address dependency.
+	 */
+	if (unlikely(!ptr_eq(addr, early_addr))) {
+		WRITE_ONCE(slot->addr, NULL);
+		return NULL;
+	}
+	return addr;
+}
+
 /**
 * hazptr_acquire - Load pointer at address and protect with hazard pointer.
 *
@@ -245,24 +268,9 @@ void *hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
 	ctx->acquire_cpu = smp_processor_id();
 	ctx->acquire_caller = _THIS_IP_;
 #endif
-	if (unlikely(slot->addr))
+	addr = __hazptr_try_acquire(ctx, addr_p, slot);
+	if (unlikely(!addr))
 		return __hazptr_acquire(ctx, addr_p);
-	WRITE_ONCE(slot->addr, READ_ONCE(hazptr_wildcard));	/* Store B */
-
-	/* Memory ordering: Store B before Load A. */
-	smp_mb();
-
-	/*
-	 * Load @addr_p after storing wildcard to the hazard pointer slot.
-	 */
-	addr = READ_ONCE(*addr_p);	/* Load A */
-
-	/*
-	 * We don't care about ordering of Store C. It will simply
-	 * replace the wildcard by a more specific address. If addr is
-	 * NULL, we simply store NULL into the slot.
-	 */
-	WRITE_ONCE(slot->addr, addr);	/* Store C */
 	slot_item->ctx.ctx = ctx;
 	ctx->slot = slot;
 	return addr;
kernel/hazptr.c +6 -16
index 13faa5ba7677..3ca73b56a5c2 100644
@@ -13,17 +13,9 @@
 #include <linux/list.h>
 #include <linux/export.h>
 
-static DEFINE_MUTEX(hazptr_phase_lock);	/* Protect the wildcard and list phase flip. */
+#define HAZPTR_WILDCARD	((void *) 1UL)
 
-/*
- * The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee
- * hazptr_synchronize forward progress even with a steady stream of readers.
- * This wildcard value is used by acquire to temporarily tag the per-CPU slots.
- * This also affects the overflow list selection: the current list used by
- * readers is array[(unsigned long) hazptr_wildcard - 1].
- */
-void *hazptr_wildcard = (void *) 1UL;
-EXPORT_SYMBOL_GPL(hazptr_wildcard);
+static DEFINE_MUTEX(hazptr_phase_lock);	/* Protect the list phase flip. */
 
 /* The current overflow list phase. */
 static unsigned int hazptr_overflow_list_phase;
@@ -41,6 +33,10 @@ struct hazptr_overflow_list {
 * successively iterates on both lists. Therefore, only list removals
 * can cause the iteration to retry, and the number of removals is
 * limited to the number of list elements.
+ *
+ * Due to the overflow list raw spin lock, the hazard pointer readers are
+ * blocking, starvation-free with bounded waiting, assuming bounded critical
+ * sections and no NMI or virtualization-induced holder preemption.
 */
 struct hazptr_overflow_list_flip {
 	struct hazptr_overflow_list array[2];
@@ -51,26 +47,12 @@ static DEFINE_PER_CPU(struct hazptr_overflow_list_flip, percpu_overflow_list_fli
 DEFINE_PER_CPU(struct hazptr_percpu_slots, hazptr_percpu_slots);
 EXPORT_PER_CPU_SYMBOL_GPL(hazptr_percpu_slots);
 
-static
-void *flip_wildcard(void *wildcard)
-{
-	return ((unsigned long) wildcard == 1UL) ? (void *) 2UL : (void *) 1UL;
-}
-

static
unsigned int flip_list_phase(unsigned int phase)

{
	return 1 - phase;
}
kernel/hazptr.c +0 -8
-static
-bool is_wildcard(void *addr)
-{
-	if ((unsigned long) addr == 1UL || (unsigned long) addr == 2UL)
-		return true;
-	return false;
-}
-

static
struct hazptr_slot *hazptr_get_free_percpu_slot(struct hazptr_ctx *ctx) {

kernel/hazptr.c +34 -13
@@ -96,16 +78,36 @@ struct hazptr_slot *hazptr_get_free_percpu_slot(struct hazptr_ctx *ctx)
 */
 void *__hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
 {
-	struct hazptr_slot *slot = hazptr_get_free_percpu_slot(ctx);
+	struct hazptr_slot *slot;
 	void *addr;
 
 	/*
-	 * If all the per-CPU slots are already in use, fallback
-	 * to the backup slot.
+	 * In case we are called due to nested use of hazard pointers,
+	 * try a slot protection with per-CPU slots.
 	 */
-	if (unlikely(!slot))
-		slot = hazptr_chain_backup_slot(ctx);
-	WRITE_ONCE(slot->addr, READ_ONCE(hazptr_wildcard));	/* Store B */
+	slot = hazptr_get_free_percpu_slot(ctx);
+	if (likely(slot)) {
+		addr = __hazptr_try_acquire(ctx, addr_p, slot);
+		if (addr) {
+			ctx->slot = slot;
+			return addr;
+		}
+	}
+
+	/*
+	 * The backup slot overflow list guarantees forward progress of both
+	 * hazard pointer readers and synchronize:
+	 *
+	 * - Readers set the wildcard, and then proceed to set the more
+	 *   specific address to replace the wildcard.
+	 *
+	 * - One synchronize alternates between two overflow list periods,
+	 *   scanning each one while readers are added to the other period,
+	 *   thus preventing a steady flow of readers from preventing
+	 *   synchronize forward progress.
+	 */
+	slot = hazptr_chain_backup_slot(ctx);
+	WRITE_ONCE(slot->addr, HAZPTR_WILDCARD);	/* Store B */
 
 	/* Memory ordering: Store B before Load A. */
 	smp_mb();
@@ -121,20 +123,21 @@ void *__hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
 	 * NULL, we simply store NULL into the slot.
 	 */
 	WRITE_ONCE(slot->addr, addr);	/* Store C */
+
 	ctx->slot = slot;
-	if (!addr && hazptr_slot_is_backup(ctx, slot))
+	if (!addr)
 		hazptr_unchain_backup_slot(ctx);
 	return addr;
 }
 EXPORT_SYMBOL_GPL(__hazptr_acquire);
 
 /*
- * Perform piecewise iteration on overflow list waiting until "addr" is
- * not present. Raw spinlock is released and taken between each list
- * item and busy loop iteration. The overflow list generation is checked
- * each time the lock is taken to validate that the list has not changed
- * before resuming iteration or busy wait. If the generation has
- * changed, retry the entire list traversal.
+ * Perform piecewise iteration on overflow list waiting until "addr" and
+ * wildcard are not present. Raw spinlock is released and taken between each
+ * list item and busy loop iteration. The overflow list generation is checked
+ * each time the lock is taken to validate that the list has not changed before
+ * resuming iteration or busy wait. If the generation has changed, retry the
+ * entire list traversal.
 */

static
void hazptr_synchronize_overflow_list(struct hazptr_overflow_list *overflow_list, void *addr)

kernel/hazptr.c +0 -0
@@ -147,13 +150,11 @@ void hazptr_synchronize_overflow_list(struct hazptr_overflow_list *overflow_list

retry:

	snapshot_gen = overflow_list->gen;
	hlist_for_each_entry(backup_slot, &overflow_list->head, overflow_node) {
kernel/hazptr.c +1 -1
-		/* Busy-wait if node is found. */
+		/* Busy-wait if addr or wildcard are found. */
		for (;;) {
			void *load_addr = smp_load_acquire(&backup_slot->slot.addr);	/* Load B */
kernel/hazptr.c +1 -3
-			/* We don't expect wildcards in overflow list. */
-			WARN_ON_ONCE(is_wildcard(load_addr));
-			if (load_addr != addr)
+			if (load_addr != addr && load_addr != HAZPTR_WILDCARD)
				break;
			raw_spin_unlock_irqrestore(&overflow_list->lock, flags);
			cpu_relax();
kernel/hazptr.c +0 -0
@@ -174,7 +175,7 @@ void hazptr_synchronize_overflow_list(struct hazptr_overflow_list *overflow_list
 }
 

static

kernel/hazptr.c +1 -1
-void hazptr_synchronize_cpu_slots(int cpu, void *addr, void *scan_wildcard)
+void hazptr_synchronize_cpu_slots(int cpu, void *addr)
{
	struct hazptr_percpu_slots *percpu_slots = per_cpu_ptr(&hazptr_percpu_slots, cpu);
	unsigned int idx;
kernel/hazptr.c +2 -2
@@ -182,13 +183,13 @@ void hazptr_synchronize_cpu_slots(int cpu, void *addr, void *scan_wildcard)
 	for (idx = 0; idx < NR_HAZPTR_PERCPU_SLOTS; idx++) {
 		struct hazptr_slot_item *item = &percpu_slots->items[idx];
 
-		/* Busy-wait if node is found. */
-		smp_cond_load_acquire(&item->slot.addr, VAL != addr && VAL != scan_wildcard); /* Load B */
+		/* Busy-wait if addr is found. */
+		smp_cond_load_acquire(&item->slot.addr, VAL != addr); /* Load B */
 	}
 }
 

static

kernel/hazptr.c +1 -1
-void hazptr_scan_cpu_slots_period(void *addr, void *scan_wildcard)
+void hazptr_scan_cpu_slots(void *addr)
{
	int cpu;
kernel/hazptr.c +2 -5
@@ -196,16 +197,13 @@ void hazptr_scan_cpu_slots_period(void *addr, void *scan_wildcard)
 	for_each_possible_cpu(cpu) {
 		/*
 		 * Scan CPU slots.
-		 * Forward progress against recurring wildcards is guaranteed
-		 * by scanning for one wildcard while new elements use the
-		 * other wildcard value (1UL vs 2UL).
 		 * Forward progress against recurring single hazard pointer
 		 * values is guaranteed by the fact that a hazard pointer
 		 * is not reclaimed nor reused until the scan for that hazard
 		 * pointer completes, which prevents a steady flow of readers
 		 * to acquire that same hazard pointer value.
 		 */
-		hazptr_synchronize_cpu_slots(cpu, addr, scan_wildcard);
+		hazptr_synchronize_cpu_slots(cpu, addr);
 	}
 }
 
@@ -237,7 +235,6 @@ void hazptr_scan_overflow_list_period(void *addr, unsigned int scan_idx)
 void hazptr_synchronize(void *addr)
 {
 	unsigned int scan_list_phase;
-	void *scan_wildcard;
 
 	/*
 	 * Busy-wait should only be done from preemptible context.
@@ -251,16 +248,14 @@ void hazptr_synchronize(void *addr)
 	 */
 	if (!addr)
 		return;
+
 	/* Memory ordering: Store A before Load B. */
 	smp_mb();
 
 	guard(mutex)(&hazptr_phase_lock);

kernel/hazptr.c void hazptr_synchronize(void *addr) · line 261

Quoted 10 lines
kernel/hazptr.c +1 -4
 
 	/* Scan per-CPU slots. */
-	scan_wildcard = flip_wildcard(hazptr_wildcard);
-	hazptr_scan_cpu_slots_period(addr, scan_wildcard);
-	WRITE_ONCE(hazptr_wildcard, scan_wildcard);			/* Flip the current wildcard. */
-	hazptr_scan_cpu_slots_period(addr, flip_wildcard(scan_wildcard));
+	hazptr_scan_cpu_slots(addr);
 
 	/*
 	 * Scan overflow lists *after* scanning per-CPU slots. See

--- Thanks!
"I'm not a very positive person" - Linus torvalds

author reviewer
#10 lore raw

Re: [PATCH hazptr v3 4/4] hazptr: Introduce "try acquire" fast path, fallback to overflow list

Quoted 32 lines · Bradley Morgan
Quoted 21 lines · Mathieu Desnoyers

Introduce a "try acquire" hazard pointer fast path, which performs an early load of the address to store it into the hazard pointer slot, and then re-loads that address after a barrier to check whether it has changed meanwhile.

On comparison failure, rather than re-try, guarantee forward progress by falling back to the __hazptr_acquire slow path on failure.

The acquire slow path attempts a try-acquire for any available per-CPU slot. If that fails, it chains the backup slot into the overflow list, therefore guaranteeing forward progress for both hazard pointer read-side and synchronize:

- Readers set the wildcard, and then proceed to set the more specific address to replace the wildcard.

- One synchronize alternates between two overflow list periods, scanning each one while readers are added to the other period, thus preventing a steady flow of readers from preventing synchronize forward progress.

Quoted 8 lines

With this change, the scan on per-CPU slots don't need to expect a wildcard anymore, because none can be produced by readers. Wildcards are only expected within overflow lists.

Quoted 5 lines

Hi, my R-B still stands from

https://lore.kernel.org/all/[email protected]/

Sorry, I forgot to add it to my commit message. I'll add it to the next round (if there is one), else hopefully Paul can add the R-B tag if he picks up this series.

Thanks!

Mathieu

Signature · 3 lines
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
#11 lore raw

Re: [PATCH hazptr v3 4/4] hazptr: Introduce "try acquire" fast path, fallback to overflow list

Quoted 41 lines · Mathieu Desnoyers
Quoted 8 lines · Bradley Morgan
Quoted 6 lines · Mathieu Desnoyers

Introduce a "try acquire" hazard pointer fast path, which performs an early load of the address to store it into the hazard pointer slot, and then re-loads that address after a barrier to check whether it has changed meanwhile.

On comparison failure, rather than re-try, guarantee forward progress

by

Quoted 10 lines
Quoted 10 lines

falling back to the __hazptr_acquire slow path on failure.

The acquire slow path attempts a try-acquire for any available per-CPU slot. If that fails, it chains the backup slot into the overflow list, therefore guaranteeing forward progress for both hazard pointer read-side and synchronize:

- Readers set the wildcard, and then proceed to set the more specific address to replace the wildcard.

Quoted 7 lines
Quoted 7 lines

- One synchronize alternates between two overflow list periods, scanning each one while readers are added to the other period, thus preventing a steady flow of readers from preventing synchronize forward progress.

With this change, the scan on per-CPU slots don't need to expect a wildcard anymore, because none can be produced by readers. Wildcards

are

Quoted 5 lines
Quoted 5 lines

only expected within overflow lists.

Link:

https://lore.kernel.org/lkmm/[email protected]/

Quoted 5 lines
Quoted 1 line

Hi, my R-B still stands from

https://lore.kernel.org/all/[email protected]/

Quoted 1 line
Quoted 7 lines
Quoted 1 line

Sorry, I forgot to add it to my commit message. I'll add it to the next round (if there is one), else hopefully Paul can add the R-B tag if he picks up this series.

Thanks!

Thanks, this thing happens.. :)

Quoted 5 lines

Mathieu

--- Thanks!
"I'm not a very positive person" - Linus torvalds

Reviewed-by on v3 4/4
#12 lore raw

Re: [PATCH hazptr v3 4/4] hazptr: Introduce "try acquire" fast path, fallback to overflow list

In reply to Mathieu Desnoyers

Hi Mathieu,

I checked the try-acquire protocol and the changes to wildcard handling.

The Store B / smp_mb() / Load A sequence checks that the early and verification loads return the same pointer value, with ptr_eq() used for the comparison.

With wildcard handling removed from per-CPU slots, the per-CPU scan no longer needs the two-phase wildcard check. The overflow-list scan still handles HAZPTR_WILDCARD.

Thanks,
Kunwu

Quoted 348 lines · Mathieu Desnoyers

Introduce a "try acquire" hazard pointer fast path, which performs an early load of the address to store it into the hazard pointer slot, and then re-loads that address after a barrier to check whether it has changed meanwhile.

On comparison failure, rather than re-try, guarantee forward progress by falling back to the __hazptr_acquire slow path on failure.

The acquire slow path attempts a try-acquire for any available per-CPU slot. If that fails, it chains the backup slot into the overflow list, therefore guaranteeing forward progress for both hazard pointer read-side and synchronize:

- Readers set the wildcard, and then proceed to set the more specific address to replace the wildcard.

- One synchronize alternates between two overflow list periods, scanning each one while readers are added to the other period, thus preventing a steady flow of readers from preventing synchronize forward progress.

With this change, the scan on per-CPU slots don't need to expect a wildcard anymore, because none can be produced by readers. Wildcards are only expected within overflow lists.

---
Changes since v2:
- Adapt include to rename from ptreq.h to ptr_eq.h.

---
 include/linux/hazptr.h |  48 +++++++++++--------
 kernel/hazptr.c        | 103 ++++++++++++++++++++---------------------
 2 files changed, 77 insertions(+), 74 deletions(-)
include/linux/hazptr.h +28 -20
index d1670121947a..90d8d5a3d244 100644
@@ -25,13 +25,11 @@
 #include <linux/types.h>
 #include <linux/cleanup.h>
 #include <linux/sched.h>
+#include <linux/ptr_eq.h>
 
 /* 4 slots (each sizeof(hazptr_slot_item)) fit in a single 64-byte cache line. */
 #define NR_HAZPTR_PERCPU_SLOTS 4
 
-/* The current hazard pointer wildcard. */
-extern void *hazptr_wildcard;
-
 /*
  * Hazard pointer slot.
  */
@@ -190,6 +188,31 @@ void hazptr_note_context_switch(void)
        }
 }
 
+/* Try hazard pointer protection. */
+static inline
+void *__hazptr_try_acquire(struct hazptr_ctx *ctx, void * const *addr_p, struct hazptr_slot *slot)
+{
+       void *early_addr, *addr;
+
+       if (unlikely(slot->addr))
+               return NULL;
+       early_addr = READ_ONCE(*addr_p);        /* Early load. */
+       WRITE_ONCE(slot->addr, early_addr);     /* Store B */
+       /* Memory ordering: Store B before Load A. */
+       smp_mb();
+       addr = READ_ONCE(*addr_p);              /* Load A */
+       /*
+        * Validate that address did not change between Early load and Load A.
+        * Use ptr_eq() to make sure that result from Load A is returned to the
+        * caller to preserve address dependency.
+        */
+       if (unlikely(!ptr_eq(addr, early_addr))) {
+               WRITE_ONCE(slot->addr, NULL);
+               return NULL;
+       }
+       return addr;
+}
+
 /**
  * hazptr_acquire - Load pointer at address and protect with hazard pointer.
  *
@@ -245,24 +268,9 @@ void *hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
        ctx->acquire_cpu = smp_processor_id();
        ctx->acquire_caller = _THIS_IP_;
 #endif
-       if (unlikely(slot->addr))
+       addr = __hazptr_try_acquire(ctx, addr_p, slot);
+       if (unlikely(!addr))
                return __hazptr_acquire(ctx, addr_p);
-       WRITE_ONCE(slot->addr, READ_ONCE(hazptr_wildcard));     /* Store B */
-
-       /* Memory ordering: Store B before Load A. */
-       smp_mb();
-
-       /*
-        * Load @addr_p after storing wildcard to the hazard pointer slot.
-        */
-       addr = READ_ONCE(*addr_p);      /* Load A */
-
-       /*
-        * We don't care about ordering of Store C. It will simply
-        * replace the wildcard by a more specific address. If addr is
-        * NULL, we simply store NULL into the slot.
-        */
-       WRITE_ONCE(slot->addr, addr);   /* Store C */
        slot_item->ctx.ctx = ctx;
        ctx->slot = slot;
        return addr;
kernel/hazptr.c +49 -55
index 13faa5ba7677..3ca73b56a5c2 100644
@@ -13,17 +13,9 @@
 #include <linux/list.h>
 #include <linux/export.h>
 
-static DEFINE_MUTEX(hazptr_phase_lock);        /* Protect the wildcard and list phase flip. */
+#define HAZPTR_WILDCARD        ((void *) 1UL)
 
-/*
- * The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee
- * hazptr_synchronize forward progress even with a steady stream of readers.
- * This wildcard value is used by acquire to temporarily tag the per-CPU slots.
- * This also affects the overflow list selection: the current list used by
- * readers is array[(unsigned long) hazptr_wildcard - 1].
- */
-void *hazptr_wildcard = (void *) 1UL;
-EXPORT_SYMBOL_GPL(hazptr_wildcard);
+static DEFINE_MUTEX(hazptr_phase_lock);        /* Protect the list phase flip. */
 
 /* The current overflow list phase. */
 static unsigned int hazptr_overflow_list_phase;
@@ -41,6 +33,10 @@ struct hazptr_overflow_list {
  * successively iterates on both lists. Therefore, only list removals
  * can cause the iteration to retry, and the number of removals is
  * limited to the number of list elements.
+ *
+ * Due to the overflow list raw spin lock, the hazard pointer readers are
+ * blocking, starvation-free with bounded waiting, assuming bounded critical
+ * sections and no NMI or virtualization-induced holder preemption.
  */
 struct hazptr_overflow_list_flip {
        struct hazptr_overflow_list array[2];
@@ -51,26 +47,12 @@ static DEFINE_PER_CPU(struct hazptr_overflow_list_flip, percpu_overflow_list_fli
 DEFINE_PER_CPU(struct hazptr_percpu_slots, hazptr_percpu_slots);
 EXPORT_PER_CPU_SYMBOL_GPL(hazptr_percpu_slots);
 
-static
-void *flip_wildcard(void *wildcard)
-{
-       return ((unsigned long) wildcard == 1UL) ? (void *) 2UL : (void *) 1UL;
-}
-
 static
 unsigned int flip_list_phase(unsigned int phase)
 {
        return 1 - phase;
 }
 
-static
-bool is_wildcard(void *addr)
-{
-       if ((unsigned long) addr == 1UL || (unsigned long) addr == 2UL)
-               return true;
-       return false;
-}
-
 static
 struct hazptr_slot *hazptr_get_free_percpu_slot(struct hazptr_ctx *ctx)
 {
@@ -96,16 +78,36 @@ struct hazptr_slot *hazptr_get_free_percpu_slot(struct hazptr_ctx *ctx)
  */
 void *__hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
 {
-       struct hazptr_slot *slot = hazptr_get_free_percpu_slot(ctx);
+       struct hazptr_slot *slot;
        void *addr;
 
        /*
-        * If all the per-CPU slots are already in use, fallback
-        * to the backup slot.
+        * In case we are called due to nested use of hazard pointers,
+        * try a slot protection with per-CPU slots.
         */
-       if (unlikely(!slot))
-               slot = hazptr_chain_backup_slot(ctx);
-       WRITE_ONCE(slot->addr, READ_ONCE(hazptr_wildcard));     /* Store B */
+       slot = hazptr_get_free_percpu_slot(ctx);
+       if (likely(slot)) {
+               addr = __hazptr_try_acquire(ctx, addr_p, slot);
+               if (addr) {
+                       ctx->slot = slot;
+                       return addr;
+               }
+       }
+
+       /*
+        * The backup slot overflow list guarantees forward progress of both
+        * hazard pointer readers and synchronize:
+        *
+        * - Readers set the wildcard, and then proceed to set the more
+        *   specific address to replace the wildcard.
+        *
+        * - One synchronize alternates between two overflow list periods,
+        *   scanning each one while readers are added to the other period,
+        *   thus preventing a steady flow of readers from preventing
+        *   synchronize forward progress.
+        */
+       slot = hazptr_chain_backup_slot(ctx);
+       WRITE_ONCE(slot->addr, HAZPTR_WILDCARD);        /* Store B */
 
        /* Memory ordering: Store B before Load A. */
        smp_mb();
@@ -121,20 +123,21 @@ void *__hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
         * NULL, we simply store NULL into the slot.
         */
        WRITE_ONCE(slot->addr, addr);   /* Store C */
+
        ctx->slot = slot;
-       if (!addr && hazptr_slot_is_backup(ctx, slot))
+       if (!addr)
                hazptr_unchain_backup_slot(ctx);
        return addr;
 }
 EXPORT_SYMBOL_GPL(__hazptr_acquire);
 
 /*
- * Perform piecewise iteration on overflow list waiting until "addr" is
- * not present. Raw spinlock is released and taken between each list
- * item and busy loop iteration. The overflow list generation is checked
- * each time the lock is taken to validate that the list has not changed
- * before resuming iteration or busy wait. If the generation has
- * changed, retry the entire list traversal.
+ * Perform piecewise iteration on overflow list waiting until "addr" and
+ * wildcard are not present. Raw spinlock is released and taken between each
+ * list item and busy loop iteration. The overflow list generation is checked
+ * each time the lock is taken to validate that the list has not changed before
+ * resuming iteration or busy wait. If the generation has changed, retry the
+ * entire list traversal.
  */
 static
 void hazptr_synchronize_overflow_list(struct hazptr_overflow_list *overflow_list, void *addr)
@@ -147,13 +150,11 @@ void hazptr_synchronize_overflow_list(struct hazptr_overflow_list *overflow_list
 retry:
        snapshot_gen = overflow_list->gen;
        hlist_for_each_entry(backup_slot, &overflow_list->head, overflow_node) {
-               /* Busy-wait if node is found. */
+               /* Busy-wait if addr or wildcard are found. */
                for (;;) {
                        void *load_addr = smp_load_acquire(&backup_slot->slot.addr);    /* Load B */
 
-                       /* We don't expect wildcards in overflow list. */
-                       WARN_ON_ONCE(is_wildcard(load_addr));
-                       if (load_addr != addr)
+                       if (load_addr != addr && load_addr != HAZPTR_WILDCARD)
                                break;
                        raw_spin_unlock_irqrestore(&overflow_list->lock, flags);
                        cpu_relax();
@@ -174,7 +175,7 @@ void hazptr_synchronize_overflow_list(struct hazptr_overflow_list *overflow_list
 }
 
 static
-void hazptr_synchronize_cpu_slots(int cpu, void *addr, void *scan_wildcard)
+void hazptr_synchronize_cpu_slots(int cpu, void *addr)
 {
        struct hazptr_percpu_slots *percpu_slots = per_cpu_ptr(&hazptr_percpu_slots, cpu);
        unsigned int idx;
@@ -182,13 +183,13 @@ void hazptr_synchronize_cpu_slots(int cpu, void *addr, void *scan_wildcard)
        for (idx = 0; idx < NR_HAZPTR_PERCPU_SLOTS; idx++) {
                struct hazptr_slot_item *item = &percpu_slots->items[idx];
 
-               /* Busy-wait if node is found. */
-               smp_cond_load_acquire(&item->slot.addr, VAL != addr && VAL != scan_wildcard); /* Load B */
+               /* Busy-wait if addr is found. */
+               smp_cond_load_acquire(&item->slot.addr, VAL != addr); /* Load B */
        }
 }
 
 static
-void hazptr_scan_cpu_slots_period(void *addr, void *scan_wildcard)
+void hazptr_scan_cpu_slots(void *addr)
 {
        int cpu;
 
@@ -196,16 +197,13 @@ void hazptr_scan_cpu_slots_period(void *addr, void *scan_wildcard)
        for_each_possible_cpu(cpu) {
                /*
                 * Scan CPU slots.
-                * Forward progress against recurring wildcards is guaranteed
-                * by scanning for one wildcard while new elements use the
-                * other wildcard value (1UL vs 2UL).
                 * Forward progress against recurring single hazard pointer
                 * values is guaranteed by the fact that a hazard pointer
                 * is not reclaimed nor reused until the scan for that hazard
                 * pointer completes, which prevents a steady flow of readers
                 * to acquire that same hazard pointer value.
                 */
-               hazptr_synchronize_cpu_slots(cpu, addr, scan_wildcard);
+               hazptr_synchronize_cpu_slots(cpu, addr);
        }
 }
 
@@ -237,7 +235,6 @@ void hazptr_scan_overflow_list_period(void *addr, unsigned int scan_idx)
 void hazptr_synchronize(void *addr)
 {
        unsigned int scan_list_phase;
-       void *scan_wildcard;
 
        /*
         * Busy-wait should only be done from preemptible context.
@@ -251,16 +248,14 @@ void hazptr_synchronize(void *addr)
         */
        if (!addr)
                return;
+
        /* Memory ordering: Store A before Load B. */
        smp_mb();
 
        guard(mutex)(&hazptr_phase_lock);
 
        /* Scan per-CPU slots. */
-       scan_wildcard = flip_wildcard(hazptr_wildcard);
-       hazptr_scan_cpu_slots_period(addr, scan_wildcard);
-       WRITE_ONCE(hazptr_wildcard, scan_wildcard);                     /* Flip the current wildcard. */
-       hazptr_scan_cpu_slots_period(addr, flip_wildcard(scan_wildcard));
+       hazptr_scan_cpu_slots(addr);
 
        /*
         * Scan overflow lists *after* scanning per-CPU slots. See
--

2.43.0

maintainer
#13 lore raw
In reply to Mathieu Desnoyers
Quoted 12 lines · Mathieu Desnoyers

Hi Paul,

This series applies on top of "hazptr: handle NULL address in hazptr_detach" you have in your rcu dev tree.

This first patch addresses a race identified by Boqun Feng in the two-phase wildcard scheme. (no change since the last series)

Patches 2-3 are prerequisites for using ptr_eq() in the 4th patch. Those were discussed at length in a prior version of hazard pointer patches.

Quoted 7 lines

Since the last series, I have renamed ptreq.h to ptr_eq.h, addressing the feedback received from Boqun and Gary.

Patch 4 introduces a "try acquire" helper to allow the fast path to not rely on wildcards, while keeping the wildcard forward progress guarantees in the acquire slow path, used on fast path failure.

I have pulled this in for additional review and testing, thank you! (With Bradley's Reviewed-by on 4/4.) Still subject to rebase, which means that I can easily replace it with an updated series from you, if you wish.

But in the meantime, some additional testing. ;-)

Thanx, Paul

Quoted 34 lines

Thanks,

Mathieu

Mathieu Desnoyers (4):
hazptr: Fix two-phase hazptr_synchronize race with detach ptr_eq.h: Introduce ptr_eq() to preserve address dependency
Documentation: RCU: Refer to ptr_eq()
hazptr: Introduce "try acquire" fast path, fallback to overflow list

Cc Paul E. McKenney <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Bradley Morgan <[email protected]>
Cc Gary Guo <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>
 Documentation/RCU/rcu_dereference.rst |  38 +++++++-
 arch/x86/include/asm/ptr_eq.h         |  16 +++
 include/asm-generic/Kbuild            |   1 +
 include/asm-generic/ptr_eq.h          |  16 +++
 include/linux/hazptr.h                |  48 +++++----
 include/linux/ptr_eq.h                |  63 ++++++++++++
 kernel/hazptr.c                       | 135 +++++++++++++++-----------
 7 files changed, 237 insertions(+), 80 deletions(-)
 create mode 100644 arch/x86/include/asm/ptr_eq.h
 create mode 100644 include/asm-generic/ptr_eq.h
 create mode 100644 include/linux/ptr_eq.h
Signature · 1 line
2.43.0
#14 lore raw
Quoted 21 lines · Paul E. McKenney
Quoted 12 lines · Mathieu Desnoyers

Hi Paul,

This series applies on top of "hazptr: handle NULL address in hazptr_detach" you have in your rcu dev tree.

This first patch addresses a race identified by Boqun Feng in the two-phase wildcard scheme. (no change since the last series)

Patches 2-3 are prerequisites for using ptr_eq() in the 4th patch. Those were discussed at length in a prior version of hazard pointer patches.

Quoted 7 lines

Since the last series, I have renamed ptreq.h to ptr_eq.h, addressing the feedback received from Boqun and Gary.

Patch 4 introduces a "try acquire" helper to allow the fast path to not rely on wildcards, while keeping the wildcard forward progress guarantees in the acquire slow path, used on fast path failure.

Quoted 4 lines

I have pulled this in for additional review and testing, thank you! (With Bradley's Reviewed-by on 4/4.) Still subject to rebase, which means that I can easily replace it with an updated series from you, if you wish.

Hi Paul, Mathieu, and everyone,

I've been following the hazard pointer work and have done some testing on earlier versions. I'd be interested in getting more involved, particularly in concurrency correctness testing.

If there are WIP patches or early design discussions where additional review or testing would be useful, could you please CC me? I'd appreciate the opportunity to get involved earlier and help identify potential concurrency issues.

Thanks,
Kai

Quoted 38 lines

But in the meantime, some additional testing. ;-)

Thanx, Paul

Quoted 34 lines

Thanks,

Mathieu

Mathieu Desnoyers (4):
hazptr: Fix two-phase hazptr_synchronize race with detach ptr_eq.h: Introduce ptr_eq() to preserve address dependency
Documentation: RCU: Refer to ptr_eq()
hazptr: Introduce "try acquire" fast path, fallback to overflow list

Cc Paul E. McKenney <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Bradley Morgan <[email protected]>
Cc Gary Guo <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>
 Documentation/RCU/rcu_dereference.rst |  38 +++++++-
 arch/x86/include/asm/ptr_eq.h         |  16 +++
 include/asm-generic/Kbuild            |   1 +
 include/asm-generic/ptr_eq.h          |  16 +++
 include/linux/hazptr.h                |  48 +++++----
 include/linux/ptr_eq.h                |  63 ++++++++++++
 kernel/hazptr.c                       | 135 +++++++++++++++-----------
 7 files changed, 237 insertions(+), 80 deletions(-)
 create mode 100644 arch/x86/include/asm/ptr_eq.h
 create mode 100644 include/asm-generic/ptr_eq.h
 create mode 100644 include/linux/ptr_eq.h
Signature · 1 line
2.43.0
Signature · 1 line
Kai Ma
#15 lore raw
Quoted 31 lines · Kai Ma
Quoted 16 lines · Paul E. McKenney
Quoted 15 lines · Mathieu Desnoyers

Hi Paul,

This series applies on top of "hazptr: handle NULL address in hazptr_detach" you have in your rcu dev tree.

This first patch addresses a race identified by Boqun Feng in the two-phase wildcard scheme. (no change since the last series)

Patches 2-3 are prerequisites for using ptr_eq() in the 4th patch. Those were discussed at length in a prior version of hazard pointer patches.

Since the last series, I have renamed ptreq.h to ptr_eq.h, addressing the feedback received from Boqun and Gary.

Quoted 7 lines
Quoted 4 lines

Patch 4 introduces a "try acquire" helper to allow the fast path to not rely on wildcards, while keeping the wildcard forward progress guarantees in the acquire slow path, used on fast path failure.

I have pulled this in for additional review and testing, thank you! (With Bradley's Reviewed-by on 4/4.) Still subject to rebase, which

means

Quoted 2 lines

that I can easily replace it with an updated series from you, if you wish.

Hi Paul, Mathieu, and everyone,

Quoted 8 lines

I've been following the hazard pointer work and have done some testing on earlier versions. I'd be interested in getting more involved, particularly in concurrency correctness testing.

If there are WIP patches or early design discussions where additional review or testing would be useful, could you please CC me? I'd appreciate the opportunity to get involved earlier and help identify potential concurrency issues.

Thanks for taking this job up, what you could do, is make Linus happy, if that makes sense.

https://lore.kernel.org/all/CAHk-=wjyniJy-CX1muCrsZp-i-sLGRYeRYEPN7sw9orRZUCwag@mail.gmail.com/

Quoted 44 lines

Thanks,
Kai

Quoted 38 lines

But in the meantime, some additional testing. ;-)

Thanx, Paul

Quoted 34 lines

Thanks,

Mathieu

Mathieu Desnoyers (4):
hazptr: Fix two-phase hazptr_synchronize race with detach ptr_eq.h: Introduce ptr_eq() to preserve address dependency
Documentation: RCU: Refer to ptr_eq()
hazptr: Introduce "try acquire" fast path, fallback to overflow list

Cc Paul E. McKenney <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Bradley Morgan <[email protected]>
Cc Gary Guo <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>
 Documentation/RCU/rcu_dereference.rst |  38 +++++++-
 arch/x86/include/asm/ptr_eq.h         |  16 +++
 include/asm-generic/Kbuild            |   1 +
 include/asm-generic/ptr_eq.h          |  16 +++
 include/linux/hazptr.h                |  48 +++++----
 include/linux/ptr_eq.h                |  63 ++++++++++++
 kernel/hazptr.c                       | 135 +++++++++++++++-----------
 7 files changed, 237 insertions(+), 80 deletions(-)
 create mode 100644 arch/x86/include/asm/ptr_eq.h
 create mode 100644 include/asm-generic/ptr_eq.h
 create mode 100644 include/linux/ptr_eq.h
Signature · 1 line
2.43.0

--- Thanks!

maintainer
#16 lore raw
In reply to Kai Ma
Quoted 30 lines · Kai Ma
Quoted 21 lines · Paul E. McKenney
Quoted 12 lines · Mathieu Desnoyers

Hi Paul,

This series applies on top of "hazptr: handle NULL address in hazptr_detach" you have in your rcu dev tree.

This first patch addresses a race identified by Boqun Feng in the two-phase wildcard scheme. (no change since the last series)

Patches 2-3 are prerequisites for using ptr_eq() in the 4th patch. Those were discussed at length in a prior version of hazard pointer patches.

Quoted 7 lines

Since the last series, I have renamed ptreq.h to ptr_eq.h, addressing the feedback received from Boqun and Gary.

Patch 4 introduces a "try acquire" helper to allow the fast path to not rely on wildcards, while keeping the wildcard forward progress guarantees in the acquire slow path, used on fast path failure.

Quoted 4 lines

I have pulled this in for additional review and testing, thank you! (With Bradley's Reviewed-by on 4/4.) Still subject to rebase, which means that I can easily replace it with an updated series from you, if you wish.

Hi Paul, Mathieu, and everyone,

Quoted 8 lines

I've been following the hazard pointer work and have done some testing on earlier versions. I'd be interested in getting more involved, particularly in concurrency correctness testing.

If there are WIP patches or early design discussions where additional review or testing would be useful, could you please CC me? I'd appreciate the opportunity to get involved earlier and help identify potential concurrency issues.

The current stack is in my -rcu tree [1] at v7.3-rc1..c3aea7747975, with these patches on top:

5df49d76ff3a3 hazptr: Fix two-phase hazptr_synchronize race with detach b41c6c2d4a33f ptr_eq.h: Introduce ptr_eq() to preserve address dependency 97ff14db1c381 Documentation: RCU: Refer to ptr_eq()
451174c765179 hazptr: Introduce "try acquire" fast path, fallback to overflow list 633f91e3408cc hazptr: Record per-CPU slot owner after claiming the slot

The biggest need is use cases, with the most likely ones being cases where per-CPU reference counts consume too much memory or a reference count is suffering from memory contention. However, we are still finding critical bugs both in the mechanism and the torture tests, so as you say, review or testing is also very important.

These patches CC the [email protected] list, so the best way to get involved in the discussions is to join that list.

And welcome aboard!!!

Thanx, Paul

[1] git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git

Quoted 44 lines

Thanks,
Kai

Quoted 38 lines

But in the meantime, some additional testing. ;-)

Thanx, Paul

Quoted 34 lines

Thanks,

Mathieu

Mathieu Desnoyers (4):
hazptr: Fix two-phase hazptr_synchronize race with detach ptr_eq.h: Introduce ptr_eq() to preserve address dependency
Documentation: RCU: Refer to ptr_eq()
hazptr: Introduce "try acquire" fast path, fallback to overflow list

Cc Paul E. McKenney <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Bradley Morgan <[email protected]>
Cc Gary Guo <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>
 Documentation/RCU/rcu_dereference.rst |  38 +++++++-
 arch/x86/include/asm/ptr_eq.h         |  16 +++
 include/asm-generic/Kbuild            |   1 +
 include/asm-generic/ptr_eq.h          |  16 +++
 include/linux/hazptr.h                |  48 +++++----
 include/linux/ptr_eq.h                |  63 ++++++++++++
 kernel/hazptr.c                       | 135 +++++++++++++++-----------
 7 files changed, 237 insertions(+), 80 deletions(-)
 create mode 100644 arch/x86/include/asm/ptr_eq.h
 create mode 100644 include/asm-generic/ptr_eq.h
 create mode 100644 include/linux/ptr_eq.h
Signature · 1 line
2.43.0
Signature · 1 line
Kai Ma
#17 lore raw
Quoted 55 lines · Paul E. McKenney
Quoted 30 lines · Kai Ma
Quoted 21 lines · Paul E. McKenney
Quoted 12 lines · Mathieu Desnoyers

Hi Paul,

This series applies on top of "hazptr: handle NULL address in hazptr_detach" you have in your rcu dev tree.

This first patch addresses a race identified by Boqun Feng in the two-phase wildcard scheme. (no change since the last series)

Patches 2-3 are prerequisites for using ptr_eq() in the 4th patch. Those were discussed at length in a prior version of hazard pointer patches.

Quoted 7 lines

Since the last series, I have renamed ptreq.h to ptr_eq.h, addressing the feedback received from Boqun and Gary.

Patch 4 introduces a "try acquire" helper to allow the fast path to not rely on wildcards, while keeping the wildcard forward progress guarantees in the acquire slow path, used on fast path failure.

Quoted 4 lines

I have pulled this in for additional review and testing, thank you! (With Bradley's Reviewed-by on 4/4.) Still subject to rebase, which means that I can easily replace it with an updated series from you, if you wish.

Hi Paul, Mathieu, and everyone,

Quoted 8 lines

I've been following the hazard pointer work and have done some testing on earlier versions. I'd be interested in getting more involved, particularly in concurrency correctness testing.

If there are WIP patches or early design discussions where additional review or testing would be useful, could you please CC me? I'd appreciate the opportunity to get involved earlier and help identify potential concurrency issues.

The current stack is in my -rcu tree [1] at v7.3-rc1..c3aea7747975, with these patches on top:

5df49d76ff3a3 hazptr: Fix two-phase hazptr_synchronize race with detach b41c6c2d4a33f ptr_eq.h: Introduce ptr_eq() to preserve address dependency 97ff14db1c381 Documentation: RCU: Refer to ptr_eq()
451174c765179 hazptr: Introduce "try acquire" fast path, fallback to overflow list 633f91e3408cc hazptr: Record per-CPU slot owner after claiming the slot

The biggest need is use cases, with the most likely ones being cases where per-CPU reference counts consume too much memory or a reference count is suffering from memory contention. However, we are still finding critical bugs both in the mechanism and the torture tests, so as you say, review or testing is also very important.

Quoted 4 lines

These patches CC the [email protected] list, so the best way to get involved in the discussions is to join that list.

And welcome aboard!!!

Thank you and also Bradley!

Kai

Quoted 46 lines

Thanx, Paul

[1] git://git.kernel.org/pub/scm/linux/kernel/git/paulmck/linux-rcu.git

Quoted 41 lines

Thanks,
Kai

Quoted 38 lines

But in the meantime, some additional testing. ;-)

Thanx, Paul

Quoted 34 lines

Thanks,

Mathieu

Mathieu Desnoyers (4):
hazptr: Fix two-phase hazptr_synchronize race with detach ptr_eq.h: Introduce ptr_eq() to preserve address dependency
Documentation: RCU: Refer to ptr_eq()
hazptr: Introduce "try acquire" fast path, fallback to overflow list

Cc Paul E. McKenney <[email protected]>
Cc Boqun Feng <[email protected]>
Cc Bradley Morgan <[email protected]>
Cc Gary Guo <[email protected]>
Cc Lian Wang <[email protected]>
Cc Kunwu Chan <[email protected]>
 Documentation/RCU/rcu_dereference.rst |  38 +++++++-
 arch/x86/include/asm/ptr_eq.h         |  16 +++
 include/asm-generic/Kbuild            |   1 +
 include/asm-generic/ptr_eq.h          |  16 +++
 include/linux/hazptr.h                |  48 +++++----
 include/linux/ptr_eq.h                |  63 ++++++++++++
 kernel/hazptr.c                       | 135 +++++++++++++++-----------
 7 files changed, 237 insertions(+), 80 deletions(-)
 create mode 100644 arch/x86/include/asm/ptr_eq.h
 create mode 100644 include/asm-generic/ptr_eq.h
 create mode 100644 include/linux/ptr_eq.h
Signature · 1 line
2.43.0