2 * SPDX-License-Identifier: LGPL-2.1-or-later
4 * Copyright 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
7 * Userspace RCU library - Lock-Free RCU Hash Table
10 #ifndef _LTTNG_UST_RCULFHASH_H
11 #define _LTTNG_UST_RCULFHASH_H
15 #include <urcu/compiler.h>
21 struct lttng_ust_lfht
;
24 * lttng_ust_lfht_node: Contains the next pointers and reverse-hash
25 * value required for lookup and traversal of the hash table.
27 * struct lttng_ust_lfht_node should be aligned on 8-bytes boundaries because
28 * the three lower bits are used as flags. It is worth noting that the
29 * information contained within these three bits could be represented on
30 * two bits by re-using the same bit for REMOVAL_OWNER_FLAG and
31 * BUCKET_FLAG. This can be done if we ensure that no iterator nor
32 * updater check the BUCKET_FLAG after it detects that the REMOVED_FLAG
33 * is set. Given the minimum size of struct lttng_ust_lfht_node is 8 bytes on
34 * 32-bit architectures, we choose to go for simplicity and reserve
37 * struct lttng_ust_lfht_node can be embedded into a structure (as a field).
38 * caa_container_of() can be used to get the structure from the struct
39 * lttng_ust_lfht_node after a lookup.
41 * The structure which embeds it typically holds the key (or key-value
42 * pair) of the object. The caller code is responsible for calculation
43 * of the hash value for lttng_ust_lfht APIs.
45 struct lttng_ust_lfht_node
{
46 struct lttng_ust_lfht_node
*next
; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */
47 unsigned long reverse_hash
;
48 } __attribute__((aligned(8)));
50 /* lttng_ust_lfht_iter: Used to track state while traversing a hash chain. */
51 struct lttng_ust_lfht_iter
{
52 struct lttng_ust_lfht_node
*node
, *next
;
56 struct lttng_ust_lfht_node
*lttng_ust_lfht_iter_get_node(struct lttng_ust_lfht_iter
*iter
)
61 struct rcu_flavor_struct
;
65 * Ensure reader and writer threads are registered as urcu readers.
68 typedef int (*lttng_ust_lfht_match_fct
)(struct lttng_ust_lfht_node
*node
, const void *key
);
71 * lttng_ust_lfht_node_init - initialize a hash table node
72 * @node: the node to initialize.
74 * This function is kept to be eventually used for debugging purposes
75 * (detection of memory corruption).
78 void lttng_ust_lfht_node_init(struct lttng_ust_lfht_node
*node
__attribute__((unused
)))
83 * Hash table creation flags.
86 LTTNG_UST_LFHT_AUTO_RESIZE
= (1U << 0),
87 LTTNG_UST_LFHT_ACCOUNTING
= (1U << 1),
90 struct lttng_ust_lfht_mm_type
{
91 struct lttng_ust_lfht
*(*alloc_lttng_ust_lfht
)(unsigned long min_nr_alloc_buckets
,
92 unsigned long max_nr_buckets
);
93 void (*alloc_bucket_table
)(struct lttng_ust_lfht
*ht
, unsigned long order
);
94 void (*free_bucket_table
)(struct lttng_ust_lfht
*ht
, unsigned long order
);
95 struct lttng_ust_lfht_node
*(*bucket_at
)(struct lttng_ust_lfht
*ht
,
99 extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_order
100 __attribute__((visibility("hidden")));
102 extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_chunk
103 __attribute__((visibility("hidden")));
105 extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_mmap
106 __attribute__((visibility("hidden")));
109 * lttng_ust_lfht_new - allocate a hash table.
110 * @init_size: number of buckets to allocate initially. Must be power of two.
111 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
112 * (must be power of two)
113 * @max_nr_buckets: the maximum number of hash table buckets allowed.
114 * (must be power of two, 0 is accepted, means
116 * @flags: hash table creation flags (can be combined with bitwise or: '|').
118 * LTTNG_UST_LFHT_AUTO_RESIZE: automatically resize hash table.
119 * LTTNG_UST_LFHT_ACCOUNTING: count the number of node addition
120 * and removal in the table
122 * Return NULL on error.
123 * Note: the RCU flavor must be already included before the hash table header.
125 extern struct lttng_ust_lfht
*lttng_ust_lfht_new(unsigned long init_size
,
126 unsigned long min_nr_alloc_buckets
,
127 unsigned long max_nr_buckets
,
129 const struct lttng_ust_lfht_mm_type
*mm
)
130 __attribute__((visibility("hidden")));
133 * lttng_ust_lfht_destroy - destroy a hash table.
134 * @ht: the hash table to destroy.
136 * Return 0 on success, negative error value on error.
138 * Prior to liburcu 0.10:
139 * - Threads calling this API need to be registered RCU read-side
141 * - lttng_ust_lfht_destroy should *not* be called from a RCU read-side
142 * critical section. It should *not* be called from a call_rcu thread
145 * Starting from liburcu 0.10, rculfhash implements its own worker
146 * thread to handle resize operations, which removes RCU requirements on
147 * lttng_ust_lfht_destroy.
149 extern int lttng_ust_lfht_destroy(struct lttng_ust_lfht
*ht
)
150 __attribute__((visibility("hidden")));
153 * lttng_ust_lfht_count_nodes - count the number of nodes in the hash table.
154 * @ht: the hash table.
155 * @split_count_before: sample the node count split-counter before traversal.
156 * @count: traverse the hash table, count the number of nodes observed.
157 * @split_count_after: sample the node count split-counter after traversal.
159 * Call with rcu_read_lock held.
160 * Threads calling this API need to be registered RCU read-side threads.
162 extern void lttng_ust_lfht_count_nodes(struct lttng_ust_lfht
*ht
,
163 long *split_count_before
,
164 unsigned long *count
,
165 long *split_count_after
)
166 __attribute__((visibility("hidden")));
169 * lttng_ust_lfht_lookup - lookup a node by key.
170 * @ht: the hash table.
171 * @hash: the key hash.
172 * @match: the key match function.
173 * @key: the current node key.
174 * @iter: node, if found (output). *iter->node set to NULL if not found.
176 * Call with rcu_read_lock held.
177 * Threads calling this API need to be registered RCU read-side threads.
178 * This function acts as a rcu_dereference() to read the node pointer.
180 extern void lttng_ust_lfht_lookup(struct lttng_ust_lfht
*ht
, unsigned long hash
,
181 lttng_ust_lfht_match_fct match
, const void *key
,
182 struct lttng_ust_lfht_iter
*iter
)
183 __attribute__((visibility("hidden")));
186 * lttng_ust_lfht_next_duplicate - get the next item with same key, after iterator.
187 * @ht: the hash table.
188 * @match: the key match function.
189 * @key: the current node key.
190 * @iter: input: current iterator.
191 * output: node, if found. *iter->node set to NULL if not found.
193 * Uses an iterator initialized by a lookup or traversal. Important: the
194 * iterator _needs_ to be initialized before calling
195 * lttng_ust_lfht_next_duplicate.
196 * Sets *iter-node to the following node with same key.
197 * Sets *iter->node to NULL if no following node exists with same key.
198 * RCU read-side lock must be held across lttng_ust_lfht_lookup and
199 * lttng_ust_lfht_next calls, and also between lttng_ust_lfht_next calls using the
200 * node returned by a previous lttng_ust_lfht_next.
201 * Call with rcu_read_lock held.
202 * Threads calling this API need to be registered RCU read-side threads.
203 * This function acts as a rcu_dereference() to read the node pointer.
205 extern void lttng_ust_lfht_next_duplicate(struct lttng_ust_lfht
*ht
,
206 lttng_ust_lfht_match_fct match
, const void *key
,
207 struct lttng_ust_lfht_iter
*iter
)
208 __attribute__((visibility("hidden")));
211 * lttng_ust_lfht_first - get the first node in the table.
212 * @ht: the hash table.
213 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
215 * Output in "*iter". *iter->node set to NULL if table is empty.
216 * Call with rcu_read_lock held.
217 * Threads calling this API need to be registered RCU read-side threads.
218 * This function acts as a rcu_dereference() to read the node pointer.
220 extern void lttng_ust_lfht_first(struct lttng_ust_lfht
*ht
, struct lttng_ust_lfht_iter
*iter
)
221 __attribute__((visibility("hidden")));
224 * lttng_ust_lfht_next - get the next node in the table.
225 * @ht: the hash table.
226 * @iter: input: current iterator.
227 * output: next node, if exists. *iter->node set to NULL if not found.
229 * Input/Output in "*iter". *iter->node set to NULL if *iter was
230 * pointing to the last table node.
231 * Call with rcu_read_lock held.
232 * Threads calling this API need to be registered RCU read-side threads.
233 * This function acts as a rcu_dereference() to read the node pointer.
235 extern void lttng_ust_lfht_next(struct lttng_ust_lfht
*ht
, struct lttng_ust_lfht_iter
*iter
)
236 __attribute__((visibility("hidden")));
239 * lttng_ust_lfht_add - add a node to the hash table.
240 * @ht: the hash table.
241 * @hash: the key hash.
242 * @node: the node to add.
244 * This function supports adding redundant keys into the table.
245 * Call with rcu_read_lock held.
246 * Threads calling this API need to be registered RCU read-side threads.
247 * This function issues a full memory barrier before and after its
250 extern void lttng_ust_lfht_add(struct lttng_ust_lfht
*ht
, unsigned long hash
,
251 struct lttng_ust_lfht_node
*node
)
252 __attribute__((visibility("hidden")));
255 * lttng_ust_lfht_add_unique - add a node to hash table, if key is not present.
256 * @ht: the hash table.
257 * @hash: the node's hash.
258 * @match: the key match function.
259 * @key: the node's key.
260 * @node: the node to try adding.
262 * Return the node added upon success.
263 * Return the unique node already present upon failure. If
264 * lttng_ust_lfht_add_unique fails, the node passed as parameter should be
265 * freed by the caller. In this case, the caller does NOT need to wait
266 * for a grace period before freeing or re-using the node.
267 * Call with rcu_read_lock held.
268 * Threads calling this API need to be registered RCU read-side threads.
270 * The semantic of this function is that if only this function is used
271 * to add keys into the table, no duplicated keys should ever be
272 * observable in the table. The same guarantee apply for combination of
273 * add_unique and add_replace (see below).
275 * Upon success, this function issues a full memory barrier before and
276 * after its atomic commit. Upon failure, this function acts like a
277 * simple lookup operation: it acts as a rcu_dereference() to read the
278 * node pointer. The failure case does not guarantee any other memory
281 extern struct lttng_ust_lfht_node
*lttng_ust_lfht_add_unique(struct lttng_ust_lfht
*ht
,
283 lttng_ust_lfht_match_fct match
,
285 struct lttng_ust_lfht_node
*node
)
286 __attribute__((visibility("hidden")));
289 * lttng_ust_lfht_add_replace - replace or add a node within hash table.
290 * @ht: the hash table.
291 * @hash: the node's hash.
292 * @match: the key match function.
293 * @key: the node's key.
294 * @node: the node to add.
296 * Return the node replaced upon success. If no node matching the key
297 * was present, return NULL, which also means the operation succeeded.
298 * This replacement operation should never fail.
299 * Call with rcu_read_lock held.
300 * Threads calling this API need to be registered RCU read-side threads.
301 * After successful replacement, a grace period must be waited for before
302 * freeing or re-using the memory reserved for the returned node.
304 * The semantic of replacement vs lookups and traversals is the
305 * following: if lookups and traversals are performed between a key
306 * unique insertion and its removal, we guarantee that the lookups and
307 * traversals will always find exactly one instance of the key if it is
308 * replaced concurrently with the lookups.
310 * Providing this semantic allows us to ensure that replacement-only
311 * schemes will never generate duplicated keys. It also allows us to
312 * guarantee that a combination of add_replace and add_unique updates
313 * will never generate duplicated keys.
315 * This function issues a full memory barrier before and after its
318 extern struct lttng_ust_lfht_node
*lttng_ust_lfht_add_replace(struct lttng_ust_lfht
*ht
,
320 lttng_ust_lfht_match_fct match
,
322 struct lttng_ust_lfht_node
*node
)
323 __attribute__((visibility("hidden")));
326 * lttng_ust_lfht_replace - replace a node pointed to by iter within hash table.
327 * @ht: the hash table.
328 * @old_iter: the iterator position of the node to replace.
329 * @hash: the node's hash.
330 * @match: the key match function.
331 * @key: the node's key.
332 * @new_node: the new node to use as replacement.
334 * Return 0 if replacement is successful, negative value otherwise.
335 * Replacing a NULL old node or an already removed node will fail with
337 * If the hash or value of the node to replace and the new node differ,
338 * this function returns -EINVAL without proceeding to the replacement.
339 * Old node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next.
340 * RCU read-side lock must be held between lookup and replacement.
341 * Call with rcu_read_lock held.
342 * Threads calling this API need to be registered RCU read-side threads.
343 * After successful replacement, a grace period must be waited for before
344 * freeing or re-using the memory reserved for the old node (which can
345 * be accessed with lttng_ust_lfht_iter_get_node).
347 * The semantic of replacement vs lookups is the same as
348 * lttng_ust_lfht_add_replace().
350 * Upon success, this function issues a full memory barrier before and
351 * after its atomic commit. Upon failure, this function does not issue
352 * any memory barrier.
354 extern int lttng_ust_lfht_replace(struct lttng_ust_lfht
*ht
,
355 struct lttng_ust_lfht_iter
*old_iter
,
357 lttng_ust_lfht_match_fct match
,
359 struct lttng_ust_lfht_node
*new_node
)
360 __attribute__((visibility("hidden")));
363 * lttng_ust_lfht_del - remove node pointed to by iterator from hash table.
364 * @ht: the hash table.
365 * @node: the node to delete.
367 * Return 0 if the node is successfully removed, negative value
369 * Deleting a NULL node or an already removed node will fail with a
371 * Node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next,
372 * followed by use of lttng_ust_lfht_iter_get_node.
373 * RCU read-side lock must be held between lookup and removal.
374 * Call with rcu_read_lock held.
375 * Threads calling this API need to be registered RCU read-side threads.
376 * After successful removal, a grace period must be waited for before
377 * freeing or re-using the memory reserved for old node (which can be
378 * accessed with lttng_ust_lfht_iter_get_node).
379 * Upon success, this function issues a full memory barrier before and
380 * after its atomic commit. Upon failure, this function does not issue
381 * any memory barrier.
383 extern int lttng_ust_lfht_del(struct lttng_ust_lfht
*ht
, struct lttng_ust_lfht_node
*node
)
384 __attribute__((visibility("hidden")));
387 * lttng_ust_lfht_is_node_deleted - query whether a node is removed from hash table.
389 * Return non-zero if the node is deleted from the hash table, 0
391 * Node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next,
392 * followed by use of lttng_ust_lfht_iter_get_node.
393 * RCU read-side lock must be held between lookup and call to this
395 * Call with rcu_read_lock held.
396 * Threads calling this API need to be registered RCU read-side threads.
397 * This function does not issue any memory barrier.
399 extern int lttng_ust_lfht_is_node_deleted(const struct lttng_ust_lfht_node
*node
)
400 __attribute__((visibility("hidden")));
403 * lttng_ust_lfht_resize - Force a hash table resize
404 * @ht: the hash table.
405 * @new_size: update to this hash table size.
407 * Threads calling this API need to be registered RCU read-side threads.
408 * This function does not (necessarily) issue memory barriers.
409 * lttng_ust_lfht_resize should *not* be called from a RCU read-side critical
412 extern void lttng_ust_lfht_resize(struct lttng_ust_lfht
*ht
, unsigned long new_size
)
413 __attribute__((visibility("hidden")));
416 * Note: it is safe to perform element removal (del), replacement, or
417 * any hash table update operation during any of the following hash
419 * These functions act as rcu_dereference() to read the node pointers.
421 #define lttng_ust_lfht_for_each(ht, iter, node) \
422 for (lttng_ust_lfht_first(ht, iter), \
423 node = lttng_ust_lfht_iter_get_node(iter); \
425 lttng_ust_lfht_next(ht, iter), \
426 node = lttng_ust_lfht_iter_get_node(iter))
428 #define lttng_ust_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
429 for (lttng_ust_lfht_lookup(ht, hash, match, key, iter), \
430 node = lttng_ust_lfht_iter_get_node(iter); \
432 lttng_ust_lfht_next_duplicate(ht, match, key, iter), \
433 node = lttng_ust_lfht_iter_get_node(iter))
435 #define lttng_ust_lfht_for_each_entry(ht, iter, pos, member) \
436 for (lttng_ust_lfht_first(ht, iter), \
437 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
438 __typeof__(*(pos)), member); \
439 lttng_ust_lfht_iter_get_node(iter) != NULL; \
440 lttng_ust_lfht_next(ht, iter), \
441 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
442 __typeof__(*(pos)), member))
444 #define lttng_ust_lfht_for_each_entry_duplicate(ht, hash, match, key, \
446 for (lttng_ust_lfht_lookup(ht, hash, match, key, iter), \
447 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
448 __typeof__(*(pos)), member); \
449 lttng_ust_lfht_iter_get_node(iter) != NULL; \
450 lttng_ust_lfht_next_duplicate(ht, match, key, iter), \
451 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
452 __typeof__(*(pos)), member))
458 #endif /* _LTTNG_UST_RCULFHASH_H */