rcuja: implement 1 dimension pool distribution
[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 <string.h>
28 #include <urcu/rcuja.h>
29 #include <urcu/compiler.h>
30 #include <urcu/arch.h>
31 #include <assert.h>
32 #include <urcu-pointer.h>
33 #include <urcu/uatomic.h>
34 #include <stdint.h>
35
36 #include "rcuja-internal.h"
37 #include "bitfield.h"
38
39 #ifndef abs
40 #define abs_int(a) ((int) (a) > 0 ? (int) (a) : -((int) (a)))
41 #endif
42
43 enum cds_ja_type_class {
44 RCU_JA_LINEAR = 0, /* Type A */
45 /* 32-bit: 1 to 25 children, 8 to 128 bytes */
46 /* 64-bit: 1 to 28 children, 16 to 256 bytes */
47 RCU_JA_POOL = 1, /* Type B */
48 /* 32-bit: 26 to 100 children, 256 to 512 bytes */
49 /* 64-bit: 29 to 112 children, 512 to 1024 bytes */
50 RCU_JA_PIGEON = 2, /* Type C */
51 /* 32-bit: 101 to 256 children, 1024 bytes */
52 /* 64-bit: 113 to 256 children, 2048 bytes */
53 /* Leaf nodes are implicit from their height in the tree */
54 RCU_JA_NR_TYPES,
55
56 RCU_JA_NULL, /* not an encoded type, but keeps code regular */
57 };
58
59 struct cds_ja_type {
60 enum cds_ja_type_class type_class;
61 uint16_t min_child; /* minimum number of children: 1 to 256 */
62 uint16_t max_child; /* maximum number of children: 1 to 256 */
63 uint16_t max_linear_child; /* per-pool max nr. children: 1 to 256 */
64 uint16_t order; /* node size is (1 << order), in bytes */
65 uint16_t nr_pool_order; /* number of pools */
66 uint16_t pool_size_order; /* pool size */
67 };
68
69 /*
70 * Iteration on the array to find the right node size for the number of
71 * children stops when it reaches .max_child == 256 (this is the largest
72 * possible node size, which contains 256 children).
73 * The min_child overlaps with the previous max_child to provide an
74 * hysteresis loop to reallocation for patterns of cyclic add/removal
75 * within the same node.
76 * The node the index within the following arrays is represented on 3
77 * bits. It identifies the node type, min/max number of children, and
78 * the size order.
79 * The max_child values for the RCU_JA_POOL below result from
80 * statistical approximation: over million populations, the max_child
81 * covers between 97% and 99% of the populations generated. Therefore, a
82 * fallback should exist to cover the rare extreme population unbalance
83 * cases, but it will not have a major impact on speed nor space
84 * consumption, since those are rare cases.
85 */
86
87 #if (CAA_BITS_PER_LONG < 64)
88 /* 32-bit pointers */
89 enum {
90 ja_type_0_max_child = 1,
91 ja_type_1_max_child = 3,
92 ja_type_2_max_child = 6,
93 ja_type_3_max_child = 12,
94 ja_type_4_max_child = 25,
95 ja_type_5_max_child = 48,
96 ja_type_6_max_child = 92,
97 ja_type_7_max_child = 256,
98 ja_type_8_max_child = 0, /* NULL */
99 };
100
101 enum {
102 ja_type_0_max_linear_child = 1,
103 ja_type_1_max_linear_child = 3,
104 ja_type_2_max_linear_child = 6,
105 ja_type_3_max_linear_child = 12,
106 ja_type_4_max_linear_child = 25,
107 ja_type_5_max_linear_child = 24,
108 ja_type_6_max_linear_child = 23,
109 };
110
111 enum {
112 ja_type_5_nr_pool_order = 1,
113 ja_type_6_nr_pool_order = 2,
114 };
115
116 const struct cds_ja_type ja_types[] = {
117 { .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, },
118 { .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, },
119 { .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, },
120 { .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, },
121 { .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, },
122
123 /* Pools may fill sooner than max_child */
124 { .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, },
125 { .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, },
126
127 /*
128 * Upon node removal below min_child, if child pool is filled
129 * beyond capacity, we roll back to pigeon.
130 */
131 { .type_class = RCU_JA_PIGEON, .min_child = 89, .max_child = ja_type_7_max_child, .order = 10, },
132
133 { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, },
134 };
135 #else /* !(CAA_BITS_PER_LONG < 64) */
136 /* 64-bit pointers */
137 enum {
138 ja_type_0_max_child = 1,
139 ja_type_1_max_child = 3,
140 ja_type_2_max_child = 7,
141 ja_type_3_max_child = 14,
142 ja_type_4_max_child = 28,
143 ja_type_5_max_child = 54,
144 ja_type_6_max_child = 104,
145 ja_type_7_max_child = 256,
146 ja_type_8_max_child = 256,
147 };
148
149 enum {
150 ja_type_0_max_linear_child = 1,
151 ja_type_1_max_linear_child = 3,
152 ja_type_2_max_linear_child = 7,
153 ja_type_3_max_linear_child = 14,
154 ja_type_4_max_linear_child = 28,
155 ja_type_5_max_linear_child = 27,
156 ja_type_6_max_linear_child = 26,
157 };
158
159 enum {
160 ja_type_5_nr_pool_order = 1,
161 ja_type_6_nr_pool_order = 2,
162 };
163
164 const struct cds_ja_type ja_types[] = {
165 { .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, },
166 { .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, },
167 { .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, },
168 { .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, },
169 { .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, },
170
171 /* Pools may fill sooner than max_child. */
172 { .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, },
173 { .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, },
174
175 /*
176 * Upon node removal below min_child, if child pool is filled
177 * beyond capacity, we roll back to pigeon.
178 */
179 { .type_class = RCU_JA_PIGEON, .min_child = 101, .max_child = ja_type_7_max_child, .order = 11, },
180
181 { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, },
182 };
183 #endif /* !(BITS_PER_LONG < 64) */
184
185 static inline __attribute__((unused))
186 void static_array_size_check(void)
187 {
188 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) < JA_TYPE_MAX_NR);
189 }
190
191 /*
192 * The cds_ja_node contains the compressed node data needed for
193 * read-side. For linear and pool node configurations, it starts with a
194 * byte counting the number of children in the node. Then, the
195 * node-specific data is placed.
196 * The node mutex, if any is needed, protecting concurrent updated of
197 * each node is placed in a separate hash table indexed by node address.
198 * For the pigeon configuration, the number of children is also kept in
199 * a separate hash table, indexed by node address, because it is only
200 * required for updates.
201 */
202
203 #define DECLARE_LINEAR_NODE(index) \
204 struct { \
205 uint8_t nr_child; \
206 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
207 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
208 }
209
210 #define DECLARE_POOL_NODE(index) \
211 struct { \
212 struct { \
213 uint8_t nr_child; \
214 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
215 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
216 } linear[1U << ja_type_## index ##_nr_pool_order]; \
217 }
218
219 struct cds_ja_inode {
220 union {
221 /* Linear configuration */
222 DECLARE_LINEAR_NODE(0) conf_0;
223 DECLARE_LINEAR_NODE(1) conf_1;
224 DECLARE_LINEAR_NODE(2) conf_2;
225 DECLARE_LINEAR_NODE(3) conf_3;
226 DECLARE_LINEAR_NODE(4) conf_4;
227
228 /* Pool configuration */
229 DECLARE_POOL_NODE(5) conf_5;
230 DECLARE_POOL_NODE(6) conf_6;
231
232 /* Pigeon configuration */
233 struct {
234 struct cds_ja_inode_flag *child[ja_type_7_max_child];
235 } conf_7;
236 /* data aliasing nodes for computed accesses */
237 uint8_t data[sizeof(struct cds_ja_inode_flag *) * ja_type_7_max_child];
238 } u;
239 };
240
241 enum ja_recompact {
242 JA_RECOMPACT,
243 JA_RECOMPACT_ADD,
244 JA_RECOMPACT_DEL,
245 };
246
247 static
248 struct cds_ja_inode *_ja_node_mask_ptr(struct cds_ja_inode_flag *node)
249 {
250 return (struct cds_ja_inode *) (((unsigned long) node) & JA_PTR_MASK);
251 }
252
253 unsigned long ja_node_type(struct cds_ja_inode_flag *node)
254 {
255 unsigned long type;
256
257 if (_ja_node_mask_ptr(node) == NULL) {
258 return NODE_INDEX_NULL;
259 }
260 type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK);
261 assert(type < (1UL << JA_TYPE_BITS));
262 return type;
263 }
264
265 struct cds_ja_inode *ja_node_ptr(struct cds_ja_inode_flag *node)
266 {
267 unsigned long type_index = ja_node_type(node);
268 const struct cds_ja_type *type;
269
270 type = &ja_types[type_index];
271 switch (type->type_class) {
272 case RCU_JA_LINEAR:
273 case RCU_JA_PIGEON: /* fall-through */
274 case RCU_JA_NULL: /* fall-through */
275 default: /* fall-through */
276 return _ja_node_mask_ptr(node);
277 case RCU_JA_POOL:
278 switch (type->nr_pool_order) {
279 case 1:
280 return (struct cds_ja_inode *) (((unsigned long) node) & ~(JA_POOL_1D_MASK | JA_TYPE_MASK));
281 case 2:
282 return (struct cds_ja_inode *) (((unsigned long) node) & ~(JA_POOL_2D_MASK | JA_POOL_1D_MASK | JA_TYPE_MASK));
283 default:
284 assert(0);
285 }
286 }
287 }
288
289 struct cds_ja_inode *alloc_cds_ja_node(const struct cds_ja_type *ja_type)
290 {
291 size_t len = 1U << ja_type->order;
292 void *p;
293 int ret;
294
295 ret = posix_memalign(&p, len, len);
296 if (ret || !p) {
297 return NULL;
298 }
299 memset(p, 0, len);
300 return p;
301 }
302
303 void free_cds_ja_node(struct cds_ja_inode *node)
304 {
305 free(node);
306 }
307
308 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
309 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
310 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
311 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
312
313 static
314 uint8_t *align_ptr_size(uint8_t *ptr)
315 {
316 return (uint8_t *) JA_ALIGN((unsigned long) ptr, sizeof(void *));
317 }
318
319 static
320 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type *type,
321 struct cds_ja_inode *node)
322 {
323 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
324 return rcu_dereference(node->u.data[0]);
325 }
326
327 /*
328 * The order in which values and pointers are does does not matter: if
329 * a value is missing, we return NULL. If a value is there, but its
330 * associated pointers is still NULL, we return NULL too.
331 */
332 static
333 struct cds_ja_inode_flag *ja_linear_node_get_nth(const struct cds_ja_type *type,
334 struct cds_ja_inode *node,
335 struct cds_ja_inode_flag ***child_node_flag_ptr,
336 struct cds_ja_inode_flag **child_node_flag_v,
337 struct cds_ja_inode_flag ***node_flag_ptr,
338 uint8_t n)
339 {
340 uint8_t nr_child;
341 uint8_t *values;
342 struct cds_ja_inode_flag **pointers;
343 struct cds_ja_inode_flag *ptr;
344 unsigned int i;
345
346 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
347
348 nr_child = ja_linear_node_get_nr_child(type, node);
349 cmm_smp_rmb(); /* read nr_child before values and pointers */
350 assert(nr_child <= type->max_linear_child);
351 assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child);
352
353 values = &node->u.data[1];
354 for (i = 0; i < nr_child; i++) {
355 if (CMM_LOAD_SHARED(values[i]) == n)
356 break;
357 }
358 if (i >= nr_child) {
359 if (caa_unlikely(node_flag_ptr))
360 *node_flag_ptr = NULL;
361 return NULL;
362 }
363 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
364 ptr = rcu_dereference(pointers[i]);
365 if (caa_unlikely(child_node_flag_ptr) && ptr)
366 *child_node_flag_ptr = &pointers[i];
367 if (caa_unlikely(child_node_flag_v) && ptr)
368 *child_node_flag_v = ptr;
369 if (caa_unlikely(node_flag_ptr))
370 *node_flag_ptr = &pointers[i];
371 return ptr;
372 }
373
374 static
375 void ja_linear_node_get_ith_pos(const struct cds_ja_type *type,
376 struct cds_ja_inode *node,
377 uint8_t i,
378 uint8_t *v,
379 struct cds_ja_inode_flag **iter)
380 {
381 uint8_t *values;
382 struct cds_ja_inode_flag **pointers;
383
384 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
385 assert(i < ja_linear_node_get_nr_child(type, node));
386
387 values = &node->u.data[1];
388 *v = values[i];
389 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
390 *iter = pointers[i];
391 }
392
393 static
394 struct cds_ja_inode_flag *ja_pool_node_get_nth(const struct cds_ja_type *type,
395 struct cds_ja_inode *node,
396 struct cds_ja_inode_flag *node_flag,
397 struct cds_ja_inode_flag ***child_node_flag_ptr,
398 struct cds_ja_inode_flag **child_node_flag_v,
399 struct cds_ja_inode_flag ***node_flag_ptr,
400 uint8_t n)
401 {
402 struct cds_ja_inode *linear;
403
404 assert(type->type_class == RCU_JA_POOL);
405
406 switch (type->nr_pool_order) {
407 case 1:
408 {
409 unsigned long bitsel, index;
410
411 bitsel = ja_node_pool_1d_bitsel(node_flag);
412 assert(bitsel < CHAR_BIT);
413 index = ((unsigned long) n >> bitsel) & type->nr_pool_order;
414 linear = (struct cds_ja_inode *) &node->u.data[index << type->pool_size_order];
415 break;
416 }
417 case 2:
418 {
419 /*
420 * TODO: currently, we select the pool by highest bits. We
421 * should support various encodings.
422 */
423 linear = (struct cds_ja_inode *)
424 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
425 break;
426 }
427 default:
428 linear = NULL;
429 assert(0);
430 }
431 return ja_linear_node_get_nth(type, linear, child_node_flag_ptr,
432 child_node_flag_v, node_flag_ptr, n);
433 }
434
435 static
436 struct cds_ja_inode *ja_pool_node_get_ith_pool(const struct cds_ja_type *type,
437 struct cds_ja_inode *node,
438 uint8_t i)
439 {
440 assert(type->type_class == RCU_JA_POOL);
441 return (struct cds_ja_inode *)
442 &node->u.data[(unsigned int) i << type->pool_size_order];
443 }
444
445 static
446 struct cds_ja_inode_flag *ja_pigeon_node_get_nth(const struct cds_ja_type *type,
447 struct cds_ja_inode *node,
448 struct cds_ja_inode_flag ***child_node_flag_ptr,
449 struct cds_ja_inode_flag **child_node_flag_v,
450 struct cds_ja_inode_flag ***node_flag_ptr,
451 uint8_t n)
452 {
453 struct cds_ja_inode_flag **child_node_flag;
454 struct cds_ja_inode_flag *child_node_flag_read;
455
456 assert(type->type_class == RCU_JA_PIGEON);
457 child_node_flag = &((struct cds_ja_inode_flag **) node->u.data)[n];
458 child_node_flag_read = rcu_dereference(*child_node_flag);
459 dbg_printf("ja_pigeon_node_get_nth child_node_flag_ptr %p\n",
460 child_node_flag);
461 if (caa_unlikely(child_node_flag_ptr) && child_node_flag_read)
462 *child_node_flag_ptr = child_node_flag;
463 if (caa_unlikely(child_node_flag_v) && child_node_flag_read)
464 *child_node_flag_v = child_node_flag_read;
465 if (caa_unlikely(node_flag_ptr))
466 *node_flag_ptr = child_node_flag;
467 return child_node_flag_read;
468 }
469
470 static
471 struct cds_ja_inode_flag *ja_pigeon_node_get_ith_pos(const struct cds_ja_type *type,
472 struct cds_ja_inode *node,
473 uint8_t i)
474 {
475 return ja_pigeon_node_get_nth(type, node, NULL, NULL, NULL, i);
476 }
477
478 /*
479 * ja_node_get_nth: get nth item from a node.
480 * node_flag is already rcu_dereference'd.
481 */
482 static
483 struct cds_ja_inode_flag *ja_node_get_nth(struct cds_ja_inode_flag *node_flag,
484 struct cds_ja_inode_flag ***child_node_flag_ptr,
485 struct cds_ja_inode_flag **child_node_flag,
486 struct cds_ja_inode_flag ***node_flag_ptr,
487 uint8_t n)
488 {
489 unsigned int type_index;
490 struct cds_ja_inode *node;
491 const struct cds_ja_type *type;
492
493 node = ja_node_ptr(node_flag);
494 assert(node != NULL);
495 type_index = ja_node_type(node_flag);
496 type = &ja_types[type_index];
497
498 switch (type->type_class) {
499 case RCU_JA_LINEAR:
500 return ja_linear_node_get_nth(type, node,
501 child_node_flag_ptr, child_node_flag,
502 node_flag_ptr, n);
503 case RCU_JA_POOL:
504 return ja_pool_node_get_nth(type, node, node_flag,
505 child_node_flag_ptr, child_node_flag,
506 node_flag_ptr, n);
507 case RCU_JA_PIGEON:
508 return ja_pigeon_node_get_nth(type, node,
509 child_node_flag_ptr, child_node_flag,
510 node_flag_ptr, n);
511 default:
512 assert(0);
513 return (void *) -1UL;
514 }
515 }
516
517 static
518 int ja_linear_node_set_nth(const struct cds_ja_type *type,
519 struct cds_ja_inode *node,
520 struct cds_ja_shadow_node *shadow_node,
521 uint8_t n,
522 struct cds_ja_inode_flag *child_node_flag)
523 {
524 uint8_t nr_child;
525 uint8_t *values, *nr_child_ptr;
526 struct cds_ja_inode_flag **pointers;
527 unsigned int i, unused = 0;
528
529 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
530
531 nr_child_ptr = &node->u.data[0];
532 dbg_printf("linear set nth: nr_child_ptr %p\n", nr_child_ptr);
533 nr_child = *nr_child_ptr;
534 assert(nr_child <= type->max_linear_child);
535
536 values = &node->u.data[1];
537 pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]);
538 /* Check if node value is already populated */
539 for (i = 0; i < nr_child; i++) {
540 if (values[i] == n) {
541 if (pointers[i])
542 return -EEXIST;
543 else
544 break;
545 } else {
546 if (!pointers[i])
547 unused++;
548 }
549 }
550 if (i == nr_child && nr_child >= type->max_linear_child) {
551 if (unused)
552 return -ERANGE; /* recompact node */
553 else
554 return -ENOSPC; /* No space left in this node type */
555 }
556
557 assert(pointers[i] == NULL);
558 rcu_assign_pointer(pointers[i], child_node_flag);
559 /* If we expanded the nr_child, increment it */
560 if (i == nr_child) {
561 CMM_STORE_SHARED(values[nr_child], n);
562 /* write pointer and value before nr_child */
563 cmm_smp_wmb();
564 CMM_STORE_SHARED(*nr_child_ptr, nr_child + 1);
565 }
566 shadow_node->nr_child++;
567 dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n",
568 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr),
569 (unsigned int) shadow_node->nr_child,
570 node, shadow_node);
571
572 return 0;
573 }
574
575 static
576 int ja_pool_node_set_nth(const struct cds_ja_type *type,
577 struct cds_ja_inode *node,
578 struct cds_ja_inode_flag *node_flag,
579 struct cds_ja_shadow_node *shadow_node,
580 uint8_t n,
581 struct cds_ja_inode_flag *child_node_flag)
582 {
583 struct cds_ja_inode *linear;
584
585 assert(type->type_class == RCU_JA_POOL);
586
587 switch (type->nr_pool_order) {
588 case 1:
589 {
590 unsigned long bitsel, index;
591
592 bitsel = ja_node_pool_1d_bitsel(node_flag);
593 assert(bitsel < CHAR_BIT);
594 index = ((unsigned long) n >> bitsel) & type->nr_pool_order;
595 linear = (struct cds_ja_inode *) &node->u.data[index << type->pool_size_order];
596 break;
597 }
598 case 2:
599 {
600 /*
601 * TODO: currently, we select the pool by highest bits. We
602 * should support various encodings.
603 */
604 linear = (struct cds_ja_inode *)
605 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
606 break;
607 }
608 default:
609 linear = NULL;
610 assert(0);
611 }
612
613 return ja_linear_node_set_nth(type, linear, shadow_node,
614 n, child_node_flag);
615 }
616
617 static
618 int ja_pigeon_node_set_nth(const struct cds_ja_type *type,
619 struct cds_ja_inode *node,
620 struct cds_ja_shadow_node *shadow_node,
621 uint8_t n,
622 struct cds_ja_inode_flag *child_node_flag)
623 {
624 struct cds_ja_inode_flag **ptr;
625
626 assert(type->type_class == RCU_JA_PIGEON);
627 ptr = &((struct cds_ja_inode_flag **) node->u.data)[n];
628 if (*ptr)
629 return -EEXIST;
630 rcu_assign_pointer(*ptr, child_node_flag);
631 shadow_node->nr_child++;
632 return 0;
633 }
634
635 /*
636 * _ja_node_set_nth: set nth item within a node. Return an error
637 * (negative error value) if it is already there.
638 */
639 static
640 int _ja_node_set_nth(const struct cds_ja_type *type,
641 struct cds_ja_inode *node,
642 struct cds_ja_inode_flag *node_flag,
643 struct cds_ja_shadow_node *shadow_node,
644 uint8_t n,
645 struct cds_ja_inode_flag *child_node_flag)
646 {
647 switch (type->type_class) {
648 case RCU_JA_LINEAR:
649 return ja_linear_node_set_nth(type, node, shadow_node, n,
650 child_node_flag);
651 case RCU_JA_POOL:
652 return ja_pool_node_set_nth(type, node, node_flag, shadow_node, n,
653 child_node_flag);
654 case RCU_JA_PIGEON:
655 return ja_pigeon_node_set_nth(type, node, shadow_node, n,
656 child_node_flag);
657 case RCU_JA_NULL:
658 return -ENOSPC;
659 default:
660 assert(0);
661 return -EINVAL;
662 }
663
664 return 0;
665 }
666
667 static
668 int ja_linear_node_clear_ptr(const struct cds_ja_type *type,
669 struct cds_ja_inode *node,
670 struct cds_ja_shadow_node *shadow_node,
671 struct cds_ja_inode_flag **node_flag_ptr)
672 {
673 uint8_t nr_child;
674 uint8_t *nr_child_ptr;
675
676 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
677
678 nr_child_ptr = &node->u.data[0];
679 dbg_printf("linear clear ptr: nr_child_ptr %p\n", nr_child_ptr);
680 nr_child = *nr_child_ptr;
681 assert(nr_child <= type->max_linear_child);
682
683 if (shadow_node->fallback_removal_count) {
684 shadow_node->fallback_removal_count--;
685 } else {
686 if (shadow_node->nr_child <= type->min_child) {
687 /* We need to try recompacting the node */
688 return -EFBIG;
689 }
690 }
691 assert(*node_flag_ptr != NULL);
692 rcu_assign_pointer(*node_flag_ptr, NULL);
693 /*
694 * Value and nr_child are never changed (would cause ABA issue).
695 * Instead, we leave the pointer to NULL and recompact the node
696 * once in a while. It is allowed to set a NULL pointer to a new
697 * value without recompaction though.
698 * Only update the shadow node accounting.
699 */
700 shadow_node->nr_child--;
701 dbg_printf("linear clear ptr: %u child, shadow: %u child, for node %p shadow %p\n",
702 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr),
703 (unsigned int) shadow_node->nr_child,
704 node, shadow_node);
705
706 return 0;
707 }
708
709 static
710 int ja_pool_node_clear_ptr(const struct cds_ja_type *type,
711 struct cds_ja_inode *node,
712 struct cds_ja_shadow_node *shadow_node,
713 struct cds_ja_inode_flag **node_flag_ptr,
714 uint8_t n)
715 {
716 struct cds_ja_inode *linear;
717
718 assert(type->type_class == RCU_JA_POOL);
719 linear = (struct cds_ja_inode *)
720 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
721 return ja_linear_node_clear_ptr(type, linear, shadow_node, node_flag_ptr);
722 }
723
724 static
725 int ja_pigeon_node_clear_ptr(const struct cds_ja_type *type,
726 struct cds_ja_inode *node,
727 struct cds_ja_shadow_node *shadow_node,
728 struct cds_ja_inode_flag **node_flag_ptr)
729 {
730 assert(type->type_class == RCU_JA_PIGEON);
731 dbg_printf("ja_pigeon_node_clear_ptr: clearing ptr: %p\n", *node_flag_ptr);
732 rcu_assign_pointer(*node_flag_ptr, NULL);
733 shadow_node->nr_child--;
734 return 0;
735 }
736
737 /*
738 * _ja_node_clear_ptr: clear ptr item within a node. Return an error
739 * (negative error value) if it is not found (-ENOENT).
740 */
741 static
742 int _ja_node_clear_ptr(const struct cds_ja_type *type,
743 struct cds_ja_inode *node,
744 struct cds_ja_shadow_node *shadow_node,
745 struct cds_ja_inode_flag **node_flag_ptr,
746 uint8_t n)
747 {
748 switch (type->type_class) {
749 case RCU_JA_LINEAR:
750 return ja_linear_node_clear_ptr(type, node, shadow_node, node_flag_ptr);
751 case RCU_JA_POOL:
752 return ja_pool_node_clear_ptr(type, node, shadow_node, node_flag_ptr, n);
753 case RCU_JA_PIGEON:
754 return ja_pigeon_node_clear_ptr(type, node, shadow_node, node_flag_ptr);
755 case RCU_JA_NULL:
756 return -ENOENT;
757 default:
758 assert(0);
759 return -EINVAL;
760 }
761
762 return 0;
763 }
764
765 /*
766 * Calculate bit distribution. Returns the bit (0 to 7) that splits the
767 * distribution in two sub-distributions containing as much elements one
768 * compared to the other.
769 */
770 static
771 unsigned int ja_node_sum_distribution_1d(enum ja_recompact mode,
772 struct cds_ja *ja,
773 unsigned int type_index,
774 const struct cds_ja_type *type,
775 struct cds_ja_inode *node,
776 struct cds_ja_shadow_node *shadow_node,
777 uint8_t n,
778 struct cds_ja_inode_flag *child_node_flag,
779 struct cds_ja_inode_flag **nullify_node_flag_ptr)
780 {
781 uint8_t nr_one[JA_BITS_PER_BYTE];
782 unsigned int bitsel = 0, bit_i, overall_best_distance = UINT_MAX;
783 unsigned int distrib_nr_child = 0;
784
785 memset(nr_one, 0, sizeof(nr_one));
786
787 switch (type->type_class) {
788 case RCU_JA_LINEAR:
789 {
790 uint8_t nr_child =
791 ja_linear_node_get_nr_child(type, node);
792 unsigned int i;
793
794 for (i = 0; i < nr_child; i++) {
795 struct cds_ja_inode_flag *iter;
796 unsigned int bit;
797 uint8_t v;
798
799 ja_linear_node_get_ith_pos(type, node, i, &v, &iter);
800 if (!iter)
801 continue;
802 if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter)
803 continue;
804 for (bit = 0; bit < JA_BITS_PER_BYTE; bit++) {
805 if (v & (1U << bit))
806 nr_one[bit]++;
807 }
808 distrib_nr_child++;
809 }
810 break;
811 }
812 case RCU_JA_POOL:
813 {
814 unsigned int pool_nr;
815
816 for (pool_nr = 0; pool_nr < (1U << type->nr_pool_order); pool_nr++) {
817 struct cds_ja_inode *pool =
818 ja_pool_node_get_ith_pool(type,
819 node, pool_nr);
820 uint8_t nr_child =
821 ja_linear_node_get_nr_child(type, pool);
822 unsigned int j;
823
824 for (j = 0; j < nr_child; j++) {
825 struct cds_ja_inode_flag *iter;
826 unsigned int bit;
827 uint8_t v;
828
829 ja_linear_node_get_ith_pos(type, pool,
830 j, &v, &iter);
831 if (!iter)
832 continue;
833 if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter)
834 continue;
835 for (bit = 0; bit < JA_BITS_PER_BYTE; bit++) {
836 if (v & (1U << bit))
837 nr_one[bit]++;
838 }
839 distrib_nr_child++;
840 }
841 }
842 break;
843 }
844 case RCU_JA_PIGEON:
845 {
846 uint8_t nr_child;
847 unsigned int i;
848
849 assert(mode == JA_RECOMPACT_DEL);
850 nr_child = shadow_node->nr_child;
851 for (i = 0; i < nr_child; i++) {
852 struct cds_ja_inode_flag *iter;
853 unsigned int bit;
854
855 iter = ja_pigeon_node_get_ith_pos(type, node, i);
856 if (!iter)
857 continue;
858 if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter)
859 continue;
860 for (bit = 0; bit < JA_BITS_PER_BYTE; bit++) {
861 if (i & (1U << bit))
862 nr_one[bit]++;
863 }
864 distrib_nr_child++;
865 }
866 break;
867 }
868 case RCU_JA_NULL:
869 assert(mode == JA_RECOMPACT_ADD);
870 break;
871 default:
872 assert(0);
873 break;
874 }
875
876 if (mode == JA_RECOMPACT_ADD) {
877 unsigned int bit;
878
879 for (bit = 0; bit < JA_BITS_PER_BYTE; bit++) {
880 if (n & (1U << bit))
881 nr_one[bit]++;
882 }
883 distrib_nr_child++;
884 }
885
886 /*
887 * The best bit selector is that for which the number of ones is
888 * closest to half of the number of children in the
889 * distribution.
890 */
891 for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) {
892 unsigned int distance_to_best;
893
894 distance_to_best = abs_int(nr_one[bit_i] - (distrib_nr_child >> 1U));
895 if (distance_to_best < overall_best_distance) {
896 overall_best_distance = distance_to_best;
897 bitsel = bit_i;
898 }
899 }
900 dbg_printf("1 dimension pool bit selection: (%u)\n", bitsel);
901 return bitsel;
902 }
903
904 /*
905 * ja_node_recompact_add: recompact a node, adding a new child.
906 * Return 0 on success, -EAGAIN if need to retry, or other negative
907 * error value otherwise.
908 */
909 static
910 int ja_node_recompact(enum ja_recompact mode,
911 struct cds_ja *ja,
912 unsigned int old_type_index,
913 const struct cds_ja_type *old_type,
914 struct cds_ja_inode *old_node,
915 struct cds_ja_shadow_node *shadow_node,
916 struct cds_ja_inode_flag **old_node_flag_ptr, uint8_t n,
917 struct cds_ja_inode_flag *child_node_flag,
918 struct cds_ja_inode_flag **nullify_node_flag_ptr)
919 {
920 unsigned int new_type_index;
921 struct cds_ja_inode *new_node;
922 struct cds_ja_shadow_node *new_shadow_node = NULL;
923 const struct cds_ja_type *new_type;
924 struct cds_ja_inode_flag *new_node_flag, *old_node_flag;
925 int ret;
926 int fallback = 0;
927
928 old_node_flag = *old_node_flag_ptr;
929
930 switch (mode) {
931 case JA_RECOMPACT:
932 new_type_index = old_type_index;
933 break;
934 case JA_RECOMPACT_ADD:
935 if (!shadow_node || old_type_index == NODE_INDEX_NULL) {
936 new_type_index = 0;
937 } else {
938 new_type_index = old_type_index + 1;
939 }
940 break;
941 case JA_RECOMPACT_DEL:
942 if (old_type_index == 0) {
943 new_type_index = NODE_INDEX_NULL;
944 } else {
945 new_type_index = old_type_index - 1;
946 }
947 break;
948 default:
949 assert(0);
950 }
951
952 retry: /* for fallback */
953 dbg_printf("Recompact from type %d to type %d\n",
954 old_type_index, new_type_index);
955 new_type = &ja_types[new_type_index];
956 if (new_type_index != NODE_INDEX_NULL) {
957 new_node = alloc_cds_ja_node(new_type);
958 if (!new_node)
959 return -ENOMEM;
960
961 if (new_type->type_class == RCU_JA_POOL) {
962 switch (new_type->nr_pool_order) {
963 case 1:
964 {
965 unsigned int node_distrib_bitsel = 0;
966 node_distrib_bitsel =
967 ja_node_sum_distribution_1d(mode, ja,
968 old_type_index, old_type,
969 old_node, shadow_node,
970 n, child_node_flag,
971 nullify_node_flag_ptr);
972 assert(!((unsigned long) new_node & JA_POOL_1D_MASK));
973 new_node_flag = ja_node_flag_pool_1d(new_node,
974 new_type_index, node_distrib_bitsel);
975 break;
976 }
977 case 2:
978 {
979 /* TODO: pool order 2 in 2d */
980 assert(!((unsigned long) new_node & JA_POOL_1D_MASK));
981 assert(!((unsigned long) new_node & JA_POOL_2D_MASK));
982 new_node_flag = ja_node_flag(new_node, new_type_index);
983 break;
984 }
985 default:
986 assert(0);
987 }
988 } else {
989 new_node_flag = ja_node_flag(new_node, new_type_index);
990 }
991
992 dbg_printf("Recompact inherit lock from %p\n", shadow_node);
993 new_shadow_node = rcuja_shadow_set(ja->ht, new_node_flag, shadow_node, ja);
994 if (!new_shadow_node) {
995 free(new_node);
996 return -ENOMEM;
997 }
998 if (fallback)
999 new_shadow_node->fallback_removal_count =
1000 JA_FALLBACK_REMOVAL_COUNT;
1001 } else {
1002 new_node = NULL;
1003 new_node_flag = NULL;
1004 }
1005
1006 assert(mode != JA_RECOMPACT_ADD || old_type->type_class != RCU_JA_PIGEON);
1007
1008 if (new_type_index == NODE_INDEX_NULL)
1009 goto skip_copy;
1010
1011 switch (old_type->type_class) {
1012 case RCU_JA_LINEAR:
1013 {
1014 uint8_t nr_child =
1015 ja_linear_node_get_nr_child(old_type, old_node);
1016 unsigned int i;
1017
1018 for (i = 0; i < nr_child; i++) {
1019 struct cds_ja_inode_flag *iter;
1020 uint8_t v;
1021
1022 ja_linear_node_get_ith_pos(old_type, old_node, i, &v, &iter);
1023 if (!iter)
1024 continue;
1025 if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter)
1026 continue;
1027 ret = _ja_node_set_nth(new_type, new_node, new_node_flag,
1028 new_shadow_node,
1029 v, iter);
1030 if (new_type->type_class == RCU_JA_POOL && ret) {
1031 goto fallback_toosmall;
1032 }
1033 assert(!ret);
1034 }
1035 break;
1036 }
1037 case RCU_JA_POOL:
1038 {
1039 unsigned int pool_nr;
1040
1041 for (pool_nr = 0; pool_nr < (1U << old_type->nr_pool_order); pool_nr++) {
1042 struct cds_ja_inode *pool =
1043 ja_pool_node_get_ith_pool(old_type,
1044 old_node, pool_nr);
1045 uint8_t nr_child =
1046 ja_linear_node_get_nr_child(old_type, pool);
1047 unsigned int j;
1048
1049 for (j = 0; j < nr_child; j++) {
1050 struct cds_ja_inode_flag *iter;
1051 uint8_t v;
1052
1053 ja_linear_node_get_ith_pos(old_type, pool,
1054 j, &v, &iter);
1055 if (!iter)
1056 continue;
1057 if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter)
1058 continue;
1059 ret = _ja_node_set_nth(new_type, new_node, new_node_flag,
1060 new_shadow_node,
1061 v, iter);
1062 if (new_type->type_class == RCU_JA_POOL
1063 && ret) {
1064 goto fallback_toosmall;
1065 }
1066 assert(!ret);
1067 }
1068 }
1069 break;
1070 }
1071 case RCU_JA_NULL:
1072 assert(mode == JA_RECOMPACT_ADD);
1073 break;
1074 case RCU_JA_PIGEON:
1075 {
1076 uint8_t nr_child;
1077 unsigned int i;
1078
1079 assert(mode == JA_RECOMPACT_DEL);
1080 nr_child = shadow_node->nr_child;
1081 for (i = 0; i < nr_child; i++) {
1082 struct cds_ja_inode_flag *iter;
1083
1084 iter = ja_pigeon_node_get_ith_pos(old_type, old_node, i);
1085 if (!iter)
1086 continue;
1087 if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter)
1088 continue;
1089 ret = _ja_node_set_nth(new_type, new_node, new_node_flag,
1090 new_shadow_node,
1091 i, iter);
1092 if (new_type->type_class == RCU_JA_POOL && ret) {
1093 goto fallback_toosmall;
1094 }
1095 assert(!ret);
1096 }
1097 break;
1098 }
1099 default:
1100 assert(0);
1101 ret = -EINVAL;
1102 goto end;
1103 }
1104 skip_copy:
1105
1106 if (mode == JA_RECOMPACT_ADD) {
1107 /* add node */
1108 ret = _ja_node_set_nth(new_type, new_node, new_node_flag,
1109 new_shadow_node,
1110 n, child_node_flag);
1111 if (new_type->type_class == RCU_JA_POOL && ret) {
1112 goto fallback_toosmall;
1113 }
1114 assert(!ret);
1115 }
1116 /* Return pointer to new recompacted node through old_node_flag_ptr */
1117 *old_node_flag_ptr = new_node_flag;
1118 if (old_node) {
1119 int flags;
1120
1121 flags = RCUJA_SHADOW_CLEAR_FREE_NODE;
1122 /*
1123 * It is OK to free the lock associated with a node
1124 * going to NULL, since we are holding the parent lock.
1125 * This synchronizes removal with re-add of that node.
1126 */
1127 if (new_type_index == NODE_INDEX_NULL)
1128 flags = RCUJA_SHADOW_CLEAR_FREE_LOCK;
1129 ret = rcuja_shadow_clear(ja->ht, old_node_flag, shadow_node,
1130 flags);
1131 assert(!ret);
1132 }
1133
1134 ret = 0;
1135 end:
1136 return ret;
1137
1138 fallback_toosmall:
1139 /* fallback if next pool is too small */
1140 assert(new_shadow_node);
1141 ret = rcuja_shadow_clear(ja->ht, new_node_flag, new_shadow_node,
1142 RCUJA_SHADOW_CLEAR_FREE_NODE);
1143 assert(!ret);
1144
1145 /* Choose fallback type: pigeon */
1146 new_type_index = (1UL << JA_TYPE_BITS) - 1;
1147 dbg_printf("Fallback to type %d\n", new_type_index);
1148 uatomic_inc(&ja->nr_fallback);
1149 fallback = 1;
1150 goto retry;
1151 }
1152
1153 /*
1154 * Return 0 on success, -EAGAIN if need to retry, or other negative
1155 * error value otherwise.
1156 */
1157 static
1158 int ja_node_set_nth(struct cds_ja *ja,
1159 struct cds_ja_inode_flag **node_flag, uint8_t n,
1160 struct cds_ja_inode_flag *child_node_flag,
1161 struct cds_ja_shadow_node *shadow_node)
1162 {
1163 int ret;
1164 unsigned int type_index;
1165 const struct cds_ja_type *type;
1166 struct cds_ja_inode *node;
1167
1168 dbg_printf("ja_node_set_nth for n=%u, node %p, shadow %p\n",
1169 (unsigned int) n, ja_node_ptr(*node_flag), shadow_node);
1170
1171 node = ja_node_ptr(*node_flag);
1172 type_index = ja_node_type(*node_flag);
1173 type = &ja_types[type_index];
1174 ret = _ja_node_set_nth(type, node, *node_flag, shadow_node,
1175 n, child_node_flag);
1176 switch (ret) {
1177 case -ENOSPC:
1178 /* Not enough space in node, need to recompact. */
1179 ret = ja_node_recompact(JA_RECOMPACT_ADD, ja, type_index, type, node,
1180 shadow_node, node_flag, n, child_node_flag, NULL);
1181 break;
1182 case -ERANGE:
1183 /* Node needs to be recompacted. */
1184 ret = ja_node_recompact(JA_RECOMPACT, ja, type_index, type, node,
1185 shadow_node, node_flag, n, child_node_flag, NULL);
1186 break;
1187 }
1188 return ret;
1189 }
1190
1191 /*
1192 * Return 0 on success, -EAGAIN if need to retry, or other negative
1193 * error value otherwise.
1194 */
1195 static
1196 int ja_node_clear_ptr(struct cds_ja *ja,
1197 struct cds_ja_inode_flag **node_flag_ptr, /* Pointer to location to nullify */
1198 struct cds_ja_inode_flag **parent_node_flag_ptr, /* Address of parent ptr in its parent */
1199 struct cds_ja_shadow_node *shadow_node, /* of parent */
1200 uint8_t n)
1201 {
1202 int ret;
1203 unsigned int type_index;
1204 const struct cds_ja_type *type;
1205 struct cds_ja_inode *node;
1206
1207 dbg_printf("ja_node_clear_ptr for node %p, shadow %p, target ptr %p\n",
1208 ja_node_ptr(*parent_node_flag_ptr), shadow_node, node_flag_ptr);
1209
1210 node = ja_node_ptr(*parent_node_flag_ptr);
1211 type_index = ja_node_type(*parent_node_flag_ptr);
1212 type = &ja_types[type_index];
1213 ret = _ja_node_clear_ptr(type, node, shadow_node, node_flag_ptr, n);
1214 if (ret == -EFBIG) {
1215 /* Should to try recompaction. */
1216 ret = ja_node_recompact(JA_RECOMPACT_DEL, ja, type_index, type, node,
1217 shadow_node, parent_node_flag_ptr, n, NULL,
1218 node_flag_ptr);
1219 }
1220 return ret;
1221 }
1222
1223 struct cds_hlist_head cds_ja_lookup(struct cds_ja *ja, uint64_t key)
1224 {
1225 unsigned int tree_depth, i;
1226 struct cds_ja_inode_flag *node_flag;
1227 struct cds_hlist_head head = { NULL };
1228
1229 if (caa_unlikely(key > ja->key_max))
1230 return head;
1231 tree_depth = ja->tree_depth;
1232 node_flag = rcu_dereference(ja->root);
1233
1234 /* level 0: root node */
1235 if (!ja_node_ptr(node_flag))
1236 return head;
1237
1238 for (i = 1; i < tree_depth; i++) {
1239 uint8_t iter_key;
1240
1241 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1)));
1242 node_flag = ja_node_get_nth(node_flag, NULL, NULL, NULL,
1243 iter_key);
1244 dbg_printf("cds_ja_lookup iter key lookup %u finds node_flag %p\n",
1245 (unsigned int) iter_key, node_flag);
1246 if (!ja_node_ptr(node_flag))
1247 return head;
1248 }
1249
1250 /* Last level lookup succeded. We got an actual match. */
1251 head.next = (struct cds_hlist_node *) node_flag;
1252 return head;
1253 }
1254
1255 /*
1256 * We reached an unpopulated node. Create it and the children we need,
1257 * and then attach the entire branch to the current node. This may
1258 * trigger recompaction of the current node. Locks needed: node lock
1259 * (for add), and, possibly, parent node lock (to update pointer due to
1260 * node recompaction).
1261 *
1262 * First take node lock, check if recompaction is needed, then take
1263 * parent lock (if needed). Then we can proceed to create the new
1264 * branch. Publish the new branch, and release locks.
1265 * TODO: we currently always take the parent lock even when not needed.
1266 */
1267 static
1268 int ja_attach_node(struct cds_ja *ja,
1269 struct cds_ja_inode_flag **attach_node_flag_ptr,
1270 struct cds_ja_inode_flag *attach_node_flag,
1271 struct cds_ja_inode_flag **node_flag_ptr,
1272 struct cds_ja_inode_flag *node_flag,
1273 struct cds_ja_inode_flag *parent_node_flag,
1274 uint64_t key,
1275 unsigned int level,
1276 struct cds_ja_node *child_node)
1277 {
1278 struct cds_ja_shadow_node *shadow_node = NULL,
1279 *parent_shadow_node = NULL;
1280 struct cds_ja_inode *node = ja_node_ptr(node_flag);
1281 struct cds_ja_inode *parent_node = ja_node_ptr(parent_node_flag);
1282 struct cds_hlist_head head;
1283 struct cds_ja_inode_flag *iter_node_flag, *iter_dest_node_flag;
1284 int ret, i;
1285 struct cds_ja_inode_flag *created_nodes[JA_MAX_DEPTH];
1286 int nr_created_nodes = 0;
1287
1288 dbg_printf("Attach node at level %u (node %p, node_flag %p)\n",
1289 level, node, node_flag);
1290
1291 assert(node);
1292 shadow_node = rcuja_shadow_lookup_lock(ja->ht, node_flag);
1293 if (!shadow_node) {
1294 ret = -EAGAIN;
1295 goto end;
1296 }
1297 if (parent_node) {
1298 parent_shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1299 parent_node_flag);
1300 if (!parent_shadow_node) {
1301 ret = -EAGAIN;
1302 goto unlock_shadow;
1303 }
1304 }
1305
1306 if (node_flag_ptr && ja_node_ptr(*node_flag_ptr)) {
1307 /*
1308 * Target node has been updated between RCU lookup and
1309 * lock acquisition. We need to re-try lookup and
1310 * attach.
1311 */
1312 ret = -EAGAIN;
1313 goto unlock_parent;
1314 }
1315
1316 if (attach_node_flag_ptr && ja_node_ptr(*attach_node_flag_ptr) !=
1317 ja_node_ptr(attach_node_flag)) {
1318 /*
1319 * Target node has been updated between RCU lookup and
1320 * lock acquisition. We need to re-try lookup and
1321 * attach.
1322 */
1323 ret = -EAGAIN;
1324 goto unlock_parent;
1325 }
1326
1327 /* Create new branch, starting from bottom */
1328 CDS_INIT_HLIST_HEAD(&head);
1329 cds_hlist_add_head_rcu(&child_node->list, &head);
1330 iter_node_flag = (struct cds_ja_inode_flag *) head.next;
1331
1332 for (i = ja->tree_depth; i > (int) level; i--) {
1333 uint8_t iter_key;
1334
1335 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (ja->tree_depth - i)));
1336 dbg_printf("branch creation level %d, key %u\n",
1337 i - 1, (unsigned int) iter_key);
1338 iter_dest_node_flag = NULL;
1339 ret = ja_node_set_nth(ja, &iter_dest_node_flag,
1340 iter_key,
1341 iter_node_flag,
1342 NULL);
1343 if (ret)
1344 goto check_error;
1345 created_nodes[nr_created_nodes++] = iter_dest_node_flag;
1346 iter_node_flag = iter_dest_node_flag;
1347 }
1348
1349 if (level > 1) {
1350 uint8_t iter_key;
1351
1352 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (ja->tree_depth - level)));
1353 /* We need to use set_nth on the previous level. */
1354 iter_dest_node_flag = node_flag;
1355 ret = ja_node_set_nth(ja, &iter_dest_node_flag,
1356 iter_key,
1357 iter_node_flag,
1358 shadow_node);
1359 if (ret)
1360 goto check_error;
1361 created_nodes[nr_created_nodes++] = iter_dest_node_flag;
1362 iter_node_flag = iter_dest_node_flag;
1363 }
1364
1365 /* Publish new branch */
1366 dbg_printf("Publish branch %p, replacing %p\n",
1367 iter_node_flag, *attach_node_flag_ptr);
1368 rcu_assign_pointer(*attach_node_flag_ptr, iter_node_flag);
1369
1370 /* Success */
1371 ret = 0;
1372
1373 check_error:
1374 if (ret) {
1375 for (i = 0; i < nr_created_nodes; i++) {
1376 int tmpret;
1377 int flags;
1378
1379 flags = RCUJA_SHADOW_CLEAR_FREE_LOCK;
1380 if (i)
1381 flags |= RCUJA_SHADOW_CLEAR_FREE_NODE;
1382 tmpret = rcuja_shadow_clear(ja->ht,
1383 created_nodes[i],
1384 NULL,
1385 flags);
1386 assert(!tmpret);
1387 }
1388 }
1389 unlock_parent:
1390 if (parent_shadow_node)
1391 rcuja_shadow_unlock(parent_shadow_node);
1392 unlock_shadow:
1393 if (shadow_node)
1394 rcuja_shadow_unlock(shadow_node);
1395 end:
1396 return ret;
1397 }
1398
1399 /*
1400 * Lock the parent containing the hlist head pointer, and add node to list of
1401 * duplicates. Failure can happen if concurrent update changes the
1402 * parent before we get the lock. We return -EAGAIN in that case.
1403 * Return 0 on success, negative error value on failure.
1404 */
1405 static
1406 int ja_chain_node(struct cds_ja *ja,
1407 struct cds_ja_inode_flag *parent_node_flag,
1408 struct cds_ja_inode_flag **node_flag_ptr,
1409 struct cds_ja_inode_flag *node_flag,
1410 struct cds_hlist_head *head,
1411 struct cds_ja_node *node)
1412 {
1413 struct cds_ja_shadow_node *shadow_node;
1414 int ret = 0;
1415
1416 shadow_node = rcuja_shadow_lookup_lock(ja->ht, parent_node_flag);
1417 if (!shadow_node) {
1418 return -EAGAIN;
1419 }
1420 if (ja_node_ptr(*node_flag_ptr) != ja_node_ptr(node_flag)) {
1421 ret = -EAGAIN;
1422 goto end;
1423 }
1424 cds_hlist_add_head_rcu(&node->list, head);
1425 end:
1426 rcuja_shadow_unlock(shadow_node);
1427 return ret;
1428 }
1429
1430 int cds_ja_add(struct cds_ja *ja, uint64_t key,
1431 struct cds_ja_node *new_node)
1432 {
1433 unsigned int tree_depth, i;
1434 struct cds_ja_inode_flag **attach_node_flag_ptr,
1435 **node_flag_ptr;
1436 struct cds_ja_inode_flag *node_flag,
1437 *parent_node_flag,
1438 *parent2_node_flag,
1439 *attach_node_flag;
1440 int ret;
1441
1442 if (caa_unlikely(key > ja->key_max)) {
1443 return -EINVAL;
1444 }
1445 tree_depth = ja->tree_depth;
1446
1447 retry:
1448 dbg_printf("cds_ja_add attempt: key %" PRIu64 ", node %p\n",
1449 key, new_node);
1450 parent2_node_flag = NULL;
1451 parent_node_flag =
1452 (struct cds_ja_inode_flag *) &ja->root; /* Use root ptr address as key for mutex */
1453 attach_node_flag_ptr = &ja->root;
1454 attach_node_flag = rcu_dereference(ja->root);
1455 node_flag_ptr = &ja->root;
1456 node_flag = rcu_dereference(ja->root);
1457
1458 /* Iterate on all internal levels */
1459 for (i = 1; i < tree_depth; i++) {
1460 uint8_t iter_key;
1461
1462 dbg_printf("cds_ja_add iter attach_node_flag_ptr %p node_flag_ptr %p node_flag %p\n",
1463 attach_node_flag_ptr, node_flag_ptr, node_flag);
1464 if (!ja_node_ptr(node_flag)) {
1465 ret = ja_attach_node(ja, attach_node_flag_ptr,
1466 attach_node_flag,
1467 node_flag_ptr,
1468 parent_node_flag,
1469 parent2_node_flag,
1470 key, i, new_node);
1471 if (ret == -EAGAIN || ret == -EEXIST)
1472 goto retry;
1473 else
1474 goto end;
1475 }
1476 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1)));
1477 parent2_node_flag = parent_node_flag;
1478 parent_node_flag = node_flag;
1479 node_flag = ja_node_get_nth(node_flag,
1480 &attach_node_flag_ptr,
1481 &attach_node_flag,
1482 &node_flag_ptr,
1483 iter_key);
1484 dbg_printf("cds_ja_add iter key lookup %u finds node_flag %p attach_node_flag_ptr %p node_flag_ptr %p\n",
1485 (unsigned int) iter_key, node_flag,
1486 attach_node_flag_ptr,
1487 node_flag_ptr);
1488 }
1489
1490 /*
1491 * We reached bottom of tree, simply add node to last internal
1492 * level, or chain it if key is already present.
1493 */
1494 if (!ja_node_ptr(node_flag)) {
1495 dbg_printf("cds_ja_add attach_node_flag_ptr %p node_flag_ptr %p node_flag %p\n",
1496 attach_node_flag_ptr, node_flag_ptr, node_flag);
1497 ret = ja_attach_node(ja, attach_node_flag_ptr,
1498 attach_node_flag,
1499 node_flag_ptr, parent_node_flag,
1500 parent2_node_flag, key, i, new_node);
1501 } else {
1502 ret = ja_chain_node(ja,
1503 parent_node_flag,
1504 node_flag_ptr,
1505 node_flag,
1506 (struct cds_hlist_head *) attach_node_flag_ptr,
1507 new_node);
1508 }
1509 if (ret == -EAGAIN || ret == -EEXIST)
1510 goto retry;
1511 end:
1512 return ret;
1513 }
1514
1515 /*
1516 * Note: there is no need to lookup the pointer address associated with
1517 * each node's nth item after taking the lock: it's already been done by
1518 * cds_ja_del while holding the rcu read-side lock, and our node rules
1519 * ensure that when a match value -> pointer is found in a node, it is
1520 * _NEVER_ changed for that node without recompaction, and recompaction
1521 * reallocates the node.
1522 * However, when a child is removed from "linear" nodes, its pointer
1523 * is set to NULL. We therefore check, while holding the locks, if this
1524 * pointer is NULL, and return -ENOENT to the caller if it is the case.
1525 */
1526 static
1527 int ja_detach_node(struct cds_ja *ja,
1528 struct cds_ja_inode_flag **snapshot,
1529 struct cds_ja_inode_flag ***snapshot_ptr,
1530 uint8_t *snapshot_n,
1531 int nr_snapshot,
1532 uint64_t key,
1533 struct cds_ja_node *node)
1534 {
1535 struct cds_ja_shadow_node *shadow_nodes[JA_MAX_DEPTH];
1536 struct cds_ja_inode_flag **node_flag_ptr = NULL,
1537 *parent_node_flag = NULL,
1538 **parent_node_flag_ptr = NULL;
1539 struct cds_ja_inode_flag *iter_node_flag;
1540 int ret, i, nr_shadow = 0, nr_clear = 0, nr_branch = 0;
1541 uint8_t n = 0;
1542
1543 assert(nr_snapshot == ja->tree_depth + 1);
1544
1545 /*
1546 * From the last internal level node going up, get the node
1547 * lock, check if the node has only one child left. If it is the
1548 * case, we continue iterating upward. When we reach a node
1549 * which has more that one child left, we lock the parent, and
1550 * proceed to the node deletion (removing its children too).
1551 */
1552 for (i = nr_snapshot - 2; i >= 1; i--) {
1553 struct cds_ja_shadow_node *shadow_node;
1554
1555 shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1556 snapshot[i]);
1557 if (!shadow_node) {
1558 ret = -EAGAIN;
1559 goto end;
1560 }
1561 shadow_nodes[nr_shadow++] = shadow_node;
1562
1563 /*
1564 * Check if node has been removed between RCU
1565 * lookup and lock acquisition.
1566 */
1567 assert(snapshot_ptr[i + 1]);
1568 if (ja_node_ptr(*snapshot_ptr[i + 1])
1569 != ja_node_ptr(snapshot[i + 1])) {
1570 ret = -ENOENT;
1571 goto end;
1572 }
1573
1574 assert(shadow_node->nr_child > 0);
1575 if (shadow_node->nr_child == 1 && i > 1)
1576 nr_clear++;
1577 nr_branch++;
1578 if (shadow_node->nr_child > 1 || i == 1) {
1579 /* Lock parent and break */
1580 shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1581 snapshot[i - 1]);
1582 if (!shadow_node) {
1583 ret = -EAGAIN;
1584 goto end;
1585 }
1586 shadow_nodes[nr_shadow++] = shadow_node;
1587
1588 /*
1589 * Check if node has been removed between RCU
1590 * lookup and lock acquisition.
1591 */
1592 assert(snapshot_ptr[i]);
1593 if (ja_node_ptr(*snapshot_ptr[i])
1594 != ja_node_ptr(snapshot[i])) {
1595 ret = -ENOENT;
1596 goto end;
1597 }
1598
1599 node_flag_ptr = snapshot_ptr[i + 1];
1600 n = snapshot_n[i + 1];
1601 parent_node_flag_ptr = snapshot_ptr[i];
1602 parent_node_flag = snapshot[i];
1603
1604 if (i > 1) {
1605 /*
1606 * Lock parent's parent, in case we need
1607 * to recompact parent.
1608 */
1609 shadow_node = rcuja_shadow_lookup_lock(ja->ht,
1610 snapshot[i - 2]);
1611 if (!shadow_node) {
1612 ret = -EAGAIN;
1613 goto end;
1614 }
1615 shadow_nodes[nr_shadow++] = shadow_node;
1616
1617 /*
1618 * Check if node has been removed between RCU
1619 * lookup and lock acquisition.
1620 */
1621 assert(snapshot_ptr[i - 1]);
1622 if (ja_node_ptr(*snapshot_ptr[i - 1])
1623 != ja_node_ptr(snapshot[i - 1])) {
1624 ret = -ENOENT;
1625 goto end;
1626 }
1627 }
1628
1629 break;
1630 }
1631 }
1632
1633 /*
1634 * At this point, we want to delete all nodes that are about to
1635 * be removed from shadow_nodes (except the last one, which is
1636 * either the root or the parent of the upmost node with 1
1637 * child). OK to free lock here, because RCU read lock is held,
1638 * and free only performed in call_rcu.
1639 */
1640
1641 for (i = 0; i < nr_clear; i++) {
1642 ret = rcuja_shadow_clear(ja->ht,
1643 shadow_nodes[i]->node_flag,
1644 shadow_nodes[i],
1645 RCUJA_SHADOW_CLEAR_FREE_NODE
1646 | RCUJA_SHADOW_CLEAR_FREE_LOCK);
1647 assert(!ret);
1648 }
1649
1650 iter_node_flag = parent_node_flag;
1651 /* Remove from parent */
1652 ret = ja_node_clear_ptr(ja,
1653 node_flag_ptr, /* Pointer to location to nullify */
1654 &iter_node_flag, /* Old new parent ptr in its parent */
1655 shadow_nodes[nr_branch - 1], /* of parent */
1656 n);
1657 if (ret)
1658 goto end;
1659
1660 dbg_printf("ja_detach_node: publish %p instead of %p\n",
1661 iter_node_flag, *parent_node_flag_ptr);
1662 /* Update address of parent ptr in its parent */
1663 rcu_assign_pointer(*parent_node_flag_ptr, iter_node_flag);
1664
1665 end:
1666 for (i = 0; i < nr_shadow; i++)
1667 rcuja_shadow_unlock(shadow_nodes[i]);
1668 return ret;
1669 }
1670
1671 static
1672 int ja_unchain_node(struct cds_ja *ja,
1673 struct cds_ja_inode_flag *parent_node_flag,
1674 struct cds_ja_inode_flag **node_flag_ptr,
1675 struct cds_ja_inode_flag *node_flag,
1676 struct cds_ja_node *node)
1677 {
1678 struct cds_ja_shadow_node *shadow_node;
1679 struct cds_hlist_node *hlist_node;
1680 struct cds_hlist_head hlist_head;
1681 int ret = 0, count = 0, found = 0;
1682
1683 shadow_node = rcuja_shadow_lookup_lock(ja->ht, parent_node_flag);
1684 if (!shadow_node)
1685 return -EAGAIN;
1686 if (ja_node_ptr(*node_flag_ptr) != ja_node_ptr(node_flag)) {
1687 ret = -EAGAIN;
1688 goto end;
1689 }
1690 hlist_head.next = (struct cds_hlist_node *) ja_node_ptr(node_flag);
1691 /*
1692 * Retry if another thread removed all but one of duplicates
1693 * since check (this check was performed without lock).
1694 * Ensure that the node we are about to remove is still in the
1695 * list (while holding lock).
1696 */
1697 cds_hlist_for_each_rcu(hlist_node, &hlist_head) {
1698 if (count == 0) {
1699 /* FIXME: currently a work-around */
1700 hlist_node->prev = (struct cds_hlist_node *) node_flag_ptr;
1701 }
1702 count++;
1703 if (hlist_node == &node->list)
1704 found++;
1705 }
1706 assert(found <= 1);
1707 if (!found || count == 1) {
1708 ret = -EAGAIN;
1709 goto end;
1710 }
1711 cds_hlist_del_rcu(&node->list);
1712 /*
1713 * Validate that we indeed removed the node from linked list.
1714 */
1715 assert(ja_node_ptr(*node_flag_ptr) != (struct cds_ja_inode *) node);
1716 end:
1717 rcuja_shadow_unlock(shadow_node);
1718 return ret;
1719 }
1720
1721 /*
1722 * Called with RCU read lock held.
1723 */
1724 int cds_ja_del(struct cds_ja *ja, uint64_t key,
1725 struct cds_ja_node *node)
1726 {
1727 unsigned int tree_depth, i;
1728 struct cds_ja_inode_flag *snapshot[JA_MAX_DEPTH];
1729 struct cds_ja_inode_flag **snapshot_ptr[JA_MAX_DEPTH];
1730 uint8_t snapshot_n[JA_MAX_DEPTH];
1731 struct cds_ja_inode_flag *node_flag;
1732 struct cds_ja_inode_flag **prev_node_flag_ptr,
1733 **node_flag_ptr;
1734 int nr_snapshot;
1735 int ret;
1736
1737 if (caa_unlikely(key > ja->key_max))
1738 return -EINVAL;
1739 tree_depth = ja->tree_depth;
1740
1741 retry:
1742 nr_snapshot = 0;
1743 dbg_printf("cds_ja_del attempt: key %" PRIu64 ", node %p\n",
1744 key, node);
1745
1746 /* snapshot for level 0 is only for shadow node lookup */
1747 snapshot_n[0] = 0;
1748 snapshot_n[1] = 0;
1749 snapshot_ptr[nr_snapshot] = NULL;
1750 snapshot[nr_snapshot++] = (struct cds_ja_inode_flag *) &ja->root;
1751 node_flag = rcu_dereference(ja->root);
1752 prev_node_flag_ptr = &ja->root;
1753 node_flag_ptr = &ja->root;
1754
1755 /* Iterate on all internal levels */
1756 for (i = 1; i < tree_depth; i++) {
1757 uint8_t iter_key;
1758
1759 dbg_printf("cds_ja_del iter node_flag %p\n",
1760 node_flag);
1761 if (!ja_node_ptr(node_flag)) {
1762 return -ENOENT;
1763 }
1764 iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1)));
1765 snapshot_n[nr_snapshot + 1] = iter_key;
1766 snapshot_ptr[nr_snapshot] = prev_node_flag_ptr;
1767 snapshot[nr_snapshot++] = node_flag;
1768 node_flag = ja_node_get_nth(node_flag,
1769 &prev_node_flag_ptr,
1770 NULL,
1771 &node_flag_ptr,
1772 iter_key);
1773 dbg_printf("cds_ja_del iter key lookup %u finds node_flag %p, prev_node_flag_ptr %p\n",
1774 (unsigned int) iter_key, node_flag,
1775 prev_node_flag_ptr);
1776 }
1777 /*
1778 * We reached bottom of tree, try to find the node we are trying
1779 * to remove. Fail if we cannot find it.
1780 */
1781 if (!ja_node_ptr(node_flag)) {
1782 dbg_printf("cds_ja_del: no node found for key %" PRIu64 "\n",
1783 key);
1784 return -ENOENT;
1785 } else {
1786 struct cds_hlist_head hlist_head;
1787 struct cds_hlist_node *hlist_node;
1788 struct cds_ja_node *entry, *match = NULL;
1789 int count = 0;
1790
1791 hlist_head.next =
1792 (struct cds_hlist_node *) ja_node_ptr(node_flag);
1793 cds_hlist_for_each_entry_rcu(entry,
1794 hlist_node,
1795 &hlist_head,
1796 list) {
1797 dbg_printf("cds_ja_del: compare %p with entry %p\n", node, entry);
1798 if (entry == node)
1799 match = entry;
1800 count++;
1801 }
1802 if (!match) {
1803 dbg_printf("cds_ja_del: no node match for node %p key %" PRIu64 "\n", node, key);
1804 return -ENOENT;
1805 }
1806 assert(count > 0);
1807 if (count == 1) {
1808 /*
1809 * Removing last of duplicates. Last snapshot
1810 * does not have a shadow node (external leafs).
1811 */
1812 snapshot_ptr[nr_snapshot] = prev_node_flag_ptr;
1813 snapshot[nr_snapshot++] = node_flag;
1814 ret = ja_detach_node(ja, snapshot, snapshot_ptr,
1815 snapshot_n, nr_snapshot, key, node);
1816 } else {
1817 ret = ja_unchain_node(ja, snapshot[nr_snapshot - 1],
1818 node_flag_ptr, node_flag, match);
1819 }
1820 }
1821 /*
1822 * Explanation of -ENOENT handling: caused by concurrent delete
1823 * between RCU lookup and actual removal. Need to re-do the
1824 * lookup and removal attempt.
1825 */
1826 if (ret == -EAGAIN || ret == -ENOENT)
1827 goto retry;
1828 return ret;
1829 }
1830
1831 struct cds_ja *_cds_ja_new(unsigned int key_bits,
1832 const struct rcu_flavor_struct *flavor)
1833 {
1834 struct cds_ja *ja;
1835 int ret;
1836 struct cds_ja_shadow_node *root_shadow_node;
1837
1838 ja = calloc(sizeof(*ja), 1);
1839 if (!ja)
1840 goto ja_error;
1841
1842 switch (key_bits) {
1843 case 8:
1844 case 16:
1845 case 24:
1846 case 32:
1847 case 40:
1848 case 48:
1849 case 56:
1850 ja->key_max = (1ULL << key_bits) - 1;
1851 break;
1852 case 64:
1853 ja->key_max = UINT64_MAX;
1854 break;
1855 default:
1856 goto check_error;
1857 }
1858
1859 /* ja->root is NULL */
1860 /* tree_depth 0 is for pointer to root node */
1861 ja->tree_depth = (key_bits >> JA_LOG2_BITS_PER_BYTE) + 1;
1862 assert(ja->tree_depth <= JA_MAX_DEPTH);
1863 ja->ht = rcuja_create_ht(flavor);
1864 if (!ja->ht)
1865 goto ht_error;
1866
1867 /*
1868 * Note: we should not free this node until judy array destroy.
1869 */
1870 root_shadow_node = rcuja_shadow_set(ja->ht,
1871 (struct cds_ja_inode_flag *) &ja->root,
1872 NULL, ja);
1873 if (!root_shadow_node) {
1874 ret = -ENOMEM;
1875 goto ht_node_error;
1876 }
1877 root_shadow_node->level = 0;
1878
1879 return ja;
1880
1881 ht_node_error:
1882 ret = rcuja_delete_ht(ja->ht);
1883 assert(!ret);
1884 ht_error:
1885 check_error:
1886 free(ja);
1887 ja_error:
1888 return NULL;
1889 }
1890
1891 /*
1892 * Called from RCU read-side CS.
1893 */
1894 __attribute__((visibility("protected")))
1895 void rcuja_free_all_children(struct cds_ja_shadow_node *shadow_node,
1896 struct cds_ja_inode_flag *node_flag,
1897 void (*free_node_cb)(struct rcu_head *head))
1898 {
1899 const struct rcu_flavor_struct *flavor;
1900 unsigned int type_index;
1901 struct cds_ja_inode *node;
1902 const struct cds_ja_type *type;
1903
1904 flavor = cds_lfht_rcu_flavor(shadow_node->ja->ht);
1905 node = ja_node_ptr(node_flag);
1906 assert(node != NULL);
1907 type_index = ja_node_type(node_flag);
1908 type = &ja_types[type_index];
1909
1910 switch (type->type_class) {
1911 case RCU_JA_LINEAR:
1912 {
1913 uint8_t nr_child =
1914 ja_linear_node_get_nr_child(type, node);
1915 unsigned int i;
1916
1917 for (i = 0; i < nr_child; i++) {
1918 struct cds_ja_inode_flag *iter;
1919 struct cds_hlist_head head;
1920 struct cds_ja_node *entry;
1921 struct cds_hlist_node *pos;
1922 uint8_t v;
1923
1924 ja_linear_node_get_ith_pos(type, node, i, &v, &iter);
1925 if (!iter)
1926 continue;
1927 head.next = (struct cds_hlist_node *) iter;
1928 cds_hlist_for_each_entry_rcu(entry, pos, &head, list) {
1929 flavor->update_call_rcu(&entry->head, free_node_cb);
1930 }
1931 }
1932 break;
1933 }
1934 case RCU_JA_POOL:
1935 {
1936 unsigned int pool_nr;
1937
1938 for (pool_nr = 0; pool_nr < (1U << type->nr_pool_order); pool_nr++) {
1939 struct cds_ja_inode *pool =
1940 ja_pool_node_get_ith_pool(type, node, pool_nr);
1941 uint8_t nr_child =
1942 ja_linear_node_get_nr_child(type, pool);
1943 unsigned int j;
1944
1945 for (j = 0; j < nr_child; j++) {
1946 struct cds_ja_inode_flag *iter;
1947 struct cds_hlist_head head;
1948 struct cds_ja_node *entry;
1949 struct cds_hlist_node *pos;
1950 uint8_t v;
1951
1952 ja_linear_node_get_ith_pos(type, node, j, &v, &iter);
1953 if (!iter)
1954 continue;
1955 head.next = (struct cds_hlist_node *) iter;
1956 cds_hlist_for_each_entry_rcu(entry, pos, &head, list) {
1957 flavor->update_call_rcu(&entry->head, free_node_cb);
1958 }
1959 }
1960 }
1961 break;
1962 }
1963 case RCU_JA_NULL:
1964 break;
1965 case RCU_JA_PIGEON:
1966 {
1967 uint8_t nr_child;
1968 unsigned int i;
1969
1970 nr_child = shadow_node->nr_child;
1971 for (i = 0; i < nr_child; i++) {
1972 struct cds_ja_inode_flag *iter;
1973 struct cds_hlist_head head;
1974 struct cds_ja_node *entry;
1975 struct cds_hlist_node *pos;
1976
1977 iter = ja_pigeon_node_get_ith_pos(type, node, i);
1978 if (!iter)
1979 continue;
1980 head.next = (struct cds_hlist_node *) iter;
1981 cds_hlist_for_each_entry_rcu(entry, pos, &head, list) {
1982 flavor->update_call_rcu(&entry->head, free_node_cb);
1983 }
1984 }
1985 break;
1986 }
1987 default:
1988 assert(0);
1989 }
1990 }
1991
1992 /*
1993 * There should be no more concurrent add to the judy array while it is
1994 * being destroyed (ensured by the caller).
1995 */
1996 int cds_ja_destroy(struct cds_ja *ja,
1997 void (*free_node_cb)(struct rcu_head *head))
1998 {
1999 int ret;
2000
2001 rcuja_shadow_prune(ja->ht,
2002 RCUJA_SHADOW_CLEAR_FREE_NODE | RCUJA_SHADOW_CLEAR_FREE_LOCK,
2003 free_node_cb);
2004 ret = rcuja_delete_ht(ja->ht);
2005 if (ret)
2006 return ret;
2007 if (uatomic_read(&ja->nr_fallback))
2008 fprintf(stderr,
2009 "[warning] RCU Judy Array used %lu fallback node(s)\n",
2010 uatomic_read(&ja->nr_fallback));
2011 free(ja);
2012 return 0;
2013 }
This page took 0.090981 seconds and 5 git commands to generate.