summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2020-04-21 20:14:34 +0200
committerDamjan Marion <dmarion@me.com>2020-04-22 08:47:29 +0000
commit74ee18b0b33aad974705ae16739a86c48f3b966b (patch)
tree2b911b913d5052163b04410c3716b711aaacf456
parent801ec2a080d9414b3fab80906333bdb94b5d4043 (diff)
vppinfra: fix potential race in bihash bucket lock
Type: improvement Change-Id: Ia04bd26ecd689894753e036e52920316de611910 Signed-off-by: Damjan Marion <damarion@cisco.com>
-rw-r--r--src/vppinfra/bihash_template.h20
1 files changed, 9 insertions, 11 deletions
diff --git a/src/vppinfra/bihash_template.h b/src/vppinfra/bihash_template.h
index b8e0a239013..50b4af65710 100644
--- a/src/vppinfra/bihash_template.h
+++ b/src/vppinfra/bihash_template.h
@@ -260,21 +260,19 @@ static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
{
- BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
+ /* *INDENT-OFF* */
+ BVT (clib_bihash_bucket) mask = { .lock = 1 };
+ /* *INDENT-ON* */
+ u64 old;
- locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
- unlocked_bucket.lock = 0;
- locked_bucket.lock = 1;
+try_again:
+ old = clib_atomic_fetch_or (&b->as_u64, mask.as_u64);
- while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
- locked_bucket.as_u64, 1 /* weak */ ,
- __ATOMIC_ACQUIRE,
- __ATOMIC_ACQUIRE) == 0)
+ if (PREDICT_FALSE (old & mask.as_u64))
{
+ /* somebody else flipped the bit, try again */
CLIB_PAUSE ();
- locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
- unlocked_bucket.lock = 0;
- locked_bucket.lock = 1;
+ goto try_again;
}
}