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 struct cds_lfht_node
*cds_lfht_iter_get_node(struct cds_lfht_iter
*iter
)
77 struct rcu_flavor_struct
;
81 * Ensure reader and writer threads are registered as urcu readers.
84 typedef int (*cds_lfht_match_fct
)(struct cds_lfht_node
*node
, const void *key
);
87 * cds_lfht_node_init - initialize a hash table node
88 * @node: the node to initialize.
90 * This function is kept to be eventually used for debugging purposes
91 * (detection of memory corruption).
94 void cds_lfht_node_init(struct cds_lfht_node
*node
__attribute__((unused
)))
99 * cds_lfht_node_init_deleted - initialize a hash table node to "removed" state
100 * @node: the node to initialize.
102 * Initialize the node such that cds_lfht_is_node_deleted() can be used
103 * on the node before it is added to a hash table.
106 void cds_lfht_node_init_deleted(struct cds_lfht_node
*node
);
109 * Hash table creation flags.
112 CDS_LFHT_AUTO_RESIZE
= (1U << 0),
113 CDS_LFHT_ACCOUNTING
= (1U << 1),
116 struct cds_lfht_mm_type
{
117 struct cds_lfht
*(*alloc_cds_lfht
)(unsigned long min_nr_alloc_buckets
,
118 unsigned long max_nr_buckets
);
119 void (*alloc_bucket_table
)(struct cds_lfht
*ht
, unsigned long order
);
120 void (*free_bucket_table
)(struct cds_lfht
*ht
, unsigned long order
);
121 struct cds_lfht_node
*(*bucket_at
)(struct cds_lfht
*ht
,
122 unsigned long index
);
125 extern const struct cds_lfht_mm_type cds_lfht_mm_order
;
126 extern const struct cds_lfht_mm_type cds_lfht_mm_chunk
;
127 extern const struct cds_lfht_mm_type cds_lfht_mm_mmap
;
130 * _cds_lfht_new - API used by cds_lfht_new wrapper. Do not use directly.
133 struct cds_lfht
*_cds_lfht_new(unsigned long init_size
,
134 unsigned long min_nr_alloc_buckets
,
135 unsigned long max_nr_buckets
,
137 const struct cds_lfht_mm_type
*mm
,
138 const struct rcu_flavor_struct
*flavor
,
139 pthread_attr_t
*attr
);
142 * cds_lfht_new_flavor - allocate a hash table tied to a RCU flavor.
143 * @init_size: number of buckets to allocate initially. Must be power of two.
144 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
145 * (must be power of two)
146 * @max_nr_buckets: the maximum number of hash table buckets allowed.
147 * (must be power of two, 0 is accepted, means
149 * @flavor: flavor of liburcu to use to synchronize the hash table
150 * @flags: hash table creation flags (can be combined with bitwise or: '|').
152 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
153 * CDS_LFHT_ACCOUNTING: count the number of node addition
154 * and removal in the table
155 * @attr: optional resize worker thread attributes. NULL for default.
157 * Return NULL on error.
158 * Note: the RCU flavor must be already included before the hash table header.
160 * The programmer is responsible for ensuring that resize operation has a
161 * priority equal to hash table updater threads. It should be performed by
162 * specifying the appropriate priority in the pthread "attr" argument, and,
163 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
164 * this priority level. Having lower priority for call_rcu and resize threads
165 * does not pose any correctness issue, but the resize operations could be
166 * starved by updates, thus leading to long hash table bucket chains.
167 * Threads calling cds_lfht_new are NOT required to be registered RCU
168 * read-side threads. It can be called very early. (e.g. before RCU is
172 struct cds_lfht
*cds_lfht_new_flavor(unsigned long init_size
,
173 unsigned long min_nr_alloc_buckets
,
174 unsigned long max_nr_buckets
,
176 const struct rcu_flavor_struct
*flavor
,
177 pthread_attr_t
*attr
)
179 return _cds_lfht_new(init_size
, min_nr_alloc_buckets
, max_nr_buckets
,
180 flags
, NULL
, flavor
, attr
);
186 * cds_lfht_new - allocate a hash table.
187 * @init_size: number of buckets to allocate initially. Must be power of two.
188 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
189 * (must be power of two)
190 * @max_nr_buckets: the maximum number of hash table buckets allowed.
191 * (must be power of two, 0 is accepted, means
193 * @flags: hash table creation flags (can be combined with bitwise or: '|').
195 * CDS_LFHT_AUTO_RESIZE: automatically resize hash table.
196 * CDS_LFHT_ACCOUNTING: count the number of node addition
197 * and removal in the table
198 * @attr: optional resize worker thread attributes. NULL for default.
200 * Return NULL on error.
201 * Note: the RCU flavor must be already included before the hash table header.
203 * The programmer is responsible for ensuring that resize operation has a
204 * priority equal to hash table updater threads. It should be performed by
205 * specifying the appropriate priority in the pthread "attr" argument, and,
206 * for CDS_LFHT_AUTO_RESIZE, by ensuring that call_rcu worker threads also have
207 * this priority level. Having lower priority for call_rcu and resize threads
208 * does not pose any correctness issue, but the resize operations could be
209 * starved by updates, thus leading to long hash table bucket chains.
210 * Threads calling cds_lfht_new are NOT required to be registered RCU
211 * read-side threads. It can be called very early. (e.g. before RCU is
215 struct cds_lfht
*cds_lfht_new(unsigned long init_size
,
216 unsigned long min_nr_alloc_buckets
,
217 unsigned long max_nr_buckets
,
219 pthread_attr_t
*attr
)
221 return _cds_lfht_new(init_size
, min_nr_alloc_buckets
, max_nr_buckets
,
222 flags
, NULL
, &rcu_flavor
, attr
);
224 #endif /* URCU_API_MAP */
227 * cds_lfht_destroy - destroy a hash table.
228 * @ht: the hash table to destroy.
229 * @attr: (output) resize worker thread attributes, as received by cds_lfht_new.
230 * The caller will typically want to free this pointer if dynamically
231 * allocated. The attr point can be NULL if the caller does not
232 * need to be informed of the value passed to cds_lfht_new().
234 * Return 0 on success, negative error value on error.
236 * Threads calling this API need to be registered RCU read-side threads.
238 * Prior to liburcu 0.10:
239 * - cds_lfht_destroy should *not* be called from a RCU read-side
240 * critical section. It should *not* be called from a call_rcu thread
243 * Starting from liburcu 0.10, rculfhash implements its own worker
244 * thread to handle resize operations, which removes the above RCU
245 * read-side critical section requirement on cds_lfht_destroy.
248 int cds_lfht_destroy(struct cds_lfht
*ht
, pthread_attr_t
**attr
);
251 * cds_lfht_count_nodes - count the number of nodes in the hash table.
252 * @ht: the hash table.
253 * @split_count_before: sample the node count split-counter before traversal.
254 * @count: traverse the hash table, count the number of nodes observed.
255 * @split_count_after: sample the node count split-counter after traversal.
257 * Call with rcu_read_lock held.
258 * Threads calling this API need to be registered RCU read-side threads.
261 void cds_lfht_count_nodes(struct cds_lfht
*ht
,
262 long *split_count_before
,
263 unsigned long *count
,
264 long *split_count_after
);
267 * cds_lfht_lookup - lookup a node by key.
268 * @ht: the hash table.
269 * @hash: the key hash.
270 * @match: the key match function.
271 * @key: the current node key.
272 * @iter: node, if found (output). *iter->node set to NULL if not found.
274 * Call with rcu_read_lock held.
275 * Threads calling this API need to be registered RCU read-side threads.
276 * This function acts as a rcu_dereference() to read the node pointer.
279 void cds_lfht_lookup(struct cds_lfht
*ht
, unsigned long hash
,
280 cds_lfht_match_fct match
, const void *key
,
281 struct cds_lfht_iter
*iter
);
284 * cds_lfht_next_duplicate - get the next item with same key, after iterator.
285 * @ht: the hash table.
286 * @match: the key match function.
287 * @key: the current node key.
288 * @iter: input: current iterator.
289 * output: node, if found. *iter->node set to NULL if not found.
291 * Uses an iterator initialized by a lookup or traversal. Important: the
292 * iterator _needs_ to be initialized before calling
293 * cds_lfht_next_duplicate.
294 * Sets *iter-node to the following node with same key.
295 * Sets *iter->node to NULL if no following node exists with same key.
296 * RCU read-side lock must be held across cds_lfht_lookup and
297 * cds_lfht_next calls, and also between cds_lfht_next calls using the
298 * node returned by a previous cds_lfht_next.
299 * Call with rcu_read_lock held.
300 * Threads calling this API need to be registered RCU read-side threads.
301 * This function acts as a rcu_dereference() to read the node pointer.
304 void cds_lfht_next_duplicate(struct cds_lfht
*ht
,
305 cds_lfht_match_fct match
, const void *key
,
306 struct cds_lfht_iter
*iter
);
309 * cds_lfht_first - get the first node in the table.
310 * @ht: the hash table.
311 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
313 * Output in "*iter". *iter->node set to NULL if table is empty.
314 * Call with rcu_read_lock held.
315 * Threads calling this API need to be registered RCU read-side threads.
316 * This function acts as a rcu_dereference() to read the node pointer.
319 void cds_lfht_first(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
322 * cds_lfht_next - get the next node in the table.
323 * @ht: the hash table.
324 * @iter: input: current iterator.
325 * output: next node, if exists. *iter->node set to NULL if not found.
327 * Input/Output in "*iter". *iter->node set to NULL if *iter was
328 * pointing to the last table node.
329 * Call with rcu_read_lock held.
330 * Threads calling this API need to be registered RCU read-side threads.
331 * This function acts as a rcu_dereference() to read the node pointer.
334 void cds_lfht_next(struct cds_lfht
*ht
, struct cds_lfht_iter
*iter
);
337 * cds_lfht_add - add a node to the hash table.
338 * @ht: the hash table.
339 * @hash: the key hash.
340 * @node: the node to add.
342 * This function supports adding redundant keys into the table.
343 * Call with rcu_read_lock held.
344 * Threads calling this API need to be registered RCU read-side threads.
345 * This function issues a full memory barrier before and after its
349 void cds_lfht_add(struct cds_lfht
*ht
, unsigned long hash
,
350 struct cds_lfht_node
*node
);
353 * cds_lfht_add_unique - add a node to hash table, if key is not present.
354 * @ht: the hash table.
355 * @hash: the node's hash.
356 * @match: the key match function.
357 * @key: the node's key.
358 * @node: the node to try adding.
360 * Return the node added upon success.
361 * Return the unique node already present upon failure. If
362 * cds_lfht_add_unique fails, the node passed as parameter should be
363 * freed by the caller. In this case, the caller does NOT need to wait
364 * for a grace period before freeing or re-using the node.
365 * Call with rcu_read_lock held.
366 * Threads calling this API need to be registered RCU read-side threads.
368 * The semantic of this function is that if only this function is used
369 * to add keys into the table, no duplicated keys should ever be
370 * observable in the table. The same guarantee apply for combination of
371 * add_unique and add_replace (see below).
373 * Upon success, this function issues a full memory barrier before and
374 * after its atomic commit. Upon failure, this function acts like a
375 * simple lookup operation: it acts as a rcu_dereference() to read the
376 * node pointer. The failure case does not guarantee any other memory
380 struct cds_lfht_node
*cds_lfht_add_unique(struct cds_lfht
*ht
,
382 cds_lfht_match_fct match
,
384 struct cds_lfht_node
*node
);
387 * cds_lfht_add_replace - replace or add a node within hash table.
388 * @ht: the hash table.
389 * @hash: the node's hash.
390 * @match: the key match function.
391 * @key: the node's key.
392 * @node: the node to add.
394 * Return the node replaced upon success. If no node matching the key
395 * was present, return NULL, which also means the operation succeeded.
396 * This replacement operation should never fail.
397 * Call with rcu_read_lock held.
398 * Threads calling this API need to be registered RCU read-side threads.
399 * After successful replacement, a grace period must be waited for before
400 * freeing or re-using the memory reserved for the returned node.
402 * The semantic of replacement vs lookups and traversals is the
403 * following: if lookups and traversals are performed between a key
404 * unique insertion and its removal, we guarantee that the lookups and
405 * traversals will always find exactly one instance of the key if it is
406 * replaced concurrently with the lookups.
408 * Providing this semantic allows us to ensure that replacement-only
409 * schemes will never generate duplicated keys. It also allows us to
410 * guarantee that a combination of add_replace and add_unique updates
411 * will never generate duplicated keys.
413 * This function issues a full memory barrier before and after its
417 struct cds_lfht_node
*cds_lfht_add_replace(struct cds_lfht
*ht
,
419 cds_lfht_match_fct match
,
421 struct cds_lfht_node
*node
);
424 * cds_lfht_replace - replace a node pointed to by iter within hash table.
425 * @ht: the hash table.
426 * @old_iter: the iterator position of the node to replace.
427 * @hash: the node's hash.
428 * @match: the key match function.
429 * @key: the node's key.
430 * @new_node: the new node to use as replacement.
432 * Return 0 if replacement is successful, negative value otherwise.
433 * Replacing a NULL old node or an already removed node will fail with
435 * If the hash or value of the node to replace and the new node differ,
436 * this function returns -EINVAL without proceeding to the replacement.
437 * Old node can be looked up with cds_lfht_lookup and cds_lfht_next.
438 * RCU read-side lock must be held between lookup and replacement.
439 * Call with rcu_read_lock held.
440 * Threads calling this API need to be registered RCU read-side threads.
441 * After successful replacement, a grace period must be waited for before
442 * freeing or re-using the memory reserved for the old node (which can
443 * be accessed with cds_lfht_iter_get_node).
445 * The semantic of replacement vs lookups is the same as
446 * cds_lfht_add_replace().
448 * Upon success, this function issues a full memory barrier before and
449 * after its atomic commit. Upon failure, this function does not issue
450 * any memory barrier.
453 int cds_lfht_replace(struct cds_lfht
*ht
,
454 struct cds_lfht_iter
*old_iter
,
456 cds_lfht_match_fct match
,
458 struct cds_lfht_node
*new_node
);
461 * cds_lfht_del - remove node pointed to by iterator from hash table.
462 * @ht: the hash table.
463 * @node: the node to delete.
465 * Return 0 if the node is successfully removed, negative value
467 * Deleting a NULL node or an already removed node will fail with a
469 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
470 * followed by use of cds_lfht_iter_get_node.
471 * RCU read-side lock must be held between lookup and removal.
472 * Call with rcu_read_lock held.
473 * Threads calling this API need to be registered RCU read-side threads.
474 * After successful removal, a grace period must be waited for before
475 * freeing or re-using the memory reserved for old node (which can be
476 * accessed with cds_lfht_iter_get_node).
477 * Upon success, this function issues a full memory barrier before and
478 * after its atomic commit. Upon failure, this function does not issue
479 * any memory barrier.
482 int cds_lfht_del(struct cds_lfht
*ht
, struct cds_lfht_node
*node
);
485 * cds_lfht_is_node_deleted - query whether a node is removed from hash table.
487 * Return non-zero if the node is deleted from the hash table, 0
489 * Node can be looked up with cds_lfht_lookup and cds_lfht_next,
490 * followed by use of cds_lfht_iter_get_node.
491 * RCU read-side lock must be held between lookup and call to this
493 * Call with rcu_read_lock held.
494 * Threads calling this API need to be registered RCU read-side threads.
495 * This function does not issue any memory barrier.
498 int cds_lfht_is_node_deleted(const struct cds_lfht_node
*node
);
501 * cds_lfht_resize - Force a hash table resize
502 * @ht: the hash table.
503 * @new_size: update to this hash table size.
505 * Threads calling this API need to be registered RCU read-side threads.
506 * This function does not (necessarily) issue memory barriers.
507 * cds_lfht_resize should *not* be called from a RCU read-side critical
511 void cds_lfht_resize(struct cds_lfht
*ht
, unsigned long new_size
);
514 * Note: it is safe to perform element removal (del), replacement, or
515 * any hash table update operation during any of the following hash
517 * These functions act as rcu_dereference() to read the node pointers.
519 #define cds_lfht_for_each(ht, iter, node) \
520 for (cds_lfht_first(ht, iter), \
521 node = cds_lfht_iter_get_node(iter); \
523 cds_lfht_next(ht, iter), \
524 node = cds_lfht_iter_get_node(iter))
526 #define cds_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
527 for (cds_lfht_lookup(ht, hash, match, key, iter), \
528 node = cds_lfht_iter_get_node(iter); \
530 cds_lfht_next_duplicate(ht, match, key, iter), \
531 node = cds_lfht_iter_get_node(iter))
533 #define cds_lfht_entry(ptr, type, member) \
534 caa_container_of_check_null(ptr, type, member)
536 #define cds_lfht_for_each_entry(ht, iter, pos, member) \
537 for (cds_lfht_first(ht, iter), \
538 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
539 __typeof__(*(pos)), member); \
541 cds_lfht_next(ht, iter), \
542 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
543 __typeof__(*(pos)), member))
545 #define cds_lfht_for_each_entry_duplicate(ht, hash, match, key, \
547 for (cds_lfht_lookup(ht, hash, match, key, iter), \
548 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
549 __typeof__(*(pos)), member); \
551 cds_lfht_next_duplicate(ht, match, key, iter), \
552 pos = cds_lfht_entry(cds_lfht_iter_get_node(iter), \
553 __typeof__(*(pos)), member))
559 #endif /* _URCU_RCULFHASH_H */