[PATCH RFC v2 04/15] hazptr: add scoped_guard() support
From: Kunwu Chan
Date: Fri Oct 02 2026 - 13:10:21 EST
Add a scoped_guard() class for hazptr so that callers can pair
hazptr_acquire() with hazptr_release() on scope exit.
The guard holds a pointer to a caller-provided hazptr_ctx rather
than embedding it. The ctx must remain at a stable address while
the hazard pointer is held because the slow path can chain its
backup slot into the overflow list by address.
Suggested-by: Boqun Feng <boqun@xxxxxxxxxx>
Signed-off-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
---
include/linux/hazptr.h | 51 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+)
diff --git a/include/linux/hazptr.h b/include/linux/hazptr.h
index d1670121947a..275cbe54b075 100644
--- a/include/linux/hazptr.h
+++ b/include/linux/hazptr.h
@@ -320,6 +320,57 @@ void hazptr_release(struct hazptr_ctx *ctx, void *addr)
hazptr_unchain_backup_slot(ctx);
}
+/**
+ * struct hazptr_guard - cleanup.h guard for a single hazard pointer
+ *
+ * @ctx: Pointer to the caller-provided hazard-pointer context.
+ * @addr: The address returned by the matching hazptr_acquire().
+ */
+struct hazptr_guard {
+ struct hazptr_ctx *ctx;
+ void *addr;
+};
+
+/**
+ * hazptr_guard_acquire - Acquire a hazard pointer for scoped_guard() use
+ *
+ * @ctx: The caller-provided hazard-pointer context.
+ * @addr_p: Pointer to the pointer that is to be hazard-pointer protected.
+ *
+ * Acquire a hazard pointer using @ctx and return a guard that
+ * cleanup.h releases via hazptr_release() on scope exit. The guard
+ * only carries the ctx pointer and the address returned by
+ * hazptr_acquire().
+ */
+static inline struct hazptr_guard
+hazptr_guard_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
+{
+ struct hazptr_guard g;
+
+ g.ctx = ctx;
+ g.addr = hazptr_acquire(ctx, addr_p);
+ return g;
+}
+
+DEFINE_CLASS(hazptr, struct hazptr_guard,
+ hazptr_release(_T.ctx, _T.addr),
+ hazptr_guard_acquire(ctx, addr_p),
+ struct hazptr_ctx *ctx, void * const *addr_p)
+DEFINE_CLASS_IS_UNCONDITIONAL(hazptr)
+
+/*
+ * The ctx must remain at a stable address while the hazard pointer
+ * is held because the slow path can chain its backup slot
+ * into the overflow list by address.
+ *
+ * struct hazptr_ctx ctx;
+ * void *obj = ptr;
+ *
+ * scoped_guard(hazptr, &ctx, &obj) {
+ * ...
+ * }
+ */
+
void hazptr_init(void);
#endif /* _LINUX_HAZPTR_H */
--
2.43.0