subr_thmap.c revision 1.1 1 1.1 rmind /*-
2 1.1 rmind * Copyright (c) 2018 Mindaugas Rasiukevicius <rmind at noxt eu>
3 1.1 rmind * All rights reserved.
4 1.1 rmind *
5 1.1 rmind * Redistribution and use in source and binary forms, with or without
6 1.1 rmind * modification, are permitted provided that the following conditions
7 1.1 rmind * are met:
8 1.1 rmind * 1. Redistributions of source code must retain the above copyright
9 1.1 rmind * notice, this list of conditions and the following disclaimer.
10 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 rmind * notice, this list of conditions and the following disclaimer in the
12 1.1 rmind * documentation and/or other materials provided with the distribution.
13 1.1 rmind *
14 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 1.1 rmind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 1.1 rmind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 1.1 rmind * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 1.1 rmind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 1.1 rmind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 1.1 rmind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 1.1 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 1.1 rmind * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 1.1 rmind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 1.1 rmind * SUCH DAMAGE.
25 1.1 rmind *
26 1.1 rmind * Upstream: https://github.com/rmind/thmap/
27 1.1 rmind */
28 1.1 rmind
29 1.1 rmind /*
30 1.1 rmind * Concurrent trie-hash map.
31 1.1 rmind *
32 1.1 rmind * The data structure is conceptually a radix trie on hashed keys.
33 1.1 rmind * Keys are hashed using a 32-bit function. The root level is a special
34 1.1 rmind * case: it is managed using the compare-and-swap (CAS) atomic operation
35 1.1 rmind * and has a fanout of 64. The subsequent levels are constructed using
36 1.1 rmind * intermediate nodes with a fanout of 16 (using 4 bits). As more levels
37 1.1 rmind * are created, more blocks of the 32-bit hash value might be generated
38 1.1 rmind * by incrementing the seed parameter of the hash function.
39 1.1 rmind *
40 1.1 rmind * Concurrency
41 1.1 rmind *
42 1.1 rmind * - READERS: Descending is simply walking through the slot values of
43 1.1 rmind * the intermediate nodes. It is lock-free as there is no intermediate
44 1.1 rmind * state: the slot is either empty or has a pointer to the child node.
45 1.1 rmind * The main assumptions here are the following:
46 1.1 rmind *
47 1.1 rmind * i) modifications must preserve consistency with the respect to the
48 1.1 rmind * readers i.e. the readers can only see the valid node values;
49 1.1 rmind *
50 1.1 rmind * ii) any invalid view must "fail" the reads, e.g. by making them
51 1.1 rmind * re-try from the root; this is a case for deletions and is achieved
52 1.1 rmind * using the NODE_DELETED flag.
53 1.1 rmind *
54 1.1 rmind * iii) the node destruction must be synchronised with the readers,
55 1.1 rmind * e.g. by using the Epoch-based reclamation or other techniques.
56 1.1 rmind *
57 1.1 rmind * - WRITERS AND LOCKING: Each intermediate node has a spin-lock (which
58 1.1 rmind * is implemented using the NODE_LOCKED bit) -- it provides mutual
59 1.1 rmind * exclusion amongst concurrent writers. The lock order for the nodes
60 1.1 rmind * is "bottom-up" i.e. they are locked as we ascend the trie. A key
61 1.1 rmind * constraint here is that parent pointer never changes.
62 1.1 rmind *
63 1.1 rmind * - DELETES: In addition to writer's locking, the deletion keeps the
64 1.1 rmind * intermediate nodes in a valid state and sets the NODE_DELETED flag,
65 1.1 rmind * to indicate that the readers must re-start the walk from the root.
66 1.1 rmind * As the levels are collapsed, NODE_DELETED gets propagated up-tree.
67 1.1 rmind * The leaf nodes just stay as-is until they are reclaimed.
68 1.1 rmind *
69 1.1 rmind * - ROOT LEVEL: The root level is a special case, as it is implemented
70 1.1 rmind * as an array (rather than intermediate node). The root-level slot can
71 1.1 rmind * only be set using CAS and it can only be set to a valid intermediate
72 1.1 rmind * node. The root-level slot can only be cleared when the node it points
73 1.1 rmind * at becomes empty, is locked and marked as NODE_DELETED (this causes
74 1.1 rmind * the insert/delete operations to re-try until the slot is set to NULL).
75 1.1 rmind *
76 1.1 rmind * References:
77 1.1 rmind *
78 1.1 rmind * W. Litwin, 1981, Trie Hashing.
79 1.1 rmind * Proceedings of the 1981 ACM SIGMOD, p. 19-29
80 1.1 rmind * https://dl.acm.org/citation.cfm?id=582322
81 1.1 rmind *
82 1.1 rmind * P. L. Lehman and S. B. Yao.
83 1.1 rmind * Efficient locking for concurrent operations on B-trees.
84 1.1 rmind * ACM TODS, 6(4):650-670, 1981
85 1.1 rmind * https://www.csd.uoc.gr/~hy460/pdf/p650-lehman.pdf
86 1.1 rmind */
87 1.1 rmind
88 1.1 rmind #ifdef _KERNEL
89 1.1 rmind #include <sys/cdefs.h>
90 1.1 rmind #include <sys/param.h>
91 1.1 rmind #include <sys/types.h>
92 1.1 rmind #include <sys/thmap.h>
93 1.1 rmind #include <sys/kmem.h>
94 1.1 rmind #include <sys/lock.h>
95 1.1 rmind #include <sys/atomic.h>
96 1.1 rmind #include <sys/hash.h>
97 1.1 rmind #else
98 1.1 rmind #include <stdio.h>
99 1.1 rmind #include <stdlib.h>
100 1.1 rmind #include <stdbool.h>
101 1.1 rmind #include <stddef.h>
102 1.1 rmind #include <inttypes.h>
103 1.1 rmind #include <string.h>
104 1.1 rmind #include <limits.h>
105 1.1 rmind
106 1.1 rmind #include "thmap.h"
107 1.1 rmind #include "utils.h"
108 1.1 rmind #endif
109 1.1 rmind
110 1.1 rmind /*
111 1.1 rmind * NetBSD kernel wrappers
112 1.1 rmind */
113 1.1 rmind #ifdef _KERNEL
114 1.1 rmind #define ASSERT KASSERT
115 1.1 rmind #define DEBUG 1
116 1.1 rmind #define atomic_thread_fence(x) x
117 1.1 rmind #define memory_order_stores membar_producer()
118 1.1 rmind #define memory_order_loads membar_consumer()
119 1.1 rmind #define atomic_cas_32_p(p, e, n) (atomic_cas_32((p), (e), (n)) == (e))
120 1.1 rmind #define atomic_cas_ptr_p(p, e, n) \
121 1.1 rmind (atomic_cas_ptr((p), (void *)(e), (void *)(n)) == (e))
122 1.1 rmind #define atomic_exchange atomic_swap_ptr
123 1.1 rmind #define murmurhash3 murmurhash2
124 1.1 rmind #endif
125 1.1 rmind
126 1.1 rmind /*
127 1.1 rmind * The root level fanout is 64 (indexed by the last 6 bits of the hash
128 1.1 rmind * value XORed with the length). Each subsequent level, represented by
129 1.1 rmind * intermediate nodes, has a fanout of 16 (using 4 bits).
130 1.1 rmind *
131 1.1 rmind * The hash function produces 32-bit values.
132 1.1 rmind */
133 1.1 rmind
134 1.1 rmind #define HASHVAL_BITS (32)
135 1.1 rmind #define HASHVAL_MOD (HASHVAL_BITS - 1)
136 1.1 rmind #define HASHVAL_SHIFT (5)
137 1.1 rmind
138 1.1 rmind #define ROOT_BITS (6)
139 1.1 rmind #define ROOT_SIZE (1 << ROOT_BITS)
140 1.1 rmind #define ROOT_MASK (ROOT_SIZE - 1)
141 1.1 rmind #define ROOT_MSBITS (HASHVAL_BITS - ROOT_BITS)
142 1.1 rmind
143 1.1 rmind #define LEVEL_BITS (4)
144 1.1 rmind #define LEVEL_SIZE (1 << LEVEL_BITS)
145 1.1 rmind #define LEVEL_MASK (LEVEL_SIZE - 1)
146 1.1 rmind
147 1.1 rmind /*
148 1.1 rmind * Instead of raw pointers, we use offsets from the base address.
149 1.1 rmind * This accommodates the use of this data structure in shared memory,
150 1.1 rmind * where mappings can be in different address spaces.
151 1.1 rmind *
152 1.1 rmind * The pointers must be aligned, since pointer tagging is used to
153 1.1 rmind * differentiate the intermediate nodes from leaves. We reserve the
154 1.1 rmind * least significant bit.
155 1.1 rmind */
156 1.1 rmind typedef uintptr_t thmap_ptr_t;
157 1.1 rmind
158 1.1 rmind #define THMAP_NULL ((thmap_ptr_t)0)
159 1.1 rmind
160 1.1 rmind #define THMAP_LEAF_BIT (0x1)
161 1.1 rmind
162 1.1 rmind #define THMAP_ALIGNED_P(p) (((uintptr_t)(p) & 3) == 0)
163 1.1 rmind #define THMAP_ALIGN(p) ((uintptr_t)(p) & ~(uintptr_t)3)
164 1.1 rmind #define THMAP_INODE_P(p) (((uintptr_t)(p) & THMAP_LEAF_BIT) == 0)
165 1.1 rmind
166 1.1 rmind #define THMAP_GETPTR(th, p) ((void *)((th)->baseptr + (uintptr_t)(p)))
167 1.1 rmind #define THMAP_GETOFF(th, p) ((thmap_ptr_t)((uintptr_t)(p) - (th)->baseptr))
168 1.1 rmind #define THMAP_NODE(th, p) THMAP_GETPTR(th, THMAP_ALIGN(p))
169 1.1 rmind
170 1.1 rmind /*
171 1.1 rmind * State field.
172 1.1 rmind */
173 1.1 rmind
174 1.1 rmind #define NODE_LOCKED (1U << 31) // lock (writers)
175 1.1 rmind #define NODE_DELETED (1U << 30) // node deleted
176 1.1 rmind #define NODE_COUNT(s) ((s) & 0x3fffffff) // slot count mask
177 1.1 rmind
178 1.1 rmind /*
179 1.1 rmind * There are two types of nodes:
180 1.1 rmind * - Intermediate nodes -- arrays pointing to another level or a leaf;
181 1.1 rmind * - Leaves, which store a key-value pair.
182 1.1 rmind */
183 1.1 rmind
184 1.1 rmind typedef struct {
185 1.1 rmind uint32_t state;
186 1.1 rmind thmap_ptr_t parent;
187 1.1 rmind thmap_ptr_t slots[LEVEL_SIZE];
188 1.1 rmind } thmap_inode_t;
189 1.1 rmind
190 1.1 rmind #define THMAP_INODE_LEN sizeof(thmap_inode_t)
191 1.1 rmind
192 1.1 rmind typedef struct {
193 1.1 rmind thmap_ptr_t key;
194 1.1 rmind size_t len;
195 1.1 rmind void * val;
196 1.1 rmind } thmap_leaf_t;
197 1.1 rmind
198 1.1 rmind typedef struct {
199 1.1 rmind unsigned rslot; // root-level slot index
200 1.1 rmind unsigned level; // current level in the tree
201 1.1 rmind unsigned hashidx; // current hash index (block of bits)
202 1.1 rmind uint32_t hashval; // current hash value
203 1.1 rmind } thmap_query_t;
204 1.1 rmind
205 1.1 rmind typedef struct {
206 1.1 rmind uintptr_t addr;
207 1.1 rmind size_t len;
208 1.1 rmind void * next;
209 1.1 rmind } thmap_gc_t;
210 1.1 rmind
211 1.1 rmind #define THMAP_ROOT_LEN (sizeof(thmap_ptr_t) * ROOT_SIZE)
212 1.1 rmind
213 1.1 rmind struct thmap {
214 1.1 rmind uintptr_t baseptr;
215 1.1 rmind thmap_ptr_t * root;
216 1.1 rmind unsigned flags;
217 1.1 rmind const thmap_ops_t *ops;
218 1.1 rmind thmap_gc_t * gc_list;
219 1.1 rmind };
220 1.1 rmind
221 1.1 rmind static void stage_mem_gc(thmap_t *, uintptr_t, size_t);
222 1.1 rmind
223 1.1 rmind /*
224 1.1 rmind * A few low-level helper routines.
225 1.1 rmind */
226 1.1 rmind
227 1.1 rmind static uintptr_t
228 1.1 rmind alloc_wrapper(size_t len)
229 1.1 rmind {
230 1.1 rmind return (uintptr_t)kmem_intr_alloc(len, KM_SLEEP);
231 1.1 rmind }
232 1.1 rmind
233 1.1 rmind static void
234 1.1 rmind free_wrapper(uintptr_t addr, size_t len)
235 1.1 rmind {
236 1.1 rmind kmem_intr_free((void *)addr, len);
237 1.1 rmind }
238 1.1 rmind
239 1.1 rmind static const thmap_ops_t thmap_default_ops = {
240 1.1 rmind .alloc = alloc_wrapper,
241 1.1 rmind .free = free_wrapper
242 1.1 rmind };
243 1.1 rmind
244 1.1 rmind /*
245 1.1 rmind * NODE LOCKING.
246 1.1 rmind */
247 1.1 rmind
248 1.1 rmind #ifdef DEBUG
249 1.1 rmind static inline bool
250 1.1 rmind node_locked_p(const thmap_inode_t *node)
251 1.1 rmind {
252 1.1 rmind return (node->state & NODE_LOCKED) != 0;
253 1.1 rmind }
254 1.1 rmind #endif
255 1.1 rmind
256 1.1 rmind static void
257 1.1 rmind lock_node(thmap_inode_t *node)
258 1.1 rmind {
259 1.1 rmind unsigned bcount = SPINLOCK_BACKOFF_MIN;
260 1.1 rmind uint32_t s;
261 1.1 rmind again:
262 1.1 rmind s = node->state;
263 1.1 rmind if (s & NODE_LOCKED) {
264 1.1 rmind SPINLOCK_BACKOFF(bcount);
265 1.1 rmind goto again;
266 1.1 rmind }
267 1.1 rmind /*
268 1.1 rmind * CAS will issue a full memory fence for us.
269 1.1 rmind *
270 1.1 rmind * WARNING: for optimisations purposes, callers rely on us
271 1.1 rmind * issuing load and store fence
272 1.1 rmind */
273 1.1 rmind if (!atomic_cas_32_p(&node->state, s, s | NODE_LOCKED)) {
274 1.1 rmind bcount = SPINLOCK_BACKOFF_MIN;
275 1.1 rmind goto again;
276 1.1 rmind }
277 1.1 rmind }
278 1.1 rmind
279 1.1 rmind static void
280 1.1 rmind unlock_node(thmap_inode_t *node)
281 1.1 rmind {
282 1.1 rmind uint32_t s = node->state & ~NODE_LOCKED;
283 1.1 rmind
284 1.1 rmind ASSERT(node_locked_p(node));
285 1.1 rmind atomic_thread_fence(memory_order_stores);
286 1.1 rmind node->state = s; // atomic store
287 1.1 rmind }
288 1.1 rmind
289 1.1 rmind /*
290 1.1 rmind * HASH VALUE AND KEY OPERATIONS.
291 1.1 rmind */
292 1.1 rmind
293 1.1 rmind static inline void
294 1.1 rmind hashval_init(thmap_query_t *query, const void * restrict key, size_t len)
295 1.1 rmind {
296 1.1 rmind const uint32_t hashval = murmurhash3(key, len, 0);
297 1.1 rmind
298 1.1 rmind query->rslot = ((hashval >> ROOT_MSBITS) ^ len) & ROOT_MASK;
299 1.1 rmind query->level = 0;
300 1.1 rmind query->hashval = hashval;
301 1.1 rmind query->hashidx = 0;
302 1.1 rmind }
303 1.1 rmind
304 1.1 rmind /*
305 1.1 rmind * hashval_getslot: given the key, compute the hash (if not already cached)
306 1.1 rmind * and return the offset for the current level.
307 1.1 rmind */
308 1.1 rmind static unsigned
309 1.1 rmind hashval_getslot(thmap_query_t *query, const void * restrict key, size_t len)
310 1.1 rmind {
311 1.1 rmind const unsigned offset = query->level * LEVEL_BITS;
312 1.1 rmind const unsigned shift = offset & HASHVAL_MOD;
313 1.1 rmind const unsigned i = offset >> HASHVAL_SHIFT;
314 1.1 rmind
315 1.1 rmind if (query->hashidx != i) {
316 1.1 rmind /* Generate a hash value for a required range. */
317 1.1 rmind query->hashval = murmurhash3(key, len, i);
318 1.1 rmind query->hashidx = i;
319 1.1 rmind }
320 1.1 rmind return (query->hashval >> shift) & LEVEL_MASK;
321 1.1 rmind }
322 1.1 rmind
323 1.1 rmind static unsigned
324 1.1 rmind hashval_getleafslot(const thmap_t *thmap,
325 1.1 rmind const thmap_leaf_t *leaf, unsigned level)
326 1.1 rmind {
327 1.1 rmind const void *key = THMAP_GETPTR(thmap, leaf->key);
328 1.1 rmind const unsigned offset = level * LEVEL_BITS;
329 1.1 rmind const unsigned shift = offset & HASHVAL_MOD;
330 1.1 rmind const unsigned i = offset >> HASHVAL_SHIFT;
331 1.1 rmind
332 1.1 rmind return (murmurhash3(key, leaf->len, i) >> shift) & LEVEL_MASK;
333 1.1 rmind }
334 1.1 rmind
335 1.1 rmind static inline unsigned
336 1.1 rmind hashval_getl0slot(const thmap_t *thmap, const thmap_query_t *query,
337 1.1 rmind const thmap_leaf_t *leaf)
338 1.1 rmind {
339 1.1 rmind if (__predict_true(query->hashidx == 0)) {
340 1.1 rmind return query->hashval & LEVEL_MASK;
341 1.1 rmind }
342 1.1 rmind return hashval_getleafslot(thmap, leaf, 0);
343 1.1 rmind }
344 1.1 rmind
345 1.1 rmind static bool
346 1.1 rmind key_cmp_p(const thmap_t *thmap, const thmap_leaf_t *leaf,
347 1.1 rmind const void * restrict key, size_t len)
348 1.1 rmind {
349 1.1 rmind const void *leafkey = THMAP_GETPTR(thmap, leaf->key);
350 1.1 rmind return len == leaf->len && memcmp(key, leafkey, len) == 0;
351 1.1 rmind }
352 1.1 rmind
353 1.1 rmind /*
354 1.1 rmind * INTER-NODE OPERATIONS.
355 1.1 rmind */
356 1.1 rmind
357 1.1 rmind static thmap_inode_t *
358 1.1 rmind node_create(thmap_t *thmap, thmap_inode_t *parent)
359 1.1 rmind {
360 1.1 rmind thmap_inode_t *node;
361 1.1 rmind uintptr_t p;
362 1.1 rmind
363 1.1 rmind p = thmap->ops->alloc(THMAP_INODE_LEN);
364 1.1 rmind if (!p) {
365 1.1 rmind return NULL;
366 1.1 rmind }
367 1.1 rmind node = THMAP_GETPTR(thmap, p);
368 1.1 rmind ASSERT(THMAP_ALIGNED_P(node));
369 1.1 rmind
370 1.1 rmind memset(node, 0, THMAP_INODE_LEN);
371 1.1 rmind if (parent) {
372 1.1 rmind node->state = NODE_LOCKED;
373 1.1 rmind node->parent = THMAP_GETOFF(thmap, parent);
374 1.1 rmind }
375 1.1 rmind return node;
376 1.1 rmind }
377 1.1 rmind
378 1.1 rmind static void
379 1.1 rmind node_insert(thmap_inode_t *node, unsigned slot, thmap_ptr_t child)
380 1.1 rmind {
381 1.1 rmind ASSERT(node_locked_p(node) || node->parent == THMAP_NULL);
382 1.1 rmind ASSERT((node->state & NODE_DELETED) == 0);
383 1.1 rmind ASSERT(node->slots[slot] == THMAP_NULL);
384 1.1 rmind
385 1.1 rmind ASSERT(NODE_COUNT(node->state) < LEVEL_SIZE);
386 1.1 rmind
387 1.1 rmind node->slots[slot] = child;
388 1.1 rmind node->state++;
389 1.1 rmind }
390 1.1 rmind
391 1.1 rmind static void
392 1.1 rmind node_remove(thmap_inode_t *node, unsigned slot)
393 1.1 rmind {
394 1.1 rmind ASSERT(node_locked_p(node));
395 1.1 rmind ASSERT((node->state & NODE_DELETED) == 0);
396 1.1 rmind ASSERT(node->slots[slot] != THMAP_NULL);
397 1.1 rmind
398 1.1 rmind ASSERT(NODE_COUNT(node->state) > 0);
399 1.1 rmind ASSERT(NODE_COUNT(node->state) <= LEVEL_SIZE);
400 1.1 rmind
401 1.1 rmind node->slots[slot] = THMAP_NULL;
402 1.1 rmind node->state--;
403 1.1 rmind }
404 1.1 rmind
405 1.1 rmind /*
406 1.1 rmind * LEAF OPERATIONS.
407 1.1 rmind */
408 1.1 rmind
409 1.1 rmind static thmap_leaf_t *
410 1.1 rmind leaf_create(const thmap_t *thmap, const void *key, size_t len, void *val)
411 1.1 rmind {
412 1.1 rmind thmap_leaf_t *leaf;
413 1.1 rmind uintptr_t leaf_off, key_off;
414 1.1 rmind
415 1.1 rmind leaf_off = thmap->ops->alloc(sizeof(thmap_leaf_t));
416 1.1 rmind if (!leaf_off) {
417 1.1 rmind return NULL;
418 1.1 rmind }
419 1.1 rmind leaf = THMAP_GETPTR(thmap, leaf_off);
420 1.1 rmind ASSERT(THMAP_ALIGNED_P(leaf));
421 1.1 rmind
422 1.1 rmind if ((thmap->flags & THMAP_NOCOPY) == 0) {
423 1.1 rmind /*
424 1.1 rmind * Copy the key.
425 1.1 rmind */
426 1.1 rmind key_off = thmap->ops->alloc(len);
427 1.1 rmind if (!key_off) {
428 1.1 rmind thmap->ops->free(leaf_off, sizeof(thmap_leaf_t));
429 1.1 rmind return NULL;
430 1.1 rmind }
431 1.1 rmind memcpy(THMAP_GETPTR(thmap, key_off), key, len);
432 1.1 rmind leaf->key = key_off;
433 1.1 rmind } else {
434 1.1 rmind /* Otherwise, we use a reference. */
435 1.1 rmind leaf->key = (uintptr_t)key;
436 1.1 rmind }
437 1.1 rmind leaf->len = len;
438 1.1 rmind leaf->val = val;
439 1.1 rmind return leaf;
440 1.1 rmind }
441 1.1 rmind
442 1.1 rmind static void
443 1.1 rmind leaf_free(const thmap_t *thmap, thmap_leaf_t *leaf)
444 1.1 rmind {
445 1.1 rmind if ((thmap->flags & THMAP_NOCOPY) == 0) {
446 1.1 rmind thmap->ops->free(leaf->key, leaf->len);
447 1.1 rmind }
448 1.1 rmind thmap->ops->free(THMAP_GETOFF(thmap, leaf), sizeof(thmap_leaf_t));
449 1.1 rmind }
450 1.1 rmind
451 1.1 rmind static thmap_leaf_t *
452 1.1 rmind get_leaf(const thmap_t *thmap, thmap_inode_t *parent, unsigned slot)
453 1.1 rmind {
454 1.1 rmind thmap_ptr_t node;
455 1.1 rmind
456 1.1 rmind node = parent->slots[slot];
457 1.1 rmind if (THMAP_INODE_P(node)) {
458 1.1 rmind return NULL;
459 1.1 rmind }
460 1.1 rmind return THMAP_NODE(thmap, node);
461 1.1 rmind }
462 1.1 rmind
463 1.1 rmind /*
464 1.1 rmind * ROOT OPERATIONS.
465 1.1 rmind */
466 1.1 rmind
467 1.1 rmind static inline bool
468 1.1 rmind root_try_put(thmap_t *thmap, const thmap_query_t *query, thmap_leaf_t *leaf)
469 1.1 rmind {
470 1.1 rmind const unsigned i = query->rslot;
471 1.1 rmind thmap_inode_t *node;
472 1.1 rmind thmap_ptr_t nptr;
473 1.1 rmind unsigned slot;
474 1.1 rmind
475 1.1 rmind /*
476 1.1 rmind * Must pre-check first.
477 1.1 rmind */
478 1.1 rmind if (thmap->root[i]) {
479 1.1 rmind return false;
480 1.1 rmind }
481 1.1 rmind
482 1.1 rmind /*
483 1.1 rmind * Create an intermediate node. Since there is no parent set,
484 1.1 rmind * it will be created unlocked and the CAS operation will issue
485 1.1 rmind * the store memory fence for us.
486 1.1 rmind */
487 1.1 rmind node = node_create(thmap, NULL);
488 1.1 rmind slot = hashval_getl0slot(thmap, query, leaf);
489 1.1 rmind node_insert(node, slot, THMAP_GETOFF(thmap, leaf) | THMAP_LEAF_BIT);
490 1.1 rmind nptr = THMAP_GETOFF(thmap, node);
491 1.1 rmind again:
492 1.1 rmind if (thmap->root[i]) {
493 1.1 rmind thmap->ops->free(nptr, THMAP_INODE_LEN);
494 1.1 rmind return false;
495 1.1 rmind }
496 1.1 rmind if (!atomic_cas_ptr_p(&thmap->root[i], THMAP_NULL, nptr)) {
497 1.1 rmind goto again;
498 1.1 rmind }
499 1.1 rmind return true;
500 1.1 rmind }
501 1.1 rmind
502 1.1 rmind /*
503 1.1 rmind * find_edge_node: given the hash, traverse the tree to find the edge node.
504 1.1 rmind *
505 1.1 rmind * => Returns an aligned (clean) pointer to the parent node.
506 1.1 rmind * => Returns the slot number and sets current level.
507 1.1 rmind */
508 1.1 rmind static thmap_inode_t *
509 1.1 rmind find_edge_node(const thmap_t *thmap, thmap_query_t *query,
510 1.1 rmind const void * restrict key, size_t len, unsigned *slot)
511 1.1 rmind {
512 1.1 rmind thmap_ptr_t root_slot = thmap->root[query->rslot];
513 1.1 rmind thmap_inode_t *parent;
514 1.1 rmind thmap_ptr_t node;
515 1.1 rmind unsigned off;
516 1.1 rmind
517 1.1 rmind ASSERT(query->level == 0);
518 1.1 rmind
519 1.1 rmind parent = THMAP_NODE(thmap, root_slot);
520 1.1 rmind if (!parent) {
521 1.1 rmind return NULL;
522 1.1 rmind }
523 1.1 rmind descend:
524 1.1 rmind off = hashval_getslot(query, key, len);
525 1.1 rmind node = parent->slots[off];
526 1.1 rmind
527 1.1 rmind /* Ensure the parent load happens before the child load. */
528 1.1 rmind atomic_thread_fence(memory_order_loads);
529 1.1 rmind
530 1.1 rmind /* Descend the tree until we find a leaf or empty slot. */
531 1.1 rmind if (node && THMAP_INODE_P(node)) {
532 1.1 rmind parent = THMAP_NODE(thmap, node);
533 1.1 rmind query->level++;
534 1.1 rmind goto descend;
535 1.1 rmind }
536 1.1 rmind if (parent->state & NODE_DELETED) {
537 1.1 rmind return NULL;
538 1.1 rmind }
539 1.1 rmind *slot = off;
540 1.1 rmind return parent;
541 1.1 rmind }
542 1.1 rmind
543 1.1 rmind /*
544 1.1 rmind * find_edge_node_locked: traverse the tree, like find_edge_node(),
545 1.1 rmind * but attempt to lock the edge node.
546 1.1 rmind *
547 1.1 rmind * => Returns NULL if the deleted node is found. This indicates that
548 1.1 rmind * the caller must re-try from the root, as the root slot might have
549 1.1 rmind * changed too.
550 1.1 rmind */
551 1.1 rmind static thmap_inode_t *
552 1.1 rmind find_edge_node_locked(const thmap_t *thmap, thmap_query_t *query,
553 1.1 rmind const void * restrict key, size_t len, unsigned *slot)
554 1.1 rmind {
555 1.1 rmind thmap_inode_t *node;
556 1.1 rmind thmap_ptr_t target;
557 1.1 rmind retry:
558 1.1 rmind /*
559 1.1 rmind * Find the edge node and lock it! Re-check the state since
560 1.1 rmind * the tree might change by the time we acquire the lock.
561 1.1 rmind */
562 1.1 rmind node = find_edge_node(thmap, query, key, len, slot);
563 1.1 rmind if (!node) {
564 1.1 rmind /* The root slot is empty -- let the caller decide. */
565 1.1 rmind query->level = 0;
566 1.1 rmind return NULL;
567 1.1 rmind }
568 1.1 rmind lock_node(node);
569 1.1 rmind if (__predict_false(node->state & NODE_DELETED)) {
570 1.1 rmind /*
571 1.1 rmind * The node has been deleted. The tree might have a new
572 1.1 rmind * shape now, therefore we must re-start from the root.
573 1.1 rmind */
574 1.1 rmind unlock_node(node);
575 1.1 rmind query->level = 0;
576 1.1 rmind return NULL;
577 1.1 rmind }
578 1.1 rmind target = node->slots[*slot];
579 1.1 rmind if (__predict_false(target && THMAP_INODE_P(target))) {
580 1.1 rmind /*
581 1.1 rmind * The target slot has been changed and it is now an
582 1.1 rmind * intermediate node. Re-start from the top internode.
583 1.1 rmind */
584 1.1 rmind unlock_node(node);
585 1.1 rmind query->level = 0;
586 1.1 rmind goto retry;
587 1.1 rmind }
588 1.1 rmind return node;
589 1.1 rmind }
590 1.1 rmind
591 1.1 rmind /*
592 1.1 rmind * thmap_get: lookup a value given the key.
593 1.1 rmind */
594 1.1 rmind void *
595 1.1 rmind thmap_get(thmap_t *thmap, const void *key, size_t len)
596 1.1 rmind {
597 1.1 rmind thmap_query_t query;
598 1.1 rmind thmap_inode_t *parent;
599 1.1 rmind thmap_leaf_t *leaf;
600 1.1 rmind unsigned slot;
601 1.1 rmind
602 1.1 rmind hashval_init(&query, key, len);
603 1.1 rmind parent = find_edge_node(thmap, &query, key, len, &slot);
604 1.1 rmind if (!parent) {
605 1.1 rmind return NULL;
606 1.1 rmind }
607 1.1 rmind leaf = get_leaf(thmap, parent, slot);
608 1.1 rmind if (!leaf) {
609 1.1 rmind return NULL;
610 1.1 rmind }
611 1.1 rmind if (!key_cmp_p(thmap, leaf, key, len)) {
612 1.1 rmind return NULL;
613 1.1 rmind }
614 1.1 rmind return leaf->val;
615 1.1 rmind }
616 1.1 rmind
617 1.1 rmind /*
618 1.1 rmind * thmap_put: insert a value given the key.
619 1.1 rmind *
620 1.1 rmind * => If the key is already present, return the associated value.
621 1.1 rmind * => Otherwise, on successful insert, return the given value.
622 1.1 rmind */
623 1.1 rmind void *
624 1.1 rmind thmap_put(thmap_t *thmap, const void *key, size_t len, void *val)
625 1.1 rmind {
626 1.1 rmind thmap_query_t query;
627 1.1 rmind thmap_leaf_t *leaf, *other;
628 1.1 rmind thmap_inode_t *parent, *child;
629 1.1 rmind unsigned slot, other_slot;
630 1.1 rmind thmap_ptr_t target;
631 1.1 rmind
632 1.1 rmind /*
633 1.1 rmind * First, pre-allocate and initialise the leaf node.
634 1.1 rmind *
635 1.1 rmind * NOTE: locking of the edge node below will issue the
636 1.1 rmind * store fence for us.
637 1.1 rmind */
638 1.1 rmind leaf = leaf_create(thmap, key, len, val);
639 1.1 rmind if (__predict_false(!leaf)) {
640 1.1 rmind return NULL;
641 1.1 rmind }
642 1.1 rmind hashval_init(&query, key, len);
643 1.1 rmind retry:
644 1.1 rmind /*
645 1.1 rmind * Try to insert into the root first, if its slot is empty.
646 1.1 rmind */
647 1.1 rmind if (root_try_put(thmap, &query, leaf)) {
648 1.1 rmind /* Success: the leaf was inserted; no locking involved. */
649 1.1 rmind return val;
650 1.1 rmind }
651 1.1 rmind
652 1.1 rmind /*
653 1.1 rmind * Find the edge node and the target slot.
654 1.1 rmind */
655 1.1 rmind parent = find_edge_node_locked(thmap, &query, key, len, &slot);
656 1.1 rmind if (!parent) {
657 1.1 rmind goto retry;
658 1.1 rmind }
659 1.1 rmind target = parent->slots[slot]; // tagged offset
660 1.1 rmind if (THMAP_INODE_P(target)) {
661 1.1 rmind /*
662 1.1 rmind * Empty slot: simply insert the new leaf. The store
663 1.1 rmind * fence is already issued for us.
664 1.1 rmind */
665 1.1 rmind target = THMAP_GETOFF(thmap, leaf) | THMAP_LEAF_BIT;
666 1.1 rmind node_insert(parent, slot, target);
667 1.1 rmind goto out;
668 1.1 rmind }
669 1.1 rmind
670 1.1 rmind /*
671 1.1 rmind * Collision or duplicate.
672 1.1 rmind */
673 1.1 rmind other = THMAP_NODE(thmap, target);
674 1.1 rmind if (key_cmp_p(thmap, other, key, len)) {
675 1.1 rmind /*
676 1.1 rmind * Duplicate. Free the pre-allocated leaf and
677 1.1 rmind * return the present value.
678 1.1 rmind */
679 1.1 rmind leaf_free(thmap, leaf);
680 1.1 rmind val = other->val;
681 1.1 rmind goto out;
682 1.1 rmind }
683 1.1 rmind descend:
684 1.1 rmind /*
685 1.1 rmind * Collision -- expand the tree. Create an intermediate node
686 1.1 rmind * which will be locked (NODE_LOCKED) for us. At this point,
687 1.1 rmind * we advance to the next level.
688 1.1 rmind */
689 1.1 rmind child = node_create(thmap, parent);
690 1.1 rmind if (__predict_false(!child)) {
691 1.1 rmind leaf_free(thmap, leaf);
692 1.1 rmind val = NULL;
693 1.1 rmind goto out;
694 1.1 rmind }
695 1.1 rmind query.level++;
696 1.1 rmind
697 1.1 rmind /*
698 1.1 rmind * Insert the other (colliding) leaf first.
699 1.1 rmind */
700 1.1 rmind other_slot = hashval_getleafslot(thmap, other, query.level);
701 1.1 rmind target = THMAP_GETOFF(thmap, other) | THMAP_LEAF_BIT;
702 1.1 rmind node_insert(child, other_slot, target);
703 1.1 rmind
704 1.1 rmind /*
705 1.1 rmind * Insert the intermediate node into the parent node.
706 1.1 rmind * It becomes the new parent for the our new leaf.
707 1.1 rmind *
708 1.1 rmind * Ensure that stores to the child (and leaf) reach the
709 1.1 rmind * global visibility before it gets inserted to the parent.
710 1.1 rmind */
711 1.1 rmind atomic_thread_fence(memory_order_stores);
712 1.1 rmind parent->slots[slot] = THMAP_GETOFF(thmap, child);
713 1.1 rmind
714 1.1 rmind unlock_node(parent);
715 1.1 rmind ASSERT(node_locked_p(child));
716 1.1 rmind parent = child;
717 1.1 rmind
718 1.1 rmind /*
719 1.1 rmind * Get the new slot and check for another collision
720 1.1 rmind * at the next level.
721 1.1 rmind */
722 1.1 rmind slot = hashval_getslot(&query, key, len);
723 1.1 rmind if (slot == other_slot) {
724 1.1 rmind /* Another collision -- descend and expand again. */
725 1.1 rmind goto descend;
726 1.1 rmind }
727 1.1 rmind
728 1.1 rmind /* Insert our new leaf once we expanded enough. */
729 1.1 rmind target = THMAP_GETOFF(thmap, leaf) | THMAP_LEAF_BIT;
730 1.1 rmind node_insert(parent, slot, target);
731 1.1 rmind out:
732 1.1 rmind unlock_node(parent);
733 1.1 rmind return val;
734 1.1 rmind }
735 1.1 rmind
736 1.1 rmind /*
737 1.1 rmind * thmap_del: remove the entry given the key.
738 1.1 rmind */
739 1.1 rmind void *
740 1.1 rmind thmap_del(thmap_t *thmap, const void *key, size_t len)
741 1.1 rmind {
742 1.1 rmind thmap_query_t query;
743 1.1 rmind thmap_leaf_t *leaf;
744 1.1 rmind thmap_inode_t *parent;
745 1.1 rmind unsigned slot;
746 1.1 rmind void *val;
747 1.1 rmind
748 1.1 rmind hashval_init(&query, key, len);
749 1.1 rmind parent = find_edge_node_locked(thmap, &query, key, len, &slot);
750 1.1 rmind if (!parent) {
751 1.1 rmind /* Root slot empty: not found. */
752 1.1 rmind return NULL;
753 1.1 rmind }
754 1.1 rmind leaf = get_leaf(thmap, parent, slot);
755 1.1 rmind if (!leaf || !key_cmp_p(thmap, leaf, key, len)) {
756 1.1 rmind /* Not found. */
757 1.1 rmind unlock_node(parent);
758 1.1 rmind return NULL;
759 1.1 rmind }
760 1.1 rmind
761 1.1 rmind /* Remove the leaf. */
762 1.1 rmind ASSERT(THMAP_NODE(thmap, parent->slots[slot]) == leaf);
763 1.1 rmind node_remove(parent, slot);
764 1.1 rmind
765 1.1 rmind /*
766 1.1 rmind * Collapse the levels if removing the last item.
767 1.1 rmind */
768 1.1 rmind while (query.level && NODE_COUNT(parent->state) == 0) {
769 1.1 rmind thmap_inode_t *node = parent;
770 1.1 rmind
771 1.1 rmind ASSERT(node->state == NODE_LOCKED);
772 1.1 rmind
773 1.1 rmind /*
774 1.1 rmind * Ascend one level up.
775 1.1 rmind * => Mark our current parent as deleted.
776 1.1 rmind * => Lock the parent one level up.
777 1.1 rmind */
778 1.1 rmind query.level--;
779 1.1 rmind slot = hashval_getslot(&query, key, len);
780 1.1 rmind parent = THMAP_NODE(thmap, node->parent);
781 1.1 rmind ASSERT(parent != NULL);
782 1.1 rmind
783 1.1 rmind lock_node(parent);
784 1.1 rmind ASSERT((parent->state & NODE_DELETED) == 0);
785 1.1 rmind
786 1.1 rmind node->state |= NODE_DELETED;
787 1.1 rmind unlock_node(node); // memory_order_stores
788 1.1 rmind
789 1.1 rmind ASSERT(THMAP_NODE(thmap, parent->slots[slot]) == node);
790 1.1 rmind node_remove(parent, slot);
791 1.1 rmind
792 1.1 rmind /* Stage the removed node for G/C. */
793 1.1 rmind stage_mem_gc(thmap, THMAP_GETOFF(thmap, node), THMAP_INODE_LEN);
794 1.1 rmind }
795 1.1 rmind
796 1.1 rmind /*
797 1.1 rmind * If the top node is empty, then we need to remove it from the
798 1.1 rmind * root level. Mark the node as deleted and clear the slot.
799 1.1 rmind *
800 1.1 rmind * Note: acquiring the lock on the top node effectively prevents
801 1.1 rmind * the root slot from changing.
802 1.1 rmind */
803 1.1 rmind if (NODE_COUNT(parent->state) == 0) {
804 1.1 rmind const unsigned rslot = query.rslot;
805 1.1 rmind const thmap_ptr_t nptr = thmap->root[rslot];
806 1.1 rmind
807 1.1 rmind ASSERT(query.level == 0);
808 1.1 rmind ASSERT(parent->parent == THMAP_NULL);
809 1.1 rmind ASSERT(THMAP_GETOFF(thmap, parent) == nptr);
810 1.1 rmind
811 1.1 rmind /* Mark as deleted and remove from the root-level slot. */
812 1.1 rmind parent->state |= NODE_DELETED;
813 1.1 rmind atomic_thread_fence(memory_order_stores);
814 1.1 rmind thmap->root[rslot] = THMAP_NULL;
815 1.1 rmind
816 1.1 rmind stage_mem_gc(thmap, nptr, THMAP_INODE_LEN);
817 1.1 rmind }
818 1.1 rmind unlock_node(parent);
819 1.1 rmind
820 1.1 rmind /*
821 1.1 rmind * Save the value and stage the leaf for G/C.
822 1.1 rmind */
823 1.1 rmind val = leaf->val;
824 1.1 rmind if ((thmap->flags & THMAP_NOCOPY) == 0) {
825 1.1 rmind stage_mem_gc(thmap, leaf->key, leaf->len);
826 1.1 rmind }
827 1.1 rmind stage_mem_gc(thmap, THMAP_GETOFF(thmap, leaf), sizeof(thmap_leaf_t));
828 1.1 rmind return val;
829 1.1 rmind }
830 1.1 rmind
831 1.1 rmind /*
832 1.1 rmind * G/C routines.
833 1.1 rmind */
834 1.1 rmind
835 1.1 rmind static void
836 1.1 rmind stage_mem_gc(thmap_t *thmap, uintptr_t addr, size_t len)
837 1.1 rmind {
838 1.1 rmind thmap_gc_t *head, *gc;
839 1.1 rmind
840 1.1 rmind gc = kmem_intr_alloc(sizeof(thmap_gc_t), KM_SLEEP);
841 1.1 rmind gc->addr = addr;
842 1.1 rmind gc->len = len;
843 1.1 rmind retry:
844 1.1 rmind gc->next = head = thmap->gc_list;
845 1.1 rmind if (!atomic_cas_ptr_p(&thmap->gc_list, head, gc)) {
846 1.1 rmind goto retry;
847 1.1 rmind }
848 1.1 rmind }
849 1.1 rmind
850 1.1 rmind void *
851 1.1 rmind thmap_stage_gc(thmap_t *thmap)
852 1.1 rmind {
853 1.1 rmind return atomic_exchange(&thmap->gc_list, NULL);
854 1.1 rmind }
855 1.1 rmind
856 1.1 rmind void
857 1.1 rmind thmap_gc(thmap_t *thmap, void *ref)
858 1.1 rmind {
859 1.1 rmind thmap_gc_t *gc = ref;
860 1.1 rmind
861 1.1 rmind while (gc) {
862 1.1 rmind thmap_gc_t *next = gc->next;
863 1.1 rmind thmap->ops->free(gc->addr, gc->len);
864 1.1 rmind kmem_intr_free(gc, sizeof(thmap_gc_t));
865 1.1 rmind gc = next;
866 1.1 rmind }
867 1.1 rmind }
868 1.1 rmind
869 1.1 rmind /*
870 1.1 rmind * thmap_create: construct a new trie-hash map object.
871 1.1 rmind */
872 1.1 rmind thmap_t *
873 1.1 rmind thmap_create(uintptr_t baseptr, const thmap_ops_t *ops, unsigned flags)
874 1.1 rmind {
875 1.1 rmind thmap_t *thmap;
876 1.1 rmind uintptr_t root;
877 1.1 rmind
878 1.1 rmind /*
879 1.1 rmind * Setup the map object.
880 1.1 rmind */
881 1.1 rmind if (!THMAP_ALIGNED_P(baseptr)) {
882 1.1 rmind return NULL;
883 1.1 rmind }
884 1.1 rmind thmap = kmem_zalloc(sizeof(thmap_t), KM_SLEEP);
885 1.1 rmind if (!thmap) {
886 1.1 rmind return NULL;
887 1.1 rmind }
888 1.1 rmind thmap->baseptr = baseptr;
889 1.1 rmind thmap->ops = ops ? ops : &thmap_default_ops;
890 1.1 rmind thmap->flags = flags;
891 1.1 rmind
892 1.1 rmind if ((thmap->flags & THMAP_SETROOT) == 0) {
893 1.1 rmind /* Allocate the root level. */
894 1.1 rmind root = thmap->ops->alloc(THMAP_ROOT_LEN);
895 1.1 rmind thmap->root = THMAP_GETPTR(thmap, root);
896 1.1 rmind if (!thmap->root) {
897 1.1 rmind kmem_free(thmap, sizeof(thmap_t));
898 1.1 rmind return NULL;
899 1.1 rmind }
900 1.1 rmind memset(thmap->root, 0, THMAP_ROOT_LEN);
901 1.1 rmind }
902 1.1 rmind return thmap;
903 1.1 rmind }
904 1.1 rmind
905 1.1 rmind int
906 1.1 rmind thmap_setroot(thmap_t *thmap, uintptr_t root_off)
907 1.1 rmind {
908 1.1 rmind if (thmap->root) {
909 1.1 rmind return -1;
910 1.1 rmind }
911 1.1 rmind thmap->root = THMAP_GETPTR(thmap, root_off);
912 1.1 rmind return 0;
913 1.1 rmind }
914 1.1 rmind
915 1.1 rmind uintptr_t
916 1.1 rmind thmap_getroot(const thmap_t *thmap)
917 1.1 rmind {
918 1.1 rmind return THMAP_GETOFF(thmap, thmap->root);
919 1.1 rmind }
920 1.1 rmind
921 1.1 rmind void
922 1.1 rmind thmap_destroy(thmap_t *thmap)
923 1.1 rmind {
924 1.1 rmind uintptr_t root = THMAP_GETOFF(thmap, thmap->root);
925 1.1 rmind void *ref;
926 1.1 rmind
927 1.1 rmind ref = thmap_stage_gc(thmap);
928 1.1 rmind thmap_gc(thmap, ref);
929 1.1 rmind
930 1.1 rmind if ((thmap->flags & THMAP_SETROOT) == 0) {
931 1.1 rmind thmap->ops->free(root, THMAP_ROOT_LEN);
932 1.1 rmind }
933 1.1 rmind kmem_free(thmap, sizeof(thmap_t));
934 1.1 rmind }
935