| 1 | /* |
| 2 | * rculfhash.c |
| 3 | * |
| 4 | * Userspace RCU library - Lock-Free Expandable RCU Hash Table |
| 5 | * |
| 6 | * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; either |
| 11 | * version 2.1 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * Based on the following articles: |
| 25 | * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free |
| 26 | * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405. |
| 27 | * - Michael, M. M. High performance dynamic lock-free hash tables |
| 28 | * and list-based sets. In Proceedings of the fourteenth annual ACM |
| 29 | * symposium on Parallel algorithms and architectures, ACM Press, |
| 30 | * (2002), 73-82. |
| 31 | * |
| 32 | * Some specificities of this Lock-Free Expandable RCU Hash Table |
| 33 | * implementation: |
| 34 | * |
| 35 | * - RCU read-side critical section allows readers to perform hash |
| 36 | * table lookups and use the returned objects safely by delaying |
| 37 | * memory reclaim of a grace period. |
| 38 | * - Add and remove operations are lock-free, and do not need to |
| 39 | * allocate memory. They need to be executed within RCU read-side |
| 40 | * critical section to ensure the objects they read are valid and to |
| 41 | * deal with the cmpxchg ABA problem. |
| 42 | * - add and add_unique operations are supported. add_unique checks if |
| 43 | * the node key already exists in the hash table. It ensures no key |
| 44 | * duplicata exists. |
| 45 | * - The resize operation executes concurrently with add/remove/lookup. |
| 46 | * - Hash table nodes are contained within a split-ordered list. This |
| 47 | * list is ordered by incrementing reversed-bits-hash value. |
| 48 | * - An index of dummy nodes is kept. These dummy nodes are the hash |
| 49 | * table "buckets", and they are also chained together in the |
| 50 | * split-ordered list, which allows recursive expansion. |
| 51 | * - The resize operation only allows expanding the hash table. |
| 52 | * It is triggered either through an API call or automatically by |
| 53 | * detecting long chains in the add operation. |
| 54 | * - Resize operation initiated by long chain detection is executed by a |
| 55 | * call_rcu thread, which keeps lock-freedom of add and remove. |
| 56 | * - Resize operations are protected by a mutex. |
| 57 | * - The removal operation is split in two parts: first, a "removed" |
| 58 | * flag is set in the next pointer within the node to remove. Then, |
| 59 | * a "garbage collection" is performed in the bucket containing the |
| 60 | * removed node (from the start of the bucket up to the removed node). |
| 61 | * All encountered nodes with "removed" flag set in their next |
| 62 | * pointers are removed from the linked-list. If the cmpxchg used for |
| 63 | * removal fails (due to concurrent garbage-collection or concurrent |
| 64 | * add), we retry from the beginning of the bucket. This ensures that |
| 65 | * the node with "removed" flag set is removed from the hash table |
| 66 | * (not visible to lookups anymore) before the RCU read-side critical |
| 67 | * section held across removal ends. Furthermore, this ensures that |
| 68 | * the node with "removed" flag set is removed from the linked-list |
| 69 | * before its memory is reclaimed. Only the thread which removal |
| 70 | * successfully set the "removed" flag (with a cmpxchg) into a node's |
| 71 | * next pointer is considered to have succeeded its removal (and thus |
| 72 | * owns the node to reclaim). Because we garbage-collect starting from |
| 73 | * an invariant node (the start-of-bucket dummy node) up to the |
| 74 | * "removed" node (or find a reverse-hash that is higher), we are sure |
| 75 | * that a successful traversal of the chain leads to a chain that is |
| 76 | * present in the linked-list (the start node is never removed) and |
| 77 | * that is does not contain the "removed" node anymore, even if |
| 78 | * concurrent delete/add operations are changing the structure of the |
| 79 | * list concurrently. |
| 80 | * - A RCU "order table" indexed by log2(hash index) is copied and |
| 81 | * expanded by the resize operation. This order table allows finding |
| 82 | * the "dummy node" tables. |
| 83 | * - There is one dummy node table per hash index order. The size of |
| 84 | * each dummy node table is half the number of hashes contained in |
| 85 | * this order. |
| 86 | * - call_rcu is used to garbage-collect the old order table. |
| 87 | * - The per-order dummy node tables contain a compact version of the |
| 88 | * hash table nodes. These tables are invariant after they are |
| 89 | * populated into the hash table. |
| 90 | */ |
| 91 | |
| 92 | #define _LGPL_SOURCE |
| 93 | #include <stdlib.h> |
| 94 | #include <errno.h> |
| 95 | #include <assert.h> |
| 96 | #include <stdio.h> |
| 97 | #include <stdint.h> |
| 98 | #include <string.h> |
| 99 | |
| 100 | #include <urcu.h> |
| 101 | #include <urcu-call-rcu.h> |
| 102 | #include <urcu/arch.h> |
| 103 | #include <urcu/uatomic.h> |
| 104 | #include <urcu/jhash.h> |
| 105 | #include <urcu/compiler.h> |
| 106 | #include <urcu/rculfhash.h> |
| 107 | #include <stdio.h> |
| 108 | #include <pthread.h> |
| 109 | |
| 110 | #ifdef DEBUG |
| 111 | #define dbg_printf(fmt, args...) printf(fmt, ## args) |
| 112 | #else |
| 113 | #define dbg_printf(fmt, args...) |
| 114 | #endif |
| 115 | |
| 116 | #define CHAIN_LEN_TARGET 4 |
| 117 | #define CHAIN_LEN_RESIZE_THRESHOLD 8 |
| 118 | |
| 119 | #ifndef max |
| 120 | #define max(a, b) ((a) > (b) ? (a) : (b)) |
| 121 | #endif |
| 122 | |
| 123 | /* |
| 124 | * The removed flag needs to be updated atomically with the pointer. |
| 125 | * The dummy flag does not require to be updated atomically with the |
| 126 | * pointer, but it is added as a pointer low bit flag to save space. |
| 127 | */ |
| 128 | #define REMOVED_FLAG (1UL << 0) |
| 129 | #define DUMMY_FLAG (1UL << 1) |
| 130 | #define FLAGS_MASK ((1UL << 2) - 1) |
| 131 | |
| 132 | struct rcu_table { |
| 133 | unsigned long size; /* always a power of 2 */ |
| 134 | unsigned long resize_target; |
| 135 | int resize_initiated; |
| 136 | struct rcu_head head; |
| 137 | struct _rcu_ht_node *tbl[0]; |
| 138 | }; |
| 139 | |
| 140 | struct rcu_ht { |
| 141 | struct rcu_table *t; /* shared */ |
| 142 | ht_hash_fct hash_fct; |
| 143 | ht_compare_fct compare_fct; |
| 144 | unsigned long hash_seed; |
| 145 | pthread_mutex_t resize_mutex; /* resize mutex: add/del mutex */ |
| 146 | unsigned int in_progress_resize, in_progress_destroy; |
| 147 | void (*ht_call_rcu)(struct rcu_head *head, |
| 148 | void (*func)(struct rcu_head *head)); |
| 149 | }; |
| 150 | |
| 151 | struct rcu_resize_work { |
| 152 | struct rcu_head head; |
| 153 | struct rcu_ht *ht; |
| 154 | }; |
| 155 | |
| 156 | /* |
| 157 | * Algorithm to reverse bits in a word by lookup table, extended to |
| 158 | * 64-bit words. |
| 159 | * Source: |
| 160 | * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable |
| 161 | * Originally from Public Domain. |
| 162 | */ |
| 163 | |
| 164 | static const uint8_t BitReverseTable256[256] = |
| 165 | { |
| 166 | #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64 |
| 167 | #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16) |
| 168 | #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 ) |
| 169 | R6(0), R6(2), R6(1), R6(3) |
| 170 | }; |
| 171 | #undef R2 |
| 172 | #undef R4 |
| 173 | #undef R6 |
| 174 | |
| 175 | static |
| 176 | uint8_t bit_reverse_u8(uint8_t v) |
| 177 | { |
| 178 | return BitReverseTable256[v]; |
| 179 | } |
| 180 | |
| 181 | static __attribute__((unused)) |
| 182 | uint32_t bit_reverse_u32(uint32_t v) |
| 183 | { |
| 184 | return ((uint32_t) bit_reverse_u8(v) << 24) | |
| 185 | ((uint32_t) bit_reverse_u8(v >> 8) << 16) | |
| 186 | ((uint32_t) bit_reverse_u8(v >> 16) << 8) | |
| 187 | ((uint32_t) bit_reverse_u8(v >> 24)); |
| 188 | } |
| 189 | |
| 190 | static __attribute__((unused)) |
| 191 | uint64_t bit_reverse_u64(uint64_t v) |
| 192 | { |
| 193 | return ((uint64_t) bit_reverse_u8(v) << 56) | |
| 194 | ((uint64_t) bit_reverse_u8(v >> 8) << 48) | |
| 195 | ((uint64_t) bit_reverse_u8(v >> 16) << 40) | |
| 196 | ((uint64_t) bit_reverse_u8(v >> 24) << 32) | |
| 197 | ((uint64_t) bit_reverse_u8(v >> 32) << 24) | |
| 198 | ((uint64_t) bit_reverse_u8(v >> 40) << 16) | |
| 199 | ((uint64_t) bit_reverse_u8(v >> 48) << 8) | |
| 200 | ((uint64_t) bit_reverse_u8(v >> 56)); |
| 201 | } |
| 202 | |
| 203 | static |
| 204 | unsigned long bit_reverse_ulong(unsigned long v) |
| 205 | { |
| 206 | #if (CAA_BITS_PER_LONG == 32) |
| 207 | return bit_reverse_u32(v); |
| 208 | #else |
| 209 | return bit_reverse_u64(v); |
| 210 | #endif |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * fls: returns the position of the most significant bit. |
| 215 | * Returns 0 if no bit is set, else returns the position of the most |
| 216 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). |
| 217 | */ |
| 218 | #if defined(__i386) || defined(__x86_64) |
| 219 | static inline |
| 220 | unsigned int fls_u32(uint32_t x) |
| 221 | { |
| 222 | int r; |
| 223 | |
| 224 | asm("bsrl %1,%0\n\t" |
| 225 | "jnz 1f\n\t" |
| 226 | "movl $-1,%0\n\t" |
| 227 | "1:\n\t" |
| 228 | : "=r" (r) : "rm" (x)); |
| 229 | return r + 1; |
| 230 | } |
| 231 | #define HAS_FLS_U32 |
| 232 | #endif |
| 233 | |
| 234 | #if defined(__x86_64) |
| 235 | static inline |
| 236 | unsigned int fls_u64(uint64_t x) |
| 237 | { |
| 238 | long r; |
| 239 | |
| 240 | asm("bsrq %1,%0\n\t" |
| 241 | "jnz 1f\n\t" |
| 242 | "movq $-1,%0\n\t" |
| 243 | "1:\n\t" |
| 244 | : "=r" (r) : "rm" (x)); |
| 245 | return r + 1; |
| 246 | } |
| 247 | #define HAS_FLS_U64 |
| 248 | #endif |
| 249 | |
| 250 | #ifndef HAS_FLS_U64 |
| 251 | static __attribute__((unused)) |
| 252 | unsigned int fls_u64(uint64_t x) |
| 253 | { |
| 254 | unsigned int r = 64; |
| 255 | |
| 256 | if (!x) |
| 257 | return 0; |
| 258 | |
| 259 | if (!(x & 0xFFFFFFFF00000000ULL)) { |
| 260 | x <<= 32; |
| 261 | r -= 32; |
| 262 | } |
| 263 | if (!(x & 0xFFFF000000000000ULL)) { |
| 264 | x <<= 16; |
| 265 | r -= 16; |
| 266 | } |
| 267 | if (!(x & 0xFF00000000000000ULL)) { |
| 268 | x <<= 8; |
| 269 | r -= 8; |
| 270 | } |
| 271 | if (!(x & 0xF000000000000000ULL)) { |
| 272 | x <<= 4; |
| 273 | r -= 4; |
| 274 | } |
| 275 | if (!(x & 0xC000000000000000ULL)) { |
| 276 | x <<= 2; |
| 277 | r -= 2; |
| 278 | } |
| 279 | if (!(x & 0x8000000000000000ULL)) { |
| 280 | x <<= 1; |
| 281 | r -= 1; |
| 282 | } |
| 283 | return r; |
| 284 | } |
| 285 | #endif |
| 286 | |
| 287 | #ifndef HAS_FLS_U32 |
| 288 | static __attribute__((unused)) |
| 289 | unsigned int fls_u32(uint32_t x) |
| 290 | { |
| 291 | unsigned int r = 32; |
| 292 | |
| 293 | if (!x) |
| 294 | return 0; |
| 295 | if (!(x & 0xFFFF0000U)) { |
| 296 | x <<= 16; |
| 297 | r -= 16; |
| 298 | } |
| 299 | if (!(x & 0xFF000000U)) { |
| 300 | x <<= 8; |
| 301 | r -= 8; |
| 302 | } |
| 303 | if (!(x & 0xF0000000U)) { |
| 304 | x <<= 4; |
| 305 | r -= 4; |
| 306 | } |
| 307 | if (!(x & 0xC0000000U)) { |
| 308 | x <<= 2; |
| 309 | r -= 2; |
| 310 | } |
| 311 | if (!(x & 0x80000000U)) { |
| 312 | x <<= 1; |
| 313 | r -= 1; |
| 314 | } |
| 315 | return r; |
| 316 | } |
| 317 | #endif |
| 318 | |
| 319 | unsigned int fls_ulong(unsigned long x) |
| 320 | { |
| 321 | #if (CAA_BITS_PER_lONG == 32) |
| 322 | return fls_u32(x); |
| 323 | #else |
| 324 | return fls_u64(x); |
| 325 | #endif |
| 326 | } |
| 327 | |
| 328 | int get_count_order_u32(uint32_t x) |
| 329 | { |
| 330 | int order; |
| 331 | |
| 332 | order = fls_u32(x) - 1; |
| 333 | if (x & (x - 1)) |
| 334 | order++; |
| 335 | return order; |
| 336 | } |
| 337 | |
| 338 | int get_count_order_ulong(unsigned long x) |
| 339 | { |
| 340 | int order; |
| 341 | |
| 342 | order = fls_ulong(x) - 1; |
| 343 | if (x & (x - 1)) |
| 344 | order++; |
| 345 | return order; |
| 346 | } |
| 347 | |
| 348 | static |
| 349 | void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth); |
| 350 | |
| 351 | static |
| 352 | void check_resize(struct rcu_ht *ht, struct rcu_table *t, |
| 353 | uint32_t chain_len) |
| 354 | { |
| 355 | if (chain_len > 100) |
| 356 | dbg_printf("rculfhash: WARNING: large chain length: %u.\n", |
| 357 | chain_len); |
| 358 | if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD) |
| 359 | ht_resize_lazy(ht, t, |
| 360 | get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1))); |
| 361 | } |
| 362 | |
| 363 | static |
| 364 | struct rcu_ht_node *clear_flag(struct rcu_ht_node *node) |
| 365 | { |
| 366 | return (struct rcu_ht_node *) (((unsigned long) node) & ~FLAGS_MASK); |
| 367 | } |
| 368 | |
| 369 | static |
| 370 | int is_removed(struct rcu_ht_node *node) |
| 371 | { |
| 372 | return ((unsigned long) node) & REMOVED_FLAG; |
| 373 | } |
| 374 | |
| 375 | static |
| 376 | struct rcu_ht_node *flag_removed(struct rcu_ht_node *node) |
| 377 | { |
| 378 | return (struct rcu_ht_node *) (((unsigned long) node) | REMOVED_FLAG); |
| 379 | } |
| 380 | |
| 381 | static |
| 382 | int is_dummy(struct rcu_ht_node *node) |
| 383 | { |
| 384 | return ((unsigned long) node) & DUMMY_FLAG; |
| 385 | } |
| 386 | |
| 387 | static |
| 388 | struct rcu_ht_node *flag_dummy(struct rcu_ht_node *node) |
| 389 | { |
| 390 | return (struct rcu_ht_node *) (((unsigned long) node) | DUMMY_FLAG); |
| 391 | } |
| 392 | |
| 393 | static |
| 394 | unsigned long _uatomic_max(unsigned long *ptr, unsigned long v) |
| 395 | { |
| 396 | unsigned long old1, old2; |
| 397 | |
| 398 | old1 = uatomic_read(ptr); |
| 399 | do { |
| 400 | old2 = old1; |
| 401 | if (old2 >= v) |
| 402 | return old2; |
| 403 | } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2); |
| 404 | return v; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Remove all logically deleted nodes from a bucket up to a certain node key. |
| 409 | */ |
| 410 | static |
| 411 | void _ht_gc_bucket(struct rcu_ht_node *dummy, struct rcu_ht_node *node) |
| 412 | { |
| 413 | struct rcu_ht_node *iter_prev, *iter, *next, *new_next; |
| 414 | |
| 415 | for (;;) { |
| 416 | iter_prev = dummy; |
| 417 | /* We can always skip the dummy node initially */ |
| 418 | iter = rcu_dereference(iter_prev->p.next); |
| 419 | assert(iter_prev->p.reverse_hash <= node->p.reverse_hash); |
| 420 | for (;;) { |
| 421 | if (unlikely(!clear_flag(iter))) |
| 422 | return; |
| 423 | if (clear_flag(iter)->p.reverse_hash > node->p.reverse_hash) |
| 424 | return; |
| 425 | next = rcu_dereference(clear_flag(iter)->p.next); |
| 426 | if (is_removed(next)) |
| 427 | break; |
| 428 | iter_prev = clear_flag(iter); |
| 429 | iter = next; |
| 430 | } |
| 431 | assert(!is_removed(iter)); |
| 432 | if (is_dummy(iter)) |
| 433 | new_next = flag_dummy(clear_flag(next)); |
| 434 | else |
| 435 | new_next = clear_flag(next); |
| 436 | (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | static |
| 441 | struct rcu_ht_node *_ht_add(struct rcu_ht *ht, struct rcu_table *t, |
| 442 | struct rcu_ht_node *node, int unique, int dummy) |
| 443 | { |
| 444 | struct rcu_ht_node *iter_prev, *iter, *next, *new_node, *new_next, |
| 445 | *dummy_node; |
| 446 | struct _rcu_ht_node *lookup; |
| 447 | unsigned long hash, index, order; |
| 448 | |
| 449 | if (!t->size) { |
| 450 | assert(dummy); |
| 451 | node->p.next = flag_dummy(NULL); |
| 452 | return node; /* Initial first add (head) */ |
| 453 | } |
| 454 | hash = bit_reverse_ulong(node->p.reverse_hash); |
| 455 | for (;;) { |
| 456 | uint32_t chain_len = 0; |
| 457 | |
| 458 | /* |
| 459 | * iter_prev points to the non-removed node prior to the |
| 460 | * insert location. |
| 461 | */ |
| 462 | index = hash & (t->size - 1); |
| 463 | order = get_count_order_ulong(index + 1); |
| 464 | lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)]; |
| 465 | iter_prev = (struct rcu_ht_node *) lookup; |
| 466 | /* We can always skip the dummy node initially */ |
| 467 | iter = rcu_dereference(iter_prev->p.next); |
| 468 | assert(iter_prev->p.reverse_hash <= node->p.reverse_hash); |
| 469 | for (;;) { |
| 470 | if (unlikely(!clear_flag(iter))) |
| 471 | goto insert; |
| 472 | if (clear_flag(iter)->p.reverse_hash > node->p.reverse_hash) |
| 473 | goto insert; |
| 474 | next = rcu_dereference(clear_flag(iter)->p.next); |
| 475 | if (is_removed(next)) |
| 476 | goto gc_node; |
| 477 | if (unique |
| 478 | && !is_dummy(next) |
| 479 | && !ht->compare_fct(node->key, node->key_len, |
| 480 | clear_flag(iter)->key, |
| 481 | clear_flag(iter)->key_len)) |
| 482 | return clear_flag(iter); |
| 483 | /* Only account for identical reverse hash once */ |
| 484 | if (iter_prev->p.reverse_hash != clear_flag(iter)->p.reverse_hash |
| 485 | && !is_dummy(next)) |
| 486 | check_resize(ht, t, ++chain_len); |
| 487 | iter_prev = clear_flag(iter); |
| 488 | iter = next; |
| 489 | } |
| 490 | insert: |
| 491 | assert(node != clear_flag(iter)); |
| 492 | assert(!is_removed(iter_prev)); |
| 493 | assert(iter_prev != node); |
| 494 | if (!dummy) |
| 495 | node->p.next = clear_flag(iter); |
| 496 | else |
| 497 | node->p.next = flag_dummy(clear_flag(iter)); |
| 498 | if (is_dummy(iter)) |
| 499 | new_node = flag_dummy(node); |
| 500 | else |
| 501 | new_node = node; |
| 502 | if (uatomic_cmpxchg(&iter_prev->p.next, iter, |
| 503 | new_node) != iter) |
| 504 | continue; /* retry */ |
| 505 | else |
| 506 | goto gc_end; |
| 507 | gc_node: |
| 508 | assert(!is_removed(iter)); |
| 509 | if (is_dummy(iter)) |
| 510 | new_next = flag_dummy(clear_flag(next)); |
| 511 | else |
| 512 | new_next = clear_flag(next); |
| 513 | (void) uatomic_cmpxchg(&iter_prev->p.next, iter, new_next); |
| 514 | /* retry */ |
| 515 | } |
| 516 | gc_end: |
| 517 | /* Garbage collect logically removed nodes in the bucket */ |
| 518 | index = hash & (t->size - 1); |
| 519 | order = get_count_order_ulong(index + 1); |
| 520 | lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)]; |
| 521 | dummy_node = (struct rcu_ht_node *) lookup; |
| 522 | _ht_gc_bucket(dummy_node, node); |
| 523 | return node; |
| 524 | } |
| 525 | |
| 526 | static |
| 527 | int _ht_remove(struct rcu_ht *ht, struct rcu_table *t, struct rcu_ht_node *node) |
| 528 | { |
| 529 | struct rcu_ht_node *dummy, *next, *old; |
| 530 | struct _rcu_ht_node *lookup; |
| 531 | int flagged = 0; |
| 532 | unsigned long hash, index, order; |
| 533 | |
| 534 | /* logically delete the node */ |
| 535 | old = rcu_dereference(node->p.next); |
| 536 | do { |
| 537 | next = old; |
| 538 | if (is_removed(next)) |
| 539 | goto end; |
| 540 | assert(!is_dummy(next)); |
| 541 | old = uatomic_cmpxchg(&node->p.next, next, |
| 542 | flag_removed(next)); |
| 543 | } while (old != next); |
| 544 | |
| 545 | /* We performed the (logical) deletion. */ |
| 546 | flagged = 1; |
| 547 | |
| 548 | /* |
| 549 | * Ensure that the node is not visible to readers anymore: lookup for |
| 550 | * the node, and remove it (along with any other logically removed node) |
| 551 | * if found. |
| 552 | */ |
| 553 | hash = bit_reverse_ulong(node->p.reverse_hash); |
| 554 | index = hash & (t->size - 1); |
| 555 | order = get_count_order_ulong(index + 1); |
| 556 | lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)]; |
| 557 | dummy = (struct rcu_ht_node *) lookup; |
| 558 | _ht_gc_bucket(dummy, node); |
| 559 | end: |
| 560 | /* |
| 561 | * Only the flagging action indicated that we (and no other) |
| 562 | * removed the node from the hash. |
| 563 | */ |
| 564 | if (flagged) { |
| 565 | assert(is_removed(rcu_dereference(node->p.next))); |
| 566 | return 0; |
| 567 | } else |
| 568 | return -ENOENT; |
| 569 | } |
| 570 | |
| 571 | static |
| 572 | void init_table(struct rcu_ht *ht, struct rcu_table *t, |
| 573 | unsigned long first_order, unsigned long len_order) |
| 574 | { |
| 575 | unsigned long i, end_order; |
| 576 | |
| 577 | dbg_printf("rculfhash: init table: first_order %lu end_order %lu\n", |
| 578 | first_order, first_order + len_order); |
| 579 | end_order = first_order + len_order; |
| 580 | t->size = !first_order ? 0 : (1UL << (first_order - 1)); |
| 581 | for (i = first_order; i < end_order; i++) { |
| 582 | unsigned long j, len; |
| 583 | |
| 584 | len = !i ? 1 : 1UL << (i - 1); |
| 585 | dbg_printf("rculfhash: init order %lu len: %lu\n", i, len); |
| 586 | t->tbl[i] = calloc(len, sizeof(struct _rcu_ht_node)); |
| 587 | for (j = 0; j < len; j++) { |
| 588 | dbg_printf("rculfhash: init entry: i %lu j %lu hash %lu\n", |
| 589 | i, j, !i ? 0 : (1UL << (i - 1)) + j); |
| 590 | struct rcu_ht_node *new_node = |
| 591 | (struct rcu_ht_node *) &t->tbl[i][j]; |
| 592 | new_node->p.reverse_hash = |
| 593 | bit_reverse_ulong(!i ? 0 : (1UL << (i - 1)) + j); |
| 594 | (void) _ht_add(ht, t, new_node, 0, 1); |
| 595 | if (CMM_LOAD_SHARED(ht->in_progress_destroy)) |
| 596 | break; |
| 597 | } |
| 598 | /* Update table size */ |
| 599 | t->size = !i ? 1 : (1UL << i); |
| 600 | dbg_printf("rculfhash: init new size: %lu\n", t->size); |
| 601 | if (CMM_LOAD_SHARED(ht->in_progress_destroy)) |
| 602 | break; |
| 603 | } |
| 604 | t->resize_target = t->size; |
| 605 | t->resize_initiated = 0; |
| 606 | } |
| 607 | |
| 608 | struct rcu_ht *ht_new(ht_hash_fct hash_fct, |
| 609 | ht_compare_fct compare_fct, |
| 610 | unsigned long hash_seed, |
| 611 | unsigned long init_size, |
| 612 | void (*ht_call_rcu)(struct rcu_head *head, |
| 613 | void (*func)(struct rcu_head *head))) |
| 614 | { |
| 615 | struct rcu_ht *ht; |
| 616 | unsigned long order; |
| 617 | |
| 618 | ht = calloc(1, sizeof(struct rcu_ht)); |
| 619 | ht->hash_fct = hash_fct; |
| 620 | ht->compare_fct = compare_fct; |
| 621 | ht->hash_seed = hash_seed; |
| 622 | ht->ht_call_rcu = ht_call_rcu; |
| 623 | ht->in_progress_resize = 0; |
| 624 | /* this mutex should not nest in read-side C.S. */ |
| 625 | pthread_mutex_init(&ht->resize_mutex, NULL); |
| 626 | order = get_count_order_ulong(max(init_size, 1)) + 1; |
| 627 | ht->t = calloc(1, sizeof(struct rcu_table) |
| 628 | + (order * sizeof(struct _rcu_ht_node *))); |
| 629 | ht->t->size = 0; |
| 630 | pthread_mutex_lock(&ht->resize_mutex); |
| 631 | init_table(ht, ht->t, 0, order); |
| 632 | pthread_mutex_unlock(&ht->resize_mutex); |
| 633 | return ht; |
| 634 | } |
| 635 | |
| 636 | struct rcu_ht_node *ht_lookup(struct rcu_ht *ht, void *key, size_t key_len) |
| 637 | { |
| 638 | struct rcu_table *t; |
| 639 | struct rcu_ht_node *node, *next; |
| 640 | struct _rcu_ht_node *lookup; |
| 641 | unsigned long hash, reverse_hash, index, order; |
| 642 | |
| 643 | hash = ht->hash_fct(key, key_len, ht->hash_seed); |
| 644 | reverse_hash = bit_reverse_ulong(hash); |
| 645 | |
| 646 | t = rcu_dereference(ht->t); |
| 647 | index = hash & (t->size - 1); |
| 648 | order = get_count_order_ulong(index + 1); |
| 649 | lookup = &t->tbl[order][index & ((1UL << (order - 1)) - 1)]; |
| 650 | dbg_printf("rculfhash: lookup hash %lu index %lu order %lu aridx %lu\n", |
| 651 | hash, index, order, index & ((1UL << (order - 1)) - 1)); |
| 652 | node = (struct rcu_ht_node *) lookup; |
| 653 | for (;;) { |
| 654 | if (unlikely(!node)) |
| 655 | break; |
| 656 | if (unlikely(node->p.reverse_hash > reverse_hash)) { |
| 657 | node = NULL; |
| 658 | break; |
| 659 | } |
| 660 | next = rcu_dereference(node->p.next); |
| 661 | if (likely(!is_removed(next)) |
| 662 | && !is_dummy(next) |
| 663 | && likely(!ht->compare_fct(node->key, node->key_len, key, key_len))) { |
| 664 | break; |
| 665 | } |
| 666 | node = clear_flag(next); |
| 667 | } |
| 668 | assert(!node || !is_dummy(rcu_dereference(node->p.next))); |
| 669 | return node; |
| 670 | } |
| 671 | |
| 672 | void ht_add(struct rcu_ht *ht, struct rcu_ht_node *node) |
| 673 | { |
| 674 | struct rcu_table *t; |
| 675 | unsigned long hash; |
| 676 | |
| 677 | hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed); |
| 678 | node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash); |
| 679 | |
| 680 | t = rcu_dereference(ht->t); |
| 681 | (void) _ht_add(ht, t, node, 0, 0); |
| 682 | } |
| 683 | |
| 684 | struct rcu_ht_node *ht_add_unique(struct rcu_ht *ht, struct rcu_ht_node *node) |
| 685 | { |
| 686 | struct rcu_table *t; |
| 687 | unsigned long hash; |
| 688 | |
| 689 | hash = ht->hash_fct(node->key, node->key_len, ht->hash_seed); |
| 690 | node->p.reverse_hash = bit_reverse_ulong((unsigned long) hash); |
| 691 | |
| 692 | t = rcu_dereference(ht->t); |
| 693 | return _ht_add(ht, t, node, 1, 0); |
| 694 | } |
| 695 | |
| 696 | int ht_remove(struct rcu_ht *ht, struct rcu_ht_node *node) |
| 697 | { |
| 698 | struct rcu_table *t; |
| 699 | |
| 700 | t = rcu_dereference(ht->t); |
| 701 | return _ht_remove(ht, t, node); |
| 702 | } |
| 703 | |
| 704 | static |
| 705 | int ht_delete_dummy(struct rcu_ht *ht) |
| 706 | { |
| 707 | struct rcu_table *t; |
| 708 | struct rcu_ht_node *node; |
| 709 | struct _rcu_ht_node *lookup; |
| 710 | unsigned long order, i; |
| 711 | |
| 712 | t = ht->t; |
| 713 | /* Check that the table is empty */ |
| 714 | lookup = &t->tbl[0][0]; |
| 715 | node = (struct rcu_ht_node *) lookup; |
| 716 | do { |
| 717 | node = clear_flag(node)->p.next; |
| 718 | if (!is_dummy(node)) |
| 719 | return -EPERM; |
| 720 | assert(!is_removed(node)); |
| 721 | } while (clear_flag(node)); |
| 722 | /* Internal sanity check: all nodes left should be dummy */ |
| 723 | for (order = 0; order < get_count_order_ulong(t->size) + 1; order++) { |
| 724 | unsigned long len; |
| 725 | |
| 726 | len = !order ? 1 : 1UL << (order - 1); |
| 727 | for (i = 0; i < len; i++) { |
| 728 | dbg_printf("rculfhash: delete order %lu i %lu hash %lu\n", |
| 729 | order, i, |
| 730 | bit_reverse_ulong(t->tbl[order][i].reverse_hash)); |
| 731 | assert(is_dummy(t->tbl[order][i].next)); |
| 732 | } |
| 733 | free(t->tbl[order]); |
| 734 | } |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Should only be called when no more concurrent readers nor writers can |
| 740 | * possibly access the table. |
| 741 | */ |
| 742 | int ht_destroy(struct rcu_ht *ht) |
| 743 | { |
| 744 | int ret; |
| 745 | |
| 746 | /* Wait for in-flight resize operations to complete */ |
| 747 | CMM_STORE_SHARED(ht->in_progress_destroy, 1); |
| 748 | while (uatomic_read(&ht->in_progress_resize)) |
| 749 | poll(NULL, 0, 100); /* wait for 100ms */ |
| 750 | ret = ht_delete_dummy(ht); |
| 751 | if (ret) |
| 752 | return ret; |
| 753 | free(ht->t); |
| 754 | free(ht); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | void ht_count_nodes(struct rcu_ht *ht, |
| 759 | unsigned long *count, |
| 760 | unsigned long *removed) |
| 761 | { |
| 762 | struct rcu_table *t; |
| 763 | struct rcu_ht_node *node, *next; |
| 764 | struct _rcu_ht_node *lookup; |
| 765 | unsigned long nr_dummy = 0; |
| 766 | |
| 767 | *count = 0; |
| 768 | *removed = 0; |
| 769 | |
| 770 | t = rcu_dereference(ht->t); |
| 771 | /* Count non-dummy nodes in the table */ |
| 772 | lookup = &t->tbl[0][0]; |
| 773 | node = (struct rcu_ht_node *) lookup; |
| 774 | do { |
| 775 | next = rcu_dereference(node->p.next); |
| 776 | if (is_removed(next)) { |
| 777 | assert(!is_dummy(next)); |
| 778 | (*removed)++; |
| 779 | } else if (!is_dummy(next)) |
| 780 | (*count)++; |
| 781 | else |
| 782 | (nr_dummy)++; |
| 783 | node = clear_flag(next); |
| 784 | } while (node); |
| 785 | dbg_printf("rculfhash: number of dummy nodes: %lu\n", nr_dummy); |
| 786 | } |
| 787 | |
| 788 | static |
| 789 | void ht_free_table_cb(struct rcu_head *head) |
| 790 | { |
| 791 | struct rcu_table *t = |
| 792 | caa_container_of(head, struct rcu_table, head); |
| 793 | free(t); |
| 794 | } |
| 795 | |
| 796 | /* called with resize mutex held */ |
| 797 | static |
| 798 | void _do_ht_resize(struct rcu_ht *ht) |
| 799 | { |
| 800 | unsigned long new_size, old_size, old_order, new_order; |
| 801 | struct rcu_table *new_t, *old_t; |
| 802 | |
| 803 | old_t = ht->t; |
| 804 | old_size = old_t->size; |
| 805 | old_order = get_count_order_ulong(old_size) + 1; |
| 806 | |
| 807 | new_size = CMM_LOAD_SHARED(old_t->resize_target); |
| 808 | if (old_size == new_size) |
| 809 | return; |
| 810 | new_order = get_count_order_ulong(new_size) + 1; |
| 811 | printf("rculfhash: resize from %lu (order %lu) to %lu (order %lu) buckets\n", |
| 812 | old_size, old_order, new_size, new_order); |
| 813 | new_t = malloc(sizeof(struct rcu_table) |
| 814 | + (new_order * sizeof(struct _rcu_ht_node *))); |
| 815 | assert(new_size > old_size); |
| 816 | memcpy(&new_t->tbl, &old_t->tbl, |
| 817 | old_order * sizeof(struct _rcu_ht_node *)); |
| 818 | init_table(ht, new_t, old_order, new_order - old_order); |
| 819 | /* Changing table and size atomically wrt lookups */ |
| 820 | rcu_assign_pointer(ht->t, new_t); |
| 821 | ht->ht_call_rcu(&old_t->head, ht_free_table_cb); |
| 822 | } |
| 823 | |
| 824 | static |
| 825 | unsigned long resize_target_update(struct rcu_table *t, |
| 826 | int growth_order) |
| 827 | { |
| 828 | return _uatomic_max(&t->resize_target, |
| 829 | t->size << growth_order); |
| 830 | } |
| 831 | |
| 832 | void ht_resize(struct rcu_ht *ht, int growth) |
| 833 | { |
| 834 | struct rcu_table *t = rcu_dereference(ht->t); |
| 835 | unsigned long target_size; |
| 836 | |
| 837 | target_size = resize_target_update(t, growth); |
| 838 | if (t->size < target_size) { |
| 839 | CMM_STORE_SHARED(t->resize_initiated, 1); |
| 840 | pthread_mutex_lock(&ht->resize_mutex); |
| 841 | _do_ht_resize(ht); |
| 842 | pthread_mutex_unlock(&ht->resize_mutex); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | static |
| 847 | void do_resize_cb(struct rcu_head *head) |
| 848 | { |
| 849 | struct rcu_resize_work *work = |
| 850 | caa_container_of(head, struct rcu_resize_work, head); |
| 851 | struct rcu_ht *ht = work->ht; |
| 852 | |
| 853 | pthread_mutex_lock(&ht->resize_mutex); |
| 854 | _do_ht_resize(ht); |
| 855 | pthread_mutex_unlock(&ht->resize_mutex); |
| 856 | free(work); |
| 857 | cmm_smp_mb(); /* finish resize before decrement */ |
| 858 | uatomic_dec(&ht->in_progress_resize); |
| 859 | } |
| 860 | |
| 861 | static |
| 862 | void ht_resize_lazy(struct rcu_ht *ht, struct rcu_table *t, int growth) |
| 863 | { |
| 864 | struct rcu_resize_work *work; |
| 865 | unsigned long target_size; |
| 866 | |
| 867 | target_size = resize_target_update(t, growth); |
| 868 | if (!CMM_LOAD_SHARED(t->resize_initiated) && t->size < target_size) { |
| 869 | uatomic_inc(&ht->in_progress_resize); |
| 870 | cmm_smp_mb(); /* increment resize count before calling it */ |
| 871 | work = malloc(sizeof(*work)); |
| 872 | work->ht = ht; |
| 873 | ht->ht_call_rcu(&work->head, do_resize_cb); |
| 874 | CMM_STORE_SHARED(t->resize_initiated, 1); |
| 875 | } |
| 876 | } |