hazptr: batch synchronize operations through a shared scan
Subsystems (8)
v3 cover hazptr: batch synchronize operations through a shared scan
This series extends Mathieu Desnoyers's hazard pointer implementation [1] with a shared scan path for concurrent hazptr_synchronize() callers, and adapts the lockdep dynamic-key hashlist use case from Boqun Feng's 2025 shazptr series [2] to the current hazptr API.
The series also adds rcuscale support, torture coverage, and litmus tests for the hazptr implementation.
The lockdep conversion replaces the expedited RCU wait in lockdep_unregister_key() with hazptr_synchronize(). This limits the wait to hazard pointers protecting the target hash bucket instead of waiting for a system-wide expedited RCU grace period.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
Performance data
================
The measurements below were collected on an ARM64 KVM guest running on an ARM64 server (96 vCPUs, 8 GB RAM), unless noted otherwise. The kernel is based on Paul McKenney's -rcu tree "dev" branch at commit d21906b0aa1e ("doc: Document additional RCU task-stall dump information") with the full hazptr patch series applied.
CONFIG_PREEMPT=y, CONFIG_PREEMPT_RCU=y, CONFIG_NR_CPUS=256. Configuration is saved alongside each result set.
Following Paul's suggestion, the reader-side refscale numbers are reported separately for CONFIG_PROVE_LOCKING=n and CONFIG_PROVE_LOCKING=y, since lockdep instrumentation significantly affects the measured RCU and SRCU reader-side costs but has little effect on the hazptr reader path in this setup.
Lockdep workload -- tc qdisc mq x100, CONFIG_PROVE_LOCKING=y, 96 background hazptr readers:
function-call IPIs (100 tc qdisc operations): 183
For reference, insmod/rmmod x10 in the same setup generated 4930 function-call IPIs.
The lockdep_unregister_key() path is the motivating use case for this series: it replaces a system-wide synchronize_rcu_expedited() call with a hazptr_synchronize() scoped to the per-key hash bucket.
Writer-side effective per-GP time (rcuscale, nwriters=16, nreaders=0, one run per CPU count; computed as total test duration divided by the total number of grace periods across all writers):
hazptr RCU Tree SRCU CPUs per-GP per-GP per-GP ----------------------------------------------------------- 24 498 us 863 us 632 us 96 493 us 865 us 599 us 128 493 us 873 us 789 us 256 500 us 1162 us 476 us
At 96 CPUs the measurements are from the comparison blocks in the same guest boot (order: hazptr nw=16, hazptr nw=1, hazptr nw=16, rcu nw=1, rcu nw=16, srcu nw=1, srcu nw=16). The hazptr scalability row uses the first hazptr nw=16 block, which is consistent with the single per-CPU runs at 24, 128, and 256 CPUs. RCU and Tree SRCU use their standard implementations.
Hazptr effective per-GP time stays nearly constant from 24 to 256 CPUs, remaining within 493-500 us, consistent with the shared-scan kthread amortising the scan across concurrent synchronize callers. At 96 CPUs, the effective per-GP times are approximately 865 us for RCU, 599 us for SRCU, and 493 us for hazptr with 16 concurrent callers.
For comparison, the nwriters=1, nreaders=0 case measured about 19 us per grace period in this run at 96 CPUs.
Reader-side overhead (refscale, nreaders=-1 for 75% of 96 online CPUs, nruns=5, median of 5 runs per scale type):
The lockdep=n and lockdep=y refscale numbers were collected in separate QEMU boots from kernels built from the same source tree with PROVE_LOCKING toggled. The first hazptr block in each boot is used. All 96 vCPUs were visible to the guest (CONFIG_NR_CPUS=256).
PROVE_LOCKING=n PROVE_LOCKING=y
hazptr 26.1 ns 25.6 ns
RCU 5.0 ns 165.5 ns
SRCU 38.8 ns 192.3 ns
Hazptr reader-side overhead is nearly unchanged with and without CONFIG_PROVE_LOCKING in this setup, while RCU and SRCU show substantially higher costs when lockdep is enabled. This is why the refscale results are reported separately for the two configurations.
Hazptr torture regression (CONFIG_PROVE_LOCKING=y):
basic, lockdep+wq_churn, and SLOWPATH PASS at 96 CPUs; CPU sweep (8/16/32/64/128/256) also PASS with no lockdep warnings.
Additional x86 server testing with Lian Wang is planned(maybe after LPC).
Test reproducibility
---------------------
Across five refscale runs, the standard deviation was 0.16 ns for hazptr and 0.12 ns for RCU with PROVE_LOCKING=n. Rcuscale data points are based on a single test run per CPU count, but each run covers thousands of individual grace periods and the hazptr measurements remain within 493-500us across the tested CPU counts. The lockdep workload measurement uses 100 tc qdisc operations in a single QEMU boot; repeated runs agree to within ~10 IPIs on this server.
Changes since v2
================
- v2: https://lore.kernel.org/all/[email protected]/
Only cover letter changes; no code changes since v2.
- Fixed attribution: the base hazptr implementation is Mathieu Desnoyers's work.
- Split the refscale reader-overhead table into separate PROVE_LOCKING=n and PROVE_LOCKING=y columns, following Paul's suggestion, so that the lockdep impact on RCU and SRCU fast paths is visible rather than folded into a single number.
- Replaced the rcuscale "per-writer latency" column with per-grace-period values. The new metric (total test duration divided by total grace periods) is more directly interpretable when comparing 1-vs-16 concurrent synchronize callers. The nw=16 comparison now uses the same rcuscale test block for hazptr, RCU, and SRCU at each CPU count. The nw=1 baseline is provided for reference so that the reader can see the single- writer cost and the per-GP cost under 16 concurrent callers side by side.
- Added test-condition and reproducibility notes (kernel commit, PREEMPT model, visible CPU count, boot ordering, standard deviation, sample sizes).
- Removed the unreviewed srcua scale-type patch from this series; it will be posted separately.
Changes since RFC/WIP
=====================
- RFC/WIP: https://lore.kernel.org/all/[email protected]/
- Split the original 4-patch RFC/WIP into smaller commits covering shared scanning, correctness, API support, lockdep, scaling, and torture testing.
- Incorporated Boqun Feng's review feedback: use a Bloom filter to avoid per-waiter allocation, add scoped_guard() support, and add a debug option to force the hazptr acquire slow path.
- Fixed scan ordering around backup-slot promotion by scanning all per-CPU slots before the overflow lists, with a separate overflow-list phase.
- Simplified the scan cycle to flip first and drain only the old wildcard generation, with herd7-verified LKMM tests for both the in-flight and resolved publication cases.
- Extended rcuscale and hazptrtorture coverage, added a selftest script for the torture configurations, and fixed the hazptr_release() kernel-doc.
Kunwu Chan (15):
hazptr: add shared scan kthread
hazptr: use Bloom filter for shared scan waiters
hazptr: scan all per-CPU slots before overflow lists
hazptr: add scoped_guard() support
hazptr: add debug option to force the acquire slow path
hazptr: elide redundant first drain pass Documentation/litmus-tests: add hazptr wildcard-flip escape test locking/lockdep: use hazptr to wait for dynamic key lookups
rcuscale: add hazptr scale type
hazptr: fix kernel-doc of hazptr_release() Documentation/litmus-tests: add hazptr acquire-before-scan test
hazptrtorture: add slowpath and lockdep scenarios
hazptrtorture: add READERS4 and READERS0 torture configs
hazptrtorture: add 128- and 256-CPU configs selftests/rcutorture: add hazptr torture test script
Documentation/litmus-tests/README | 13 + .../hazptr/hazptr-acquire-before-scan.litmus | 45 +++ .../hazptr/hazptr-wildcard-flip-escape.litmus | 46 +++ include/linux/hazptr.h | 56 ++- kernel/hazptr.c | 336 +++++++++++++++++- kernel/locking/lockdep.c | 25 +- kernel/rcu/Kconfig.debug | 10 + kernel/rcu/hazptrtorture.c | 57 ++- kernel/rcu/rcuscale.c | 70 +++- .../selftests/rcutorture/bin/hazptr.sh | 146 ++++++++ .../rcutorture/configs/hazptr/CFLIST | 6 + .../rcutorture/configs/hazptr/CPU128 | 16 + .../rcutorture/configs/hazptr/CPU128.boot | 1 + .../rcutorture/configs/hazptr/CPU256 | 16 + .../rcutorture/configs/hazptr/CPU256.boot | 1 + .../rcutorture/configs/hazptr/LOCKDEP | 17 + .../rcutorture/configs/hazptr/LOCKDEP.boot | 1 + .../rcutorture/configs/hazptr/READERS0 | 16 + .../rcutorture/configs/hazptr/READERS0.boot | 2 + .../rcutorture/configs/hazptr/READERS4 | 16 + .../rcutorture/configs/hazptr/READERS4.boot | 2 + .../rcutorture/configs/hazptr/SLOWPATH | 16 + 22 files changed, 881 insertions(+), 33 deletions(-) create mode 100644 Documentation/litmus-tests/hazptr/hazptr-acquire-before-scan.litmus create mode 100644 Documentation/litmus-tests/hazptr/hazptr-wildcard-flip-escape.litmus create mode 100755 tools/testing/selftests/rcutorture/bin/hazptr.sh create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/CPU128 create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/CPU128.boot create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/CPU256 create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/CPU256.boot create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/LOCKDEP create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/LOCKDEP.boot create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/READERS0 create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/READERS0.boot create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/READERS4 create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/READERS4.boot create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/SLOWPATH
base-commit: d21906b0aa1e9573cdb5e7acaca44966b9d1dcd2
Signature · 1 line
2.43.0
RFC PATCH v3 1/15 hazptr: add shared scan kthread
Batch concurrent hazptr_synchronize() callers into a shared scan cycle, avoiding redundant wildcard flips and slot scans.
Queue waiters to a kthread and perform a two-phase wildcard scan once for all queued waiters. After both phases, complete waiters whose address is no longer held by any slot; waiters that remain blocked are retried in a later cycle.
Waiters are embedded in the caller's stack frame, so no dynamic allocation is needed in the synchronize path.
Fall back to the direct scan if the scan kthread is unavailable.
Adapted from the scan-kthread approach in Boqun Feng's shazptr implementation.
--- kernel/hazptr.c | 201 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 201 insertions(+)
kernel/hazptr.c +201 -0
@@ -12,6 +12,10 @@#include <linux/mutex.h> #include <linux/list.h> #include <linux/export.h> +#include <linux/completion.h> +#include <linux/kthread.h> +#include <linux/swait.h> +#include <linux/sched.h> /* * The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee
@@ -209,12 +213,179 @@ void hazptr_scan_period(void *addr, void *scan_wildcard) } } +struct hazptr_waiter { +struct list_head node; +void *addr; +struct completion done; +}; + +struct hazptr_scan_state { +struct task_struct *kthread; +struct swait_queue_head wq; +bool wakeup; +struct mutex lock; +struct list_head pending; +struct list_head scanning;/* kthread only */ +}; +static struct hazptr_scan_state hazptr_scan; + +/* +* Check per-CPU slots before overflow-list slots to match the +* acquisition ordering of promoted slots. +*/ +static bool hazptr_value_present (void *val) +{ +int cpu; + +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]; + +/* Pairs with smp_store_release in hazptr_release(). */ +if (smp_load_acquire (&item->slot.addr) == val) +return true; + } +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) { +/* Pairs with smp_store_release in hazptr_release(). */ +if (smp_load_acquire (&b->slot.addr) == val) { +raw_spin_unlock_irqrestore (&list->lock, flags); +return true; + } + } +raw_spin_unlock_irqrestore (&list->lock, flags); + } + } +return false; +} + +/* +* Wait until no per-CPU slot or overflow-list slot holds @wc. +* Callers must ensure that the wildcard value in use by new acquires +* differs from @wc, so that the set of slots holding @wc only +* shrinks, which guarantees forward progress. +*/ +static void hazptr_drain_wildcard (void *wc) +{ +while (hazptr_value_present (wc)) +cond_resched (); +} + +/* +* Move pending waiters to ->scanning and perform a two-phase +* wildcard scan shared by all waiters. +*/ +static void hazptr_scan_do_cycle (void ) +{ +void *scan_wildcard, *old_wildcard; +struct hazptr_waiter *w, *n; +LIST_HEAD (done); + +mutex_lock (&hazptr_wildcard_lock); + +mutex_lock (&hazptr_scan.lock); +list_splice_tail_init (&hazptr_scan.pending, &hazptr_scan.scanning); +mutex_unlock (&hazptr_scan.lock); + +if (list_empty (&hazptr_scan.scanning)) { +mutex_unlock (&hazptr_wildcard_lock); +return ; + } + +/* Pass 1: drain the unpublished wildcard. */ + scan_wildcard =flip_wildcard (READ_ONCE (hazptr_wildcard)); +hazptr_drain_wildcard (scan_wildcard); + +/* Flip so new acquires use the new generation. */ +WRITE_ONCE (hazptr_wildcard, scan_wildcard); + old_wildcard =flip_wildcard (scan_wildcard); + +/* Pass 2: drain the old wildcard. */ +hazptr_drain_wildcard (old_wildcard); + +/* Complete waiters whose address is no longer held by any slot. */ +list_for_each_entry_safe (w, n, &hazptr_scan.scanning, node) { +if (!hazptr_value_present (w->addr)) +list_move (&w->node, &done); + } + +mutex_unlock (&hazptr_wildcard_lock); + +list_for_each_entry_safe (w, n, &done, node) { +list_del_init (&w->node); +complete (&w->done); + } +} + +static int hazptr_scan_kthread (void *unused) +{ +for (;;) { +bool idle; + +swait_event_idle_exclusive (hazptr_scan.wq, +READ_ONCE (hazptr_scan.wakeup)); + +hazptr_scan_do_cycle (); + +mutex_lock (&hazptr_scan.lock); + idle =list_empty (&hazptr_scan.pending) && +list_empty (&hazptr_scan.scanning); +if (idle) +WRITE_ONCE (hazptr_scan.wakeup, false); +mutex_unlock (&hazptr_scan.lock); + +if (idle) +continue ; +/* Waiters still blocked: retry after a short delay. */ +schedule_timeout_idle (1 ); + } +return 0 ; +} + +/* +* Queue @addr for the shared scan. The waiter lives on the +* caller's stack, so no allocation is needed. +*/ +static void hazptr_synchronize_queued (void *addr) +{ +struct hazptr_waiter waiter = { + .addr = addr, + }; + +init_completion (&waiter.done); +INIT_LIST_HEAD (&waiter.node); + +/* Enqueue and wake the scan kthread. */ +mutex_lock (&hazptr_scan.lock); +list_add_tail (&waiter.node, &hazptr_scan.pending); +if (!READ_ONCE (hazptr_scan.wakeup)) { +WRITE_ONCE (hazptr_scan.wakeup, true); +swake_up_one (&hazptr_scan.wq); + } +mutex_unlock (&hazptr_scan.lock); + +/* Sleep until the scan kthread completes this waiter. */ +wait_for_completion (&waiter.done); +} +/* * hazptr_synchronize: Wait until @addr is released from all slots. * * Wait to observe that each slot contains a value that differs from * @addr before returning. * Should be called from preemptible context. +* +* If available, queue the caller for a shared scan; otherwise use +* the direct scan path. */ void hazptr_synchronize (void *addr) {
@@ -235,6 +406,13 @@ void hazptr_synchronize(void *addr)/* Memory ordering: Store A before Load B. */ smp_mb (); +/* Pairs with smp_store_release in hazptr_scan_init(). */ +if (smp_load_acquire (&hazptr_scan.kthread)) { +hazptr_synchronize_queued (addr); +return ; + } + +/* Fallback: use the direct scan path. */ guard (mutex)(&hazptr_wildcard_lock); scan_wildcard =flip_wildcard (hazptr_wildcard);hazptr_scan_period (addr, scan_wildcard);
@@ -282,3 +460,26 @@ void __init hazptr_init(void) } } } + +/* +* Initialize the scan kthread. Failure falls back to the +* direct scan. +*/ +static int __init hazptr_scan_init (void ) +{ +struct task_struct *t; + +init_swait_queue_head (&hazptr_scan.wq); +mutex_init (&hazptr_scan.lock); +INIT_LIST_HEAD (&hazptr_scan.pending); +INIT_LIST_HEAD (&hazptr_scan.scanning); + + t =kthread_run (hazptr_scan_kthread,NULL ,"hazptr_scan" ); +if (!IS_ERR (t)) +/* Pairs with smp_load_acquire in hazptr_synchronize(). */ +smp_store_release (&hazptr_scan.kthread, t); +else +pr_warn ("hazptr: scan kthread failed, using direct scan\n" ); +return 0 ; +} +core_initcall (hazptr_scan_init);
Signature · 1 line
2.43.0
RFC PATCH v3 2/15 hazptr: use Bloom filter for shared scan waiters
In reply to Kunwu ChanReplace the per-waiter exact-match scan with a shared Bloom filter built during the second drain pass: each observed non-wildcard address sets three bits, so 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 final drain 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, so waiter state remains on the caller's stack and no dynamic allocation is needed.
--- kernel/hazptr.c | 93 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 10 deletions(-)
kernel/hazptr.c +83 -10
@@ -7,6 +7,7 @@*/ #include <linux/hazptr.h> +#include <linux/bitops.h> #include <linux/percpu.h> #include <linux/spinlock.h> #include <linux/mutex.h>
@@ -16,6 +17,7 @@#include <linux/kthread.h> #include <linux/swait.h> #include <linux/sched.h> +#include <linux/bitmap.h> /* * The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee
@@ -213,6 +215,55 @@ void hazptr_scan_period(void *addr, void *scan_wildcard) } } +/* 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; +} +struct hazptr_waiter {struct list_head node;void *addr;
@@ -226,17 +277,26 @@ 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;/* -* Check per-CPU slots before overflow-list slots to match the -* acquisition ordering of promoted slots. +* Walk all slots and return true if @watch is present. If @bloom +* is non-NULL, record observed non-wildcard addresses in it. +* +* Per-CPU slots are examined before overflow-list slots on each CPU +* to preserve the acquisition ordering required by the promote path: +* synchronize must observe the per-CPU slot release before the +* overflow-list entry can be missed. */ -static bool hazptr_value_present (void *val) +static bool hazptr_scan_walk (void *watch,struct hazptr_bloom *bloom) {int cpu; +if (bloom) +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);
@@ -244,10 +304,14 @@ static bool hazptr_value_present(void *val)for (idx =0 ; idx <NR_HAZPTR_PERCPU_SLOTS ; idx++) {struct hazptr_slot_item *item = &percpu_slots->items[idx]; +void *v;/* Pairs with smp_store_release in hazptr_release(). */ -if (smp_load_acquire (&item->slot.addr) == val) + v =smp_load_acquire (&item->slot.addr); +if (v == watch)return true; +if (bloom && 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];
@@ -256,11 +320,16 @@ static bool hazptr_value_present(void *val)raw_spin_lock_irqsave (&list->lock, flags);hlist_for_each_entry (b, &list->head, overflow_node) { +void *v; +/* Pairs with smp_store_release in hazptr_release(). */ -if (smp_load_acquire (&b->slot.addr) == val) { + v =smp_load_acquire (&b->slot.addr); +if (v == watch) {raw_spin_unlock_irqrestore (&list->lock, flags);return true; } +if (bloom && v && !is_wildcard (v)) +hazptr_bloom_add (bloom, v); }raw_spin_unlock_irqrestore (&list->lock, flags); }
@@ -276,7 +345,7 @@ static bool hazptr_value_present(void *val)*/ static void hazptr_drain_wildcard (void *wc) { -while (hazptr_value_present (wc)) +while (hazptr_scan_walk (wc,NULL ))cond_resched (); }
@@ -309,12 +378,16 @@ static void hazptr_scan_do_cycle(void)WRITE_ONCE (hazptr_wildcard, scan_wildcard); old_wildcard =flip_wildcard (scan_wildcard); -/* Pass 2: drain the old wildcard. */ -hazptr_drain_wildcard (old_wildcard); +/* +* Pass 2: drain the old wildcard while collecting observed +* addresses into the Bloom filter. +*/ +while (hazptr_scan_walk (old_wildcard, &hazptr_scan.bloom)) +cond_resched (); -/* Complete waiters whose address is no longer held by any slot. */ +/* Complete waiters whose address is not in the filter. */ list_for_each_entry_safe (w, n, &hazptr_scan.scanning, node) { -if (!hazptr_value_present (w->addr)) +if (!hazptr_bloom_contains (&hazptr_scan.bloom, w->addr))list_move (&w->node, &done); }
Signature · 1 line
2.43.0
RFC PATCH v3 3/15 hazptr: scan all per-CPU slots before overflow lists
In reply to Kunwu ChanBoqun Feng pointed out that a promote racing a scan can cause the scan to miss a hazard pointer: hazptr_promote_to_backup_slot() chains the backup slot into an overflow list before clearing the per-CPU slot, so the scan must observe the old location before the new one. The scan currently visits each CPU's overflow lists right after that CPU's per-CPU slots, so a promote that moves a hazard pointer from a per-CPU slot of a later-scanned CPU into an overflow list of an earlier-scanned CPU can be missed.
The direct two-phase scan has a related race because the overflow list selection follows the wildcard phase: a promote racing the wildcard flip can move a backup slot to the list not covered by the second phase.
Fix this by scanning all per-CPU slots before any overflow-list slot, in both the shared-scan walk and the direct fallback scan. Give the overflow lists a flip phase of their own, independent of the wildcard, so each list is scanned while it is the non-live one:
new backup slots are chained to the other list, preserving forward progress.
The same race was also addressed by Mathieu Desnoyers.
--- kernel/hazptr.c | 88 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 24 deletions(-)
kernel/hazptr.c +64 -24
@@ -23,13 +23,20 @@* The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee * hazptr_synchronize forward progress even with a steady stream of readers. * This wildcard value is used by acquire to temporarily tag the per-CPU slots. -* This also affects the overflow list selection: the current list used by -* readers is array[(unsigned long) hazptr_wildcard - 1]. */ -static DEFINE_MUTEX (hazptr_wildcard_lock);/* Protect the wildcard flip. */ +static DEFINE_MUTEX (hazptr_phase_lock); +/* Protect the wildcard and overflow-list phase flips. */ void *hazptr_wildcard = (void *)1UL ;EXPORT_SYMBOL_GPL (hazptr_wildcard); +/* +* The current overflow list phase. Independent from the wildcard so that the +* overflow list scan can be placed after the per-CPU slot scan while still +* scanning the non-live list: the current list used by readers is +* array[hazptr_overflow_list_phase]. +*/ +static unsigned int hazptr_overflow_list_phase; +struct hazptr_overflow_list {raw_spinlock_t lock;/* Lock protecting overflow list and list generation. */ struct hlist_head head;/* Overflow list head. */
@@ -59,6 +66,12 @@ void *flip_wildcard(void *wildcard)return ((unsigned long ) wildcard ==1UL ) ? (void *)2UL : (void *)1UL ; } +static +unsigned int flip_list_phase (unsigned int phase) +{ +return 1 - phase; +} +static bool is_wildcard (void *addr) {
@@ -184,15 +197,12 @@ void hazptr_synchronize_cpu_slots(int cpu, void *addr, void *scan_wildcard) }static -void hazptr_scan_period (void *addr,void *scan_wildcard) +void hazptr_scan_cpu_slots_period (void *addr,void *scan_wildcard) { -unsigned int scan_idx = (unsigned long ) scan_wildcard -1 ;int cpu;/* Scan all CPUs slots. */ for_each_possible_cpu (cpu) { -struct hazptr_overflow_list_flip *overflow_list_flip =per_cpu_ptr (&percpu_overflow_list_flip, cpu); -/* * Scan CPU slots. * Forward progress against recurring wildcards is guaranteed
@@ -205,12 +215,22 @@ void hazptr_scan_period(void *addr, void *scan_wildcard)* to acquire that same hazard pointer value. */ hazptr_synchronize_cpu_slots (cpu, addr, scan_wildcard); + } +} + +static +void hazptr_scan_overflow_list_period (void *addr,unsigned int scan_idx) +{ +int cpu; + +/* +* Scan backup slots in percpu overflow lists. +* Forward progress is guaranteed by scanning one list +* while new elements are added into the other list. +*/ +for_each_possible_cpu (cpu) { +struct hazptr_overflow_list_flip *overflow_list_flip =per_cpu_ptr (&percpu_overflow_list_flip, cpu); -/* -* Scan backup slots in percpu overflow lists. -* Forward progress is guaranteed by scanning one list -* while new elements are added into the other list. -*/ hazptr_synchronize_overflow_list (&overflow_list_flip->array[scan_idx], addr); } }
@@ -285,10 +305,13 @@ static struct hazptr_scan_state hazptr_scan;* Walk all slots and return true if @watch is present. If @bloom * is non-NULL, record observed non-wildcard addresses in it. * -* Per-CPU slots are examined before overflow-list slots on each CPU -* to preserve the acquisition ordering required by the promote path: -* synchronize must observe the per-CPU slot release before the -* overflow-list entry can be missed. +* All per-CPU slots are examined before any overflow-list slot: a +* promote (hazptr_detach() or a context switch) moves a hazard +* pointer from a per-CPU slot to an overflow list by chaining the +* backup slot before clearing the per-CPU slot, see +* hazptr_promote_to_backup_slot(). Therefore the scan must observe +* the old location before the new location for every slot/list +* pair, including pairs on different CPUs. */ static bool hazptr_scan_walk (void *watch,struct hazptr_bloom *bloom) {
@@ -297,9 +320,9 @@ static bool hazptr_scan_walk(void *watch, struct hazptr_bloom *bloom)if (bloom)hazptr_bloom_reset (bloom); +/* Scan all per-CPU slots before any overflow-list slot. */ 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++) {
@@ -313,6 +336,10 @@ static bool hazptr_scan_walk(void *watch, struct hazptr_bloom *bloom)if (bloom && v && !is_wildcard (v))hazptr_bloom_add (bloom, v); } + } +for_each_possible_cpu (cpu) { +struct hazptr_overflow_list_flip *overflow_list_flip =per_cpu_ptr (&percpu_overflow_list_flip, cpu); +for (int i =0 ; i <2 ; i++) {struct hazptr_overflow_list *list = &overflow_list_flip->array[i];struct hazptr_backup_slot *b;
@@ -359,14 +386,14 @@ static void hazptr_scan_do_cycle(void)struct hazptr_waiter *w, *n;LIST_HEAD (done); -mutex_lock (&hazptr_wildcard_lock); +mutex_lock (&hazptr_phase_lock);mutex_lock (&hazptr_scan.lock);list_splice_tail_init (&hazptr_scan.pending, &hazptr_scan.scanning);mutex_unlock (&hazptr_scan.lock);if (list_empty (&hazptr_scan.scanning)) { -mutex_unlock (&hazptr_wildcard_lock); +mutex_unlock (&hazptr_phase_lock);return ; }
@@ -391,7 +418,7 @@ static void hazptr_scan_do_cycle(void)list_move (&w->node, &done); } -mutex_unlock (&hazptr_wildcard_lock); +mutex_unlock (&hazptr_phase_lock);list_for_each_entry_safe (w, n, &done, node) {list_del_init (&w->node);
@@ -463,6 +490,7 @@ static void hazptr_synchronize_queued(void *addr)void hazptr_synchronize (void *addr) {void *scan_wildcard; +unsigned int scan_list_phase;/* * Busy-wait should only be done from preemptible context.
@@ -486,18 +514,30 @@ void hazptr_synchronize(void *addr) }/* Fallback: use the direct scan path. */ -guard (mutex)(&hazptr_wildcard_lock); +guard (mutex)(&hazptr_phase_lock); scan_wildcard =flip_wildcard (hazptr_wildcard); -hazptr_scan_period (addr, scan_wildcard); +/* Scan per-CPU slots. */ +hazptr_scan_cpu_slots_period (addr, scan_wildcard);WRITE_ONCE (hazptr_wildcard, scan_wildcard);/* Flip the current wildcard. */ -hazptr_scan_period (addr,flip_wildcard (scan_wildcard)); +hazptr_scan_cpu_slots_period (addr,flip_wildcard (scan_wildcard)); + +/* +* Scan overflow lists *after* scanning all per-CPU slots, see +* hazptr_promote_to_backup_slot(). Flip the overflow list phase +* between the two lists so that each list is scanned while it is +* the non-live one. +*/ + 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); +hazptr_scan_overflow_list_period (addr,flip_list_phase (scan_list_phase)); }EXPORT_SYMBOL_GPL (hazptr_synchronize);struct hazptr_slot *hazptr_chain_backup_slot (struct hazptr_ctx *ctx) {struct hazptr_overflow_list_flip *overflow_list_flip =this_cpu_ptr (&percpu_overflow_list_flip); -unsigned int list_idx = (unsigned long )READ_ONCE (hazptr_wildcard) -1 ; +unsigned int list_idx =READ_ONCE (hazptr_overflow_list_phase);struct hazptr_overflow_list *overflow_list = &overflow_list_flip->array[list_idx];struct hazptr_slot *slot = &ctx->backup_slot.slot;
Signature · 1 line
2.43.0
RFC PATCH v3 4/15 hazptr: add scoped_guard() support
In reply to Kunwu ChanAdd 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.
--- include/linux/hazptr.h | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)
include/linux/hazptr.h +51 -0
@@ -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 */
Signature · 1 line
2.43.0
RFC PATCH v3 5/15 hazptr: add debug option to force the acquire slow path
In reply to Kunwu ChanAdd a debug option that makes hazptr_acquire() always enter __hazptr_acquire(), even when a per-CPU slot is available.
This allows the slow path, including its lock acquisition, to be exercised by lockdep-heavy workloads. It also lets hazptrtorture exercise the slow path without requiring the per-CPU slots to be exhausted.
--- include/linux/hazptr.h | 3 ++- kernel/rcu/Kconfig.debug | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-)
include/linux/hazptr.h +2 -1
@@ -245,7 +245,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 (unlikely (slot->addr)) +if (IS_ENABLED (CONFIG_HAZPTR_ACQUIRE_FORCE_SLOWPATH ) || +unlikely (slot->addr))return __hazptr_acquire (ctx, addr_p);WRITE_ONCE (slot->addr,READ_ONCE (hazptr_wildcard));/* Store B */
kernel/rcu/Kconfig.debug +10 -0
@@ -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"
Signature · 1 line
2.43.0
RFC PATCH v3 6/15 hazptr: elide redundant first drain pass
In reply to Kunwu ChanEach scan cycle drained both wildcard generations: the unpublished "other" generation (pass 1) and the pre-flip current generation (pass 2). Pass 1 is not needed for correctness, so flip the wildcard first and drain only the old generation while building the Bloom filter.
An acquire that read the old wildcard before the flip may publish it into its slot after the drain has passed that slot. Such a straggling acquire cannot have loaded the pre-unpublish pointer; see the following LKMM test for the ordering argument.
--- kernel/hazptr.c | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-)
kernel/hazptr.c +13 -23
@@ -365,24 +365,19 @@ static bool hazptr_scan_walk(void *watch, struct hazptr_bloom *bloom) }/* -* Wait until no per-CPU slot or overflow-list slot holds @wc. -* Callers must ensure that the wildcard value in use by new acquires -* differs from @wc, so that the set of slots holding @wc only -* shrinks, which guarantees forward progress. -*/ -static void hazptr_drain_wildcard (void *wc) -{ -while (hazptr_scan_walk (wc,NULL )) -cond_resched (); -} - -/* -* Move pending waiters to ->scanning and perform a two-phase -* wildcard scan shared by all waiters. +* Move pending waiters to ->scanning, flip the wildcard, then +* drain the old generation while collecting observed addresses +* into the Bloom filter. New acquires use the new generation, +* so old-generation slots normally only drain. +* +* An acquire that read the old wildcard before the flip may +* publish it after the scanner has passed its slot, but cannot +* have loaded the pre-unpublish pointer. See +* Documentation/litmus-tests/hazptr/hazptr-wildcard-flip-escape.litmus. */ static void hazptr_scan_do_cycle (void ) { -void *scan_wildcard, *old_wildcard; +void *old_wildcard;struct hazptr_waiter *w, *n;LIST_HEAD (done);
@@ -397,16 +392,11 @@ static void hazptr_scan_do_cycle(void)return ; } -/* Pass 1: drain the unpublished wildcard. */ - scan_wildcard =flip_wildcard (READ_ONCE (hazptr_wildcard)); -hazptr_drain_wildcard (scan_wildcard); - -/* Flip so new acquires use the new generation. */ -WRITE_ONCE (hazptr_wildcard, scan_wildcard); - old_wildcard =flip_wildcard (scan_wildcard); + old_wildcard =READ_ONCE (hazptr_wildcard); +WRITE_ONCE (hazptr_wildcard,flip_wildcard (old_wildcard));/* -* Pass 2: drain the old wildcard while collecting observed +* Drain the old wildcard while collecting observed * addresses into the Bloom filter. */ while (hazptr_scan_walk (old_wildcard, &hazptr_scan.bloom))
Signature · 1 line
2.43.0
RFC PATCH v3 7/15 Documentation/litmus-tests: add hazptr wildcard-flip escape test
In reply to Kunwu ChanAdd a three-process litmus test for the in-flight wildcard window of the single-pass wildcard-flip scan.
P0 models hazptr_synchronize(): it unpublishes the object and enqueues the waiter after its memory barrier.
P1 models the scan kthread: it picks up the waiter, flips the wildcard, and performs the final hazard-pointer walk.
P2 models a straggling hazptr_acquire(): it reads and publishes the old wildcard, then loads the object after its publication barrier.
The forbidden outcome is that the final walk misses the straggler's slot while the straggler still loads the unpublished object.
Verified with herd7 (linux-kernel.cfg): Never 0 13.
--- .../hazptr/hazptr-wildcard-flip-escape.litmus | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Documentation/litmus-tests/hazptr/hazptr-wildcard-flip-escape.litmus
Documentation/litmus-tests/hazptr/hazptr-wildcard-flip-escape.litmus new file +46 -0
@@ -0,0 +1,46 @@ +C hazptr-wildcard-flip-escape + +(* + * Result: Never + * + * Check that a straggling acquire which published the old + * wildcard after the final walk cannot still load the + * unpublished object. + *) + +{ + int ptr = 3; + int hp = 0; + int wildcard = 1; + int enq = 0; +} + +P0(int *ptr, int *enq) +{ + WRITE_ONCE(*ptr, 0); + smp_mb(); + smp_store_release(enq, 1); +} + +P1(int *enq, int *wildcard, int *hp) +{ + int r1; + int r2; + + r1 = smp_load_acquire(enq); + WRITE_ONCE(*wildcard, 2); + r2 = READ_ONCE(*hp); +} + +P2(int *wildcard, int *hp, int *ptr) +{ + int r0; + int r3; + + r0 = READ_ONCE(*wildcard); + WRITE_ONCE(*hp, r0); + smp_mb(); + r3 = READ_ONCE(*ptr); +} + +exists (1:r1=1 /\ 1:r2=0 /\ 2:r3=3)
Signature · 1 line
2.43.0
RFC PATCH v3 8/15 locking/lockdep: use hazptr to wait for dynamic key lookups
In reply to Kunwu Chanlockdep_unregister_key() waits for is_dynamic_key() callers with synchronize_rcu_expedited(), which requires system-wide coordination. Make is_dynamic_key() protect the hash bucket with a hazard pointer and use hazptr_synchronize() to wait for traversals of that bucket.
The hash bucket returned by keyhashentry() has a stable address, so it can be used as the hazptr synchronization target. Keep the key hashlist lifetime RCU-based with hlist_del_rcu() and call_rcu().
Protect the traversal with the hazptr scoped_guard() added earlier in this series.
This adapts the lockdep use case from Boqun Feng's shazptr series to the current hazptr API.
--- kernel/locking/lockdep.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)
kernel/locking/lockdep.c +15 -10
@@ -58,6 +58,7 @@#include <linux/context_tracking.h> #include <linux/console.h> #include <linux/kasan.h> +#include <linux/hazptr.h> #include <asm/sections.h>
@@ -1280,14 +1281,19 @@ static bool is_dynamic_key(const struct lock_class_key *key) hash_head =keyhashentry (key); -rcu_read_lock (); -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { -if (k == key) { - found = true; -break ; + { +struct hazptr_ctx ctx; +void *bucket = hash_head; + + scoped_guard(hazptr, &ctx, &bucket) { +hlist_for_each_entry_rcu (k, hash_head, hash_entry,1 ) { +if (k == key) { + found = true; +break ; + } + } } } -rcu_read_unlock ();return found; }
@@ -6683,11 +6689,10 @@ void lockdep_unregister_key(struct lock_class_key *key)* * Some operations like __qdisc_destroy() will call this in a debug * kernel, and the network traffic is disabled while waiting, hence -* the delay of the wait matters in debugging cases. Currently use a -* synchronize_rcu_expedited() to speed up the wait at the cost of -* system IPIs. TODO: Replace RCU with hazptr for this. +* the delay of the wait matters in debugging cases. Replace the +* expedited RCU wait with hazptr_synchronize(). */ -synchronize_rcu_expedited (); +hazptr_synchronize (keyhashentry (key)); }EXPORT_SYMBOL_GPL (lockdep_unregister_key);
Signature · 1 line
2.43.0
RFC PATCH v3 9/15 rcuscale: add hazptr scale type
In reply to Kunwu ChanAdd hazptr reader and synchronize operations so that synchronize latency can be measured alongside RCU and SRCU.
Use a small set of objects so readers do not always protect the same target. Each reader protects the object selected by its CPU, while the updater round-robins through the objects.
Use hazptr_synchronize() for both normal and expedited scale operations, since hazptr has no separate expedited operation.
--- kernel/rcu/rcuscale.c | 70 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-)
kernel/rcu/rcuscale.c +69 -1
@@ -39,6 +39,7 @@#include <linux/torture.h> #include <linux/vmalloc.h> #include <linux/rcupdate_trace.h> +#include <linux/hazptr.h> #include <linux/sched/debug.h> #include "rcu.h"
@@ -418,6 +419,71 @@ static struct rcu_scale_ops tasks_tracing_ops = {#endif // #else // #ifdef CONFIG_TASKS_TRACE_RCU +#if IS_ENABLED (CONFIG_HAZPTR_TORTURE_TEST ) + +#define HAZPTR_SCALE_NR_OBJS 8 +static int hazptr_scale_obj[HAZPTR_SCALE_NR_OBJS ]; +static atomic_t hazptr_scale_sync_idx =ATOMIC_INIT (0 ); + +struct hazptr_scale_state { +struct hazptr_ctx ctx; +void *addr; +}; +static DEFINE_PER_CPU(struct hazptr_scale_state, hazptr_scale_state); + +static int hazptr_scale_read_lock (void ) +{ +struct hazptr_scale_state *state =this_cpu_ptr (&hazptr_scale_state); +int idx =smp_processor_id () %HAZPTR_SCALE_NR_OBJS ; +void *ptr = &hazptr_scale_obj[idx]; + + state->addr =hazptr_acquire (&state->ctx, &ptr); +return 0 ; +} + +static void hazptr_scale_read_unlock (int idx) +{ +struct hazptr_scale_state *state =this_cpu_ptr (&hazptr_scale_state); + +hazptr_release (&state->ctx, state->addr); +} + +static unsigned long hazptr_scale_completed (void ) +{ +return 0 ; +} + +static void hazptr_scale_sync (void ) +{ +int idx =atomic_fetch_inc (&hazptr_scale_sync_idx) %HAZPTR_SCALE_NR_OBJS ; + +hazptr_synchronize (&hazptr_scale_obj[idx]); +} + +static void hazptr_scale_sync_exp (void ) +{ +int idx =atomic_fetch_inc (&hazptr_scale_sync_idx) %HAZPTR_SCALE_NR_OBJS ; + +hazptr_synchronize (&hazptr_scale_obj[idx]); +} + +static struct rcu_scale_ops hazptr_scale_ops = { + .ptype =0 , + .readlock = hazptr_scale_read_lock, + .readunlock = hazptr_scale_read_unlock, + .get_gp_seq = hazptr_scale_completed, + .gp_diff =NULL , + .exp_completed = hazptr_scale_completed, + .sync = hazptr_scale_sync, + .exp_sync = hazptr_scale_sync_exp, + .name ="hazptr" , +}; + +#define HAZPTR_SCALE_OPS &hazptr_scale_ops, +#else +#define HAZPTR_SCALE_OPS +#endif +static unsigned long rcuscale_seq_diff (unsigned long new,unsigned long old) {if (!cur_ops->gp_diff)
@@ -1110,7 +1176,9 @@ rcu_scale_init(void)long i;long j;static struct rcu_scale_ops *scale_ops[] = { - &rcu_ops, &srcu_ops, &srcud_ops, TASKS_OPS TASKS_RUDE_OPS TASKS_TRACING_OPS + &rcu_ops, &srcu_ops, &srcud_ops, + TASKS_OPS TASKS_RUDE_OPS TASKS_TRACING_OPS +HAZPTR_SCALE_OPS };if (!torture_init_begin (scale_type, verbose))
Signature · 1 line
2.43.0
RFC PATCH v3 10/15 hazptr: fix kernel-doc of hazptr_release()
In reply to Kunwu ChanThe second parameter of hazptr_release() is named @addr, not @addr_p. It is the address passed to the matching hazptr_acquire().
Fix the kernel-doc header to match the API.
--- include/linux/hazptr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/hazptr.h +1 -1
@@ -293,7 +293,7 @@ static inline void hazptr_release_debug(struct hazptr_ctx *ctx, void *addr) { }* hazptr_release - Release the specified hazard pointer * * @ctx: The hazard-pointer context that was passed to hazptr_acquire(). -* @addr_p: The pointer that is to be hazard-pointer unprotected. +* @addr: The address passed to the matching hazptr_acquire(). * * Release the protected hazard pointer recorded in @ctx. *
Signature · 1 line
2.43.0
RFC PATCH v3 11/15 Documentation/litmus-tests: add hazptr acquire-before-scan test
In reply to Kunwu ChanAdd an LKMM test for the hazard-pointer publication protocol used by the lockdep conversion.
The test checks that a scan cannot miss a hazard-pointer publication while the reader still observes the pre-unpublish pointer. The smp_mb() pair provides the required ordering.
This covers the resolved-publication case. The in-flight wildcard window of the wildcard-flip scan is covered by hazptr-wildcard-flip-escape.litmus.
Verified with herd7: Never 0 3.
--- Documentation/litmus-tests/README | 13 ++++++ .../hazptr/hazptr-acquire-before-scan.litmus | 45 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 Documentation/litmus-tests/hazptr/hazptr-acquire-before-scan.litmus
Documentation/litmus-tests/README +13 -0
@@ -97,3 +97,16 @@ SRCU-fastpath-scan-before-anchor.litmus period, violating the SRCU grace-period guarantee. See SRCU-fastpath-anchor-before-scan.litmus for the opposite ordering. + + +hazptr (/hazptr directory) +-------------------------- + +hazptr-acquire-before-scan.litmus + Test the resolved-publication ordering between hazard-pointer + publication and the scan. + +hazptr-wildcard-flip-escape.litmus + Test the in-flight wildcard window of the single-pass wildcard-flip + scan: a straggling acquire that published the old wildcard after + the final walk cannot still load the unpublished object.
Documentation/litmus-tests/hazptr/hazptr-acquire-before-scan.litmus new file +45 -0
45 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v3 12/15 hazptrtorture: add slowpath and lockdep scenarios
In reply to Kunwu ChanAdd a wq_churn kthread and module parameter to exercise lockdep dynamic-key handling by repeatedly creating and destroying workqueues. Each workqueue creation registers a dynamic lock class key and each destruction unregisters it, exercising both is_dynamic_key() and lockdep_unregister_key().
Add SLOWPATH and LOCKDEP torture scenarios to exercise the hazptr acquire slow path, including its use by lockdep under CONFIG_PROVE_LOCKING.
Add both scenarios to CFLIST so that torture.sh --do-hazptr includes them.
--- kernel/rcu/hazptrtorture.c | 57 +++++++++++++++++-- .../rcutorture/configs/hazptr/CFLIST | 2 + .../rcutorture/configs/hazptr/LOCKDEP | 17 ++++++ .../rcutorture/configs/hazptr/LOCKDEP.boot | 1 + .../rcutorture/configs/hazptr/SLOWPATH | 16 ++++++ 5 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/LOCKDEP create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/LOCKDEP.boot create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/SLOWPATH
kernel/rcu/hazptrtorture.c +53 -4
@@ -23,6 +23,7 @@#include <linux/torture.h> #include <linux/hazptr.h> #include <linux/rcupdate.h> +#include <linux/workqueue.h> #include "rcu.h"
@@ -49,6 +50,8 @@ torture_param(int, shutdown_secs, 0, "Shutdown time (s), <= zero to disable.");torture_param (int, stat_interval,60 ,"Number of seconds between stats printk()s" );torture_param (int, stutter,5 ,"Number of seconds to run/halt test" );torture_param (int, verbose,1 ,"Enable verbose debugging printk()s" ); +torture_param (bool, wq_churn, false, +"Churn workqueues to exercise lockdep dynamic keys" );static char *torture_type ="hazptr" ;module_param (torture_type, charp,0444 );
@@ -60,6 +63,7 @@ static struct task_struct *preempt_task;static struct task_struct **reader_tasks;static struct task_struct *do_pending_task;static struct task_struct *stats_task; +static struct task_struct *wq_churn_task;#define HAZPTR_TORTURE_PIPE_LEN 10
@@ -87,6 +91,7 @@ static DEFINE_PER_CPU(atomic_long_t, hazptr_torture_acquires_irq); static DEFINE_PER_CPU(atomic_long_t, hazptr_torture_releases_irq); static DEFINE_PER_CPU(atomic_long_t, hazptr_torture_releases_defer); static DEFINE_PER_CPU(atomic_long_t, hazptr_torture_releases_undefer); +static atomic_t n_hazptr_torture_wq_churn;static struct list_head hazptr_torture_removed;// State for a deferred (AKA pending) hazard pointer
@@ -643,13 +648,14 @@ hazptr_torture_stats_print(void)atomic_read (&n_hazptr_torture_alloc_fail), atomic_read(&n_hazptr_torture_free));torture_onoff_stats (); -pr_cont ("acq: %lld rel: %lld acqirq: %lld relirq: %lld reldefer: %lld relundefer %lld\n" , +pr_cont ("acq: %lld rel: %lld acqirq: %lld relirq: %lld reldefer: %lld relundefer %lld wqchurn: %d\n" ,torture_sum_pcpu_atomic_long (&hazptr_torture_acquires),torture_sum_pcpu_atomic_long (&hazptr_torture_releases),torture_sum_pcpu_atomic_long (&hazptr_torture_acquires_irq),torture_sum_pcpu_atomic_long (&hazptr_torture_releases_irq),torture_sum_pcpu_atomic_long (&hazptr_torture_releases_defer), -torture_sum_pcpu_atomic_long (&hazptr_torture_releases_undefer)); +torture_sum_pcpu_atomic_long (&hazptr_torture_releases_undefer), +atomic_read (&n_hazptr_torture_wq_churn));pr_alert ("%s%s " , torture_type,TORTURE_FLAG );if (i >1 ) {
@@ -720,14 +726,14 @@ hazptr_torture_print_module_parms(struct hazptr_torture_ops *cur_ops, const char"defer_modulus=%d irq_acquire=%d irq_release=%d kthread_do_pending_ms=%d " "onoff_interval=%d onoff_holdoff=%d " "preempt_duration=%d preempt_interval=%d " -"reader_sleep_us=%d " +"reader_sleep_us=%d wq_churn=%d " "shuffle_interval=%d shutdown_secs=%d stat_interval=%d stutter=%d " "verbose=%d\n" , torture_type, tag, nrealreaders, nwriters, defer_modulus, irq_acquire, irq_release, kthread_do_pending_ms, onoff_interval, onoff_holdoff, preempt_duration, preempt_interval, - reader_sleep_us, + reader_sleep_us, wq_churn, shuffle_interval, shutdown_secs, stat_interval, stutter, verbose); }
@@ -761,6 +767,42 @@ static int hazptr_torture_preempt(void *unused)return 0 ; } +static void hazptr_torture_wq_churn_fn (struct work_struct *work) +{ +} + +/* +* Repeatedly create and destroy workqueues. Each workqueue creation +* registers a lockdep dynamic key and each destruction unregisters it, +* which exercises both is_dynamic_key() and lockdep_unregister_key(), +* the two lockdep paths that use hazptr. Most useful in kernels with +* CONFIG_PROVE_LOCKING=y and CONFIG_HAZPTR_ACQUIRE_FORCE_SLOWPATH=y. +*/ +static int hazptr_torture_wq_churn (void *unused) +{ +DEFINE_TORTURE_RANDOM (rand); + +VERBOSE_TOROUT_STRING ("hazptr_torture_wq_churn task started" ); +do { +struct workqueue_struct *wq; +struct work_struct churn_work; + + wq =alloc_workqueue ("hazptr-torture-churn" ,WQ_PERCPU ,1 ); +if (!wq) { +torture_hrtimeout_ms (10 ,10 , &rand); +continue ; + } +INIT_WORK (&churn_work, hazptr_torture_wq_churn_fn); +queue_work (wq, &churn_work); +flush_work (&churn_work); +destroy_workqueue (wq); +atomic_inc (&n_hazptr_torture_wq_churn); +stutter_wait ("hazptr_torture_wq_churn" ); + }while (!torture_must_stop ()); +torture_kthread_stopping ("hazptr_torture_wq_churn" ); +return 0 ; +} +static void hazptr_torture_cleanup (void ) {
@@ -775,6 +817,7 @@ hazptr_torture_cleanup(void)torture_stop_kthread (hazptr_torture_do_pending, do_pending_task);torture_stop_kthread (hazptr_torture_preempt, preempt_task); +torture_stop_kthread (hazptr_torture_wq_churn, wq_churn_task);torture_stop_kthread (hazptr_torture_writer, writer_task);if (reader_tasks) {
@@ -853,6 +896,7 @@ static int __init hazptr_torture_init(void)atomic_set (&n_hazptr_torture_alloc_fail,0 );atomic_set (&n_hazptr_torture_free,0 );atomic_set (&n_hazptr_torture_error,0 ); +atomic_set (&n_hazptr_torture_wq_churn,0 );for (i =0 ; i <HAZPTR_TORTURE_PIPE_LEN +1 ; i++)atomic_set (&hazptr_torture_wcount[i],0 );for_each_possible_cpu (cpu) {
@@ -928,6 +972,11 @@ static int __init hazptr_torture_init(void)if (torture_init_error (firsterr)) goto unwind; } +if (wq_churn) { + firsterr =torture_create_kthread (hazptr_torture_wq_churn,NULL , wq_churn_task); +if (torture_init_error (firsterr)) + goto unwind; + }torture_init_end ();return 0 ;
tools/testing/selftests/rcutorture/configs/hazptr/CFLIST +2 -0
@@ -1,2 +1,4 @@ NOPREEMPT PREEMPT +SLOWPATH +LOCKDEP
tools/testing/selftests/rcutorture/configs/hazptr/LOCKDEP new file +17 -0
@@ -0,0 +1,17 @@ +CONFIG_SMP=y +CONFIG_NR_CPUS=16 +CONFIG_PREEMPT_NONE=n +CONFIG_PREEMPT_VOLUNTARY=n +CONFIG_PREEMPT=y +CONFIG_HZ_PERIODIC=n +CONFIG_NO_HZ_IDLE=y +CONFIG_NO_HZ_FULL=n +CONFIG_HOTPLUG_CPU=y +CONFIG_SUSPEND=n +CONFIG_HIBERNATION=n +CONFIG_DEBUG_LOCK_ALLOC=y +CONFIG_PROVE_LOCKING=y +CONFIG_DEBUG_OBJECTS_RCU_HEAD=n +CONFIG_DEBUG_KERNEL=y +CONFIG_HAZPTR_DEBUG=y +CONFIG_HAZPTR_ACQUIRE_FORCE_SLOWPATH=y
tools/testing/selftests/rcutorture/configs/hazptr/LOCKDEP.boot new file +1 -0
@@ -0,0 +1 @@ +hazptrtorture.wq_churn=1
tools/testing/selftests/rcutorture/configs/hazptr/SLOWPATH new file +16 -0
@@ -0,0 +1,16 @@ +CONFIG_SMP=y +CONFIG_NR_CPUS=16 +CONFIG_PREEMPT_NONE=n +CONFIG_PREEMPT_VOLUNTARY=n +CONFIG_PREEMPT=y +CONFIG_HZ_PERIODIC=n +CONFIG_NO_HZ_IDLE=y +CONFIG_NO_HZ_FULL=n +CONFIG_HOTPLUG_CPU=y +CONFIG_SUSPEND=n +CONFIG_HIBERNATION=n +CONFIG_PROVE_LOCKING=n +CONFIG_DEBUG_OBJECTS_RCU_HEAD=n +CONFIG_DEBUG_KERNEL=y +CONFIG_HAZPTR_DEBUG=y +CONFIG_HAZPTR_ACQUIRE_FORCE_SLOWPATH=y
Signature · 1 line
2.43.0
RFC PATCH v3 13/15 hazptrtorture: add READERS4 and READERS0 torture configs
In reply to Kunwu ChanAdd READERS4 and READERS0 configs to cover reader-active and reader-free synchronize paths.
Both use the PREEMPT baseline and hazptr-stack torture type, with four readers for READERS4 and no readers for READERS0.
--- .../selftests/rcutorture/configs/hazptr/CFLIST | 2 ++ .../selftests/rcutorture/configs/hazptr/READERS0 | 16 ++++++++++++++++ .../rcutorture/configs/hazptr/READERS0.boot | 2 ++ .../selftests/rcutorture/configs/hazptr/READERS4 | 16 ++++++++++++++++ .../rcutorture/configs/hazptr/READERS4.boot | 2 ++ 5 files changed, 38 insertions(+) create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/READERS0 create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/READERS0.boot create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/READERS4 create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/READERS4.boot
tools/testing/selftests/rcutorture/configs/hazptr/CFLIST +2 -0
@@ -1,4 +1,6 @@ NOPREEMPT PREEMPT +READERS0 +READERS4 SLOWPATH LOCKDEP
tools/testing/selftests/rcutorture/configs/hazptr/READERS0 new file +16 -0
@@ -0,0 +1,16 @@ +CONFIG_SMP=y +CONFIG_NR_CPUS=16 +CONFIG_PREEMPT_NONE=n +CONFIG_PREEMPT_VOLUNTARY=n +CONFIG_PREEMPT=y +CONFIG_HZ_PERIODIC=n +CONFIG_NO_HZ_IDLE=y +CONFIG_NO_HZ_FULL=n +CONFIG_HOTPLUG_CPU=y +CONFIG_SUSPEND=n +CONFIG_HIBERNATION=n +CONFIG_DEBUG_LOCK_ALLOC=n +CONFIG_PROVE_LOCKING=n +CONFIG_DEBUG_OBJECTS_RCU_HEAD=n +CONFIG_DEBUG_KERNEL=y +CONFIG_HAZPTR_DEBUG=y
tools/testing/selftests/rcutorture/configs/hazptr/READERS0.boot new file +2 -0
@@ -0,0 +1,2 @@ +hazptrtorture.nreaders=0 +hazptrtorture.torture_type=hazptr-stack
tools/testing/selftests/rcutorture/configs/hazptr/READERS4 new file +16 -0
@@ -0,0 +1,16 @@ +CONFIG_SMP=y +CONFIG_NR_CPUS=16 +CONFIG_PREEMPT_NONE=n +CONFIG_PREEMPT_VOLUNTARY=n +CONFIG_PREEMPT=y +CONFIG_HZ_PERIODIC=n +CONFIG_NO_HZ_IDLE=y +CONFIG_NO_HZ_FULL=n +CONFIG_HOTPLUG_CPU=y +CONFIG_SUSPEND=n +CONFIG_HIBERNATION=n +CONFIG_DEBUG_LOCK_ALLOC=n +CONFIG_PROVE_LOCKING=n +CONFIG_DEBUG_OBJECTS_RCU_HEAD=n +CONFIG_DEBUG_KERNEL=y +CONFIG_HAZPTR_DEBUG=y
tools/testing/selftests/rcutorture/configs/hazptr/READERS4.boot new file +2 -0
@@ -0,0 +1,2 @@ +hazptrtorture.nreaders=4 +hazptrtorture.torture_type=hazptr-stack
Signature · 1 line
2.43.0
RFC PATCH v3 14/15 hazptrtorture: add 128- and 256-CPU configs
In reply to Kunwu ChanAdd CPU128 and CPU256 configs for hazptr-stack torture testing.
These configs exercise the shared-scan path at larger CPU counts.
--- .../selftests/rcutorture/configs/hazptr/CFLIST | 2 ++ .../selftests/rcutorture/configs/hazptr/CPU128 | 16 ++++++++++++++++ .../rcutorture/configs/hazptr/CPU128.boot | 1 + .../selftests/rcutorture/configs/hazptr/CPU256 | 16 ++++++++++++++++ .../rcutorture/configs/hazptr/CPU256.boot | 1 + 5 files changed, 36 insertions(+) create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/CPU128 create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/CPU128.boot create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/CPU256 create mode 100644 tools/testing/selftests/rcutorture/configs/hazptr/CPU256.boot
tools/testing/selftests/rcutorture/configs/hazptr/CFLIST +2 -0
@@ -4,3 +4,5 @@ READERS0 READERS4 SLOWPATH LOCKDEP +CPU128 +CPU256
tools/testing/selftests/rcutorture/configs/hazptr/CPU128 new file +16 -0
@@ -0,0 +1,16 @@ +CONFIG_SMP=y +CONFIG_NR_CPUS=128 +CONFIG_PREEMPT_NONE=n +CONFIG_PREEMPT_VOLUNTARY=n +CONFIG_PREEMPT=y +CONFIG_HZ_PERIODIC=n +CONFIG_NO_HZ_IDLE=y +CONFIG_NO_HZ_FULL=n +CONFIG_HOTPLUG_CPU=y +CONFIG_SUSPEND=n +CONFIG_HIBERNATION=n +CONFIG_DEBUG_LOCK_ALLOC=n +CONFIG_PROVE_LOCKING=n +CONFIG_DEBUG_OBJECTS_RCU_HEAD=n +CONFIG_DEBUG_KERNEL=y +CONFIG_HAZPTR_DEBUG=y
tools/testing/selftests/rcutorture/configs/hazptr/CPU128.boot new file +1 -0
@@ -0,0 +1 @@ +hazptrtorture.torture_type=hazptr-stack
tools/testing/selftests/rcutorture/configs/hazptr/CPU256 new file +16 -0
@@ -0,0 +1,16 @@ +CONFIG_SMP=y +CONFIG_NR_CPUS=256 +CONFIG_PREEMPT_NONE=n +CONFIG_PREEMPT_VOLUNTARY=n +CONFIG_PREEMPT=y +CONFIG_HZ_PERIODIC=n +CONFIG_NO_HZ_IDLE=y +CONFIG_NO_HZ_FULL=n +CONFIG_HOTPLUG_CPU=y +CONFIG_SUSPEND=n +CONFIG_HIBERNATION=n +CONFIG_DEBUG_LOCK_ALLOC=n +CONFIG_PROVE_LOCKING=n +CONFIG_DEBUG_OBJECTS_RCU_HEAD=n +CONFIG_DEBUG_KERNEL=y +CONFIG_HAZPTR_DEBUG=y
tools/testing/selftests/rcutorture/configs/hazptr/CPU256.boot new file +1 -0
@@ -0,0 +1 @@ +hazptrtorture.torture_type=hazptr-stack
Signature · 1 line
2.43.0
RFC PATCH v3 15/15 selftests/rcutorture: add hazptr torture test script
In reply to Kunwu ChanAdd hazptr.sh as a one-command regression test for the hazptr torture configs.
Run NOPREEMPT, PREEMPT, READERS0, and READERS4, followed by LOCKDEP with wq_churn and SLOWPATH with CONFIG_PROVE_LOCKING=y. The latter two also check the console for lockdep warnings.
With --allcpus, repeat the PREEMPT test at CPU counts 8, 16, 32, and so on up to 256.
Follow the pattern of the existing srcu_lockdep.sh script.
--- .../selftests/rcutorture/bin/hazptr.sh | 146 ++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100755 tools/testing/selftests/rcutorture/bin/hazptr.sh
tools/testing/selftests/rcutorture/bin/hazptr.sh new file +146 -0
@@ -0,0 +1,146 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0+ +# +# Run hazptr torture tests and report any that fail. +# +# Usage: hazptr.sh [ --cpus N ] [ --allcpus ] [ --datestamp string ] +# +# Copyright (C) 2026 Kunwu Chan +# +# Authors: Kunwu Chan <[email protected]> + +usage () { + echo "Usage: $scriptname optional arguments:" + echo " --cpus N override CPU count (default: auto-detect)" + echo " --allcpus test at CPU counts 8,16,32,...,256" + echo " --datestamp string" + exit 1 +} + +ds=`date +%Y.%m.%d-%H.%M.%S`-hazptr +scriptname="$0" + +RCUTORTURE="`pwd`/tools/testing/selftests/rcutorture"; export RCUTORTURE +PATH=${RCUTORTURE}/bin:$PATH; export PATH +. functions.sh + +cpus="" # empty = let kvm.sh auto-detect +allcpus=0 + +while test $# -gt 0 +do + case "$1" in + --datestamp) + checkarg --datestamp "(relative pathname)" "$#" "$2" '^[a-zA-Z0-9._/-]*$' '^--' + ds=$2 + shift + ;; + --cpus) + checkarg --cpus "(number)" "$#" "$2" '^[0-9]\+$' '^--' + cpus="$2" + shift + ;; + --allcpus) + allcpus=1 + ;; + *) + echo Unknown argument $1 + usage + ;; + esac + shift +done + +nerrs=0 + +run_kvm () { + local ds_suffix="$1" + local configs="$2" + local cpu_arg="$3" + local kconfig="$4" + local resdir="$RCUTORTURE/res/$ds/$ds_suffix" + + mkdir -p "$resdir" + + tools/testing/selftests/rcutorture/bin/kvm.sh \ + --torture hazptr \ + --duration 5s \ + --configs "$configs" \ + ${cpu_arg:+--cpus "$cpu_arg"} \ + ${kconfig:+--kconfig "$kconfig"} \ + --trust-make \ + --datestamp "$ds/$ds_suffix" > "$resdir/kvm.sh.out" 2>&1 + return $? +} + +check_lockdep () { + local ds_suffix="$1" config_dir="$2" label="$3" + local resdir="$RCUTORTURE/res/$ds/$ds_suffix" + local console="$resdir/$config_dir/console.log" + + if ! test -f "$console" + then + echo "Missing console.log for $label ($ds_suffix)" > "$resdir/kvm.sh.err" + return 1 + fi + if grep -qE "WARNING: possible (recursive locking|circular locking dependency)" "$console" + then + echo "Lockdep warning in $label" > "$resdir/kvm.sh.err" + return 1 + fi + return 0 +} + +# Basic torture: all hazptr configs, CPU count auto-detected + +echo "--- hazptr basic torture${cpus:+ ($cpus CPUs)} ---" +run_kvm basic "NOPREEMPT PREEMPT READERS0 READERS4" "$cpus" || nerrs=$((nerrs+1)) + +# LOCKDEP: verify no lockdep splats with PROVE_LOCKING + wq_churn + +echo "--- hazptr lockdep + wq_churn ---" +if run_kvm lockdep-wq LOCKDEP "$cpus" +then + check_lockdep lockdep-wq LOCKDEP "LOCKDEP wq_churn" || + nerrs=$((nerrs+1)) +else + nerrs=$((nerrs+1)) +fi + +# SLOWPATH + PROVE_LOCKING: verify forced-slowpath does not deadlock + +echo "--- hazptr SLOWPATH + lockdep ---" +if run_kvm slowpath-lockdep SLOWPATH "$cpus" \ + "CONFIG_PROVE_LOCKING=y" +then + check_lockdep slowpath-lockdep SLOWPATH \ + "SLOWPATH+PROVE_LOCKING" || + nerrs=$((nerrs+1)) +else + nerrs=$((nerrs+1)) +fi + +# CPU scaling: optionally test at multiple CPU counts + +if test "$allcpus" -ne 0 +then + max_cpus=`grep '^processor' /proc/cpuinfo 2>/dev/null | wc -l` + for n in 8 16 32 64 128 256 + do + if test "$n" -le "$max_cpus" + then + echo "--- hazptr CPU=$n ---" + run_kvm "cpu$n" "PREEMPT" "$n" "CONFIG_NR_CPUS=$n" || nerrs=$((nerrs+1)) + fi + done +fi + +# Report + +if test "$nerrs" -ne 0 +then + echo "hazptr.sh: $nerrs test(s) failed" + exit 1 +fi +echo "hazptr.sh: all tests passed" +exit 0
Signature · 1 line
2.43.0
Mathieu Desnoyers
reviewerRe: [PATCH RFC v3 02/15] hazptr: use Bloom filter for shared scan waiters
In reply to Kunwu ChanQuoted 9 lines · Kunwu Chan
Replace the per-waiter exact-match scan with a shared Bloom filter built during the second drain pass: each observed non-wildcard address sets three bits, so 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 final drain scan cannot be held by an observed slot; false positives only delay completion to a later scan cycle.
Quoted 6 lines
The filter uses a one-page bitmap with three multiply-shift hash functions. It lives in the
hazptr_scan_stateand is only touched by the scan kthread, so waiter state remains on the caller's stack and no dynamic allocation is needed.Suggested-by Boqun Feng <[email protected]>
I actually suggested that to Boqun in the first place. Feel free to add my suggested-by tag as well. :)
Did you run your Bloom filter in a test program on the side to see how it behaves with pointers, and confirm that the false positive rate is not too high due to hashing algorithm/constants concerns ?
Thanks!
Mathieu
Quoted 175 lines
Signed-off-by Kunwu Chan <[email protected]>--- kernel/hazptr.c | 93 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 10 deletions(-)kernel/hazptr.c +83
-10@@ -7,6 +7,7 @@*/ #include <linux/hazptr.h> +#include <linux/bitops.h> #include <linux/percpu.h> #include <linux/spinlock.h> #include <linux/mutex.h> @@ -16,6 +17,7 @@#include <linux/kthread.h> #include <linux/swait.h> #include <linux/sched.h> +#include <linux/bitmap.h> /* * The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee @@ -213,6 +215,55 @@ void hazptr_scan_period(void *addr, void *scan_wildcard) } } +/* 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; +} +struct hazptr_waiter {struct list_head node;void *addr;@@ -226,17 +277,26 @@ 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;/* -* Check per-CPU slots before overflow-list slots to match the -* acquisition ordering of promoted slots. +* Walk all slots and return true if @watch is present. If @bloom +* is non-NULL, record observed non-wildcard addresses in it. +* +* Per-CPU slots are examined before overflow-list slots on each CPU +* to preserve the acquisition ordering required by the promote path: +* synchronize must observe the per-CPU slot release before the +* overflow-list entry can be missed. */ -static bool hazptr_value_present (void *val) +static bool hazptr_scan_walk (void *watch,struct hazptr_bloom *bloom) {int cpu; +if (bloom) +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);@@ -244,10 +304,14 @@ static bool hazptr_value_present(void *val)for (idx =0 ; idx <NR_HAZPTR_PERCPU_SLOTS ; idx++) {struct hazptr_slot_item *item = &percpu_slots->items[idx]; +void *v;/* Pairs with smp_store_release in hazptr_release(). */ -if (smp_load_acquire (&item->slot.addr) == val) + v =smp_load_acquire (&item->slot.addr); +if (v == watch)return true; +if (bloom && 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];@@ -256,11 +320,16 @@ static bool hazptr_value_present(void *val)raw_spin_lock_irqsave (&list->lock, flags);hlist_for_each_entry (b, &list->head, overflow_node) { +void *v; +/* Pairs with smp_store_release in hazptr_release(). */ -if (smp_load_acquire (&b->slot.addr) == val) { + v =smp_load_acquire (&b->slot.addr); +if (v == watch) {raw_spin_unlock_irqrestore (&list->lock, flags);return true; } +if (bloom && v && !is_wildcard (v)) +hazptr_bloom_add (bloom, v); }raw_spin_unlock_irqrestore (&list->lock, flags); }@@ -276,7 +345,7 @@ static bool hazptr_value_present(void *val)*/ static void hazptr_drain_wildcard (void *wc) { -while (hazptr_value_present (wc)) +while (hazptr_scan_walk (wc,NULL ))cond_resched (); }@@ -309,12 +378,16 @@ static void hazptr_scan_do_cycle(void)WRITE_ONCE (hazptr_wildcard, scan_wildcard); old_wildcard =flip_wildcard (scan_wildcard); -/* Pass 2: drain the old wildcard. */ -hazptr_drain_wildcard (old_wildcard); +/* +* Pass 2: drain the old wildcard while collecting observed +* addresses into the Bloom filter. +*/ +while (hazptr_scan_walk (old_wildcard, &hazptr_scan.bloom)) +cond_resched (); -/* Complete waiters whose address is no longer held by any slot. */ +/* Complete waiters whose address is not in the filter. */ list_for_each_entry_safe (w, n, &hazptr_scan.scanning, node) { -if (!hazptr_value_present (w->addr)) +if (!hazptr_bloom_contains (&hazptr_scan.bloom, w->addr))list_move (&w->node, &done); }
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Re: [PATCH RFC v3 02/15] hazptr: use Bloom filter for shared scan waiters
Quoted 16 lines · Mathieu Desnoyers
Quoted 14 lines · Kunwu Chan
Replace the per-waiter exact-match scan with a shared Bloom filter built during the second drain pass: each observed non-wildcard address sets three bits, so 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 final drain 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_stateand is only touched by the scan kthread, so waiter state remains on the caller's stack and no dynamic allocation is needed.
Quoted 5 lines
Quoted 1 line
Suggested-by Boqun Feng <[email protected]>I actually suggested that to Boqun in the first place. Feel free to add my suggested-by tag as well. :)
Thanks, Mathieu. I wasn't aware that you originally suggested the Bloom filter idea to Boqun. I'll add your Suggested-by tag.
Quoted 5 lines
Did you run your Bloom filter in a test program on the side to see how it behaves with pointers, and confirm that the false positive rate is not too high due to hashing algorithm/constants concerns ?
I haven't run a standalone Bloom filter test yet. I'll test the actual hash functions
and constants from the patch with pointer-like and aligned address distributions,
and measure the false-positive rate for representative numbers of observed addresses.
The in-kernel tests currently pass, including hazptr.sh and rcuscale, but I agree that
these don't directly characterize the Bloom filter's false-positive rate. I'll add the standalone test results to the next version.
Thanks,
Kunwu
Quoted 185 lines
Thanks!
Mathieu
Quoted 165 lines
Signed-off-by Kunwu Chan <[email protected]>--- kernel/hazptr.c | 93 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 10 deletions(-)kernel/hazptr.c +80
-8@@ -7,6 +7,7 @@*/ #include <linux/hazptr.h> +#include <linux/bitops.h> #include <linux/percpu.h> #include <linux/spinlock.h> #include <linux/mutex.h> @@ -16,6 +17,7 @@#include <linux/kthread.h> #include <linux/swait.h> #include <linux/sched.h> +#include <linux/bitmap.h> /* * The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee @@ -213,6 +215,55 @@ void hazptr_scan_period(void *addr, void *scan_wildcard) } } +/* 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; +} +struct hazptr_waiter {struct list_head node;void *addr;@@ -226,17 +277,26 @@ 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;/* -* Check per-CPU slots before overflow-list slots to match the -* acquisition ordering of promoted slots. +* Walk all slots and return true if @watch is present. If @bloom +* is non-NULL, record observed non-wildcard addresses in it. +* +* Per-CPU slots are examined before overflow-list slots on each CPU +* to preserve the acquisition ordering required by the promote path: +* synchronize must observe the per-CPU slot release before the +* overflow-list entry can be missed. */ -static bool hazptr_value_present (void *val) +static bool hazptr_scan_walk (void *watch,struct hazptr_bloom *bloom) {int cpu; +if (bloom) +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);@@ -244,10 +304,14 @@ static bool hazptr_value_present(void *val)for (idx =0 ; idx <NR_HAZPTR_PERCPU_SLOTS ; idx++) {struct hazptr_slot_item *item = &percpu_slots->items[idx]; +void *v;/* Pairs with smp_store_release in hazptr_release(). */ -if (smp_load_acquire (&item->slot.addr) == val) + v =smp_load_acquire (&item->slot.addr); +if (v == watch)return true; +if (bloom && 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];@@ -256,11 +320,16 @@ static bool hazptr_value_present(void *val)raw_spin_lock_irqsave (&list->lock, flags);hlist_for_each_entry (b, &list->head, overflow_node) { +void *v; +/* Pairs with smp_store_release in hazptr_release(). */ -if (smp_load_acquire (&b->slot.addr) == val) { + v =smp_load_acquire (&b->slot.addr); +if (v == watch) {raw_spin_unlock_irqrestore (&list->lock, flags);return true; } +if (bloom && v && !is_wildcard (v)) +hazptr_bloom_add (bloom, v); }raw_spin_unlock_irqrestore (&list->lock, flags); }@@ -276,7 +345,7 @@ static bool hazptr_value_present(void *val)*/ static void hazptr_drain_wildcard (void *wc) { -while (hazptr_value_present (wc)) +while (hazptr_scan_walk (wc,NULL ))cond_resched (); }@@ -309,12 +378,16 @@ static void hazptr_scan_do_cycle(void)WRITE_ONCE (hazptr_wildcard, scan_wildcard); old_wildcard =flip_wildcard (scan_wildcard); -/* Pass 2: drain the old wildcard. */ -hazptr_drain_wildcard (old_wildcard); +/* +* Pass 2: drain the old wildcard while collecting observed +* addresses into the Bloom filter. +*/ +while (hazptr_scan_walk (old_wildcard, &hazptr_scan.bloom))kernel/hazptr.c static void hazptr_scan_do_cycle(void) · line 393
Quoted 10 lines
kernel/hazptr.c +3
-2+cond_resched (); -/* Complete waiters whose address is no longer held by any slot. */ +/* Complete waiters whose address is not in the filter. */ list_for_each_entry_safe (w, n, &hazptr_scan.scanning, node) { -if (!hazptr_value_present (w->addr)) +if (!hazptr_bloom_contains (&hazptr_scan.bloom, w->addr))list_move (&w->node, &done); }--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
Re: [PATCH RFC v3 02/15] hazptr: use Bloom filter for shared scan waiters
In reply to Mathieu DesnoyersQuoted 18 lines · Mathieu Desnoyers
Quoted 9 lines · Kunwu Chan
Replace the per-waiter exact-match scan with a shared Bloom filter built during the second drain pass: each observed non-wildcard address sets three bits, so 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 final drain scan cannot be held by an observed slot; false positives only delay completion to a later scan cycle.
Quoted 6 lines
The filter uses a one-page bitmap with three multiply-shift hash functions. It lives in the
hazptr_scan_stateand is only touched by the scan kthread, so waiter state remains on the caller's stack and no dynamic allocation is needed.Suggested-by Boqun Feng <[email protected]>
Quoted 8 lines
I actually suggested that to Boqun in the first place. Feel free to add my suggested-by tag as well. :)
Did you run your Bloom filter in a test program on the side to see how it behaves with pointers, and confirm that the false positive rate is not too high due to hashing algorithm/constants concerns ?
Hi Mathieu,
I ran a standalone test using the hash functions and constants from the patch, with random,
page-aligned, cacheline-aligned, sequential, and two synthetic kernel-like address patterns.
With 768 inserted addresses, the measured false-positive rates were 0.025%–0.034% across
these patterns, compared with an ideal Bloom-filter estimate of about 0.031%. I also tested larger
insertion counts as stress cases. The results were consistent across
three seeds,
with only minor variation for the random distribution.
These are synthetic address distributions rather than measurements from actual kernel waiter
addresses, but they provide an initial check of the hash functions
against several structured
address patterns.
Thanks,
Kunwu
Quoted 185 lines
Thanks!
Mathieu
Quoted 165 lines
Signed-off-by Kunwu Chan <[email protected]>--- kernel/hazptr.c | 93 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 10 deletions(-)kernel/hazptr.c +80
-8@@ -7,6 +7,7 @@*/ #include <linux/hazptr.h> +#include <linux/bitops.h> #include <linux/percpu.h> #include <linux/spinlock.h> #include <linux/mutex.h> @@ -16,6 +17,7 @@#include <linux/kthread.h> #include <linux/swait.h> #include <linux/sched.h> +#include <linux/bitmap.h> /* * The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee @@ -213,6 +215,55 @@ void hazptr_scan_period(void *addr, void *scan_wildcard) } } +/* 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; +} +struct hazptr_waiter {struct list_head node;void *addr;@@ -226,17 +277,26 @@ 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;/* -* Check per-CPU slots before overflow-list slots to match the -* acquisition ordering of promoted slots. +* Walk all slots and return true if @watch is present. If @bloom +* is non-NULL, record observed non-wildcard addresses in it. +* +* Per-CPU slots are examined before overflow-list slots on each CPU +* to preserve the acquisition ordering required by the promote path: +* synchronize must observe the per-CPU slot release before the +* overflow-list entry can be missed. */ -static bool hazptr_value_present (void *val) +static bool hazptr_scan_walk (void *watch,struct hazptr_bloom *bloom) {int cpu; +if (bloom) +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);@@ -244,10 +304,14 @@ static bool hazptr_value_present(void *val)for (idx =0 ; idx <NR_HAZPTR_PERCPU_SLOTS ; idx++) {struct hazptr_slot_item *item = &percpu_slots->items[idx]; +void *v;/* Pairs with smp_store_release in hazptr_release(). */ -if (smp_load_acquire (&item->slot.addr) == val) + v =smp_load_acquire (&item->slot.addr); +if (v == watch)return true; +if (bloom && 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];@@ -256,11 +320,16 @@ static bool hazptr_value_present(void *val)raw_spin_lock_irqsave (&list->lock, flags);hlist_for_each_entry (b, &list->head, overflow_node) { +void *v; +/* Pairs with smp_store_release in hazptr_release(). */ -if (smp_load_acquire (&b->slot.addr) == val) { + v =smp_load_acquire (&b->slot.addr); +if (v == watch) {raw_spin_unlock_irqrestore (&list->lock, flags);return true; } +if (bloom && v && !is_wildcard (v)) +hazptr_bloom_add (bloom, v); }raw_spin_unlock_irqrestore (&list->lock, flags); }@@ -276,7 +345,7 @@ static bool hazptr_value_present(void *val)*/ static void hazptr_drain_wildcard (void *wc) { -while (hazptr_value_present (wc)) +while (hazptr_scan_walk (wc,NULL ))cond_resched (); }@@ -309,12 +378,16 @@ static void hazptr_scan_do_cycle(void)WRITE_ONCE (hazptr_wildcard, scan_wildcard); old_wildcard =flip_wildcard (scan_wildcard); -/* Pass 2: drain the old wildcard. */ -hazptr_drain_wildcard (old_wildcard); +/* +* Pass 2: drain the old wildcard while collecting observed +* addresses into the Bloom filter. +*/ +while (hazptr_scan_walk (old_wildcard, &hazptr_scan.bloom))kernel/hazptr.c static void hazptr_scan_do_cycle(void) · line 393
Quoted 10 lines
kernel/hazptr.c +3
-2+cond_resched (); -/* Complete waiters whose address is no longer held by any slot. */ +/* Complete waiters whose address is not in the filter. */ list_for_each_entry_safe (w, n, &hazptr_scan.scanning, node) { -if (!hazptr_value_present (w->addr)) +if (!hazptr_bloom_contains (&hazptr_scan.bloom, w->addr))list_move (&w->node, &done); }--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
Lai Jiangshan
reviewerRe: [PATCH RFC v3 08/15] locking/lockdep: use hazptr to wait for dynamic key lookups
In reply to Kunwu ChanQuoted 9 lines · Kunwu Chan
lockdep_unregister_key()waits foris_dynamic_key()callers withsynchronize_rcu_expedited(), which requires system-wide coordination. Makeis_dynamic_key()protect the hash bucket with a hazard pointer and usehazptr_synchronize()to wait for traversals of that bucket.The hash bucket returned by
keyhashentry()has a stable address, so it can be used as the hazptr synchronization target. Keep the key hashlist lifetime RCU-based withhlist_del_rcu()andcall_rcu().
Quoted 7 lines
Protect the traversal with the hazptr
scoped_guard()added earlier in this series.This adapts the lockdep use case from Boqun Feng's shazptr series to the current hazptr API.
Hello,
I think this is a misuse of hazard pointers.
The bucket has a permanent, static address; its lifetime does not need hazard-pointer protection. Instead, this patch combines a hazard pointer with that address to construct a read-side/synchronize mechanism in the same category as RCU. However, this construction lacks the generation distinction needed to separate pre-existing readers from subsequent readers.
The object being removed is the key, but the synchronization target is the bucket. Unlinking the key does not prevent new lookups from acquiring the same bucket address, including lookups for unrelated keys that hash to that bucket.
This breaks the forward-progress argument used by the underlying hazptr implementation: after removing an object from publication, there should not be a continuing stream of new readers acquiring that same object. Here, the bucket is never unpublished.
For example, let B be the bucket containing key K:
Updater Reader Scan kthread
------- ------ ------------
Unlink K from B
hazptr_synchronize(B)
Acquire B
Observe B;
retain waiter
Release B
Acquire B again
Observe B;
retain waiter
Release B
... ...Every reader critical section can finish, and the scan kthread can keep running, while the updater waits indefinitely. Readers that could have observed K may already have completed; later lookups alone can keep the waiter pending.
RCU does not need the reader population to become empty. Its generation (grace-period) machinery allows pre-existing readers to drain without letting a continuous stream of subsequent readers indefinitely extend that wait. This address-based construction has no equivalent distinction.
So this is not merely protecting a different object with hazptr:
it is using hazptr as a different synchronization primitive without providing the corresponding progress guarantee.
I think we should retain the RCU wait here. If the intention is to implement bucket-scoped read-side synchronization, that needs an explicit protocol to distinguish the readers being drained from new arrivals; a permanent bucket address is not sufficient.
Sorry for the lengthy reply, which was written with the help of an AI agent. I noticed this issue when this use case was first proposed, but I struggled to articulate my concerns clearly back then. Well, now we have AI. :)
Thanks
Lai
Quoted 61 lines
Signed-off-by Kunwu Chan <[email protected]>--- kernel/locking/lockdep.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)kernel/locking/lockdep.c +15
-10@@ -58,6 +58,7 @@#include <linux/context_tracking.h> #include <linux/console.h> #include <linux/kasan.h> +#include <linux/hazptr.h> #include <asm/sections.h> @@ -1280,14 +1281,19 @@ static bool is_dynamic_key(const struct lock_class_key *key) hash_head =keyhashentry (key); -rcu_read_lock (); -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { -if (k == key) { - found = true; -break ; + { +struct hazptr_ctx ctx; +void *bucket = hash_head; + + scoped_guard(hazptr, &ctx, &bucket) { +hlist_for_each_entry_rcu (k, hash_head, hash_entry,1 ) { +if (k == key) { + found = true; +break ; + } + } } } -rcu_read_unlock ();return found; }@@ -6683,11 +6689,10 @@ void lockdep_unregister_key(struct lock_class_key *key)* * Some operations like __qdisc_destroy() will call this in a debug * kernel, and the network traffic is disabled while waiting, hence -* the delay of the wait matters in debugging cases. Currently use a -* synchronize_rcu_expedited() to speed up the wait at the cost of -* system IPIs. TODO: Replace RCU with hazptr for this. +* the delay of the wait matters in debugging cases. Replace the +* expedited RCU wait with hazptr_synchronize(). */ -synchronize_rcu_expedited (); +hazptr_synchronize (keyhashentry (key)); }EXPORT_SYMBOL_GPL (lockdep_unregister_key);Signature · 1 line
2.43.0
Re: [PATCH RFC v3 03/15] hazptr: scan all per-CPU slots before overflow lists
In reply to Kunwu ChanQuoted 14 lines · Kunwu Chan
Boqun Feng pointed out that a promote racing a scan can cause the scan to miss a hazard pointer:
hazptr_promote_to_backup_slot()chains the backup slot into an overflow list before clearing the per-CPU slot, so the scan must observe the old location before the new one. The scan currently visits each CPU's overflow lists right after that CPU's per-CPU slots, so a promote that moves a hazard pointer from a per-CPU slot of a later-scanned CPU into an overflow list of an earlier-scanned CPU can be missed.The direct two-phase scan has a related race because the overflow list selection follows the wildcard phase: a promote racing the wildcard flip can move a backup slot to the list not covered by the second phase.
Quoted 8 lines
Fix this by scanning all per-CPU slots before any overflow-list slot, in both the shared-scan walk and the direct fallback scan. Give the overflow lists a flip phase of their own, independent of the wildcard, so each list is scanned while it is the non-live one:
new backup slots are chained to the other list, preserving forward progress.The same race was also addressed by Mathieu Desnoyers.
Hello,
I tested the previous faulting patch with a standalone reproducer. It admits the following interleaving, resulting in Too-short grace period.
Initially, hazptr_wildcard is 1. Below, p is a pointer to the protected object, and slot is the reader's per-CPU slot.
Mutator Reader
------- ------
/* hazptr_acquire() */
void *addr;
WRITE_ONCE(slot->addr,
READ_ONCE(hazptr_wildcard));
...
addr = READ_ONCE(*addr_p);
/* addr == p */void *old = p;smp_store_release(&p, NULL);hazptr_synchronize(old);
...
scan_wildcard =
flip_wildcard(hazptr_wildcard);
/* scan_wildcard == 2 */
/* First pass: Load B */
smp_cond_load_acquire(
&item->slot.addr,
VAL != addr &&
VAL != scan_wildcard);
/* Sees wildcard 1, so proceeds. */ /* Store C */
WRITE_ONCE(slot->addr, addr); /* scans list 1. */
hazptr_synchronize_overflow_list(
&overflow_list_flip->array[scan_idx],
addr);...
WRITE_ONCE(hazptr_wildcard,
scan_wildcard);
/* hazptr_wildcard == 2 */
hazptr_detach(&ctx);
...
/* hazptr_chain_backup_slot() */
unsigned int list_idx =
(unsigned long)
READ_ONCE(hazptr_wildcard) - 1;
/* list_idx == 1 */
...
/* Protection moves to the
* backup slot in list 1.
* The per-CPU slot is cleared. */hazptr_scan_period(
addr, flip_wildcard(scan_wildcard));
/* Second pass sees the cleared
* per-CPU slot and scans list 0,
* missing the backup slot in list 1. */
/* Returns while the reader
* still holds the hazard pointer. */
The critical orderings extracted from this failure are:
1. [Load B] -> [Store C]
2. [WRITE_ONCE(hazptr_wildcard, scan_wildcard)]
-> [unsigned int list_idx = (unsigned long) READ_ONCE(hazptr_wildcard) - 1]
Kunwu's patch (hazptr: scan all per-CPU slots before overflow lists) addresses the second ordering. The overflow-list phase is now flipped only after both per-CPU scan passes.
I also tested the patched version and it did not reproduce the previous failure.
Quoted 202 lines
Fixes 0b8114b25f17 ("hazptr: Implement two-phase wildcard scan")Reported-by Boqun Feng <[email protected]>Signed-off-by Kunwu Chan <[email protected]>--- kernel/hazptr.c | 88 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 24 deletions(-)kernel/hazptr.c +64
-24@@ -23,13 +23,20 @@* The current hazard pointer wildcard. Flips between 1UL and 2UL to guarantee * hazptr_synchronize forward progress even with a steady stream of readers. * This wildcard value is used by acquire to temporarily tag the per-CPU slots. -* This also affects the overflow list selection: the current list used by -* readers is array[(unsigned long) hazptr_wildcard - 1]. */ -static DEFINE_MUTEX (hazptr_wildcard_lock);/* Protect the wildcard flip. */ +static DEFINE_MUTEX (hazptr_phase_lock); +/* Protect the wildcard and overflow-list phase flips. */ void *hazptr_wildcard = (void *)1UL ;EXPORT_SYMBOL_GPL (hazptr_wildcard); +/* +* The current overflow list phase. Independent from the wildcard so that the +* overflow list scan can be placed after the per-CPU slot scan while still +* scanning the non-live list: the current list used by readers is +* array[hazptr_overflow_list_phase]. +*/ +static unsigned int hazptr_overflow_list_phase; +struct hazptr_overflow_list {raw_spinlock_t lock;/* Lock protecting overflow list and list generation. */ struct hlist_head head;/* Overflow list head. */ @@ -59,6 +66,12 @@ void *flip_wildcard(void *wildcard)return ((unsigned long ) wildcard ==1UL ) ? (void *)2UL : (void *)1UL ; } +static +unsigned int flip_list_phase (unsigned int phase) +{ +return 1 - phase; +} +static bool is_wildcard (void *addr) {@@ -184,15 +197,12 @@ void hazptr_synchronize_cpu_slots(int cpu, void *addr, void *scan_wildcard) }static -void hazptr_scan_period (void *addr,void *scan_wildcard) +void hazptr_scan_cpu_slots_period (void *addr,void *scan_wildcard) { -unsigned int scan_idx = (unsigned long ) scan_wildcard -1 ;int cpu;/* Scan all CPUs slots. */ for_each_possible_cpu (cpu) { -struct hazptr_overflow_list_flip *overflow_list_flip =per_cpu_ptr (&percpu_overflow_list_flip, cpu); -/* * Scan CPU slots. * Forward progress against recurring wildcards is guaranteed @@ -205,12 +215,22 @@ void hazptr_scan_period(void *addr, void *scan_wildcard)* to acquire that same hazard pointer value. */ hazptr_synchronize_cpu_slots (cpu, addr, scan_wildcard); + } +} + +static +void hazptr_scan_overflow_list_period (void *addr,unsigned int scan_idx) +{ +int cpu; + +/* +* Scan backup slots in percpu overflow lists. +* Forward progress is guaranteed by scanning one list +* while new elements are added into the other list. +*/ +for_each_possible_cpu (cpu) { +struct hazptr_overflow_list_flip *overflow_list_flip =per_cpu_ptr (&percpu_overflow_list_flip, cpu); -/* -* Scan backup slots in percpu overflow lists. -* Forward progress is guaranteed by scanning one list -* while new elements are added into the other list. -*/ hazptr_synchronize_overflow_list (&overflow_list_flip->array[scan_idx], addr); } }@@ -285,10 +305,13 @@ static struct hazptr_scan_state hazptr_scan;* Walk all slots and return true if @watch is present. If @bloom * is non-NULL, record observed non-wildcard addresses in it. * -* Per-CPU slots are examined before overflow-list slots on each CPU -* to preserve the acquisition ordering required by the promote path: -* synchronize must observe the per-CPU slot release before the -* overflow-list entry can be missed. +* All per-CPU slots are examined before any overflow-list slot: a +* promote (hazptr_detach() or a context switch) moves a hazard +* pointer from a per-CPU slot to an overflow list by chaining the +* backup slot before clearing the per-CPU slot, see +* hazptr_promote_to_backup_slot(). Therefore the scan must observe +* the old location before the new location for every slot/list +* pair, including pairs on different CPUs. */ static bool hazptr_scan_walk (void *watch,struct hazptr_bloom *bloom) {@@ -297,9 +320,9 @@ static bool hazptr_scan_walk(void *watch, struct hazptr_bloom *bloom)if (bloom)hazptr_bloom_reset (bloom); +/* Scan all per-CPU slots before any overflow-list slot. */ 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++) {@@ -313,6 +336,10 @@ static bool hazptr_scan_walk(void *watch, struct hazptr_bloom *bloom)if (bloom && v && !is_wildcard (v))hazptr_bloom_add (bloom, v); } + } +for_each_possible_cpu (cpu) { +struct hazptr_overflow_list_flip *overflow_list_flip =per_cpu_ptr (&percpu_overflow_list_flip, cpu); +for (int i =0 ; i <2 ; i++) {struct hazptr_overflow_list *list = &overflow_list_flip->array[i];struct hazptr_backup_slot *b;@@ -359,14 +386,14 @@ static void hazptr_scan_do_cycle(void)struct hazptr_waiter *w, *n;LIST_HEAD (done); -mutex_lock (&hazptr_wildcard_lock); +mutex_lock (&hazptr_phase_lock);mutex_lock (&hazptr_scan.lock);list_splice_tail_init (&hazptr_scan.pending, &hazptr_scan.scanning);mutex_unlock (&hazptr_scan.lock);if (list_empty (&hazptr_scan.scanning)) { -mutex_unlock (&hazptr_wildcard_lock); +mutex_unlock (&hazptr_phase_lock);return ; }@@ -391,7 +418,7 @@ static void hazptr_scan_do_cycle(void)list_move (&w->node, &done); } -mutex_unlock (&hazptr_wildcard_lock); +mutex_unlock (&hazptr_phase_lock);list_for_each_entry_safe (w, n, &done, node) {list_del_init (&w->node);@@ -463,6 +490,7 @@ static void hazptr_synchronize_queued(void *addr)void hazptr_synchronize (void *addr) {void *scan_wildcard; +unsigned int scan_list_phase;/* * Busy-wait should only be done from preemptible context. @@ -486,18 +514,30 @@ void hazptr_synchronize(void *addr) }/* Fallback: use the direct scan path. */ -guard (mutex)(&hazptr_wildcard_lock); +guard (mutex)(&hazptr_phase_lock); scan_wildcard =flip_wildcard (hazptr_wildcard); -hazptr_scan_period (addr, scan_wildcard); +/* Scan per-CPU slots. */ +hazptr_scan_cpu_slots_period (addr, scan_wildcard);WRITE_ONCE (hazptr_wildcard, scan_wildcard);/* Flip the current wildcard. */ -hazptr_scan_period (addr,flip_wildcard (scan_wildcard)); +hazptr_scan_cpu_slots_period (addr,flip_wildcard (scan_wildcard)); + +/* +* Scan overflow lists *after* scanning all per-CPU slots, see +* hazptr_promote_to_backup_slot(). Flip the overflow list phase +* between the two lists so that each list is scanned while it is +* the non-live one. +*/ + 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); +hazptr_scan_overflow_list_period (addr,flip_list_phase (scan_list_phase)); }EXPORT_SYMBOL_GPL (hazptr_synchronize);struct hazptr_slot *hazptr_chain_backup_slot (struct hazptr_ctx *ctx) {struct hazptr_overflow_list_flip *overflow_list_flip =this_cpu_ptr (&percpu_overflow_list_flip); -unsigned int list_idx = (unsigned long )READ_ONCE (hazptr_wildcard) -1 ; +unsigned int list_idx =READ_ONCE (hazptr_overflow_list_phase);struct hazptr_overflow_list *overflow_list = &overflow_list_flip->array[list_idx];struct hazptr_slot *slot = &ctx->backup_slot.slot;
Signature · 1 line
Kai Ma
Boqun Feng
maintainerRe: [PATCH RFC v3 08/15] locking/lockdep: use hazptr to wait for dynamic key lookups
In reply to Lai JiangshanQuoted 13 lines · Lai Jiangshan
Quoted 12 lines · Kunwu Chan
lockdep_unregister_key()waits foris_dynamic_key()callers withsynchronize_rcu_expedited(), which requires system-wide coordination. Makeis_dynamic_key()protect the hash bucket with a hazard pointer and usehazptr_synchronize()to wait for traversals of that bucket.The hash bucket returned by
keyhashentry()has a stable address, so it can be used as the hazptr synchronization target. Keep the key hashlist lifetime RCU-based withhlist_del_rcu()andcall_rcu().Protect the traversal with the hazptr
scoped_guard()added earlier in this series.
Quoted 9 lines
Quoted 4 lines
This adapts the lockdep use case from Boqun Feng's shazptr series to the current hazptr API.
Hello,
I think this is a misuse of hazard pointers.
I disagree.
Is this usage a typical hazard pointer usage, maybe not. But it's a valid usage IMO.
Quoted 7 lines
The bucket has a permanent, static address; its lifetime does not need hazard-pointer protection. Instead, this patch combines a hazard pointer with that address to construct a read-side/synchronize mechanism in the same category as RCU. However, this construction lacks the generation distinction needed to separate pre-existing readers from subsequent readers.
The "power" of hazard pointer allows you to use it protect almost anything, it happens to be the pointer of the object to free in the major use cases, but as long as readers and updaters use some address in their synchronization protocol, it's fine, because the synchronization works.
Quoted 12 lines
The object being removed is the key, but the synchronization target is the bucket. Unlinking the key does not prevent new lookups from acquiring the same bucket address, including lookups for unrelated keys that hash to that bucket.
This breaks the forward-progress argument used by the underlying hazptr implementation: after removing an object from publication, there should not be a continuing stream of new readers acquiring that same object. Here, the bucket is never unpublished.
For example, let B be the bucket containing key K:
Quoted 14 lines
Updater Reader Scan kthread
------- ------ ------------
Unlink K from B
hazptr_synchronize(B)
Acquire B
Observe B;
retain waiter
Release B
Acquire B again
Observe B;
retain waiter
Release B
... ...This is more of an implementation issue, and luckily the readers in lockdep are less frequent, so we don't have this issue at the moment. Even if we do, there are pretty ways we can duel with it. E.g. the scan thread can identify a "sticky" address like B, and publish it to all the readers, so they will notify the scan thread when they finish release it:
hazptr_release(): if (steady_address != NULL && steady_address == *slot) notify_scan_thread(); smp_store_release(slot, NULL);
in this way, scan kthread will be able to track down each reader of the steady address (with a bitmap or something), and make the necessary forward progress if the updater waits for too long.
And more importantly, even without this, the constant stream of readers of B can only block a hazptr_synchronize(B), but not a hazptr_synchronize(C), that's exactly the point of hazptr: readers don't block the hazptr_synchronize() on other addresses.
Quoted 11 lines
Every reader critical section can finish, and the scan kthread can keep running, while the updater waits indefinitely. Readers that could have observed K may already have completed; later lookups alone can keep the waiter pending.
RCU does not need the reader population to become empty. Its generation (grace-period) machinery allows pre-existing readers to drain without letting a continuous stream of subsequent readers indefinitely extend that wait. This address-based construction has no equivalent distinction.
It can be added as a slowpath as I mentioned above, surely it comes with a cost, but it makes the API usage simpler: you can use hazptr to protect something that is about to free and you can also use it to protect something that your readers and updaters have agreed on an value for synchronization.
Quoted 4 lines
So this is not merely protecting a different object with hazptr:
it is using hazptr as a different synchronization primitive without providing the corresponding progress guarantee.
To me it's still the same, mainly because how readers protect something.
Quoted 1 line
I think we should retain the RCU wait here. If the intention is to
Nope. synchronize_rcu() is too slow according to Breno's usage, and synchronize_rcu_expedited() is way more IPI. Hope you got the conclusion from AI, because it may lack some context here.
Don't make me wrong, if the main argument is "hey this is not a typical hazptr usage, so we need more proper usages to upstream hazptr", then I agree. It's just in a world where hazptr is in the kernel, such a non-typical usage should be supported.
Quoted 9 lines
implement bucket-scoped read-side synchronization, that needs an explicit protocol to distinguish the readers being drained from new arrivals; a permanent bucket address is not sufficient.
Sorry for the lengthy reply, which was written with the help of an AI agent. I noticed this issue when this use case was first proposed, but I struggled to articulate my concerns clearly back then. Well, now we have AI. :)
Thank for sharing the concern, it's valuable. If you or the AI have a proper hazptr usage in kernel, that'll be great!
Regards,
Boqun
Quoted 64 lines
Thanks
LaiQuoted 61 lines
Signed-off-by Kunwu Chan <[email protected]>--- kernel/locking/lockdep.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)kernel/locking/lockdep.c +15
-10@@ -58,6 +58,7 @@#include <linux/context_tracking.h> #include <linux/console.h> #include <linux/kasan.h> +#include <linux/hazptr.h> #include <asm/sections.h> @@ -1280,14 +1281,19 @@ static bool is_dynamic_key(const struct lock_class_key *key) hash_head =keyhashentry (key); -rcu_read_lock (); -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { -if (k == key) { - found = true; -break ; + { +struct hazptr_ctx ctx; +void *bucket = hash_head; + + scoped_guard(hazptr, &ctx, &bucket) { +hlist_for_each_entry_rcu (k, hash_head, hash_entry,1 ) { +if (k == key) { + found = true; +break ; + } + } } } -rcu_read_unlock ();return found; }@@ -6683,11 +6689,10 @@ void lockdep_unregister_key(struct lock_class_key *key)* * Some operations like __qdisc_destroy() will call this in a debug * kernel, and the network traffic is disabled while waiting, hence -* the delay of the wait matters in debugging cases. Currently use a -* synchronize_rcu_expedited() to speed up the wait at the cost of -* system IPIs. TODO: Replace RCU with hazptr for this. +* the delay of the wait matters in debugging cases. Replace the +* expedited RCU wait with hazptr_synchronize(). */ -synchronize_rcu_expedited (); +hazptr_synchronize (keyhashentry (key)); }EXPORT_SYMBOL_GPL (lockdep_unregister_key);Signature · 1 line
2.43.0
Mathieu Desnoyers
reviewerRe: [PATCH RFC v3 08/15] locking/lockdep: use hazptr to wait for dynamic key lookups
Quoted 1 line · Boqun Feng
On Tue, Oct 13, 2026 at 03:40:36PM +0800, Lai Jiangshan wrote:
[...]
Quoted 23 lines
Quoted 8 lines
This breaks the forward-progress argument used by the underlying hazptr implementation: after removing an object from publication, there should not be a continuing stream of new readers acquiring that same object. Here, the bucket is never unpublished.
For example, let B be the bucket containing key K:
Quoted 14 lines
Updater Reader Scan kthread ------- ------ ------------ Unlink K from B hazptr_synchronize(B) Acquire B Observe B; retain waiter Release B Acquire B again Observe B; retain waiter Release B ... ...
Quoted 1 line
This is more of an implementation issue,
I agree with Boqun about the point that this is really an implementation issue.
If we care about forward progress in the scenario of a repeated address reuse, we just need to wire up a "0/1" low bit phase tag and make hazptr acquire pick it up and set it within its hazptr slot. This will ensure forward progress in that scenario. hazptr synchronzie would therefore flip this phase between 0 and 1, and check that the pointer it's looking for is not set in the other phase, in alternance, handling both phases within one synchronize. Indeed, this phase concept comes from the realm of RCU implementations.
I think that the overall mindset so far has been "RCU does this" and "Hazard pointers do this" as if those were entirely predefined mechanisms. I think the way forward is not "one or the other", but rather to combine the strengths of each of those mechanisms into something better.
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Lai Jiangshan
reviewerRe: [PATCH RFC v3 08/15] locking/lockdep: use hazptr to wait for dynamic key lookups
Quoted 31 lines · Mathieu Desnoyers
Quoted 1 line · Boqun Feng
On Tue, Oct 13, 2026 at 03:40:36PM +0800, Lai Jiangshan wrote:
[...]
Quoted 23 lines
Quoted 8 lines
This breaks the forward-progress argument used by the underlying hazptr implementation: after removing an object from publication, there should not be a continuing stream of new readers acquiring that same object. Here, the bucket is never unpublished.
For example, let B be the bucket containing key K:
Quoted 14 lines
Updater Reader Scan kthread ------- ------ ------------ Unlink K from B hazptr_synchronize(B) Acquire B Observe B; retain waiter Release B Acquire B again Observe B; retain waiter Release B ... ...Quoted 1 line
This is more of an implementation issue,
I agree with Boqun about the point that this is really an implementation issue.
Quoted 8 lines
If we care about forward progress in the scenario of a repeated address reuse, we just need to wire up a "0/1" low bit phase tag and make hazptr acquire pick it up and set it within its hazptr slot. This will ensure forward progress in that scenario. hazptr synchronzie would therefore flip this phase between 0 and 1, and check that the pointer it's looking for is not set in the other phase, in alternance, handling both phases within one synchronize. Indeed, this phase concept comes from the realm of RCU implementations.
Regardless of which implementation approach is adopted, this would effectively introduce a new read-side synchronization mechanism based on hazard pointers. I would prefer to see it introduced under a distinct name in the kernel, so that its
semantics and intended usage can be clearly distinguished from those of conventional hazard pointers.
Thanks,
Lai
Quoted 15 lines
I think that the overall mindset so far has been "RCU does this" and "Hazard pointers do this" as if those were entirely predefined mechanisms. I think the way forward is not "one or the other", but rather to combine the strengths of each of those mechanisms into something better.
Thanks,
Mathieu
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
Paul E. McKenney
maintainerRe: [PATCH RFC v3 08/15] locking/lockdep: use hazptr to wait for dynamic key lookups
In reply to Mathieu DesnoyersQuoted 39 lines · Mathieu Desnoyers
Quoted 1 line · Boqun Feng
On Tue, Oct 13, 2026 at 03:40:36PM +0800, Lai Jiangshan wrote:
[...]
Quoted 23 lines
Quoted 8 lines
This breaks the forward-progress argument used by the underlying hazptr implementation: after removing an object from publication, there should not be a continuing stream of new readers acquiring that same object. Here, the bucket is never unpublished.
For example, let B be the bucket containing key K:
Quoted 14 lines
Updater Reader Scan kthread ------- ------ ------------ Unlink K from B hazptr_synchronize(B) Acquire B Observe B; retain waiter Release B Acquire B again Observe B; retain waiter Release B ... ...Quoted 1 line
This is more of an implementation issue,
I agree with Boqun about the point that this is really an implementation issue.
If we care about forward progress in the scenario of a repeated address reuse, we just need to wire up a "0/1" low bit phase tag and make hazptr acquire pick it up and set it within its hazptr slot. This will ensure forward progress in that scenario. hazptr synchronzie would therefore flip this phase between 0 and 1, and check that the pointer it's looking for is not set in the other phase, in alternance, handling both phases within one synchronize. Indeed, this phase concept comes from the realm of RCU implementations.
Quoted 5 lines
I think that the overall mindset so far has been "RCU does this" and "Hazard pointers do this" as if those were entirely predefined mechanisms. I think the way forward is not "one or the other", but rather to combine the strengths of each of those mechanisms into something better.
There have been a number of academic attempts to do just that, all of which ended up providing the worst of both worlds. Large memory footprint *and* conditional pointer traversal being the usual suboptimal outcome. :-(
However, I don't see a problem with use of hazard pointers for static addresses, at least assuming use of something like the above low-bit trick to ensure forward progress.
Thanx, Paul
Quoted 8 lines
Thanks,
Mathieu
Signature · 3 lines
Mathieu Desnoyers EfficiOS Inc. https://www.efficios.com
Re: [PATCH RFC v3 08/15] locking/lockdep: use hazptr to wait for dynamic key lookups
In reply to Lai JiangshanQuoted 76 lines · Lai Jiangshan
Quoted 9 lines · Kunwu Chan
lockdep_unregister_key()waits foris_dynamic_key()callers withsynchronize_rcu_expedited(), which requires system-wide coordination. Makeis_dynamic_key()protect the hash bucket with a hazard pointer and usehazptr_synchronize()to wait for traversals of that bucket.The hash bucket returned by
keyhashentry()has a stable address, so it can be used as the hazptr synchronization target. Keep the key hashlist lifetime RCU-based withhlist_del_rcu()andcall_rcu().Quoted 7 lines
Protect the traversal with the hazptr
scoped_guard()added earlier in this series.This adapts the lockdep use case from Boqun Feng's shazptr series to the current hazptr API.
Hello,
I think this is a misuse of hazard pointers.
The bucket has a permanent, static address; its lifetime does not need hazard-pointer protection. Instead, this patch combines a hazard pointer with that address to construct a read-side/synchronize mechanism in the same category as RCU. However, this construction lacks the generation distinction needed to separate pre-existing readers from subsequent readers.
The object being removed is the key, but the synchronization target is the bucket. Unlinking the key does not prevent new lookups from acquiring the same bucket address, including lookups for unrelated keys that hash to that bucket.
This breaks the forward-progress argument used by the underlying hazptr implementation: after removing an object from publication, there should not be a continuing stream of new readers acquiring that same object. Here, the bucket is never unpublished.
For example, let B be the bucket containing key K:
Updater Reader Scan kthread ------- ------ ------------ Unlink K from B hazptr_synchronize(B) Acquire B Observe B; retain waiter Release B Acquire B again Observe B; retain waiter Release B ... ...Every reader critical section can finish, and the scan kthread can keep running, while the updater waits indefinitely. Readers that could have observed K may already have completed; later lookups alone can keep the waiter pending.
RCU does not need the reader population to become empty. Its generation (grace-period) machinery allows pre-existing readers to drain without letting a continuous stream of subsequent readers indefinitely extend that wait. This address-based construction has no equivalent distinction.
So this is not merely protecting a different object with hazptr:
it is using hazptr as a different synchronization primitive without providing the corresponding progress guarantee.I think we should retain the RCU wait here. If the intention is to implement bucket-scoped read-side synchronization, that needs an explicit protocol to distinguish the readers being drained from new arrivals; a permanent bucket address is not sufficient.
Quoted 5 lines
Sorry for the lengthy reply, which was written with the help of an AI agent. I noticed this issue when this use case was first proposed, but I struggled to articulate my concerns clearly back then. Well, now we have AI. :)
Hi Lai, thanks for pointing this out.
And thanks Boqun, Mathieu, and Paul for the helpful replies.
I'll look into adding an RCU-stall-like mechanism to detect long-lived pointer waiters, and work with Boqun and Mathieu to find a more suitable scenario to exercise this case.
Thanks,
Kunwu
Quoted 64 lines
Thanks
LaiQuoted 61 lines
Signed-off-by Kunwu Chan <[email protected]>--- kernel/locking/lockdep.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-)kernel/locking/lockdep.c +15
-11@@ -58,6 +58,7 @@#include <linux/context_tracking.h> #include <linux/console.h> #include <linux/kasan.h> +#include <linux/hazptr.h> #include <asm/sections.h> @@ -1280,14 +1281,19 @@ static bool is_dynamic_key(const struct lock_class_key *key) hash_head =keyhashentry (key); -rcu_read_lock (); -hlist_for_each_entry_rcu (k, hash_head, hash_entry) { -if (k == key) { - found = true; -break ; + { +struct hazptr_ctx ctx; +void *bucket = hash_head; + + scoped_guard(hazptr, &ctx, &bucket) { +hlist_for_each_entry_rcu (k, hash_head, hash_entry,1 ) { +if (k == key) { + found = true; +break ; + } + } } } -rcu_read_unlock ();return found; }@@ -6683,11 +6689,10 @@ void lockdep_unregister_key(struct lock_class_key *key)* * Some operations like __qdisc_destroy() will call this in a debug * kernel, and the network traffic is disabled while waiting, hence -* the delay of the wait matters in debugging cases. Currently use a -* synchronize_rcu_expedited() to speed up the wait at the cost of -* system IPIs. TODO: Replace RCU with hazptr for this. +* the delay of the wait matters in debugging cases. Replace the +* expedited RCU wait with hazptr_synchronize(). */ -synchronize_rcu_expedited (); +hazptr_synchronize (keyhashentry (key)); }EXPORT_SYMBOL_GPL (lockdep_unregister_key); --2.43.0