vppinfra: fix RC in bihash instantiation
[vpp.git] / src / vppinfra / bihash_template.c
index 89bfc8b..aac66cb 100644 (file)
@@ -38,7 +38,7 @@ static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
       void *base, *rv;
       uword alloc = alloc_arena_next (h) - alloc_arena_mapped (h);
       int mmap_flags = MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS;
-      int mmap_flags_huge = (mmap_flags | MAP_HUGETLB |
+      int mmap_flags_huge = (mmap_flags | MAP_HUGETLB | MAP_LOCKED |
                             BIHASH_LOG2_HUGEPAGE_SIZE << MAP_HUGE_SHIFT);
 
       /* new allocation is 25% of existing one */
@@ -53,7 +53,7 @@ static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
       rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags_huge, -1, 0);
 
       /* fallback - maybe we are still able to allocate normal pages */
-      if (rv == MAP_FAILED)
+      if (rv == MAP_FAILED || mlock (base, alloc) != 0)
        rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
 
       if (rv == MAP_FAILED)
@@ -106,7 +106,7 @@ static void BV (clib_bihash_instantiate) (BVT (clib_bihash) * h)
                         sizeof (BVT (clib_bihash_kv))));
        }
     }
-  CLIB_MEMORY_BARRIER ();
+  CLIB_MEMORY_STORE_BARRIER ();
   h->instantiated = 1;
 }
 
@@ -184,7 +184,7 @@ void BV (clib_bihash_init)
 #define MFD_ALLOW_SEALING 0x0002U
 #endif
 
-void BV (clib_bihash_master_init_svm)
+void BV (clib_bihash_initiator_init_svm)
   (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
 {
   uword bucket_size;
@@ -253,7 +253,7 @@ void BV (clib_bihash_master_init_svm)
   h->instantiated = 1;
 }
 
-void BV (clib_bihash_slave_init_svm)
+void BV (clib_bihash_responder_init_svm)
   (BVT (clib_bihash) * h, char *name, int fd)
 {
   u8 *mmap_addr;
@@ -433,7 +433,7 @@ BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
   clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
   working_bucket.as_u64 = b->as_u64;
   working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
-  CLIB_MEMORY_BARRIER ();
+  CLIB_MEMORY_STORE_BARRIER ();
   b->as_u64 = working_bucket.as_u64;
   h->working_copies[thread_index] = working_copy;
 }
@@ -463,8 +463,7 @@ BV (split_and_rehash)
 
       /* rehash the item onto its new home-page */
       new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
-      new_hash >>= h->log2_nbuckets;
-      new_hash &= (1 << new_log2_pages) - 1;
+      new_hash = extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
       new_v = &new_values[new_hash];
 
       /* Across the new home-page */
@@ -534,19 +533,27 @@ BV (split_and_rehash_linear)
   return new_values;
 }
 
-static inline int BV (clib_bihash_add_del_inline)
-  (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
+static_always_inline int BV (clib_bihash_add_del_inline_with_hash)
+  (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, u64 hash, int is_add,
    int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
 {
   BVT (clib_bihash_bucket) * b, tmp_b;
   BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
   int i, limit;
-  u64 hash, new_hash;
+  u64 new_hash;
   u32 new_log2_pages, old_log2_pages;
   u32 thread_index = os_get_thread_index ();
   int mark_bucket_linear;
   int resplit_once;
 
+  /* *INDENT-OFF* */
+  static const BVT (clib_bihash_bucket) mask = {
+    .linear_search = 1,
+    .log2_pages = -1
+  };
+  /* *INDENT-ON* */
+
+#if BIHASH_LAZY_INSTANTIATE
   /*
    * Create the table (is_add=1,2), or flunk the request now (is_add=0)
    * Use the alloc_lock to protect the instantiate operation.
@@ -561,13 +568,13 @@ static inline int BV (clib_bihash_add_del_inline)
        BV (clib_bihash_instantiate) (h);
       BV (clib_bihash_alloc_unlock) (h);
     }
-
-  hash = BV (clib_bihash_hash) (add_v);
+#else
+  /* Debug image: make sure the table has been instantiated */
+  ASSERT (h->instantiated != 0);
+#endif
 
   b = BV (clib_bihash_get_bucket) (h, hash);
 
-  hash >>= h->log2_nbuckets;
-
   BV (clib_bihash_lock_bucket) (b);
 
   /* First elt in the bucket? */
@@ -587,7 +594,7 @@ static inline int BV (clib_bihash_add_del_inline)
       tmp_b.as_u64 = 0;                /* clears bucket lock */
       tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
       tmp_b.refcnt = 1;
-      CLIB_MEMORY_BARRIER ();
+      CLIB_MEMORY_STORE_BARRIER ();
 
       b->as_u64 = tmp_b.as_u64;        /* unlocks the bucket */
       BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
@@ -599,9 +606,13 @@ static inline int BV (clib_bihash_add_del_inline)
   limit = BIHASH_KVP_PER_PAGE;
   v = BV (clib_bihash_get_value) (h, b->offset);
 
-  v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
-  if (b->linear_search)
-    limit <<= b->log2_pages;
+  if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
+    {
+      if (PREDICT_FALSE (b->linear_search))
+       limit <<= b->log2_pages;
+      else
+       v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
+    }
 
   if (is_add)
     {
@@ -627,8 +638,8 @@ static inline int BV (clib_bihash_add_del_inline)
                  return (-2);
                }
 
-             CLIB_MEMORY_BARRIER ();   /* Add a delay */
-             clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
+             clib_memcpy_fast (&(v->kvp[i].value),
+                               &add_v->value, sizeof (add_v->value));
              BV (clib_bihash_unlock_bucket) (b);
              BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
              return (0);
@@ -647,7 +658,7 @@ static inline int BV (clib_bihash_add_del_inline)
               */
              clib_memcpy_fast (&(v->kvp[i].value),
                                &add_v->value, sizeof (add_v->value));
-             CLIB_MEMORY_BARRIER ();   /* Make sure the value has settled */
+             CLIB_MEMORY_STORE_BARRIER ();     /* Make sure the value has settled */
              clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
                                sizeof (add_v->key));
              b->refcnt++;
@@ -664,8 +675,8 @@ static inline int BV (clib_bihash_add_del_inline)
            {
              if (is_stale_cb (&(v->kvp[i]), arg))
                {
-                 CLIB_MEMORY_BARRIER ();
                  clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
+                 CLIB_MEMORY_STORE_BARRIER ();
                  BV (clib_bihash_unlock_bucket) (b);
                  BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
                  return (0);
@@ -681,7 +692,7 @@ static inline int BV (clib_bihash_add_del_inline)
          /* Found the key? Kill it... */
          if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
            {
-             clib_memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
+             clib_memset_u8 (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
              /* Is the bucket empty? */
              if (PREDICT_TRUE (b->refcnt > 1))
                {
@@ -696,14 +707,15 @@ static inline int BV (clib_bihash_add_del_inline)
                      b->linear_search = 0;
                      b->log2_pages = 0;
                      /* Clean up the bucket-level kvp array */
-                     clib_memset
-                       ((b + 1), 0xff,
-                        BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)));
+                     clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
+                                     sizeof (BVT (clib_bihash_kv)));
+                     CLIB_MEMORY_STORE_BARRIER ();
                      BV (clib_bihash_unlock_bucket) (b);
                      BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
                      goto free_backing_store;
                    }
 
+                 CLIB_MEMORY_STORE_BARRIER ();
                  BV (clib_bihash_unlock_bucket) (b);
                  BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
                  return (0);
@@ -712,7 +724,6 @@ static inline int BV (clib_bihash_add_del_inline)
                {
                  /* Save old bucket value, need log2_pages to free it */
                  tmp_b.as_u64 = b->as_u64;
-                 CLIB_MEMORY_BARRIER ();
 
                  /* Kill and unlock the bucket */
                  b->as_u64 = 0;
@@ -783,9 +794,8 @@ static inline int BV (clib_bihash_add_del_inline)
   limit = BIHASH_KVP_PER_PAGE;
   if (mark_bucket_linear)
     limit <<= new_log2_pages;
-  new_hash >>= h->log2_nbuckets;
-  new_hash &= (1 << new_log2_pages) - 1;
-  new_v += mark_bucket_linear ? 0 : new_hash;
+  else
+    new_v += extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
 
   for (i = 0; i < limit; i++)
     {
@@ -818,7 +828,7 @@ expand_ok:
     tmp_b.refcnt = h->saved_bucket.refcnt + 1;
   ASSERT (tmp_b.refcnt > 0);
   tmp_b.lock = 0;
-  CLIB_MEMORY_BARRIER ();
+  CLIB_MEMORY_STORE_BARRIER ();
   b->as_u64 = tmp_b.as_u64;
 
 #if BIHASH_KVP_AT_BUCKET_LEVEL
@@ -839,6 +849,15 @@ expand_ok:
   return (0);
 }
 
+static_always_inline int BV (clib_bihash_add_del_inline)
+  (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
+   int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
+{
+  u64 hash = BV (clib_bihash_hash) (add_v);
+  return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add,
+                                                   is_stale_cb, arg);
+}
+
 int BV (clib_bihash_add_del)
   (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
 {
@@ -856,49 +875,7 @@ int BV (clib_bihash_search)
   (BVT (clib_bihash) * h,
    BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
 {
-  u64 hash;
-  BVT (clib_bihash_value) * v;
-  BVT (clib_bihash_bucket) * b;
-  int i, limit;
-
-  ASSERT (valuep);
-
-#if BIHASH_LAZY_INSTANTIATE
-  if (PREDICT_FALSE (alloc_arena (h) == 0))
-    return -1;
-#endif
-
-  hash = BV (clib_bihash_hash) (search_key);
-
-  b = BV (clib_bihash_get_bucket) (h, hash);
-
-  if (BV (clib_bihash_bucket_is_empty) (b))
-    return -1;
-
-  if (PREDICT_FALSE (b->lock))
-    {
-      volatile BVT (clib_bihash_bucket) * bv = b;
-      while (bv->lock)
-       CLIB_PAUSE ();
-    }
-
-  hash >>= h->log2_nbuckets;
-
-  v = BV (clib_bihash_get_value) (h, b->offset);
-  limit = BIHASH_KVP_PER_PAGE;
-  v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
-  if (PREDICT_FALSE (b->linear_search))
-    limit <<= b->log2_pages;
-
-  for (i = 0; i < limit; i++)
-    {
-      if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
-       {
-         *valuep = v->kvp[i];
-         return 0;
-       }
-    }
-  return -1;
+  return BV (clib_bihash_search_inline_2) (h, search_key, valuep);
 }
 
 u8 *BV (format_bihash) (u8 * s, va_list * args)
@@ -916,7 +893,7 @@ u8 *BV (format_bihash) (u8 * s, va_list * args)
   s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
 
 #if BIHASH_LAZY_INSTANTIATE
-  if (PREDICT_FALSE (alloc_arena (h) == 0))
+  if (PREDICT_FALSE (h->instantiated == 0))
     return format (s, "[empty, uninitialized]");
 #endif
 
@@ -1017,7 +994,7 @@ void BV (clib_bihash_foreach_key_value_pair)
 
 
 #if BIHASH_LAZY_INSTANTIATE
-  if (PREDICT_FALSE (alloc_arena (h) == 0))
+  if (PREDICT_FALSE (h->instantiated == 0))
     return;
 #endif