Commit | Line | Data |
---|---|---|
61009379 MD |
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 | ||
195e72d3 | 23 | #define _LGPL_SOURCE |
e5227865 | 24 | #include <stdint.h> |
8e519e3c | 25 | #include <errno.h> |
d68c6810 | 26 | #include <limits.h> |
61009379 | 27 | #include <urcu/rcuja.h> |
d68c6810 MD |
28 | #include <urcu/compiler.h> |
29 | #include <urcu/arch.h> | |
30 | #include <assert.h> | |
8e519e3c | 31 | #include <urcu-pointer.h> |
f07b240f | 32 | #include <urcu/uatomic.h> |
b4540e8a | 33 | #include <stdint.h> |
8e519e3c | 34 | |
61009379 | 35 | #include "rcuja-internal.h" |
d68c6810 | 36 | #include "bitfield.h" |
61009379 | 37 | |
d96bfb0d | 38 | enum cds_ja_type_class { |
e5227865 | 39 | RCU_JA_LINEAR = 0, /* Type A */ |
fd800776 MD |
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 */ | |
e5227865 | 45 | RCU_JA_PIGEON = 2, /* Type C */ |
fd800776 MD |
46 | /* 32-bit: 101 to 256 children, 1024 bytes */ |
47 | /* 64-bit: 113 to 256 children, 2048 bytes */ | |
e5227865 | 48 | /* Leaf nodes are implicit from their height in the tree */ |
1db4943c | 49 | RCU_JA_NR_TYPES, |
e1db2db5 MD |
50 | |
51 | RCU_JA_NULL, /* not an encoded type, but keeps code regular */ | |
e5227865 MD |
52 | }; |
53 | ||
d96bfb0d MD |
54 | struct cds_ja_type { |
55 | enum cds_ja_type_class type_class; | |
8e519e3c MD |
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 */ | |
fd800776 MD |
60 | uint16_t nr_pool_order; /* number of pools */ |
61 | uint16_t pool_size_order; /* pool size */ | |
e5227865 MD |
62 | }; |
63 | ||
d68c6810 MD |
64 | /* |
65 | * Number of least significant pointer bits reserved to represent the | |
66 | * child type. | |
67 | */ | |
68 | #define JA_TYPE_BITS 3 | |
a2a7ff59 | 69 | #define JA_TYPE_MAX_NR (1UL << JA_TYPE_BITS) |
d68c6810 MD |
70 | #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1) |
71 | #define JA_PTR_MASK (~JA_TYPE_MASK) | |
72 | ||
73 | #define JA_ENTRY_PER_NODE 256UL | |
b4540e8a | 74 | #define JA_BITS_PER_BYTE 3 |
d68c6810 | 75 | |
5b0f19e6 | 76 | #define JA_MAX_DEPTH 9 /* Maximum depth, including leafs */ |
5a9a87dd | 77 | |
e1db2db5 MD |
78 | /* |
79 | * Entry for NULL node is at index 8 of the table. It is never encoded | |
80 | * in flags. | |
81 | */ | |
82 | #define NODE_INDEX_NULL 8 | |
83 | ||
f07b240f MD |
84 | /* |
85 | * Number of removals needed on a fallback node before we try to shrink | |
86 | * it. | |
87 | */ | |
88 | #define JA_FALLBACK_REMOVAL_COUNT 8 | |
89 | ||
e5227865 MD |
90 | /* |
91 | * Iteration on the array to find the right node size for the number of | |
d68c6810 | 92 | * children stops when it reaches .max_child == 256 (this is the largest |
e5227865 | 93 | * possible node size, which contains 256 children). |
d68c6810 MD |
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 | |
99 | * the size order. | |
3d45251f MD |
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. | |
e5227865 | 106 | */ |
e5227865 | 107 | |
d68c6810 MD |
108 | #if (CAA_BITS_PER_LONG < 64) |
109 | /* 32-bit pointers */ | |
1db4943c MD |
110 | enum { |
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, | |
e1db2db5 | 119 | ja_type_8_max_child = 0, /* NULL */ |
1db4943c MD |
120 | }; |
121 | ||
8e519e3c MD |
122 | enum { |
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, | |
130 | }; | |
131 | ||
1db4943c MD |
132 | enum { |
133 | ja_type_5_nr_pool_order = 1, | |
134 | ja_type_6_nr_pool_order = 2, | |
135 | }; | |
136 | ||
d96bfb0d | 137 | const struct cds_ja_type ja_types[] = { |
8e519e3c MD |
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, }, | |
e5227865 | 143 | |
fd800776 | 144 | /* Pools may fill sooner than max_child */ |
8e519e3c MD |
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, }, | |
3d45251f MD |
147 | |
148 | /* | |
149 | * TODO: Upon node removal below min_child, if child pool is | |
150 | * filled beyond capacity, we need to roll back to pigeon. | |
151 | */ | |
1db4943c | 152 | { .type_class = RCU_JA_PIGEON, .min_child = 89, .max_child = ja_type_7_max_child, .order = 10, }, |
e1db2db5 MD |
153 | |
154 | { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, }, | |
d68c6810 | 155 | }; |
d68c6810 MD |
156 | #else /* !(CAA_BITS_PER_LONG < 64) */ |
157 | /* 64-bit pointers */ | |
1db4943c MD |
158 | enum { |
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, | |
e1db2db5 | 167 | ja_type_8_max_child = 256, |
1db4943c MD |
168 | }; |
169 | ||
8e519e3c MD |
170 | enum { |
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, | |
178 | }; | |
179 | ||
1db4943c MD |
180 | enum { |
181 | ja_type_5_nr_pool_order = 1, | |
182 | ja_type_6_nr_pool_order = 2, | |
183 | }; | |
184 | ||
d96bfb0d | 185 | const struct cds_ja_type ja_types[] = { |
8e519e3c MD |
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, }, | |
e5227865 | 191 | |
3d45251f | 192 | /* Pools may fill sooner than max_child. */ |
8e519e3c MD |
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, }, | |
e5227865 | 195 | |
3d45251f MD |
196 | /* |
197 | * TODO: Upon node removal below min_child, if child pool is | |
198 | * filled beyond capacity, we need to roll back to pigeon. | |
199 | */ | |
1db4943c | 200 | { .type_class = RCU_JA_PIGEON, .min_child = 101, .max_child = ja_type_7_max_child, .order = 11, }, |
e1db2db5 MD |
201 | |
202 | { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, }, | |
e5227865 | 203 | }; |
d68c6810 | 204 | #endif /* !(BITS_PER_LONG < 64) */ |
e5227865 | 205 | |
1db4943c MD |
206 | static inline __attribute__((unused)) |
207 | void static_array_size_check(void) | |
208 | { | |
e1db2db5 | 209 | CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) < JA_TYPE_MAX_NR); |
1db4943c MD |
210 | } |
211 | ||
e5227865 | 212 | /* |
d96bfb0d | 213 | * The cds_ja_node contains the compressed node data needed for |
1db4943c MD |
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. | |
e5227865 | 222 | */ |
1db4943c | 223 | |
ff38c745 MD |
224 | #define DECLARE_LINEAR_NODE(index) \ |
225 | struct { \ | |
226 | uint8_t nr_child; \ | |
227 | uint8_t child_value[ja_type_## index ##_max_linear_child]; \ | |
b4540e8a | 228 | struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \ |
ff38c745 MD |
229 | } |
230 | ||
231 | #define DECLARE_POOL_NODE(index) \ | |
232 | struct { \ | |
233 | struct { \ | |
234 | uint8_t nr_child; \ | |
235 | uint8_t child_value[ja_type_## index ##_max_linear_child]; \ | |
b4540e8a | 236 | struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \ |
ff38c745 MD |
237 | } linear[1U << ja_type_## index ##_nr_pool_order]; \ |
238 | } | |
1db4943c | 239 | |
b4540e8a | 240 | struct cds_ja_inode { |
1db4943c MD |
241 | union { |
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; | |
248 | ||
249 | /* Pool configuration */ | |
250 | DECLARE_POOL_NODE(5) conf_5; | |
251 | DECLARE_POOL_NODE(6) conf_6; | |
252 | ||
253 | /* Pigeon configuration */ | |
254 | struct { | |
b4540e8a | 255 | struct cds_ja_inode_flag *child[ja_type_7_max_child]; |
1db4943c MD |
256 | } conf_7; |
257 | /* data aliasing nodes for computed accesses */ | |
b4540e8a | 258 | uint8_t data[sizeof(struct cds_ja_inode_flag *) * ja_type_7_max_child]; |
1db4943c | 259 | } u; |
e5227865 MD |
260 | }; |
261 | ||
d68c6810 | 262 | static |
b4540e8a | 263 | struct cds_ja_inode_flag *ja_node_flag(struct cds_ja_inode *node, |
a2a7ff59 | 264 | unsigned long type) |
d68c6810 | 265 | { |
a2a7ff59 | 266 | assert(type < (1UL << JA_TYPE_BITS)); |
b4540e8a | 267 | return (struct cds_ja_inode_flag *) (((unsigned long) node) | type); |
d68c6810 MD |
268 | } |
269 | ||
e1db2db5 | 270 | static |
b4540e8a | 271 | struct cds_ja_inode *ja_node_ptr(struct cds_ja_inode_flag *node) |
e1db2db5 | 272 | { |
a2a7ff59 | 273 | return (struct cds_ja_inode *) (((unsigned long) node) & JA_PTR_MASK); |
e1db2db5 MD |
274 | } |
275 | ||
d68c6810 | 276 | static |
a2a7ff59 | 277 | unsigned long ja_node_type(struct cds_ja_inode_flag *node) |
d68c6810 | 278 | { |
a2a7ff59 | 279 | unsigned long type; |
d68c6810 | 280 | |
e1db2db5 MD |
281 | if (ja_node_ptr(node) == NULL) { |
282 | return NODE_INDEX_NULL; | |
283 | } | |
d68c6810 | 284 | type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK); |
a2a7ff59 | 285 | assert(type < (1UL << JA_TYPE_BITS)); |
d68c6810 MD |
286 | return type; |
287 | } | |
288 | ||
b4540e8a | 289 | struct cds_ja_inode *alloc_cds_ja_node(const struct cds_ja_type *ja_type) |
e5227865 | 290 | { |
1db4943c | 291 | return calloc(1U << ja_type->order, sizeof(char)); |
e5227865 MD |
292 | } |
293 | ||
b4540e8a | 294 | void free_cds_ja_node(struct cds_ja_inode *node) |
e5227865 MD |
295 | { |
296 | free(node); | |
297 | } | |
298 | ||
d68c6810 MD |
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) | |
303 | ||
304 | static | |
1db4943c | 305 | uint8_t *align_ptr_size(uint8_t *ptr) |
d68c6810 | 306 | { |
1db4943c | 307 | return (uint8_t *) JA_ALIGN((unsigned long) ptr, sizeof(void *)); |
d68c6810 MD |
308 | } |
309 | ||
11c5e016 | 310 | static |
d96bfb0d | 311 | uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type *type, |
b4540e8a | 312 | struct cds_ja_inode *node) |
11c5e016 MD |
313 | { |
314 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
315 | return CMM_LOAD_SHARED(node->u.data[0]); | |
316 | } | |
317 | ||
13a7f5a6 MD |
318 | /* |
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. | |
322 | */ | |
d68c6810 | 323 | static |
b4540e8a MD |
324 | struct cds_ja_inode_flag *ja_linear_node_get_nth(const struct cds_ja_type *type, |
325 | struct cds_ja_inode *node, | |
5a9a87dd | 326 | struct cds_ja_inode_flag ***child_node_flag_ptr, |
8e519e3c | 327 | uint8_t n) |
d68c6810 MD |
328 | { |
329 | uint8_t nr_child; | |
330 | uint8_t *values; | |
b4540e8a MD |
331 | struct cds_ja_inode_flag **pointers; |
332 | struct cds_ja_inode_flag *ptr; | |
d68c6810 MD |
333 | unsigned int i; |
334 | ||
8e519e3c | 335 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); |
d68c6810 | 336 | |
11c5e016 | 337 | nr_child = ja_linear_node_get_nr_child(type, node); |
13a7f5a6 | 338 | cmm_smp_rmb(); /* read nr_child before values and pointers */ |
8e519e3c MD |
339 | assert(nr_child <= type->max_linear_child); |
340 | assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child); | |
d68c6810 | 341 | |
1db4943c | 342 | values = &node->u.data[1]; |
d68c6810 | 343 | for (i = 0; i < nr_child; i++) { |
13a7f5a6 | 344 | if (CMM_LOAD_SHARED(values[i]) == n) |
d68c6810 MD |
345 | break; |
346 | } | |
347 | if (i >= nr_child) | |
348 | return NULL; | |
b4540e8a | 349 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); |
5a9a87dd MD |
350 | if (caa_unlikely(child_node_flag_ptr)) |
351 | *child_node_flag_ptr = &pointers[i]; | |
13a7f5a6 | 352 | ptr = rcu_dereference(pointers[i]); |
d68c6810 MD |
353 | assert(ja_node_ptr(ptr) != NULL); |
354 | return ptr; | |
355 | } | |
356 | ||
11c5e016 | 357 | static |
5a9a87dd | 358 | void ja_linear_node_get_ith_pos(const struct cds_ja_type *type, |
b4540e8a | 359 | struct cds_ja_inode *node, |
11c5e016 MD |
360 | uint8_t i, |
361 | uint8_t *v, | |
b4540e8a | 362 | struct cds_ja_inode_flag **iter) |
11c5e016 MD |
363 | { |
364 | uint8_t *values; | |
b4540e8a | 365 | struct cds_ja_inode_flag **pointers; |
11c5e016 MD |
366 | |
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)); | |
369 | ||
370 | values = &node->u.data[1]; | |
371 | *v = values[i]; | |
b4540e8a | 372 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); |
11c5e016 MD |
373 | *iter = pointers[i]; |
374 | } | |
375 | ||
d68c6810 | 376 | static |
b4540e8a MD |
377 | struct cds_ja_inode_flag *ja_pool_node_get_nth(const struct cds_ja_type *type, |
378 | struct cds_ja_inode *node, | |
5a9a87dd | 379 | struct cds_ja_inode_flag ***child_node_flag_ptr, |
8e519e3c | 380 | uint8_t n) |
d68c6810 | 381 | { |
b4540e8a | 382 | struct cds_ja_inode *linear; |
d68c6810 | 383 | |
fd800776 | 384 | assert(type->type_class == RCU_JA_POOL); |
e1db2db5 MD |
385 | /* |
386 | * TODO: currently, we select the pool by highest bits. We | |
387 | * should support various encodings. | |
388 | */ | |
b4540e8a | 389 | linear = (struct cds_ja_inode *) |
1db4943c | 390 | &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order]; |
5a9a87dd | 391 | return ja_linear_node_get_nth(type, linear, child_node_flag_ptr, n); |
d68c6810 MD |
392 | } |
393 | ||
11c5e016 | 394 | static |
b4540e8a MD |
395 | struct cds_ja_inode *ja_pool_node_get_ith_pool(const struct cds_ja_type *type, |
396 | struct cds_ja_inode *node, | |
11c5e016 MD |
397 | uint8_t i) |
398 | { | |
399 | assert(type->type_class == RCU_JA_POOL); | |
b4540e8a | 400 | return (struct cds_ja_inode *) |
11c5e016 MD |
401 | &node->u.data[(unsigned int) i << type->pool_size_order]; |
402 | } | |
403 | ||
d68c6810 | 404 | static |
b4540e8a MD |
405 | struct cds_ja_inode_flag *ja_pigeon_node_get_nth(const struct cds_ja_type *type, |
406 | struct cds_ja_inode *node, | |
5a9a87dd | 407 | struct cds_ja_inode_flag ***child_node_flag_ptr, |
8e519e3c | 408 | uint8_t n) |
d68c6810 | 409 | { |
5a9a87dd MD |
410 | struct cds_ja_inode_flag **child_node_flag; |
411 | ||
d68c6810 | 412 | assert(type->type_class == RCU_JA_PIGEON); |
5a9a87dd MD |
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); | |
d68c6810 MD |
417 | } |
418 | ||
13a7f5a6 MD |
419 | /* |
420 | * ja_node_get_nth: get nth item from a node. | |
421 | * node_flag is already rcu_dereference'd. | |
422 | */ | |
d68c6810 | 423 | static |
41975c12 | 424 | struct cds_ja_inode_flag * ja_node_get_nth(struct cds_ja_inode_flag *node_flag, |
5a9a87dd | 425 | struct cds_ja_inode_flag ***child_node_flag_ptr, |
8e519e3c | 426 | uint8_t n) |
d68c6810 MD |
427 | { |
428 | unsigned int type_index; | |
b4540e8a | 429 | struct cds_ja_inode *node; |
d96bfb0d | 430 | const struct cds_ja_type *type; |
d68c6810 | 431 | |
d68c6810 | 432 | node = ja_node_ptr(node_flag); |
5a9a87dd | 433 | assert(node != NULL); |
d68c6810 MD |
434 | type_index = ja_node_type(node_flag); |
435 | type = &ja_types[type_index]; | |
436 | ||
437 | switch (type->type_class) { | |
438 | case RCU_JA_LINEAR: | |
5a9a87dd MD |
439 | return ja_linear_node_get_nth(type, node, |
440 | child_node_flag_ptr, n); | |
fd800776 | 441 | case RCU_JA_POOL: |
5a9a87dd MD |
442 | return ja_pool_node_get_nth(type, node, |
443 | child_node_flag_ptr, n); | |
d68c6810 | 444 | case RCU_JA_PIGEON: |
5a9a87dd MD |
445 | return ja_pigeon_node_get_nth(type, node, |
446 | child_node_flag_ptr, n); | |
d68c6810 MD |
447 | default: |
448 | assert(0); | |
449 | return (void *) -1UL; | |
450 | } | |
451 | } | |
452 | ||
335d8b18 MD |
453 | /* |
454 | * TODO: use ja_get_nr_child to monitor limits triggering shrink | |
455 | * recompaction. | |
456 | * Also use ja_get_nr_child to make the difference between resize and | |
457 | * pool change of compaction bit(s). | |
458 | */ | |
e1db2db5 | 459 | static |
d96bfb0d | 460 | unsigned int ja_get_nr_child(struct cds_ja_shadow_node *shadow_node) |
e1db2db5 MD |
461 | { |
462 | return shadow_node->nr_child; | |
463 | } | |
464 | ||
8e519e3c | 465 | static |
d96bfb0d | 466 | int ja_linear_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 467 | struct cds_ja_inode *node, |
d96bfb0d | 468 | struct cds_ja_shadow_node *shadow_node, |
8e519e3c | 469 | uint8_t n, |
b4540e8a | 470 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c MD |
471 | { |
472 | uint8_t nr_child; | |
473 | uint8_t *values, *nr_child_ptr; | |
b4540e8a | 474 | struct cds_ja_inode_flag **pointers; |
8e519e3c MD |
475 | unsigned int i; |
476 | ||
477 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
478 | ||
479 | nr_child_ptr = &node->u.data[0]; | |
a2a7ff59 | 480 | dbg_printf("linear set nth: nr_child_ptr %p\n", nr_child_ptr); |
8e519e3c MD |
481 | nr_child = *nr_child_ptr; |
482 | assert(nr_child <= type->max_linear_child); | |
8e519e3c MD |
483 | |
484 | values = &node->u.data[1]; | |
485 | for (i = 0; i < nr_child; i++) { | |
486 | if (values[i] == n) | |
487 | return -EEXIST; | |
488 | } | |
489 | if (nr_child >= type->max_linear_child) { | |
490 | /* No space left in this node type */ | |
491 | return -ENOSPC; | |
492 | } | |
b4540e8a | 493 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); |
13a7f5a6 MD |
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); | |
e1db2db5 | 499 | shadow_node->nr_child++; |
a2a7ff59 MD |
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, | |
503 | node, shadow_node); | |
504 | ||
8e519e3c MD |
505 | return 0; |
506 | } | |
507 | ||
508 | static | |
d96bfb0d | 509 | int ja_pool_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 510 | struct cds_ja_inode *node, |
d96bfb0d | 511 | struct cds_ja_shadow_node *shadow_node, |
8e519e3c | 512 | uint8_t n, |
b4540e8a | 513 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c | 514 | { |
b4540e8a | 515 | struct cds_ja_inode *linear; |
8e519e3c MD |
516 | |
517 | assert(type->type_class == RCU_JA_POOL); | |
b4540e8a | 518 | linear = (struct cds_ja_inode *) |
8e519e3c | 519 | &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order]; |
e1db2db5 MD |
520 | return ja_linear_node_set_nth(type, linear, shadow_node, |
521 | n, child_node_flag); | |
8e519e3c MD |
522 | } |
523 | ||
524 | static | |
d96bfb0d | 525 | int ja_pigeon_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 526 | struct cds_ja_inode *node, |
d96bfb0d | 527 | struct cds_ja_shadow_node *shadow_node, |
8e519e3c | 528 | uint8_t n, |
b4540e8a | 529 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c | 530 | { |
b4540e8a | 531 | struct cds_ja_inode_flag **ptr; |
8e519e3c MD |
532 | |
533 | assert(type->type_class == RCU_JA_PIGEON); | |
b4540e8a | 534 | ptr = &((struct cds_ja_inode_flag **) node->u.data)[n]; |
5a9a87dd | 535 | if (*ptr) |
8e519e3c MD |
536 | return -EEXIST; |
537 | rcu_assign_pointer(*ptr, child_node_flag); | |
e1db2db5 | 538 | shadow_node->nr_child++; |
8e519e3c MD |
539 | return 0; |
540 | } | |
541 | ||
d68c6810 | 542 | /* |
7a0b2331 | 543 | * _ja_node_set_nth: set nth item within a node. Return an error |
8e519e3c MD |
544 | * (negative error value) if it is already there. |
545 | * TODO: exclusive access on node. | |
d68c6810 | 546 | */ |
8e519e3c | 547 | static |
d96bfb0d | 548 | int _ja_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 549 | struct cds_ja_inode *node, |
d96bfb0d | 550 | struct cds_ja_shadow_node *shadow_node, |
e1db2db5 | 551 | uint8_t n, |
b4540e8a | 552 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c | 553 | { |
8e519e3c MD |
554 | switch (type->type_class) { |
555 | case RCU_JA_LINEAR: | |
e1db2db5 | 556 | return ja_linear_node_set_nth(type, node, shadow_node, n, |
8e519e3c MD |
557 | child_node_flag); |
558 | case RCU_JA_POOL: | |
e1db2db5 | 559 | return ja_pool_node_set_nth(type, node, shadow_node, n, |
8e519e3c MD |
560 | child_node_flag); |
561 | case RCU_JA_PIGEON: | |
e1db2db5 | 562 | return ja_pigeon_node_set_nth(type, node, shadow_node, n, |
8e519e3c | 563 | child_node_flag); |
e1db2db5 MD |
564 | case RCU_JA_NULL: |
565 | return -ENOSPC; | |
8e519e3c MD |
566 | default: |
567 | assert(0); | |
568 | return -EINVAL; | |
569 | } | |
570 | ||
571 | return 0; | |
572 | } | |
7a0b2331 MD |
573 | |
574 | /* | |
575 | * ja_node_recompact_add: recompact a node, adding a new child. | |
e1db2db5 | 576 | * TODO: for pool type, take selection bit(s) into account. |
5a9a87dd MD |
577 | * Return 0 on success, -ENOENT if need to retry, or other negative |
578 | * error value otherwise. | |
7a0b2331 MD |
579 | */ |
580 | static | |
d96bfb0d | 581 | int ja_node_recompact_add(struct cds_ja *ja, |
e1db2db5 | 582 | unsigned int old_type_index, |
d96bfb0d | 583 | const struct cds_ja_type *old_type, |
b4540e8a | 584 | struct cds_ja_inode *old_node, |
5a9a87dd | 585 | struct cds_ja_shadow_node *shadow_node, |
b4540e8a MD |
586 | struct cds_ja_inode_flag **old_node_flag, uint8_t n, |
587 | struct cds_ja_inode_flag *child_node_flag) | |
7a0b2331 | 588 | { |
e1db2db5 | 589 | unsigned int new_type_index; |
b4540e8a | 590 | struct cds_ja_inode *new_node; |
f07b240f | 591 | struct cds_ja_shadow_node *new_shadow_node; |
d96bfb0d | 592 | const struct cds_ja_type *new_type; |
b4540e8a | 593 | struct cds_ja_inode_flag *new_node_flag; |
7a0b2331 | 594 | int ret; |
f07b240f | 595 | int fallback = 0; |
7a0b2331 | 596 | |
a2a7ff59 | 597 | if (!shadow_node || old_type_index == NODE_INDEX_NULL) { |
7a0b2331 MD |
598 | new_type_index = 0; |
599 | } else { | |
7a0b2331 MD |
600 | new_type_index = old_type_index + 1; |
601 | } | |
a2a7ff59 | 602 | |
f07b240f MD |
603 | retry: /* for fallback */ |
604 | dbg_printf("Recompact to type %d\n", new_type_index); | |
7a0b2331 | 605 | new_type = &ja_types[new_type_index]; |
d96bfb0d | 606 | new_node = alloc_cds_ja_node(new_type); |
7a0b2331 MD |
607 | if (!new_node) |
608 | return -ENOMEM; | |
609 | new_node_flag = ja_node_flag(new_node, new_type_index); | |
610 | ||
a2a7ff59 | 611 | dbg_printf("Recompact inherit lock from %p\n", shadow_node); |
f07b240f MD |
612 | new_shadow_node = rcuja_shadow_set(ja->ht, new_node, shadow_node); |
613 | if (!new_shadow_node) { | |
5a9a87dd | 614 | free(new_node); |
f07b240f | 615 | return -ENOMEM; |
e1db2db5 | 616 | } |
f07b240f MD |
617 | if (fallback) |
618 | new_shadow_node->fallback_removal_count = | |
619 | JA_FALLBACK_REMOVAL_COUNT; | |
11c5e016 MD |
620 | |
621 | assert(old_type->type_class != RCU_JA_PIGEON); | |
622 | switch (old_type->type_class) { | |
623 | case RCU_JA_LINEAR: | |
624 | { | |
625 | uint8_t nr_child = | |
626 | ja_linear_node_get_nr_child(old_type, old_node); | |
627 | unsigned int i; | |
628 | ||
629 | for (i = 0; i < nr_child; i++) { | |
b4540e8a | 630 | struct cds_ja_inode_flag *iter; |
11c5e016 MD |
631 | uint8_t v; |
632 | ||
633 | ja_linear_node_get_ith_pos(old_type, old_node, i, &v, &iter); | |
634 | if (!iter) | |
635 | continue; | |
f07b240f MD |
636 | ret = _ja_node_set_nth(new_type, new_node, |
637 | new_shadow_node, | |
11c5e016 | 638 | v, iter); |
f07b240f MD |
639 | if (new_type->type_class == RCU_JA_POOL && ret) { |
640 | goto fallback_toosmall; | |
641 | } | |
11c5e016 MD |
642 | assert(!ret); |
643 | } | |
644 | break; | |
645 | } | |
646 | case RCU_JA_POOL: | |
647 | { | |
648 | unsigned int pool_nr; | |
649 | ||
650 | for (pool_nr = 0; pool_nr < (1U << old_type->nr_pool_order); pool_nr++) { | |
b4540e8a | 651 | struct cds_ja_inode *pool = |
11c5e016 MD |
652 | ja_pool_node_get_ith_pool(old_type, |
653 | old_node, pool_nr); | |
654 | uint8_t nr_child = | |
655 | ja_linear_node_get_nr_child(old_type, pool); | |
656 | unsigned int j; | |
657 | ||
658 | for (j = 0; j < nr_child; j++) { | |
b4540e8a | 659 | struct cds_ja_inode_flag *iter; |
11c5e016 MD |
660 | uint8_t v; |
661 | ||
662 | ja_linear_node_get_ith_pos(old_type, pool, | |
663 | j, &v, &iter); | |
664 | if (!iter) | |
665 | continue; | |
f07b240f MD |
666 | ret = _ja_node_set_nth(new_type, new_node, |
667 | new_shadow_node, | |
11c5e016 | 668 | v, iter); |
f07b240f MD |
669 | if (new_type->type_class == RCU_JA_POOL |
670 | && ret) { | |
671 | goto fallback_toosmall; | |
672 | } | |
11c5e016 MD |
673 | assert(!ret); |
674 | } | |
675 | } | |
676 | break; | |
7a0b2331 | 677 | } |
a2a7ff59 MD |
678 | case RCU_JA_NULL: |
679 | /* Nothing to copy */ | |
680 | break; | |
11c5e016 MD |
681 | case RCU_JA_PIGEON: |
682 | default: | |
683 | assert(0); | |
5a9a87dd | 684 | ret = -EINVAL; |
f07b240f | 685 | goto end; |
11c5e016 MD |
686 | } |
687 | ||
7a0b2331 | 688 | /* add node */ |
f07b240f MD |
689 | ret = _ja_node_set_nth(new_type, new_node, |
690 | new_shadow_node, | |
e1db2db5 | 691 | n, child_node_flag); |
7a0b2331 | 692 | assert(!ret); |
5a9a87dd MD |
693 | /* Return pointer to new recompacted new through old_node_flag */ |
694 | *old_node_flag = new_node_flag; | |
a2a7ff59 MD |
695 | if (old_node) { |
696 | ret = rcuja_shadow_clear(ja->ht, old_node, shadow_node, | |
697 | RCUJA_SHADOW_CLEAR_FREE_NODE); | |
698 | assert(!ret); | |
699 | } | |
5a9a87dd MD |
700 | |
701 | ret = 0; | |
f07b240f | 702 | end: |
5a9a87dd | 703 | return ret; |
f07b240f MD |
704 | |
705 | fallback_toosmall: | |
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); | |
709 | assert(!ret); | |
710 | ||
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); | |
715 | fallback = 1; | |
716 | goto retry; | |
7a0b2331 MD |
717 | } |
718 | ||
5a9a87dd MD |
719 | /* |
720 | * Return 0 on success, -ENOENT if need to retry, or other negative | |
721 | * error value otherwise. | |
722 | */ | |
7a0b2331 | 723 | static |
d96bfb0d | 724 | int ja_node_set_nth(struct cds_ja *ja, |
b4540e8a | 725 | struct cds_ja_inode_flag **node_flag, uint8_t n, |
5a9a87dd MD |
726 | struct cds_ja_inode_flag *child_node_flag, |
727 | struct cds_ja_shadow_node *shadow_node) | |
7a0b2331 MD |
728 | { |
729 | int ret; | |
e1db2db5 | 730 | unsigned int type_index; |
d96bfb0d | 731 | const struct cds_ja_type *type; |
b4540e8a | 732 | struct cds_ja_inode *node; |
7a0b2331 | 733 | |
a2a7ff59 MD |
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); | |
736 | ||
e1db2db5 MD |
737 | node = ja_node_ptr(*node_flag); |
738 | type_index = ja_node_type(*node_flag); | |
739 | type = &ja_types[type_index]; | |
e1db2db5 MD |
740 | ret = _ja_node_set_nth(type, node, shadow_node, |
741 | n, child_node_flag); | |
7a0b2331 | 742 | if (ret == -ENOSPC) { |
e1db2db5 MD |
743 | /* Not enough space in node, need to recompact. */ |
744 | ret = ja_node_recompact_add(ja, type_index, type, node, | |
5a9a87dd | 745 | shadow_node, node_flag, n, child_node_flag); |
7a0b2331 MD |
746 | } |
747 | return ret; | |
748 | } | |
be9a7474 | 749 | |
5a9a87dd | 750 | struct cds_hlist_head *cds_ja_lookup(struct cds_ja *ja, uint64_t key) |
b4540e8a | 751 | { |
41975c12 MD |
752 | unsigned int tree_depth, i; |
753 | struct cds_ja_inode_flag *node_flag; | |
754 | ||
755 | if (caa_unlikely(key > ja->key_max)) | |
756 | return NULL; | |
757 | tree_depth = ja->tree_depth; | |
5a9a87dd | 758 | node_flag = rcu_dereference(ja->root); |
41975c12 | 759 | |
5a9a87dd MD |
760 | /* level 0: root node */ |
761 | if (!ja_node_ptr(node_flag)) | |
762 | return NULL; | |
763 | ||
764 | for (i = 1; i < tree_depth; i++) { | |
79b41067 MD |
765 | uint8_t iter_key; |
766 | ||
767 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1))); | |
5a9a87dd | 768 | node_flag = ja_node_get_nth(node_flag, NULL, |
79b41067 | 769 | iter_key); |
41975c12 MD |
770 | if (!ja_node_ptr(node_flag)) |
771 | return NULL; | |
41975c12 MD |
772 | } |
773 | ||
5a9a87dd MD |
774 | /* Last level lookup succeded. We got an actual match. */ |
775 | return (struct cds_hlist_head *) node_flag; | |
776 | } | |
777 | ||
778 | /* | |
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). | |
784 | * | |
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. | |
789 | */ | |
790 | static | |
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, | |
795 | uint64_t key, | |
79b41067 | 796 | unsigned int level, |
5a9a87dd MD |
797 | struct cds_ja_node *child_node) |
798 | { | |
799 | struct cds_ja_shadow_node *shadow_node = NULL, | |
f07b240f MD |
800 | *parent_shadow_node = NULL, |
801 | *iter_shadow_node; | |
5a9a87dd MD |
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; | |
806 | int ret, i; | |
a2a7ff59 | 807 | struct cds_ja_inode_flag *created_nodes[JA_MAX_DEPTH]; |
5a9a87dd MD |
808 | int nr_created_nodes = 0; |
809 | ||
79b41067 | 810 | dbg_printf("Attach node at level %u\n", level); |
a2a7ff59 | 811 | |
5a9a87dd MD |
812 | assert(node); |
813 | shadow_node = rcuja_shadow_lookup_lock(ja->ht, node); | |
814 | if (!shadow_node) { | |
815 | ret = -ENOENT; | |
816 | goto end; | |
817 | } | |
818 | if (parent_node) { | |
819 | parent_shadow_node = rcuja_shadow_lookup_lock(ja->ht, | |
820 | parent_node); | |
821 | if (!parent_shadow_node) { | |
822 | ret = -ENOENT; | |
823 | goto unlock_shadow; | |
824 | } | |
825 | } | |
826 | ||
a2a7ff59 | 827 | /* Create new branch, starting from bottom */ |
5a9a87dd MD |
828 | CDS_INIT_HLIST_HEAD(&head); |
829 | cds_hlist_add_head_rcu(&child_node->list, &head); | |
a2a7ff59 | 830 | iter_node_flag = (struct cds_ja_inode_flag *) head.next; |
5a9a87dd | 831 | |
a2a7ff59 MD |
832 | /* Create shadow node for the leaf node */ |
833 | dbg_printf("leaf shadow node creation\n"); | |
f07b240f MD |
834 | iter_shadow_node = rcuja_shadow_set(ja->ht, |
835 | ja_node_ptr(iter_node_flag), NULL); | |
836 | if (!iter_shadow_node) { | |
837 | ret = -ENOMEM; | |
a2a7ff59 | 838 | goto check_error; |
f07b240f | 839 | } |
a2a7ff59 | 840 | created_nodes[nr_created_nodes++] = iter_node_flag; |
5a9a87dd | 841 | |
79b41067 MD |
842 | for (i = ja->tree_depth; i > (int) level; i--) { |
843 | uint8_t iter_key; | |
844 | ||
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); | |
5a9a87dd MD |
848 | iter_dest_node_flag = NULL; |
849 | ret = ja_node_set_nth(ja, &iter_dest_node_flag, | |
79b41067 | 850 | iter_key, |
5a9a87dd MD |
851 | iter_node_flag, |
852 | NULL); | |
853 | if (ret) | |
854 | goto check_error; | |
855 | created_nodes[nr_created_nodes++] = iter_dest_node_flag; | |
856 | iter_node_flag = iter_dest_node_flag; | |
857 | } | |
858 | ||
79b41067 MD |
859 | if (level > 1) { |
860 | uint8_t iter_key; | |
861 | ||
862 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (ja->tree_depth - level))); | |
a2a7ff59 MD |
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, | |
79b41067 | 866 | iter_key, |
a2a7ff59 MD |
867 | iter_node_flag, |
868 | shadow_node); | |
869 | if (ret) | |
870 | goto check_error; | |
871 | created_nodes[nr_created_nodes++] = iter_dest_node_flag; | |
872 | iter_node_flag = iter_dest_node_flag; | |
873 | } | |
874 | ||
5a9a87dd | 875 | /* Publish new branch */ |
a2a7ff59 MD |
876 | dbg_printf("Publish branch %p, replacing %p\n", |
877 | iter_node_flag, *node_flag_ptr); | |
5a9a87dd MD |
878 | rcu_assign_pointer(*node_flag_ptr, iter_node_flag); |
879 | ||
880 | /* Success */ | |
881 | ret = 0; | |
882 | ||
883 | check_error: | |
884 | if (ret) { | |
885 | for (i = 0; i < nr_created_nodes; i++) { | |
886 | int tmpret; | |
a2a7ff59 MD |
887 | int flags; |
888 | ||
889 | flags = RCUJA_SHADOW_CLEAR_FREE_LOCK; | |
890 | if (i) | |
891 | flags |= RCUJA_SHADOW_CLEAR_FREE_NODE; | |
5a9a87dd MD |
892 | tmpret = rcuja_shadow_clear(ja->ht, |
893 | ja_node_ptr(created_nodes[i]), | |
a2a7ff59 MD |
894 | NULL, |
895 | flags); | |
5a9a87dd MD |
896 | assert(!tmpret); |
897 | } | |
898 | } | |
5a9a87dd MD |
899 | if (parent_shadow_node) |
900 | rcuja_shadow_unlock(parent_shadow_node); | |
901 | unlock_shadow: | |
902 | if (shadow_node) | |
903 | rcuja_shadow_unlock(shadow_node); | |
904 | end: | |
905 | return ret; | |
906 | } | |
907 | ||
908 | /* | |
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. | |
913 | */ | |
914 | static | |
915 | int ja_chain_node(struct cds_ja *ja, | |
916 | struct cds_hlist_head *head, | |
917 | struct cds_ja_node *node) | |
918 | { | |
919 | struct cds_ja_shadow_node *shadow_node; | |
920 | ||
921 | shadow_node = rcuja_shadow_lookup_lock(ja->ht, | |
922 | (struct cds_ja_inode *) head); | |
923 | if (!shadow_node) | |
924 | return -ENOENT; | |
925 | cds_hlist_add_head_rcu(&node->list, head); | |
926 | rcuja_shadow_unlock(shadow_node); | |
927 | return 0; | |
928 | } | |
929 | ||
930 | int cds_ja_add(struct cds_ja *ja, uint64_t key, | |
931 | struct cds_ja_node *new_node) | |
932 | { | |
933 | unsigned int tree_depth, i; | |
5a9a87dd MD |
934 | struct cds_ja_inode_flag **node_flag_ptr; /* in parent */ |
935 | struct cds_ja_inode_flag *node_flag, | |
936 | *parent_node_flag, | |
937 | *parent2_node_flag; | |
938 | int ret; | |
939 | ||
940 | if (caa_unlikely(key > ja->key_max)) | |
941 | return -EINVAL; | |
942 | tree_depth = ja->tree_depth; | |
943 | ||
944 | retry: | |
a2a7ff59 MD |
945 | dbg_printf("cds_ja_add attempt: key %" PRIu64 ", node %p\n", |
946 | key, new_node); | |
5a9a87dd | 947 | parent2_node_flag = NULL; |
b0f74e47 MD |
948 | parent_node_flag = |
949 | (struct cds_ja_inode_flag *) &ja->root; /* Use root ptr address as key for mutex */ | |
5a9a87dd MD |
950 | node_flag_ptr = &ja->root; |
951 | node_flag = rcu_dereference(*node_flag_ptr); | |
952 | ||
953 | /* Iterate on all internal levels */ | |
a2a7ff59 | 954 | for (i = 1; i < tree_depth; i++) { |
79b41067 MD |
955 | uint8_t iter_key; |
956 | ||
5a9a87dd MD |
957 | if (!ja_node_ptr(node_flag)) { |
958 | ret = ja_attach_node(ja, node_flag_ptr, | |
959 | parent_node_flag, parent2_node_flag, | |
960 | key, i, new_node); | |
961 | if (ret == -ENOENT || ret == -EEXIST) | |
962 | goto retry; | |
963 | else | |
964 | goto end; | |
965 | } | |
79b41067 | 966 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1))); |
5a9a87dd MD |
967 | parent2_node_flag = parent_node_flag; |
968 | parent_node_flag = node_flag; | |
969 | node_flag = ja_node_get_nth(node_flag, | |
970 | &node_flag_ptr, | |
79b41067 | 971 | iter_key); |
5a9a87dd MD |
972 | } |
973 | ||
974 | /* | |
975 | * We reached bottom of tree, simply add node to last internal | |
976 | * level, or chain it if key is already present. | |
977 | */ | |
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); | |
981 | } else { | |
982 | ret = ja_chain_node(ja, | |
983 | (struct cds_hlist_head *) ja_node_ptr(node_flag), | |
984 | new_node); | |
985 | } | |
986 | if (ret == -ENOENT) | |
987 | goto retry; | |
988 | end: | |
989 | return ret; | |
b4540e8a MD |
990 | } |
991 | ||
992 | struct cds_ja *_cds_ja_new(unsigned int key_bits, | |
993 | const struct rcu_flavor_struct *flavor) | |
be9a7474 MD |
994 | { |
995 | struct cds_ja *ja; | |
b0f74e47 | 996 | int ret; |
f07b240f | 997 | struct cds_ja_shadow_node *root_shadow_node; |
be9a7474 MD |
998 | |
999 | ja = calloc(sizeof(*ja), 1); | |
1000 | if (!ja) | |
1001 | goto ja_error; | |
b4540e8a MD |
1002 | |
1003 | switch (key_bits) { | |
1004 | case 8: | |
1005 | ja->key_max = UINT8_MAX; | |
1006 | break; | |
1007 | case 16: | |
1008 | ja->key_max = UINT16_MAX; | |
1009 | break; | |
1010 | case 32: | |
1011 | ja->key_max = UINT32_MAX; | |
1012 | break; | |
1013 | case 64: | |
1014 | ja->key_max = UINT64_MAX; | |
1015 | break; | |
1016 | default: | |
1017 | goto check_error; | |
1018 | } | |
1019 | ||
be9a7474 | 1020 | /* ja->root is NULL */ |
5a9a87dd MD |
1021 | /* tree_depth 0 is for pointer to root node */ |
1022 | ja->tree_depth = (key_bits >> JA_BITS_PER_BYTE) + 1; | |
a2a7ff59 | 1023 | assert(ja->tree_depth <= JA_MAX_DEPTH); |
be9a7474 MD |
1024 | ja->ht = rcuja_create_ht(flavor); |
1025 | if (!ja->ht) | |
1026 | goto ht_error; | |
b0f74e47 MD |
1027 | |
1028 | /* | |
1029 | * Note: we should not free this node until judy array destroy. | |
1030 | */ | |
f07b240f | 1031 | root_shadow_node = rcuja_shadow_set(ja->ht, |
b0f74e47 MD |
1032 | ja_node_ptr((struct cds_ja_inode_flag *) &ja->root), |
1033 | NULL); | |
f07b240f MD |
1034 | if (!root_shadow_node) { |
1035 | ret = -ENOMEM; | |
b0f74e47 | 1036 | goto ht_node_error; |
f07b240f | 1037 | } |
b0f74e47 | 1038 | |
be9a7474 MD |
1039 | return ja; |
1040 | ||
b0f74e47 MD |
1041 | ht_node_error: |
1042 | ret = rcuja_delete_ht(ja->ht); | |
1043 | assert(!ret); | |
be9a7474 | 1044 | ht_error: |
b4540e8a | 1045 | check_error: |
be9a7474 MD |
1046 | free(ja); |
1047 | ja_error: | |
1048 | return NULL; | |
1049 | } | |
1050 | ||
1051 | /* | |
1052 | * There should be no more concurrent add to the judy array while it is | |
1053 | * being destroyed (ensured by the caller). | |
1054 | */ | |
1055 | int cds_ja_destroy(struct cds_ja *ja) | |
1056 | { | |
b4540e8a MD |
1057 | int ret; |
1058 | ||
be9a7474 MD |
1059 | rcuja_shadow_prune(ja->ht, |
1060 | RCUJA_SHADOW_CLEAR_FREE_NODE | RCUJA_SHADOW_CLEAR_FREE_LOCK); | |
b4540e8a MD |
1061 | ret = rcuja_delete_ht(ja->ht); |
1062 | if (ret) | |
1063 | return ret; | |
f07b240f MD |
1064 | if (uatomic_read(&ja->nr_fallback)) |
1065 | fprintf(stderr, | |
1066 | "[warning] RCU Judy Array used %lu fallback node(s)\n", | |
1067 | uatomic_read(&ja->nr_fallback)); | |
b4540e8a | 1068 | free(ja); |
41975c12 | 1069 | return 0; |
be9a7474 | 1070 | } |