4 * Userspace RCU library - RCU Judy Array
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
27 #include <urcu/rcuja.h>
28 #include <urcu/compiler.h>
29 #include <urcu/arch.h>
31 #include <urcu-pointer.h>
32 #include <urcu/uatomic.h>
35 #include "rcuja-internal.h"
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 */
51 RCU_JA_NULL
, /* not an encoded type, but keeps code regular */
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 */
65 * Number of least significant pointer bits reserved to represent the
68 #define JA_TYPE_BITS 3
69 #define JA_TYPE_MAX_NR (1UL << JA_TYPE_BITS)
70 #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1)
71 #define JA_PTR_MASK (~JA_TYPE_MASK)
73 #define JA_ENTRY_PER_NODE 256UL
74 #define JA_BITS_PER_BYTE 3
76 #define JA_MAX_DEPTH 9 /* Maximum depth, including leafs */
79 * Entry for NULL node is at index 8 of the table. It is never encoded
82 #define NODE_INDEX_NULL 8
85 * Number of removals needed on a fallback node before we try to shrink
88 #define JA_FALLBACK_REMOVAL_COUNT 8
91 * Iteration on the array to find the right node size for the number of
92 * children stops when it reaches .max_child == 256 (this is the largest
93 * possible node size, which contains 256 children).
94 * The min_child overlaps with the previous max_child to provide an
95 * hysteresis loop to reallocation for patterns of cyclic add/removal
96 * within the same node.
97 * The node the index within the following arrays is represented on 3
98 * bits. It identifies the node type, min/max number of children, and
100 * The max_child values for the RCU_JA_POOL below result from
101 * statistical approximation: over million populations, the max_child
102 * covers between 97% and 99% of the populations generated. Therefore, a
103 * fallback should exist to cover the rare extreme population unbalance
104 * cases, but it will not have a major impact on speed nor space
105 * consumption, since those are rare cases.
108 #if (CAA_BITS_PER_LONG < 64)
109 /* 32-bit pointers */
111 ja_type_0_max_child
= 1,
112 ja_type_1_max_child
= 3,
113 ja_type_2_max_child
= 6,
114 ja_type_3_max_child
= 12,
115 ja_type_4_max_child
= 25,
116 ja_type_5_max_child
= 48,
117 ja_type_6_max_child
= 92,
118 ja_type_7_max_child
= 256,
119 ja_type_8_max_child
= 0, /* NULL */
123 ja_type_0_max_linear_child
= 1,
124 ja_type_1_max_linear_child
= 3,
125 ja_type_2_max_linear_child
= 6,
126 ja_type_3_max_linear_child
= 12,
127 ja_type_4_max_linear_child
= 25,
128 ja_type_5_max_linear_child
= 24,
129 ja_type_6_max_linear_child
= 23,
133 ja_type_5_nr_pool_order
= 1,
134 ja_type_6_nr_pool_order
= 2,
137 const struct cds_ja_type ja_types
[] = {
138 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_0_max_child
, .max_linear_child
= ja_type_0_max_linear_child
, .order
= 3, },
139 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_1_max_child
, .max_linear_child
= ja_type_1_max_linear_child
, .order
= 4, },
140 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= ja_type_2_max_child
, .max_linear_child
= ja_type_2_max_linear_child
, .order
= 5, },
141 { .type_class
= RCU_JA_LINEAR
, .min_child
= 4, .max_child
= ja_type_3_max_child
, .max_linear_child
= ja_type_3_max_linear_child
, .order
= 6, },
142 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= ja_type_4_max_child
, .max_linear_child
= ja_type_4_max_linear_child
, .order
= 7, },
144 /* Pools may fill sooner than max_child */
145 { .type_class
= RCU_JA_POOL
, .min_child
= 20, .max_child
= ja_type_5_max_child
, .max_linear_child
= ja_type_5_max_linear_child
, .order
= 8, .nr_pool_order
= ja_type_5_nr_pool_order
, .pool_size_order
= 7, },
146 { .type_class
= RCU_JA_POOL
, .min_child
= 45, .max_child
= ja_type_6_max_child
, .max_linear_child
= ja_type_6_max_linear_child
, .order
= 9, .nr_pool_order
= ja_type_6_nr_pool_order
, .pool_size_order
= 7, },
149 * TODO: Upon node removal below min_child, if child pool is
150 * filled beyond capacity, we need to roll back to pigeon.
152 { .type_class
= RCU_JA_PIGEON
, .min_child
= 89, .max_child
= ja_type_7_max_child
, .order
= 10, },
154 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
156 #else /* !(CAA_BITS_PER_LONG < 64) */
157 /* 64-bit pointers */
159 ja_type_0_max_child
= 1,
160 ja_type_1_max_child
= 3,
161 ja_type_2_max_child
= 7,
162 ja_type_3_max_child
= 14,
163 ja_type_4_max_child
= 28,
164 ja_type_5_max_child
= 54,
165 ja_type_6_max_child
= 104,
166 ja_type_7_max_child
= 256,
167 ja_type_8_max_child
= 256,
171 ja_type_0_max_linear_child
= 1,
172 ja_type_1_max_linear_child
= 3,
173 ja_type_2_max_linear_child
= 7,
174 ja_type_3_max_linear_child
= 14,
175 ja_type_4_max_linear_child
= 28,
176 ja_type_5_max_linear_child
= 27,
177 ja_type_6_max_linear_child
= 26,
181 ja_type_5_nr_pool_order
= 1,
182 ja_type_6_nr_pool_order
= 2,
185 const struct cds_ja_type ja_types
[] = {
186 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_0_max_child
, .max_linear_child
= ja_type_0_max_linear_child
, .order
= 4, },
187 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_1_max_child
, .max_linear_child
= ja_type_1_max_linear_child
, .order
= 5, },
188 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= ja_type_2_max_child
, .max_linear_child
= ja_type_2_max_linear_child
, .order
= 6, },
189 { .type_class
= RCU_JA_LINEAR
, .min_child
= 5, .max_child
= ja_type_3_max_child
, .max_linear_child
= ja_type_3_max_linear_child
, .order
= 7, },
190 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= ja_type_4_max_child
, .max_linear_child
= ja_type_4_max_linear_child
, .order
= 8, },
192 /* Pools may fill sooner than max_child. */
193 { .type_class
= RCU_JA_POOL
, .min_child
= 22, .max_child
= ja_type_5_max_child
, .max_linear_child
= ja_type_5_max_linear_child
, .order
= 9, .nr_pool_order
= ja_type_5_nr_pool_order
, .pool_size_order
= 8, },
194 { .type_class
= RCU_JA_POOL
, .min_child
= 51, .max_child
= ja_type_6_max_child
, .max_linear_child
= ja_type_6_max_linear_child
, .order
= 10, .nr_pool_order
= ja_type_6_nr_pool_order
, .pool_size_order
= 8, },
197 * TODO: Upon node removal below min_child, if child pool is
198 * filled beyond capacity, we need to roll back to pigeon.
200 { .type_class
= RCU_JA_PIGEON
, .min_child
= 101, .max_child
= ja_type_7_max_child
, .order
= 11, },
202 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
204 #endif /* !(BITS_PER_LONG < 64) */
206 static inline __attribute__((unused
))
207 void static_array_size_check(void)
209 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) < JA_TYPE_MAX_NR
);
213 * The cds_ja_node contains the compressed node data needed for
214 * read-side. For linear and pool node configurations, it starts with a
215 * byte counting the number of children in the node. Then, the
216 * node-specific data is placed.
217 * The node mutex, if any is needed, protecting concurrent updated of
218 * each node is placed in a separate hash table indexed by node address.
219 * For the pigeon configuration, the number of children is also kept in
220 * a separate hash table, indexed by node address, because it is only
221 * required for updates.
224 #define DECLARE_LINEAR_NODE(index) \
227 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
228 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
231 #define DECLARE_POOL_NODE(index) \
235 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
236 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
237 } linear[1U << ja_type_## index ##_nr_pool_order]; \
240 struct cds_ja_inode
{
242 /* Linear configuration */
243 DECLARE_LINEAR_NODE(0) conf_0
;
244 DECLARE_LINEAR_NODE(1) conf_1
;
245 DECLARE_LINEAR_NODE(2) conf_2
;
246 DECLARE_LINEAR_NODE(3) conf_3
;
247 DECLARE_LINEAR_NODE(4) conf_4
;
249 /* Pool configuration */
250 DECLARE_POOL_NODE(5) conf_5
;
251 DECLARE_POOL_NODE(6) conf_6
;
253 /* Pigeon configuration */
255 struct cds_ja_inode_flag
*child
[ja_type_7_max_child
];
257 /* data aliasing nodes for computed accesses */
258 uint8_t data
[sizeof(struct cds_ja_inode_flag
*) * ja_type_7_max_child
];
263 struct cds_ja_inode_flag
*ja_node_flag(struct cds_ja_inode
*node
,
266 assert(type
< (1UL << JA_TYPE_BITS
));
267 return (struct cds_ja_inode_flag
*) (((unsigned long) node
) | type
);
271 struct cds_ja_inode
*ja_node_ptr(struct cds_ja_inode_flag
*node
)
273 return (struct cds_ja_inode
*) (((unsigned long) node
) & JA_PTR_MASK
);
277 unsigned long ja_node_type(struct cds_ja_inode_flag
*node
)
281 if (ja_node_ptr(node
) == NULL
) {
282 return NODE_INDEX_NULL
;
284 type
= (unsigned int) ((unsigned long) node
& JA_TYPE_MASK
);
285 assert(type
< (1UL << JA_TYPE_BITS
));
289 struct cds_ja_inode
*alloc_cds_ja_node(const struct cds_ja_type
*ja_type
)
291 return calloc(1U << ja_type
->order
, sizeof(char));
294 void free_cds_ja_node(struct cds_ja_inode
*node
)
299 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
300 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
301 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
302 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
305 uint8_t *align_ptr_size(uint8_t *ptr
)
307 return (uint8_t *) JA_ALIGN((unsigned long) ptr
, sizeof(void *));
311 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type
*type
,
312 struct cds_ja_inode
*node
)
314 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
315 return CMM_LOAD_SHARED(node
->u
.data
[0]);
319 * The order in which values and pointers are does does not matter: if
320 * a value is missing, we return NULL. If a value is there, but its
321 * associated pointers is still NULL, we return NULL too.
324 struct cds_ja_inode_flag
*ja_linear_node_get_nth(const struct cds_ja_type
*type
,
325 struct cds_ja_inode
*node
,
326 struct cds_ja_inode_flag
***child_node_flag_ptr
,
331 struct cds_ja_inode_flag
**pointers
;
332 struct cds_ja_inode_flag
*ptr
;
335 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
337 nr_child
= ja_linear_node_get_nr_child(type
, node
);
338 cmm_smp_rmb(); /* read nr_child before values and pointers */
339 assert(nr_child
<= type
->max_linear_child
);
340 assert(type
->type_class
!= RCU_JA_LINEAR
|| nr_child
>= type
->min_child
);
342 values
= &node
->u
.data
[1];
343 for (i
= 0; i
< nr_child
; i
++) {
344 if (CMM_LOAD_SHARED(values
[i
]) == n
)
349 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
350 if (caa_unlikely(child_node_flag_ptr
))
351 *child_node_flag_ptr
= &pointers
[i
];
352 ptr
= rcu_dereference(pointers
[i
]);
353 assert(ja_node_ptr(ptr
) != NULL
);
358 void ja_linear_node_get_ith_pos(const struct cds_ja_type
*type
,
359 struct cds_ja_inode
*node
,
362 struct cds_ja_inode_flag
**iter
)
365 struct cds_ja_inode_flag
**pointers
;
367 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
368 assert(i
< ja_linear_node_get_nr_child(type
, node
));
370 values
= &node
->u
.data
[1];
372 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
377 struct cds_ja_inode_flag
*ja_pool_node_get_nth(const struct cds_ja_type
*type
,
378 struct cds_ja_inode
*node
,
379 struct cds_ja_inode_flag
***child_node_flag_ptr
,
382 struct cds_ja_inode
*linear
;
384 assert(type
->type_class
== RCU_JA_POOL
);
386 * TODO: currently, we select the pool by highest bits. We
387 * should support various encodings.
389 linear
= (struct cds_ja_inode
*)
390 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
391 return ja_linear_node_get_nth(type
, linear
, child_node_flag_ptr
, n
);
395 struct cds_ja_inode
*ja_pool_node_get_ith_pool(const struct cds_ja_type
*type
,
396 struct cds_ja_inode
*node
,
399 assert(type
->type_class
== RCU_JA_POOL
);
400 return (struct cds_ja_inode
*)
401 &node
->u
.data
[(unsigned int) i
<< type
->pool_size_order
];
405 struct cds_ja_inode_flag
*ja_pigeon_node_get_nth(const struct cds_ja_type
*type
,
406 struct cds_ja_inode
*node
,
407 struct cds_ja_inode_flag
***child_node_flag_ptr
,
410 struct cds_ja_inode_flag
**child_node_flag
;
412 assert(type
->type_class
== RCU_JA_PIGEON
);
413 child_node_flag
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
414 if (caa_unlikely(child_node_flag_ptr
))
415 *child_node_flag_ptr
= child_node_flag
;
416 return rcu_dereference(*child_node_flag
);
420 * ja_node_get_nth: get nth item from a node.
421 * node_flag is already rcu_dereference'd.
424 struct cds_ja_inode_flag
* ja_node_get_nth(struct cds_ja_inode_flag
*node_flag
,
425 struct cds_ja_inode_flag
***child_node_flag_ptr
,
428 unsigned int type_index
;
429 struct cds_ja_inode
*node
;
430 const struct cds_ja_type
*type
;
432 node
= ja_node_ptr(node_flag
);
433 assert(node
!= NULL
);
434 type_index
= ja_node_type(node_flag
);
435 type
= &ja_types
[type_index
];
437 switch (type
->type_class
) {
439 return ja_linear_node_get_nth(type
, node
,
440 child_node_flag_ptr
, n
);
442 return ja_pool_node_get_nth(type
, node
,
443 child_node_flag_ptr
, n
);
445 return ja_pigeon_node_get_nth(type
, node
,
446 child_node_flag_ptr
, n
);
449 return (void *) -1UL;
454 * TODO: use ja_get_nr_child to monitor limits triggering shrink
456 * Also use ja_get_nr_child to make the difference between resize and
457 * pool change of compaction bit(s).
460 unsigned int ja_get_nr_child(struct cds_ja_shadow_node
*shadow_node
)
462 return shadow_node
->nr_child
;
466 int ja_linear_node_set_nth(const struct cds_ja_type
*type
,
467 struct cds_ja_inode
*node
,
468 struct cds_ja_shadow_node
*shadow_node
,
470 struct cds_ja_inode_flag
*child_node_flag
)
473 uint8_t *values
, *nr_child_ptr
;
474 struct cds_ja_inode_flag
**pointers
;
477 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
479 nr_child_ptr
= &node
->u
.data
[0];
480 dbg_printf("linear set nth: nr_child_ptr %p\n", nr_child_ptr
);
481 nr_child
= *nr_child_ptr
;
482 assert(nr_child
<= type
->max_linear_child
);
484 values
= &node
->u
.data
[1];
485 for (i
= 0; i
< nr_child
; i
++) {
489 if (nr_child
>= type
->max_linear_child
) {
490 /* No space left in this node type */
493 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
494 assert(pointers
[nr_child
] == NULL
);
495 rcu_assign_pointer(pointers
[nr_child
], child_node_flag
);
496 CMM_STORE_SHARED(values
[nr_child
], n
);
497 cmm_smp_wmb(); /* write value and pointer before nr_child */
498 CMM_STORE_SHARED(*nr_child_ptr
, nr_child
+ 1);
499 shadow_node
->nr_child
++;
500 dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n",
501 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr
),
502 (unsigned int) shadow_node
->nr_child
,
509 int ja_pool_node_set_nth(const struct cds_ja_type
*type
,
510 struct cds_ja_inode
*node
,
511 struct cds_ja_shadow_node
*shadow_node
,
513 struct cds_ja_inode_flag
*child_node_flag
)
515 struct cds_ja_inode
*linear
;
517 assert(type
->type_class
== RCU_JA_POOL
);
518 linear
= (struct cds_ja_inode
*)
519 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
520 return ja_linear_node_set_nth(type
, linear
, shadow_node
,
525 int ja_pigeon_node_set_nth(const struct cds_ja_type
*type
,
526 struct cds_ja_inode
*node
,
527 struct cds_ja_shadow_node
*shadow_node
,
529 struct cds_ja_inode_flag
*child_node_flag
)
531 struct cds_ja_inode_flag
**ptr
;
533 assert(type
->type_class
== RCU_JA_PIGEON
);
534 ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
537 rcu_assign_pointer(*ptr
, child_node_flag
);
538 shadow_node
->nr_child
++;
543 * _ja_node_set_nth: set nth item within a node. Return an error
544 * (negative error value) if it is already there.
545 * TODO: exclusive access on node.
548 int _ja_node_set_nth(const struct cds_ja_type
*type
,
549 struct cds_ja_inode
*node
,
550 struct cds_ja_shadow_node
*shadow_node
,
552 struct cds_ja_inode_flag
*child_node_flag
)
554 switch (type
->type_class
) {
556 return ja_linear_node_set_nth(type
, node
, shadow_node
, n
,
559 return ja_pool_node_set_nth(type
, node
, shadow_node
, n
,
562 return ja_pigeon_node_set_nth(type
, node
, shadow_node
, n
,
575 * ja_node_recompact_add: recompact a node, adding a new child.
576 * TODO: for pool type, take selection bit(s) into account.
577 * Return 0 on success, -ENOENT if need to retry, or other negative
578 * error value otherwise.
581 int ja_node_recompact_add(struct cds_ja
*ja
,
582 unsigned int old_type_index
,
583 const struct cds_ja_type
*old_type
,
584 struct cds_ja_inode
*old_node
,
585 struct cds_ja_shadow_node
*shadow_node
,
586 struct cds_ja_inode_flag
**old_node_flag
, uint8_t n
,
587 struct cds_ja_inode_flag
*child_node_flag
)
589 unsigned int new_type_index
;
590 struct cds_ja_inode
*new_node
;
591 struct cds_ja_shadow_node
*new_shadow_node
;
592 const struct cds_ja_type
*new_type
;
593 struct cds_ja_inode_flag
*new_node_flag
;
597 if (!shadow_node
|| old_type_index
== NODE_INDEX_NULL
) {
600 new_type_index
= old_type_index
+ 1;
603 retry
: /* for fallback */
604 dbg_printf("Recompact to type %d\n", new_type_index
);
605 new_type
= &ja_types
[new_type_index
];
606 new_node
= alloc_cds_ja_node(new_type
);
609 new_node_flag
= ja_node_flag(new_node
, new_type_index
);
611 dbg_printf("Recompact inherit lock from %p\n", shadow_node
);
612 new_shadow_node
= rcuja_shadow_set(ja
->ht
, new_node
, shadow_node
);
613 if (!new_shadow_node
) {
618 new_shadow_node
->fallback_removal_count
=
619 JA_FALLBACK_REMOVAL_COUNT
;
621 assert(old_type
->type_class
!= RCU_JA_PIGEON
);
622 switch (old_type
->type_class
) {
626 ja_linear_node_get_nr_child(old_type
, old_node
);
629 for (i
= 0; i
< nr_child
; i
++) {
630 struct cds_ja_inode_flag
*iter
;
633 ja_linear_node_get_ith_pos(old_type
, old_node
, i
, &v
, &iter
);
636 ret
= _ja_node_set_nth(new_type
, new_node
,
639 if (new_type
->type_class
== RCU_JA_POOL
&& ret
) {
640 goto fallback_toosmall
;
648 unsigned int pool_nr
;
650 for (pool_nr
= 0; pool_nr
< (1U << old_type
->nr_pool_order
); pool_nr
++) {
651 struct cds_ja_inode
*pool
=
652 ja_pool_node_get_ith_pool(old_type
,
655 ja_linear_node_get_nr_child(old_type
, pool
);
658 for (j
= 0; j
< nr_child
; j
++) {
659 struct cds_ja_inode_flag
*iter
;
662 ja_linear_node_get_ith_pos(old_type
, pool
,
666 ret
= _ja_node_set_nth(new_type
, new_node
,
669 if (new_type
->type_class
== RCU_JA_POOL
671 goto fallback_toosmall
;
679 /* Nothing to copy */
689 ret
= _ja_node_set_nth(new_type
, new_node
,
693 /* Return pointer to new recompacted new through old_node_flag */
694 *old_node_flag
= new_node_flag
;
696 ret
= rcuja_shadow_clear(ja
->ht
, old_node
, shadow_node
,
697 RCUJA_SHADOW_CLEAR_FREE_NODE
);
706 /* fallback if next pool is too small */
707 ret
= rcuja_shadow_clear(ja
->ht
, new_node
, new_shadow_node
,
708 RCUJA_SHADOW_CLEAR_FREE_NODE
);
711 /* Last type: pigeon */
712 new_type_index
= (1UL << JA_TYPE_BITS
) - 1;
713 dbg_printf("Fallback to type %d\n", new_type_index
);
714 uatomic_inc(&ja
->nr_fallback
);
720 * Return 0 on success, -ENOENT if need to retry, or other negative
721 * error value otherwise.
724 int ja_node_set_nth(struct cds_ja
*ja
,
725 struct cds_ja_inode_flag
**node_flag
, uint8_t n
,
726 struct cds_ja_inode_flag
*child_node_flag
,
727 struct cds_ja_shadow_node
*shadow_node
)
730 unsigned int type_index
;
731 const struct cds_ja_type
*type
;
732 struct cds_ja_inode
*node
;
734 dbg_printf("ja_node_set_nth for n=%u, node %p, shadow %p\n",
735 (unsigned int) n
, ja_node_ptr(*node_flag
), shadow_node
);
737 node
= ja_node_ptr(*node_flag
);
738 type_index
= ja_node_type(*node_flag
);
739 type
= &ja_types
[type_index
];
740 ret
= _ja_node_set_nth(type
, node
, shadow_node
,
742 if (ret
== -ENOSPC
) {
743 /* Not enough space in node, need to recompact. */
744 ret
= ja_node_recompact_add(ja
, type_index
, type
, node
,
745 shadow_node
, node_flag
, n
, child_node_flag
);
750 struct cds_hlist_head
*cds_ja_lookup(struct cds_ja
*ja
, uint64_t key
)
752 unsigned int tree_depth
, i
;
753 struct cds_ja_inode_flag
*node_flag
;
755 if (caa_unlikely(key
> ja
->key_max
))
757 tree_depth
= ja
->tree_depth
;
758 node_flag
= rcu_dereference(ja
->root
);
760 /* level 0: root node */
761 if (!ja_node_ptr(node_flag
))
764 for (i
= 1; i
< tree_depth
; i
++) {
767 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
768 node_flag
= ja_node_get_nth(node_flag
, NULL
,
770 if (!ja_node_ptr(node_flag
))
774 /* Last level lookup succeded. We got an actual match. */
775 return (struct cds_hlist_head
*) node_flag
;
779 * We reached an unpopulated node. Create it and the children we need,
780 * and then attach the entire branch to the current node. This may
781 * trigger recompaction of the current node. Locks needed: node lock
782 * (for add), and, possibly, parent node lock (to update pointer due to
783 * node recompaction).
785 * First take node lock, check if recompaction is needed, then take
786 * parent lock (if needed). Then we can proceed to create the new
787 * branch. Publish the new branch, and release locks.
788 * TODO: we currently always take the parent lock even when not needed.
791 int ja_attach_node(struct cds_ja
*ja
,
792 struct cds_ja_inode_flag
**node_flag_ptr
,
793 struct cds_ja_inode_flag
*node_flag
,
794 struct cds_ja_inode_flag
*parent_node_flag
,
797 struct cds_ja_node
*child_node
)
799 struct cds_ja_shadow_node
*shadow_node
= NULL
,
800 *parent_shadow_node
= NULL
,
802 struct cds_ja_inode
*node
= ja_node_ptr(node_flag
);
803 struct cds_ja_inode
*parent_node
= ja_node_ptr(parent_node_flag
);
804 struct cds_hlist_head head
;
805 struct cds_ja_inode_flag
*iter_node_flag
, *iter_dest_node_flag
;
807 struct cds_ja_inode_flag
*created_nodes
[JA_MAX_DEPTH
];
808 int nr_created_nodes
= 0;
810 dbg_printf("Attach node at level %u\n", level
);
813 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, node
);
819 parent_shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
821 if (!parent_shadow_node
) {
827 /* Create new branch, starting from bottom */
828 CDS_INIT_HLIST_HEAD(&head
);
829 cds_hlist_add_head_rcu(&child_node
->list
, &head
);
830 iter_node_flag
= (struct cds_ja_inode_flag
*) head
.next
;
832 /* Create shadow node for the leaf node */
833 dbg_printf("leaf shadow node creation\n");
834 iter_shadow_node
= rcuja_shadow_set(ja
->ht
,
835 ja_node_ptr(iter_node_flag
), NULL
);
836 if (!iter_shadow_node
) {
840 created_nodes
[nr_created_nodes
++] = iter_node_flag
;
842 for (i
= ja
->tree_depth
; i
> (int) level
; i
--) {
845 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (ja
->tree_depth
- i
)));
846 dbg_printf("branch creation level %d, key %u\n",
847 i
- 1, (unsigned int) iter_key
);
848 iter_dest_node_flag
= NULL
;
849 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
855 created_nodes
[nr_created_nodes
++] = iter_dest_node_flag
;
856 iter_node_flag
= iter_dest_node_flag
;
862 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (ja
->tree_depth
- level
)));
863 /* We need to use set_nth on the previous level. */
864 iter_dest_node_flag
= node_flag
;
865 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
871 created_nodes
[nr_created_nodes
++] = iter_dest_node_flag
;
872 iter_node_flag
= iter_dest_node_flag
;
875 /* Publish new branch */
876 dbg_printf("Publish branch %p, replacing %p\n",
877 iter_node_flag
, *node_flag_ptr
);
878 rcu_assign_pointer(*node_flag_ptr
, iter_node_flag
);
885 for (i
= 0; i
< nr_created_nodes
; i
++) {
889 flags
= RCUJA_SHADOW_CLEAR_FREE_LOCK
;
891 flags
|= RCUJA_SHADOW_CLEAR_FREE_NODE
;
892 tmpret
= rcuja_shadow_clear(ja
->ht
,
893 ja_node_ptr(created_nodes
[i
]),
899 if (parent_shadow_node
)
900 rcuja_shadow_unlock(parent_shadow_node
);
903 rcuja_shadow_unlock(shadow_node
);
909 * Lock the hlist head shadow node mutex, and add node to list of
910 * duplicates. Failure can happen if concurrent removal removes the last
911 * node with same key before we get the lock.
912 * Return 0 on success, negative error value on failure.
915 int ja_chain_node(struct cds_ja
*ja
,
916 struct cds_hlist_head
*head
,
917 struct cds_ja_node
*node
)
919 struct cds_ja_shadow_node
*shadow_node
;
921 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
922 (struct cds_ja_inode
*) head
);
925 cds_hlist_add_head_rcu(&node
->list
, head
);
926 rcuja_shadow_unlock(shadow_node
);
930 int cds_ja_add(struct cds_ja
*ja
, uint64_t key
,
931 struct cds_ja_node
*new_node
)
933 unsigned int tree_depth
, i
;
934 struct cds_ja_inode_flag
**node_flag_ptr
; /* in parent */
935 struct cds_ja_inode_flag
*node_flag
,
940 if (caa_unlikely(key
> ja
->key_max
))
942 tree_depth
= ja
->tree_depth
;
945 dbg_printf("cds_ja_add attempt: key %" PRIu64
", node %p\n",
947 parent2_node_flag
= NULL
;
949 (struct cds_ja_inode_flag
*) &ja
->root
; /* Use root ptr address as key for mutex */
950 node_flag_ptr
= &ja
->root
;
951 node_flag
= rcu_dereference(*node_flag_ptr
);
953 /* Iterate on all internal levels */
954 for (i
= 1; i
< tree_depth
; i
++) {
957 if (!ja_node_ptr(node_flag
)) {
958 ret
= ja_attach_node(ja
, node_flag_ptr
,
959 parent_node_flag
, parent2_node_flag
,
961 if (ret
== -ENOENT
|| ret
== -EEXIST
)
966 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
967 parent2_node_flag
= parent_node_flag
;
968 parent_node_flag
= node_flag
;
969 node_flag
= ja_node_get_nth(node_flag
,
975 * We reached bottom of tree, simply add node to last internal
976 * level, or chain it if key is already present.
978 if (!ja_node_ptr(node_flag
)) {
979 ret
= ja_attach_node(ja
, node_flag_ptr
, parent_node_flag
,
980 parent2_node_flag
, key
, i
, new_node
);
982 ret
= ja_chain_node(ja
,
983 (struct cds_hlist_head
*) ja_node_ptr(node_flag
),
992 struct cds_ja
*_cds_ja_new(unsigned int key_bits
,
993 const struct rcu_flavor_struct
*flavor
)
997 struct cds_ja_shadow_node
*root_shadow_node
;
999 ja
= calloc(sizeof(*ja
), 1);
1005 ja
->key_max
= UINT8_MAX
;
1008 ja
->key_max
= UINT16_MAX
;
1011 ja
->key_max
= UINT32_MAX
;
1014 ja
->key_max
= UINT64_MAX
;
1020 /* ja->root is NULL */
1021 /* tree_depth 0 is for pointer to root node */
1022 ja
->tree_depth
= (key_bits
>> JA_BITS_PER_BYTE
) + 1;
1023 assert(ja
->tree_depth
<= JA_MAX_DEPTH
);
1024 ja
->ht
= rcuja_create_ht(flavor
);
1029 * Note: we should not free this node until judy array destroy.
1031 root_shadow_node
= rcuja_shadow_set(ja
->ht
,
1032 ja_node_ptr((struct cds_ja_inode_flag
*) &ja
->root
),
1034 if (!root_shadow_node
) {
1042 ret
= rcuja_delete_ht(ja
->ht
);
1052 * There should be no more concurrent add to the judy array while it is
1053 * being destroyed (ensured by the caller).
1055 int cds_ja_destroy(struct cds_ja
*ja
)
1059 rcuja_shadow_prune(ja
->ht
,
1060 RCUJA_SHADOW_CLEAR_FREE_NODE
| RCUJA_SHADOW_CLEAR_FREE_LOCK
);
1061 ret
= rcuja_delete_ht(ja
->ht
);
1064 if (uatomic_read(&ja
->nr_fallback
))
1066 "[warning] RCU Judy Array used %lu fallback node(s)\n",
1067 uatomic_read(&ja
->nr_fallback
));