1 // SPDX-FileCopyrightText: 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
2 // SPDX-FileCopyrightText: 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
4 // SPDX-License-Identifier: LGPL-2.1-or-later
6 #ifndef _URCU_RCULFHASH_H
7 #define _URCU_RCULFHASH_H
10 * Userspace RCU library - Lock-Free RCU Hash Table
12 * For use with URCU_API_MAP (API mapping of liburcu), include this file
13 * _after_ including your URCU flavor.
16 #include <urcu/config.h>
19 #include <urcu/compiler.h>
28 * cds_lfht_node: Contains the next pointers and reverse-hash
29 * value required for lookup and traversal of the hash table.
31 * struct cds_lfht_node should be aligned on 8-bytes boundaries because
32 * the three lower bits are used as flags. It is worth noting that the
33 * information contained within these three bits could be represented on
34 * two bits by re-using the same bit for REMOVAL_OWNER_FLAG and
35 * BUCKET_FLAG. This can be done if we ensure that no iterator nor
36 * updater check the BUCKET_FLAG after it detects that the REMOVED_FLAG
37 * is set. Given the minimum size of struct cds_lfht_node is 8 bytes on
38 * 32-bit architectures, we choose to go for simplicity and reserve
41 * struct cds_lfht_node can be embedded into a structure (as a field).
42 * caa_container_of() can be used to get the structure from the struct
43 * cds_lfht_node after a lookup.
45 * The structure which embeds it typically holds the key (or key-value
46 * pair) of the object. The caller code is responsible for calculation
47 * of the hash value for cds_lfht APIs.
49 struct cds_lfht_node
{
50 struct cds_lfht_node
*next
; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */
51 unsigned long reverse_hash
;
52 } __attribute__((aligned(8)));
54 /* cds_lfht_iter: Used to track state while traversing a hash chain. */
55 struct cds_lfht_iter
{
56 struct cds_lfht_node
*node
, *next
;
58 * For debugging purposes, build both API users and rculfhash
59 * library with CDS_LFHT_ITER_DEBUG defined. This enables extra
60 * consistency checks for calls to a cds_lfht_next() or
61 * cds_lfht_next_duplicate() after the iterator has been
62 * re-purposed to iterate on a different hash table. This is a
63 * common programming mistake when performing hash table lookup
64 * nested in a hash table traversal.
66 #ifdef CONFIG_CDS_LFHT_ITER_DEBUG
67 struct cds_lfht
*lfht
;
72 * cds_lfht_alloc: Callbacks if we want to use custom memory allocator.
74 struct cds_lfht_alloc
{
75 void *(*malloc
)(void *state
, size_t size
);
76 void *(*calloc
)(void *state
, size_t nmemb
, size_t size
);
77 void *(*realloc
)(void *state
, void *ptr
, size_t size
);
78 void *(*aligned_alloc
)(void *state
, size_t alignment
, size_t size
);
79 void (*free
)(void *state
, void *ptr
);
84 struct cds_lfht_node
*cds_lfht_iter_get_node(const struct cds_lfht_iter
*iter
)
89 struct rcu_flavor_struct
;
93 * Ensure reader and writer threads are registered as urcu readers.
96 typedef int (*cds_lfht_match_fct
)(struct cds_lfht_node
*node
, const void *key
);
99 * cds_lfht_node_init - initialize a hash table node
100 * @node: the node to initialize.
102 * This function is kept to be eventually used for debugging purposes
103 * (detection of memory corruption).
106 void cds_lfht_node_init(struct cds_lfht_node
*node
__attribute__((unused
)))
111 * cds_lfht_node_init_deleted - initialize a hash table node to "removed" state
112 * @node: the node to initialize.
114 * Initialize the node such that cds_lfht_is_node_deleted() can be used
115 * on the node before it is added to a hash table.
118 void cds_lfht_node_init_deleted(struct cds_lfht_node
*node
);
121 * Hash table creation flags.
124 CDS_LFHT_AUTO_RESIZE
= (1U << 0),
125 CDS_LFHT_ACCOUNTING
= (1U << 1),
128 struct cds_lfht_mm_type
{
129 struct cds_lfht
*(*alloc_cds_lfht
)(unsigned long min_nr_alloc_buckets
,
130 unsigned long max_nr_buckets
, const struct cds_lfht_alloc
*alloc
);
131 void (*alloc_bucket_table
)(struct cds_lfht
*ht
, unsigned long order
);
132 void (*free_bucket_table
)(struct cds_lfht
*ht
, unsigned long order
);
133 struct cds_lfht_node
*(*bucket_at
)(struct cds_lfht
*ht
,
134 unsigned long index
);
137 extern const struct cds_lfht_mm_type cds_lfht_mm_order
;
138 extern const struct cds_lfht_mm_type cds_lfht_mm_chunk
;
139 extern const struct cds_lfht_mm_type cds_lfht_mm_mmap
;
142 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
145 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
146 unsigned long min_nr_alloc_buckets
,
147 unsigned long max_nr_buckets
,
149 const struct cds_lfht_mm_type
*mm
,
150 const struct rcu_flavor_struct
*flavor
,
151 pthread_attr_t
*attr
);
154 * _cds_lfht_new_with_alloc - API used by cds_lfht_new_with_flavor_alloc.
157 struct cds_lfht
*_cds_lfht_new_with_alloc(unsigned long init_size
,
158 unsigned long min_nr_alloc_buckets
,
159 unsigned long max_nr_buckets
,
161 const struct cds_lfht_mm_type
*mm
,
162 const struct rcu_flavor_struct
*flavor
,
163 const struct cds_lfht_alloc
*alloc
,
164 pthread_attr_t
*attr
);
167 * cds_lfht_new_flavor - allocate a hash table tied to a RCU flavor.
168 * @init_size: number of buckets to allocate initially. Must be power of two.
169 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
170 * (must be power of two)
171 * @max_nr_buckets: the maximum number of hash table buckets allowed.
172 * (must be power of two, 0 is accepted, means
174 * @flavor: flavor of liburcu to use to synchronize the hash table
175 * @flags: hash table creation flags (can be combined with bitwise or: '|').
177 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
178 * CDS_LFHT_ACCOUNTING: count the number of node addition
179 * and removal in the table
180 * @attr: optional resize worker thread attributes. NULL for default.
182 * Return NULL on error.
183 * Note: the RCU flavor must be already included before the hash table header.
185 * The programmer is responsible for ensuring that resize operation has a
186 * priority equal to hash table updater threads. It should be performed by
187 * specifying the appropriate priority in the pthread "attr" argument, and,
188 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
189 * this priority level. Having lower priority for call_rcu and resize threads
190 * does not pose any correctness issue, but the resize operations could be
191 * starved by updates, thus leading to long hash table bucket chains.
192 * Threads calling cds_lfht_new are NOT required to be registered RCU
193 * read-side threads. It can be called very early. (e.g. before RCU is
197 struct cds_lfht
*cds_lfht_new_flavor(unsigned long init_size
,
198 unsigned long min_nr_alloc_buckets
,
199 unsigned long max_nr_buckets
,
201 const struct rcu_flavor_struct
*flavor
,
202 pthread_attr_t
*attr
)
204 return _cds_lfht_new(init_size
, min_nr_alloc_buckets
, max_nr_buckets
,
205 flags
, NULL
, flavor
, attr
);
209 * cds_lfht_new_with_flavor_alloc - allocate a hash table tied to a RCU flavor.
210 * @init_size: number of buckets to allocate initially. Must be power of two.
211 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
212 * (must be power of two)
213 * @max_nr_buckets: the maximum number of hash table buckets allowed.
214 * (must be power of two, 0 is accepted, means
216 * @flavor: flavor of liburcu to use to synchronize the hash table
217 * @alloc: Custom memory allocator for hash table memory management.
218 * NULL for default. If a custom allocator is used, then
219 * the whole interface of struct cds_lfht_alloc must be implemented.
220 * @flags: hash table creation flags (can be combined with bitwise or: '|').
222 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
223 * CDS_LFHT_ACCOUNTING: count the number of node addition
224 * and removal in the table
225 * @attr: optional resize worker thread attributes. NULL for default.
227 * Return NULL on error.
228 * Note: the RCU flavor must be already included before the hash table header.
230 * The programmer is responsible for ensuring that resize operation has a
231 * priority equal to hash table updater threads. It should be performed by
232 * specifying the appropriate priority in the pthread "attr" argument, and,
233 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
234 * this priority level. Having lower priority for call_rcu and resize threads
235 * does not pose any correctness issue, but the resize operations could be
236 * starved by updates, thus leading to long hash table bucket chains.
237 * Threads calling cds_lfht_new are NOT required to be registered RCU
238 * read-side threads. It can be called very early. (e.g. before RCU is
242 struct cds_lfht
*cds_lfht_new_with_flavor_alloc(unsigned long init_size
,
243 unsigned long min_nr_alloc_buckets
,
244 unsigned long max_nr_buckets
,
246 const struct rcu_flavor_struct
*flavor
,
247 const struct cds_lfht_alloc
*alloc
,
248 pthread_attr_t
*attr
)
250 return _cds_lfht_new_with_alloc(init_size
, min_nr_alloc_buckets
, max_nr_buckets
,
251 flags
, NULL
, flavor
, alloc
, attr
);
257 * cds_lfht_new - allocate a hash table.
258 * @init_size: number of buckets to allocate initially. Must be power of two.
259 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
260 * (must be power of two)
261 * @max_nr_buckets: the maximum number of hash table buckets allowed.
262 * (must be power of two, 0 is accepted, means
264 * @flags: hash table creation flags (can be combined with bitwise or: '|').
266 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
267 * CDS_LFHT_ACCOUNTING: count the number of node addition
268 * and removal in the table
269 * @attr: optional resize worker thread attributes. NULL for default.
271 * Return NULL on error.
272 * Note: the RCU flavor must be already included before the hash table header.
274 * The programmer is responsible for ensuring that resize operation has a
275 * priority equal to hash table updater threads. It should be performed by
276 * specifying the appropriate priority in the pthread "attr" argument, and,
277 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
278 * this priority level. Having lower priority for call_rcu and resize threads
279 * does not pose any correctness issue, but the resize operations could be
280 * starved by updates, thus leading to long hash table bucket chains.
281 * Threads calling cds_lfht_new are NOT required to be registered RCU
282 * read-side threads. It can be called very early. (e.g. before RCU is
286 struct cds_lfht
*cds_lfht_new(unsigned long init_size
,
287 unsigned long min_nr_alloc_buckets
,
288 unsigned long max_nr_buckets
,
290 pthread_attr_t
*attr
)
292 return _cds_lfht_new(init_size
, min_nr_alloc_buckets
, max_nr_buckets
,
293 flags
, NULL
, &rcu_flavor
, attr
);
295 #endif /* URCU_API_MAP */
298 * cds_lfht_destroy - destroy a hash table.
299 * @ht: the hash table to destroy.
300 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
301 * The caller will typically want to free this pointer if dynamically
302 * allocated. The attr point can be NULL if the caller does not
303 * need to be informed of the value passed to cds_lfht_new().
305 * Return 0 on success, negative error value on error.
307 * Threads calling this API need to be registered RCU read-side threads.
309 * Prior to liburcu 0.10:
310 * - cds_lfht_destroy should *not* be called from a RCU read-side
311 * critical section. It should *not* be called from a call_rcu thread
314 * Starting from liburcu 0.10, rculfhash implements its own worker
315 * thread to handle resize operations, which removes the above RCU
316 * read-side critical section requirement on cds_lfht_destroy.
319 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
);
322 * cds_lfht_count_nodes - count the number of nodes in the hash table.
323 * @ht: the hash table.
324 * @split_count_before: sample the node count split-counter before traversal.
325 * @count: traverse the hash table, count the number of nodes observed.
326 * @split_count_after: sample the node count split-counter after traversal.
328 * Call with rcu_read_lock held.
329 * Threads calling this API need to be registered RCU read-side threads.
332 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
333 long *split_count_before
,
334 unsigned long *count
,
335 long *split_count_after
);
338 * cds_lfht_lookup - lookup a node by key.
339 * @ht: the hash table.
340 * @hash: the key hash.
341 * @match: the key match function.
342 * @key: the current node key.
343 * @iter: node, if found (output). *iter->node set to NULL if not found.
345 * Call with rcu_read_lock held.
346 * Threads calling this API need to be registered RCU read-side threads.
347 * This function acts as a rcu_dereference() to read the node pointer.
350 void cds_lfht_lookup(struct cds_lfht
*ht
, unsigned long hash
,
351 cds_lfht_match_fct match
, const void *key
,
352 struct cds_lfht_iter
*iter
);
355 * cds_lfht_next_duplicate - get the next item with same key, after iterator.
356 * @ht: the hash table.
357 * @match: the key match function.
358 * @key: the current node key.
359 * @iter: input: current iterator.
360 * output: node, if found. *iter->node set to NULL if not found.
362 * Uses an iterator initialized by a lookup or traversal. Important: the
363 * iterator _needs_ to be initialized before calling
364 * cds_lfht_next_duplicate.
365 * Sets *iter-node to the following node with same key.
366 * Sets *iter->node to NULL if no following node exists with same key.
367 * RCU read-side lock must be held across cds_lfht_lookup and
368 * cds_lfht_next calls, and also between cds_lfht_next calls using the
369 * node returned by a previous cds_lfht_next.
370 * Call with rcu_read_lock held.
371 * Threads calling this API need to be registered RCU read-side threads.
372 * This function acts as a rcu_dereference() to read the node pointer.
375 void cds_lfht_next_duplicate(struct cds_lfht
*ht
,
376 cds_lfht_match_fct match
, const void *key
,
377 struct cds_lfht_iter
*iter
);
380 * cds_lfht_first - get the first node in the table.
381 * @ht: the hash table.
382 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
384 * Output in "*iter". *iter->node set to NULL if table is empty.
385 * Call with rcu_read_lock held.
386 * Threads calling this API need to be registered RCU read-side threads.
387 * This function acts as a rcu_dereference() to read the node pointer.
390 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
393 * cds_lfht_next - get the next node in the table.
394 * @ht: the hash table.
395 * @iter: input: current iterator.
396 * output: next node, if exists. *iter->node set to NULL if not found.
398 * Input/Output in "*iter". *iter->node set to NULL if *iter was
399 * pointing to the last table node.
400 * Call with rcu_read_lock held.
401 * Threads calling this API need to be registered RCU read-side threads.
402 * This function acts as a rcu_dereference() to read the node pointer.
405 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
408 * cds_lfht_add - add a node to the hash table.
409 * @ht: the hash table.
410 * @hash: the key hash.
411 * @node: the node to add.
413 * This function supports adding redundant keys into the table.
414 * Call with rcu_read_lock held.
415 * Threads calling this API need to be registered RCU read-side threads.
416 * This function issues a full memory barrier before and after its
420 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
421 struct cds_lfht_node
*node
);
424 * cds_lfht_add_unique - add a node to hash table, if key is not present.
425 * @ht: the hash table.
426 * @hash: the node's hash.
427 * @match: the key match function.
428 * @key: the node's key.
429 * @node: the node to try adding.
431 * Return the node added upon success.
432 * Return the unique node already present upon failure. If
433 * cds_lfht_add_unique fails, the node passed as parameter should be
434 * freed by the caller. In this case, the caller does NOT need to wait
435 * for a grace period before freeing or re-using the node.
436 * Call with rcu_read_lock held.
437 * Threads calling this API need to be registered RCU read-side threads.
439 * The semantic of this function is that if only this function is used
440 * to add keys into the table, no duplicated keys should ever be
441 * observable in the table. The same guarantee apply for combination of
442 * add_unique and add_replace (see below).
444 * Upon success, this function issues a full memory barrier before and
445 * after its atomic commit. Upon failure, this function acts like a
446 * simple lookup operation: it acts as a rcu_dereference() to read the
447 * node pointer. The failure case does not guarantee any other memory
451 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
453 cds_lfht_match_fct match
,
455 struct cds_lfht_node
*node
);
458 * cds_lfht_add_replace - replace or add a node within hash table.
459 * @ht: the hash table.
460 * @hash: the node's hash.
461 * @match: the key match function.
462 * @key: the node's key.
463 * @node: the node to add.
465 * Return the node replaced upon success. If no node matching the key
466 * was present, return NULL, which also means the operation succeeded.
467 * This replacement operation should never fail.
468 * Call with rcu_read_lock held.
469 * Threads calling this API need to be registered RCU read-side threads.
470 * After successful replacement, a grace period must be waited for before
471 * freeing or re-using the memory reserved for the returned node.
473 * The semantic of replacement vs lookups and traversals is the
474 * following: if lookups and traversals are performed between a key
475 * unique insertion and its removal, we guarantee that the lookups and
476 * traversals will always find exactly one instance of the key if it is
477 * replaced concurrently with the lookups.
479 * Providing this semantic allows us to ensure that replacement-only
480 * schemes will never generate duplicated keys. It also allows us to
481 * guarantee that a combination of add_replace and add_unique updates
482 * will never generate duplicated keys.
484 * This function issues a full memory barrier before and after its
488 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
490 cds_lfht_match_fct match
,
492 struct cds_lfht_node
*node
);
495 * cds_lfht_replace - replace a node pointed to by iter within hash table.
496 * @ht: the hash table.
497 * @old_iter: the iterator position of the node to replace.
498 * @hash: the node's hash.
499 * @match: the key match function.
500 * @key: the node's key.
501 * @new_node: the new node to use as replacement.
503 * Return 0 if replacement is successful, negative value otherwise.
504 * Replacing a NULL old node or an already removed node will fail with
506 * If the hash or value of the node to replace and the new node differ,
507 * this function returns -EINVAL without proceeding to the replacement.
508 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
509 * RCU read-side lock must be held between lookup and replacement.
510 * Call with rcu_read_lock held.
511 * Threads calling this API need to be registered RCU read-side threads.
512 * After successful replacement, a grace period must be waited for before
513 * freeing or re-using the memory reserved for the old node (which can
514 * be accessed with cds_lfht_iter_get_node).
516 * The semantic of replacement vs lookups is the same as
517 * cds_lfht_add_replace().
519 * Upon success, this function issues a full memory barrier before and
520 * after its atomic commit. Upon failure, this function does not issue
521 * any memory barrier.
524 int cds_lfht_replace(struct cds_lfht
*ht
,
525 struct cds_lfht_iter
*old_iter
,
527 cds_lfht_match_fct match
,
529 struct cds_lfht_node
*new_node
);
532 * cds_lfht_del - remove node pointed to by iterator from hash table.
533 * @ht: the hash table.
534 * @node: the node to delete.
536 * Return 0 if the node is successfully removed, negative value
538 * Deleting a NULL node or an already removed node will fail with a
540 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
541 * followed by use of cds_lfht_iter_get_node.
542 * RCU read-side lock must be held between lookup and removal.
543 * Call with rcu_read_lock held.
544 * Threads calling this API need to be registered RCU read-side threads.
545 * After successful removal, a grace period must be waited for before
546 * freeing or re-using the memory reserved for old node (which can be
547 * accessed with cds_lfht_iter_get_node).
548 * Upon success, this function issues a full memory barrier before and
549 * after its atomic commit. Upon failure, this function does not issue
550 * any memory barrier.
553 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
556 * cds_lfht_is_node_deleted - query whether a node is removed from hash table.
558 * Return non-zero if the node is deleted from the hash table, 0
560 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
561 * followed by use of cds_lfht_iter_get_node.
562 * RCU read-side lock must be held between lookup and call to this
564 * Call with rcu_read_lock held.
565 * Threads calling this API need to be registered RCU read-side threads.
566 * This function does not issue any memory barrier.
569 int cds_lfht_is_node_deleted(const struct cds_lfht_node
*node
);
572 * cds_lfht_resize - Force a hash table resize
573 * @ht: the hash table.
574 * @new_size: update to this hash table size.
576 * Threads calling this API need to be registered RCU read-side threads.
577 * This function does not (necessarily) issue memory barriers.
578 * cds_lfht_resize should *not* be called from a RCU read-side critical
582 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
);
585 * Note: it is safe to perform element removal (del), replacement, or
586 * any hash table update operation during any of the following hash
588 * These functions act as rcu_dereference() to read the node pointers.
590 #define cds_lfht_for_each(ht, iter, node) \
591 for (cds_lfht_first(ht, iter), \
592 node = cds_lfht_iter_get_node(iter); \
594 cds_lfht_next(ht, iter), \
595 node = cds_lfht_iter_get_node(iter))
597 #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
598 for (cds_lfht_lookup(ht, hash, match, key, iter), \
599 node = cds_lfht_iter_get_node(iter); \
601 cds_lfht_next_duplicate(ht, match, key, iter), \
602 node = cds_lfht_iter_get_node(iter))
604 #define cds_lfht_entry(ptr, type, member) \
605 caa_container_of_check_null(ptr, type, member)
607 #define cds_lfht_for_each_entry(ht, iter, pos, member) \
608 for (cds_lfht_first(ht, iter), \
609 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
610 __typeof__(*(pos)), member); \
612 cds_lfht_next(ht, iter), \
613 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
614 __typeof__(*(pos)), member))
616 #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \
618 for (cds_lfht_lookup(ht, hash, match, key, iter), \
619 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
620 __typeof__(*(pos)), member); \
622 cds_lfht_next_duplicate(ht, match, key, iter), \
623 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
624 __typeof__(*(pos)), member))
630 #endif /* _URCU_RCULFHASH_H */