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_LOG2_BITS_PER_BYTE 3U
75 #define JA_BITS_PER_BYTE (1U << JA_LOG2_BITS_PER_BYTE)
77 #define JA_MAX_DEPTH 9 /* Maximum depth, including leafs */
80 * Entry for NULL node is at index 8 of the table. It is never encoded
83 #define NODE_INDEX_NULL 8
86 * Number of removals needed on a fallback node before we try to shrink
89 #define JA_FALLBACK_REMOVAL_COUNT 8
92 * Iteration on the array to find the right node size for the number of
93 * children stops when it reaches .max_child == 256 (this is the largest
94 * possible node size, which contains 256 children).
95 * The min_child overlaps with the previous max_child to provide an
96 * hysteresis loop to reallocation for patterns of cyclic add/removal
97 * within the same node.
98 * The node the index within the following arrays is represented on 3
99 * bits. It identifies the node type, min/max number of children, and
101 * The max_child values for the RCU_JA_POOL below result from
102 * statistical approximation: over million populations, the max_child
103 * covers between 97% and 99% of the populations generated. Therefore, a
104 * fallback should exist to cover the rare extreme population unbalance
105 * cases, but it will not have a major impact on speed nor space
106 * consumption, since those are rare cases.
109 #if (CAA_BITS_PER_LONG < 64)
110 /* 32-bit pointers */
112 ja_type_0_max_child
= 1,
113 ja_type_1_max_child
= 3,
114 ja_type_2_max_child
= 6,
115 ja_type_3_max_child
= 12,
116 ja_type_4_max_child
= 25,
117 ja_type_5_max_child
= 48,
118 ja_type_6_max_child
= 92,
119 ja_type_7_max_child
= 256,
120 ja_type_8_max_child
= 0, /* NULL */
124 ja_type_0_max_linear_child
= 1,
125 ja_type_1_max_linear_child
= 3,
126 ja_type_2_max_linear_child
= 6,
127 ja_type_3_max_linear_child
= 12,
128 ja_type_4_max_linear_child
= 25,
129 ja_type_5_max_linear_child
= 24,
130 ja_type_6_max_linear_child
= 23,
134 ja_type_5_nr_pool_order
= 1,
135 ja_type_6_nr_pool_order
= 2,
138 const struct cds_ja_type ja_types
[] = {
139 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_0_max_child
, .max_linear_child
= ja_type_0_max_linear_child
, .order
= 3, },
140 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_1_max_child
, .max_linear_child
= ja_type_1_max_linear_child
, .order
= 4, },
141 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= ja_type_2_max_child
, .max_linear_child
= ja_type_2_max_linear_child
, .order
= 5, },
142 { .type_class
= RCU_JA_LINEAR
, .min_child
= 4, .max_child
= ja_type_3_max_child
, .max_linear_child
= ja_type_3_max_linear_child
, .order
= 6, },
143 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= ja_type_4_max_child
, .max_linear_child
= ja_type_4_max_linear_child
, .order
= 7, },
145 /* Pools may fill sooner than max_child */
146 { .type_class
= RCU_JA_POOL
, .min_child
= 20, .max_child
= ja_type_5_max_child
, .max_linear_child
= ja_type_5_max_linear_child
, .order
= 8, .nr_pool_order
= ja_type_5_nr_pool_order
, .pool_size_order
= 7, },
147 { .type_class
= RCU_JA_POOL
, .min_child
= 45, .max_child
= ja_type_6_max_child
, .max_linear_child
= ja_type_6_max_linear_child
, .order
= 9, .nr_pool_order
= ja_type_6_nr_pool_order
, .pool_size_order
= 7, },
150 * TODO: Upon node removal below min_child, if child pool is
151 * filled beyond capacity, we need to roll back to pigeon.
153 { .type_class
= RCU_JA_PIGEON
, .min_child
= 89, .max_child
= ja_type_7_max_child
, .order
= 10, },
155 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
157 #else /* !(CAA_BITS_PER_LONG < 64) */
158 /* 64-bit pointers */
160 ja_type_0_max_child
= 1,
161 ja_type_1_max_child
= 3,
162 ja_type_2_max_child
= 7,
163 ja_type_3_max_child
= 14,
164 ja_type_4_max_child
= 28,
165 ja_type_5_max_child
= 54,
166 ja_type_6_max_child
= 104,
167 ja_type_7_max_child
= 256,
168 ja_type_8_max_child
= 256,
172 ja_type_0_max_linear_child
= 1,
173 ja_type_1_max_linear_child
= 3,
174 ja_type_2_max_linear_child
= 7,
175 ja_type_3_max_linear_child
= 14,
176 ja_type_4_max_linear_child
= 28,
177 ja_type_5_max_linear_child
= 27,
178 ja_type_6_max_linear_child
= 26,
182 ja_type_5_nr_pool_order
= 1,
183 ja_type_6_nr_pool_order
= 2,
186 const struct cds_ja_type ja_types
[] = {
187 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_0_max_child
, .max_linear_child
= ja_type_0_max_linear_child
, .order
= 4, },
188 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= ja_type_1_max_child
, .max_linear_child
= ja_type_1_max_linear_child
, .order
= 5, },
189 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= ja_type_2_max_child
, .max_linear_child
= ja_type_2_max_linear_child
, .order
= 6, },
190 { .type_class
= RCU_JA_LINEAR
, .min_child
= 5, .max_child
= ja_type_3_max_child
, .max_linear_child
= ja_type_3_max_linear_child
, .order
= 7, },
191 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= ja_type_4_max_child
, .max_linear_child
= ja_type_4_max_linear_child
, .order
= 8, },
193 /* Pools may fill sooner than max_child. */
194 { .type_class
= RCU_JA_POOL
, .min_child
= 22, .max_child
= ja_type_5_max_child
, .max_linear_child
= ja_type_5_max_linear_child
, .order
= 9, .nr_pool_order
= ja_type_5_nr_pool_order
, .pool_size_order
= 8, },
195 { .type_class
= RCU_JA_POOL
, .min_child
= 51, .max_child
= ja_type_6_max_child
, .max_linear_child
= ja_type_6_max_linear_child
, .order
= 10, .nr_pool_order
= ja_type_6_nr_pool_order
, .pool_size_order
= 8, },
198 * TODO: Upon node removal below min_child, if child pool is
199 * filled beyond capacity, we need to roll back to pigeon.
201 { .type_class
= RCU_JA_PIGEON
, .min_child
= 101, .max_child
= ja_type_7_max_child
, .order
= 11, },
203 { .type_class
= RCU_JA_NULL
, .min_child
= 0, .max_child
= ja_type_8_max_child
, },
205 #endif /* !(BITS_PER_LONG < 64) */
207 static inline __attribute__((unused
))
208 void static_array_size_check(void)
210 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) < JA_TYPE_MAX_NR
);
214 * The cds_ja_node contains the compressed node data needed for
215 * read-side. For linear and pool node configurations, it starts with a
216 * byte counting the number of children in the node. Then, the
217 * node-specific data is placed.
218 * The node mutex, if any is needed, protecting concurrent updated of
219 * each node is placed in a separate hash table indexed by node address.
220 * For the pigeon configuration, the number of children is also kept in
221 * a separate hash table, indexed by node address, because it is only
222 * required for updates.
225 #define DECLARE_LINEAR_NODE(index) \
228 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
229 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
232 #define DECLARE_POOL_NODE(index) \
236 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
237 struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
238 } linear[1U << ja_type_## index ##_nr_pool_order]; \
241 struct cds_ja_inode
{
243 /* Linear configuration */
244 DECLARE_LINEAR_NODE(0) conf_0
;
245 DECLARE_LINEAR_NODE(1) conf_1
;
246 DECLARE_LINEAR_NODE(2) conf_2
;
247 DECLARE_LINEAR_NODE(3) conf_3
;
248 DECLARE_LINEAR_NODE(4) conf_4
;
250 /* Pool configuration */
251 DECLARE_POOL_NODE(5) conf_5
;
252 DECLARE_POOL_NODE(6) conf_6
;
254 /* Pigeon configuration */
256 struct cds_ja_inode_flag
*child
[ja_type_7_max_child
];
258 /* data aliasing nodes for computed accesses */
259 uint8_t data
[sizeof(struct cds_ja_inode_flag
*) * ja_type_7_max_child
];
270 struct cds_ja_inode_flag
*ja_node_flag(struct cds_ja_inode
*node
,
273 assert(type
< (1UL << JA_TYPE_BITS
));
274 return (struct cds_ja_inode_flag
*) (((unsigned long) node
) | type
);
278 struct cds_ja_inode
*ja_node_ptr(struct cds_ja_inode_flag
*node
)
280 return (struct cds_ja_inode
*) (((unsigned long) node
) & JA_PTR_MASK
);
284 unsigned long ja_node_type(struct cds_ja_inode_flag
*node
)
288 if (ja_node_ptr(node
) == NULL
) {
289 return NODE_INDEX_NULL
;
291 type
= (unsigned int) ((unsigned long) node
& JA_TYPE_MASK
);
292 assert(type
< (1UL << JA_TYPE_BITS
));
296 struct cds_ja_inode
*alloc_cds_ja_node(const struct cds_ja_type
*ja_type
)
298 return calloc(1U << ja_type
->order
, sizeof(char));
301 void free_cds_ja_node(struct cds_ja_inode
*node
)
306 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
307 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
308 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
309 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
312 uint8_t *align_ptr_size(uint8_t *ptr
)
314 return (uint8_t *) JA_ALIGN((unsigned long) ptr
, sizeof(void *));
318 uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type
*type
,
319 struct cds_ja_inode
*node
)
321 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
322 return rcu_dereference(node
->u
.data
[0]);
326 * The order in which values and pointers are does does not matter: if
327 * a value is missing, we return NULL. If a value is there, but its
328 * associated pointers is still NULL, we return NULL too.
331 struct cds_ja_inode_flag
*ja_linear_node_get_nth(const struct cds_ja_type
*type
,
332 struct cds_ja_inode
*node
,
333 struct cds_ja_inode_flag
***child_node_flag_ptr
,
338 struct cds_ja_inode_flag
**pointers
;
339 struct cds_ja_inode_flag
*ptr
;
342 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
344 nr_child
= ja_linear_node_get_nr_child(type
, node
);
345 cmm_smp_rmb(); /* read nr_child before values and pointers */
346 assert(nr_child
<= type
->max_linear_child
);
347 assert(type
->type_class
!= RCU_JA_LINEAR
|| nr_child
>= type
->min_child
);
349 values
= &node
->u
.data
[1];
350 for (i
= 0; i
< nr_child
; i
++) {
351 if (CMM_LOAD_SHARED(values
[i
]) == n
)
356 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
357 ptr
= rcu_dereference(pointers
[i
]);
358 if (caa_unlikely(child_node_flag_ptr
) && ptr
)
359 *child_node_flag_ptr
= &pointers
[i
];
364 void ja_linear_node_get_ith_pos(const struct cds_ja_type
*type
,
365 struct cds_ja_inode
*node
,
368 struct cds_ja_inode_flag
**iter
)
371 struct cds_ja_inode_flag
**pointers
;
373 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
374 assert(i
< ja_linear_node_get_nr_child(type
, node
));
376 values
= &node
->u
.data
[1];
378 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
383 struct cds_ja_inode_flag
*ja_pool_node_get_nth(const struct cds_ja_type
*type
,
384 struct cds_ja_inode
*node
,
385 struct cds_ja_inode_flag
***child_node_flag_ptr
,
388 struct cds_ja_inode
*linear
;
390 assert(type
->type_class
== RCU_JA_POOL
);
392 * TODO: currently, we select the pool by highest bits. We
393 * should support various encodings.
395 linear
= (struct cds_ja_inode
*)
396 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
397 return ja_linear_node_get_nth(type
, linear
, child_node_flag_ptr
, n
);
401 struct cds_ja_inode
*ja_pool_node_get_ith_pool(const struct cds_ja_type
*type
,
402 struct cds_ja_inode
*node
,
405 assert(type
->type_class
== RCU_JA_POOL
);
406 return (struct cds_ja_inode
*)
407 &node
->u
.data
[(unsigned int) i
<< type
->pool_size_order
];
411 struct cds_ja_inode_flag
*ja_pigeon_node_get_nth(const struct cds_ja_type
*type
,
412 struct cds_ja_inode
*node
,
413 struct cds_ja_inode_flag
***child_node_flag_ptr
,
416 struct cds_ja_inode_flag
**child_node_flag
;
418 assert(type
->type_class
== RCU_JA_PIGEON
);
419 child_node_flag
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
420 dbg_printf("ja_pigeon_node_get_nth child_node_flag_ptr %p\n",
422 if (caa_unlikely(child_node_flag_ptr
) && *child_node_flag
)
423 *child_node_flag_ptr
= child_node_flag
;
424 return rcu_dereference(*child_node_flag
);
428 struct cds_ja_inode_flag
*ja_pigeon_node_get_ith_pos(const struct cds_ja_type
*type
,
429 struct cds_ja_inode
*node
,
432 return ja_pigeon_node_get_nth(type
, node
, NULL
, i
);
436 * ja_node_get_nth: get nth item from a node.
437 * node_flag is already rcu_dereference'd.
440 struct cds_ja_inode_flag
* ja_node_get_nth(struct cds_ja_inode_flag
*node_flag
,
441 struct cds_ja_inode_flag
***child_node_flag_ptr
,
444 unsigned int type_index
;
445 struct cds_ja_inode
*node
;
446 const struct cds_ja_type
*type
;
448 node
= ja_node_ptr(node_flag
);
449 assert(node
!= NULL
);
450 type_index
= ja_node_type(node_flag
);
451 type
= &ja_types
[type_index
];
453 switch (type
->type_class
) {
455 return ja_linear_node_get_nth(type
, node
,
456 child_node_flag_ptr
, n
);
458 return ja_pool_node_get_nth(type
, node
,
459 child_node_flag_ptr
, n
);
461 return ja_pigeon_node_get_nth(type
, node
,
462 child_node_flag_ptr
, n
);
465 return (void *) -1UL;
470 int ja_linear_node_set_nth(const struct cds_ja_type
*type
,
471 struct cds_ja_inode
*node
,
472 struct cds_ja_shadow_node
*shadow_node
,
474 struct cds_ja_inode_flag
*child_node_flag
)
477 uint8_t *values
, *nr_child_ptr
;
478 struct cds_ja_inode_flag
**pointers
;
479 unsigned int i
, unused
= 0;
481 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
483 nr_child_ptr
= &node
->u
.data
[0];
484 dbg_printf("linear set nth: nr_child_ptr %p\n", nr_child_ptr
);
485 nr_child
= *nr_child_ptr
;
486 assert(nr_child
<= type
->max_linear_child
);
488 values
= &node
->u
.data
[1];
489 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
490 /* Check if node value is already populated */
491 for (i
= 0; i
< nr_child
; i
++) {
492 if (values
[i
] == n
) {
502 if (i
== nr_child
&& nr_child
>= type
->max_linear_child
) {
504 return -ERANGE
; /* recompact node */
506 return -ENOSPC
; /* No space left in this node type */
509 assert(pointers
[i
] == NULL
);
510 rcu_assign_pointer(pointers
[i
], child_node_flag
);
511 /* If we expanded the nr_child, increment it */
513 CMM_STORE_SHARED(values
[nr_child
], n
);
514 /* write pointer and value before nr_child */
516 CMM_STORE_SHARED(*nr_child_ptr
, nr_child
+ 1);
518 shadow_node
->nr_child
++;
519 dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n",
520 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr
),
521 (unsigned int) shadow_node
->nr_child
,
528 int ja_pool_node_set_nth(const struct cds_ja_type
*type
,
529 struct cds_ja_inode
*node
,
530 struct cds_ja_shadow_node
*shadow_node
,
532 struct cds_ja_inode_flag
*child_node_flag
)
534 struct cds_ja_inode
*linear
;
536 assert(type
->type_class
== RCU_JA_POOL
);
537 linear
= (struct cds_ja_inode
*)
538 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
539 return ja_linear_node_set_nth(type
, linear
, shadow_node
,
544 int ja_pigeon_node_set_nth(const struct cds_ja_type
*type
,
545 struct cds_ja_inode
*node
,
546 struct cds_ja_shadow_node
*shadow_node
,
548 struct cds_ja_inode_flag
*child_node_flag
)
550 struct cds_ja_inode_flag
**ptr
;
552 assert(type
->type_class
== RCU_JA_PIGEON
);
553 ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
556 rcu_assign_pointer(*ptr
, child_node_flag
);
557 shadow_node
->nr_child
++;
562 * _ja_node_set_nth: set nth item within a node. Return an error
563 * (negative error value) if it is already there.
566 int _ja_node_set_nth(const struct cds_ja_type
*type
,
567 struct cds_ja_inode
*node
,
568 struct cds_ja_shadow_node
*shadow_node
,
570 struct cds_ja_inode_flag
*child_node_flag
)
572 switch (type
->type_class
) {
574 return ja_linear_node_set_nth(type
, node
, shadow_node
, n
,
577 return ja_pool_node_set_nth(type
, node
, shadow_node
, n
,
580 return ja_pigeon_node_set_nth(type
, node
, shadow_node
, n
,
593 int ja_linear_node_clear_nth(const struct cds_ja_type
*type
,
594 struct cds_ja_inode
*node
,
595 struct cds_ja_shadow_node
*shadow_node
,
599 uint8_t *values
, *nr_child_ptr
;
600 struct cds_ja_inode_flag
**pointers
;
603 assert(type
->type_class
== RCU_JA_LINEAR
|| type
->type_class
== RCU_JA_POOL
);
605 nr_child_ptr
= &node
->u
.data
[0];
606 dbg_printf("linear clear nth: nr_child_ptr %p\n", nr_child_ptr
);
607 nr_child
= *nr_child_ptr
;
608 assert(nr_child
<= type
->max_linear_child
);
610 values
= &node
->u
.data
[1];
611 pointers
= (struct cds_ja_inode_flag
**) align_ptr_size(&values
[type
->max_linear_child
]);
612 for (i
= 0; i
< nr_child
; i
++) {
613 if (values
[i
] == n
) {
622 if (shadow_node
->fallback_removal_count
) {
623 shadow_node
->fallback_removal_count
--;
625 if (shadow_node
->nr_child
<= type
->min_child
) {
626 /* We need to try recompacting the node */
630 assert(pointers
[i
] != NULL
);
631 rcu_assign_pointer(pointers
[i
], NULL
);
633 * Value and nr_child are never changed (would cause ABA issue).
634 * Instead, we leave the pointer to NULL and recompact the node
635 * once in a while. It is allowed to set a NULL pointer to a new
636 * value without recompaction though.
637 * Only update the shadow node accounting.
639 shadow_node
->nr_child
--;
640 dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n",
641 (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr
),
642 (unsigned int) shadow_node
->nr_child
,
649 int ja_pool_node_clear_nth(const struct cds_ja_type
*type
,
650 struct cds_ja_inode
*node
,
651 struct cds_ja_shadow_node
*shadow_node
,
654 struct cds_ja_inode
*linear
;
656 assert(type
->type_class
== RCU_JA_POOL
);
657 linear
= (struct cds_ja_inode
*)
658 &node
->u
.data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
659 return ja_linear_node_clear_nth(type
, linear
, shadow_node
, n
);
663 int ja_pigeon_node_clear_nth(const struct cds_ja_type
*type
,
664 struct cds_ja_inode
*node
,
665 struct cds_ja_shadow_node
*shadow_node
,
668 struct cds_ja_inode_flag
**ptr
;
670 assert(type
->type_class
== RCU_JA_PIGEON
);
671 ptr
= &((struct cds_ja_inode_flag
**) node
->u
.data
)[n
];
674 rcu_assign_pointer(*ptr
, NULL
);
675 shadow_node
->nr_child
--;
680 * _ja_node_clear_nth: clear nth item within a node. Return an error
681 * (negative error value) if it is not found (-ENOENT).
684 int _ja_node_clear_nth(const struct cds_ja_type
*type
,
685 struct cds_ja_inode
*node
,
686 struct cds_ja_shadow_node
*shadow_node
,
689 switch (type
->type_class
) {
691 return ja_linear_node_clear_nth(type
, node
, shadow_node
, n
);
693 return ja_pool_node_clear_nth(type
, node
, shadow_node
, n
);
695 return ja_pigeon_node_clear_nth(type
, node
, shadow_node
, n
);
707 * ja_node_recompact_add: recompact a node, adding a new child.
708 * TODO: for pool type, take selection bit(s) into account.
709 * Return 0 on success, -EAGAIN if need to retry, or other negative
710 * error value otherwise.
713 int ja_node_recompact(enum ja_recompact mode
,
715 unsigned int old_type_index
,
716 const struct cds_ja_type
*old_type
,
717 struct cds_ja_inode
*old_node
,
718 struct cds_ja_shadow_node
*shadow_node
,
719 struct cds_ja_inode_flag
**old_node_flag
, uint8_t n
,
720 struct cds_ja_inode_flag
*child_node_flag
)
722 unsigned int new_type_index
;
723 struct cds_ja_inode
*new_node
;
724 struct cds_ja_shadow_node
*new_shadow_node
;
725 const struct cds_ja_type
*new_type
;
726 struct cds_ja_inode_flag
*new_node_flag
;
732 new_type_index
= old_type_index
;
734 case JA_RECOMPACT_ADD
:
735 if (!shadow_node
|| old_type_index
== NODE_INDEX_NULL
) {
738 new_type_index
= old_type_index
+ 1;
741 case JA_RECOMPACT_DEL
:
742 if (old_type_index
== 0) {
743 new_type_index
= NODE_INDEX_NULL
;
745 new_type_index
= old_type_index
- 1;
752 retry
: /* for fallback */
753 dbg_printf("Recompact from type %d to type %d\n",
754 old_type_index
, new_type_index
);
755 new_type
= &ja_types
[new_type_index
];
756 if (new_type_index
!= NODE_INDEX_NULL
) {
757 new_node
= alloc_cds_ja_node(new_type
);
760 new_node_flag
= ja_node_flag(new_node
, new_type_index
);
761 dbg_printf("Recompact inherit lock from %p\n", shadow_node
);
762 new_shadow_node
= rcuja_shadow_set(ja
->ht
, new_node
, shadow_node
);
763 if (!new_shadow_node
) {
768 new_shadow_node
->fallback_removal_count
=
769 JA_FALLBACK_REMOVAL_COUNT
;
772 new_node_flag
= NULL
;
775 assert(mode
!= JA_RECOMPACT_ADD
|| old_type
->type_class
!= RCU_JA_PIGEON
);
777 if (new_type_index
== NODE_INDEX_NULL
)
780 switch (old_type
->type_class
) {
784 ja_linear_node_get_nr_child(old_type
, old_node
);
787 for (i
= 0; i
< nr_child
; i
++) {
788 struct cds_ja_inode_flag
*iter
;
791 ja_linear_node_get_ith_pos(old_type
, old_node
, i
, &v
, &iter
);
794 if (mode
== JA_RECOMPACT_DEL
&& v
== n
)
796 ret
= _ja_node_set_nth(new_type
, new_node
,
799 if (new_type
->type_class
== RCU_JA_POOL
&& ret
) {
800 goto fallback_toosmall
;
808 unsigned int pool_nr
;
810 for (pool_nr
= 0; pool_nr
< (1U << old_type
->nr_pool_order
); pool_nr
++) {
811 struct cds_ja_inode
*pool
=
812 ja_pool_node_get_ith_pool(old_type
,
815 ja_linear_node_get_nr_child(old_type
, pool
);
818 for (j
= 0; j
< nr_child
; j
++) {
819 struct cds_ja_inode_flag
*iter
;
822 ja_linear_node_get_ith_pos(old_type
, pool
,
826 if (mode
== JA_RECOMPACT_DEL
&& v
== n
)
828 ret
= _ja_node_set_nth(new_type
, new_node
,
831 if (new_type
->type_class
== RCU_JA_POOL
833 goto fallback_toosmall
;
841 assert(mode
== JA_RECOMPACT_ADD
);
848 assert(mode
== JA_RECOMPACT_DEL
);
849 nr_child
= shadow_node
->nr_child
;
850 for (i
= 0; i
< nr_child
; i
++) {
851 struct cds_ja_inode_flag
*iter
;
853 iter
= ja_pigeon_node_get_ith_pos(old_type
, old_node
, i
);
856 if (mode
== JA_RECOMPACT_DEL
&& i
== n
)
858 ret
= _ja_node_set_nth(new_type
, new_node
,
861 if (new_type
->type_class
== RCU_JA_POOL
&& ret
) {
862 goto fallback_toosmall
;
875 if (JA_RECOMPACT_ADD
) {
877 ret
= _ja_node_set_nth(new_type
, new_node
,
882 /* Return pointer to new recompacted node through old_node_flag */
883 *old_node_flag
= new_node_flag
;
887 flags
= RCUJA_SHADOW_CLEAR_FREE_NODE
;
889 * It is OK to free the lock associated with a node
890 * going to NULL, since we are holding the parent lock.
891 * This synchronizes removal with re-add of that node.
893 if (new_type_index
== NODE_INDEX_NULL
)
894 flags
= RCUJA_SHADOW_CLEAR_FREE_LOCK
;
895 ret
= rcuja_shadow_clear(ja
->ht
, old_node
, shadow_node
,
905 /* fallback if next pool is too small */
906 ret
= rcuja_shadow_clear(ja
->ht
, new_node
, new_shadow_node
,
907 RCUJA_SHADOW_CLEAR_FREE_NODE
);
910 /* Choose fallback type: pigeon */
911 new_type_index
= (1UL << JA_TYPE_BITS
) - 1;
912 dbg_printf("Fallback to type %d\n", new_type_index
);
913 uatomic_inc(&ja
->nr_fallback
);
919 * Return 0 on success, -EAGAIN if need to retry, or other negative
920 * error value otherwise.
923 int ja_node_set_nth(struct cds_ja
*ja
,
924 struct cds_ja_inode_flag
**node_flag
, uint8_t n
,
925 struct cds_ja_inode_flag
*child_node_flag
,
926 struct cds_ja_shadow_node
*shadow_node
)
929 unsigned int type_index
;
930 const struct cds_ja_type
*type
;
931 struct cds_ja_inode
*node
;
933 dbg_printf("ja_node_set_nth for n=%u, node %p, shadow %p\n",
934 (unsigned int) n
, ja_node_ptr(*node_flag
), shadow_node
);
936 node
= ja_node_ptr(*node_flag
);
937 type_index
= ja_node_type(*node_flag
);
938 type
= &ja_types
[type_index
];
939 ret
= _ja_node_set_nth(type
, node
, shadow_node
,
943 /* Not enough space in node, need to recompact. */
944 ret
= ja_node_recompact(JA_RECOMPACT_ADD
, ja
, type_index
, type
, node
,
945 shadow_node
, node_flag
, n
, child_node_flag
);
948 /* Node needs to be recompacted. */
949 ret
= ja_node_recompact(JA_RECOMPACT
, ja
, type_index
, type
, node
,
950 shadow_node
, node_flag
, n
, child_node_flag
);
957 * Return 0 on success, -EAGAIN if need to retry, or other negative
958 * error value otherwise.
961 int ja_node_clear_nth(struct cds_ja
*ja
,
962 struct cds_ja_inode_flag
**node_flag
, uint8_t n
,
963 struct cds_ja_shadow_node
*shadow_node
)
966 unsigned int type_index
;
967 const struct cds_ja_type
*type
;
968 struct cds_ja_inode
*node
;
970 dbg_printf("ja_node_clear_nth for n=%u, node %p, shadow %p\n",
971 (unsigned int) n
, ja_node_ptr(*node_flag
), shadow_node
);
973 node
= ja_node_ptr(*node_flag
);
974 type_index
= ja_node_type(*node_flag
);
975 type
= &ja_types
[type_index
];
976 ret
= _ja_node_clear_nth(type
, node
, shadow_node
, n
);
978 /* Should to try recompaction. */
979 ret
= ja_node_recompact(JA_RECOMPACT_DEL
, ja
, type_index
, type
, node
,
980 shadow_node
, node_flag
, n
, NULL
);
985 struct cds_hlist_head
*cds_ja_lookup(struct cds_ja
*ja
, uint64_t key
)
987 unsigned int tree_depth
, i
;
988 struct cds_ja_inode_flag
*node_flag
;
990 if (caa_unlikely(key
> ja
->key_max
))
992 tree_depth
= ja
->tree_depth
;
993 node_flag
= rcu_dereference(ja
->root
);
995 /* level 0: root node */
996 if (!ja_node_ptr(node_flag
))
999 for (i
= 1; i
< tree_depth
; i
++) {
1002 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
1003 node_flag
= ja_node_get_nth(node_flag
, NULL
,
1005 dbg_printf("cds_ja_lookup iter key lookup %u finds node_flag %p\n",
1006 (unsigned int) iter_key
, node_flag
);
1007 if (!ja_node_ptr(node_flag
))
1011 /* Last level lookup succeded. We got an actual match. */
1012 return (struct cds_hlist_head
*) node_flag
;
1016 * We reached an unpopulated node. Create it and the children we need,
1017 * and then attach the entire branch to the current node. This may
1018 * trigger recompaction of the current node. Locks needed: node lock
1019 * (for add), and, possibly, parent node lock (to update pointer due to
1020 * node recompaction).
1022 * First take node lock, check if recompaction is needed, then take
1023 * parent lock (if needed). Then we can proceed to create the new
1024 * branch. Publish the new branch, and release locks.
1025 * TODO: we currently always take the parent lock even when not needed.
1028 int ja_attach_node(struct cds_ja
*ja
,
1029 struct cds_ja_inode_flag
**node_flag_ptr
,
1030 struct cds_ja_inode_flag
*node_flag
,
1031 struct cds_ja_inode_flag
*parent_node_flag
,
1034 struct cds_ja_node
*child_node
)
1036 struct cds_ja_shadow_node
*shadow_node
= NULL
,
1037 *parent_shadow_node
= NULL
,
1039 struct cds_ja_inode
*node
= ja_node_ptr(node_flag
);
1040 struct cds_ja_inode
*parent_node
= ja_node_ptr(parent_node_flag
);
1041 struct cds_hlist_head head
;
1042 struct cds_ja_inode_flag
*iter_node_flag
, *iter_dest_node_flag
;
1044 struct cds_ja_inode_flag
*created_nodes
[JA_MAX_DEPTH
];
1045 int nr_created_nodes
= 0;
1047 dbg_printf("Attach node at level %u (node %p, node_flag %p)\n",
1048 level
, node
, node_flag
);
1051 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
, node
);
1057 parent_shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
1059 if (!parent_shadow_node
) {
1065 /* Create new branch, starting from bottom */
1066 CDS_INIT_HLIST_HEAD(&head
);
1067 cds_hlist_add_head_rcu(&child_node
->list
, &head
);
1068 iter_node_flag
= (struct cds_ja_inode_flag
*) head
.next
;
1070 /* Create shadow node for the leaf node */
1071 dbg_printf("leaf shadow node creation\n");
1072 iter_shadow_node
= rcuja_shadow_set(ja
->ht
,
1073 ja_node_ptr(iter_node_flag
), NULL
);
1074 if (!iter_shadow_node
) {
1078 created_nodes
[nr_created_nodes
++] = iter_node_flag
;
1080 for (i
= ja
->tree_depth
; i
> (int) level
; i
--) {
1083 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (ja
->tree_depth
- i
)));
1084 dbg_printf("branch creation level %d, key %u\n",
1085 i
- 1, (unsigned int) iter_key
);
1086 iter_dest_node_flag
= NULL
;
1087 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
1093 created_nodes
[nr_created_nodes
++] = iter_dest_node_flag
;
1094 iter_node_flag
= iter_dest_node_flag
;
1100 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (ja
->tree_depth
- level
)));
1101 /* We need to use set_nth on the previous level. */
1102 iter_dest_node_flag
= node_flag
;
1103 ret
= ja_node_set_nth(ja
, &iter_dest_node_flag
,
1109 created_nodes
[nr_created_nodes
++] = iter_dest_node_flag
;
1110 iter_node_flag
= iter_dest_node_flag
;
1113 /* Publish new branch */
1114 dbg_printf("Publish branch %p, replacing %p\n",
1115 iter_node_flag
, *node_flag_ptr
);
1116 rcu_assign_pointer(*node_flag_ptr
, iter_node_flag
);
1123 for (i
= 0; i
< nr_created_nodes
; i
++) {
1127 flags
= RCUJA_SHADOW_CLEAR_FREE_LOCK
;
1129 flags
|= RCUJA_SHADOW_CLEAR_FREE_NODE
;
1130 tmpret
= rcuja_shadow_clear(ja
->ht
,
1131 ja_node_ptr(created_nodes
[i
]),
1137 if (parent_shadow_node
)
1138 rcuja_shadow_unlock(parent_shadow_node
);
1141 rcuja_shadow_unlock(shadow_node
);
1147 * Lock the hlist head shadow node mutex, and add node to list of
1148 * duplicates. Failure can happen if concurrent removal removes the last
1149 * node with same key before we get the lock.
1150 * Return 0 on success, negative error value on failure.
1153 int ja_chain_node(struct cds_ja
*ja
,
1154 struct cds_hlist_head
*head
,
1155 struct cds_ja_node
*node
)
1157 struct cds_ja_shadow_node
*shadow_node
;
1159 shadow_node
= rcuja_shadow_lookup_lock(ja
->ht
,
1160 (struct cds_ja_inode
*) head
);
1163 cds_hlist_add_head_rcu(&node
->list
, head
);
1164 rcuja_shadow_unlock(shadow_node
);
1168 int cds_ja_add(struct cds_ja
*ja
, uint64_t key
,
1169 struct cds_ja_node
*new_node
)
1171 unsigned int tree_depth
, i
;
1172 struct cds_ja_inode_flag
**node_flag_ptr
; /* in parent */
1173 struct cds_ja_inode_flag
*node_flag
,
1178 if (caa_unlikely(key
> ja
->key_max
))
1180 tree_depth
= ja
->tree_depth
;
1183 dbg_printf("cds_ja_add attempt: key %" PRIu64
", node %p\n",
1185 parent2_node_flag
= NULL
;
1187 (struct cds_ja_inode_flag
*) &ja
->root
; /* Use root ptr address as key for mutex */
1188 node_flag_ptr
= &ja
->root
;
1189 node_flag
= rcu_dereference(ja
->root
);
1191 /* Iterate on all internal levels */
1192 for (i
= 1; i
< tree_depth
; i
++) {
1195 dbg_printf("cds_ja_add iter node_flag_ptr %p node_flag %p\n",
1196 *node_flag_ptr
, node_flag
);
1197 if (!ja_node_ptr(node_flag
)) {
1198 ret
= ja_attach_node(ja
, node_flag_ptr
,
1199 parent_node_flag
, parent2_node_flag
,
1201 if (ret
== -EAGAIN
|| ret
== -EEXIST
)
1206 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
1207 parent2_node_flag
= parent_node_flag
;
1208 parent_node_flag
= node_flag
;
1209 node_flag
= ja_node_get_nth(node_flag
,
1212 dbg_printf("cds_ja_add iter key lookup %u finds node_flag %p node_flag_ptr %p\n",
1213 (unsigned int) iter_key
, node_flag
, *node_flag_ptr
);
1217 * We reached bottom of tree, simply add node to last internal
1218 * level, or chain it if key is already present.
1220 if (!ja_node_ptr(node_flag
)) {
1221 dbg_printf("cds_ja_add last node_flag_ptr %p node_flag %p\n",
1222 *node_flag_ptr
, node_flag
);
1223 ret
= ja_attach_node(ja
, node_flag_ptr
, parent_node_flag
,
1224 parent2_node_flag
, key
, i
, new_node
);
1226 ret
= ja_chain_node(ja
,
1227 (struct cds_hlist_head
*) ja_node_ptr(node_flag
),
1237 int ja_detach_node(struct cds_ja
*ja
,
1238 struct cds_ja_inode_flag
**snapshot
,
1241 struct cds_ja_node
*node
)
1245 assert(nr_snapshot
== ja
->tree_depth
- 1);
1250 int cds_ja_del(struct cds_ja
*ja
, uint64_t key
,
1251 struct cds_ja_node
*node
)
1253 unsigned int tree_depth
, i
;
1254 struct cds_ja_inode_flag
*snapshot
[JA_MAX_DEPTH
];
1255 struct cds_ja_inode_flag
*node_flag
;
1256 int nr_snapshot
= 0;
1259 if (caa_unlikely(key
> ja
->key_max
))
1261 tree_depth
= ja
->tree_depth
;
1264 dbg_printf("cds_ja_del attempt: key %" PRIu64
", node %p\n",
1267 /* snapshot for level 0 is only for shadow node lookup */
1268 snapshot
[nr_snapshot
++] = (struct cds_ja_inode_flag
*) &ja
->root
;
1269 node_flag
= rcu_dereference(ja
->root
);
1271 /* Iterate on all internal levels */
1272 for (i
= 1; i
< tree_depth
; i
++) {
1275 dbg_printf("cds_ja_del iter node_flag %p\n",
1277 if (!ja_node_ptr(node_flag
)) {
1280 snapshot
[nr_snapshot
++] = node_flag
;
1281 iter_key
= (uint8_t) (key
>> (JA_BITS_PER_BYTE
* (tree_depth
- i
- 1)));
1282 node_flag
= ja_node_get_nth(node_flag
,
1285 dbg_printf("cds_ja_del iter key lookup %u finds node_flag %p\n",
1286 (unsigned int) iter_key
, node_flag
);
1290 * We reached bottom of tree, try to find the node we are trying
1291 * to remove. Fail if we cannot find it.
1293 if (!ja_node_ptr(node_flag
)) {
1296 struct cds_hlist_head
*hlist_head
;
1297 struct cds_hlist_node
*hlist_node
;
1298 struct cds_ja_node
*entry
;
1301 hlist_head
= (struct cds_hlist_head
*) ja_node_ptr(node_flag
);
1302 cds_hlist_for_each_entry(entry
,
1306 if (entry
== node
) {
1313 ret
= ja_detach_node(ja
, snapshot
, nr_snapshot
, key
, node
);
1320 struct cds_ja
*_cds_ja_new(unsigned int key_bits
,
1321 const struct rcu_flavor_struct
*flavor
)
1325 struct cds_ja_shadow_node
*root_shadow_node
;
1327 ja
= calloc(sizeof(*ja
), 1);
1333 ja
->key_max
= UINT8_MAX
;
1336 ja
->key_max
= UINT16_MAX
;
1339 ja
->key_max
= UINT32_MAX
;
1342 ja
->key_max
= UINT64_MAX
;
1348 /* ja->root is NULL */
1349 /* tree_depth 0 is for pointer to root node */
1350 ja
->tree_depth
= (key_bits
>> JA_LOG2_BITS_PER_BYTE
) + 1;
1351 assert(ja
->tree_depth
<= JA_MAX_DEPTH
);
1352 ja
->ht
= rcuja_create_ht(flavor
);
1357 * Note: we should not free this node until judy array destroy.
1359 root_shadow_node
= rcuja_shadow_set(ja
->ht
,
1360 ja_node_ptr((struct cds_ja_inode_flag
*) &ja
->root
),
1362 if (!root_shadow_node
) {
1366 root_shadow_node
->is_root
= 1;
1371 ret
= rcuja_delete_ht(ja
->ht
);
1381 * There should be no more concurrent add to the judy array while it is
1382 * being destroyed (ensured by the caller).
1384 int cds_ja_destroy(struct cds_ja
*ja
)
1388 rcuja_shadow_prune(ja
->ht
,
1389 RCUJA_SHADOW_CLEAR_FREE_NODE
| RCUJA_SHADOW_CLEAR_FREE_LOCK
);
1390 ret
= rcuja_delete_ht(ja
->ht
);
1393 if (uatomic_read(&ja
->nr_fallback
))
1395 "[warning] RCU Judy Array used %lu fallback node(s)\n",
1396 uatomic_read(&ja
->nr_fallback
));