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