From: Kunwu Chan <kunwu.chan@gmail.com>
To: paulmck@kernel.org, boqun@kernel.org,
mathieu.desnoyers@efficios.com, jiangshanlai@gmail.com,
corbet@lwn.net, mingo@redhat.com, frederic@kernel.org,
neeraj.upadhyay@kernel.org, josh@joshtriplett.org,
urezki@gmail.com, dave@stgolabs.net
Cc: stern@rowland.harvard.edu, parri.andrea@gmail.com,
will@kernel.org, dhowells@redhat.com, j.alglave@ucl.ac.uk,
luc.maranget@inria.fr, akiyks@gmail.com, dlustig@nvidia.com,
joelagnelf@nvidia.com, skhan@linuxfoundation.org,
rdunlap@infradead.org, longman@redhat.com, rostedt@goodmis.org,
qiang.zhang@linux.dev, kunwu.chan@gmail.com,
brads@mainlining.org, linux-kernel@vger.kernel.org,
linux-arch@vger.kernel.org, lkmm@lists.linux.dev,
linux-doc@vger.kernel.org, rcu@vger.kernel.org,
linux-kselftest@vger.kernel.org
Subject: [PATCH 12/28] hazptr: Remove per-waiter scan fallback, simplify synchronize
Date: Thu, 5 Nov 2026 00:54:30 +0800 [thread overview]
Message-ID: <20261104165504.2430480-13-kunwu.chan@gmail.com> (raw)
In-Reply-To: <20261104165504.2430480-1-kunwu.chan@gmail.com>
With try_acquire, per-CPU slots publish their target address
directly (no wildcard transient), so a single Bloom-walk pass
suffices. Drop the per-waiter exact-match fallback that
busy-waited on remote slots; waiters whose address is in the
Bloom filter wait for the next scan cycle.
Extend hazptr_scan_walk() with an optional @watch parameter for
exact-match lookups (used by the upcoming stall detector).
Signed-off-by: Kunwu Chan <kunwu.chan@gmail.com>
---
kernel/hazptr.c | 104 +++++++++++++++++++++++++++---------------------
1 file changed, 58 insertions(+), 46 deletions(-)
diff --git a/kernel/hazptr.c b/kernel/hazptr.c
index 059c356ceaa7..4784df78be4e 100644
--- a/kernel/hazptr.c
+++ b/kernel/hazptr.c
@@ -39,10 +39,6 @@ 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];
@@ -153,12 +149,12 @@ void *__hazptr_acquire(struct hazptr_ctx *ctx, void * const *addr_p)
EXPORT_SYMBOL_GPL(__hazptr_acquire);
/*
- * 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.
+ * 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.
*/
static
void hazptr_synchronize_overflow_list(struct hazptr_overflow_list *overflow_list, void *addr)
@@ -228,8 +224,8 @@ void hazptr_scan_cpu_slots(void *addr)
}
}
-static
-void hazptr_scan_overflow_list_period(void *addr, unsigned int scan_idx)
+static void __maybe_unused
+hazptr_scan_overflow_list_period(void *addr, unsigned int scan_idx)
{
int cpu;
@@ -310,15 +306,28 @@ static bool hazptr_bloom_contains(const struct hazptr_bloom *bloom, void *addr)
}
/*
- * 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).
+ * Walk all slots and return true if @watch (non-NULL) is present.
+ * If @bloom is non-NULL, record observed non-wildcard addresses in it.
+ * A NULL @watch means pure collection mode: never early-returns on a
+ * match.
+ *
+ * A wildcard in an overflow-list slot means a slow-path acquire is in
+ * flight: that slot may resolve to any address (including @watch or a
+ * Bloom-collected one) after the walk. To keep the single-pass
+ * completion decision safe, the caller must observe wildcards and
+ * defer completion decisions for one cycle; @wildcard (if non-NULL)
+ * is set to true in that case. In watch mode, a wildcard also counts
+ * as "present" so exact-match callers do not complete.
*/
-static void hazptr_scan_walk(struct hazptr_bloom *bloom)
+static bool hazptr_scan_walk(void *watch, struct hazptr_bloom *bloom,
+ bool *wildcard)
{
int cpu;
- hazptr_bloom_reset(bloom);
+ if (bloom)
+ hazptr_bloom_reset(bloom);
+ if (wildcard)
+ *wildcard = false;
for_each_possible_cpu(cpu) {
struct hazptr_percpu_slots *percpu_slots = per_cpu_ptr(&hazptr_percpu_slots, cpu);
@@ -329,8 +338,11 @@ static void hazptr_scan_walk(struct hazptr_bloom *bloom)
struct hazptr_slot_item *item = &percpu_slots->items[idx];
void *v;
+ /* Pairs with smp_store_release in hazptr_release(). */
v = smp_load_acquire(&item->slot.addr);
- if (v && !is_wildcard(v))
+ if (watch && (v == watch || is_wildcard(v)))
+ return true;
+ if (bloom && v && !is_wildcard(v))
hazptr_bloom_add(bloom, v);
}
for (int i = 0; i < 2; i++) {
@@ -342,16 +354,24 @@ static void hazptr_scan_walk(struct hazptr_bloom *bloom)
hlist_for_each_entry(b, &list->head, overflow_node) {
void *v;
+ /* Pairs with smp_store_release in hazptr_release(). */
v = smp_load_acquire(&b->slot.addr);
- if (v && !is_wildcard(v))
+ if (watch && (v == watch || is_wildcard(v))) {
+ raw_spin_unlock_irqrestore(&list->lock, flags);
+ return true;
+ }
+ if (bloom && v && !is_wildcard(v))
hazptr_bloom_add(bloom, v);
+ else if (v && is_wildcard(v) && wildcard)
+ *wildcard = true;
}
raw_spin_unlock_irqrestore(&list->lock, flags);
}
}
+
+ return false;
}
-/* 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
@@ -416,28 +436,32 @@ static void hazptr_scan_do_cycle(void)
}
/*
- * Walk all slots and record observed addresses into the
+ * Scan all slots: 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.
+ * so a single pass suffices for per-CPU slots.
*/
- hazptr_scan_walk(&hazptr_scan.bloom);
+ {
+ bool wildcard;
- 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);
+ hazptr_scan_walk(NULL, &hazptr_scan.bloom, &wildcard);
+ /*
+ * A wildcard in an overflow-list slot is a slow-path
+ * acquire in flight: that slot can resolve to any
+ * address after the walk, so defer completion
+ * decisions to the next cycle.
+ */
+ if (!wildcard)
+ /* Complete waiters whose address is not in the filter. */
+ 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);
- }
-
list_for_each_entry_safe(w, n, &done, node) {
list_del_init(&w->node);
complete(&w->done);
@@ -496,8 +520,6 @@ core_initcall(hazptr_scan_init);
*/
void hazptr_synchronize(void *addr)
{
- unsigned int scan_list_phase;
-
/*
* Busy-wait should only be done from preemptible context.
*/
@@ -521,18 +543,8 @@ void hazptr_synchronize(void *addr)
}
guard(mutex)(&hazptr_phase_lock);
-
- /* Scan per-CPU slots. */
hazptr_scan_cpu_slots(addr);
-
- /*
- * 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));
+ hazptr_scan_overflow_list_all(addr);
}
EXPORT_SYMBOL_GPL(hazptr_synchronize);
--
2.43.0
next prev parent reply other threads:[~2026-11-04 16:57 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-04 16:54 [PATCH 00/28] hazptr: shared-scan kthread, drain-assist, and stall detector Kunwu Chan
2026-11-04 16:54 ` [PATCH 01/28] hazptr: Add shared scan kthread Kunwu Chan
2026-11-04 16:54 ` [PATCH 02/28] hazptr: Use Bloom filter for shared scan waiters Kunwu Chan
2026-11-04 16:54 ` [PATCH 03/28] hazptr: Add scoped_guard() support Kunwu Chan
2026-11-04 16:54 ` [PATCH 04/28] hazptr: Add debug option to force the acquire slow path Kunwu Chan
2026-11-04 16:54 ` [PATCH 05/28] Documentation/litmus-tests: Add hazptr wildcard-flip escape test Kunwu Chan
2026-11-04 16:54 ` [PATCH 06/28] Documentation/litmus-tests: Add hazptr acquire-before-scan test Kunwu Chan
2026-11-04 16:54 ` [PATCH 07/28] hazptr: Fix kernel-doc of hazptr_release() Kunwu Chan
2026-11-04 16:54 ` [PATCH 08/28] hazptrtorture: Add slowpath and lockdep scenarios Kunwu Chan
2026-11-04 16:54 ` [PATCH 09/28] hazptrtorture: Add READERS4 and READERS0 torture configs Kunwu Chan
2026-11-04 16:54 ` [PATCH 10/28] hazptrtorture: Add 128- and 256-CPU configs Kunwu Chan
2026-11-04 16:54 ` [PATCH 11/28] selftests/rcutorture: Add hazptr torture test script Kunwu Chan
2026-11-04 16:54 ` Kunwu Chan [this message]
2026-11-04 16:54 ` [PATCH 13/28] hazptr: Add waiter-enqueue tracepoint Kunwu Chan
2026-11-04 16:54 ` [PATCH 14/28] hazptr: Add waiter stall detector Kunwu Chan
2026-11-04 16:54 ` [PATCH 15/28] hazptr: Add reader-assisted drain for stalled waiters Kunwu Chan
2026-11-04 16:54 ` [PATCH 16/28] Documentation/litmus-tests: Add hazptr drain engage-wildcard test Kunwu Chan
2026-11-04 16:54 ` [PATCH 17/28] Documentation/litmus-tests: Add hazptr drain resnapshot-slot-race test Kunwu Chan
2026-11-04 16:54 ` [PATCH 18/28] Documentation/litmus-tests: Add hazptr drain safety test Kunwu Chan
2026-11-04 16:54 ` [PATCH 19/28] Documentation/litmus-tests: Add hazptr drain generation-visibility test Kunwu Chan
2026-11-04 16:54 ` [PATCH 20/28] locking/lockdep: Use hazptr to wait for dynamic key lookups Kunwu Chan
2026-11-04 16:54 ` [PATCH 21/28] hazptrtorture: Add HOTADDR and LONGHOLD scenarios Kunwu Chan
2026-11-04 16:54 ` [PATCH 22/28] hazptrtorture: Add drain fairness rotation scenario Kunwu Chan
2026-11-04 16:54 ` [PATCH 23/28] rcuscale: Add hazptr scale type Kunwu Chan
2026-11-04 16:54 ` [PATCH 24/28] hazptr: Add runtime sysfs controls for stall detector Kunwu Chan
2026-11-04 16:54 ` [PATCH 25/28] hazptr: Select TRACING for HAZPTR_TORTURE_TEST to enable tracepoint Kunwu Chan
2026-11-04 16:54 ` [PATCH 26/28] hazptrtorture: Add torture_type=none clean skip for rcuscale coexistence Kunwu Chan
2026-11-04 16:54 ` [PATCH 27/28] hazptr: Add Bloom false-positive exact-match fallback Kunwu Chan
2026-11-04 16:54 ` [PATCH 28/28] Documentation/litmus-tests: Add hazptr bloom wildcard-hole test Kunwu Chan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261104165504.2430480-13-kunwu.chan@gmail.com \
--to=kunwu.chan@gmail.com \
--cc=akiyks@gmail.com \
--cc=boqun@kernel.org \
--cc=brads@mainlining.org \
--cc=corbet@lwn.net \
--cc=dave@stgolabs.net \
--cc=dhowells@redhat.com \
--cc=dlustig@nvidia.com \
--cc=frederic@kernel.org \
--cc=j.alglave@ucl.ac.uk \
--cc=jiangshanlai@gmail.com \
--cc=joelagnelf@nvidia.com \
--cc=josh@joshtriplett.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=lkmm@lists.linux.dev \
--cc=longman@redhat.com \
--cc=luc.maranget@inria.fr \
--cc=mathieu.desnoyers@efficios.com \
--cc=mingo@redhat.com \
--cc=neeraj.upadhyay@kernel.org \
--cc=parri.andrea@gmail.com \
--cc=paulmck@kernel.org \
--cc=qiang.zhang@linux.dev \
--cc=rcu@vger.kernel.org \
--cc=rdunlap@infradead.org \
--cc=rostedt@goodmis.org \
--cc=skhan@linuxfoundation.org \
--cc=stern@rowland.harvard.edu \
--cc=urezki@gmail.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®