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