rculfhash: Ensure future-proof memory barrier semantic consistency
[urcu.git] / rculfhash.c
1 /*
2 * rculfhash.c
3 *
4 * Userspace RCU library - Lock-Free Resizable RCU Hash Table
5 *
6 * Copyright 2010-2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * Copyright 2011 - Lai Jiangshan <laijs@cn.fujitsu.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /*
25 * Based on the following articles:
26 * - Ori Shalev and Nir Shavit. Split-ordered lists: Lock-free
27 * extensible hash tables. J. ACM 53, 3 (May 2006), 379-405.
28 * - Michael, M. M. High performance dynamic lock-free hash tables
29 * and list-based sets. In Proceedings of the fourteenth annual ACM
30 * symposium on Parallel algorithms and architectures, ACM Press,
31 * (2002), 73-82.
32 *
33 * Some specificities of this Lock-Free Resizable RCU Hash Table
34 * implementation:
35 *
36 * - RCU read-side critical section allows readers to perform hash
37 * table lookups, as well as traversals, and use the returned objects
38 * safely by allowing memory reclaim to take place only after a grace
39 * period.
40 * - Add and remove operations are lock-free, and do not need to
41 * allocate memory. They need to be executed within RCU read-side
42 * critical section to ensure the objects they read are valid and to
43 * deal with the cmpxchg ABA problem.
44 * - add and add_unique operations are supported. add_unique checks if
45 * the node key already exists in the hash table. It ensures not to
46 * populate a duplicate key if the node key already exists in the hash
47 * table.
48 * - The resize operation executes concurrently with
49 * add/add_unique/add_replace/remove/lookup/traversal.
50 * - Hash table nodes are contained within a split-ordered list. This
51 * list is ordered by incrementing reversed-bits-hash value.
52 * - An index of bucket nodes is kept. These bucket nodes are the hash
53 * table "buckets". These buckets are internal nodes that allow to
54 * perform a fast hash lookup, similarly to a skip list. These
55 * buckets are chained together in the split-ordered list, which
56 * allows recursive expansion by inserting new buckets between the
57 * existing buckets. The split-ordered list allows adding new buckets
58 * between existing buckets as the table needs to grow.
59 * - The resize operation for small tables only allows expanding the
60 * hash table. It is triggered automatically by detecting long chains
61 * in the add operation.
62 * - The resize operation for larger tables (and available through an
63 * API) allows both expanding and shrinking the hash table.
64 * - Split-counters are used to keep track of the number of
65 * nodes within the hash table for automatic resize triggering.
66 * - Resize operation initiated by long chain detection is executed by a
67 * call_rcu thread, which keeps lock-freedom of add and remove.
68 * - Resize operations are protected by a mutex.
69 * - The removal operation is split in two parts: first, a "removed"
70 * flag is set in the next pointer within the node to remove. Then,
71 * a "garbage collection" is performed in the bucket containing the
72 * removed node (from the start of the bucket up to the removed node).
73 * All encountered nodes with "removed" flag set in their next
74 * pointers are removed from the linked-list. If the cmpxchg used for
75 * removal fails (due to concurrent garbage-collection or concurrent
76 * add), we retry from the beginning of the bucket. This ensures that
77 * the node with "removed" flag set is removed from the hash table
78 * (not visible to lookups anymore) before the RCU read-side critical
79 * section held across removal ends. Furthermore, this ensures that
80 * the node with "removed" flag set is removed from the linked-list
81 * before its memory is reclaimed. After setting the "removal" flag,
82 * only the thread which removal is the first to set the "removal
83 * owner" flag (with an xchg) into a node's next pointer is considered
84 * to have succeeded its removal (and thus owns the node to reclaim).
85 * Because we garbage-collect starting from an invariant node (the
86 * start-of-bucket bucket node) up to the "removed" node (or find a
87 * reverse-hash that is higher), we are sure that a successful
88 * traversal of the chain leads to a chain that is present in the
89 * linked-list (the start node is never removed) and that it does not
90 * contain the "removed" node anymore, even if concurrent delete/add
91 * operations are changing the structure of the list concurrently.
92 * - The add operations perform garbage collection of buckets if they
93 * encounter nodes with removed flag set in the bucket where they want
94 * to add their new node. This ensures lock-freedom of add operation by
95 * helping the remover unlink nodes from the list rather than to wait
96 * for it do to so.
97 * - There are three memory backends for the hash table buckets: the
98 * "order table", the "chunks", and the "mmap".
99 * - These bucket containers contain a compact version of the hash table
100 * nodes.
101 * - The RCU "order table":
102 * - has a first level table indexed by log2(hash index) which is
103 * copied and expanded by the resize operation. This order table
104 * allows finding the "bucket node" tables.
105 * - There is one bucket node table per hash index order. The size of
106 * each bucket node table is half the number of hashes contained in
107 * this order (except for order 0).
108 * - The RCU "chunks" is best suited for close interaction with a page
109 * allocator. It uses a linear array as index to "chunks" containing
110 * each the same number of buckets.
111 * - The RCU "mmap" memory backend uses a single memory map to hold
112 * all buckets.
113 * - synchronize_rcu is used to garbage-collect the old bucket node table.
114 *
115 * Ordering Guarantees:
116 *
117 * To discuss these guarantees, we first define "read" operation as any
118 * of the the basic cds_lfht_lookup, cds_lfht_next_duplicate,
119 * cds_lfht_first, cds_lfht_next operation, as well as
120 * cds_lfht_add_unique (failure).
121 *
122 * We define "read traversal" operation as any of the following
123 * group of operations
124 * - cds_lfht_lookup followed by iteration with cds_lfht_next_duplicate
125 * (and/or cds_lfht_next, although less common).
126 * - cds_lfht_add_unique (failure) followed by iteration with
127 * cds_lfht_next_duplicate (and/or cds_lfht_next, although less
128 * common).
129 * - cds_lfht_first followed iteration with cds_lfht_next (and/or
130 * cds_lfht_next_duplicate, although less common).
131 *
132 * We define "write" operations as any of cds_lfht_add,
133 * cds_lfht_add_unique (success), cds_lfht_add_replace, cds_lfht_del.
134 *
135 * When cds_lfht_add_unique succeeds (returns the node passed as
136 * parameter), it acts as a "write" operation. When cds_lfht_add_unique
137 * fails (returns a node different from the one passed as parameter), it
138 * acts as a "read" operation. A cds_lfht_add_unique failure is a
139 * cds_lfht_lookup "read" operation, therefore, any ordering guarantee
140 * referring to "lookup" imply any of "lookup" or cds_lfht_add_unique
141 * (failure).
142 *
143 * We define "prior" and "later" node as nodes observable by reads and
144 * read traversals respectively before and after a write or sequence of
145 * write operations.
146 *
147 * Hash-table operations are often cascaded, for example, the pointer
148 * returned by a cds_lfht_lookup() might be passed to a cds_lfht_next(),
149 * whose return value might in turn be passed to another hash-table
150 * operation. This entire cascaded series of operations must be enclosed
151 * by a pair of matching rcu_read_lock() and rcu_read_unlock()
152 * operations.
153 *
154 * The following ordering guarantees are offered by this hash table:
155 *
156 * A.1) "read" after "write": if there is ordering between a write and a
157 * later read, then the read is guaranteed to see the write or some
158 * later write.
159 * A.2) "read traversal" after "write": given that there is dependency
160 * ordering between reads in a "read traversal", if there is
161 * ordering between a write and the first read of the traversal,
162 * then the "read traversal" is guaranteed to see the write or
163 * some later write.
164 * B.1) "write" after "read": if there is ordering between a read and a
165 * later write, then the read will never see the write.
166 * B.2) "write" after "read traversal": given that there is dependency
167 * ordering between reads in a "read traversal", if there is
168 * ordering between the last read of the traversal and a later
169 * write, then the "read traversal" will never see the write.
170 * C) "write" while "read traversal": if a write occurs during a "read
171 * traversal", the traversal may, or may not, see the write.
172 * D.1) "write" after "write": if there is ordering between a write and
173 * a later write, then the later write is guaranteed to see the
174 * effects of the first write.
175 * D.2) Concurrent "write" pairs: The system will assign an arbitrary
176 * order to any pair of concurrent conflicting writes.
177 * Non-conflicting writes (for example, to different keys) are
178 * unordered.
179 * E) If a grace period separates a "del" or "replace" operation
180 * and a subsequent operation, then that subsequent operation is
181 * guaranteed not to see the removed item.
182 * F) Uniqueness guarantee: given a hash table that does not contain
183 * duplicate items for a given key, there will only be one item in
184 * the hash table after an arbitrary sequence of add_unique and/or
185 * add_replace operations. Note, however, that a pair of
186 * concurrent read operations might well access two different items
187 * with that key.
188 * G.1) If a pair of lookups for a given key are ordered (e.g. by a
189 * memory barrier), then the second lookup will return the same
190 * node as the previous lookup, or some later node.
191 * G.2) A "read traversal" that starts after the end of a prior "read
192 * traversal" (ordered by memory barriers) is guaranteed to see the
193 * same nodes as the previous traversal, or some later nodes.
194 * G.3) Concurrent "read" pairs: concurrent reads are unordered. For
195 * example, if a pair of reads to the same key run concurrently
196 * with an insertion of that same key, the reads remain unordered
197 * regardless of their return values. In other words, you cannot
198 * rely on the values returned by the reads to deduce ordering.
199 *
200 * Progress guarantees:
201 *
202 * * Reads are wait-free. These operations always move forward in the
203 * hash table linked list, and this list has no loop.
204 * * Writes are lock-free. Any retry loop performed by a write operation
205 * is triggered by progress made within another update operation.
206 *
207 * Bucket node tables:
208 *
209 * hash table hash table the last all bucket node tables
210 * order size bucket node 0 1 2 3 4 5 6(index)
211 * table size
212 * 0 1 1 1
213 * 1 2 1 1 1
214 * 2 4 2 1 1 2
215 * 3 8 4 1 1 2 4
216 * 4 16 8 1 1 2 4 8
217 * 5 32 16 1 1 2 4 8 16
218 * 6 64 32 1 1 2 4 8 16 32
219 *
220 * When growing/shrinking, we only focus on the last bucket node table
221 * which size is (!order ? 1 : (1 << (order -1))).
222 *
223 * Example for growing/shrinking:
224 * grow hash table from order 5 to 6: init the index=6 bucket node table
225 * shrink hash table from order 6 to 5: fini the index=6 bucket node table
226 *
227 * A bit of ascii art explanation:
228 *
229 * The order index is the off-by-one compared to the actual power of 2
230 * because we use index 0 to deal with the 0 special-case.
231 *
232 * This shows the nodes for a small table ordered by reversed bits:
233 *
234 * bits reverse
235 * 0 000 000
236 * 4 100 001
237 * 2 010 010
238 * 6 110 011
239 * 1 001 100
240 * 5 101 101
241 * 3 011 110
242 * 7 111 111
243 *
244 * This shows the nodes in order of non-reversed bits, linked by
245 * reversed-bit order.
246 *
247 * order bits reverse
248 * 0 0 000 000
249 * 1 | 1 001 100 <-
250 * 2 | | 2 010 010 <- |
251 * | | | 3 011 110 | <- |
252 * 3 -> | | | 4 100 001 | |
253 * -> | | 5 101 101 |
254 * -> | 6 110 011
255 * -> 7 111 111
256 */
257
258 #define _LGPL_SOURCE
259 #define _GNU_SOURCE
260 #include <stdlib.h>
261 #include <errno.h>
262 #include <assert.h>
263 #include <stdio.h>
264 #include <stdint.h>
265 #include <string.h>
266 #include <sched.h>
267
268 #include "config.h"
269 #include <urcu.h>
270 #include <urcu-call-rcu.h>
271 #include <urcu-flavor.h>
272 #include <urcu/arch.h>
273 #include <urcu/uatomic.h>
274 #include <urcu/compiler.h>
275 #include <urcu/rculfhash.h>
276 #include <rculfhash-internal.h>
277 #include <stdio.h>
278 #include <pthread.h>
279
280 /*
281 * Split-counters lazily update the global counter each 1024
282 * addition/removal. It automatically keeps track of resize required.
283 * We use the bucket length as indicator for need to expand for small
284 * tables and machines lacking per-cpu data suppport.
285 */
286 #define COUNT_COMMIT_ORDER 10
287 #define DEFAULT_SPLIT_COUNT_MASK 0xFUL
288 #define CHAIN_LEN_TARGET 1
289 #define CHAIN_LEN_RESIZE_THRESHOLD 3
290
291 /*
292 * Define the minimum table size.
293 */
294 #define MIN_TABLE_ORDER 0
295 #define MIN_TABLE_SIZE (1UL << MIN_TABLE_ORDER)
296
297 /*
298 * Minimum number of bucket nodes to touch per thread to parallelize grow/shrink.
299 */
300 #define MIN_PARTITION_PER_THREAD_ORDER 12
301 #define MIN_PARTITION_PER_THREAD (1UL << MIN_PARTITION_PER_THREAD_ORDER)
302
303 /*
304 * The removed flag needs to be updated atomically with the pointer.
305 * It indicates that no node must attach to the node scheduled for
306 * removal, and that node garbage collection must be performed.
307 * The bucket flag does not require to be updated atomically with the
308 * pointer, but it is added as a pointer low bit flag to save space.
309 * The "removal owner" flag is used to detect which of the "del"
310 * operation that has set the "removed flag" gets to return the removed
311 * node to its caller. Note that the replace operation does not need to
312 * iteract with the "removal owner" flag, because it validates that
313 * the "removed" flag is not set before performing its cmpxchg.
314 */
315 #define REMOVED_FLAG (1UL << 0)
316 #define BUCKET_FLAG (1UL << 1)
317 #define REMOVAL_OWNER_FLAG (1UL << 2)
318 #define FLAGS_MASK ((1UL << 3) - 1)
319
320 /* Value of the end pointer. Should not interact with flags. */
321 #define END_VALUE NULL
322
323 /*
324 * ht_items_count: Split-counters counting the number of node addition
325 * and removal in the table. Only used if the CDS_LFHT_ACCOUNTING flag
326 * is set at hash table creation.
327 *
328 * These are free-running counters, never reset to zero. They count the
329 * number of add/remove, and trigger every (1 << COUNT_COMMIT_ORDER)
330 * operations to update the global counter. We choose a power-of-2 value
331 * for the trigger to deal with 32 or 64-bit overflow of the counter.
332 */
333 struct ht_items_count {
334 unsigned long add, del;
335 } __attribute__((aligned(CAA_CACHE_LINE_SIZE)));
336
337 /*
338 * rcu_resize_work: Contains arguments passed to RCU worker thread
339 * responsible for performing lazy resize.
340 */
341 struct rcu_resize_work {
342 struct rcu_head head;
343 struct cds_lfht *ht;
344 };
345
346 /*
347 * partition_resize_work: Contains arguments passed to worker threads
348 * executing the hash table resize on partitions of the hash table
349 * assigned to each processor's worker thread.
350 */
351 struct partition_resize_work {
352 pthread_t thread_id;
353 struct cds_lfht *ht;
354 unsigned long i, start, len;
355 void (*fct)(struct cds_lfht *ht, unsigned long i,
356 unsigned long start, unsigned long len);
357 };
358
359 /*
360 * Algorithm to reverse bits in a word by lookup table, extended to
361 * 64-bit words.
362 * Source:
363 * http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
364 * Originally from Public Domain.
365 */
366
367 static const uint8_t BitReverseTable256[256] =
368 {
369 #define R2(n) (n), (n) + 2*64, (n) + 1*64, (n) + 3*64
370 #define R4(n) R2(n), R2((n) + 2*16), R2((n) + 1*16), R2((n) + 3*16)
371 #define R6(n) R4(n), R4((n) + 2*4 ), R4((n) + 1*4 ), R4((n) + 3*4 )
372 R6(0), R6(2), R6(1), R6(3)
373 };
374 #undef R2
375 #undef R4
376 #undef R6
377
378 static
379 uint8_t bit_reverse_u8(uint8_t v)
380 {
381 return BitReverseTable256[v];
382 }
383
384 static __attribute__((unused))
385 uint32_t bit_reverse_u32(uint32_t v)
386 {
387 return ((uint32_t) bit_reverse_u8(v) << 24) |
388 ((uint32_t) bit_reverse_u8(v >> 8) << 16) |
389 ((uint32_t) bit_reverse_u8(v >> 16) << 8) |
390 ((uint32_t) bit_reverse_u8(v >> 24));
391 }
392
393 static __attribute__((unused))
394 uint64_t bit_reverse_u64(uint64_t v)
395 {
396 return ((uint64_t) bit_reverse_u8(v) << 56) |
397 ((uint64_t) bit_reverse_u8(v >> 8) << 48) |
398 ((uint64_t) bit_reverse_u8(v >> 16) << 40) |
399 ((uint64_t) bit_reverse_u8(v >> 24) << 32) |
400 ((uint64_t) bit_reverse_u8(v >> 32) << 24) |
401 ((uint64_t) bit_reverse_u8(v >> 40) << 16) |
402 ((uint64_t) bit_reverse_u8(v >> 48) << 8) |
403 ((uint64_t) bit_reverse_u8(v >> 56));
404 }
405
406 static
407 unsigned long bit_reverse_ulong(unsigned long v)
408 {
409 #if (CAA_BITS_PER_LONG == 32)
410 return bit_reverse_u32(v);
411 #else
412 return bit_reverse_u64(v);
413 #endif
414 }
415
416 /*
417 * fls: returns the position of the most significant bit.
418 * Returns 0 if no bit is set, else returns the position of the most
419 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
420 */
421 #if defined(__i386) || defined(__x86_64)
422 static inline
423 unsigned int fls_u32(uint32_t x)
424 {
425 int r;
426
427 asm("bsrl %1,%0\n\t"
428 "jnz 1f\n\t"
429 "movl $-1,%0\n\t"
430 "1:\n\t"
431 : "=r" (r) : "rm" (x));
432 return r + 1;
433 }
434 #define HAS_FLS_U32
435 #endif
436
437 #if defined(__x86_64)
438 static inline
439 unsigned int fls_u64(uint64_t x)
440 {
441 long r;
442
443 asm("bsrq %1,%0\n\t"
444 "jnz 1f\n\t"
445 "movq $-1,%0\n\t"
446 "1:\n\t"
447 : "=r" (r) : "rm" (x));
448 return r + 1;
449 }
450 #define HAS_FLS_U64
451 #endif
452
453 #ifndef HAS_FLS_U64
454 static __attribute__((unused))
455 unsigned int fls_u64(uint64_t x)
456 {
457 unsigned int r = 64;
458
459 if (!x)
460 return 0;
461
462 if (!(x & 0xFFFFFFFF00000000ULL)) {
463 x <<= 32;
464 r -= 32;
465 }
466 if (!(x & 0xFFFF000000000000ULL)) {
467 x <<= 16;
468 r -= 16;
469 }
470 if (!(x & 0xFF00000000000000ULL)) {
471 x <<= 8;
472 r -= 8;
473 }
474 if (!(x & 0xF000000000000000ULL)) {
475 x <<= 4;
476 r -= 4;
477 }
478 if (!(x & 0xC000000000000000ULL)) {
479 x <<= 2;
480 r -= 2;
481 }
482 if (!(x & 0x8000000000000000ULL)) {
483 x <<= 1;
484 r -= 1;
485 }
486 return r;
487 }
488 #endif
489
490 #ifndef HAS_FLS_U32
491 static __attribute__((unused))
492 unsigned int fls_u32(uint32_t x)
493 {
494 unsigned int r = 32;
495
496 if (!x)
497 return 0;
498 if (!(x & 0xFFFF0000U)) {
499 x <<= 16;
500 r -= 16;
501 }
502 if (!(x & 0xFF000000U)) {
503 x <<= 8;
504 r -= 8;
505 }
506 if (!(x & 0xF0000000U)) {
507 x <<= 4;
508 r -= 4;
509 }
510 if (!(x & 0xC0000000U)) {
511 x <<= 2;
512 r -= 2;
513 }
514 if (!(x & 0x80000000U)) {
515 x <<= 1;
516 r -= 1;
517 }
518 return r;
519 }
520 #endif
521
522 unsigned int cds_lfht_fls_ulong(unsigned long x)
523 {
524 #if (CAA_BITS_PER_LONG == 32)
525 return fls_u32(x);
526 #else
527 return fls_u64(x);
528 #endif
529 }
530
531 /*
532 * Return the minimum order for which x <= (1UL << order).
533 * Return -1 if x is 0.
534 */
535 int cds_lfht_get_count_order_u32(uint32_t x)
536 {
537 if (!x)
538 return -1;
539
540 return fls_u32(x - 1);
541 }
542
543 /*
544 * Return the minimum order for which x <= (1UL << order).
545 * Return -1 if x is 0.
546 */
547 int cds_lfht_get_count_order_ulong(unsigned long x)
548 {
549 if (!x)
550 return -1;
551
552 return cds_lfht_fls_ulong(x - 1);
553 }
554
555 static
556 void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth);
557
558 static
559 void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
560 unsigned long count);
561
562 static long nr_cpus_mask = -1;
563 static long split_count_mask = -1;
564
565 #if defined(HAVE_SYSCONF)
566 static void ht_init_nr_cpus_mask(void)
567 {
568 long maxcpus;
569
570 maxcpus = sysconf(_SC_NPROCESSORS_CONF);
571 if (maxcpus <= 0) {
572 nr_cpus_mask = -2;
573 return;
574 }
575 /*
576 * round up number of CPUs to next power of two, so we
577 * can use & for modulo.
578 */
579 maxcpus = 1UL << cds_lfht_get_count_order_ulong(maxcpus);
580 nr_cpus_mask = maxcpus - 1;
581 }
582 #else /* #if defined(HAVE_SYSCONF) */
583 static void ht_init_nr_cpus_mask(void)
584 {
585 nr_cpus_mask = -2;
586 }
587 #endif /* #else #if defined(HAVE_SYSCONF) */
588
589 static
590 void alloc_split_items_count(struct cds_lfht *ht)
591 {
592 struct ht_items_count *count;
593
594 if (nr_cpus_mask == -1) {
595 ht_init_nr_cpus_mask();
596 if (nr_cpus_mask < 0)
597 split_count_mask = DEFAULT_SPLIT_COUNT_MASK;
598 else
599 split_count_mask = nr_cpus_mask;
600 }
601
602 assert(split_count_mask >= 0);
603
604 if (ht->flags & CDS_LFHT_ACCOUNTING) {
605 ht->split_count = calloc(split_count_mask + 1, sizeof(*count));
606 assert(ht->split_count);
607 } else {
608 ht->split_count = NULL;
609 }
610 }
611
612 static
613 void free_split_items_count(struct cds_lfht *ht)
614 {
615 poison_free(ht->split_count);
616 }
617
618 #if defined(HAVE_SCHED_GETCPU)
619 static
620 int ht_get_split_count_index(unsigned long hash)
621 {
622 int cpu;
623
624 assert(split_count_mask >= 0);
625 cpu = sched_getcpu();
626 if (caa_unlikely(cpu < 0))
627 return hash & split_count_mask;
628 else
629 return cpu & split_count_mask;
630 }
631 #else /* #if defined(HAVE_SCHED_GETCPU) */
632 static
633 int ht_get_split_count_index(unsigned long hash)
634 {
635 return hash & split_count_mask;
636 }
637 #endif /* #else #if defined(HAVE_SCHED_GETCPU) */
638
639 static
640 void ht_count_add(struct cds_lfht *ht, unsigned long size, unsigned long hash)
641 {
642 unsigned long split_count;
643 int index;
644 long count;
645
646 if (caa_unlikely(!ht->split_count))
647 return;
648 index = ht_get_split_count_index(hash);
649 split_count = uatomic_add_return(&ht->split_count[index].add, 1);
650 if (caa_likely(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))
651 return;
652 /* Only if number of add multiple of 1UL << COUNT_COMMIT_ORDER */
653
654 dbg_printf("add split count %lu\n", split_count);
655 count = uatomic_add_return(&ht->count,
656 1UL << COUNT_COMMIT_ORDER);
657 if (caa_likely(count & (count - 1)))
658 return;
659 /* Only if global count is power of 2 */
660
661 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) < size)
662 return;
663 dbg_printf("add set global %ld\n", count);
664 cds_lfht_resize_lazy_count(ht, size,
665 count >> (CHAIN_LEN_TARGET - 1));
666 }
667
668 static
669 void ht_count_del(struct cds_lfht *ht, unsigned long size, unsigned long hash)
670 {
671 unsigned long split_count;
672 int index;
673 long count;
674
675 if (caa_unlikely(!ht->split_count))
676 return;
677 index = ht_get_split_count_index(hash);
678 split_count = uatomic_add_return(&ht->split_count[index].del, 1);
679 if (caa_likely(split_count & ((1UL << COUNT_COMMIT_ORDER) - 1)))
680 return;
681 /* Only if number of deletes multiple of 1UL << COUNT_COMMIT_ORDER */
682
683 dbg_printf("del split count %lu\n", split_count);
684 count = uatomic_add_return(&ht->count,
685 -(1UL << COUNT_COMMIT_ORDER));
686 if (caa_likely(count & (count - 1)))
687 return;
688 /* Only if global count is power of 2 */
689
690 if ((count >> CHAIN_LEN_RESIZE_THRESHOLD) >= size)
691 return;
692 dbg_printf("del set global %ld\n", count);
693 /*
694 * Don't shrink table if the number of nodes is below a
695 * certain threshold.
696 */
697 if (count < (1UL << COUNT_COMMIT_ORDER) * (split_count_mask + 1))
698 return;
699 cds_lfht_resize_lazy_count(ht, size,
700 count >> (CHAIN_LEN_TARGET - 1));
701 }
702
703 static
704 void check_resize(struct cds_lfht *ht, unsigned long size, uint32_t chain_len)
705 {
706 unsigned long count;
707
708 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
709 return;
710 count = uatomic_read(&ht->count);
711 /*
712 * Use bucket-local length for small table expand and for
713 * environments lacking per-cpu data support.
714 */
715 if (count >= (1UL << COUNT_COMMIT_ORDER))
716 return;
717 if (chain_len > 100)
718 dbg_printf("WARNING: large chain length: %u.\n",
719 chain_len);
720 if (chain_len >= CHAIN_LEN_RESIZE_THRESHOLD)
721 cds_lfht_resize_lazy_grow(ht, size,
722 cds_lfht_get_count_order_u32(chain_len - (CHAIN_LEN_TARGET - 1)));
723 }
724
725 static
726 struct cds_lfht_node *clear_flag(struct cds_lfht_node *node)
727 {
728 return (struct cds_lfht_node *) (((unsigned long) node) & ~FLAGS_MASK);
729 }
730
731 static
732 int is_removed(struct cds_lfht_node *node)
733 {
734 return ((unsigned long) node) & REMOVED_FLAG;
735 }
736
737 static
738 int is_bucket(struct cds_lfht_node *node)
739 {
740 return ((unsigned long) node) & BUCKET_FLAG;
741 }
742
743 static
744 struct cds_lfht_node *flag_bucket(struct cds_lfht_node *node)
745 {
746 return (struct cds_lfht_node *) (((unsigned long) node) | BUCKET_FLAG);
747 }
748
749 static
750 int is_removal_owner(struct cds_lfht_node *node)
751 {
752 return ((unsigned long) node) & REMOVAL_OWNER_FLAG;
753 }
754
755 static
756 struct cds_lfht_node *flag_removal_owner(struct cds_lfht_node *node)
757 {
758 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVAL_OWNER_FLAG);
759 }
760
761 static
762 struct cds_lfht_node *flag_removed_or_removal_owner(struct cds_lfht_node *node)
763 {
764 return (struct cds_lfht_node *) (((unsigned long) node) | REMOVED_FLAG | REMOVAL_OWNER_FLAG);
765 }
766
767 static
768 struct cds_lfht_node *get_end(void)
769 {
770 return (struct cds_lfht_node *) END_VALUE;
771 }
772
773 static
774 int is_end(struct cds_lfht_node *node)
775 {
776 return clear_flag(node) == (struct cds_lfht_node *) END_VALUE;
777 }
778
779 static
780 unsigned long _uatomic_xchg_monotonic_increase(unsigned long *ptr,
781 unsigned long v)
782 {
783 unsigned long old1, old2;
784
785 old1 = uatomic_read(ptr);
786 do {
787 old2 = old1;
788 if (old2 >= v)
789 return old2;
790 } while ((old1 = uatomic_cmpxchg(ptr, old2, v)) != old2);
791 return old2;
792 }
793
794 static
795 void cds_lfht_alloc_bucket_table(struct cds_lfht *ht, unsigned long order)
796 {
797 return ht->mm->alloc_bucket_table(ht, order);
798 }
799
800 /*
801 * cds_lfht_free_bucket_table() should be called with decreasing order.
802 * When cds_lfht_free_bucket_table(0) is called, it means the whole
803 * lfht is destroyed.
804 */
805 static
806 void cds_lfht_free_bucket_table(struct cds_lfht *ht, unsigned long order)
807 {
808 return ht->mm->free_bucket_table(ht, order);
809 }
810
811 static inline
812 struct cds_lfht_node *bucket_at(struct cds_lfht *ht, unsigned long index)
813 {
814 return ht->bucket_at(ht, index);
815 }
816
817 static inline
818 struct cds_lfht_node *lookup_bucket(struct cds_lfht *ht, unsigned long size,
819 unsigned long hash)
820 {
821 assert(size > 0);
822 return bucket_at(ht, hash & (size - 1));
823 }
824
825 /*
826 * Remove all logically deleted nodes from a bucket up to a certain node key.
827 */
828 static
829 void _cds_lfht_gc_bucket(struct cds_lfht_node *bucket, struct cds_lfht_node *node)
830 {
831 struct cds_lfht_node *iter_prev, *iter, *next, *new_next;
832
833 assert(!is_bucket(bucket));
834 assert(!is_removed(bucket));
835 assert(!is_bucket(node));
836 assert(!is_removed(node));
837 for (;;) {
838 iter_prev = bucket;
839 /* We can always skip the bucket node initially */
840 iter = rcu_dereference(iter_prev->next);
841 assert(!is_removed(iter));
842 assert(iter_prev->reverse_hash <= node->reverse_hash);
843 /*
844 * We should never be called with bucket (start of chain)
845 * and logically removed node (end of path compression
846 * marker) being the actual same node. This would be a
847 * bug in the algorithm implementation.
848 */
849 assert(bucket != node);
850 for (;;) {
851 if (caa_unlikely(is_end(iter)))
852 return;
853 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
854 return;
855 next = rcu_dereference(clear_flag(iter)->next);
856 if (caa_likely(is_removed(next)))
857 break;
858 iter_prev = clear_flag(iter);
859 iter = next;
860 }
861 assert(!is_removed(iter));
862 if (is_bucket(iter))
863 new_next = flag_bucket(clear_flag(next));
864 else
865 new_next = clear_flag(next);
866 (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
867 }
868 }
869
870 static
871 int _cds_lfht_replace(struct cds_lfht *ht, unsigned long size,
872 struct cds_lfht_node *old_node,
873 struct cds_lfht_node *old_next,
874 struct cds_lfht_node *new_node)
875 {
876 struct cds_lfht_node *bucket, *ret_next;
877
878 if (!old_node) /* Return -ENOENT if asked to replace NULL node */
879 return -ENOENT;
880
881 assert(!is_removed(old_node));
882 assert(!is_bucket(old_node));
883 assert(!is_removed(new_node));
884 assert(!is_bucket(new_node));
885 assert(new_node != old_node);
886 for (;;) {
887 /* Insert after node to be replaced */
888 if (is_removed(old_next)) {
889 /*
890 * Too late, the old node has been removed under us
891 * between lookup and replace. Fail.
892 */
893 return -ENOENT;
894 }
895 assert(old_next == clear_flag(old_next));
896 assert(new_node != old_next);
897 /*
898 * REMOVAL_OWNER flag is _NEVER_ set before the REMOVED
899 * flag. It is either set atomically at the same time
900 * (replace) or after (del).
901 */
902 assert(!is_removal_owner(old_next));
903 new_node->next = old_next;
904 /*
905 * Here is the whole trick for lock-free replace: we add
906 * the replacement node _after_ the node we want to
907 * replace by atomically setting its next pointer at the
908 * same time we set its removal flag. Given that
909 * the lookups/get next use an iterator aware of the
910 * next pointer, they will either skip the old node due
911 * to the removal flag and see the new node, or use
912 * the old node, but will not see the new one.
913 * This is a replacement of a node with another node
914 * that has the same value: we are therefore not
915 * removing a value from the hash table. We set both the
916 * REMOVED and REMOVAL_OWNER flags atomically so we own
917 * the node after successful cmpxchg.
918 */
919 ret_next = uatomic_cmpxchg(&old_node->next,
920 old_next, flag_removed_or_removal_owner(new_node));
921 if (ret_next == old_next)
922 break; /* We performed the replacement. */
923 old_next = ret_next;
924 }
925
926 /*
927 * Ensure that the old node is not visible to readers anymore:
928 * lookup for the node, and remove it (along with any other
929 * logically removed node) if found.
930 */
931 bucket = lookup_bucket(ht, size, bit_reverse_ulong(old_node->reverse_hash));
932 _cds_lfht_gc_bucket(bucket, new_node);
933
934 assert(is_removed(CMM_LOAD_SHARED(old_node->next)));
935 return 0;
936 }
937
938 /*
939 * A non-NULL unique_ret pointer uses the "add unique" (or uniquify) add
940 * mode. A NULL unique_ret allows creation of duplicate keys.
941 */
942 static
943 void _cds_lfht_add(struct cds_lfht *ht,
944 unsigned long hash,
945 cds_lfht_match_fct match,
946 const void *key,
947 unsigned long size,
948 struct cds_lfht_node *node,
949 struct cds_lfht_iter *unique_ret,
950 int bucket_flag)
951 {
952 struct cds_lfht_node *iter_prev, *iter, *next, *new_node, *new_next,
953 *return_node;
954 struct cds_lfht_node *bucket;
955
956 assert(!is_bucket(node));
957 assert(!is_removed(node));
958 bucket = lookup_bucket(ht, size, hash);
959 for (;;) {
960 uint32_t chain_len = 0;
961
962 /*
963 * iter_prev points to the non-removed node prior to the
964 * insert location.
965 */
966 iter_prev = bucket;
967 /* We can always skip the bucket node initially */
968 iter = rcu_dereference(iter_prev->next);
969 assert(iter_prev->reverse_hash <= node->reverse_hash);
970 for (;;) {
971 if (caa_unlikely(is_end(iter)))
972 goto insert;
973 if (caa_likely(clear_flag(iter)->reverse_hash > node->reverse_hash))
974 goto insert;
975
976 /* bucket node is the first node of the identical-hash-value chain */
977 if (bucket_flag && clear_flag(iter)->reverse_hash == node->reverse_hash)
978 goto insert;
979
980 next = rcu_dereference(clear_flag(iter)->next);
981 if (caa_unlikely(is_removed(next)))
982 goto gc_node;
983
984 /* uniquely add */
985 if (unique_ret
986 && !is_bucket(next)
987 && clear_flag(iter)->reverse_hash == node->reverse_hash) {
988 struct cds_lfht_iter d_iter = { .node = node, .next = iter, };
989
990 /*
991 * uniquely adding inserts the node as the first
992 * node of the identical-hash-value node chain.
993 *
994 * This semantic ensures no duplicated keys
995 * should ever be observable in the table
996 * (including traversing the table node by
997 * node by forward iterations)
998 */
999 cds_lfht_next_duplicate(ht, match, key, &d_iter);
1000 if (!d_iter.node)
1001 goto insert;
1002
1003 *unique_ret = d_iter;
1004 return;
1005 }
1006
1007 /* Only account for identical reverse hash once */
1008 if (iter_prev->reverse_hash != clear_flag(iter)->reverse_hash
1009 && !is_bucket(next))
1010 check_resize(ht, size, ++chain_len);
1011 iter_prev = clear_flag(iter);
1012 iter = next;
1013 }
1014
1015 insert:
1016 assert(node != clear_flag(iter));
1017 assert(!is_removed(iter_prev));
1018 assert(!is_removed(iter));
1019 assert(iter_prev != node);
1020 if (!bucket_flag)
1021 node->next = clear_flag(iter);
1022 else
1023 node->next = flag_bucket(clear_flag(iter));
1024 if (is_bucket(iter))
1025 new_node = flag_bucket(node);
1026 else
1027 new_node = node;
1028 if (uatomic_cmpxchg(&iter_prev->next, iter,
1029 new_node) != iter) {
1030 continue; /* retry */
1031 } else {
1032 return_node = node;
1033 goto end;
1034 }
1035
1036 gc_node:
1037 assert(!is_removed(iter));
1038 if (is_bucket(iter))
1039 new_next = flag_bucket(clear_flag(next));
1040 else
1041 new_next = clear_flag(next);
1042 (void) uatomic_cmpxchg(&iter_prev->next, iter, new_next);
1043 /* retry */
1044 }
1045 end:
1046 if (unique_ret) {
1047 unique_ret->node = return_node;
1048 /* unique_ret->next left unset, never used. */
1049 }
1050 }
1051
1052 static
1053 int _cds_lfht_del(struct cds_lfht *ht, unsigned long size,
1054 struct cds_lfht_node *node)
1055 {
1056 struct cds_lfht_node *bucket, *next;
1057
1058 if (!node) /* Return -ENOENT if asked to delete NULL node */
1059 return -ENOENT;
1060
1061 /* logically delete the node */
1062 assert(!is_bucket(node));
1063 assert(!is_removed(node));
1064 assert(!is_removal_owner(node));
1065
1066 /*
1067 * We are first checking if the node had previously been
1068 * logically removed (this check is not atomic with setting the
1069 * logical removal flag). Return -ENOENT if the node had
1070 * previously been removed.
1071 */
1072 next = CMM_LOAD_SHARED(node->next); /* next is not dereferenced */
1073 if (caa_unlikely(is_removed(next)))
1074 return -ENOENT;
1075 assert(!is_bucket(next));
1076 /*
1077 * The del operation semantic guarantees a full memory barrier
1078 * before the uatomic_or atomic commit of the deletion flag.
1079 */
1080 cmm_smp_mb__before_uatomic_or();
1081 /*
1082 * We set the REMOVED_FLAG unconditionally. Note that there may
1083 * be more than one concurrent thread setting this flag.
1084 * Knowing which wins the race will be known after the garbage
1085 * collection phase, stay tuned!
1086 */
1087 uatomic_or(&node->next, REMOVED_FLAG);
1088 /* We performed the (logical) deletion. */
1089
1090 /*
1091 * Ensure that the node is not visible to readers anymore: lookup for
1092 * the node, and remove it (along with any other logically removed node)
1093 * if found.
1094 */
1095 bucket = lookup_bucket(ht, size, bit_reverse_ulong(node->reverse_hash));
1096 _cds_lfht_gc_bucket(bucket, node);
1097
1098 assert(is_removed(CMM_LOAD_SHARED(node->next)));
1099 /*
1100 * Last phase: atomically exchange node->next with a version
1101 * having "REMOVAL_OWNER_FLAG" set. If the returned node->next
1102 * pointer did _not_ have "REMOVAL_OWNER_FLAG" set, we now own
1103 * the node and win the removal race.
1104 * It is interesting to note that all "add" paths are forbidden
1105 * to change the next pointer starting from the point where the
1106 * REMOVED_FLAG is set, so here using a read, followed by a
1107 * xchg() suffice to guarantee that the xchg() will ever only
1108 * set the "REMOVAL_OWNER_FLAG" (or change nothing if the flag
1109 * was already set).
1110 */
1111 if (!is_removal_owner(uatomic_xchg(&node->next,
1112 flag_removal_owner(node->next))))
1113 return 0;
1114 else
1115 return -ENOENT;
1116 }
1117
1118 static
1119 void *partition_resize_thread(void *arg)
1120 {
1121 struct partition_resize_work *work = arg;
1122
1123 work->ht->flavor->register_thread();
1124 work->fct(work->ht, work->i, work->start, work->len);
1125 work->ht->flavor->unregister_thread();
1126 return NULL;
1127 }
1128
1129 static
1130 void partition_resize_helper(struct cds_lfht *ht, unsigned long i,
1131 unsigned long len,
1132 void (*fct)(struct cds_lfht *ht, unsigned long i,
1133 unsigned long start, unsigned long len))
1134 {
1135 unsigned long partition_len;
1136 struct partition_resize_work *work;
1137 int thread, ret;
1138 unsigned long nr_threads;
1139
1140 /*
1141 * Note: nr_cpus_mask + 1 is always power of 2.
1142 * We spawn just the number of threads we need to satisfy the minimum
1143 * partition size, up to the number of CPUs in the system.
1144 */
1145 if (nr_cpus_mask > 0) {
1146 nr_threads = min(nr_cpus_mask + 1,
1147 len >> MIN_PARTITION_PER_THREAD_ORDER);
1148 } else {
1149 nr_threads = 1;
1150 }
1151 partition_len = len >> cds_lfht_get_count_order_ulong(nr_threads);
1152 work = calloc(nr_threads, sizeof(*work));
1153 assert(work);
1154 for (thread = 0; thread < nr_threads; thread++) {
1155 work[thread].ht = ht;
1156 work[thread].i = i;
1157 work[thread].len = partition_len;
1158 work[thread].start = thread * partition_len;
1159 work[thread].fct = fct;
1160 ret = pthread_create(&(work[thread].thread_id), ht->resize_attr,
1161 partition_resize_thread, &work[thread]);
1162 assert(!ret);
1163 }
1164 for (thread = 0; thread < nr_threads; thread++) {
1165 ret = pthread_join(work[thread].thread_id, NULL);
1166 assert(!ret);
1167 }
1168 free(work);
1169 }
1170
1171 /*
1172 * Holding RCU read lock to protect _cds_lfht_add against memory
1173 * reclaim that could be performed by other call_rcu worker threads (ABA
1174 * problem).
1175 *
1176 * When we reach a certain length, we can split this population phase over
1177 * many worker threads, based on the number of CPUs available in the system.
1178 * This should therefore take care of not having the expand lagging behind too
1179 * many concurrent insertion threads by using the scheduler's ability to
1180 * schedule bucket node population fairly with insertions.
1181 */
1182 static
1183 void init_table_populate_partition(struct cds_lfht *ht, unsigned long i,
1184 unsigned long start, unsigned long len)
1185 {
1186 unsigned long j, size = 1UL << (i - 1);
1187
1188 assert(i > MIN_TABLE_ORDER);
1189 ht->flavor->read_lock();
1190 for (j = size + start; j < size + start + len; j++) {
1191 struct cds_lfht_node *new_node = bucket_at(ht, j);
1192
1193 assert(j >= size && j < (size << 1));
1194 dbg_printf("init populate: order %lu index %lu hash %lu\n",
1195 i, j, j);
1196 new_node->reverse_hash = bit_reverse_ulong(j);
1197 _cds_lfht_add(ht, j, NULL, NULL, size, new_node, NULL, 1);
1198 }
1199 ht->flavor->read_unlock();
1200 }
1201
1202 static
1203 void init_table_populate(struct cds_lfht *ht, unsigned long i,
1204 unsigned long len)
1205 {
1206 assert(nr_cpus_mask != -1);
1207 if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
1208 ht->flavor->thread_online();
1209 init_table_populate_partition(ht, i, 0, len);
1210 ht->flavor->thread_offline();
1211 return;
1212 }
1213 partition_resize_helper(ht, i, len, init_table_populate_partition);
1214 }
1215
1216 static
1217 void init_table(struct cds_lfht *ht,
1218 unsigned long first_order, unsigned long last_order)
1219 {
1220 unsigned long i;
1221
1222 dbg_printf("init table: first_order %lu last_order %lu\n",
1223 first_order, last_order);
1224 assert(first_order > MIN_TABLE_ORDER);
1225 for (i = first_order; i <= last_order; i++) {
1226 unsigned long len;
1227
1228 len = 1UL << (i - 1);
1229 dbg_printf("init order %lu len: %lu\n", i, len);
1230
1231 /* Stop expand if the resize target changes under us */
1232 if (CMM_LOAD_SHARED(ht->resize_target) < (1UL << i))
1233 break;
1234
1235 cds_lfht_alloc_bucket_table(ht, i);
1236
1237 /*
1238 * Set all bucket nodes reverse hash values for a level and
1239 * link all bucket nodes into the table.
1240 */
1241 init_table_populate(ht, i, len);
1242
1243 /*
1244 * Update table size.
1245 */
1246 cmm_smp_wmb(); /* populate data before RCU size */
1247 CMM_STORE_SHARED(ht->size, 1UL << i);
1248
1249 dbg_printf("init new size: %lu\n", 1UL << i);
1250 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1251 break;
1252 }
1253 }
1254
1255 /*
1256 * Holding RCU read lock to protect _cds_lfht_remove against memory
1257 * reclaim that could be performed by other call_rcu worker threads (ABA
1258 * problem).
1259 * For a single level, we logically remove and garbage collect each node.
1260 *
1261 * As a design choice, we perform logical removal and garbage collection on a
1262 * node-per-node basis to simplify this algorithm. We also assume keeping good
1263 * cache locality of the operation would overweight possible performance gain
1264 * that could be achieved by batching garbage collection for multiple levels.
1265 * However, this would have to be justified by benchmarks.
1266 *
1267 * Concurrent removal and add operations are helping us perform garbage
1268 * collection of logically removed nodes. We guarantee that all logically
1269 * removed nodes have been garbage-collected (unlinked) before call_rcu is
1270 * invoked to free a hole level of bucket nodes (after a grace period).
1271 *
1272 * Logical removal and garbage collection can therefore be done in batch
1273 * or on a node-per-node basis, as long as the guarantee above holds.
1274 *
1275 * When we reach a certain length, we can split this removal over many worker
1276 * threads, based on the number of CPUs available in the system. This should
1277 * take care of not letting resize process lag behind too many concurrent
1278 * updater threads actively inserting into the hash table.
1279 */
1280 static
1281 void remove_table_partition(struct cds_lfht *ht, unsigned long i,
1282 unsigned long start, unsigned long len)
1283 {
1284 unsigned long j, size = 1UL << (i - 1);
1285
1286 assert(i > MIN_TABLE_ORDER);
1287 ht->flavor->read_lock();
1288 for (j = size + start; j < size + start + len; j++) {
1289 struct cds_lfht_node *fini_bucket = bucket_at(ht, j);
1290 struct cds_lfht_node *parent_bucket = bucket_at(ht, j - size);
1291
1292 assert(j >= size && j < (size << 1));
1293 dbg_printf("remove entry: order %lu index %lu hash %lu\n",
1294 i, j, j);
1295 /* Set the REMOVED_FLAG to freeze the ->next for gc */
1296 uatomic_or(&fini_bucket->next, REMOVED_FLAG);
1297 _cds_lfht_gc_bucket(parent_bucket, fini_bucket);
1298 }
1299 ht->flavor->read_unlock();
1300 }
1301
1302 static
1303 void remove_table(struct cds_lfht *ht, unsigned long i, unsigned long len)
1304 {
1305
1306 assert(nr_cpus_mask != -1);
1307 if (nr_cpus_mask < 0 || len < 2 * MIN_PARTITION_PER_THREAD) {
1308 ht->flavor->thread_online();
1309 remove_table_partition(ht, i, 0, len);
1310 ht->flavor->thread_offline();
1311 return;
1312 }
1313 partition_resize_helper(ht, i, len, remove_table_partition);
1314 }
1315
1316 /*
1317 * fini_table() is never called for first_order == 0, which is why
1318 * free_by_rcu_order == 0 can be used as criterion to know if free must
1319 * be called.
1320 */
1321 static
1322 void fini_table(struct cds_lfht *ht,
1323 unsigned long first_order, unsigned long last_order)
1324 {
1325 long i;
1326 unsigned long free_by_rcu_order = 0;
1327
1328 dbg_printf("fini table: first_order %lu last_order %lu\n",
1329 first_order, last_order);
1330 assert(first_order > MIN_TABLE_ORDER);
1331 for (i = last_order; i >= first_order; i--) {
1332 unsigned long len;
1333
1334 len = 1UL << (i - 1);
1335 dbg_printf("fini order %lu len: %lu\n", i, len);
1336
1337 /* Stop shrink if the resize target changes under us */
1338 if (CMM_LOAD_SHARED(ht->resize_target) > (1UL << (i - 1)))
1339 break;
1340
1341 cmm_smp_wmb(); /* populate data before RCU size */
1342 CMM_STORE_SHARED(ht->size, 1UL << (i - 1));
1343
1344 /*
1345 * We need to wait for all add operations to reach Q.S. (and
1346 * thus use the new table for lookups) before we can start
1347 * releasing the old bucket nodes. Otherwise their lookup will
1348 * return a logically removed node as insert position.
1349 */
1350 ht->flavor->update_synchronize_rcu();
1351 if (free_by_rcu_order)
1352 cds_lfht_free_bucket_table(ht, free_by_rcu_order);
1353
1354 /*
1355 * Set "removed" flag in bucket nodes about to be removed.
1356 * Unlink all now-logically-removed bucket node pointers.
1357 * Concurrent add/remove operation are helping us doing
1358 * the gc.
1359 */
1360 remove_table(ht, i, len);
1361
1362 free_by_rcu_order = i;
1363
1364 dbg_printf("fini new size: %lu\n", 1UL << i);
1365 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1366 break;
1367 }
1368
1369 if (free_by_rcu_order) {
1370 ht->flavor->update_synchronize_rcu();
1371 cds_lfht_free_bucket_table(ht, free_by_rcu_order);
1372 }
1373 }
1374
1375 static
1376 void cds_lfht_create_bucket(struct cds_lfht *ht, unsigned long size)
1377 {
1378 struct cds_lfht_node *prev, *node;
1379 unsigned long order, len, i;
1380
1381 cds_lfht_alloc_bucket_table(ht, 0);
1382
1383 dbg_printf("create bucket: order 0 index 0 hash 0\n");
1384 node = bucket_at(ht, 0);
1385 node->next = flag_bucket(get_end());
1386 node->reverse_hash = 0;
1387
1388 for (order = 1; order < cds_lfht_get_count_order_ulong(size) + 1; order++) {
1389 len = 1UL << (order - 1);
1390 cds_lfht_alloc_bucket_table(ht, order);
1391
1392 for (i = 0; i < len; i++) {
1393 /*
1394 * Now, we are trying to init the node with the
1395 * hash=(len+i) (which is also a bucket with the
1396 * index=(len+i)) and insert it into the hash table,
1397 * so this node has to be inserted after the bucket
1398 * with the index=(len+i)&(len-1)=i. And because there
1399 * is no other non-bucket node nor bucket node with
1400 * larger index/hash inserted, so the bucket node
1401 * being inserted should be inserted directly linked
1402 * after the bucket node with index=i.
1403 */
1404 prev = bucket_at(ht, i);
1405 node = bucket_at(ht, len + i);
1406
1407 dbg_printf("create bucket: order %lu index %lu hash %lu\n",
1408 order, len + i, len + i);
1409 node->reverse_hash = bit_reverse_ulong(len + i);
1410
1411 /* insert after prev */
1412 assert(is_bucket(prev->next));
1413 node->next = prev->next;
1414 prev->next = flag_bucket(node);
1415 }
1416 }
1417 }
1418
1419 struct cds_lfht *_cds_lfht_new(unsigned long init_size,
1420 unsigned long min_nr_alloc_buckets,
1421 unsigned long max_nr_buckets,
1422 int flags,
1423 const struct cds_lfht_mm_type *mm,
1424 const struct rcu_flavor_struct *flavor,
1425 pthread_attr_t *attr)
1426 {
1427 struct cds_lfht *ht;
1428 unsigned long order;
1429
1430 /* min_nr_alloc_buckets must be power of two */
1431 if (!min_nr_alloc_buckets || (min_nr_alloc_buckets & (min_nr_alloc_buckets - 1)))
1432 return NULL;
1433
1434 /* init_size must be power of two */
1435 if (!init_size || (init_size & (init_size - 1)))
1436 return NULL;
1437
1438 /*
1439 * Memory management plugin default.
1440 */
1441 if (!mm) {
1442 if (CAA_BITS_PER_LONG > 32
1443 && max_nr_buckets
1444 && max_nr_buckets <= (1ULL << 32)) {
1445 /*
1446 * For 64-bit architectures, with max number of
1447 * buckets small enough not to use the entire
1448 * 64-bit memory mapping space (and allowing a
1449 * fair number of hash table instances), use the
1450 * mmap allocator, which is faster than the
1451 * order allocator.
1452 */
1453 mm = &cds_lfht_mm_mmap;
1454 } else {
1455 /*
1456 * The fallback is to use the order allocator.
1457 */
1458 mm = &cds_lfht_mm_order;
1459 }
1460 }
1461
1462 /* max_nr_buckets == 0 for order based mm means infinite */
1463 if (mm == &cds_lfht_mm_order && !max_nr_buckets)
1464 max_nr_buckets = 1UL << (MAX_TABLE_ORDER - 1);
1465
1466 /* max_nr_buckets must be power of two */
1467 if (!max_nr_buckets || (max_nr_buckets & (max_nr_buckets - 1)))
1468 return NULL;
1469
1470 min_nr_alloc_buckets = max(min_nr_alloc_buckets, MIN_TABLE_SIZE);
1471 init_size = max(init_size, MIN_TABLE_SIZE);
1472 max_nr_buckets = max(max_nr_buckets, min_nr_alloc_buckets);
1473 init_size = min(init_size, max_nr_buckets);
1474
1475 ht = mm->alloc_cds_lfht(min_nr_alloc_buckets, max_nr_buckets);
1476 assert(ht);
1477 assert(ht->mm == mm);
1478 assert(ht->bucket_at == mm->bucket_at);
1479
1480 ht->flags = flags;
1481 ht->flavor = flavor;
1482 ht->resize_attr = attr;
1483 alloc_split_items_count(ht);
1484 /* this mutex should not nest in read-side C.S. */
1485 pthread_mutex_init(&ht->resize_mutex, NULL);
1486 order = cds_lfht_get_count_order_ulong(init_size);
1487 ht->resize_target = 1UL << order;
1488 cds_lfht_create_bucket(ht, 1UL << order);
1489 ht->size = 1UL << order;
1490 return ht;
1491 }
1492
1493 void cds_lfht_lookup(struct cds_lfht *ht, unsigned long hash,
1494 cds_lfht_match_fct match, const void *key,
1495 struct cds_lfht_iter *iter)
1496 {
1497 struct cds_lfht_node *node, *next, *bucket;
1498 unsigned long reverse_hash, size;
1499
1500 reverse_hash = bit_reverse_ulong(hash);
1501
1502 size = rcu_dereference(ht->size);
1503 bucket = lookup_bucket(ht, size, hash);
1504 /* We can always skip the bucket node initially */
1505 node = rcu_dereference(bucket->next);
1506 node = clear_flag(node);
1507 for (;;) {
1508 if (caa_unlikely(is_end(node))) {
1509 node = next = NULL;
1510 break;
1511 }
1512 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
1513 node = next = NULL;
1514 break;
1515 }
1516 next = rcu_dereference(node->next);
1517 assert(node == clear_flag(node));
1518 if (caa_likely(!is_removed(next))
1519 && !is_bucket(next)
1520 && node->reverse_hash == reverse_hash
1521 && caa_likely(match(node, key))) {
1522 break;
1523 }
1524 node = clear_flag(next);
1525 }
1526 assert(!node || !is_bucket(CMM_LOAD_SHARED(node->next)));
1527 iter->node = node;
1528 iter->next = next;
1529 }
1530
1531 void cds_lfht_next_duplicate(struct cds_lfht *ht, cds_lfht_match_fct match,
1532 const void *key, struct cds_lfht_iter *iter)
1533 {
1534 struct cds_lfht_node *node, *next;
1535 unsigned long reverse_hash;
1536
1537 node = iter->node;
1538 reverse_hash = node->reverse_hash;
1539 next = iter->next;
1540 node = clear_flag(next);
1541
1542 for (;;) {
1543 if (caa_unlikely(is_end(node))) {
1544 node = next = NULL;
1545 break;
1546 }
1547 if (caa_unlikely(node->reverse_hash > reverse_hash)) {
1548 node = next = NULL;
1549 break;
1550 }
1551 next = rcu_dereference(node->next);
1552 if (caa_likely(!is_removed(next))
1553 && !is_bucket(next)
1554 && caa_likely(match(node, key))) {
1555 break;
1556 }
1557 node = clear_flag(next);
1558 }
1559 assert(!node || !is_bucket(CMM_LOAD_SHARED(node->next)));
1560 iter->node = node;
1561 iter->next = next;
1562 }
1563
1564 void cds_lfht_next(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1565 {
1566 struct cds_lfht_node *node, *next;
1567
1568 node = clear_flag(iter->next);
1569 for (;;) {
1570 if (caa_unlikely(is_end(node))) {
1571 node = next = NULL;
1572 break;
1573 }
1574 next = rcu_dereference(node->next);
1575 if (caa_likely(!is_removed(next))
1576 && !is_bucket(next)) {
1577 break;
1578 }
1579 node = clear_flag(next);
1580 }
1581 assert(!node || !is_bucket(CMM_LOAD_SHARED(node->next)));
1582 iter->node = node;
1583 iter->next = next;
1584 }
1585
1586 void cds_lfht_first(struct cds_lfht *ht, struct cds_lfht_iter *iter)
1587 {
1588 /*
1589 * Get next after first bucket node. The first bucket node is the
1590 * first node of the linked list.
1591 */
1592 iter->next = bucket_at(ht, 0)->next;
1593 cds_lfht_next(ht, iter);
1594 }
1595
1596 void cds_lfht_add(struct cds_lfht *ht, unsigned long hash,
1597 struct cds_lfht_node *node)
1598 {
1599 unsigned long size;
1600
1601 node->reverse_hash = bit_reverse_ulong(hash);
1602 size = rcu_dereference(ht->size);
1603 _cds_lfht_add(ht, hash, NULL, NULL, size, node, NULL, 0);
1604 ht_count_add(ht, size, hash);
1605 }
1606
1607 struct cds_lfht_node *cds_lfht_add_unique(struct cds_lfht *ht,
1608 unsigned long hash,
1609 cds_lfht_match_fct match,
1610 const void *key,
1611 struct cds_lfht_node *node)
1612 {
1613 unsigned long size;
1614 struct cds_lfht_iter iter;
1615
1616 node->reverse_hash = bit_reverse_ulong(hash);
1617 size = rcu_dereference(ht->size);
1618 _cds_lfht_add(ht, hash, match, key, size, node, &iter, 0);
1619 if (iter.node == node)
1620 ht_count_add(ht, size, hash);
1621 return iter.node;
1622 }
1623
1624 struct cds_lfht_node *cds_lfht_add_replace(struct cds_lfht *ht,
1625 unsigned long hash,
1626 cds_lfht_match_fct match,
1627 const void *key,
1628 struct cds_lfht_node *node)
1629 {
1630 unsigned long size;
1631 struct cds_lfht_iter iter;
1632
1633 node->reverse_hash = bit_reverse_ulong(hash);
1634 size = rcu_dereference(ht->size);
1635 for (;;) {
1636 _cds_lfht_add(ht, hash, match, key, size, node, &iter, 0);
1637 if (iter.node == node) {
1638 ht_count_add(ht, size, hash);
1639 return NULL;
1640 }
1641
1642 if (!_cds_lfht_replace(ht, size, iter.node, iter.next, node))
1643 return iter.node;
1644 }
1645 }
1646
1647 int cds_lfht_replace(struct cds_lfht *ht,
1648 struct cds_lfht_iter *old_iter,
1649 unsigned long hash,
1650 cds_lfht_match_fct match,
1651 const void *key,
1652 struct cds_lfht_node *new_node)
1653 {
1654 unsigned long size;
1655
1656 new_node->reverse_hash = bit_reverse_ulong(hash);
1657 if (!old_iter->node)
1658 return -ENOENT;
1659 if (caa_unlikely(old_iter->node->reverse_hash != new_node->reverse_hash))
1660 return -EINVAL;
1661 if (caa_unlikely(!match(old_iter->node, key)))
1662 return -EINVAL;
1663 size = rcu_dereference(ht->size);
1664 return _cds_lfht_replace(ht, size, old_iter->node, old_iter->next,
1665 new_node);
1666 }
1667
1668 int cds_lfht_del(struct cds_lfht *ht, struct cds_lfht_node *node)
1669 {
1670 unsigned long size, hash;
1671 int ret;
1672
1673 size = rcu_dereference(ht->size);
1674 ret = _cds_lfht_del(ht, size, node);
1675 if (!ret) {
1676 hash = bit_reverse_ulong(node->reverse_hash);
1677 ht_count_del(ht, size, hash);
1678 }
1679 return ret;
1680 }
1681
1682 int cds_lfht_is_node_deleted(struct cds_lfht_node *node)
1683 {
1684 return is_removed(CMM_LOAD_SHARED(node->next));
1685 }
1686
1687 static
1688 int cds_lfht_delete_bucket(struct cds_lfht *ht)
1689 {
1690 struct cds_lfht_node *node;
1691 unsigned long order, i, size;
1692
1693 /* Check that the table is empty */
1694 node = bucket_at(ht, 0);
1695 do {
1696 node = clear_flag(node)->next;
1697 if (!is_bucket(node))
1698 return -EPERM;
1699 assert(!is_removed(node));
1700 } while (!is_end(node));
1701 /*
1702 * size accessed without rcu_dereference because hash table is
1703 * being destroyed.
1704 */
1705 size = ht->size;
1706 /* Internal sanity check: all nodes left should be buckets */
1707 for (i = 0; i < size; i++) {
1708 node = bucket_at(ht, i);
1709 dbg_printf("delete bucket: index %lu expected hash %lu hash %lu\n",
1710 i, i, bit_reverse_ulong(node->reverse_hash));
1711 assert(is_bucket(node->next));
1712 }
1713
1714 for (order = cds_lfht_get_count_order_ulong(size); (long)order >= 0; order--)
1715 cds_lfht_free_bucket_table(ht, order);
1716
1717 return 0;
1718 }
1719
1720 /*
1721 * Should only be called when no more concurrent readers nor writers can
1722 * possibly access the table.
1723 */
1724 int cds_lfht_destroy(struct cds_lfht *ht, pthread_attr_t **attr)
1725 {
1726 int ret;
1727
1728 /* Wait for in-flight resize operations to complete */
1729 _CMM_STORE_SHARED(ht->in_progress_destroy, 1);
1730 cmm_smp_mb(); /* Store destroy before load resize */
1731 while (uatomic_read(&ht->in_progress_resize))
1732 poll(NULL, 0, 100); /* wait for 100ms */
1733 ret = cds_lfht_delete_bucket(ht);
1734 if (ret)
1735 return ret;
1736 free_split_items_count(ht);
1737 if (attr)
1738 *attr = ht->resize_attr;
1739 poison_free(ht);
1740 return ret;
1741 }
1742
1743 void cds_lfht_count_nodes(struct cds_lfht *ht,
1744 long *approx_before,
1745 unsigned long *count,
1746 long *approx_after)
1747 {
1748 struct cds_lfht_node *node, *next;
1749 unsigned long nr_bucket = 0, nr_removed = 0;
1750
1751 *approx_before = 0;
1752 if (ht->split_count) {
1753 int i;
1754
1755 for (i = 0; i < split_count_mask + 1; i++) {
1756 *approx_before += uatomic_read(&ht->split_count[i].add);
1757 *approx_before -= uatomic_read(&ht->split_count[i].del);
1758 }
1759 }
1760
1761 *count = 0;
1762
1763 /* Count non-bucket nodes in the table */
1764 node = bucket_at(ht, 0);
1765 do {
1766 next = rcu_dereference(node->next);
1767 if (is_removed(next)) {
1768 if (!is_bucket(next))
1769 (nr_removed)++;
1770 else
1771 (nr_bucket)++;
1772 } else if (!is_bucket(next))
1773 (*count)++;
1774 else
1775 (nr_bucket)++;
1776 node = clear_flag(next);
1777 } while (!is_end(node));
1778 dbg_printf("number of logically removed nodes: %lu\n", nr_removed);
1779 dbg_printf("number of bucket nodes: %lu\n", nr_bucket);
1780 *approx_after = 0;
1781 if (ht->split_count) {
1782 int i;
1783
1784 for (i = 0; i < split_count_mask + 1; i++) {
1785 *approx_after += uatomic_read(&ht->split_count[i].add);
1786 *approx_after -= uatomic_read(&ht->split_count[i].del);
1787 }
1788 }
1789 }
1790
1791 /* called with resize mutex held */
1792 static
1793 void _do_cds_lfht_grow(struct cds_lfht *ht,
1794 unsigned long old_size, unsigned long new_size)
1795 {
1796 unsigned long old_order, new_order;
1797
1798 old_order = cds_lfht_get_count_order_ulong(old_size);
1799 new_order = cds_lfht_get_count_order_ulong(new_size);
1800 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1801 old_size, old_order, new_size, new_order);
1802 assert(new_size > old_size);
1803 init_table(ht, old_order + 1, new_order);
1804 }
1805
1806 /* called with resize mutex held */
1807 static
1808 void _do_cds_lfht_shrink(struct cds_lfht *ht,
1809 unsigned long old_size, unsigned long new_size)
1810 {
1811 unsigned long old_order, new_order;
1812
1813 new_size = max(new_size, MIN_TABLE_SIZE);
1814 old_order = cds_lfht_get_count_order_ulong(old_size);
1815 new_order = cds_lfht_get_count_order_ulong(new_size);
1816 dbg_printf("resize from %lu (order %lu) to %lu (order %lu) buckets\n",
1817 old_size, old_order, new_size, new_order);
1818 assert(new_size < old_size);
1819
1820 /* Remove and unlink all bucket nodes to remove. */
1821 fini_table(ht, new_order + 1, old_order);
1822 }
1823
1824
1825 /* called with resize mutex held */
1826 static
1827 void _do_cds_lfht_resize(struct cds_lfht *ht)
1828 {
1829 unsigned long new_size, old_size;
1830
1831 /*
1832 * Resize table, re-do if the target size has changed under us.
1833 */
1834 do {
1835 assert(uatomic_read(&ht->in_progress_resize));
1836 if (CMM_LOAD_SHARED(ht->in_progress_destroy))
1837 break;
1838 ht->resize_initiated = 1;
1839 old_size = ht->size;
1840 new_size = CMM_LOAD_SHARED(ht->resize_target);
1841 if (old_size < new_size)
1842 _do_cds_lfht_grow(ht, old_size, new_size);
1843 else if (old_size > new_size)
1844 _do_cds_lfht_shrink(ht, old_size, new_size);
1845 ht->resize_initiated = 0;
1846 /* write resize_initiated before read resize_target */
1847 cmm_smp_mb();
1848 } while (ht->size != CMM_LOAD_SHARED(ht->resize_target));
1849 }
1850
1851 static
1852 unsigned long resize_target_grow(struct cds_lfht *ht, unsigned long new_size)
1853 {
1854 return _uatomic_xchg_monotonic_increase(&ht->resize_target, new_size);
1855 }
1856
1857 static
1858 void resize_target_update_count(struct cds_lfht *ht,
1859 unsigned long count)
1860 {
1861 count = max(count, MIN_TABLE_SIZE);
1862 count = min(count, ht->max_nr_buckets);
1863 uatomic_set(&ht->resize_target, count);
1864 }
1865
1866 void cds_lfht_resize(struct cds_lfht *ht, unsigned long new_size)
1867 {
1868 resize_target_update_count(ht, new_size);
1869 CMM_STORE_SHARED(ht->resize_initiated, 1);
1870 ht->flavor->thread_offline();
1871 pthread_mutex_lock(&ht->resize_mutex);
1872 _do_cds_lfht_resize(ht);
1873 pthread_mutex_unlock(&ht->resize_mutex);
1874 ht->flavor->thread_online();
1875 }
1876
1877 static
1878 void do_resize_cb(struct rcu_head *head)
1879 {
1880 struct rcu_resize_work *work =
1881 caa_container_of(head, struct rcu_resize_work, head);
1882 struct cds_lfht *ht = work->ht;
1883
1884 ht->flavor->thread_offline();
1885 pthread_mutex_lock(&ht->resize_mutex);
1886 _do_cds_lfht_resize(ht);
1887 pthread_mutex_unlock(&ht->resize_mutex);
1888 ht->flavor->thread_online();
1889 poison_free(work);
1890 cmm_smp_mb(); /* finish resize before decrement */
1891 uatomic_dec(&ht->in_progress_resize);
1892 }
1893
1894 static
1895 void __cds_lfht_resize_lazy_launch(struct cds_lfht *ht)
1896 {
1897 struct rcu_resize_work *work;
1898
1899 /* Store resize_target before read resize_initiated */
1900 cmm_smp_mb();
1901 if (!CMM_LOAD_SHARED(ht->resize_initiated)) {
1902 uatomic_inc(&ht->in_progress_resize);
1903 cmm_smp_mb(); /* increment resize count before load destroy */
1904 if (CMM_LOAD_SHARED(ht->in_progress_destroy)) {
1905 uatomic_dec(&ht->in_progress_resize);
1906 return;
1907 }
1908 work = malloc(sizeof(*work));
1909 if (work == NULL) {
1910 dbg_printf("error allocating resize work, bailing out\n");
1911 uatomic_dec(&ht->in_progress_resize);
1912 return;
1913 }
1914 work->ht = ht;
1915 ht->flavor->update_call_rcu(&work->head, do_resize_cb);
1916 CMM_STORE_SHARED(ht->resize_initiated, 1);
1917 }
1918 }
1919
1920 static
1921 void cds_lfht_resize_lazy_grow(struct cds_lfht *ht, unsigned long size, int growth)
1922 {
1923 unsigned long target_size = size << growth;
1924
1925 target_size = min(target_size, ht->max_nr_buckets);
1926 if (resize_target_grow(ht, target_size) >= target_size)
1927 return;
1928
1929 __cds_lfht_resize_lazy_launch(ht);
1930 }
1931
1932 /*
1933 * We favor grow operations over shrink. A shrink operation never occurs
1934 * if a grow operation is queued for lazy execution. A grow operation
1935 * cancels any pending shrink lazy execution.
1936 */
1937 static
1938 void cds_lfht_resize_lazy_count(struct cds_lfht *ht, unsigned long size,
1939 unsigned long count)
1940 {
1941 if (!(ht->flags & CDS_LFHT_AUTO_RESIZE))
1942 return;
1943 count = max(count, MIN_TABLE_SIZE);
1944 count = min(count, ht->max_nr_buckets);
1945 if (count == size)
1946 return; /* Already the right size, no resize needed */
1947 if (count > size) { /* lazy grow */
1948 if (resize_target_grow(ht, count) >= count)
1949 return;
1950 } else { /* lazy shrink */
1951 for (;;) {
1952 unsigned long s;
1953
1954 s = uatomic_cmpxchg(&ht->resize_target, size, count);
1955 if (s == size)
1956 break; /* no resize needed */
1957 if (s > size)
1958 return; /* growing is/(was just) in progress */
1959 if (s <= count)
1960 return; /* some other thread do shrink */
1961 size = s;
1962 }
1963 }
1964 __cds_lfht_resize_lazy_launch(ht);
1965 }
This page took 0.066036 seconds and 5 git commands to generate.