hash: add local variable 01/37501/2
authorGabriel Oginski <gabrielx.oginski@intel.com>
Fri, 21 Oct 2022 07:05:56 +0000 (07:05 +0000)
committerFan Zhang <royzhang1980@hotmail.com>
Tue, 25 Oct 2022 08:30:02 +0000 (08:30 +0000)
The current implmentation of the hash table is not thread-safe.
This design leads to a segfault when VPP handling a lot of tunnels for
Wireguard, where one thread modify the hash table and other threads
starting to lookup at the same time.

The fix add a local variable to store how many bits are used by a user
object.

Type: fix
Signed-off-by: Gabriel Oginski <gabrielx.oginski@intel.com>
Change-Id: Iecf6b3ef9f308b61015c66277cc459a6d019c9c1

src/vppinfra/hash.h

index 968c778..3c754c8 100644 (file)
@@ -123,8 +123,9 @@ always_inline uword
 hash_is_user (void *v, uword i)
 {
   hash_t *h = hash_header (v);
-  uword i0 = i / BITS (h->is_user[0]);
-  uword i1 = i % BITS (h->is_user[0]);
+  uword bits = BITS (h->is_user[0]);
+  uword i0 = i / bits;
+  uword i1 = i % bits;
   return (h->is_user[i0] & ((uword) 1 << i1)) != 0;
 }