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