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