[RFC PATCH v4 0/4] Hazard Pointers

From: Mathieu Desnoyers

Date: Wed Dec 17 2025 - 20:45:47 EST


Hi,

Here is a revisited version of my Hazard Pointers series. Boqun, Joel,
if you guys have time to try it out with your use-cases it would be
great!

This new version does the following:

- It has 8 preallocated hazard pointer slots per CPU (one cache line),
- The hazard pointer user allocates a hazard pointer context variable
(typically on the stack), which contains the pointer to the slot *and*
a backup slot,
- When all the per-CPU slots are in use, fallback to the backup slot.
Chain the backup slot into per-CPU lists, each protected by a raw
spinlock.
- The hazard pointer synchronize does a piecewise iteration on the
per-CPU overflow slots lists, releasing the raw spinlock between
each list item. It uses a 64-bit generation counter to check for
concurrent list changes, and restart the traversal on generation
counter mismatch.
- There is a new CONFIG_PREEMPT_HAZPTR config option. When enabled,
the hazard pointer acquire/release adds and then removes the hazard
pointer context from a per-task linked list. On context switch, the
scheduler migrates the per-CPU slots used by the task to the backup
per-context slots, thus making sure the per-CPU slots are not used
by preempted and blocked tasks.

It is based on v6.18.1.

Review is very welcome,

Thanks,

Mathieu

Cc: Nicholas Piggin <npiggin@xxxxxxxxx>
Cc: Michael Ellerman <mpe@xxxxxxxxxxxxxx>
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Cc: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
Cc: "Paul E. McKenney" <paulmck@xxxxxxxxxx>
Cc: Will Deacon <will@xxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Boqun Feng <boqun.feng@xxxxxxxxx>
Cc: Alan Stern <stern@xxxxxxxxxxxxxxxxxxx>
Cc: John Stultz <jstultz@xxxxxxxxxx>
Cc: Neeraj Upadhyay <Neeraj.Upadhyay@xxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Boqun Feng <boqun.feng@xxxxxxxxx>
Cc: Frederic Weisbecker <frederic@xxxxxxxxxx>
Cc: Joel Fernandes <joel@xxxxxxxxxxxxxxxxx>
Cc: Josh Triplett <josh@xxxxxxxxxxxxxxxx>
Cc: Uladzislau Rezki <urezki@xxxxxxxxx>
Cc: Steven Rostedt <rostedt@xxxxxxxxxxx>
Cc: Lai Jiangshan <jiangshanlai@xxxxxxxxx>
Cc: Zqiang <qiang.zhang1211@xxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: Waiman Long <longman@xxxxxxxxxx>
Cc: Mark Rutland <mark.rutland@xxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Vlastimil Babka <vbabka@xxxxxxx>
Cc: maged.michael@xxxxxxxxx
Cc: Mateusz Guzik <mjguzik@xxxxxxxxx>
Cc: Jonas Oberhauser <jonas.oberhauser@xxxxxxxxxxxxxxx>
Cc: rcu@xxxxxxxxxxxxxxx
Cc: linux-mm@xxxxxxxxx
Cc: lkmm@xxxxxxxxxxxxxxx

Mathieu Desnoyers (4):
compiler.h: Introduce ptr_eq() to preserve address dependency
Documentation: RCU: Refer to ptr_eq()
hazptr: Implement Hazard Pointers
hazptr: Migrate per-CPU slots to backup slot on context switch

Documentation/RCU/rcu_dereference.rst | 38 +++-
include/linux/compiler.h | 63 +++++++
include/linux/hazptr.h | 241 ++++++++++++++++++++++++++
include/linux/sched.h | 4 +
init/init_task.c | 3 +
init/main.c | 2 +
kernel/Kconfig.preempt | 10 ++
kernel/Makefile | 2 +-
kernel/fork.c | 3 +
kernel/hazptr.c | 150 ++++++++++++++++
kernel/sched/core.c | 2 +
11 files changed, 512 insertions(+), 6 deletions(-)
create mode 100644 include/linux/hazptr.h
create mode 100644 kernel/hazptr.c

--
2.39.5