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