Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust / rculfhash.h
CommitLineData
10544ee8 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-or-later
10544ee8 3 *
c0c0989a
MJ
4 * Copyright 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright 2011 Lai Jiangshan <laijs@cn.fujitsu.com>
10544ee8 6 *
c0c0989a 7 * Userspace RCU library - Lock-Free RCU Hash Table
10544ee8
MD
8 */
9
c0c0989a
MJ
10#ifndef _LTTNG_UST_RCULFHASH_H
11#define _LTTNG_UST_RCULFHASH_H
12
10544ee8
MD
13#include <stdint.h>
14#include <pthread.h>
15#include <urcu/compiler.h>
d2dd7baa 16#include "helper.h"
10544ee8
MD
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22struct lttng_ust_lfht;
23
24/*
25 * lttng_ust_lfht_node: Contains the next pointers and reverse-hash
26 * value required for lookup and traversal of the hash table.
27 *
28 * struct lttng_ust_lfht_node should be aligned on 8-bytes boundaries because
29 * the three lower bits are used as flags. It is worth noting that the
30 * information contained within these three bits could be represented on
31 * two bits by re-using the same bit for REMOVAL_OWNER_FLAG and
32 * BUCKET_FLAG. This can be done if we ensure that no iterator nor
33 * updater check the BUCKET_FLAG after it detects that the REMOVED_FLAG
34 * is set. Given the minimum size of struct lttng_ust_lfht_node is 8 bytes on
35 * 32-bit architectures, we choose to go for simplicity and reserve
36 * three bits.
37 *
38 * struct lttng_ust_lfht_node can be embedded into a structure (as a field).
39 * caa_container_of() can be used to get the structure from the struct
40 * lttng_ust_lfht_node after a lookup.
41 *
42 * The structure which embeds it typically holds the key (or key-value
43 * pair) of the object. The caller code is responsible for calculation
44 * of the hash value for lttng_ust_lfht APIs.
45 */
46struct lttng_ust_lfht_node {
47 struct lttng_ust_lfht_node *next; /* ptr | REMOVAL_OWNER_FLAG | BUCKET_FLAG | REMOVED_FLAG */
48 unsigned long reverse_hash;
49} __attribute__((aligned(8)));
50
51/* lttng_ust_lfht_iter: Used to track state while traversing a hash chain. */
52struct lttng_ust_lfht_iter {
53 struct lttng_ust_lfht_node *node, *next;
54};
55
56static inline
57struct lttng_ust_lfht_node *lttng_ust_lfht_iter_get_node(struct lttng_ust_lfht_iter *iter)
58{
59 return iter->node;
60}
61
62struct rcu_flavor_struct;
63
64/*
65 * Caution !
66 * Ensure reader and writer threads are registered as urcu readers.
67 */
68
69typedef int (*lttng_ust_lfht_match_fct)(struct lttng_ust_lfht_node *node, const void *key);
70
71/*
72 * lttng_ust_lfht_node_init - initialize a hash table node
73 * @node: the node to initialize.
74 *
75 * This function is kept to be eventually used for debugging purposes
76 * (detection of memory corruption).
77 */
78static inline
79void lttng_ust_lfht_node_init(struct lttng_ust_lfht_node *node)
80{
81}
82
83/*
84 * Hash table creation flags.
85 */
86enum {
87 LTTNG_UST_LFHT_AUTO_RESIZE = (1U << 0),
88 LTTNG_UST_LFHT_ACCOUNTING = (1U << 1),
89};
90
91struct lttng_ust_lfht_mm_type {
92 struct lttng_ust_lfht *(*alloc_lttng_ust_lfht)(unsigned long min_nr_alloc_buckets,
93 unsigned long max_nr_buckets);
94 void (*alloc_bucket_table)(struct lttng_ust_lfht *ht, unsigned long order);
95 void (*free_bucket_table)(struct lttng_ust_lfht *ht, unsigned long order);
96 struct lttng_ust_lfht_node *(*bucket_at)(struct lttng_ust_lfht *ht,
97 unsigned long index);
98};
99
d2dd7baa 100LTTNG_HIDDEN
10544ee8 101extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_order;
d2dd7baa 102LTTNG_HIDDEN
10544ee8 103extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_chunk;
d2dd7baa 104LTTNG_HIDDEN
10544ee8
MD
105extern const struct lttng_ust_lfht_mm_type lttng_ust_lfht_mm_mmap;
106
107/*
108 * lttng_ust_lfht_new - allocate a hash table.
109 * @init_size: number of buckets to allocate initially. Must be power of two.
110 * @min_nr_alloc_buckets: the minimum number of allocated buckets.
111 * (must be power of two)
112 * @max_nr_buckets: the maximum number of hash table buckets allowed.
113 * (must be power of two, 0 is accepted, means
114 * "infinite")
115 * @flags: hash table creation flags (can be combined with bitwise or: '|').
116 * 0: no flags.
117 * LTTNG_UST_LFHT_AUTO_RESIZE: automatically resize hash table.
118 * LTTNG_UST_LFHT_ACCOUNTING: count the number of node addition
119 * and removal in the table
120 *
121 * Return NULL on error.
122 * Note: the RCU flavor must be already included before the hash table header.
123 */
d2dd7baa
MJ
124LTTNG_HIDDEN
125extern struct lttng_ust_lfht *lttng_ust_lfht_new(unsigned long init_size,
10544ee8
MD
126 unsigned long min_nr_alloc_buckets,
127 unsigned long max_nr_buckets,
128 int flags,
129 const struct lttng_ust_lfht_mm_type *mm);
130
131/*
132 * lttng_ust_lfht_destroy - destroy a hash table.
133 * @ht: the hash table to destroy.
134 *
135 * Return 0 on success, negative error value on error.
136
137 * Prior to liburcu 0.10:
138 * - Threads calling this API need to be registered RCU read-side
139 * threads.
140 * - lttng_ust_lfht_destroy should *not* be called from a RCU read-side
141 * critical section. It should *not* be called from a call_rcu thread
142 * context neither.
143 *
144 * Starting from liburcu 0.10, rculfhash implements its own worker
145 * thread to handle resize operations, which removes RCU requirements on
146 * lttng_ust_lfht_destroy.
147 */
d2dd7baa
MJ
148LTTNG_HIDDEN
149extern int lttng_ust_lfht_destroy(struct lttng_ust_lfht *ht);
10544ee8
MD
150
151/*
152 * lttng_ust_lfht_count_nodes - count the number of nodes in the hash table.
153 * @ht: the hash table.
154 * @split_count_before: sample the node count split-counter before traversal.
155 * @count: traverse the hash table, count the number of nodes observed.
156 * @split_count_after: sample the node count split-counter after traversal.
157 *
158 * Call with rcu_read_lock held.
159 * Threads calling this API need to be registered RCU read-side threads.
160 */
d2dd7baa
MJ
161LTTNG_HIDDEN
162extern void lttng_ust_lfht_count_nodes(struct lttng_ust_lfht *ht,
10544ee8
MD
163 long *split_count_before,
164 unsigned long *count,
165 long *split_count_after);
166
167/*
168 * lttng_ust_lfht_lookup - lookup a node by key.
169 * @ht: the hash table.
170 * @hash: the key hash.
171 * @match: the key match function.
172 * @key: the current node key.
173 * @iter: node, if found (output). *iter->node set to NULL if not found.
174 *
175 * Call with rcu_read_lock held.
176 * Threads calling this API need to be registered RCU read-side threads.
177 * This function acts as a rcu_dereference() to read the node pointer.
178 */
d2dd7baa
MJ
179LTTNG_HIDDEN
180extern void lttng_ust_lfht_lookup(struct lttng_ust_lfht *ht, unsigned long hash,
10544ee8
MD
181 lttng_ust_lfht_match_fct match, const void *key,
182 struct lttng_ust_lfht_iter *iter);
183
184/*
185 * lttng_ust_lfht_next_duplicate - get the next item with same key, after iterator.
186 * @ht: the hash table.
187 * @match: the key match function.
188 * @key: the current node key.
189 * @iter: input: current iterator.
190 * output: node, if found. *iter->node set to NULL if not found.
191 *
192 * Uses an iterator initialized by a lookup or traversal. Important: the
193 * iterator _needs_ to be initialized before calling
194 * lttng_ust_lfht_next_duplicate.
195 * Sets *iter-node to the following node with same key.
196 * Sets *iter->node to NULL if no following node exists with same key.
197 * RCU read-side lock must be held across lttng_ust_lfht_lookup and
198 * lttng_ust_lfht_next calls, and also between lttng_ust_lfht_next calls using the
199 * node returned by a previous lttng_ust_lfht_next.
200 * Call with rcu_read_lock held.
201 * Threads calling this API need to be registered RCU read-side threads.
202 * This function acts as a rcu_dereference() to read the node pointer.
203 */
d2dd7baa
MJ
204LTTNG_HIDDEN
205extern void lttng_ust_lfht_next_duplicate(struct lttng_ust_lfht *ht,
10544ee8
MD
206 lttng_ust_lfht_match_fct match, const void *key,
207 struct lttng_ust_lfht_iter *iter);
208
209/*
210 * lttng_ust_lfht_first - get the first node in the table.
211 * @ht: the hash table.
212 * @iter: First node, if exists (output). *iter->node set to NULL if not found.
213 *
214 * Output in "*iter". *iter->node set to NULL if table is empty.
215 * Call with rcu_read_lock held.
216 * Threads calling this API need to be registered RCU read-side threads.
217 * This function acts as a rcu_dereference() to read the node pointer.
218 */
d2dd7baa
MJ
219LTTNG_HIDDEN
220extern void lttng_ust_lfht_first(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_iter *iter);
10544ee8
MD
221
222/*
223 * lttng_ust_lfht_next - get the next node in the table.
224 * @ht: the hash table.
225 * @iter: input: current iterator.
226 * output: next node, if exists. *iter->node set to NULL if not found.
227 *
228 * Input/Output in "*iter". *iter->node set to NULL if *iter was
229 * pointing to the last table node.
230 * Call with rcu_read_lock held.
231 * Threads calling this API need to be registered RCU read-side threads.
232 * This function acts as a rcu_dereference() to read the node pointer.
233 */
d2dd7baa
MJ
234LTTNG_HIDDEN
235extern void lttng_ust_lfht_next(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_iter *iter);
10544ee8
MD
236
237/*
238 * lttng_ust_lfht_add - add a node to the hash table.
239 * @ht: the hash table.
240 * @hash: the key hash.
241 * @node: the node to add.
242 *
243 * This function supports adding redundant keys into the table.
244 * Call with rcu_read_lock held.
245 * Threads calling this API need to be registered RCU read-side threads.
246 * This function issues a full memory barrier before and after its
247 * atomic commit.
248 */
d2dd7baa
MJ
249LTTNG_HIDDEN
250extern void lttng_ust_lfht_add(struct lttng_ust_lfht *ht, unsigned long hash,
10544ee8
MD
251 struct lttng_ust_lfht_node *node);
252
253/*
254 * lttng_ust_lfht_add_unique - add a node to hash table, if key is not present.
255 * @ht: the hash table.
256 * @hash: the node's hash.
257 * @match: the key match function.
258 * @key: the node's key.
259 * @node: the node to try adding.
260 *
261 * Return the node added upon success.
262 * Return the unique node already present upon failure. If
263 * lttng_ust_lfht_add_unique fails, the node passed as parameter should be
264 * freed by the caller. In this case, the caller does NOT need to wait
265 * for a grace period before freeing or re-using the node.
266 * Call with rcu_read_lock held.
267 * Threads calling this API need to be registered RCU read-side threads.
268 *
269 * The semantic of this function is that if only this function is used
270 * to add keys into the table, no duplicated keys should ever be
271 * observable in the table. The same guarantee apply for combination of
272 * add_unique and add_replace (see below).
273 *
274 * Upon success, this function issues a full memory barrier before and
275 * after its atomic commit. Upon failure, this function acts like a
276 * simple lookup operation: it acts as a rcu_dereference() to read the
277 * node pointer. The failure case does not guarantee any other memory
278 * barrier.
279 */
d2dd7baa
MJ
280LTTNG_HIDDEN
281extern struct lttng_ust_lfht_node *lttng_ust_lfht_add_unique(struct lttng_ust_lfht *ht,
10544ee8
MD
282 unsigned long hash,
283 lttng_ust_lfht_match_fct match,
284 const void *key,
285 struct lttng_ust_lfht_node *node);
286
287/*
288 * lttng_ust_lfht_add_replace - replace or add a node within hash table.
289 * @ht: the hash table.
290 * @hash: the node's hash.
291 * @match: the key match function.
292 * @key: the node's key.
293 * @node: the node to add.
294 *
295 * Return the node replaced upon success. If no node matching the key
296 * was present, return NULL, which also means the operation succeeded.
297 * This replacement operation should never fail.
298 * Call with rcu_read_lock held.
299 * Threads calling this API need to be registered RCU read-side threads.
300 * After successful replacement, a grace period must be waited for before
301 * freeing or re-using the memory reserved for the returned node.
302 *
303 * The semantic of replacement vs lookups and traversals is the
304 * following: if lookups and traversals are performed between a key
305 * unique insertion and its removal, we guarantee that the lookups and
306 * traversals will always find exactly one instance of the key if it is
307 * replaced concurrently with the lookups.
308 *
309 * Providing this semantic allows us to ensure that replacement-only
310 * schemes will never generate duplicated keys. It also allows us to
311 * guarantee that a combination of add_replace and add_unique updates
312 * will never generate duplicated keys.
313 *
314 * This function issues a full memory barrier before and after its
315 * atomic commit.
316 */
d2dd7baa
MJ
317LTTNG_HIDDEN
318extern struct lttng_ust_lfht_node *lttng_ust_lfht_add_replace(struct lttng_ust_lfht *ht,
10544ee8
MD
319 unsigned long hash,
320 lttng_ust_lfht_match_fct match,
321 const void *key,
322 struct lttng_ust_lfht_node *node);
323
324/*
325 * lttng_ust_lfht_replace - replace a node pointed to by iter within hash table.
326 * @ht: the hash table.
327 * @old_iter: the iterator position of the node to replace.
328 * @hash: the node's hash.
329 * @match: the key match function.
330 * @key: the node's key.
331 * @new_node: the new node to use as replacement.
332 *
333 * Return 0 if replacement is successful, negative value otherwise.
334 * Replacing a NULL old node or an already removed node will fail with
335 * -ENOENT.
336 * If the hash or value of the node to replace and the new node differ,
337 * this function returns -EINVAL without proceeding to the replacement.
338 * Old node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next.
339 * RCU read-side lock must be held between lookup and replacement.
340 * Call with rcu_read_lock held.
341 * Threads calling this API need to be registered RCU read-side threads.
342 * After successful replacement, a grace period must be waited for before
343 * freeing or re-using the memory reserved for the old node (which can
344 * be accessed with lttng_ust_lfht_iter_get_node).
345 *
346 * The semantic of replacement vs lookups is the same as
347 * lttng_ust_lfht_add_replace().
348 *
349 * Upon success, this function issues a full memory barrier before and
350 * after its atomic commit. Upon failure, this function does not issue
351 * any memory barrier.
352 */
d2dd7baa
MJ
353LTTNG_HIDDEN
354extern int lttng_ust_lfht_replace(struct lttng_ust_lfht *ht,
10544ee8
MD
355 struct lttng_ust_lfht_iter *old_iter,
356 unsigned long hash,
357 lttng_ust_lfht_match_fct match,
358 const void *key,
359 struct lttng_ust_lfht_node *new_node);
360
361/*
362 * lttng_ust_lfht_del - remove node pointed to by iterator from hash table.
363 * @ht: the hash table.
364 * @node: the node to delete.
365 *
366 * Return 0 if the node is successfully removed, negative value
367 * otherwise.
368 * Deleting a NULL node or an already removed node will fail with a
369 * negative value.
370 * Node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next,
371 * followed by use of lttng_ust_lfht_iter_get_node.
372 * RCU read-side lock must be held between lookup and removal.
373 * Call with rcu_read_lock held.
374 * Threads calling this API need to be registered RCU read-side threads.
375 * After successful removal, a grace period must be waited for before
376 * freeing or re-using the memory reserved for old node (which can be
377 * accessed with lttng_ust_lfht_iter_get_node).
378 * Upon success, this function issues a full memory barrier before and
379 * after its atomic commit. Upon failure, this function does not issue
380 * any memory barrier.
381 */
d2dd7baa
MJ
382LTTNG_HIDDEN
383extern int lttng_ust_lfht_del(struct lttng_ust_lfht *ht, struct lttng_ust_lfht_node *node);
10544ee8
MD
384
385/*
386 * lttng_ust_lfht_is_node_deleted - query whether a node is removed from hash table.
387 *
388 * Return non-zero if the node is deleted from the hash table, 0
389 * otherwise.
390 * Node can be looked up with lttng_ust_lfht_lookup and lttng_ust_lfht_next,
391 * followed by use of lttng_ust_lfht_iter_get_node.
392 * RCU read-side lock must be held between lookup and call to this
393 * function.
394 * Call with rcu_read_lock held.
395 * Threads calling this API need to be registered RCU read-side threads.
396 * This function does not issue any memory barrier.
397 */
d2dd7baa
MJ
398LTTNG_HIDDEN
399extern int lttng_ust_lfht_is_node_deleted(const struct lttng_ust_lfht_node *node);
10544ee8
MD
400
401/*
402 * lttng_ust_lfht_resize - Force a hash table resize
403 * @ht: the hash table.
404 * @new_size: update to this hash table size.
405 *
406 * Threads calling this API need to be registered RCU read-side threads.
407 * This function does not (necessarily) issue memory barriers.
408 * lttng_ust_lfht_resize should *not* be called from a RCU read-side critical
409 * section.
410 */
d2dd7baa
MJ
411LTTNG_HIDDEN
412extern void lttng_ust_lfht_resize(struct lttng_ust_lfht *ht, unsigned long new_size);
10544ee8
MD
413
414/*
415 * Note: it is safe to perform element removal (del), replacement, or
416 * any hash table update operation during any of the following hash
417 * table traversals.
418 * These functions act as rcu_dereference() to read the node pointers.
419 */
420#define lttng_ust_lfht_for_each(ht, iter, node) \
421 for (lttng_ust_lfht_first(ht, iter), \
422 node = lttng_ust_lfht_iter_get_node(iter); \
423 node != NULL; \
424 lttng_ust_lfht_next(ht, iter), \
425 node = lttng_ust_lfht_iter_get_node(iter))
426
427#define lttng_ust_lfht_for_each_duplicate(ht, hash, match, key, iter, node) \
428 for (lttng_ust_lfht_lookup(ht, hash, match, key, iter), \
429 node = lttng_ust_lfht_iter_get_node(iter); \
430 node != NULL; \
431 lttng_ust_lfht_next_duplicate(ht, match, key, iter), \
432 node = lttng_ust_lfht_iter_get_node(iter))
433
434#define lttng_ust_lfht_for_each_entry(ht, iter, pos, member) \
435 for (lttng_ust_lfht_first(ht, iter), \
436 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
437 __typeof__(*(pos)), member); \
438 lttng_ust_lfht_iter_get_node(iter) != NULL; \
439 lttng_ust_lfht_next(ht, iter), \
440 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
441 __typeof__(*(pos)), member))
442
443#define lttng_ust_lfht_for_each_entry_duplicate(ht, hash, match, key, \
444 iter, pos, member) \
445 for (lttng_ust_lfht_lookup(ht, hash, match, key, iter), \
446 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
447 __typeof__(*(pos)), member); \
448 lttng_ust_lfht_iter_get_node(iter) != NULL; \
449 lttng_ust_lfht_next_duplicate(ht, match, key, iter), \
450 pos = caa_container_of(lttng_ust_lfht_iter_get_node(iter), \
451 __typeof__(*(pos)), member))
452
453#ifdef __cplusplus
454}
455#endif
456
457#endif /* _LTTNG_UST_RCULFHASH_H */
This page took 0.039677 seconds and 4 git commands to generate.