1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2013 EMC Corp.
5 * Copyright (c) 2011 Jeffrey Roberson <jeff@freebsd.org>
6 * Copyright (c) 2008 Mayur Shardul <mayur.shardul@gmail.com>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #ifndef _SYS_PCTRIE_H_
32 #define _SYS_PCTRIE_H_
33
34 #include <sys/_pctrie.h>
35 #include <sys/_smr.h>
36
37 struct pctrie_iter {
38 struct pctrie *ptree;
39 struct pctrie_node *node;
40 uint64_t index;
41 uint64_t limit;
42 };
43
44 static __inline void
pctrie_iter_reset(struct pctrie_iter * it)45 pctrie_iter_reset(struct pctrie_iter *it)
46 {
47 it->node = NULL;
48 }
49
50 static __inline bool
pctrie_iter_is_reset(struct pctrie_iter * it)51 pctrie_iter_is_reset(struct pctrie_iter *it)
52 {
53 return (it->node == NULL);
54 }
55
56 static __inline void
pctrie_iter_init(struct pctrie_iter * it,struct pctrie * ptree)57 pctrie_iter_init(struct pctrie_iter *it, struct pctrie *ptree)
58 {
59 it->ptree = ptree;
60 it->node = NULL;
61 it->limit = 0;
62 }
63
64 static __inline void
pctrie_iter_limit_init(struct pctrie_iter * it,struct pctrie * ptree,uint64_t limit)65 pctrie_iter_limit_init(struct pctrie_iter *it, struct pctrie *ptree,
66 uint64_t limit)
67 {
68 pctrie_iter_init(it, ptree);
69 it->limit = limit;
70 }
71
72 #ifdef _KERNEL
73
74 typedef void (*pctrie_cb_t)(void *ptr, void *arg);
75
76 #define PCTRIE_DEFINE_SMR(name, type, field, allocfn, freefn, smr) \
77 PCTRIE_DEFINE(name, type, field, allocfn, freefn) \
78 \
79 static __inline struct type * \
80 name##_PCTRIE_LOOKUP_UNLOCKED(struct pctrie *ptree, uint64_t key) \
81 { \
82 \
83 return name##_PCTRIE_VAL2PTR(pctrie_lookup_unlocked(ptree, \
84 key, (smr))); \
85 } \
86
87 #define PCTRIE_DEFINE(name, type, field, allocfn, freefn) \
88 \
89 CTASSERT(sizeof(((struct type *)0)->field) == sizeof(uint64_t)); \
90 /* \
91 * XXX This assert protects flag bits, it does not enforce natural \
92 * alignment. 32bit architectures do not naturally align 64bit fields. \
93 */ \
94 CTASSERT((__offsetof(struct type, field) & (sizeof(uint32_t) - 1)) == 0); \
95 \
96 static __inline struct type * \
97 name##_PCTRIE_VAL2PTR(uint64_t *val) \
98 { \
99 \
100 if (val == NULL) \
101 return (NULL); \
102 return (struct type *) \
103 ((uintptr_t)val - __offsetof(struct type, field)); \
104 } \
105 \
106 static __inline uint64_t * \
107 name##_PCTRIE_PTR2VAL(struct type *ptr) \
108 { \
109 \
110 return &ptr->field; \
111 } \
112 \
113 static __inline __unused int \
114 name##_PCTRIE_INSERT_BASE(struct pctrie *ptree, uint64_t *val, \
115 struct pctrie_node *parent, void *parentp, \
116 uint64_t *found, struct type **found_out) \
117 { \
118 struct pctrie_node *child; \
119 \
120 if (__predict_false(found != NULL)) { \
121 *found_out = name##_PCTRIE_VAL2PTR(found); \
122 return (EEXIST); \
123 } \
124 if (parentp != NULL) { \
125 child = allocfn(ptree); \
126 if (__predict_false(child == NULL)) { \
127 if (found_out != NULL) \
128 *found_out = NULL; \
129 return (ENOMEM); \
130 } \
131 pctrie_insert_node(val, parent, parentp, child); \
132 } \
133 return (0); \
134 } \
135 \
136 static __inline __unused int \
137 name##_PCTRIE_INSERT(struct pctrie *ptree, struct type *ptr) \
138 { \
139 void *parentp; \
140 struct pctrie_node *parent; \
141 uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \
142 \
143 parentp = pctrie_insert_lookup_strict(ptree, val, &parent); \
144 return (name##_PCTRIE_INSERT_BASE(ptree, val, parent, parentp, \
145 NULL, NULL)); \
146 } \
147 \
148 static __inline __unused int \
149 name##_PCTRIE_FIND_OR_INSERT(struct pctrie *ptree, struct type *ptr, \
150 struct type **found_out_opt) \
151 { \
152 void *parentp; \
153 struct pctrie_node *parent; \
154 uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \
155 uint64_t *found; \
156 \
157 parentp = pctrie_insert_lookup(ptree, val, &parent, &found); \
158 return (name##_PCTRIE_INSERT_BASE(ptree, val, parent, parentp, \
159 found, found_out_opt)); \
160 } \
161 \
162 static __inline __unused int \
163 name##_PCTRIE_INSERT_LOOKUP_LE(struct pctrie *ptree, struct type *ptr, \
164 struct type **found_out) \
165 { \
166 struct pctrie_node *parent; \
167 void *parentp; \
168 uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \
169 uint64_t *found; \
170 int retval; \
171 \
172 parentp = pctrie_insert_lookup(ptree, val, &parent, &found); \
173 retval = name##_PCTRIE_INSERT_BASE(ptree, val, parent, parentp, \
174 found, found_out); \
175 if (retval != 0) \
176 return (retval); \
177 found = pctrie_subtree_lookup_lt(ptree, parent, *val); \
178 *found_out = name##_PCTRIE_VAL2PTR(found); \
179 return (0); \
180 } \
181 \
182 static __inline __unused int \
183 name##_PCTRIE_ITER_INSERT(struct pctrie_iter *it, struct type *ptr) \
184 { \
185 void *parentp; \
186 uint64_t *val = name##_PCTRIE_PTR2VAL(ptr); \
187 \
188 parentp = pctrie_iter_insert_lookup(it, val); \
189 return (name##_PCTRIE_INSERT_BASE(it->ptree, val, it->node, \
190 parentp, NULL, NULL)); \
191 } \
192 \
193 static __inline __unused struct type * \
194 name##_PCTRIE_LOOKUP(struct pctrie *ptree, uint64_t key) \
195 { \
196 \
197 return name##_PCTRIE_VAL2PTR(pctrie_lookup(ptree, key)); \
198 } \
199 \
200 static __inline __unused struct type * \
201 name##_PCTRIE_LOOKUP_LE(struct pctrie *ptree, uint64_t key) \
202 { \
203 \
204 return name##_PCTRIE_VAL2PTR(pctrie_lookup_le(ptree, key)); \
205 } \
206 \
207 static __inline __unused struct type * \
208 name##_PCTRIE_LOOKUP_GE(struct pctrie *ptree, uint64_t key) \
209 { \
210 \
211 return name##_PCTRIE_VAL2PTR(pctrie_lookup_ge(ptree, key)); \
212 } \
213 \
214 static __inline __unused void \
215 name##_PCTRIE_RECLAIM(struct pctrie *ptree) \
216 { \
217 struct pctrie_node *freenode, *node; \
218 \
219 for (freenode = pctrie_reclaim_begin(&node, ptree); \
220 freenode != NULL; \
221 freenode = pctrie_reclaim_resume(&node)) \
222 freefn(ptree, freenode); \
223 } \
224 \
225 /* \
226 * While reclaiming all internal trie nodes, invoke callback(leaf, arg) \
227 * on every leaf in the trie, in order. \
228 */ \
229 static __inline __unused void \
230 name##_PCTRIE_RECLAIM_CALLBACK(struct pctrie *ptree, \
231 void (*typed_cb)(struct type *, void *), void *arg) \
232 { \
233 struct pctrie_node *freenode, *node; \
234 pctrie_cb_t callback = (pctrie_cb_t)typed_cb; \
235 \
236 for (freenode = pctrie_reclaim_begin_cb(&node, ptree, \
237 callback, __offsetof(struct type, field), arg); \
238 freenode != NULL; \
239 freenode = pctrie_reclaim_resume_cb(&node, \
240 callback, __offsetof(struct type, field), arg)) \
241 freefn(ptree, freenode); \
242 } \
243 \
244 static __inline __unused struct type * \
245 name##_PCTRIE_ITER_LOOKUP(struct pctrie_iter *it, uint64_t index) \
246 { \
247 return name##_PCTRIE_VAL2PTR(pctrie_iter_lookup(it, index)); \
248 } \
249 \
250 static __inline __unused struct type * \
251 name##_PCTRIE_ITER_STRIDE(struct pctrie_iter *it, int stride) \
252 { \
253 return name##_PCTRIE_VAL2PTR(pctrie_iter_stride(it, stride)); \
254 } \
255 \
256 static __inline __unused struct type * \
257 name##_PCTRIE_ITER_NEXT(struct pctrie_iter *it) \
258 { \
259 return name##_PCTRIE_VAL2PTR(pctrie_iter_next(it)); \
260 } \
261 \
262 static __inline __unused struct type * \
263 name##_PCTRIE_ITER_PREV(struct pctrie_iter *it) \
264 { \
265 return name##_PCTRIE_VAL2PTR(pctrie_iter_prev(it)); \
266 } \
267 \
268 static __inline __unused struct type * \
269 name##_PCTRIE_ITER_VALUE(struct pctrie_iter *it) \
270 { \
271 return name##_PCTRIE_VAL2PTR(pctrie_iter_value(it)); \
272 } \
273 \
274 static __inline __unused struct type * \
275 name##_PCTRIE_ITER_LOOKUP_GE(struct pctrie_iter *it, uint64_t index) \
276 { \
277 return name##_PCTRIE_VAL2PTR(pctrie_iter_lookup_ge(it, index)); \
278 } \
279 \
280 static __inline __unused struct type * \
281 name##_PCTRIE_ITER_JUMP_GE(struct pctrie_iter *it, int64_t jump) \
282 { \
283 return name##_PCTRIE_VAL2PTR(pctrie_iter_jump_ge(it, jump)); \
284 } \
285 \
286 static __inline __unused struct type * \
287 name##_PCTRIE_ITER_STEP_GE(struct pctrie_iter *it) \
288 { \
289 return name##_PCTRIE_VAL2PTR(pctrie_iter_jump_ge(it, 1)); \
290 } \
291 \
292 static __inline __unused struct type * \
293 name##_PCTRIE_ITER_LOOKUP_LE(struct pctrie_iter *it, uint64_t index) \
294 { \
295 return name##_PCTRIE_VAL2PTR(pctrie_iter_lookup_le(it, index)); \
296 } \
297 \
298 static __inline __unused struct type * \
299 name##_PCTRIE_ITER_JUMP_LE(struct pctrie_iter *it, int64_t jump) \
300 { \
301 return name##_PCTRIE_VAL2PTR(pctrie_iter_jump_le(it, jump)); \
302 } \
303 \
304 static __inline __unused struct type * \
305 name##_PCTRIE_ITER_STEP_LE(struct pctrie_iter *it) \
306 { \
307 return name##_PCTRIE_VAL2PTR(pctrie_iter_jump_le(it, 1)); \
308 } \
309 \
310 static __inline __unused void \
311 name##_PCTRIE_REMOVE_BASE(struct pctrie *ptree, \
312 struct pctrie_node *freenode) \
313 { \
314 if (freenode != NULL) \
315 freefn(ptree, freenode); \
316 } \
317 \
318 static __inline __unused void \
319 name##_PCTRIE_ITER_REMOVE(struct pctrie_iter *it) \
320 { \
321 struct pctrie_node *freenode; \
322 \
323 pctrie_iter_remove(it, &freenode); \
324 name##_PCTRIE_REMOVE_BASE(it->ptree, freenode); \
325 } \
326 \
327 static __inline __unused struct type * \
328 name##_PCTRIE_REPLACE(struct pctrie *ptree, struct type *ptr) \
329 { \
330 \
331 return name##_PCTRIE_VAL2PTR( \
332 pctrie_replace(ptree, name##_PCTRIE_PTR2VAL(ptr))); \
333 } \
334 \
335 static __inline __unused void \
336 name##_PCTRIE_REMOVE(struct pctrie *ptree, uint64_t key) \
337 { \
338 uint64_t *val; \
339 struct pctrie_node *freenode; \
340 \
341 val = pctrie_remove_lookup(ptree, key, &freenode); \
342 if (val == NULL) \
343 panic("%s: key not found", __func__); \
344 name##_PCTRIE_REMOVE_BASE(ptree, freenode); \
345 } \
346 \
347 static __inline __unused struct type * \
348 name##_PCTRIE_REMOVE_LOOKUP(struct pctrie *ptree, uint64_t key) \
349 { \
350 uint64_t *val; \
351 struct pctrie_node *freenode; \
352 \
353 val = pctrie_remove_lookup(ptree, key, &freenode); \
354 name##_PCTRIE_REMOVE_BASE(ptree, freenode); \
355 return name##_PCTRIE_VAL2PTR(val); \
356 }
357
358 struct pctrie_iter;
359 void *pctrie_insert_lookup(struct pctrie *ptree, uint64_t *val,
360 struct pctrie_node **parent_out, uint64_t **found_out);
361 void *pctrie_insert_lookup_strict(struct pctrie *ptree, uint64_t *val,
362 struct pctrie_node **parent_out);
363 void pctrie_insert_node(uint64_t *val, struct pctrie_node *parent,
364 void *parentp, struct pctrie_node *child);
365 uint64_t *pctrie_lookup(struct pctrie *ptree, uint64_t key);
366 uint64_t *pctrie_lookup_unlocked(struct pctrie *ptree, uint64_t key,
367 smr_t smr);
368 uint64_t *pctrie_iter_lookup(struct pctrie_iter *it, uint64_t index);
369 uint64_t *pctrie_iter_stride(struct pctrie_iter *it, int stride);
370 uint64_t *pctrie_iter_next(struct pctrie_iter *it);
371 uint64_t *pctrie_iter_prev(struct pctrie_iter *it);
372 void *pctrie_iter_insert_lookup(struct pctrie_iter *it,
373 uint64_t *val);
374 uint64_t *pctrie_lookup_ge(struct pctrie *ptree, uint64_t key);
375 uint64_t *pctrie_iter_lookup_ge(struct pctrie_iter *it, uint64_t index);
376 uint64_t *pctrie_iter_jump_ge(struct pctrie_iter *it, int64_t jump);
377 uint64_t *pctrie_lookup_le(struct pctrie *ptree, uint64_t key);
378 uint64_t *pctrie_subtree_lookup_lt(struct pctrie *ptree,
379 struct pctrie_node *node, uint64_t key);
380 uint64_t *pctrie_iter_lookup_le(struct pctrie_iter *it, uint64_t index);
381 uint64_t *pctrie_iter_jump_le(struct pctrie_iter *it, int64_t jump);
382 struct pctrie_node *pctrie_reclaim_begin(struct pctrie_node **pnode,
383 struct pctrie *ptree);
384 struct pctrie_node *pctrie_reclaim_resume(struct pctrie_node **pnode);
385 struct pctrie_node *pctrie_reclaim_begin_cb(struct pctrie_node **pnode,
386 struct pctrie *ptree,
387 pctrie_cb_t callback, int keyoff, void *arg);
388 struct pctrie_node *pctrie_reclaim_resume_cb(struct pctrie_node **pnode,
389 pctrie_cb_t callback, int keyoff, void *arg);
390 uint64_t *pctrie_remove_lookup(struct pctrie *ptree, uint64_t index,
391 struct pctrie_node **killnode);
392 void pctrie_iter_remove(struct pctrie_iter *it,
393 struct pctrie_node **freenode);
394 uint64_t *pctrie_iter_value(struct pctrie_iter *it);
395 uint64_t *pctrie_replace(struct pctrie *ptree, uint64_t *newval);
396 size_t pctrie_node_size(void);
397 int pctrie_zone_init(void *mem, int size, int flags);
398
399 /*
400 * Each search path in the trie terminates at a leaf, which is a pointer to a
401 * value marked with a set 1-bit. A leaf may be associated with a null pointer
402 * to indicate no value there.
403 */
404 #define PCTRIE_ISLEAF 0x1
405 #define PCTRIE_NULL (struct pctrie_node *)PCTRIE_ISLEAF
406
407 static __inline void
pctrie_init(struct pctrie * ptree)408 pctrie_init(struct pctrie *ptree)
409 {
410 ptree->pt_root = PCTRIE_NULL;
411 }
412
413 static __inline bool
pctrie_is_empty(struct pctrie * ptree)414 pctrie_is_empty(struct pctrie *ptree)
415 {
416 return (ptree->pt_root == PCTRIE_NULL);
417 }
418
419 /* Set of all flag bits stored in node pointers. */
420 #define PCTRIE_FLAGS (PCTRIE_ISLEAF)
421 /* Minimum align parameter for uma_zcreate. */
422 #define PCTRIE_PAD PCTRIE_FLAGS
423
424 /*
425 * These widths should allow the pointers to a node's children to fit within
426 * a single cache line. The extra levels from a narrow width should not be
427 * a problem thanks to path compression.
428 */
429 #ifdef __LP64__
430 #define PCTRIE_WIDTH 4
431 #else
432 #define PCTRIE_WIDTH 3
433 #endif
434
435 #define PCTRIE_COUNT (1 << PCTRIE_WIDTH)
436
437 #endif /* _KERNEL */
438 #endif /* !_SYS_PCTRIE_H_ */
439