rcuja: add fallback nodes
[userspace-rcu.git] / rcuja / rcuja.c
1 /*
2 * rcuja/rcuja.c
3 *
4 * Userspace RCU library - RCU Judy Array
5 *
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #define _LGPL_SOURCE
24 #include <stdint.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <urcu/rcuja.h>
28 #include <urcu/compiler.h>
29 #include <urcu/arch.h>
30 #include <assert.h>
31 #include <urcu-pointer.h>
32 #include <urcu/uatomic.h>
33 #include <stdint.h>
34
35 #include "rcuja-internal.h"
36 #include "bitfield.h"
37
38 enum cds_ja_type_class {
39 RCU_JA_LINEAR = 0, /* Type A */
40 /* 32-bit: 1 to 25 children, 8 to 128 bytes */
41 /* 64-bit: 1 to 28 children, 16 to 256 bytes */
42 RCU_JA_POOL = 1, /* Type B */
43 /* 32-bit: 26 to 100 children, 256 to 512 bytes */
44 /* 64-bit: 29 to 112 children, 512 to 1024 bytes */
45 RCU_JA_PIGEON = 2, /* Type C */
46 /* 32-bit: 101 to 256 children, 1024 bytes */
47 /* 64-bit: 113 to 256 children, 2048 bytes */
48 /* Leaf nodes are implicit from their height in the tree */
49 RCU_JA_NR_TYPES,
50
51 RCU_JA_NULL, /* not an encoded type, but keeps code regular */
52 };
53
54 struct cds_ja_type {
55 enum cds_ja_type_class type_class;
56 uint16_t min_child; /* minimum number of children: 1 to 256 */
57 uint16_t max_child; /* maximum number of children: 1 to 256 */
58 uint16_t max_linear_child; /* per-pool max nr. children: 1 to 256 */
59 uint16_t order; /* node size is (1 << order), in bytes */
60 uint16_t nr_pool_order; /* number of pools */
61 uint16_t pool_size_order; /* pool size */
62 };
63
64 /*
65 * Number of least significant pointer bits reserved to represent the
66 * child type.
67 */
68 #define JA_TYPE_BITS 3
69 #define JA_TYPE_MAX_NR (1UL << JA_TYPE_BITS)
70 #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1)
71 #define JA_PTR_MASK (~JA_TYPE_MASK)
72
73 #define JA_ENTRY_PER_NODE 256UL
74 #define JA_BITS_PER_BYTE 3
75
76 #define JA_MAX_DEPTH 5 /* Maximum depth, including leafs */
77
78 /*
79 * Entry for NULL node is at index 8 of the table. It is never encoded
80 * in flags.
81 */
82 #define NODE_INDEX_NULL 8
83
84 /*
85 * Number of removals needed on a fallback node before we try to shrink
86 * it.
87 */
88 #define JA_FALLBACK_REMOVAL_COUNT 8
89
90 /*
91 * Iteration on the array to find the right node size for the number of
92 * children stops when it reaches .max_child == 256 (this is the largest
93 * possible node size, which contains 256 children).
94 * The min_child overlaps with the previous max_child to provide an
95 * hysteresis loop to reallocation for patterns of cyclic add/removal
96 * within the same node.
97 * The node the index within the following arrays is represented on 3
98 * bits. It identifies the node type, min/max number of children, and
99 * the size order.
100 * The max_child values for the RCU_JA_POOL below result from
101 * statistical approximation: over million populations, the max_child
102 * covers between 97% and 99% of the populations generated. Therefore, a
103 * fallback should exist to cover the rare extreme population unbalance
104 * cases, but it will not have a major impact on speed nor space
105 * consumption, since those are rare cases.
106 */
107
108 #if (CAA_BITS_PER_LONG < 64)
109 /* 32-bit pointers */
110 enum {
111 ja_type_0_max_child = 1,
112 ja_type_1_max_child = 3,
113 ja_type_2_max_child = 6,
114 ja_type_3_max_child = 12,
115 ja_type_4_max_child = 25,
116 ja_type_5_max_child = 48,
117 ja_type_6_max_child = 92,
118 ja_type_7_max_child = 256,
119 ja_type_8_max_child = 0, /* NULL */
120 };
121
122 enum {
123 ja_type_0_max_linear_child = 1,
124 ja_type_1_max_linear_child = 3,
125 ja_type_2_max_linear_child = 6,
126 ja_type_3_max_linear_child = 12,
127 ja_type_4_max_linear_child = 25,
128 ja_type_5_max_linear_child = 24,
129 ja_type_6_max_linear_child = 23,
130 };
131
132 enum {
133 ja_type_5_nr_pool_order = 1,
134 ja_type_6_nr_pool_order = 2,
135 };
136
137 const struct cds_ja_type ja_types[] = {
138 { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = ja_type_0_max_child, .max_linear_child = ja_type_0_max_linear_child, .order = 3, },
139 { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = ja_type_1_max_child, .max_linear_child = ja_type_1_max_linear_child, .order = 4, },
140 { .type_class = RCU_JA_LINEAR, .min_child = 3, .max_child = ja_type_2_max_child, .max_linear_child = ja_type_2_max_linear_child, .order = 5, },
141 { .type_class = RCU_JA_LINEAR, .min_child = 4, .max_child = ja_type_3_max_child, .max_linear_child = ja_type_3_max_linear_child, .order = 6, },
142 { .type_class = RCU_JA_LINEAR, .min_child = 10, .max_child = ja_type_4_max_child, .max_linear_child = ja_type_4_max_linear_child, .order = 7, },
143
144 /* Pools may fill sooner than max_child */
145 { .type_class = RCU_JA_POOL, .min_child = 20, .max_child = ja_type_5_max_child, .max_linear_child = ja_type_5_max_linear_child, .order = 8, .nr_pool_order = ja_type_5_nr_pool_order, .pool_size_order = 7, },
146 { .type_class = RCU_JA_POOL, .min_child = 45, .max_child = ja_type_6_max_child, .max_linear_child = ja_type_6_max_linear_child, .order = 9, .nr_pool_order = ja_type_6_nr_pool_order, .pool_size_order = 7, },
147
148 /*
149 * TODO: Upon node removal below min_child, if child pool is
150 * filled beyond capacity, we need to roll back to pigeon.
151 */
152 { .type_class = RCU_JA_PIGEON, .min_child = 89, .max_child = ja_type_7_max_child, .order = 10, },
153
154 { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, },
155 };
156 #else /* !(CAA_BITS_PER_LONG < 64) */
157 /* 64-bit pointers */
158 enum {
159 ja_type_0_max_child = 1,
160 ja_type_1_max_child = 3,
161 ja_type_2_max_child = 7,
162 ja_type_3_max_child = 14,
163 ja_type_4_max_child = 28,
164 ja_type_5_max_child = 54,
165 ja_type_6_max_child = 104,
166 ja_type_7_max_child = 256,
167 ja_type_8_max_child = 256,
168 };
169
170 enum {
171 ja_type_0_max_linear_child = 1,
172 ja_type_1_max_linear_child = 3,
173 ja_type_2_max_linear_child = 7,
174 ja_type_3_max_linear_child = 14,
175 ja_type_4_max_linear_child = 28,
176 ja_type_5_max_linear_child = 27,
177 ja_type_6_max_linear_child = 26,
178 };
179
180 enum {
181 ja_type_5_nr_pool_order = 1,
182 ja_type_6_nr_pool_order = 2,
183 };
184
185 const struct cds_ja_type ja_types[] = {
186 { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = ja_type_0_max_child, .max_linear_child = ja_type_0_max_linear_child, .order = 4, },
187 { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = ja_type_1_max_child, .max_linear_child = ja_type_1_max_linear_child, .order = 5, },
188 { .type_class = RCU_JA_LINEAR, .min_child = 3, .max_child = ja_type_2_max_child, .max_linear_child = ja_type_2_max_linear_child, .order = 6, },
189 { .type_class = RCU_JA_LINEAR, .min_child = 5, .max_child = ja_type_3_max_child, .max_linear_child = ja_type_3_max_linear_child, .order = 7, },
190 { .type_class = RCU_JA_LINEAR, .min_child = 10, .max_child = ja_type_4_max_child, .max_linear_child = ja_type_4_max_linear_child, .order = 8, },
191
192 /* Pools may fill sooner than max_child. */
193 { .type_class = RCU_JA_POOL, .min_child = 22, .max_child = ja_type_5_max_child, .max_linear_child = ja_type_5_max_linear_child, .order = 9, .nr_pool_order = ja_type_5_nr_pool_order, .pool_size_order = 8, },
194 { .type_class = RCU_JA_POOL, .min_child = 51, .max_child = ja_type_6_max_child, .max_linear_child = ja_type_6_max_linear_child, .order = 10, .nr_pool_order = ja_type_6_nr_pool_order, .pool_size_order = 8, },
195
196 /*
197 * TODO: Upon node removal below min_child, if child pool is
198 * filled beyond capacity, we need to roll back to pigeon.
199 */
200 { .type_class = RCU_JA_PIGEON, .min_child = 101, .max_child = ja_type_7_max_child, .order = 11, },
201
202 { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, },
203 };
204 #endif /* !(BITS_PER_LONG < 64) */
205
206 static inline __attribute__((unused))
207 void static_array_size_check(void)
208 {
209 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) < JA_TYPE_MAX_NR);
210 }
211
212 /*
213 * The cds_ja_node contains the compressed node data needed for
214 * read-side. For linear and pool node configurations, it starts with a
215 * byte counting the number of children in the node. Then, the
216 * node-specific data is placed.
217 * The node mutex, if any is needed, protecting concurrent updated of
218 * each node is placed in a separate hash table indexed by node address.
219 * For the pigeon configuration, the number of children is also kept in
220 * a separate hash table, indexed by node address, because it is only
221 * required for updates.
222 */
223
224 #define DECLARE_LINEAR_NODE(index) \
225 struct { \
226 uint8_t nr_child; \
227 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
228 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
229 }
230
231 #define DECLARE_POOL_NODE(index) \
232 struct { \
233 struct { \
234 uint8_t nr_child; \
235 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
236 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
237 } linear[1U << ja_type_## index ##_nr_pool_order]; \
238 }
239
240 struct cds_ja_inode {
241 union {
242 /* Linear configuration */
243 DECLARE_LINEAR_NODE(0) conf_0;
244 DECLARE_LINEAR_NODE(1) conf_1;
245 DECLARE_LINEAR_NODE(2) conf_2;
246 DECLARE_LINEAR_NODE(3) conf_3;
247 DECLARE_LINEAR_NODE(4) conf_4;
248
249 /* Pool configuration */
250 DECLARE_POOL_NODE(5) conf_5;
251 DECLARE_POOL_NODE(6) conf_6;
252
253 /* Pigeon configuration */
254 struct {
255 struct cds_ja_inode_flag *child[ja_type_7_max_child];
256 } conf_7;
257 /* data aliasing nodes for computed accesses */
258 uint8_t data[sizeof(struct cds_ja_inode_flag *) * ja_type_7_max_child];
259 } u;
260 };
261
262 static
263 struct cds_ja_inode_flag *ja_node_flag(struct cds_ja_inode *node,
264 unsigned long type)
265 {
266 assert(type < (1UL << JA_TYPE_BITS));
267 return (struct cds_ja_inode_flag *) (((unsigned long) node) | type);
268 }
269
270 static
271 struct cds_ja_inode *ja_node_ptr(struct cds_ja_inode_flag *node)
272 {
273 return (struct cds_ja_inode *) (((unsigned long) node) & JA_PTR_MASK);
274 }
275
276 static
277 unsigned long ja_node_type(struct cds_ja_inode_flag *node)
278 {
279 unsigned long type;
280
281 if (ja_node_ptr(node) == NULL) {
282 return NODE_INDEX_NULL;
283 }
284 type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK);
285 assert(type < (1UL << JA_TYPE_BITS));
286 return type;
287 }
288
289 struct cds_ja_inode *alloc_cds_ja_node(const struct cds_ja_type *ja_type)
290 {
291 return calloc(1U << ja_type->order, sizeof(char));
292 }
293
294 void free_cds_ja_node(struct cds_ja_inode *node)
295 {
296 free(node);
297 }
298
299 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
300 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
301 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
302 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
303
304 static
305 uint8_t *align_ptr_size(uint8_t *ptr)
306 {
307 return (uint8_t *) JA_ALIGN((unsigned long) ptr, sizeof(void *));
308 }
309
310 static
311 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type *type,
312 struct cds_ja_inode *node)
313 {
314 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
315 return CMM_LOAD_SHARED(node->u.data[0]);
316 }
317
318 /*
319 * The order in which values and pointers are does does not matter: if
320 * a value is missing, we return NULL. If a value is there, but its
321 * associated pointers is still NULL, we return NULL too.
322 */
323 static
324 struct cds_ja_inode_flag *ja_linear_node_get_nth(const struct cds_ja_type *type,
325 struct cds_ja_inode *node,
326 struct cds_ja_inode_flag ***child_node_flag_ptr,
327 uint8_t n)
328 {
329 uint8_t nr_child;
330 uint8_t *values;
331 struct cds_ja_inode_flag **pointers;
332 struct cds_ja_inode_flag *ptr;
333 unsigned int i;
334
335 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
336
337 nr_child = ja_linear_node_get_nr_child(type, node);
338 cmm_smp_rmb(); /* read nr_child before values and pointers */
339 assert(nr_child <= type->max_linear_child);
340 assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child);
341
342 values = &node->u.data[1];
343 for (i = 0; i < nr_child; i++) {
344 if (CMM_LOAD_SHARED(values[i]) == n)
345 break;
346 }
347 if (i >= nr_child)
348 return NULL;
349 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
350 if (caa_unlikely(child_node_flag_ptr))
351 *child_node_flag_ptr = &pointers[i];
352 ptr = rcu_dereference(pointers[i]);
353 assert(ja_node_ptr(ptr) != NULL);
354 return ptr;
355 }
356
357 static
358 void ja_linear_node_get_ith_pos(const struct cds_ja_type *type,
359 struct cds_ja_inode *node,
360 uint8_t i,
361 uint8_t *v,
362 struct cds_ja_inode_flag **iter)
363 {
364 uint8_t *values;
365 struct cds_ja_inode_flag **pointers;
366
367 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
368 assert(i < ja_linear_node_get_nr_child(type, node));
369
370 values = &node->u.data[1];
371 *v = values[i];
372 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
373 *iter = pointers[i];
374 }
375
376 static
377 struct cds_ja_inode_flag *ja_pool_node_get_nth(const struct cds_ja_type *type,
378 struct cds_ja_inode *node,
379 struct cds_ja_inode_flag ***child_node_flag_ptr,
380 uint8_t n)
381 {
382 struct cds_ja_inode *linear;
383
384 assert(type->type_class == RCU_JA_POOL);
385 /*
386 * TODO: currently, we select the pool by highest bits. We
387 * should support various encodings.
388 */
389 linear = (struct cds_ja_inode *)
390 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
391 return ja_linear_node_get_nth(type, linear, child_node_flag_ptr, n);
392 }
393
394 static
395 struct cds_ja_inode *ja_pool_node_get_ith_pool(const struct cds_ja_type *type,
396 struct cds_ja_inode *node,
397 uint8_t i)
398 {
399 assert(type->type_class == RCU_JA_POOL);
400 return (struct cds_ja_inode *)
401 &node->u.data[(unsigned int) i << type->pool_size_order];
402 }
403
404 static
405 struct cds_ja_inode_flag *ja_pigeon_node_get_nth(const struct cds_ja_type *type,
406 struct cds_ja_inode *node,
407 struct cds_ja_inode_flag ***child_node_flag_ptr,
408 uint8_t n)
409 {
410 struct cds_ja_inode_flag **child_node_flag;
411
412 assert(type->type_class == RCU_JA_PIGEON);
413 child_node_flag = &((struct cds_ja_inode_flag **) node->u.data)[n];
414 if (caa_unlikely(child_node_flag_ptr))
415 *child_node_flag_ptr = child_node_flag;
416 return rcu_dereference(*child_node_flag);
417 }
418
419 /*
420 * ja_node_get_nth: get nth item from a node.
421 * node_flag is already rcu_dereference'd.
422 */
423 static
424 struct cds_ja_inode_flag * ja_node_get_nth(struct cds_ja_inode_flag *node_flag,
425 struct cds_ja_inode_flag ***child_node_flag_ptr,
426 uint8_t n)
427 {
428 unsigned int type_index;
429 struct cds_ja_inode *node;
430 const struct cds_ja_type *type;
431
432 node = ja_node_ptr(node_flag);
433 assert(node != NULL);
434 type_index = ja_node_type(node_flag);
435 type = &ja_types[type_index];
436
437 switch (type->type_class) {
438 case RCU_JA_LINEAR:
439 return ja_linear_node_get_nth(type, node,
440 child_node_flag_ptr, n);
441 case RCU_JA_POOL:
442 return ja_pool_node_get_nth(type, node,
443 child_node_flag_ptr, n);
444 case RCU_JA_PIGEON:
445 return ja_pigeon_node_get_nth(type, node,
446 child_node_flag_ptr, n);
447 default:
448 assert(0);
449 return (void *) -1UL;
450 }
451 }
452
453 /*
454 * TODO: use ja_get_nr_child to monitor limits triggering shrink
455 * recompaction.
456 * Also use ja_get_nr_child to make the difference between resize and
457 * pool change of compaction bit(s).
458 */
459 static
460 unsigned int ja_get_nr_child(struct cds_ja_shadow_node *shadow_node)
461 {
462 return shadow_node->nr_child;
463 }
464
465 static
466 int ja_linear_node_set_nth(const struct cds_ja_type *type,
467 struct cds_ja_inode *node,
468 struct cds_ja_shadow_node *shadow_node,
469 uint8_t n,
470 struct cds_ja_inode_flag *child_node_flag)
471 {
472 uint8_t nr_child;
473 uint8_t *values, *nr_child_ptr;
474 struct cds_ja_inode_flag **pointers;
475 unsigned int i;
476
477 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
478
479 nr_child_ptr = &node->u.data[0];
480 dbg_printf("linear set nth: nr_child_ptr %p\n", nr_child_ptr);
481 nr_child = *nr_child_ptr;
482 assert(nr_child <= type->max_linear_child);
483
484 values = &node->u.data[1];
485 for (i = 0; i < nr_child; i++) {
486 if (values[i] == n)
487 return -EEXIST;
488 }
489 if (nr_child >= type->max_linear_child) {
490 /* No space left in this node type */
491 return -ENOSPC;
492 }
493 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
494 assert(pointers[nr_child] == NULL);
495 rcu_assign_pointer(pointers[nr_child], child_node_flag);
496 CMM_STORE_SHARED(values[nr_child], n);
497 cmm_smp_wmb(); /* write value and pointer before nr_child */
498 CMM_STORE_SHARED(*nr_child_ptr, nr_child + 1);
499 shadow_node->nr_child++;
500 dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n",
501 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr),
502 (unsigned int) shadow_node->nr_child,
503 node, shadow_node);
504
505 return 0;
506 }
507
508 static
509 int ja_pool_node_set_nth(const struct cds_ja_type *type,
510 struct cds_ja_inode *node,
511 struct cds_ja_shadow_node *shadow_node,
512 uint8_t n,
513 struct cds_ja_inode_flag *child_node_flag)
514 {
515 struct cds_ja_inode *linear;
516
517 assert(type->type_class == RCU_JA_POOL);
518 linear = (struct cds_ja_inode *)
519 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
520 return ja_linear_node_set_nth(type, linear, shadow_node,
521 n, child_node_flag);
522 }
523
524 static
525 int ja_pigeon_node_set_nth(const struct cds_ja_type *type,
526 struct cds_ja_inode *node,
527 struct cds_ja_shadow_node *shadow_node,
528 uint8_t n,
529 struct cds_ja_inode_flag *child_node_flag)
530 {
531 struct cds_ja_inode_flag **ptr;
532
533 assert(type->type_class == RCU_JA_PIGEON);
534 ptr = &((struct cds_ja_inode_flag **) node->u.data)[n];
535 if (*ptr)
536 return -EEXIST;
537 rcu_assign_pointer(*ptr, child_node_flag);
538 shadow_node->nr_child++;
539 return 0;
540 }
541
542 /*
543 * _ja_node_set_nth: set nth item within a node. Return an error
544 * (negative error value) if it is already there.
545 * TODO: exclusive access on node.
546 */
547 static
548 int _ja_node_set_nth(const struct cds_ja_type *type,
549 struct cds_ja_inode *node,
550 struct cds_ja_shadow_node *shadow_node,
551 uint8_t n,
552 struct cds_ja_inode_flag *child_node_flag)
553 {
554 switch (type->type_class) {
555 case RCU_JA_LINEAR:
556 return ja_linear_node_set_nth(type, node, shadow_node, n,
557 child_node_flag);
558 case RCU_JA_POOL:
559 return ja_pool_node_set_nth(type, node, shadow_node, n,
560 child_node_flag);
561 case RCU_JA_PIGEON:
562 return ja_pigeon_node_set_nth(type, node, shadow_node, n,
563 child_node_flag);
564 case RCU_JA_NULL:
565 return -ENOSPC;
566 default:
567 assert(0);
568 return -EINVAL;
569 }
570
571 return 0;
572 }
573
574 /*
575 * ja_node_recompact_add: recompact a node, adding a new child.
576 * TODO: for pool type, take selection bit(s) into account.
577 * Return 0 on success, -ENOENT if need to retry, or other negative
578 * error value otherwise.
579 */
580 static
581 int ja_node_recompact_add(struct cds_ja *ja,
582 unsigned int old_type_index,
583 const struct cds_ja_type *old_type,
584 struct cds_ja_inode *old_node,
585 struct cds_ja_shadow_node *shadow_node,
586 struct cds_ja_inode_flag **old_node_flag, uint8_t n,
587 struct cds_ja_inode_flag *child_node_flag)
588 {
589 unsigned int new_type_index;
590 struct cds_ja_inode *new_node;
591 struct cds_ja_shadow_node *new_shadow_node;
592 const struct cds_ja_type *new_type;
593 struct cds_ja_inode_flag *new_node_flag;
594 int ret;
595 int fallback = 0;
596
597 if (!shadow_node || old_type_index == NODE_INDEX_NULL) {
598 new_type_index = 0;
599 } else {
600 new_type_index = old_type_index + 1;
601 }
602
603 retry: /* for fallback */
604 dbg_printf("Recompact to type %d\n", new_type_index);
605 new_type = &ja_types[new_type_index];
606 new_node = alloc_cds_ja_node(new_type);
607 if (!new_node)
608 return -ENOMEM;
609 new_node_flag = ja_node_flag(new_node, new_type_index);
610
611 dbg_printf("Recompact inherit lock from %p\n", shadow_node);
612 new_shadow_node = rcuja_shadow_set(ja->ht, new_node, shadow_node);
613 if (!new_shadow_node) {
614 free(new_node);
615 return -ENOMEM;
616 }
617 if (fallback)
618 new_shadow_node->fallback_removal_count =
619 JA_FALLBACK_REMOVAL_COUNT;
620
621 assert(old_type->type_class != RCU_JA_PIGEON);
622 switch (old_type->type_class) {
623 case RCU_JA_LINEAR:
624 {
625 uint8_t nr_child =
626 ja_linear_node_get_nr_child(old_type, old_node);
627 unsigned int i;
628
629 for (i = 0; i < nr_child; i++) {
630 struct cds_ja_inode_flag *iter;
631 uint8_t v;
632
633 ja_linear_node_get_ith_pos(old_type, old_node, i, &v, &iter);
634 if (!iter)
635 continue;
636 ret = _ja_node_set_nth(new_type, new_node,
637 new_shadow_node,
638 v, iter);
639 if (new_type->type_class == RCU_JA_POOL && ret) {
640 goto fallback_toosmall;
641 }
642 assert(!ret);
643 }
644 break;
645 }
646 case RCU_JA_POOL:
647 {
648 unsigned int pool_nr;
649
650 for (pool_nr = 0; pool_nr < (1U << old_type->nr_pool_order); pool_nr++) {
651 struct cds_ja_inode *pool =
652 ja_pool_node_get_ith_pool(old_type,
653 old_node, pool_nr);
654 uint8_t nr_child =
655 ja_linear_node_get_nr_child(old_type, pool);
656 unsigned int j;
657
658 for (j = 0; j < nr_child; j++) {
659 struct cds_ja_inode_flag *iter;
660 uint8_t v;
661
662 ja_linear_node_get_ith_pos(old_type, pool,
663 j, &v, &iter);
664 if (!iter)
665 continue;
666 ret = _ja_node_set_nth(new_type, new_node,
667 new_shadow_node,
668 v, iter);
669 if (new_type->type_class == RCU_JA_POOL
670 && ret) {
671 goto fallback_toosmall;
672 }
673 assert(!ret);
674 }
675 }
676 break;
677 }
678 case RCU_JA_NULL:
679 /* Nothing to copy */
680 break;
681 case RCU_JA_PIGEON:
682 default:
683 assert(0);
684 ret = -EINVAL;
685 goto end;
686 }
687
688 /* add node */
689 ret = _ja_node_set_nth(new_type, new_node,
690 new_shadow_node,
691 n, child_node_flag);
692 assert(!ret);
693 /* Return pointer to new recompacted new through old_node_flag */
694 *old_node_flag = new_node_flag;
695 if (old_node) {
696 ret = rcuja_shadow_clear(ja->ht, old_node, shadow_node,
697 RCUJA_SHADOW_CLEAR_FREE_NODE);
698 assert(!ret);
699 }
700
701 ret = 0;
702 end:
703 return ret;
704
705 fallback_toosmall:
706 /* fallback if next pool is too small */
707 ret = rcuja_shadow_clear(ja->ht, new_node, new_shadow_node,
708 RCUJA_SHADOW_CLEAR_FREE_NODE);
709 assert(!ret);
710
711 /* Last type: pigeon */
712 new_type_index = (1UL << JA_TYPE_BITS) - 1;
713 dbg_printf("Fallback to type %d\n", new_type_index);
714 uatomic_inc(&ja->nr_fallback);
715 fallback = 1;
716 goto retry;
717 }
718
719 /*
720 * Return 0 on success, -ENOENT if need to retry, or other negative
721 * error value otherwise.
722 */
723 static
724 int ja_node_set_nth(struct cds_ja *ja,
725 struct cds_ja_inode_flag **node_flag, uint8_t n,
726 struct cds_ja_inode_flag *child_node_flag,
727 struct cds_ja_shadow_node *shadow_node)
728 {
729 int ret;
730 unsigned int type_index;
731 const struct cds_ja_type *type;
732 struct cds_ja_inode *node;
733
734 dbg_printf("ja_node_set_nth for n=%u, node %p, shadow %p\n",
735 (unsigned int) n, ja_node_ptr(*node_flag), shadow_node);
736
737 node = ja_node_ptr(*node_flag);
738 type_index = ja_node_type(*node_flag);
739 type = &ja_types[type_index];
740 ret = _ja_node_set_nth(type, node, shadow_node,
741 n, child_node_flag);
742 if (ret == -ENOSPC) {
743 /* Not enough space in node, need to recompact. */
744 ret = ja_node_recompact_add(ja, type_index, type, node,
745 shadow_node, node_flag, n, child_node_flag);
746 }
747 return ret;
748 }
749
750 struct cds_hlist_head *cds_ja_lookup(struct cds_ja *ja, uint64_t key)
751 {
752 unsigned int tree_depth, i;
753 struct cds_ja_inode_flag *node_flag;
754
755 if (caa_unlikely(key > ja->key_max))
756 return NULL;
757 tree_depth = ja->tree_depth;
758 node_flag = rcu_dereference(ja->root);
759
760 /* level 0: root node */
761 if (!ja_node_ptr(node_flag))
762 return NULL;
763
764 for (i = 1; i < tree_depth; i++) {
765 node_flag = ja_node_get_nth(node_flag, NULL,
766 (unsigned char) key);
767 if (!ja_node_ptr(node_flag))
768 return NULL;
769 key >>= JA_BITS_PER_BYTE;
770 }
771
772 /* Last level lookup succeded. We got an actual match. */
773 return (struct cds_hlist_head *) node_flag;
774 }
775
776 /*
777 * We reached an unpopulated node. Create it and the children we need,
778 * and then attach the entire branch to the current node. This may
779 * trigger recompaction of the current node. Locks needed: node lock
780 * (for add), and, possibly, parent node lock (to update pointer due to
781 * node recompaction).
782 *
783 * First take node lock, check if recompaction is needed, then take
784 * parent lock (if needed). Then we can proceed to create the new
785 * branch. Publish the new branch, and release locks.
786 * TODO: we currently always take the parent lock even when not needed.
787 */
788 static
789 int ja_attach_node(struct cds_ja *ja,
790 struct cds_ja_inode_flag **node_flag_ptr,
791 struct cds_ja_inode_flag *node_flag,
792 struct cds_ja_inode_flag *parent_node_flag,
793 uint64_t key,
794 unsigned int depth,
795 struct cds_ja_node *child_node)
796 {
797 struct cds_ja_shadow_node *shadow_node = NULL,
798 *parent_shadow_node = NULL,
799 *iter_shadow_node;
800 struct cds_ja_inode *node = ja_node_ptr(node_flag);
801 struct cds_ja_inode *parent_node = ja_node_ptr(parent_node_flag);
802 struct cds_hlist_head head;
803 struct cds_ja_inode_flag *iter_node_flag, *iter_dest_node_flag;
804 int ret, i;
805 struct cds_ja_inode_flag *created_nodes[JA_MAX_DEPTH];
806 int nr_created_nodes = 0;
807
808 dbg_printf("Attach node at depth %u\n", depth);
809
810 assert(node);
811 shadow_node = rcuja_shadow_lookup_lock(ja->ht, node);
812 if (!shadow_node) {
813 ret = -ENOENT;
814 goto end;
815 }
816 if (parent_node) {
817 parent_shadow_node = rcuja_shadow_lookup_lock(ja->ht,
818 parent_node);
819 if (!parent_shadow_node) {
820 ret = -ENOENT;
821 goto unlock_shadow;
822 }
823 }
824
825 /* Create new branch, starting from bottom */
826 CDS_INIT_HLIST_HEAD(&head);
827 cds_hlist_add_head_rcu(&child_node->list, &head);
828 iter_node_flag = (struct cds_ja_inode_flag *) head.next;
829
830 /* Create shadow node for the leaf node */
831 dbg_printf("leaf shadow node creation\n");
832 iter_shadow_node = rcuja_shadow_set(ja->ht,
833 ja_node_ptr(iter_node_flag), NULL);
834 if (!iter_shadow_node) {
835 ret = -ENOMEM;
836 goto check_error;
837 }
838 created_nodes[nr_created_nodes++] = iter_node_flag;
839
840 for (i = ja->tree_depth - 1; i >= (int) depth; i--) {
841 dbg_printf("branch creation level %d, key %" PRIu64 "\n",
842 i, key >> (JA_BITS_PER_BYTE * (i - 2)));
843 iter_dest_node_flag = NULL;
844 ret = ja_node_set_nth(ja, &iter_dest_node_flag,
845 key >> (JA_BITS_PER_BYTE * (i - 2)),
846 iter_node_flag,
847 NULL);
848 if (ret)
849 goto check_error;
850 created_nodes[nr_created_nodes++] = iter_dest_node_flag;
851 iter_node_flag = iter_dest_node_flag;
852 }
853
854 if (depth > 1) {
855 /* We need to use set_nth on the previous level. */
856 iter_dest_node_flag = node_flag;
857 ret = ja_node_set_nth(ja, &iter_dest_node_flag,
858 key >> (JA_BITS_PER_BYTE * (depth - 2)),
859 iter_node_flag,
860 shadow_node);
861 if (ret)
862 goto check_error;
863 created_nodes[nr_created_nodes++] = iter_dest_node_flag;
864 iter_node_flag = iter_dest_node_flag;
865 }
866
867 /* Publish new branch */
868 dbg_printf("Publish branch %p, replacing %p\n",
869 iter_node_flag, *node_flag_ptr);
870 rcu_assign_pointer(*node_flag_ptr, iter_node_flag);
871
872 /* Success */
873 ret = 0;
874
875 check_error:
876 if (ret) {
877 for (i = 0; i < nr_created_nodes; i++) {
878 int tmpret;
879 int flags;
880
881 flags = RCUJA_SHADOW_CLEAR_FREE_LOCK;
882 if (i)
883 flags |= RCUJA_SHADOW_CLEAR_FREE_NODE;
884 tmpret = rcuja_shadow_clear(ja->ht,
885 ja_node_ptr(created_nodes[i]),
886 NULL,
887 flags);
888 assert(!tmpret);
889 }
890 }
891 if (parent_shadow_node)
892 rcuja_shadow_unlock(parent_shadow_node);
893 unlock_shadow:
894 if (shadow_node)
895 rcuja_shadow_unlock(shadow_node);
896 end:
897 return ret;
898 }
899
900 /*
901 * Lock the hlist head shadow node mutex, and add node to list of
902 * duplicates. Failure can happen if concurrent removal removes the last
903 * node with same key before we get the lock.
904 * Return 0 on success, negative error value on failure.
905 */
906 static
907 int ja_chain_node(struct cds_ja *ja,
908 struct cds_hlist_head *head,
909 struct cds_ja_node *node)
910 {
911 struct cds_ja_shadow_node *shadow_node;
912
913 shadow_node = rcuja_shadow_lookup_lock(ja->ht,
914 (struct cds_ja_inode *) head);
915 if (!shadow_node)
916 return -ENOENT;
917 cds_hlist_add_head_rcu(&node->list, head);
918 rcuja_shadow_unlock(shadow_node);
919 return 0;
920 }
921
922 int cds_ja_add(struct cds_ja *ja, uint64_t key,
923 struct cds_ja_node *new_node)
924 {
925 unsigned int tree_depth, i;
926 uint64_t iter_key;
927 struct cds_ja_inode_flag **node_flag_ptr; /* in parent */
928 struct cds_ja_inode_flag *node_flag,
929 *parent_node_flag,
930 *parent2_node_flag;
931 int ret;
932
933 if (caa_unlikely(key > ja->key_max))
934 return -EINVAL;
935 tree_depth = ja->tree_depth;
936
937 retry:
938 dbg_printf("cds_ja_add attempt: key %" PRIu64 ", node %p\n",
939 key, new_node);
940 iter_key = key;
941 parent2_node_flag = NULL;
942 parent_node_flag =
943 (struct cds_ja_inode_flag *) &ja->root; /* Use root ptr address as key for mutex */
944 node_flag_ptr = &ja->root;
945 node_flag = rcu_dereference(*node_flag_ptr);
946
947 /* Iterate on all internal levels */
948 for (i = 1; i < tree_depth; i++) {
949 if (!ja_node_ptr(node_flag)) {
950 ret = ja_attach_node(ja, node_flag_ptr,
951 parent_node_flag, parent2_node_flag,
952 key, i, new_node);
953 if (ret == -ENOENT || ret == -EEXIST)
954 goto retry;
955 else
956 goto end;
957 }
958 parent2_node_flag = parent_node_flag;
959 parent_node_flag = node_flag;
960 node_flag = ja_node_get_nth(node_flag,
961 &node_flag_ptr,
962 (unsigned char) iter_key);
963 iter_key >>= JA_BITS_PER_BYTE;
964 }
965
966 /*
967 * We reached bottom of tree, simply add node to last internal
968 * level, or chain it if key is already present.
969 */
970 if (!ja_node_ptr(node_flag)) {
971 ret = ja_attach_node(ja, node_flag_ptr, parent_node_flag,
972 parent2_node_flag, key, i, new_node);
973 } else {
974 ret = ja_chain_node(ja,
975 (struct cds_hlist_head *) ja_node_ptr(node_flag),
976 new_node);
977 }
978 if (ret == -ENOENT)
979 goto retry;
980 end:
981 return ret;
982 }
983
984 struct cds_ja *_cds_ja_new(unsigned int key_bits,
985 const struct rcu_flavor_struct *flavor)
986 {
987 struct cds_ja *ja;
988 int ret;
989 struct cds_ja_shadow_node *root_shadow_node;
990
991 ja = calloc(sizeof(*ja), 1);
992 if (!ja)
993 goto ja_error;
994
995 switch (key_bits) {
996 case 8:
997 ja->key_max = UINT8_MAX;
998 break;
999 case 16:
1000 ja->key_max = UINT16_MAX;
1001 break;
1002 case 32:
1003 ja->key_max = UINT32_MAX;
1004 break;
1005 case 64:
1006 ja->key_max = UINT64_MAX;
1007 break;
1008 default:
1009 goto check_error;
1010 }
1011
1012 /* ja->root is NULL */
1013 /* tree_depth 0 is for pointer to root node */
1014 ja->tree_depth = (key_bits >> JA_BITS_PER_BYTE) + 1;
1015 assert(ja->tree_depth <= JA_MAX_DEPTH);
1016 ja->ht = rcuja_create_ht(flavor);
1017 if (!ja->ht)
1018 goto ht_error;
1019
1020 /*
1021 * Note: we should not free this node until judy array destroy.
1022 */
1023 root_shadow_node = rcuja_shadow_set(ja->ht,
1024 ja_node_ptr((struct cds_ja_inode_flag *) &ja->root),
1025 NULL);
1026 if (!root_shadow_node) {
1027 ret = -ENOMEM;
1028 goto ht_node_error;
1029 }
1030
1031 return ja;
1032
1033 ht_node_error:
1034 ret = rcuja_delete_ht(ja->ht);
1035 assert(!ret);
1036 ht_error:
1037 check_error:
1038 free(ja);
1039 ja_error:
1040 return NULL;
1041 }
1042
1043 /*
1044 * There should be no more concurrent add to the judy array while it is
1045 * being destroyed (ensured by the caller).
1046 */
1047 int cds_ja_destroy(struct cds_ja *ja)
1048 {
1049 int ret;
1050
1051 rcuja_shadow_prune(ja->ht,
1052 RCUJA_SHADOW_CLEAR_FREE_NODE | RCUJA_SHADOW_CLEAR_FREE_LOCK);
1053 ret = rcuja_delete_ht(ja->ht);
1054 if (ret)
1055 return ret;
1056 if (uatomic_read(&ja->nr_fallback))
1057 fprintf(stderr,
1058 "[warning] RCU Judy Array used %lu fallback node(s)\n",
1059 uatomic_read(&ja->nr_fallback));
1060 free(ja);
1061 return 0;
1062 }
This page took 0.049213 seconds and 5 git commands to generate.