6b54219795cf78ac2eb28583cdb13a214d204878
[urcu.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_LOG2_BITS_PER_BYTE 3U
75 #define JA_BITS_PER_BYTE (1U << JA_LOG2_BITS_PER_BYTE)
76
77 #define JA_MAX_DEPTH 9 /* Maximum depth, including leafs */
78
79 /*
80 * Entry for NULL node is at index 8 of the table. It is never encoded
81 * in flags.
82 */
83 #define NODE_INDEX_NULL 8
84
85 /*
86 * Number of removals needed on a fallback node before we try to shrink
87 * it.
88 */
89 #define JA_FALLBACK_REMOVAL_COUNT 8
90
91 /*
92 * Iteration on the array to find the right node size for the number of
93 * children stops when it reaches .max_child == 256 (this is the largest
94 * possible node size, which contains 256 children).
95 * The min_child overlaps with the previous max_child to provide an
96 * hysteresis loop to reallocation for patterns of cyclic add/removal
97 * within the same node.
98 * The node the index within the following arrays is represented on 3
99 * bits. It identifies the node type, min/max number of children, and
100 * the size order.
101 * The max_child values for the RCU_JA_POOL below result from
102 * statistical approximation: over million populations, the max_child
103 * covers between 97% and 99% of the populations generated. Therefore, a
104 * fallback should exist to cover the rare extreme population unbalance
105 * cases, but it will not have a major impact on speed nor space
106 * consumption, since those are rare cases.
107 */
108
109 #if (CAA_BITS_PER_LONG < 64)
110 /* 32-bit pointers */
111 enum {
112 ja_type_0_max_child = 1,
113 ja_type_1_max_child = 3,
114 ja_type_2_max_child = 6,
115 ja_type_3_max_child = 12,
116 ja_type_4_max_child = 25,
117 ja_type_5_max_child = 48,
118 ja_type_6_max_child = 92,
119 ja_type_7_max_child = 256,
120 ja_type_8_max_child = 0, /* NULL */
121 };
122
123 enum {
124 ja_type_0_max_linear_child = 1,
125 ja_type_1_max_linear_child = 3,
126 ja_type_2_max_linear_child = 6,
127 ja_type_3_max_linear_child = 12,
128 ja_type_4_max_linear_child = 25,
129 ja_type_5_max_linear_child = 24,
130 ja_type_6_max_linear_child = 23,
131 };
132
133 enum {
134 ja_type_5_nr_pool_order = 1,
135 ja_type_6_nr_pool_order = 2,
136 };
137
138 const struct cds_ja_type ja_types[] = {
139 { .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, },
140 { .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, },
141 { .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, },
142 { .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, },
143 { .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, },
144
145 /* Pools may fill sooner than max_child */
146 { .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, },
147 { .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, },
148
149 /*
150 * TODO: Upon node removal below min_child, if child pool is
151 * filled beyond capacity, we need to roll back to pigeon.
152 */
153 { .type_class = RCU_JA_PIGEON, .min_child = 89, .max_child = ja_type_7_max_child, .order = 10, },
154
155 { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, },
156 };
157 #else /* !(CAA_BITS_PER_LONG < 64) */
158 /* 64-bit pointers */
159 enum {
160 ja_type_0_max_child = 1,
161 ja_type_1_max_child = 3,
162 ja_type_2_max_child = 7,
163 ja_type_3_max_child = 14,
164 ja_type_4_max_child = 28,
165 ja_type_5_max_child = 54,
166 ja_type_6_max_child = 104,
167 ja_type_7_max_child = 256,
168 ja_type_8_max_child = 256,
169 };
170
171 enum {
172 ja_type_0_max_linear_child = 1,
173 ja_type_1_max_linear_child = 3,
174 ja_type_2_max_linear_child = 7,
175 ja_type_3_max_linear_child = 14,
176 ja_type_4_max_linear_child = 28,
177 ja_type_5_max_linear_child = 27,
178 ja_type_6_max_linear_child = 26,
179 };
180
181 enum {
182 ja_type_5_nr_pool_order = 1,
183 ja_type_6_nr_pool_order = 2,
184 };
185
186 const struct cds_ja_type ja_types[] = {
187 { .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, },
188 { .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, },
189 { .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, },
190 { .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, },
191 { .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, },
192
193 /* Pools may fill sooner than max_child. */
194 { .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, },
195 { .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, },
196
197 /*
198 * TODO: Upon node removal below min_child, if child pool is
199 * filled beyond capacity, we need to roll back to pigeon.
200 */
201 { .type_class = RCU_JA_PIGEON, .min_child = 101, .max_child = ja_type_7_max_child, .order = 11, },
202
203 { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, },
204 };
205 #endif /* !(BITS_PER_LONG < 64) */
206
207 static inline __attribute__((unused))
208 void static_array_size_check(void)
209 {
210 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) < JA_TYPE_MAX_NR);
211 }
212
213 /*
214 * The cds_ja_node contains the compressed node data needed for
215 * read-side. For linear and pool node configurations, it starts with a
216 * byte counting the number of children in the node. Then, the
217 * node-specific data is placed.
218 * The node mutex, if any is needed, protecting concurrent updated of
219 * each node is placed in a separate hash table indexed by node address.
220 * For the pigeon configuration, the number of children is also kept in
221 * a separate hash table, indexed by node address, because it is only
222 * required for updates.
223 */
224
225 #define DECLARE_LINEAR_NODE(index) \
226 struct { \
227 uint8_t nr_child; \
228 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
229 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
230 }
231
232 #define DECLARE_POOL_NODE(index) \
233 struct { \
234 struct { \
235 uint8_t nr_child; \
236 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
237 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
238 } linear[1U << ja_type_## index ##_nr_pool_order]; \
239 }
240
241 struct cds_ja_inode {
242 union {
243 /* Linear configuration */
244 DECLARE_LINEAR_NODE(0) conf_0;
245 DECLARE_LINEAR_NODE(1) conf_1;
246 DECLARE_LINEAR_NODE(2) conf_2;
247 DECLARE_LINEAR_NODE(3) conf_3;
248 DECLARE_LINEAR_NODE(4) conf_4;
249
250 /* Pool configuration */
251 DECLARE_POOL_NODE(5) conf_5;
252 DECLARE_POOL_NODE(6) conf_6;
253
254 /* Pigeon configuration */
255 struct {
256 struct cds_ja_inode_flag *child[ja_type_7_max_child];
257 } conf_7;
258 /* data aliasing nodes for computed accesses */
259 uint8_t data[sizeof(struct cds_ja_inode_flag *) * ja_type_7_max_child];
260 } u;
261 };
262
263 enum ja_recompact {
264 JA_RECOMPACT,
265 JA_RECOMPACT_ADD,
266 JA_RECOMPACT_DEL,
267 };
268
269 static
270 struct cds_ja_inode_flag *ja_node_flag(struct cds_ja_inode *node,
271 unsigned long type)
272 {
273 assert(type < (1UL << JA_TYPE_BITS));
274 return (struct cds_ja_inode_flag *) (((unsigned long) node) | type);
275 }
276
277 static
278 struct cds_ja_inode *ja_node_ptr(struct cds_ja_inode_flag *node)
279 {
280 return (struct cds_ja_inode *) (((unsigned long) node) & JA_PTR_MASK);
281 }
282
283 static
284 unsigned long ja_node_type(struct cds_ja_inode_flag *node)
285 {
286 unsigned long type;
287
288 if (ja_node_ptr(node) == NULL) {
289 return NODE_INDEX_NULL;
290 }
291 type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK);
292 assert(type < (1UL << JA_TYPE_BITS));
293 return type;
294 }
295
296 struct cds_ja_inode *alloc_cds_ja_node(const struct cds_ja_type *ja_type)
297 {
298 return calloc(1U << ja_type->order, sizeof(char));
299 }
300
301 void free_cds_ja_node(struct cds_ja_inode *node)
302 {
303 free(node);
304 }
305
306 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
307 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
308 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
309 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
310
311 static
312 uint8_t *align_ptr_size(uint8_t *ptr)
313 {
314 return (uint8_t *) JA_ALIGN((unsigned long) ptr, sizeof(void *));
315 }
316
317 static
318 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type *type,
319 struct cds_ja_inode *node)
320 {
321 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
322 return rcu_dereference(node->u.data[0]);
323 }
324
325 /*
326 * The order in which values and pointers are does does not matter: if
327 * a value is missing, we return NULL. If a value is there, but its
328 * associated pointers is still NULL, we return NULL too.
329 */
330 static
331 struct cds_ja_inode_flag *ja_linear_node_get_nth(const struct cds_ja_type *type,
332 struct cds_ja_inode *node,
333 struct cds_ja_inode_flag ***child_node_flag_ptr,
334 uint8_t n)
335 {
336 uint8_t nr_child;
337 uint8_t *values;
338 struct cds_ja_inode_flag **pointers;
339 struct cds_ja_inode_flag *ptr;
340 unsigned int i;
341
342 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
343
344 nr_child = ja_linear_node_get_nr_child(type, node);
345 cmm_smp_rmb(); /* read nr_child before values and pointers */
346 assert(nr_child <= type->max_linear_child);
347 assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child);
348
349 values = &node->u.data[1];
350 for (i = 0; i < nr_child; i++) {
351 if (CMM_LOAD_SHARED(values[i]) == n)
352 break;
353 }
354 if (i >= nr_child)
355 return NULL;
356 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
357 ptr = rcu_dereference(pointers[i]);
358 if (caa_unlikely(child_node_flag_ptr) && ptr)
359 *child_node_flag_ptr = &pointers[i];
360 return ptr;
361 }
362
363 static
364 void ja_linear_node_get_ith_pos(const struct cds_ja_type *type,
365 struct cds_ja_inode *node,
366 uint8_t i,
367 uint8_t *v,
368 struct cds_ja_inode_flag **iter)
369 {
370 uint8_t *values;
371 struct cds_ja_inode_flag **pointers;
372
373 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
374 assert(i < ja_linear_node_get_nr_child(type, node));
375
376 values = &node->u.data[1];
377 *v = values[i];
378 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
379 *iter = pointers[i];
380 }
381
382 static
383 struct cds_ja_inode_flag *ja_pool_node_get_nth(const struct cds_ja_type *type,
384 struct cds_ja_inode *node,
385 struct cds_ja_inode_flag ***child_node_flag_ptr,
386 uint8_t n)
387 {
388 struct cds_ja_inode *linear;
389
390 assert(type->type_class == RCU_JA_POOL);
391 /*
392 * TODO: currently, we select the pool by highest bits. We
393 * should support various encodings.
394 */
395 linear = (struct cds_ja_inode *)
396 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
397 return ja_linear_node_get_nth(type, linear, child_node_flag_ptr, n);
398 }
399
400 static
401 struct cds_ja_inode *ja_pool_node_get_ith_pool(const struct cds_ja_type *type,
402 struct cds_ja_inode *node,
403 uint8_t i)
404 {
405 assert(type->type_class == RCU_JA_POOL);
406 return (struct cds_ja_inode *)
407 &node->u.data[(unsigned int) i << type->pool_size_order];
408 }
409
410 static
411 struct cds_ja_inode_flag *ja_pigeon_node_get_nth(const struct cds_ja_type *type,
412 struct cds_ja_inode *node,
413 struct cds_ja_inode_flag ***child_node_flag_ptr,
414 uint8_t n)
415 {
416 struct cds_ja_inode_flag **child_node_flag;
417
418 assert(type->type_class == RCU_JA_PIGEON);
419 child_node_flag = &((struct cds_ja_inode_flag **) node->u.data)[n];
420 dbg_printf("ja_pigeon_node_get_nth child_node_flag_ptr %p\n",
421 child_node_flag);
422 if (caa_unlikely(child_node_flag_ptr) && *child_node_flag)
423 *child_node_flag_ptr = child_node_flag;
424 return rcu_dereference(*child_node_flag);
425 }
426
427 static
428 struct cds_ja_inode_flag *ja_pigeon_node_get_ith_pos(const struct cds_ja_type *type,
429 struct cds_ja_inode *node,
430 uint8_t i)
431 {
432 return ja_pigeon_node_get_nth(type, node, NULL, i);
433 }
434
435 /*
436 * ja_node_get_nth: get nth item from a node.
437 * node_flag is already rcu_dereference'd.
438 */
439 static
440 struct cds_ja_inode_flag * ja_node_get_nth(struct cds_ja_inode_flag *node_flag,
441 struct cds_ja_inode_flag ***child_node_flag_ptr,
442 uint8_t n)
443 {
444 unsigned int type_index;
445 struct cds_ja_inode *node;
446 const struct cds_ja_type *type;
447
448 node = ja_node_ptr(node_flag);
449 assert(node != NULL);
450 type_index = ja_node_type(node_flag);
451 type = &ja_types[type_index];
452
453 switch (type->type_class) {
454 case RCU_JA_LINEAR:
455 return ja_linear_node_get_nth(type, node,
456 child_node_flag_ptr, n);
457 case RCU_JA_POOL:
458 return ja_pool_node_get_nth(type, node,
459 child_node_flag_ptr, n);
460 case RCU_JA_PIGEON:
461 return ja_pigeon_node_get_nth(type, node,
462 child_node_flag_ptr, n);
463 default:
464 assert(0);
465 return (void *) -1UL;
466 }
467 }
468
469 static
470 int ja_linear_node_set_nth(const struct cds_ja_type *type,
471 struct cds_ja_inode *node,
472 struct cds_ja_shadow_node *shadow_node,
473 uint8_t n,
474 struct cds_ja_inode_flag *child_node_flag)
475 {
476 uint8_t nr_child;
477 uint8_t *values, *nr_child_ptr;
478 struct cds_ja_inode_flag **pointers;
479 unsigned int i, unused = 0;
480
481 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
482
483 nr_child_ptr = &node->u.data[0];
484 dbg_printf("linear set nth: nr_child_ptr %p\n", nr_child_ptr);
485 nr_child = *nr_child_ptr;
486 assert(nr_child <= type->max_linear_child);
487
488 values = &node->u.data[1];
489 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
490 /* Check if node value is already populated */
491 for (i = 0; i < nr_child; i++) {
492 if (values[i] == n) {
493 if (pointers[i])
494 return -EEXIST;
495 else
496 break;
497 } else {
498 if (!pointers[i])
499 unused++;
500 }
501 }
502 if (i == nr_child && nr_child >= type->max_linear_child) {
503 if (unused)
504 return -ERANGE; /* recompact node */
505 else
506 return -ENOSPC; /* No space left in this node type */
507 }
508
509 assert(pointers[i] == NULL);
510 rcu_assign_pointer(pointers[i], child_node_flag);
511 /* If we expanded the nr_child, increment it */
512 if (i == nr_child) {
513 CMM_STORE_SHARED(values[nr_child], n);
514 /* write pointer and value before nr_child */
515 cmm_smp_wmb();
516 CMM_STORE_SHARED(*nr_child_ptr, nr_child + 1);
517 }
518 shadow_node->nr_child++;
519 dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n",
520 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr),
521 (unsigned int) shadow_node->nr_child,
522 node, shadow_node);
523
524 return 0;
525 }
526
527 static
528 int ja_pool_node_set_nth(const struct cds_ja_type *type,
529 struct cds_ja_inode *node,
530 struct cds_ja_shadow_node *shadow_node,
531 uint8_t n,
532 struct cds_ja_inode_flag *child_node_flag)
533 {
534 struct cds_ja_inode *linear;
535
536 assert(type->type_class == RCU_JA_POOL);
537 linear = (struct cds_ja_inode *)
538 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
539 return ja_linear_node_set_nth(type, linear, shadow_node,
540 n, child_node_flag);
541 }
542
543 static
544 int ja_pigeon_node_set_nth(const struct cds_ja_type *type,
545 struct cds_ja_inode *node,
546 struct cds_ja_shadow_node *shadow_node,
547 uint8_t n,
548 struct cds_ja_inode_flag *child_node_flag)
549 {
550 struct cds_ja_inode_flag **ptr;
551
552 assert(type->type_class == RCU_JA_PIGEON);
553 ptr = &((struct cds_ja_inode_flag **) node->u.data)[n];
554 if (*ptr)
555 return -EEXIST;
556 rcu_assign_pointer(*ptr, child_node_flag);
557 shadow_node->nr_child++;
558 return 0;
559 }
560
561 /*
562 * _ja_node_set_nth: set nth item within a node. Return an error
563 * (negative error value) if it is already there.
564 */
565 static
566 int _ja_node_set_nth(const struct cds_ja_type *type,
567 struct cds_ja_inode *node,
568 struct cds_ja_shadow_node *shadow_node,
569 uint8_t n,
570 struct cds_ja_inode_flag *child_node_flag)
571 {
572 switch (type->type_class) {
573 case RCU_JA_LINEAR:
574 return ja_linear_node_set_nth(type, node, shadow_node, n,
575 child_node_flag);
576 case RCU_JA_POOL:
577 return ja_pool_node_set_nth(type, node, shadow_node, n,
578 child_node_flag);
579 case RCU_JA_PIGEON:
580 return ja_pigeon_node_set_nth(type, node, shadow_node, n,
581 child_node_flag);
582 case RCU_JA_NULL:
583 return -ENOSPC;
584 default:
585 assert(0);
586 return -EINVAL;
587 }
588
589 return 0;
590 }
591
592 static
593 int ja_linear_node_clear_ptr(const struct cds_ja_type *type,
594 struct cds_ja_inode *node,
595 struct cds_ja_shadow_node *shadow_node,
596 struct cds_ja_inode_flag **node_flag_ptr)
597 {
598 uint8_t nr_child;
599 uint8_t *nr_child_ptr;
600
601 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
602
603 nr_child_ptr = &node->u.data[0];
604 dbg_printf("linear clear ptr: nr_child_ptr %p\n", nr_child_ptr);
605 nr_child = *nr_child_ptr;
606 assert(nr_child <= type->max_linear_child);
607
608 if (shadow_node->fallback_removal_count) {
609 shadow_node->fallback_removal_count--;
610 } else {
611 if (shadow_node->nr_child <= type->min_child) {
612 /* We need to try recompacting the node */
613 return -EFBIG;
614 }
615 }
616 assert(*node_flag_ptr != NULL);
617 rcu_assign_pointer(*node_flag_ptr, NULL);
618 /*
619 * Value and nr_child are never changed (would cause ABA issue).
620 * Instead, we leave the pointer to NULL and recompact the node
621 * once in a while. It is allowed to set a NULL pointer to a new
622 * value without recompaction though.
623 * Only update the shadow node accounting.
624 */
625 shadow_node->nr_child--;
626 dbg_printf("linear clear ptr: %u child, shadow: %u child, for node %p shadow %p\n",
627 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr),
628 (unsigned int) shadow_node->nr_child,
629 node, shadow_node);
630
631 return 0;
632 }
633
634 static
635 int ja_pool_node_clear_ptr(const struct cds_ja_type *type,
636 struct cds_ja_inode *node,
637 struct cds_ja_shadow_node *shadow_node,
638 struct cds_ja_inode_flag **node_flag_ptr,
639 uint8_t n)
640 {
641 struct cds_ja_inode *linear;
642
643 assert(type->type_class == RCU_JA_POOL);
644 linear = (struct cds_ja_inode *)
645 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
646 return ja_linear_node_clear_ptr(type, linear, shadow_node, node_flag_ptr);
647 }
648
649 static
650 int ja_pigeon_node_clear_ptr(const struct cds_ja_type *type,
651 struct cds_ja_inode *node,
652 struct cds_ja_shadow_node *shadow_node,
653 struct cds_ja_inode_flag **node_flag_ptr)
654 {
655 assert(type->type_class == RCU_JA_PIGEON);
656 rcu_assign_pointer(*node_flag_ptr, NULL);
657 shadow_node->nr_child--;
658 return 0;
659 }
660
661 /*
662 * _ja_node_clear_ptr: clear ptr item within a node. Return an error
663 * (negative error value) if it is not found (-ENOENT).
664 */
665 static
666 int _ja_node_clear_ptr(const struct cds_ja_type *type,
667 struct cds_ja_inode *node,
668 struct cds_ja_shadow_node *shadow_node,
669 struct cds_ja_inode_flag **node_flag_ptr,
670 uint8_t n)
671 {
672 switch (type->type_class) {
673 case RCU_JA_LINEAR:
674 return ja_linear_node_clear_ptr(type, node, shadow_node, node_flag_ptr);
675 case RCU_JA_POOL:
676 return ja_pool_node_clear_ptr(type, node, shadow_node, node_flag_ptr, n);
677 case RCU_JA_PIGEON:
678 return ja_pigeon_node_clear_ptr(type, node, shadow_node, node_flag_ptr);
679 case RCU_JA_NULL:
680 return -ENOENT;
681 default:
682 assert(0);
683 return -EINVAL;
684 }
685
686 return 0;
687 }
688
689 /*
690 * ja_node_recompact_add: recompact a node, adding a new child.
691 * TODO: for pool type, take selection bit(s) into account.
692 * Return 0 on success, -EAGAIN if need to retry, or other negative
693 * error value otherwise.
694 */
695 static
696 int ja_node_recompact(enum ja_recompact mode,
697 struct cds_ja *ja,
698 unsigned int old_type_index,
699 const struct cds_ja_type *old_type,
700 struct cds_ja_inode *old_node,
701 struct cds_ja_shadow_node *shadow_node,
702 struct cds_ja_inode_flag **old_node_flag, uint8_t n,
703 struct cds_ja_inode_flag *child_node_flag,
704 struct cds_ja_inode_flag **nullify_node_flag_ptr)
705 {
706 unsigned int new_type_index;
707 struct cds_ja_inode *new_node;
708 struct cds_ja_shadow_node *new_shadow_node = NULL;
709 const struct cds_ja_type *new_type;
710 struct cds_ja_inode_flag *new_node_flag;
711 int ret;
712 int fallback = 0;
713
714 switch (mode) {
715 case JA_RECOMPACT:
716 new_type_index = old_type_index;
717 break;
718 case JA_RECOMPACT_ADD:
719 if (!shadow_node || old_type_index == NODE_INDEX_NULL) {
720 new_type_index = 0;
721 } else {
722 new_type_index = old_type_index + 1;
723 }
724 break;
725 case JA_RECOMPACT_DEL:
726 if (old_type_index == 0) {
727 new_type_index = NODE_INDEX_NULL;
728 } else {
729 new_type_index = old_type_index - 1;
730 }
731 break;
732 default:
733 assert(0);
734 }
735
736 retry: /* for fallback */
737 dbg_printf("Recompact from type %d to type %d\n",
738 old_type_index, new_type_index);
739 new_type = &ja_types[new_type_index];
740 if (new_type_index != NODE_INDEX_NULL) {
741 new_node = alloc_cds_ja_node(new_type);
742 if (!new_node)
743 return -ENOMEM;
744 new_node_flag = ja_node_flag(new_node, new_type_index);
745 dbg_printf("Recompact inherit lock from %p\n", shadow_node);
746 new_shadow_node = rcuja_shadow_set(ja->ht, new_node, shadow_node);
747 if (!new_shadow_node) {
748 free(new_node);
749 return -ENOMEM;
750 }
751 if (fallback)
752 new_shadow_node->fallback_removal_count =
753 JA_FALLBACK_REMOVAL_COUNT;
754 } else {
755 new_node = NULL;
756 new_node_flag = NULL;
757 }
758
759 assert(mode != JA_RECOMPACT_ADD || old_type->type_class != RCU_JA_PIGEON);
760
761 if (new_type_index == NODE_INDEX_NULL)
762 goto skip_copy;
763
764 switch (old_type->type_class) {
765 case RCU_JA_LINEAR:
766 {
767 uint8_t nr_child =
768 ja_linear_node_get_nr_child(old_type, old_node);
769 unsigned int i;
770
771 for (i = 0; i < nr_child; i++) {
772 struct cds_ja_inode_flag *iter;
773 uint8_t v;
774
775 ja_linear_node_get_ith_pos(old_type, old_node, i, &v, &iter);
776 if (!iter)
777 continue;
778 if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter)
779 continue;
780 ret = _ja_node_set_nth(new_type, new_node,
781 new_shadow_node,
782 v, iter);
783 if (new_type->type_class == RCU_JA_POOL && ret) {
784 goto fallback_toosmall;
785 }
786 assert(!ret);
787 }
788 break;
789 }
790 case RCU_JA_POOL:
791 {
792 unsigned int pool_nr;
793
794 for (pool_nr = 0; pool_nr < (1U << old_type->nr_pool_order); pool_nr++) {
795 struct cds_ja_inode *pool =
796 ja_pool_node_get_ith_pool(old_type,
797 old_node, pool_nr);
798 uint8_t nr_child =
799 ja_linear_node_get_nr_child(old_type, pool);
800 unsigned int j;
801
802 for (j = 0; j < nr_child; j++) {
803 struct cds_ja_inode_flag *iter;
804 uint8_t v;
805
806 ja_linear_node_get_ith_pos(old_type, pool,
807 j, &v, &iter);
808 if (!iter)
809 continue;
810 if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter)
811 continue;
812 ret = _ja_node_set_nth(new_type, new_node,
813 new_shadow_node,
814 v, iter);
815 if (new_type->type_class == RCU_JA_POOL
816 && ret) {
817 goto fallback_toosmall;
818 }
819 assert(!ret);
820 }
821 }
822 break;
823 }
824 case RCU_JA_NULL:
825 assert(mode == JA_RECOMPACT_ADD);
826 break;
827 case RCU_JA_PIGEON:
828 {
829 uint8_t nr_child;
830 unsigned int i;
831
832 assert(mode == JA_RECOMPACT_DEL);
833 nr_child = shadow_node->nr_child;
834 for (i = 0; i < nr_child; i++) {
835 struct cds_ja_inode_flag *iter;
836
837 iter = ja_pigeon_node_get_ith_pos(old_type, old_node, i);
838 if (!iter)
839 continue;
840 if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter)
841 continue;
842 ret = _ja_node_set_nth(new_type, new_node,
843 new_shadow_node,
844 i, iter);
845 if (new_type->type_class == RCU_JA_POOL && ret) {
846 goto fallback_toosmall;
847 }
848 assert(!ret);
849 }
850 break;
851 }
852 default:
853 assert(0);
854 ret = -EINVAL;
855 goto end;
856 }
857 skip_copy:
858
859 if (JA_RECOMPACT_ADD) {
860 /* add node */
861 ret = _ja_node_set_nth(new_type, new_node,
862 new_shadow_node,
863 n, child_node_flag);
864 assert(!ret);
865 }
866 /* Return pointer to new recompacted node through old_node_flag */
867 *old_node_flag = new_node_flag;
868 if (old_node) {
869 int flags;
870
871 flags = RCUJA_SHADOW_CLEAR_FREE_NODE;
872 /*
873 * It is OK to free the lock associated with a node
874 * going to NULL, since we are holding the parent lock.
875 * This synchronizes removal with re-add of that node.
876 */
877 if (new_type_index == NODE_INDEX_NULL)
878 flags = RCUJA_SHADOW_CLEAR_FREE_LOCK;
879 ret = rcuja_shadow_clear(ja->ht, old_node, shadow_node,
880 flags);
881 assert(!ret);
882 }
883
884 ret = 0;
885 end:
886 return ret;
887
888 fallback_toosmall:
889 /* fallback if next pool is too small */
890 assert(new_shadow_node);
891 ret = rcuja_shadow_clear(ja->ht, new_node, new_shadow_node,
892 RCUJA_SHADOW_CLEAR_FREE_NODE);
893 assert(!ret);
894
895 /* Choose fallback type: pigeon */
896 new_type_index = (1UL << JA_TYPE_BITS) - 1;
897 dbg_printf("Fallback to type %d\n", new_type_index);
898 uatomic_inc(&ja->nr_fallback);
899 fallback = 1;
900 goto retry;
901 }
902
903 /*
904 * Return 0 on success, -EAGAIN if need to retry, or other negative
905 * error value otherwise.
906 */
907 static
908 int ja_node_set_nth(struct cds_ja *ja,
909 struct cds_ja_inode_flag **node_flag, uint8_t n,
910 struct cds_ja_inode_flag *child_node_flag,
911 struct cds_ja_shadow_node *shadow_node)
912 {
913 int ret;
914 unsigned int type_index;
915 const struct cds_ja_type *type;
916 struct cds_ja_inode *node;
917
918 dbg_printf("ja_node_set_nth for n=%u, node %p, shadow %p\n",
919 (unsigned int) n, ja_node_ptr(*node_flag), shadow_node);
920
921 node = ja_node_ptr(*node_flag);
922 type_index = ja_node_type(*node_flag);
923 type = &ja_types[type_index];
924 ret = _ja_node_set_nth(type, node, shadow_node,
925 n, child_node_flag);
926 switch (ret) {
927 case -ENOSPC:
928 /* Not enough space in node, need to recompact. */
929 ret = ja_node_recompact(JA_RECOMPACT_ADD, ja, type_index, type, node,
930 shadow_node, node_flag, n, child_node_flag, NULL);
931 break;
932 case -ERANGE:
933 /* Node needs to be recompacted. */
934 ret = ja_node_recompact(JA_RECOMPACT, ja, type_index, type, node,
935 shadow_node, node_flag, n, child_node_flag, NULL);
936 break;
937 }
938 return ret;
939 }
940
941 /*
942 * Return 0 on success, -EAGAIN if need to retry, or other negative
943 * error value otherwise.
944 */
945 static
946 int ja_node_clear_ptr(struct cds_ja *ja,
947 struct cds_ja_inode_flag **node_flag_ptr, /* Pointer to location to nullify */
948 struct cds_ja_inode_flag **parent_node_flag_ptr, /* Address of parent ptr in its parent */
949 struct cds_ja_shadow_node *shadow_node, /* of parent */
950 uint8_t n)
951 {
952 int ret;
953 unsigned int type_index;
954 const struct cds_ja_type *type;
955 struct cds_ja_inode *node;
956
957 dbg_printf("ja_node_clear_ptr for node %p, shadow %p, target ptr %p\n",
958 ja_node_ptr(*parent_node_flag_ptr), shadow_node, node_flag_ptr);
959
960 node = ja_node_ptr(*parent_node_flag_ptr);
961 type_index = ja_node_type(*parent_node_flag_ptr);
962 type = &ja_types[type_index];
963 ret = _ja_node_clear_ptr(type, node, shadow_node, node_flag_ptr, n);
964 if (ret == -EFBIG) {
965 /* Should to try recompaction. */
966 ret = ja_node_recompact(JA_RECOMPACT_DEL, ja, type_index, type, node,
967 shadow_node, parent_node_flag_ptr, n, NULL,
968 node_flag_ptr);
969 }
970 return ret;
971 }
972
973 struct cds_hlist_head cds_ja_lookup(struct cds_ja *ja, uint64_t key)
974 {
975 unsigned int tree_depth, i;
976 struct cds_ja_inode_flag *node_flag;
977 struct cds_hlist_head head = { NULL };
978
979 if (caa_unlikely(key > ja->key_max))
980 return head;
981 tree_depth = ja->tree_depth;
982 node_flag = rcu_dereference(ja->root);
983
984 /* level 0: root node */
985 if (!ja_node_ptr(node_flag))
986 return head;
987
988 for (i = 1; i < tree_depth; i++) {
989 uint8_t iter_key;
990
991 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1)));
992 node_flag = ja_node_get_nth(node_flag, NULL,
993 iter_key);
994 dbg_printf("cds_ja_lookup iter key lookup %u finds node_flag %p\n",
995 (unsigned int) iter_key, node_flag);
996 if (!ja_node_ptr(node_flag))
997 return head;
998 }
999
1000 /* Last level lookup succeded. We got an actual match. */
1001 head.next = (struct cds_hlist_node *) node_flag;
1002 return head;
1003 }
1004
1005 /*
1006 * We reached an unpopulated node. Create it and the children we need,
1007 * and then attach the entire branch to the current node. This may
1008 * trigger recompaction of the current node. Locks needed: node lock
1009 * (for add), and, possibly, parent node lock (to update pointer due to
1010 * node recompaction).
1011 *
1012 * First take node lock, check if recompaction is needed, then take
1013 * parent lock (if needed). Then we can proceed to create the new
1014 * branch. Publish the new branch, and release locks.
1015 * TODO: we currently always take the parent lock even when not needed.
1016 */
1017 static
1018 int ja_attach_node(struct cds_ja *ja,
1019 struct cds_ja_inode_flag **node_flag_ptr,
1020 struct cds_ja_inode_flag *node_flag,
1021 struct cds_ja_inode_flag *parent_node_flag,
1022 uint64_t key,
1023 unsigned int level,
1024 struct cds_ja_node *child_node)
1025 {
1026 struct cds_ja_shadow_node *shadow_node = NULL,
1027 *parent_shadow_node = NULL;
1028 struct cds_ja_inode *node = ja_node_ptr(node_flag);
1029 struct cds_ja_inode *parent_node = ja_node_ptr(parent_node_flag);
1030 struct cds_hlist_head head;
1031 struct cds_ja_inode_flag *iter_node_flag, *iter_dest_node_flag;
1032 int ret, i;
1033 struct cds_ja_inode_flag *created_nodes[JA_MAX_DEPTH];
1034 int nr_created_nodes = 0;
1035
1036 dbg_printf("Attach node at level %u (node %p, node_flag %p)\n",
1037 level, node, node_flag);
1038
1039 assert(node);
1040 shadow_node = rcuja_shadow_lookup_lock(ja->ht, node);
1041 if (!shadow_node) {
1042 ret = -EAGAIN;
1043 goto end;
1044 }
1045 if (parent_node) {
1046 parent_shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1047 parent_node);
1048 if (!parent_shadow_node) {
1049 ret = -EAGAIN;
1050 goto unlock_shadow;
1051 }
1052 }
1053
1054 /* Create new branch, starting from bottom */
1055 CDS_INIT_HLIST_HEAD(&head);
1056 cds_hlist_add_head_rcu(&child_node->list, &head);
1057 iter_node_flag = (struct cds_ja_inode_flag *) head.next;
1058
1059 for (i = ja->tree_depth; i > (int) level; i--) {
1060 uint8_t iter_key;
1061
1062 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (ja->tree_depth - i)));
1063 dbg_printf("branch creation level %d, key %u\n",
1064 i - 1, (unsigned int) iter_key);
1065 iter_dest_node_flag = NULL;
1066 ret = ja_node_set_nth(ja, &iter_dest_node_flag,
1067 iter_key,
1068 iter_node_flag,
1069 NULL);
1070 if (ret)
1071 goto check_error;
1072 created_nodes[nr_created_nodes++] = iter_dest_node_flag;
1073 iter_node_flag = iter_dest_node_flag;
1074 }
1075
1076 if (level > 1) {
1077 uint8_t iter_key;
1078
1079 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (ja->tree_depth - level)));
1080 /* We need to use set_nth on the previous level. */
1081 iter_dest_node_flag = node_flag;
1082 ret = ja_node_set_nth(ja, &iter_dest_node_flag,
1083 iter_key,
1084 iter_node_flag,
1085 shadow_node);
1086 if (ret)
1087 goto check_error;
1088 created_nodes[nr_created_nodes++] = iter_dest_node_flag;
1089 iter_node_flag = iter_dest_node_flag;
1090 }
1091
1092 /* Publish new branch */
1093 dbg_printf("Publish branch %p, replacing %p\n",
1094 iter_node_flag, *node_flag_ptr);
1095 rcu_assign_pointer(*node_flag_ptr, iter_node_flag);
1096
1097 /* Success */
1098 ret = 0;
1099
1100 check_error:
1101 if (ret) {
1102 for (i = 0; i < nr_created_nodes; i++) {
1103 int tmpret;
1104 int flags;
1105
1106 flags = RCUJA_SHADOW_CLEAR_FREE_LOCK;
1107 if (i)
1108 flags |= RCUJA_SHADOW_CLEAR_FREE_NODE;
1109 tmpret = rcuja_shadow_clear(ja->ht,
1110 ja_node_ptr(created_nodes[i]),
1111 NULL,
1112 flags);
1113 assert(!tmpret);
1114 }
1115 }
1116 if (parent_shadow_node)
1117 rcuja_shadow_unlock(parent_shadow_node);
1118 unlock_shadow:
1119 if (shadow_node)
1120 rcuja_shadow_unlock(shadow_node);
1121 end:
1122 return ret;
1123 }
1124
1125 /*
1126 * Lock the parent containing the hlist head pointer, and add node to list of
1127 * duplicates. Failure can happen if concurrent update changes the
1128 * parent before we get the lock. We return -EAGAIN in that case.
1129 * Return 0 on success, negative error value on failure.
1130 */
1131 static
1132 int ja_chain_node(struct cds_ja *ja,
1133 struct cds_ja_inode_flag *parent_node_flag,
1134 struct cds_hlist_head *head,
1135 struct cds_ja_node *node)
1136 {
1137 struct cds_ja_shadow_node *shadow_node;
1138
1139 shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1140 ja_node_ptr(parent_node_flag));
1141 if (!shadow_node)
1142 return -EAGAIN;
1143 cds_hlist_add_head_rcu(&node->list, head);
1144 rcuja_shadow_unlock(shadow_node);
1145 return 0;
1146 }
1147
1148 int cds_ja_add(struct cds_ja *ja, uint64_t key,
1149 struct cds_ja_node *new_node)
1150 {
1151 unsigned int tree_depth, i;
1152 struct cds_ja_inode_flag **node_flag_ptr; /* in parent */
1153 struct cds_ja_inode_flag *node_flag,
1154 *parent_node_flag,
1155 *parent2_node_flag;
1156 int ret;
1157
1158 if (caa_unlikely(key > ja->key_max))
1159 return -EINVAL;
1160 tree_depth = ja->tree_depth;
1161
1162 retry:
1163 dbg_printf("cds_ja_add attempt: key %" PRIu64 ", node %p\n",
1164 key, new_node);
1165 parent2_node_flag = NULL;
1166 parent_node_flag =
1167 (struct cds_ja_inode_flag *) &ja->root; /* Use root ptr address as key for mutex */
1168 node_flag_ptr = &ja->root;
1169 node_flag = rcu_dereference(ja->root);
1170
1171 /* Iterate on all internal levels */
1172 for (i = 1; i < tree_depth; i++) {
1173 uint8_t iter_key;
1174
1175 dbg_printf("cds_ja_add iter node_flag_ptr %p node_flag %p\n",
1176 *node_flag_ptr, node_flag);
1177 if (!ja_node_ptr(node_flag)) {
1178 ret = ja_attach_node(ja, node_flag_ptr,
1179 parent_node_flag, parent2_node_flag,
1180 key, i, new_node);
1181 if (ret == -EAGAIN || ret == -EEXIST)
1182 goto retry;
1183 else
1184 goto end;
1185 }
1186 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1)));
1187 parent2_node_flag = parent_node_flag;
1188 parent_node_flag = node_flag;
1189 node_flag = ja_node_get_nth(node_flag,
1190 &node_flag_ptr,
1191 iter_key);
1192 dbg_printf("cds_ja_add iter key lookup %u finds node_flag %p node_flag_ptr %p\n",
1193 (unsigned int) iter_key, node_flag, *node_flag_ptr);
1194 }
1195
1196 /*
1197 * We reached bottom of tree, simply add node to last internal
1198 * level, or chain it if key is already present.
1199 */
1200 if (!ja_node_ptr(node_flag)) {
1201 dbg_printf("cds_ja_add last node_flag_ptr %p node_flag %p\n",
1202 *node_flag_ptr, node_flag);
1203 ret = ja_attach_node(ja, node_flag_ptr, parent_node_flag,
1204 parent2_node_flag, key, i, new_node);
1205 } else {
1206 ret = ja_chain_node(ja,
1207 parent_node_flag,
1208 (struct cds_hlist_head *) node_flag_ptr,
1209 new_node);
1210 }
1211 if (ret == -EAGAIN)
1212 goto retry;
1213 end:
1214 return ret;
1215 }
1216
1217 /*
1218 * Note: there is no need to lookup the pointer address associated with
1219 * each node's nth item after taking the lock: it's already been done by
1220 * cds_ja_del while holding the rcu read-side lock, and our node rules
1221 * ensure that when a match value -> pointer is found in a node, it is
1222 * _NEVER_ changed for that node without recompaction, and recompaction
1223 * reallocates the node.
1224 */
1225 static
1226 int ja_detach_node(struct cds_ja *ja,
1227 struct cds_ja_inode_flag **snapshot,
1228 struct cds_ja_inode_flag ***snapshot_ptr,
1229 uint8_t *snapshot_n,
1230 int nr_snapshot,
1231 uint64_t key,
1232 struct cds_ja_node *node)
1233 {
1234 struct cds_ja_shadow_node *shadow_nodes[JA_MAX_DEPTH];
1235 struct cds_ja_inode_flag **node_flag_ptr = NULL,
1236 *parent_node_flag = NULL,
1237 **parent_node_flag_ptr = NULL;
1238 struct cds_ja_inode_flag *iter_node_flag;
1239 int ret, i, nr_shadow = 0, nr_clear = 0;
1240 uint8_t n;
1241
1242 assert(nr_snapshot == ja->tree_depth - 1);
1243
1244 /*
1245 * From the last internal level node going up, get the node
1246 * lock, check if the node has only one child left. If it is the
1247 * case, we continue iterating upward. When we reach a node
1248 * which has more that one child left, we lock the parent, and
1249 * proceed to the node deletion (removing its children too).
1250 */
1251 for (i = nr_snapshot - 1; i >= 1; i--) {
1252 struct cds_ja_shadow_node *shadow_node;
1253
1254 shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1255 ja_node_ptr(snapshot[i]));
1256 if (!shadow_node) {
1257 ret = -EAGAIN;
1258 goto end;
1259 }
1260 assert(shadow_node->nr_child > 0);
1261 shadow_nodes[nr_shadow++] = shadow_node;
1262 nr_clear++;
1263 if (i == nr_snapshot - 1) {
1264 /*
1265 * Re-check that last internal node level has
1266 * only one child, else trigger a retry.
1267 */
1268 if (shadow_node->nr_child != 1) {
1269 ret = -EAGAIN;
1270 goto end;
1271 }
1272 }
1273 if (shadow_node->nr_child > 1 || i == 1) {
1274 /* Lock parent and break */
1275 shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1276 ja_node_ptr(snapshot[i - 1]));
1277 if (!shadow_node) {
1278 ret = -EAGAIN;
1279 goto end;
1280 }
1281 shadow_nodes[nr_shadow++] = shadow_node;
1282 node_flag_ptr = snapshot_ptr[i];
1283 n = snapshot_n[i];
1284 parent_node_flag_ptr = snapshot_ptr[i - 1];
1285 parent_node_flag = snapshot[i - 1];
1286 if (i > 1) {
1287 /*
1288 * Lock parent's parent, in case we need
1289 * to recompact parent.
1290 */
1291 shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1292 ja_node_ptr(snapshot[i - 2]));
1293 if (!shadow_node) {
1294 ret = -EAGAIN;
1295 goto end;
1296 }
1297 shadow_nodes[nr_shadow++] = shadow_node;
1298 }
1299 break;
1300 }
1301 }
1302
1303 /*
1304 * At this point, we want to delete all nodes in shadow_nodes
1305 * (except the last one, which is either the root or the parent
1306 * of the upmost node with 1 child). OK to as to free lock here,
1307 * because RCU read lock is held, and free only performed in
1308 * call_rcu.
1309 */
1310
1311 for (i = 0; i < nr_clear; i++) {
1312 ret = rcuja_shadow_clear(ja->ht,
1313 shadow_nodes[i]->node,
1314 shadow_nodes[i],
1315 RCUJA_SHADOW_CLEAR_FREE_NODE
1316 | RCUJA_SHADOW_CLEAR_FREE_LOCK);
1317 assert(!ret);
1318 }
1319
1320 iter_node_flag = parent_node_flag;
1321 /* Remove from parent */
1322 ret = ja_node_clear_ptr(ja,
1323 node_flag_ptr, /* Pointer to location to nullify */
1324 &iter_node_flag, /* Old new parent ptr in its parent */
1325 shadow_nodes[nr_clear], /* of parent */
1326 n);
1327
1328 /* Update address of parent ptr in its parent */
1329 rcu_assign_pointer(*parent_node_flag_ptr, iter_node_flag);
1330
1331 end:
1332 for (i = 0; i < nr_shadow; i++)
1333 rcuja_shadow_unlock(shadow_nodes[i]);
1334 return ret;
1335 }
1336
1337 static
1338 int ja_unchain_node(struct cds_ja *ja,
1339 struct cds_ja_inode_flag *parent_node_flag,
1340 struct cds_ja_node *node)
1341 {
1342 struct cds_ja_shadow_node *shadow_node;
1343 int ret = 0;
1344
1345 shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1346 ja_node_ptr(parent_node_flag));
1347 if (!shadow_node)
1348 return -EAGAIN;
1349 /*
1350 * Retry if another thread removed all but one of duplicates
1351 * since check.
1352 */
1353 if (shadow_node->nr_child == 1) {
1354 ret = -EAGAIN;
1355 goto end;
1356 }
1357 cds_hlist_del_rcu(&node->list);
1358 end:
1359 rcuja_shadow_unlock(shadow_node);
1360 return ret;
1361 }
1362
1363 /*
1364 * Called with RCU read lock held.
1365 */
1366 int cds_ja_del(struct cds_ja *ja, uint64_t key,
1367 struct cds_ja_node *node)
1368 {
1369 unsigned int tree_depth, i;
1370 struct cds_ja_inode_flag *snapshot[JA_MAX_DEPTH];
1371 struct cds_ja_inode_flag **snapshot_ptr[JA_MAX_DEPTH];
1372 uint8_t snapshot_n[JA_MAX_DEPTH];
1373 struct cds_ja_inode_flag *node_flag;
1374 struct cds_ja_inode_flag **prev_node_flag_ptr;
1375 int nr_snapshot = 0;
1376 int ret;
1377
1378 if (caa_unlikely(key > ja->key_max))
1379 return -EINVAL;
1380 tree_depth = ja->tree_depth;
1381
1382 retry:
1383 dbg_printf("cds_ja_del attempt: key %" PRIu64 ", node %p\n",
1384 key, node);
1385
1386 /* snapshot for level 0 is only for shadow node lookup */
1387 snapshot_n[nr_snapshot] = 0;
1388 snapshot_ptr[nr_snapshot] = NULL;
1389 snapshot[nr_snapshot++] = (struct cds_ja_inode_flag *) &ja->root;
1390 node_flag = rcu_dereference(ja->root);
1391 prev_node_flag_ptr = &ja->root;
1392
1393 /* Iterate on all internal levels */
1394 for (i = 1; i < tree_depth; i++) {
1395 uint8_t iter_key;
1396
1397 dbg_printf("cds_ja_del iter node_flag %p\n",
1398 node_flag);
1399 if (!ja_node_ptr(node_flag)) {
1400 return -ENOENT;
1401 }
1402 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1)));
1403 if (nr_snapshot <= 1)
1404 snapshot_n[nr_snapshot] = 0;
1405 else
1406 snapshot_n[nr_snapshot - 1] = iter_key;
1407
1408 snapshot_ptr[nr_snapshot] = prev_node_flag_ptr;
1409 snapshot[nr_snapshot++] = node_flag;
1410 node_flag = ja_node_get_nth(node_flag,
1411 &prev_node_flag_ptr,
1412 iter_key);
1413 dbg_printf("cds_ja_del iter key lookup %u finds node_flag %p, prev_node_flag_ptr %p\n",
1414 (unsigned int) iter_key, node_flag,
1415 prev_node_flag_ptr);
1416 }
1417
1418 /*
1419 * We reached bottom of tree, try to find the node we are trying
1420 * to remove. Fail if we cannot find it.
1421 */
1422 if (!ja_node_ptr(node_flag)) {
1423 return -ENOENT;
1424 } else {
1425 struct cds_hlist_head *hlist_head;
1426 struct cds_hlist_node *hlist_node;
1427 struct cds_ja_node *entry, *match = NULL;
1428 int count = 0;
1429
1430 hlist_head = (struct cds_hlist_head *) ja_node_ptr(node_flag);
1431 cds_hlist_for_each_entry_rcu(entry,
1432 hlist_node,
1433 hlist_head,
1434 list) {
1435 if (entry == node)
1436 match = entry;
1437 count++;
1438 }
1439 if (!match)
1440 return -ENOENT;
1441 assert(count > 0);
1442 if (count == 1) {
1443 /*
1444 * Removing last of duplicates.
1445 */
1446 snapshot_ptr[nr_snapshot] = prev_node_flag_ptr;
1447 snapshot[nr_snapshot++] = node_flag;
1448 ret = ja_detach_node(ja, snapshot, snapshot_ptr,
1449 snapshot_n, nr_snapshot, key, node);
1450 } else {
1451 ret = ja_unchain_node(ja, node_flag, entry);
1452 }
1453 }
1454 if (ret == -EAGAIN)
1455 goto retry;
1456 return ret;
1457 }
1458
1459 struct cds_ja *_cds_ja_new(unsigned int key_bits,
1460 const struct rcu_flavor_struct *flavor)
1461 {
1462 struct cds_ja *ja;
1463 int ret;
1464 struct cds_ja_shadow_node *root_shadow_node;
1465
1466 ja = calloc(sizeof(*ja), 1);
1467 if (!ja)
1468 goto ja_error;
1469
1470 switch (key_bits) {
1471 case 8:
1472 ja->key_max = UINT8_MAX;
1473 break;
1474 case 16:
1475 ja->key_max = UINT16_MAX;
1476 break;
1477 case 32:
1478 ja->key_max = UINT32_MAX;
1479 break;
1480 case 64:
1481 ja->key_max = UINT64_MAX;
1482 break;
1483 default:
1484 goto check_error;
1485 }
1486
1487 /* ja->root is NULL */
1488 /* tree_depth 0 is for pointer to root node */
1489 ja->tree_depth = (key_bits >> JA_LOG2_BITS_PER_BYTE) + 1;
1490 assert(ja->tree_depth <= JA_MAX_DEPTH);
1491 ja->ht = rcuja_create_ht(flavor);
1492 if (!ja->ht)
1493 goto ht_error;
1494
1495 /*
1496 * Note: we should not free this node until judy array destroy.
1497 */
1498 root_shadow_node = rcuja_shadow_set(ja->ht,
1499 ja_node_ptr((struct cds_ja_inode_flag *) &ja->root),
1500 NULL);
1501 if (!root_shadow_node) {
1502 ret = -ENOMEM;
1503 goto ht_node_error;
1504 }
1505 root_shadow_node->is_root = 1;
1506
1507 return ja;
1508
1509 ht_node_error:
1510 ret = rcuja_delete_ht(ja->ht);
1511 assert(!ret);
1512 ht_error:
1513 check_error:
1514 free(ja);
1515 ja_error:
1516 return NULL;
1517 }
1518
1519 /*
1520 * There should be no more concurrent add to the judy array while it is
1521 * being destroyed (ensured by the caller).
1522 */
1523 int cds_ja_destroy(struct cds_ja *ja)
1524 {
1525 int ret;
1526
1527 rcuja_shadow_prune(ja->ht,
1528 RCUJA_SHADOW_CLEAR_FREE_NODE | RCUJA_SHADOW_CLEAR_FREE_LOCK);
1529 ret = rcuja_delete_ht(ja->ht);
1530 if (ret)
1531 return ret;
1532 if (uatomic_read(&ja->nr_fallback))
1533 fprintf(stderr,
1534 "[warning] RCU Judy Array used %lu fallback node(s)\n",
1535 uatomic_read(&ja->nr_fallback));
1536 free(ja);
1537 return 0;
1538 }
This page took 0.056604 seconds and 3 git commands to generate.