From 53431ff67947d2da2489c7477d61e8016c09592b Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Mon, 25 Nov 2024 09:44:00 +0100 Subject: [PATCH] Hash test spurious fail fixed There was a race condition in the test itself, causing the test reader access data after free. --- lib/hash_test.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/hash_test.c b/lib/hash_test.c index dc0c067e..33b9ac85 100644 --- a/lib/hash_test.c +++ b/lib/hash_test.c @@ -359,12 +359,14 @@ st_find_thread(void *_v) uint skip = st_skip[atomic_fetch_add_explicit(&st_skip_pos, 1, memory_order_acq_rel)]; - for (u64 i = 0; !atomic_load_explicit(&st_end, memory_order_relaxed); i += skip) + for (u64 i = 0; !atomic_load_explicit(&st_end, memory_order_acquire); i += skip) { struct st_node *n = SPINHASH_FIND(*v, ST, i % ST_MAX); ASSERT_DIE(!n || (n->key == i % ST_MAX)); } + atomic_fetch_add_explicit(&st_end, 1, memory_order_release); + rcu_thread_stop(); return NULL; } @@ -398,6 +400,11 @@ st_update_thread(void *_v) atomic_store_explicit(&st_end, 1, memory_order_release); + /* Wait for readers to properly end before releasing the memory, + * as the hash nodes may be accessed even after removed from hash */ + while (atomic_load_explicit(&st_end, memory_order_acquire) < ST_READERS + 1) + birdloop_yield(); + rcu_thread_stop(); return NULL; }