[PATCH 04/28] hazptr: Add debug option to force the acquire slow path

From: Kunwu Chan

Date: Wed Nov 04 2026 - 12:02:54 EST


Add CONFIG_HAZPTR_ACQUIRE_FORCE_SLOWPATH: makes hazptr_acquire()
skip the per-CPU fast path entirely and enter __hazptr_acquire(),
even when a per-CPU slot is available. This is useful for testing
slow-path locking, especially with lockdep. It also lets
hazptrtorture exercise the slow path without requiring the per-CPU
slots to be exhausted.

Note that the forced path skips __hazptr_try_acquire() rather than
running it and discarding its result: a successful try_acquire
publishes the address to the per-CPU slot, and abandoning that
publication would leave a slot pointing at a stale ctx, which
hazptr_note_context_switch() then dereferences (use-after-free).

Suggested-by: Boqun Feng <boqun@xxxxxxxxxx>
Signed-off-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
Acked-by: Bradley Morgan <brads@xxxxxxxxxxxxxx>
---
include/linux/hazptr.h | 2 ++
kernel/rcu/Kconfig.debug | 10 ++++++++++
2 files changed, 12 insertions(+)

diff --git a/include/linux/hazptr.h b/include/linux/hazptr.h
index 3372b1cdbee6..299ad8a07156 100644
--- a/include/linux/hazptr.h
+++ b/include/linux/hazptr.h
@@ -271,6 +271,8 @@ void *hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
ctx->acquire_cpu = smp_processor_id();
ctx->acquire_caller = _THIS_IP_;
#endif
+ if (IS_ENABLED(CONFIG_HAZPTR_ACQUIRE_FORCE_SLOWPATH))
+ return __hazptr_acquire(ctx, addr_p);
addr = __hazptr_try_acquire(ctx, addr_p, slot);
if (unlikely(!addr))
return __hazptr_acquire(ctx, addr_p);
diff --git a/kernel/rcu/Kconfig.debug b/kernel/rcu/Kconfig.debug
index 7c3c6017b266..3a2e9d7f58f8 100644
--- a/kernel/rcu/Kconfig.debug
+++ b/kernel/rcu/Kconfig.debug
@@ -265,4 +265,14 @@ config HAZPTR_DEBUG

Say Y here if you want to enable those assert, N otherwise.

+config HAZPTR_ACQUIRE_FORCE_SLOWPATH
+ bool "Force hazard-pointer acquire into the slow path"
+ depends on DEBUG_KERNEL
+ default n
+ help
+ Make hazptr_acquire() always enter __hazptr_acquire().
+ This is useful for testing slow-path locking, especially
+ with lockdep. It also lets hazptrtorture exercise the slow
+ path without filling the per-CPU slots.
+
endmenu # "RCU Debugging"
--
2.43.0