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