[PATCH 02/28] hazptr: Use Bloom filter for shared scan waiters
From: Kunwu Chan
Date: Wed Nov 04 2026 - 12:01:11 EST
Replace the per-waiter sequential scan with a shared Bloom filter
built during a single walk: each observed non-wildcard address
sets three bits. A waiter whose address hashes to an unset bit
can be completed without another slot walk.
A Bloom filter has no false negatives, so an address absent from
the scan cannot be held by an observed slot; false positives only
delay completion to a later scan cycle.
The filter uses a one-page bitmap with three multiply-shift hash
functions. It lives in the hazptr_scan_state and is only touched
by the scan kthread.
Suggested-by: Boqun Feng <boqun@xxxxxxxxxx>
Signed-off-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
Suggested-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Acked-by: Bradley Morgan <brads@xxxxxxxxxxxxxx>
---
kernel/hazptr.c | 122 +++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 116 insertions(+), 6 deletions(-)
diff --git a/kernel/hazptr.c b/kernel/hazptr.c
index 013c24021c43..059c356ceaa7 100644
--- a/kernel/hazptr.c
+++ b/kernel/hazptr.c
@@ -16,6 +16,8 @@
#include <linux/kthread.h>
#include <linux/swait.h>
#include <linux/sched.h>
+#include <linux/bitops.h>
+#include <linux/bitmap.h>
#define HAZPTR_WILDCARD ((void *) 1UL)
@@ -57,6 +59,12 @@ unsigned int flip_list_phase(unsigned int phase)
return 1 - phase;
}
+static
+bool is_wildcard(void *addr)
+{
+ return (unsigned long)addr == (unsigned long)HAZPTR_WILDCARD;
+}
+
static
struct hazptr_slot_item *hazptr_get_free_percpu_slot_item(void)
{
@@ -252,6 +260,98 @@ static void hazptr_scan_overflow_list_all(void *addr)
}
}
+/* Number of hash functions for the Bloom filter. */
+#define HAZPTR_BLOOM_HASHES 3
+
+/* Number of bits in the Bloom filter bitmap: one page. */
+#define HAZPTR_BLOOM_NBITS (PAGE_SIZE * 8)
+
+struct hazptr_bloom {
+ unsigned long map[PAGE_SIZE / sizeof(unsigned long)];
+};
+
+/*
+ * Multiply @addr by a per-hash odd constant and use the high bits
+ * as the Bloom filter index.
+ */
+static unsigned long hazptr_bloom_hash(void *addr, unsigned int i)
+{
+ static const u64 mult[HAZPTR_BLOOM_HASHES] = {
+ 0x9E3779B97F4A7C15ULL,
+ 0xC2B2AE3D27D4EB4FULL,
+ 0x165667B19E3779F9ULL,
+ };
+ u64 hash = (u64)(unsigned long)addr * mult[i];
+
+ return hash >> (64 - ilog2(HAZPTR_BLOOM_NBITS));
+}
+
+static void hazptr_bloom_reset(struct hazptr_bloom *bloom)
+{
+ bitmap_zero(bloom->map, HAZPTR_BLOOM_NBITS);
+}
+
+static void hazptr_bloom_add(struct hazptr_bloom *bloom, void *addr)
+{
+ unsigned int i;
+
+ for (i = 0; i < HAZPTR_BLOOM_HASHES; i++)
+ __set_bit(hazptr_bloom_hash(addr, i), bloom->map);
+}
+
+static bool hazptr_bloom_contains(const struct hazptr_bloom *bloom, void *addr)
+{
+ unsigned int i;
+
+ for (i = 0; i < HAZPTR_BLOOM_HASHES; i++)
+ if (!test_bit(hazptr_bloom_hash(addr, i), bloom->map))
+ return false;
+ return true;
+}
+
+/*
+ * Walk all slots and record observed non-wildcard addresses into
+ * the Bloom filter. A single pass suffices on the try_acquire
+ * fast path (readers publish their target directly).
+ */
+static void hazptr_scan_walk(struct hazptr_bloom *bloom)
+{
+ int cpu;
+
+ hazptr_bloom_reset(bloom);
+
+ for_each_possible_cpu(cpu) {
+ struct hazptr_percpu_slots *percpu_slots = per_cpu_ptr(&hazptr_percpu_slots, cpu);
+ struct hazptr_overflow_list_flip *overflow_list_flip = per_cpu_ptr(&percpu_overflow_list_flip, cpu);
+ unsigned int idx;
+
+ for (idx = 0; idx < NR_HAZPTR_PERCPU_SLOTS; idx++) {
+ struct hazptr_slot_item *item = &percpu_slots->items[idx];
+ void *v;
+
+ v = smp_load_acquire(&item->slot.addr);
+ if (v && !is_wildcard(v))
+ hazptr_bloom_add(bloom, v);
+ }
+ for (int i = 0; i < 2; i++) {
+ struct hazptr_overflow_list *list = &overflow_list_flip->array[i];
+ struct hazptr_backup_slot *b;
+ unsigned long flags;
+
+ raw_spin_lock_irqsave(&list->lock, flags);
+ hlist_for_each_entry(b, &list->head, overflow_node) {
+ void *v;
+
+ v = smp_load_acquire(&b->slot.addr);
+ if (v && !is_wildcard(v))
+ hazptr_bloom_add(bloom, v);
+ }
+ raw_spin_unlock_irqrestore(&list->lock, flags);
+ }
+ }
+}
+
+/* Need is_wildcard for the Bloom filter */
/*
* Shared scan: batch concurrent hazptr_synchronize() callers into a
* single kthread-driven scan, avoiding redundant per-CPU and
@@ -272,6 +372,7 @@ struct hazptr_scan_state {
struct mutex lock;
struct list_head pending;
struct list_head scanning; /* kthread only */
+ struct hazptr_bloom bloom; /* kthread only */
};
static struct hazptr_scan_state hazptr_scan;
@@ -315,19 +416,28 @@ static void hazptr_scan_do_cycle(void)
}
/*
- * Process each waiter: scan per-CPU slots and overflow lists
- * until the waiter's address is released. This is the same
- * scan the direct fallback performs, delegated to a kthread so
- * callers sleep instead of busy-waiting.
+ * Walk all slots and record observed addresses into the
+ * Bloom filter, then complete waiters whose address is
+ * not present. On the try_acquire fast path, readers
+ * publish their target directly (no wildcard transient),
+ * so a single pass suffices.
*/
+ hazptr_scan_walk(&hazptr_scan.bloom);
+
+ list_for_each_entry_safe(w, n, &hazptr_scan.scanning, node) {
+ if (!hazptr_bloom_contains(&hazptr_scan.bloom, w->addr))
+ list_move(&w->node, &done);
+ }
+
+ mutex_unlock(&hazptr_phase_lock);
+
+ /* Waiters still blocked: their address was observed present. */
list_for_each_entry_safe(w, n, &hazptr_scan.scanning, node) {
hazptr_scan_cpu_slots(w->addr);
hazptr_scan_overflow_list_all(w->addr);
list_move(&w->node, &done);
}
- mutex_unlock(&hazptr_phase_lock);
-
list_for_each_entry_safe(w, n, &done, node) {
list_del_init(&w->node);
complete(&w->done);
--
2.43.0