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 MD |
36 | #include "rcuja-internal.h" |
37 | ||
b1a90ce3 MD |
38 | #ifndef abs |
39 | #define abs_int(a) ((int) (a) > 0 ? (int) (a) : -((int) (a))) | |
40 | #endif | |
41 | ||
d96bfb0d | 42 | enum cds_ja_type_class { |
e5227865 | 43 | RCU_JA_LINEAR = 0, /* Type A */ |
fd800776 MD |
44 | /* 32-bit: 1 to 25 children, 8 to 128 bytes */ |
45 | /* 64-bit: 1 to 28 children, 16 to 256 bytes */ | |
46 | RCU_JA_POOL = 1, /* Type B */ | |
47 | /* 32-bit: 26 to 100 children, 256 to 512 bytes */ | |
48 | /* 64-bit: 29 to 112 children, 512 to 1024 bytes */ | |
e5227865 | 49 | RCU_JA_PIGEON = 2, /* Type C */ |
fd800776 MD |
50 | /* 32-bit: 101 to 256 children, 1024 bytes */ |
51 | /* 64-bit: 113 to 256 children, 2048 bytes */ | |
e5227865 | 52 | /* Leaf nodes are implicit from their height in the tree */ |
1db4943c | 53 | RCU_JA_NR_TYPES, |
e1db2db5 MD |
54 | |
55 | RCU_JA_NULL, /* not an encoded type, but keeps code regular */ | |
e5227865 MD |
56 | }; |
57 | ||
d96bfb0d MD |
58 | struct cds_ja_type { |
59 | enum cds_ja_type_class type_class; | |
8e519e3c MD |
60 | uint16_t min_child; /* minimum number of children: 1 to 256 */ |
61 | uint16_t max_child; /* maximum number of children: 1 to 256 */ | |
62 | uint16_t max_linear_child; /* per-pool max nr. children: 1 to 256 */ | |
63 | uint16_t order; /* node size is (1 << order), in bytes */ | |
fd800776 MD |
64 | uint16_t nr_pool_order; /* number of pools */ |
65 | uint16_t pool_size_order; /* pool size */ | |
e5227865 MD |
66 | }; |
67 | ||
68 | /* | |
69 | * Iteration on the array to find the right node size for the number of | |
d68c6810 | 70 | * children stops when it reaches .max_child == 256 (this is the largest |
e5227865 | 71 | * possible node size, which contains 256 children). |
d68c6810 MD |
72 | * The min_child overlaps with the previous max_child to provide an |
73 | * hysteresis loop to reallocation for patterns of cyclic add/removal | |
74 | * within the same node. | |
75 | * The node the index within the following arrays is represented on 3 | |
76 | * bits. It identifies the node type, min/max number of children, and | |
77 | * the size order. | |
3d45251f MD |
78 | * The max_child values for the RCU_JA_POOL below result from |
79 | * statistical approximation: over million populations, the max_child | |
80 | * covers between 97% and 99% of the populations generated. Therefore, a | |
81 | * fallback should exist to cover the rare extreme population unbalance | |
82 | * cases, but it will not have a major impact on speed nor space | |
83 | * consumption, since those are rare cases. | |
e5227865 | 84 | */ |
e5227865 | 85 | |
d68c6810 MD |
86 | #if (CAA_BITS_PER_LONG < 64) |
87 | /* 32-bit pointers */ | |
1db4943c MD |
88 | enum { |
89 | ja_type_0_max_child = 1, | |
90 | ja_type_1_max_child = 3, | |
91 | ja_type_2_max_child = 6, | |
92 | ja_type_3_max_child = 12, | |
93 | ja_type_4_max_child = 25, | |
94 | ja_type_5_max_child = 48, | |
95 | ja_type_6_max_child = 92, | |
96 | ja_type_7_max_child = 256, | |
e1db2db5 | 97 | ja_type_8_max_child = 0, /* NULL */ |
1db4943c MD |
98 | }; |
99 | ||
8e519e3c MD |
100 | enum { |
101 | ja_type_0_max_linear_child = 1, | |
102 | ja_type_1_max_linear_child = 3, | |
103 | ja_type_2_max_linear_child = 6, | |
104 | ja_type_3_max_linear_child = 12, | |
105 | ja_type_4_max_linear_child = 25, | |
106 | ja_type_5_max_linear_child = 24, | |
107 | ja_type_6_max_linear_child = 23, | |
108 | }; | |
109 | ||
1db4943c MD |
110 | enum { |
111 | ja_type_5_nr_pool_order = 1, | |
112 | ja_type_6_nr_pool_order = 2, | |
113 | }; | |
114 | ||
d96bfb0d | 115 | const struct cds_ja_type ja_types[] = { |
8e519e3c MD |
116 | { .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, }, |
117 | { .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, }, | |
118 | { .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, }, | |
119 | { .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, }, | |
120 | { .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 | 121 | |
fd800776 | 122 | /* Pools may fill sooner than max_child */ |
8e519e3c MD |
123 | { .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, }, |
124 | { .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 |
125 | |
126 | /* | |
b1a90ce3 MD |
127 | * Upon node removal below min_child, if child pool is filled |
128 | * beyond capacity, we roll back to pigeon. | |
3d45251f | 129 | */ |
58c16c03 | 130 | { .type_class = RCU_JA_PIGEON, .min_child = 83, .max_child = ja_type_7_max_child, .order = 10, }, |
e1db2db5 MD |
131 | |
132 | { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, }, | |
d68c6810 | 133 | }; |
d68c6810 MD |
134 | #else /* !(CAA_BITS_PER_LONG < 64) */ |
135 | /* 64-bit pointers */ | |
1db4943c MD |
136 | enum { |
137 | ja_type_0_max_child = 1, | |
138 | ja_type_1_max_child = 3, | |
139 | ja_type_2_max_child = 7, | |
140 | ja_type_3_max_child = 14, | |
141 | ja_type_4_max_child = 28, | |
142 | ja_type_5_max_child = 54, | |
143 | ja_type_6_max_child = 104, | |
144 | ja_type_7_max_child = 256, | |
e1db2db5 | 145 | ja_type_8_max_child = 256, |
1db4943c MD |
146 | }; |
147 | ||
8e519e3c MD |
148 | enum { |
149 | ja_type_0_max_linear_child = 1, | |
150 | ja_type_1_max_linear_child = 3, | |
151 | ja_type_2_max_linear_child = 7, | |
152 | ja_type_3_max_linear_child = 14, | |
153 | ja_type_4_max_linear_child = 28, | |
154 | ja_type_5_max_linear_child = 27, | |
155 | ja_type_6_max_linear_child = 26, | |
156 | }; | |
157 | ||
1db4943c MD |
158 | enum { |
159 | ja_type_5_nr_pool_order = 1, | |
160 | ja_type_6_nr_pool_order = 2, | |
161 | }; | |
162 | ||
d96bfb0d | 163 | const struct cds_ja_type ja_types[] = { |
8e519e3c MD |
164 | { .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, }, |
165 | { .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, }, | |
166 | { .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, }, | |
167 | { .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, }, | |
168 | { .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 | 169 | |
3d45251f | 170 | /* Pools may fill sooner than max_child. */ |
8e519e3c MD |
171 | { .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, }, |
172 | { .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 | 173 | |
3d45251f | 174 | /* |
b1a90ce3 MD |
175 | * Upon node removal below min_child, if child pool is filled |
176 | * beyond capacity, we roll back to pigeon. | |
3d45251f | 177 | */ |
64457f6c | 178 | { .type_class = RCU_JA_PIGEON, .min_child = 95, .max_child = ja_type_7_max_child, .order = 11, }, |
e1db2db5 MD |
179 | |
180 | { .type_class = RCU_JA_NULL, .min_child = 0, .max_child = ja_type_8_max_child, }, | |
e5227865 | 181 | }; |
d68c6810 | 182 | #endif /* !(BITS_PER_LONG < 64) */ |
e5227865 | 183 | |
1db4943c MD |
184 | static inline __attribute__((unused)) |
185 | void static_array_size_check(void) | |
186 | { | |
e1db2db5 | 187 | CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) < JA_TYPE_MAX_NR); |
1db4943c MD |
188 | } |
189 | ||
e5227865 | 190 | /* |
d96bfb0d | 191 | * The cds_ja_node contains the compressed node data needed for |
1db4943c MD |
192 | * read-side. For linear and pool node configurations, it starts with a |
193 | * byte counting the number of children in the node. Then, the | |
194 | * node-specific data is placed. | |
195 | * The node mutex, if any is needed, protecting concurrent updated of | |
196 | * each node is placed in a separate hash table indexed by node address. | |
197 | * For the pigeon configuration, the number of children is also kept in | |
198 | * a separate hash table, indexed by node address, because it is only | |
199 | * required for updates. | |
e5227865 | 200 | */ |
1db4943c | 201 | |
ff38c745 MD |
202 | #define DECLARE_LINEAR_NODE(index) \ |
203 | struct { \ | |
204 | uint8_t nr_child; \ | |
205 | uint8_t child_value[ja_type_## index ##_max_linear_child]; \ | |
b4540e8a | 206 | struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \ |
ff38c745 MD |
207 | } |
208 | ||
209 | #define DECLARE_POOL_NODE(index) \ | |
210 | struct { \ | |
211 | struct { \ | |
212 | uint8_t nr_child; \ | |
213 | uint8_t child_value[ja_type_## index ##_max_linear_child]; \ | |
b4540e8a | 214 | struct cds_ja_inode_flag *child_ptr[ja_type_## index ##_max_linear_child]; \ |
ff38c745 MD |
215 | } linear[1U << ja_type_## index ##_nr_pool_order]; \ |
216 | } | |
1db4943c | 217 | |
b4540e8a | 218 | struct cds_ja_inode { |
1db4943c MD |
219 | union { |
220 | /* Linear configuration */ | |
221 | DECLARE_LINEAR_NODE(0) conf_0; | |
222 | DECLARE_LINEAR_NODE(1) conf_1; | |
223 | DECLARE_LINEAR_NODE(2) conf_2; | |
224 | DECLARE_LINEAR_NODE(3) conf_3; | |
225 | DECLARE_LINEAR_NODE(4) conf_4; | |
226 | ||
227 | /* Pool configuration */ | |
228 | DECLARE_POOL_NODE(5) conf_5; | |
229 | DECLARE_POOL_NODE(6) conf_6; | |
230 | ||
231 | /* Pigeon configuration */ | |
232 | struct { | |
b4540e8a | 233 | struct cds_ja_inode_flag *child[ja_type_7_max_child]; |
1db4943c MD |
234 | } conf_7; |
235 | /* data aliasing nodes for computed accesses */ | |
b4540e8a | 236 | uint8_t data[sizeof(struct cds_ja_inode_flag *) * ja_type_7_max_child]; |
1db4943c | 237 | } u; |
e5227865 MD |
238 | }; |
239 | ||
2e313670 | 240 | enum ja_recompact { |
19ddcd04 MD |
241 | JA_RECOMPACT_ADD_SAME, |
242 | JA_RECOMPACT_ADD_NEXT, | |
2e313670 MD |
243 | JA_RECOMPACT_DEL, |
244 | }; | |
245 | ||
b1a90ce3 MD |
246 | static |
247 | struct cds_ja_inode *_ja_node_mask_ptr(struct cds_ja_inode_flag *node) | |
248 | { | |
249 | return (struct cds_ja_inode *) (((unsigned long) node) & JA_PTR_MASK); | |
250 | } | |
251 | ||
252 | unsigned long ja_node_type(struct cds_ja_inode_flag *node) | |
253 | { | |
254 | unsigned long type; | |
255 | ||
256 | if (_ja_node_mask_ptr(node) == NULL) { | |
257 | return NODE_INDEX_NULL; | |
258 | } | |
259 | type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK); | |
260 | assert(type < (1UL << JA_TYPE_BITS)); | |
261 | return type; | |
262 | } | |
263 | ||
264 | struct cds_ja_inode *ja_node_ptr(struct cds_ja_inode_flag *node) | |
265 | { | |
266 | unsigned long type_index = ja_node_type(node); | |
267 | const struct cds_ja_type *type; | |
268 | ||
269 | type = &ja_types[type_index]; | |
270 | switch (type->type_class) { | |
271 | case RCU_JA_LINEAR: | |
272 | case RCU_JA_PIGEON: /* fall-through */ | |
273 | case RCU_JA_NULL: /* fall-through */ | |
274 | default: /* fall-through */ | |
275 | return _ja_node_mask_ptr(node); | |
276 | case RCU_JA_POOL: | |
277 | switch (type->nr_pool_order) { | |
278 | case 1: | |
279 | return (struct cds_ja_inode *) (((unsigned long) node) & ~(JA_POOL_1D_MASK | JA_TYPE_MASK)); | |
280 | case 2: | |
281 | return (struct cds_ja_inode *) (((unsigned long) node) & ~(JA_POOL_2D_MASK | JA_POOL_1D_MASK | JA_TYPE_MASK)); | |
282 | default: | |
283 | assert(0); | |
284 | } | |
285 | } | |
286 | } | |
287 | ||
354981c2 MD |
288 | static |
289 | struct cds_ja_inode *alloc_cds_ja_node(struct cds_ja *ja, | |
290 | const struct cds_ja_type *ja_type) | |
e5227865 | 291 | { |
b1a90ce3 MD |
292 | size_t len = 1U << ja_type->order; |
293 | void *p; | |
294 | int ret; | |
295 | ||
296 | ret = posix_memalign(&p, len, len); | |
297 | if (ret || !p) { | |
298 | return NULL; | |
299 | } | |
300 | memset(p, 0, len); | |
354981c2 | 301 | uatomic_inc(&ja->nr_nodes_allocated); |
b1a90ce3 | 302 | return p; |
e5227865 MD |
303 | } |
304 | ||
354981c2 | 305 | void free_cds_ja_node(struct cds_ja *ja, struct cds_ja_inode *node) |
e5227865 MD |
306 | { |
307 | free(node); | |
48cbe001 | 308 | if (node) |
354981c2 | 309 | uatomic_inc(&ja->nr_nodes_freed); |
e5227865 MD |
310 | } |
311 | ||
d68c6810 MD |
312 | #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask)) |
313 | #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1) | |
314 | #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask)) | |
315 | #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1) | |
316 | ||
317 | static | |
1db4943c | 318 | uint8_t *align_ptr_size(uint8_t *ptr) |
d68c6810 | 319 | { |
1db4943c | 320 | return (uint8_t *) JA_ALIGN((unsigned long) ptr, sizeof(void *)); |
d68c6810 MD |
321 | } |
322 | ||
11c5e016 | 323 | static |
d96bfb0d | 324 | uint8_t ja_linear_node_get_nr_child(const struct cds_ja_type *type, |
b4540e8a | 325 | struct cds_ja_inode *node) |
11c5e016 MD |
326 | { |
327 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
2e313670 | 328 | return rcu_dereference(node->u.data[0]); |
11c5e016 MD |
329 | } |
330 | ||
13a7f5a6 MD |
331 | /* |
332 | * The order in which values and pointers are does does not matter: if | |
333 | * a value is missing, we return NULL. If a value is there, but its | |
334 | * associated pointers is still NULL, we return NULL too. | |
335 | */ | |
d68c6810 | 336 | static |
b4540e8a MD |
337 | struct cds_ja_inode_flag *ja_linear_node_get_nth(const struct cds_ja_type *type, |
338 | struct cds_ja_inode *node, | |
b0ca2d21 | 339 | struct cds_ja_inode_flag ***node_flag_ptr, |
8e519e3c | 340 | uint8_t n) |
d68c6810 MD |
341 | { |
342 | uint8_t nr_child; | |
343 | uint8_t *values; | |
b4540e8a MD |
344 | struct cds_ja_inode_flag **pointers; |
345 | struct cds_ja_inode_flag *ptr; | |
d68c6810 MD |
346 | unsigned int i; |
347 | ||
8e519e3c | 348 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); |
d68c6810 | 349 | |
11c5e016 | 350 | nr_child = ja_linear_node_get_nr_child(type, node); |
13a7f5a6 | 351 | cmm_smp_rmb(); /* read nr_child before values and pointers */ |
8e519e3c MD |
352 | assert(nr_child <= type->max_linear_child); |
353 | assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child); | |
d68c6810 | 354 | |
1db4943c | 355 | values = &node->u.data[1]; |
d68c6810 | 356 | for (i = 0; i < nr_child; i++) { |
13a7f5a6 | 357 | if (CMM_LOAD_SHARED(values[i]) == n) |
d68c6810 MD |
358 | break; |
359 | } | |
b0ca2d21 MD |
360 | if (i >= nr_child) { |
361 | if (caa_unlikely(node_flag_ptr)) | |
362 | *node_flag_ptr = NULL; | |
d68c6810 | 363 | return NULL; |
b0ca2d21 | 364 | } |
b4540e8a | 365 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); |
13a7f5a6 | 366 | ptr = rcu_dereference(pointers[i]); |
b0ca2d21 MD |
367 | if (caa_unlikely(node_flag_ptr)) |
368 | *node_flag_ptr = &pointers[i]; | |
d68c6810 MD |
369 | return ptr; |
370 | } | |
371 | ||
291b2543 MD |
372 | static |
373 | struct cds_ja_inode_flag *ja_linear_node_get_left(const struct cds_ja_type *type, | |
374 | struct cds_ja_inode *node, | |
375 | unsigned int n) | |
376 | { | |
377 | uint8_t nr_child; | |
378 | uint8_t *values; | |
379 | struct cds_ja_inode_flag **pointers; | |
380 | struct cds_ja_inode_flag *ptr; | |
381 | unsigned int i, match_idx; | |
382 | int match_v = -1; | |
383 | ||
384 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
385 | ||
386 | nr_child = ja_linear_node_get_nr_child(type, node); | |
387 | cmm_smp_rmb(); /* read nr_child before values and pointers */ | |
388 | assert(nr_child <= type->max_linear_child); | |
389 | assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child); | |
390 | ||
391 | values = &node->u.data[1]; | |
392 | for (i = 0; i < nr_child; i++) { | |
393 | unsigned int v; | |
394 | ||
395 | v = CMM_LOAD_SHARED(values[i]); | |
396 | if (v < n && (int) v > match_v) { | |
397 | match_v = v; | |
398 | match_idx = i; | |
399 | } | |
400 | } | |
401 | if (match_v < 0) { | |
402 | return NULL; | |
403 | } | |
404 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); | |
405 | ptr = rcu_dereference(pointers[match_idx]); | |
406 | return ptr; | |
407 | } | |
408 | ||
11c5e016 | 409 | static |
5a9a87dd | 410 | void ja_linear_node_get_ith_pos(const struct cds_ja_type *type, |
b4540e8a | 411 | struct cds_ja_inode *node, |
11c5e016 MD |
412 | uint8_t i, |
413 | uint8_t *v, | |
b4540e8a | 414 | struct cds_ja_inode_flag **iter) |
11c5e016 MD |
415 | { |
416 | uint8_t *values; | |
b4540e8a | 417 | struct cds_ja_inode_flag **pointers; |
11c5e016 MD |
418 | |
419 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
420 | assert(i < ja_linear_node_get_nr_child(type, node)); | |
421 | ||
422 | values = &node->u.data[1]; | |
423 | *v = values[i]; | |
b4540e8a | 424 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); |
11c5e016 MD |
425 | *iter = pointers[i]; |
426 | } | |
427 | ||
d68c6810 | 428 | static |
b4540e8a MD |
429 | struct cds_ja_inode_flag *ja_pool_node_get_nth(const struct cds_ja_type *type, |
430 | struct cds_ja_inode *node, | |
b1a90ce3 | 431 | struct cds_ja_inode_flag *node_flag, |
b0ca2d21 | 432 | struct cds_ja_inode_flag ***node_flag_ptr, |
8e519e3c | 433 | uint8_t n) |
d68c6810 | 434 | { |
b4540e8a | 435 | struct cds_ja_inode *linear; |
d68c6810 | 436 | |
fd800776 | 437 | assert(type->type_class == RCU_JA_POOL); |
b1a90ce3 MD |
438 | |
439 | switch (type->nr_pool_order) { | |
440 | case 1: | |
441 | { | |
442 | unsigned long bitsel, index; | |
443 | ||
444 | bitsel = ja_node_pool_1d_bitsel(node_flag); | |
445 | assert(bitsel < CHAR_BIT); | |
19ddcd04 | 446 | index = ((unsigned long) n >> bitsel) & 0x1; |
b1a90ce3 MD |
447 | linear = (struct cds_ja_inode *) &node->u.data[index << type->pool_size_order]; |
448 | break; | |
449 | } | |
450 | case 2: | |
451 | { | |
19ddcd04 MD |
452 | unsigned long bitsel[2], index[2], rindex; |
453 | ||
454 | ja_node_pool_2d_bitsel(node_flag, bitsel); | |
455 | assert(bitsel[0] < CHAR_BIT); | |
456 | assert(bitsel[1] < CHAR_BIT); | |
457 | index[0] = ((unsigned long) n >> bitsel[0]) & 0x1; | |
458 | index[0] <<= 1; | |
459 | index[1] = ((unsigned long) n >> bitsel[1]) & 0x1; | |
460 | rindex = index[0] | index[1]; | |
461 | linear = (struct cds_ja_inode *) &node->u.data[rindex << type->pool_size_order]; | |
b1a90ce3 MD |
462 | break; |
463 | } | |
464 | default: | |
465 | linear = NULL; | |
466 | assert(0); | |
467 | } | |
48cbe001 | 468 | return ja_linear_node_get_nth(type, linear, node_flag_ptr, n); |
d68c6810 MD |
469 | } |
470 | ||
11c5e016 | 471 | static |
b4540e8a MD |
472 | struct cds_ja_inode *ja_pool_node_get_ith_pool(const struct cds_ja_type *type, |
473 | struct cds_ja_inode *node, | |
11c5e016 MD |
474 | uint8_t i) |
475 | { | |
476 | assert(type->type_class == RCU_JA_POOL); | |
b4540e8a | 477 | return (struct cds_ja_inode *) |
11c5e016 MD |
478 | &node->u.data[(unsigned int) i << type->pool_size_order]; |
479 | } | |
480 | ||
291b2543 MD |
481 | static |
482 | struct cds_ja_inode_flag *ja_pool_node_get_left(const struct cds_ja_type *type, | |
483 | struct cds_ja_inode *node, | |
484 | unsigned int n) | |
485 | { | |
486 | unsigned int pool_nr; | |
487 | int match_v = -1; | |
488 | struct cds_ja_inode_flag *match_node_flag = NULL; | |
489 | ||
490 | assert(type->type_class == RCU_JA_POOL); | |
491 | ||
492 | for (pool_nr = 0; pool_nr < (1U << type->nr_pool_order); pool_nr++) { | |
493 | struct cds_ja_inode *pool = | |
494 | ja_pool_node_get_ith_pool(type, | |
495 | node, pool_nr); | |
496 | uint8_t nr_child = | |
497 | ja_linear_node_get_nr_child(type, pool); | |
498 | unsigned int j; | |
499 | ||
500 | for (j = 0; j < nr_child; j++) { | |
501 | struct cds_ja_inode_flag *iter; | |
502 | uint8_t v; | |
503 | ||
504 | ja_linear_node_get_ith_pos(type, pool, | |
505 | j, &v, &iter); | |
506 | if (!iter) | |
507 | continue; | |
508 | if (v < n && (int) v > match_v) { | |
509 | match_v = v; | |
510 | match_node_flag = iter; | |
511 | } | |
512 | } | |
513 | } | |
514 | return match_node_flag; | |
515 | } | |
516 | ||
d68c6810 | 517 | static |
b4540e8a MD |
518 | struct cds_ja_inode_flag *ja_pigeon_node_get_nth(const struct cds_ja_type *type, |
519 | struct cds_ja_inode *node, | |
b0ca2d21 | 520 | struct cds_ja_inode_flag ***node_flag_ptr, |
8e519e3c | 521 | uint8_t n) |
d68c6810 | 522 | { |
48cbe001 MD |
523 | struct cds_ja_inode_flag **child_node_flag_ptr; |
524 | struct cds_ja_inode_flag *child_node_flag; | |
5a9a87dd | 525 | |
d68c6810 | 526 | assert(type->type_class == RCU_JA_PIGEON); |
48cbe001 MD |
527 | child_node_flag_ptr = &((struct cds_ja_inode_flag **) node->u.data)[n]; |
528 | child_node_flag = rcu_dereference(*child_node_flag_ptr); | |
582a6ade | 529 | dbg_printf("ja_pigeon_node_get_nth child_node_flag_ptr %p\n", |
48cbe001 | 530 | child_node_flag_ptr); |
b0ca2d21 | 531 | if (caa_unlikely(node_flag_ptr)) |
48cbe001 MD |
532 | *node_flag_ptr = child_node_flag_ptr; |
533 | return child_node_flag; | |
d68c6810 MD |
534 | } |
535 | ||
291b2543 MD |
536 | static |
537 | struct cds_ja_inode_flag *ja_pigeon_node_get_left(const struct cds_ja_type *type, | |
538 | struct cds_ja_inode *node, | |
539 | unsigned int n) | |
540 | { | |
541 | struct cds_ja_inode_flag **child_node_flag_ptr; | |
542 | struct cds_ja_inode_flag *child_node_flag; | |
543 | int i; | |
544 | ||
545 | assert(type->type_class == RCU_JA_PIGEON); | |
546 | ||
547 | /* n - 1 is first value left of n */ | |
548 | for (i = n - 1; i >= 0; i--) { | |
549 | child_node_flag_ptr = &((struct cds_ja_inode_flag **) node->u.data)[i]; | |
550 | child_node_flag = rcu_dereference(*child_node_flag_ptr); | |
551 | if (child_node_flag) { | |
552 | dbg_printf("ja_pigeon_node_get_left child_node_flag %p\n", | |
553 | child_node_flag); | |
554 | return child_node_flag; | |
555 | } | |
556 | } | |
557 | return NULL; | |
558 | } | |
559 | ||
2e313670 MD |
560 | static |
561 | struct cds_ja_inode_flag *ja_pigeon_node_get_ith_pos(const struct cds_ja_type *type, | |
562 | struct cds_ja_inode *node, | |
563 | uint8_t i) | |
564 | { | |
48cbe001 | 565 | return ja_pigeon_node_get_nth(type, node, NULL, i); |
2e313670 MD |
566 | } |
567 | ||
13a7f5a6 MD |
568 | /* |
569 | * ja_node_get_nth: get nth item from a node. | |
570 | * node_flag is already rcu_dereference'd. | |
571 | */ | |
d68c6810 | 572 | static |
b62a8d0c | 573 | struct cds_ja_inode_flag *ja_node_get_nth(struct cds_ja_inode_flag *node_flag, |
b0ca2d21 | 574 | struct cds_ja_inode_flag ***node_flag_ptr, |
8e519e3c | 575 | uint8_t n) |
d68c6810 MD |
576 | { |
577 | unsigned int type_index; | |
b4540e8a | 578 | struct cds_ja_inode *node; |
d96bfb0d | 579 | const struct cds_ja_type *type; |
d68c6810 | 580 | |
d68c6810 | 581 | node = ja_node_ptr(node_flag); |
5a9a87dd | 582 | assert(node != NULL); |
d68c6810 MD |
583 | type_index = ja_node_type(node_flag); |
584 | type = &ja_types[type_index]; | |
585 | ||
586 | switch (type->type_class) { | |
587 | case RCU_JA_LINEAR: | |
5a9a87dd | 588 | return ja_linear_node_get_nth(type, node, |
b62a8d0c | 589 | node_flag_ptr, n); |
fd800776 | 590 | case RCU_JA_POOL: |
b1a90ce3 | 591 | return ja_pool_node_get_nth(type, node, node_flag, |
b62a8d0c | 592 | node_flag_ptr, n); |
d68c6810 | 593 | case RCU_JA_PIGEON: |
5a9a87dd | 594 | return ja_pigeon_node_get_nth(type, node, |
b62a8d0c | 595 | node_flag_ptr, n); |
d68c6810 MD |
596 | default: |
597 | assert(0); | |
598 | return (void *) -1UL; | |
599 | } | |
600 | } | |
601 | ||
291b2543 MD |
602 | static |
603 | struct cds_ja_inode_flag *ja_node_get_left(struct cds_ja_inode_flag *node_flag, | |
604 | unsigned int n) | |
605 | { | |
606 | unsigned int type_index; | |
607 | struct cds_ja_inode *node; | |
608 | const struct cds_ja_type *type; | |
609 | ||
610 | node = ja_node_ptr(node_flag); | |
611 | assert(node != NULL); | |
612 | type_index = ja_node_type(node_flag); | |
613 | type = &ja_types[type_index]; | |
614 | ||
615 | switch (type->type_class) { | |
616 | case RCU_JA_LINEAR: | |
617 | return ja_linear_node_get_left(type, node, n); | |
618 | case RCU_JA_POOL: | |
619 | return ja_pool_node_get_left(type, node, n); | |
620 | case RCU_JA_PIGEON: | |
621 | return ja_pigeon_node_get_left(type, node, n); | |
622 | default: | |
623 | assert(0); | |
624 | return (void *) -1UL; | |
625 | } | |
626 | } | |
627 | ||
628 | static | |
629 | struct cds_ja_inode_flag *ja_node_get_rightmost(struct cds_ja_inode_flag *node_flag) | |
630 | { | |
631 | return ja_node_get_left(node_flag, JA_ENTRY_PER_NODE); | |
632 | } | |
633 | ||
8e519e3c | 634 | static |
d96bfb0d | 635 | int ja_linear_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 636 | struct cds_ja_inode *node, |
d96bfb0d | 637 | struct cds_ja_shadow_node *shadow_node, |
8e519e3c | 638 | uint8_t n, |
b4540e8a | 639 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c MD |
640 | { |
641 | uint8_t nr_child; | |
642 | uint8_t *values, *nr_child_ptr; | |
b4540e8a | 643 | struct cds_ja_inode_flag **pointers; |
2e313670 | 644 | unsigned int i, unused = 0; |
8e519e3c MD |
645 | |
646 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
647 | ||
648 | nr_child_ptr = &node->u.data[0]; | |
48cbe001 MD |
649 | dbg_printf("linear set nth: n %u, nr_child_ptr %p\n", |
650 | (unsigned int) n, nr_child_ptr); | |
8e519e3c MD |
651 | nr_child = *nr_child_ptr; |
652 | assert(nr_child <= type->max_linear_child); | |
8e519e3c MD |
653 | |
654 | values = &node->u.data[1]; | |
2e313670 MD |
655 | pointers = (struct cds_ja_inode_flag **) align_ptr_size(&values[type->max_linear_child]); |
656 | /* Check if node value is already populated */ | |
8e519e3c | 657 | for (i = 0; i < nr_child; i++) { |
2e313670 MD |
658 | if (values[i] == n) { |
659 | if (pointers[i]) | |
660 | return -EEXIST; | |
661 | else | |
662 | break; | |
663 | } else { | |
664 | if (!pointers[i]) | |
665 | unused++; | |
666 | } | |
8e519e3c | 667 | } |
2e313670 MD |
668 | if (i == nr_child && nr_child >= type->max_linear_child) { |
669 | if (unused) | |
670 | return -ERANGE; /* recompact node */ | |
671 | else | |
672 | return -ENOSPC; /* No space left in this node type */ | |
673 | } | |
674 | ||
675 | assert(pointers[i] == NULL); | |
676 | rcu_assign_pointer(pointers[i], child_node_flag); | |
677 | /* If we expanded the nr_child, increment it */ | |
678 | if (i == nr_child) { | |
679 | CMM_STORE_SHARED(values[nr_child], n); | |
680 | /* write pointer and value before nr_child */ | |
681 | cmm_smp_wmb(); | |
682 | CMM_STORE_SHARED(*nr_child_ptr, nr_child + 1); | |
8e519e3c | 683 | } |
e1db2db5 | 684 | shadow_node->nr_child++; |
a2a7ff59 MD |
685 | dbg_printf("linear set nth: %u child, shadow: %u child, for node %p shadow %p\n", |
686 | (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr), | |
687 | (unsigned int) shadow_node->nr_child, | |
688 | node, shadow_node); | |
689 | ||
8e519e3c MD |
690 | return 0; |
691 | } | |
692 | ||
693 | static | |
d96bfb0d | 694 | int ja_pool_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 695 | struct cds_ja_inode *node, |
b1a90ce3 | 696 | struct cds_ja_inode_flag *node_flag, |
d96bfb0d | 697 | struct cds_ja_shadow_node *shadow_node, |
8e519e3c | 698 | uint8_t n, |
b4540e8a | 699 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c | 700 | { |
b4540e8a | 701 | struct cds_ja_inode *linear; |
8e519e3c MD |
702 | |
703 | assert(type->type_class == RCU_JA_POOL); | |
b1a90ce3 MD |
704 | |
705 | switch (type->nr_pool_order) { | |
706 | case 1: | |
707 | { | |
708 | unsigned long bitsel, index; | |
709 | ||
710 | bitsel = ja_node_pool_1d_bitsel(node_flag); | |
711 | assert(bitsel < CHAR_BIT); | |
19ddcd04 | 712 | index = ((unsigned long) n >> bitsel) & 0x1; |
b1a90ce3 MD |
713 | linear = (struct cds_ja_inode *) &node->u.data[index << type->pool_size_order]; |
714 | break; | |
715 | } | |
716 | case 2: | |
717 | { | |
19ddcd04 MD |
718 | unsigned long bitsel[2], index[2], rindex; |
719 | ||
720 | ja_node_pool_2d_bitsel(node_flag, bitsel); | |
721 | assert(bitsel[0] < CHAR_BIT); | |
722 | assert(bitsel[1] < CHAR_BIT); | |
723 | index[0] = ((unsigned long) n >> bitsel[0]) & 0x1; | |
724 | index[0] <<= 1; | |
725 | index[1] = ((unsigned long) n >> bitsel[1]) & 0x1; | |
726 | rindex = index[0] | index[1]; | |
727 | linear = (struct cds_ja_inode *) &node->u.data[rindex << type->pool_size_order]; | |
b1a90ce3 MD |
728 | break; |
729 | } | |
730 | default: | |
731 | linear = NULL; | |
732 | assert(0); | |
733 | } | |
734 | ||
e1db2db5 MD |
735 | return ja_linear_node_set_nth(type, linear, shadow_node, |
736 | n, child_node_flag); | |
8e519e3c MD |
737 | } |
738 | ||
739 | static | |
d96bfb0d | 740 | int ja_pigeon_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 741 | struct cds_ja_inode *node, |
d96bfb0d | 742 | struct cds_ja_shadow_node *shadow_node, |
8e519e3c | 743 | uint8_t n, |
b4540e8a | 744 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c | 745 | { |
b4540e8a | 746 | struct cds_ja_inode_flag **ptr; |
8e519e3c MD |
747 | |
748 | assert(type->type_class == RCU_JA_PIGEON); | |
b4540e8a | 749 | ptr = &((struct cds_ja_inode_flag **) node->u.data)[n]; |
5a9a87dd | 750 | if (*ptr) |
8e519e3c MD |
751 | return -EEXIST; |
752 | rcu_assign_pointer(*ptr, child_node_flag); | |
e1db2db5 | 753 | shadow_node->nr_child++; |
8e519e3c MD |
754 | return 0; |
755 | } | |
756 | ||
d68c6810 | 757 | /* |
7a0b2331 | 758 | * _ja_node_set_nth: set nth item within a node. Return an error |
8e519e3c | 759 | * (negative error value) if it is already there. |
d68c6810 | 760 | */ |
8e519e3c | 761 | static |
d96bfb0d | 762 | int _ja_node_set_nth(const struct cds_ja_type *type, |
b4540e8a | 763 | struct cds_ja_inode *node, |
b1a90ce3 | 764 | struct cds_ja_inode_flag *node_flag, |
d96bfb0d | 765 | struct cds_ja_shadow_node *shadow_node, |
e1db2db5 | 766 | uint8_t n, |
b4540e8a | 767 | struct cds_ja_inode_flag *child_node_flag) |
8e519e3c | 768 | { |
8e519e3c MD |
769 | switch (type->type_class) { |
770 | case RCU_JA_LINEAR: | |
e1db2db5 | 771 | return ja_linear_node_set_nth(type, node, shadow_node, n, |
8e519e3c MD |
772 | child_node_flag); |
773 | case RCU_JA_POOL: | |
b1a90ce3 | 774 | return ja_pool_node_set_nth(type, node, node_flag, shadow_node, n, |
8e519e3c MD |
775 | child_node_flag); |
776 | case RCU_JA_PIGEON: | |
e1db2db5 | 777 | return ja_pigeon_node_set_nth(type, node, shadow_node, n, |
8e519e3c | 778 | child_node_flag); |
e1db2db5 MD |
779 | case RCU_JA_NULL: |
780 | return -ENOSPC; | |
8e519e3c MD |
781 | default: |
782 | assert(0); | |
783 | return -EINVAL; | |
784 | } | |
785 | ||
786 | return 0; | |
787 | } | |
7a0b2331 | 788 | |
2e313670 | 789 | static |
af3cbd45 | 790 | int ja_linear_node_clear_ptr(const struct cds_ja_type *type, |
2e313670 MD |
791 | struct cds_ja_inode *node, |
792 | struct cds_ja_shadow_node *shadow_node, | |
af3cbd45 | 793 | struct cds_ja_inode_flag **node_flag_ptr) |
2e313670 MD |
794 | { |
795 | uint8_t nr_child; | |
af3cbd45 | 796 | uint8_t *nr_child_ptr; |
2e313670 MD |
797 | |
798 | assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL); | |
799 | ||
800 | nr_child_ptr = &node->u.data[0]; | |
2e313670 MD |
801 | nr_child = *nr_child_ptr; |
802 | assert(nr_child <= type->max_linear_child); | |
803 | ||
48cbe001 MD |
804 | if (type->type_class == RCU_JA_LINEAR) { |
805 | assert(!shadow_node->fallback_removal_count); | |
806 | if (shadow_node->nr_child <= type->min_child) { | |
2e313670 MD |
807 | /* We need to try recompacting the node */ |
808 | return -EFBIG; | |
809 | } | |
810 | } | |
19ddcd04 | 811 | dbg_printf("linear clear ptr: nr_child_ptr %p\n", nr_child_ptr); |
af3cbd45 MD |
812 | assert(*node_flag_ptr != NULL); |
813 | rcu_assign_pointer(*node_flag_ptr, NULL); | |
2e313670 MD |
814 | /* |
815 | * Value and nr_child are never changed (would cause ABA issue). | |
816 | * Instead, we leave the pointer to NULL and recompact the node | |
817 | * once in a while. It is allowed to set a NULL pointer to a new | |
818 | * value without recompaction though. | |
819 | * Only update the shadow node accounting. | |
820 | */ | |
821 | shadow_node->nr_child--; | |
af3cbd45 | 822 | dbg_printf("linear clear ptr: %u child, shadow: %u child, for node %p shadow %p\n", |
2e313670 MD |
823 | (unsigned int) CMM_LOAD_SHARED(*nr_child_ptr), |
824 | (unsigned int) shadow_node->nr_child, | |
825 | node, shadow_node); | |
2e313670 MD |
826 | return 0; |
827 | } | |
828 | ||
829 | static | |
af3cbd45 | 830 | int ja_pool_node_clear_ptr(const struct cds_ja_type *type, |
2e313670 | 831 | struct cds_ja_inode *node, |
19ddcd04 | 832 | struct cds_ja_inode_flag *node_flag, |
2e313670 | 833 | struct cds_ja_shadow_node *shadow_node, |
af3cbd45 | 834 | struct cds_ja_inode_flag **node_flag_ptr, |
2e313670 MD |
835 | uint8_t n) |
836 | { | |
837 | struct cds_ja_inode *linear; | |
838 | ||
839 | assert(type->type_class == RCU_JA_POOL); | |
19ddcd04 MD |
840 | |
841 | if (shadow_node->fallback_removal_count) { | |
842 | shadow_node->fallback_removal_count--; | |
843 | } else { | |
844 | /* We should try recompacting the node */ | |
845 | if (shadow_node->nr_child <= type->min_child) | |
846 | return -EFBIG; | |
847 | } | |
848 | ||
849 | switch (type->nr_pool_order) { | |
850 | case 1: | |
851 | { | |
852 | unsigned long bitsel, index; | |
853 | ||
854 | bitsel = ja_node_pool_1d_bitsel(node_flag); | |
855 | assert(bitsel < CHAR_BIT); | |
856 | index = ((unsigned long) n >> bitsel) & type->nr_pool_order; | |
857 | linear = (struct cds_ja_inode *) &node->u.data[index << type->pool_size_order]; | |
858 | break; | |
859 | } | |
860 | case 2: | |
861 | { | |
862 | unsigned long bitsel[2], index[2], rindex; | |
863 | ||
864 | ja_node_pool_2d_bitsel(node_flag, bitsel); | |
865 | assert(bitsel[0] < CHAR_BIT); | |
866 | assert(bitsel[1] < CHAR_BIT); | |
867 | index[0] = ((unsigned long) n >> bitsel[0]) & 0x1; | |
868 | index[0] <<= 1; | |
869 | index[1] = ((unsigned long) n >> bitsel[1]) & 0x1; | |
870 | rindex = index[0] | index[1]; | |
871 | linear = (struct cds_ja_inode *) &node->u.data[rindex << type->pool_size_order]; | |
872 | break; | |
873 | } | |
874 | default: | |
875 | linear = NULL; | |
876 | assert(0); | |
877 | } | |
878 | ||
af3cbd45 | 879 | return ja_linear_node_clear_ptr(type, linear, shadow_node, node_flag_ptr); |
2e313670 MD |
880 | } |
881 | ||
882 | static | |
af3cbd45 | 883 | int ja_pigeon_node_clear_ptr(const struct cds_ja_type *type, |
2e313670 MD |
884 | struct cds_ja_inode *node, |
885 | struct cds_ja_shadow_node *shadow_node, | |
af3cbd45 | 886 | struct cds_ja_inode_flag **node_flag_ptr) |
2e313670 | 887 | { |
2e313670 | 888 | assert(type->type_class == RCU_JA_PIGEON); |
19ddcd04 MD |
889 | |
890 | if (shadow_node->fallback_removal_count) { | |
891 | shadow_node->fallback_removal_count--; | |
892 | } else { | |
893 | /* We should try recompacting the node */ | |
894 | if (shadow_node->nr_child <= type->min_child) | |
895 | return -EFBIG; | |
896 | } | |
4d6ef45e | 897 | dbg_printf("ja_pigeon_node_clear_ptr: clearing ptr: %p\n", *node_flag_ptr); |
af3cbd45 | 898 | rcu_assign_pointer(*node_flag_ptr, NULL); |
2e313670 MD |
899 | shadow_node->nr_child--; |
900 | return 0; | |
901 | } | |
902 | ||
903 | /* | |
af3cbd45 | 904 | * _ja_node_clear_ptr: clear ptr item within a node. Return an error |
2e313670 MD |
905 | * (negative error value) if it is not found (-ENOENT). |
906 | */ | |
907 | static | |
af3cbd45 | 908 | int _ja_node_clear_ptr(const struct cds_ja_type *type, |
2e313670 | 909 | struct cds_ja_inode *node, |
19ddcd04 | 910 | struct cds_ja_inode_flag *node_flag, |
2e313670 | 911 | struct cds_ja_shadow_node *shadow_node, |
af3cbd45 | 912 | struct cds_ja_inode_flag **node_flag_ptr, |
2e313670 MD |
913 | uint8_t n) |
914 | { | |
915 | switch (type->type_class) { | |
916 | case RCU_JA_LINEAR: | |
af3cbd45 | 917 | return ja_linear_node_clear_ptr(type, node, shadow_node, node_flag_ptr); |
2e313670 | 918 | case RCU_JA_POOL: |
19ddcd04 | 919 | return ja_pool_node_clear_ptr(type, node, node_flag, shadow_node, node_flag_ptr, n); |
2e313670 | 920 | case RCU_JA_PIGEON: |
af3cbd45 | 921 | return ja_pigeon_node_clear_ptr(type, node, shadow_node, node_flag_ptr); |
2e313670 MD |
922 | case RCU_JA_NULL: |
923 | return -ENOENT; | |
924 | default: | |
925 | assert(0); | |
926 | return -EINVAL; | |
927 | } | |
928 | ||
929 | return 0; | |
930 | } | |
931 | ||
b1a90ce3 MD |
932 | /* |
933 | * Calculate bit distribution. Returns the bit (0 to 7) that splits the | |
934 | * distribution in two sub-distributions containing as much elements one | |
935 | * compared to the other. | |
936 | */ | |
937 | static | |
938 | unsigned int ja_node_sum_distribution_1d(enum ja_recompact mode, | |
939 | struct cds_ja *ja, | |
940 | unsigned int type_index, | |
941 | const struct cds_ja_type *type, | |
942 | struct cds_ja_inode *node, | |
943 | struct cds_ja_shadow_node *shadow_node, | |
944 | uint8_t n, | |
945 | struct cds_ja_inode_flag *child_node_flag, | |
946 | struct cds_ja_inode_flag **nullify_node_flag_ptr) | |
947 | { | |
948 | uint8_t nr_one[JA_BITS_PER_BYTE]; | |
949 | unsigned int bitsel = 0, bit_i, overall_best_distance = UINT_MAX; | |
950 | unsigned int distrib_nr_child = 0; | |
951 | ||
952 | memset(nr_one, 0, sizeof(nr_one)); | |
953 | ||
954 | switch (type->type_class) { | |
955 | case RCU_JA_LINEAR: | |
956 | { | |
957 | uint8_t nr_child = | |
958 | ja_linear_node_get_nr_child(type, node); | |
959 | unsigned int i; | |
960 | ||
961 | for (i = 0; i < nr_child; i++) { | |
962 | struct cds_ja_inode_flag *iter; | |
b1a90ce3 MD |
963 | uint8_t v; |
964 | ||
965 | ja_linear_node_get_ith_pos(type, node, i, &v, &iter); | |
966 | if (!iter) | |
967 | continue; | |
968 | if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter) | |
969 | continue; | |
f5531dd9 MD |
970 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { |
971 | if (v & (1U << bit_i)) | |
972 | nr_one[bit_i]++; | |
b1a90ce3 MD |
973 | } |
974 | distrib_nr_child++; | |
975 | } | |
976 | break; | |
977 | } | |
978 | case RCU_JA_POOL: | |
979 | { | |
980 | unsigned int pool_nr; | |
981 | ||
982 | for (pool_nr = 0; pool_nr < (1U << type->nr_pool_order); pool_nr++) { | |
983 | struct cds_ja_inode *pool = | |
984 | ja_pool_node_get_ith_pool(type, | |
985 | node, pool_nr); | |
986 | uint8_t nr_child = | |
987 | ja_linear_node_get_nr_child(type, pool); | |
988 | unsigned int j; | |
989 | ||
990 | for (j = 0; j < nr_child; j++) { | |
991 | struct cds_ja_inode_flag *iter; | |
b1a90ce3 MD |
992 | uint8_t v; |
993 | ||
994 | ja_linear_node_get_ith_pos(type, pool, | |
995 | j, &v, &iter); | |
996 | if (!iter) | |
997 | continue; | |
998 | if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter) | |
999 | continue; | |
f5531dd9 MD |
1000 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { |
1001 | if (v & (1U << bit_i)) | |
1002 | nr_one[bit_i]++; | |
b1a90ce3 MD |
1003 | } |
1004 | distrib_nr_child++; | |
1005 | } | |
1006 | } | |
1007 | break; | |
1008 | } | |
1009 | case RCU_JA_PIGEON: | |
1010 | { | |
b1a90ce3 MD |
1011 | unsigned int i; |
1012 | ||
1013 | assert(mode == JA_RECOMPACT_DEL); | |
48cbe001 | 1014 | for (i = 0; i < JA_ENTRY_PER_NODE; i++) { |
b1a90ce3 | 1015 | struct cds_ja_inode_flag *iter; |
b1a90ce3 MD |
1016 | |
1017 | iter = ja_pigeon_node_get_ith_pos(type, node, i); | |
1018 | if (!iter) | |
1019 | continue; | |
1020 | if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter) | |
1021 | continue; | |
f5531dd9 MD |
1022 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { |
1023 | if (i & (1U << bit_i)) | |
1024 | nr_one[bit_i]++; | |
b1a90ce3 MD |
1025 | } |
1026 | distrib_nr_child++; | |
1027 | } | |
1028 | break; | |
1029 | } | |
1030 | case RCU_JA_NULL: | |
19ddcd04 | 1031 | assert(mode == JA_RECOMPACT_ADD_NEXT); |
b1a90ce3 MD |
1032 | break; |
1033 | default: | |
1034 | assert(0); | |
1035 | break; | |
1036 | } | |
1037 | ||
19ddcd04 | 1038 | if (mode == JA_RECOMPACT_ADD_NEXT || mode == JA_RECOMPACT_ADD_SAME) { |
f5531dd9 MD |
1039 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { |
1040 | if (n & (1U << bit_i)) | |
1041 | nr_one[bit_i]++; | |
b1a90ce3 MD |
1042 | } |
1043 | distrib_nr_child++; | |
1044 | } | |
1045 | ||
1046 | /* | |
1047 | * The best bit selector is that for which the number of ones is | |
1048 | * closest to half of the number of children in the | |
f5531dd9 MD |
1049 | * distribution. We calculate the distance using the double of |
1050 | * the sub-distribution sizes to eliminate truncation error. | |
b1a90ce3 MD |
1051 | */ |
1052 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { | |
1053 | unsigned int distance_to_best; | |
1054 | ||
f5531dd9 | 1055 | distance_to_best = abs_int((nr_one[bit_i] << 1U) - distrib_nr_child); |
b1a90ce3 MD |
1056 | if (distance_to_best < overall_best_distance) { |
1057 | overall_best_distance = distance_to_best; | |
1058 | bitsel = bit_i; | |
1059 | } | |
1060 | } | |
1061 | dbg_printf("1 dimension pool bit selection: (%u)\n", bitsel); | |
1062 | return bitsel; | |
1063 | } | |
1064 | ||
19ddcd04 MD |
1065 | /* |
1066 | * Calculate bit distribution in two dimensions. Returns the two bits | |
1067 | * (each 0 to 7) that splits the distribution in four sub-distributions | |
1068 | * containing as much elements one compared to the other. | |
1069 | */ | |
1070 | static | |
1071 | void ja_node_sum_distribution_2d(enum ja_recompact mode, | |
1072 | struct cds_ja *ja, | |
1073 | unsigned int type_index, | |
1074 | const struct cds_ja_type *type, | |
1075 | struct cds_ja_inode *node, | |
1076 | struct cds_ja_shadow_node *shadow_node, | |
1077 | uint8_t n, | |
1078 | struct cds_ja_inode_flag *child_node_flag, | |
1079 | struct cds_ja_inode_flag **nullify_node_flag_ptr, | |
1080 | unsigned int *_bitsel) | |
1081 | { | |
1082 | uint8_t nr_2d_11[JA_BITS_PER_BYTE][JA_BITS_PER_BYTE], | |
1083 | nr_2d_10[JA_BITS_PER_BYTE][JA_BITS_PER_BYTE], | |
1084 | nr_2d_01[JA_BITS_PER_BYTE][JA_BITS_PER_BYTE], | |
1085 | nr_2d_00[JA_BITS_PER_BYTE][JA_BITS_PER_BYTE]; | |
1086 | unsigned int bitsel[2] = { 0, 1 }; | |
4a073c53 MD |
1087 | unsigned int bit_i, bit_j; |
1088 | int overall_best_distance = INT_MAX; | |
19ddcd04 MD |
1089 | unsigned int distrib_nr_child = 0; |
1090 | ||
1091 | memset(nr_2d_11, 0, sizeof(nr_2d_11)); | |
1092 | memset(nr_2d_10, 0, sizeof(nr_2d_10)); | |
4a073c53 MD |
1093 | memset(nr_2d_01, 0, sizeof(nr_2d_01)); |
1094 | memset(nr_2d_00, 0, sizeof(nr_2d_00)); | |
19ddcd04 MD |
1095 | |
1096 | switch (type->type_class) { | |
1097 | case RCU_JA_LINEAR: | |
1098 | { | |
1099 | uint8_t nr_child = | |
1100 | ja_linear_node_get_nr_child(type, node); | |
1101 | unsigned int i; | |
1102 | ||
1103 | for (i = 0; i < nr_child; i++) { | |
1104 | struct cds_ja_inode_flag *iter; | |
1105 | uint8_t v; | |
1106 | ||
1107 | ja_linear_node_get_ith_pos(type, node, i, &v, &iter); | |
1108 | if (!iter) | |
1109 | continue; | |
1110 | if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter) | |
1111 | continue; | |
1112 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { | |
1113 | for (bit_j = 0; bit_j < bit_i; bit_j++) { | |
1114 | if ((v & (1U << bit_i)) && (v & (1U << bit_j))) { | |
1115 | nr_2d_11[bit_i][bit_j]++; | |
1116 | } | |
1117 | if ((v & (1U << bit_i)) && !(v & (1U << bit_j))) { | |
1118 | nr_2d_10[bit_i][bit_j]++; | |
1119 | } | |
1120 | if (!(v & (1U << bit_i)) && (v & (1U << bit_j))) { | |
1121 | nr_2d_01[bit_i][bit_j]++; | |
1122 | } | |
1123 | if (!(v & (1U << bit_i)) && !(v & (1U << bit_j))) { | |
1124 | nr_2d_00[bit_i][bit_j]++; | |
1125 | } | |
1126 | } | |
1127 | } | |
1128 | distrib_nr_child++; | |
1129 | } | |
1130 | break; | |
1131 | } | |
1132 | case RCU_JA_POOL: | |
1133 | { | |
1134 | unsigned int pool_nr; | |
1135 | ||
1136 | for (pool_nr = 0; pool_nr < (1U << type->nr_pool_order); pool_nr++) { | |
1137 | struct cds_ja_inode *pool = | |
1138 | ja_pool_node_get_ith_pool(type, | |
1139 | node, pool_nr); | |
1140 | uint8_t nr_child = | |
1141 | ja_linear_node_get_nr_child(type, pool); | |
1142 | unsigned int j; | |
1143 | ||
1144 | for (j = 0; j < nr_child; j++) { | |
1145 | struct cds_ja_inode_flag *iter; | |
1146 | uint8_t v; | |
1147 | ||
1148 | ja_linear_node_get_ith_pos(type, pool, | |
1149 | j, &v, &iter); | |
1150 | if (!iter) | |
1151 | continue; | |
1152 | if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter) | |
1153 | continue; | |
1154 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { | |
1155 | for (bit_j = 0; bit_j < bit_i; bit_j++) { | |
1156 | if ((v & (1U << bit_i)) && (v & (1U << bit_j))) { | |
1157 | nr_2d_11[bit_i][bit_j]++; | |
1158 | } | |
1159 | if ((v & (1U << bit_i)) && !(v & (1U << bit_j))) { | |
1160 | nr_2d_10[bit_i][bit_j]++; | |
1161 | } | |
1162 | if (!(v & (1U << bit_i)) && (v & (1U << bit_j))) { | |
1163 | nr_2d_01[bit_i][bit_j]++; | |
1164 | } | |
1165 | if (!(v & (1U << bit_i)) && !(v & (1U << bit_j))) { | |
1166 | nr_2d_00[bit_i][bit_j]++; | |
1167 | } | |
1168 | } | |
1169 | } | |
1170 | distrib_nr_child++; | |
1171 | } | |
1172 | } | |
1173 | break; | |
1174 | } | |
1175 | case RCU_JA_PIGEON: | |
1176 | { | |
19ddcd04 MD |
1177 | unsigned int i; |
1178 | ||
1179 | assert(mode == JA_RECOMPACT_DEL); | |
48cbe001 | 1180 | for (i = 0; i < JA_ENTRY_PER_NODE; i++) { |
19ddcd04 MD |
1181 | struct cds_ja_inode_flag *iter; |
1182 | ||
1183 | iter = ja_pigeon_node_get_ith_pos(type, node, i); | |
1184 | if (!iter) | |
1185 | continue; | |
1186 | if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter) | |
1187 | continue; | |
1188 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { | |
1189 | for (bit_j = 0; bit_j < bit_i; bit_j++) { | |
1190 | if ((i & (1U << bit_i)) && (i & (1U << bit_j))) { | |
1191 | nr_2d_11[bit_i][bit_j]++; | |
1192 | } | |
1193 | if ((i & (1U << bit_i)) && !(i & (1U << bit_j))) { | |
1194 | nr_2d_10[bit_i][bit_j]++; | |
1195 | } | |
1196 | if (!(i & (1U << bit_i)) && (i & (1U << bit_j))) { | |
1197 | nr_2d_01[bit_i][bit_j]++; | |
1198 | } | |
1199 | if (!(i & (1U << bit_i)) && !(i & (1U << bit_j))) { | |
1200 | nr_2d_00[bit_i][bit_j]++; | |
1201 | } | |
1202 | } | |
1203 | } | |
1204 | distrib_nr_child++; | |
1205 | } | |
1206 | break; | |
1207 | } | |
1208 | case RCU_JA_NULL: | |
1209 | assert(mode == JA_RECOMPACT_ADD_NEXT); | |
1210 | break; | |
1211 | default: | |
1212 | assert(0); | |
1213 | break; | |
1214 | } | |
1215 | ||
1216 | if (mode == JA_RECOMPACT_ADD_NEXT || mode == JA_RECOMPACT_ADD_SAME) { | |
1217 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { | |
1218 | for (bit_j = 0; bit_j < bit_i; bit_j++) { | |
1219 | if ((n & (1U << bit_i)) && (n & (1U << bit_j))) { | |
1220 | nr_2d_11[bit_i][bit_j]++; | |
1221 | } | |
1222 | if ((n & (1U << bit_i)) && !(n & (1U << bit_j))) { | |
1223 | nr_2d_10[bit_i][bit_j]++; | |
1224 | } | |
1225 | if (!(n & (1U << bit_i)) && (n & (1U << bit_j))) { | |
1226 | nr_2d_01[bit_i][bit_j]++; | |
1227 | } | |
1228 | if (!(n & (1U << bit_i)) && !(n & (1U << bit_j))) { | |
1229 | nr_2d_00[bit_i][bit_j]++; | |
1230 | } | |
1231 | } | |
1232 | } | |
1233 | distrib_nr_child++; | |
1234 | } | |
1235 | ||
1236 | /* | |
1237 | * The best bit selector is that for which the number of nodes | |
1238 | * in each sub-class is closest to one-fourth of the number of | |
1239 | * children in the distribution. We calculate the distance using | |
1240 | * 4 times the size of the sub-distribution to eliminate | |
1241 | * truncation error. | |
1242 | */ | |
1243 | for (bit_i = 0; bit_i < JA_BITS_PER_BYTE; bit_i++) { | |
1244 | for (bit_j = 0; bit_j < bit_i; bit_j++) { | |
4a073c53 | 1245 | int distance_to_best[4]; |
19ddcd04 | 1246 | |
4a073c53 MD |
1247 | distance_to_best[0] = (nr_2d_11[bit_i][bit_j] << 2U) - distrib_nr_child; |
1248 | distance_to_best[1] = (nr_2d_10[bit_i][bit_j] << 2U) - distrib_nr_child; | |
1249 | distance_to_best[2] = (nr_2d_01[bit_i][bit_j] << 2U) - distrib_nr_child; | |
1250 | distance_to_best[3] = (nr_2d_00[bit_i][bit_j] << 2U) - distrib_nr_child; | |
19ddcd04 | 1251 | |
4a073c53 MD |
1252 | /* Consider worse distance above best */ |
1253 | if (distance_to_best[1] > 0 && distance_to_best[1] > distance_to_best[0]) | |
19ddcd04 | 1254 | distance_to_best[0] = distance_to_best[1]; |
4a073c53 | 1255 | if (distance_to_best[2] > 0 && distance_to_best[2] > distance_to_best[0]) |
19ddcd04 | 1256 | distance_to_best[0] = distance_to_best[2]; |
4a073c53 | 1257 | if (distance_to_best[3] > 0 && distance_to_best[3] > distance_to_best[0]) |
19ddcd04 | 1258 | distance_to_best[0] = distance_to_best[3]; |
4a073c53 | 1259 | |
19ddcd04 MD |
1260 | /* |
1261 | * If our worse distance is better than overall, | |
1262 | * we become new best candidate. | |
1263 | */ | |
1264 | if (distance_to_best[0] < overall_best_distance) { | |
1265 | overall_best_distance = distance_to_best[0]; | |
1266 | bitsel[0] = bit_i; | |
1267 | bitsel[1] = bit_j; | |
1268 | } | |
1269 | } | |
1270 | } | |
1271 | ||
1272 | dbg_printf("2 dimensions pool bit selection: (%u,%u)\n", bitsel[0], bitsel[1]); | |
1273 | ||
1274 | /* Return our bit selection */ | |
1275 | _bitsel[0] = bitsel[0]; | |
1276 | _bitsel[1] = bitsel[1]; | |
1277 | } | |
1278 | ||
48cbe001 MD |
1279 | static |
1280 | unsigned int find_nearest_type_index(unsigned int type_index, | |
1281 | unsigned int nr_nodes) | |
1282 | { | |
1283 | const struct cds_ja_type *type; | |
1284 | ||
1285 | assert(type_index != NODE_INDEX_NULL); | |
1286 | if (nr_nodes == 0) | |
1287 | return NODE_INDEX_NULL; | |
1288 | for (;;) { | |
1289 | type = &ja_types[type_index]; | |
1290 | if (nr_nodes < type->min_child) | |
1291 | type_index--; | |
1292 | else if (nr_nodes > type->max_child) | |
1293 | type_index++; | |
1294 | else | |
1295 | break; | |
1296 | } | |
1297 | return type_index; | |
1298 | } | |
1299 | ||
7a0b2331 MD |
1300 | /* |
1301 | * ja_node_recompact_add: recompact a node, adding a new child. | |
2e313670 | 1302 | * Return 0 on success, -EAGAIN if need to retry, or other negative |
5a9a87dd | 1303 | * error value otherwise. |
7a0b2331 MD |
1304 | */ |
1305 | static | |
2e313670 MD |
1306 | int ja_node_recompact(enum ja_recompact mode, |
1307 | struct cds_ja *ja, | |
e1db2db5 | 1308 | unsigned int old_type_index, |
d96bfb0d | 1309 | const struct cds_ja_type *old_type, |
b4540e8a | 1310 | struct cds_ja_inode *old_node, |
5a9a87dd | 1311 | struct cds_ja_shadow_node *shadow_node, |
3d8fe307 | 1312 | struct cds_ja_inode_flag **old_node_flag_ptr, uint8_t n, |
af3cbd45 | 1313 | struct cds_ja_inode_flag *child_node_flag, |
48cbe001 MD |
1314 | struct cds_ja_inode_flag **nullify_node_flag_ptr, |
1315 | int level) | |
7a0b2331 | 1316 | { |
e1db2db5 | 1317 | unsigned int new_type_index; |
b4540e8a | 1318 | struct cds_ja_inode *new_node; |
af3cbd45 | 1319 | struct cds_ja_shadow_node *new_shadow_node = NULL; |
d96bfb0d | 1320 | const struct cds_ja_type *new_type; |
3d8fe307 | 1321 | struct cds_ja_inode_flag *new_node_flag, *old_node_flag; |
7a0b2331 | 1322 | int ret; |
f07b240f | 1323 | int fallback = 0; |
7a0b2331 | 1324 | |
3d8fe307 MD |
1325 | old_node_flag = *old_node_flag_ptr; |
1326 | ||
48cbe001 MD |
1327 | /* |
1328 | * Need to find nearest type index even for ADD_SAME, because | |
1329 | * this recompaction, when applied to linear nodes, will garbage | |
1330 | * collect dummy (NULL) entries, and can therefore cause a few | |
1331 | * linear representations to be skipped. | |
1332 | */ | |
2e313670 | 1333 | switch (mode) { |
19ddcd04 | 1334 | case JA_RECOMPACT_ADD_SAME: |
48cbe001 MD |
1335 | new_type_index = find_nearest_type_index(old_type_index, |
1336 | shadow_node->nr_child + 1); | |
1337 | dbg_printf("Recompact for node with %u children\n", | |
1338 | shadow_node->nr_child + 1); | |
2e313670 | 1339 | break; |
19ddcd04 | 1340 | case JA_RECOMPACT_ADD_NEXT: |
2e313670 MD |
1341 | if (!shadow_node || old_type_index == NODE_INDEX_NULL) { |
1342 | new_type_index = 0; | |
48cbe001 | 1343 | dbg_printf("Recompact for NULL\n"); |
2e313670 | 1344 | } else { |
48cbe001 MD |
1345 | new_type_index = find_nearest_type_index(old_type_index, |
1346 | shadow_node->nr_child + 1); | |
1347 | dbg_printf("Recompact for node with %u children\n", | |
1348 | shadow_node->nr_child + 1); | |
2e313670 MD |
1349 | } |
1350 | break; | |
1351 | case JA_RECOMPACT_DEL: | |
48cbe001 MD |
1352 | new_type_index = find_nearest_type_index(old_type_index, |
1353 | shadow_node->nr_child - 1); | |
1354 | dbg_printf("Recompact for node with %u children\n", | |
1355 | shadow_node->nr_child - 1); | |
2e313670 MD |
1356 | break; |
1357 | default: | |
1358 | assert(0); | |
7a0b2331 | 1359 | } |
a2a7ff59 | 1360 | |
f07b240f | 1361 | retry: /* for fallback */ |
582a6ade MD |
1362 | dbg_printf("Recompact from type %d to type %d\n", |
1363 | old_type_index, new_type_index); | |
7a0b2331 | 1364 | new_type = &ja_types[new_type_index]; |
2e313670 | 1365 | if (new_type_index != NODE_INDEX_NULL) { |
354981c2 | 1366 | new_node = alloc_cds_ja_node(ja, new_type); |
2e313670 MD |
1367 | if (!new_node) |
1368 | return -ENOMEM; | |
b1a90ce3 MD |
1369 | |
1370 | if (new_type->type_class == RCU_JA_POOL) { | |
1371 | switch (new_type->nr_pool_order) { | |
1372 | case 1: | |
1373 | { | |
19ddcd04 MD |
1374 | unsigned int node_distrib_bitsel; |
1375 | ||
b1a90ce3 MD |
1376 | node_distrib_bitsel = |
1377 | ja_node_sum_distribution_1d(mode, ja, | |
1378 | old_type_index, old_type, | |
1379 | old_node, shadow_node, | |
1380 | n, child_node_flag, | |
1381 | nullify_node_flag_ptr); | |
1382 | assert(!((unsigned long) new_node & JA_POOL_1D_MASK)); | |
1383 | new_node_flag = ja_node_flag_pool_1d(new_node, | |
1384 | new_type_index, node_distrib_bitsel); | |
1385 | break; | |
1386 | } | |
1387 | case 2: | |
1388 | { | |
19ddcd04 MD |
1389 | unsigned int node_distrib_bitsel[2]; |
1390 | ||
1391 | ja_node_sum_distribution_2d(mode, ja, | |
1392 | old_type_index, old_type, | |
1393 | old_node, shadow_node, | |
1394 | n, child_node_flag, | |
1395 | nullify_node_flag_ptr, | |
1396 | node_distrib_bitsel); | |
b1a90ce3 MD |
1397 | assert(!((unsigned long) new_node & JA_POOL_1D_MASK)); |
1398 | assert(!((unsigned long) new_node & JA_POOL_2D_MASK)); | |
19ddcd04 MD |
1399 | new_node_flag = ja_node_flag_pool_2d(new_node, |
1400 | new_type_index, node_distrib_bitsel); | |
b1a90ce3 MD |
1401 | break; |
1402 | } | |
1403 | default: | |
1404 | assert(0); | |
1405 | } | |
1406 | } else { | |
1407 | new_node_flag = ja_node_flag(new_node, new_type_index); | |
1408 | } | |
1409 | ||
2e313670 | 1410 | dbg_printf("Recompact inherit lock from %p\n", shadow_node); |
48cbe001 | 1411 | new_shadow_node = rcuja_shadow_set(ja->ht, new_node_flag, shadow_node, ja, level); |
2e313670 | 1412 | if (!new_shadow_node) { |
354981c2 | 1413 | free_cds_ja_node(ja, new_node); |
2e313670 MD |
1414 | return -ENOMEM; |
1415 | } | |
1416 | if (fallback) | |
1417 | new_shadow_node->fallback_removal_count = | |
1418 | JA_FALLBACK_REMOVAL_COUNT; | |
1419 | } else { | |
1420 | new_node = NULL; | |
1421 | new_node_flag = NULL; | |
e1db2db5 | 1422 | } |
11c5e016 | 1423 | |
19ddcd04 | 1424 | assert(mode != JA_RECOMPACT_ADD_NEXT || old_type->type_class != RCU_JA_PIGEON); |
2e313670 MD |
1425 | |
1426 | if (new_type_index == NODE_INDEX_NULL) | |
1427 | goto skip_copy; | |
1428 | ||
11c5e016 MD |
1429 | switch (old_type->type_class) { |
1430 | case RCU_JA_LINEAR: | |
1431 | { | |
1432 | uint8_t nr_child = | |
1433 | ja_linear_node_get_nr_child(old_type, old_node); | |
1434 | unsigned int i; | |
1435 | ||
1436 | for (i = 0; i < nr_child; i++) { | |
b4540e8a | 1437 | struct cds_ja_inode_flag *iter; |
11c5e016 MD |
1438 | uint8_t v; |
1439 | ||
1440 | ja_linear_node_get_ith_pos(old_type, old_node, i, &v, &iter); | |
1441 | if (!iter) | |
1442 | continue; | |
af3cbd45 | 1443 | if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter) |
2e313670 | 1444 | continue; |
b1a90ce3 | 1445 | ret = _ja_node_set_nth(new_type, new_node, new_node_flag, |
f07b240f | 1446 | new_shadow_node, |
11c5e016 | 1447 | v, iter); |
f07b240f MD |
1448 | if (new_type->type_class == RCU_JA_POOL && ret) { |
1449 | goto fallback_toosmall; | |
1450 | } | |
11c5e016 MD |
1451 | assert(!ret); |
1452 | } | |
1453 | break; | |
1454 | } | |
1455 | case RCU_JA_POOL: | |
1456 | { | |
1457 | unsigned int pool_nr; | |
1458 | ||
1459 | for (pool_nr = 0; pool_nr < (1U << old_type->nr_pool_order); pool_nr++) { | |
b4540e8a | 1460 | struct cds_ja_inode *pool = |
11c5e016 MD |
1461 | ja_pool_node_get_ith_pool(old_type, |
1462 | old_node, pool_nr); | |
1463 | uint8_t nr_child = | |
1464 | ja_linear_node_get_nr_child(old_type, pool); | |
1465 | unsigned int j; | |
1466 | ||
1467 | for (j = 0; j < nr_child; j++) { | |
b4540e8a | 1468 | struct cds_ja_inode_flag *iter; |
11c5e016 MD |
1469 | uint8_t v; |
1470 | ||
1471 | ja_linear_node_get_ith_pos(old_type, pool, | |
1472 | j, &v, &iter); | |
1473 | if (!iter) | |
1474 | continue; | |
af3cbd45 | 1475 | if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter) |
2e313670 | 1476 | continue; |
b1a90ce3 | 1477 | ret = _ja_node_set_nth(new_type, new_node, new_node_flag, |
f07b240f | 1478 | new_shadow_node, |
11c5e016 | 1479 | v, iter); |
f07b240f MD |
1480 | if (new_type->type_class == RCU_JA_POOL |
1481 | && ret) { | |
1482 | goto fallback_toosmall; | |
1483 | } | |
11c5e016 MD |
1484 | assert(!ret); |
1485 | } | |
1486 | } | |
1487 | break; | |
7a0b2331 | 1488 | } |
a2a7ff59 | 1489 | case RCU_JA_NULL: |
19ddcd04 | 1490 | assert(mode == JA_RECOMPACT_ADD_NEXT); |
a2a7ff59 | 1491 | break; |
11c5e016 | 1492 | case RCU_JA_PIGEON: |
2e313670 | 1493 | { |
2e313670 MD |
1494 | unsigned int i; |
1495 | ||
1496 | assert(mode == JA_RECOMPACT_DEL); | |
48cbe001 | 1497 | for (i = 0; i < JA_ENTRY_PER_NODE; i++) { |
2e313670 MD |
1498 | struct cds_ja_inode_flag *iter; |
1499 | ||
1500 | iter = ja_pigeon_node_get_ith_pos(old_type, old_node, i); | |
1501 | if (!iter) | |
1502 | continue; | |
af3cbd45 | 1503 | if (mode == JA_RECOMPACT_DEL && *nullify_node_flag_ptr == iter) |
2e313670 | 1504 | continue; |
b1a90ce3 | 1505 | ret = _ja_node_set_nth(new_type, new_node, new_node_flag, |
2e313670 MD |
1506 | new_shadow_node, |
1507 | i, iter); | |
1508 | if (new_type->type_class == RCU_JA_POOL && ret) { | |
1509 | goto fallback_toosmall; | |
1510 | } | |
1511 | assert(!ret); | |
1512 | } | |
1513 | break; | |
1514 | } | |
11c5e016 MD |
1515 | default: |
1516 | assert(0); | |
5a9a87dd | 1517 | ret = -EINVAL; |
f07b240f | 1518 | goto end; |
11c5e016 | 1519 | } |
2e313670 | 1520 | skip_copy: |
11c5e016 | 1521 | |
19ddcd04 | 1522 | if (mode == JA_RECOMPACT_ADD_NEXT || mode == JA_RECOMPACT_ADD_SAME) { |
2e313670 | 1523 | /* add node */ |
b1a90ce3 | 1524 | ret = _ja_node_set_nth(new_type, new_node, new_node_flag, |
2e313670 MD |
1525 | new_shadow_node, |
1526 | n, child_node_flag); | |
7b413155 MD |
1527 | if (new_type->type_class == RCU_JA_POOL && ret) { |
1528 | goto fallback_toosmall; | |
1529 | } | |
2e313670 MD |
1530 | assert(!ret); |
1531 | } | |
19ddcd04 MD |
1532 | |
1533 | if (fallback) { | |
1534 | dbg_printf("Using fallback for %u children, node type index: %u, mode %s\n", | |
1535 | new_shadow_node->nr_child, old_type_index, mode == JA_RECOMPACT_ADD_NEXT ? "add_next" : | |
1536 | (mode == JA_RECOMPACT_DEL ? "del" : "add_same")); | |
354981c2 | 1537 | uatomic_inc(&ja->node_fallback_count_distribution[new_shadow_node->nr_child]); |
19ddcd04 MD |
1538 | } |
1539 | ||
3d8fe307 MD |
1540 | /* Return pointer to new recompacted node through old_node_flag_ptr */ |
1541 | *old_node_flag_ptr = new_node_flag; | |
a2a7ff59 | 1542 | if (old_node) { |
2e313670 MD |
1543 | int flags; |
1544 | ||
1545 | flags = RCUJA_SHADOW_CLEAR_FREE_NODE; | |
1546 | /* | |
1547 | * It is OK to free the lock associated with a node | |
1548 | * going to NULL, since we are holding the parent lock. | |
1549 | * This synchronizes removal with re-add of that node. | |
1550 | */ | |
1551 | if (new_type_index == NODE_INDEX_NULL) | |
48cbe001 | 1552 | flags |= RCUJA_SHADOW_CLEAR_FREE_LOCK; |
3d8fe307 | 1553 | ret = rcuja_shadow_clear(ja->ht, old_node_flag, shadow_node, |
2e313670 | 1554 | flags); |
a2a7ff59 MD |
1555 | assert(!ret); |
1556 | } | |
5a9a87dd MD |
1557 | |
1558 | ret = 0; | |
f07b240f | 1559 | end: |
5a9a87dd | 1560 | return ret; |
f07b240f MD |
1561 | |
1562 | fallback_toosmall: | |
1563 | /* fallback if next pool is too small */ | |
af3cbd45 | 1564 | assert(new_shadow_node); |
3d8fe307 | 1565 | ret = rcuja_shadow_clear(ja->ht, new_node_flag, new_shadow_node, |
f07b240f MD |
1566 | RCUJA_SHADOW_CLEAR_FREE_NODE); |
1567 | assert(!ret); | |
1568 | ||
19ddcd04 MD |
1569 | switch (mode) { |
1570 | case JA_RECOMPACT_ADD_SAME: | |
1571 | /* | |
1572 | * JA_RECOMPACT_ADD_SAME is only triggered if a linear | |
1573 | * node within a pool has unused entries. It should | |
1574 | * therefore _never_ be too small. | |
1575 | */ | |
4a073c53 | 1576 | assert(0); |
4cde8267 MD |
1577 | |
1578 | /* Fall-through */ | |
19ddcd04 MD |
1579 | case JA_RECOMPACT_ADD_NEXT: |
1580 | { | |
1581 | const struct cds_ja_type *next_type; | |
1582 | ||
1583 | /* | |
1584 | * Recompaction attempt on add failed. Should only | |
1585 | * happen if target node type is pool. Caused by | |
1586 | * hard-to-split distribution. Recompact using the next | |
1587 | * distribution size. | |
1588 | */ | |
1589 | assert(new_type->type_class == RCU_JA_POOL); | |
1590 | next_type = &ja_types[new_type_index + 1]; | |
1591 | /* | |
1592 | * Try going to the next pool size if our population | |
1593 | * fits within its range. This is not flagged as a | |
1594 | * fallback. | |
1595 | */ | |
1596 | if (shadow_node->nr_child + 1 >= next_type->min_child | |
1597 | && shadow_node->nr_child + 1 <= next_type->max_child) { | |
1598 | new_type_index++; | |
1599 | goto retry; | |
1600 | } else { | |
1601 | new_type_index++; | |
1602 | dbg_printf("Add fallback to type %d\n", new_type_index); | |
1603 | uatomic_inc(&ja->nr_fallback); | |
1604 | fallback = 1; | |
1605 | goto retry; | |
1606 | } | |
1607 | break; | |
1608 | } | |
1609 | case JA_RECOMPACT_DEL: | |
1610 | /* | |
1611 | * Recompaction attempt on delete failed. Should only | |
1612 | * happen if target node type is pool. This is caused by | |
1613 | * a hard-to-split distribution. Recompact on same node | |
1614 | * size, but flag current node as "fallback" to ensure | |
1615 | * we don't attempt recompaction before some activity | |
1616 | * has reshuffled our node. | |
1617 | */ | |
1618 | assert(new_type->type_class == RCU_JA_POOL); | |
1619 | new_type_index = old_type_index; | |
1620 | dbg_printf("Delete fallback keeping type %d\n", new_type_index); | |
1621 | uatomic_inc(&ja->nr_fallback); | |
1622 | fallback = 1; | |
1623 | goto retry; | |
1624 | default: | |
1625 | assert(0); | |
1626 | return -EINVAL; | |
1627 | } | |
1628 | ||
1629 | /* | |
1630 | * Last resort fallback: pigeon. | |
1631 | */ | |
f07b240f MD |
1632 | new_type_index = (1UL << JA_TYPE_BITS) - 1; |
1633 | dbg_printf("Fallback to type %d\n", new_type_index); | |
1634 | uatomic_inc(&ja->nr_fallback); | |
1635 | fallback = 1; | |
1636 | goto retry; | |
7a0b2331 MD |
1637 | } |
1638 | ||
5a9a87dd | 1639 | /* |
2e313670 | 1640 | * Return 0 on success, -EAGAIN if need to retry, or other negative |
5a9a87dd MD |
1641 | * error value otherwise. |
1642 | */ | |
7a0b2331 | 1643 | static |
d96bfb0d | 1644 | int ja_node_set_nth(struct cds_ja *ja, |
b4540e8a | 1645 | struct cds_ja_inode_flag **node_flag, uint8_t n, |
5a9a87dd | 1646 | struct cds_ja_inode_flag *child_node_flag, |
48cbe001 MD |
1647 | struct cds_ja_shadow_node *shadow_node, |
1648 | int level) | |
7a0b2331 MD |
1649 | { |
1650 | int ret; | |
e1db2db5 | 1651 | unsigned int type_index; |
d96bfb0d | 1652 | const struct cds_ja_type *type; |
b4540e8a | 1653 | struct cds_ja_inode *node; |
7a0b2331 | 1654 | |
a2a7ff59 MD |
1655 | dbg_printf("ja_node_set_nth for n=%u, node %p, shadow %p\n", |
1656 | (unsigned int) n, ja_node_ptr(*node_flag), shadow_node); | |
1657 | ||
e1db2db5 MD |
1658 | node = ja_node_ptr(*node_flag); |
1659 | type_index = ja_node_type(*node_flag); | |
1660 | type = &ja_types[type_index]; | |
b1a90ce3 | 1661 | ret = _ja_node_set_nth(type, node, *node_flag, shadow_node, |
e1db2db5 | 1662 | n, child_node_flag); |
2e313670 MD |
1663 | switch (ret) { |
1664 | case -ENOSPC: | |
19ddcd04 MD |
1665 | /* Not enough space in node, need to recompact to next type. */ |
1666 | ret = ja_node_recompact(JA_RECOMPACT_ADD_NEXT, ja, type_index, type, node, | |
48cbe001 | 1667 | shadow_node, node_flag, n, child_node_flag, NULL, level); |
2e313670 MD |
1668 | break; |
1669 | case -ERANGE: | |
1670 | /* Node needs to be recompacted. */ | |
19ddcd04 | 1671 | ret = ja_node_recompact(JA_RECOMPACT_ADD_SAME, ja, type_index, type, node, |
48cbe001 | 1672 | shadow_node, node_flag, n, child_node_flag, NULL, level); |
2e313670 MD |
1673 | break; |
1674 | } | |
1675 | return ret; | |
1676 | } | |
1677 | ||
1678 | /* | |
1679 | * Return 0 on success, -EAGAIN if need to retry, or other negative | |
1680 | * error value otherwise. | |
1681 | */ | |
1682 | static | |
af3cbd45 MD |
1683 | int ja_node_clear_ptr(struct cds_ja *ja, |
1684 | struct cds_ja_inode_flag **node_flag_ptr, /* Pointer to location to nullify */ | |
1685 | struct cds_ja_inode_flag **parent_node_flag_ptr, /* Address of parent ptr in its parent */ | |
1686 | struct cds_ja_shadow_node *shadow_node, /* of parent */ | |
48cbe001 | 1687 | uint8_t n, int level) |
2e313670 MD |
1688 | { |
1689 | int ret; | |
1690 | unsigned int type_index; | |
1691 | const struct cds_ja_type *type; | |
1692 | struct cds_ja_inode *node; | |
1693 | ||
af3cbd45 MD |
1694 | dbg_printf("ja_node_clear_ptr for node %p, shadow %p, target ptr %p\n", |
1695 | ja_node_ptr(*parent_node_flag_ptr), shadow_node, node_flag_ptr); | |
2e313670 | 1696 | |
af3cbd45 MD |
1697 | node = ja_node_ptr(*parent_node_flag_ptr); |
1698 | type_index = ja_node_type(*parent_node_flag_ptr); | |
2e313670 | 1699 | type = &ja_types[type_index]; |
19ddcd04 | 1700 | ret = _ja_node_clear_ptr(type, node, *parent_node_flag_ptr, shadow_node, node_flag_ptr, n); |
2e313670 | 1701 | if (ret == -EFBIG) { |
19ddcd04 | 1702 | /* Should try recompaction. */ |
2e313670 | 1703 | ret = ja_node_recompact(JA_RECOMPACT_DEL, ja, type_index, type, node, |
af3cbd45 | 1704 | shadow_node, parent_node_flag_ptr, n, NULL, |
48cbe001 | 1705 | node_flag_ptr, level); |
7a0b2331 MD |
1706 | } |
1707 | return ret; | |
1708 | } | |
be9a7474 | 1709 | |
af3cbd45 | 1710 | struct cds_hlist_head cds_ja_lookup(struct cds_ja *ja, uint64_t key) |
b4540e8a | 1711 | { |
41975c12 MD |
1712 | unsigned int tree_depth, i; |
1713 | struct cds_ja_inode_flag *node_flag; | |
af3cbd45 | 1714 | struct cds_hlist_head head = { NULL }; |
41975c12 MD |
1715 | |
1716 | if (caa_unlikely(key > ja->key_max)) | |
af3cbd45 | 1717 | return head; |
41975c12 | 1718 | tree_depth = ja->tree_depth; |
5a9a87dd | 1719 | node_flag = rcu_dereference(ja->root); |
41975c12 | 1720 | |
5a9a87dd MD |
1721 | /* level 0: root node */ |
1722 | if (!ja_node_ptr(node_flag)) | |
af3cbd45 | 1723 | return head; |
5a9a87dd MD |
1724 | |
1725 | for (i = 1; i < tree_depth; i++) { | |
79b41067 MD |
1726 | uint8_t iter_key; |
1727 | ||
1728 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1))); | |
48cbe001 | 1729 | node_flag = ja_node_get_nth(node_flag, NULL, iter_key); |
582a6ade MD |
1730 | dbg_printf("cds_ja_lookup iter key lookup %u finds node_flag %p\n", |
1731 | (unsigned int) iter_key, node_flag); | |
41975c12 | 1732 | if (!ja_node_ptr(node_flag)) |
af3cbd45 | 1733 | return head; |
41975c12 MD |
1734 | } |
1735 | ||
5a9a87dd | 1736 | /* Last level lookup succeded. We got an actual match. */ |
af3cbd45 MD |
1737 | head.next = (struct cds_hlist_node *) node_flag; |
1738 | return head; | |
5a9a87dd MD |
1739 | } |
1740 | ||
3c52f0f9 MD |
1741 | /* |
1742 | * cds_ja_lookup_lower_equal() may need to retry if a concurrent removal | |
1743 | * causes failure to find the previous node. | |
1744 | */ | |
291b2543 MD |
1745 | struct cds_hlist_head cds_ja_lookup_lower_equal(struct cds_ja *ja, uint64_t key) |
1746 | { | |
1747 | int tree_depth, level; | |
1748 | struct cds_ja_inode_flag *node_flag, *cur_node_depth[JA_MAX_DEPTH]; | |
1749 | struct cds_hlist_head head = { NULL }; | |
1750 | ||
1751 | if (caa_unlikely(key > ja->key_max || !key)) | |
1752 | return head; | |
1753 | ||
3c52f0f9 | 1754 | retry: |
291b2543 MD |
1755 | memset(cur_node_depth, 0, sizeof(cur_node_depth)); |
1756 | tree_depth = ja->tree_depth; | |
1757 | node_flag = rcu_dereference(ja->root); | |
1758 | cur_node_depth[0] = node_flag; | |
1759 | ||
1760 | /* level 0: root node */ | |
1761 | if (!ja_node_ptr(node_flag)) | |
1762 | return head; | |
1763 | ||
1764 | for (level = 1; level < tree_depth; level++) { | |
1765 | uint8_t iter_key; | |
1766 | ||
1767 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - level - 1))); | |
1768 | node_flag = ja_node_get_nth(node_flag, NULL, iter_key); | |
1769 | if (!ja_node_ptr(node_flag)) | |
1770 | break; | |
1771 | cur_node_depth[level] = node_flag; | |
1772 | dbg_printf("cds_ja_lookup iter key lookup %u finds node_flag %p\n", | |
1773 | (unsigned int) iter_key, node_flag); | |
1774 | } | |
1775 | ||
1776 | if (level == tree_depth) { | |
1777 | /* Last level lookup succeded. We got an equal match. */ | |
1778 | head.next = (struct cds_hlist_node *) node_flag; | |
1779 | return head; | |
1780 | } | |
1781 | ||
1782 | /* | |
1783 | * Find highest value left of current node. | |
1784 | * Current node is cur_node_depth[level]. | |
1785 | * Start at current level. If we cannot find any key left of | |
1786 | * ours, go one level up, seek highest value left of current | |
1787 | * (recursively), and when we find one, get the rightmost child | |
1788 | * of its rightmost child (recursively). | |
1789 | */ | |
1790 | for (; level > 0; level--) { | |
1791 | uint8_t iter_key; | |
1792 | ||
1793 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - level - 1))); | |
1794 | node_flag = ja_node_get_left(cur_node_depth[level - 1], | |
1795 | iter_key); | |
1796 | /* If found left sibling, find rightmost child. */ | |
1797 | if (ja_node_ptr(node_flag)) | |
1798 | break; | |
1799 | } | |
1800 | ||
1801 | if (!level) { | |
1802 | /* Reached the root and could not find a left sibling. */ | |
1803 | return head; | |
1804 | } | |
1805 | ||
1806 | level++; | |
3c52f0f9 MD |
1807 | |
1808 | /* | |
1809 | * From this point, we should be able to find a "lower than" | |
1810 | * match. The only reason why we could fail to find such a match | |
1811 | * would be due to a concurrent removal of the branch that | |
1812 | * contains the match. If this happens, we have no choice but to | |
1813 | * retry the entire lookup. Indeed, just because we reached a | |
1814 | * dead-end due to concurrent removal of the branch does not | |
1815 | * mean that other match don't exist. However, this requires | |
1816 | * going up into the tree, hence the retry. | |
1817 | */ | |
1818 | ||
291b2543 MD |
1819 | /* Find rightmost child of rightmost child (recursively). */ |
1820 | for (; level < tree_depth; level++) { | |
1821 | node_flag = ja_node_get_rightmost(node_flag); | |
1822 | /* If found left sibling, find rightmost child. */ | |
1823 | if (!ja_node_ptr(node_flag)) | |
1824 | break; | |
1825 | } | |
1826 | ||
3c52f0f9 MD |
1827 | if (level != tree_depth) { |
1828 | /* | |
1829 | * We did not get a match. Caused by concurrent removal. | |
1830 | * We need to retry the entire lookup. | |
1831 | */ | |
1832 | goto retry; | |
291b2543 MD |
1833 | } |
1834 | ||
3c52f0f9 MD |
1835 | /* Last level lookup succeded. We got a "lower than" match. */ |
1836 | head.next = (struct cds_hlist_node *) node_flag; | |
291b2543 MD |
1837 | return head; |
1838 | } | |
1839 | ||
5a9a87dd MD |
1840 | /* |
1841 | * We reached an unpopulated node. Create it and the children we need, | |
1842 | * and then attach the entire branch to the current node. This may | |
1843 | * trigger recompaction of the current node. Locks needed: node lock | |
1844 | * (for add), and, possibly, parent node lock (to update pointer due to | |
1845 | * node recompaction). | |
1846 | * | |
1847 | * First take node lock, check if recompaction is needed, then take | |
1848 | * parent lock (if needed). Then we can proceed to create the new | |
1849 | * branch. Publish the new branch, and release locks. | |
1850 | * TODO: we currently always take the parent lock even when not needed. | |
1851 | */ | |
1852 | static | |
1853 | int ja_attach_node(struct cds_ja *ja, | |
b0ca2d21 | 1854 | struct cds_ja_inode_flag **attach_node_flag_ptr, |
b62a8d0c | 1855 | struct cds_ja_inode_flag *attach_node_flag, |
48cbe001 MD |
1856 | struct cds_ja_inode_flag *parent_attach_node_flag, |
1857 | struct cds_ja_inode_flag **old_node_flag_ptr, | |
1858 | struct cds_ja_inode_flag *old_node_flag, | |
5a9a87dd | 1859 | uint64_t key, |
79b41067 | 1860 | unsigned int level, |
5a9a87dd MD |
1861 | struct cds_ja_node *child_node) |
1862 | { | |
1863 | struct cds_ja_shadow_node *shadow_node = NULL, | |
af3cbd45 | 1864 | *parent_shadow_node = NULL; |
5a9a87dd MD |
1865 | struct cds_hlist_head head; |
1866 | struct cds_ja_inode_flag *iter_node_flag, *iter_dest_node_flag; | |
1867 | int ret, i; | |
a2a7ff59 | 1868 | struct cds_ja_inode_flag *created_nodes[JA_MAX_DEPTH]; |
5a9a87dd MD |
1869 | int nr_created_nodes = 0; |
1870 | ||
48cbe001 MD |
1871 | dbg_printf("Attach node at level %u (old_node_flag %p, attach_node_flag_ptr %p attach_node_flag %p, parent_attach_node_flag %p)\n", |
1872 | level, old_node_flag, attach_node_flag_ptr, attach_node_flag, parent_attach_node_flag); | |
a2a7ff59 | 1873 | |
48cbe001 MD |
1874 | assert(!old_node_flag); |
1875 | if (attach_node_flag) { | |
1876 | shadow_node = rcuja_shadow_lookup_lock(ja->ht, attach_node_flag); | |
1877 | if (!shadow_node) { | |
1878 | ret = -EAGAIN; | |
1879 | goto end; | |
1880 | } | |
5a9a87dd | 1881 | } |
48cbe001 | 1882 | if (parent_attach_node_flag) { |
5a9a87dd | 1883 | parent_shadow_node = rcuja_shadow_lookup_lock(ja->ht, |
48cbe001 | 1884 | parent_attach_node_flag); |
5a9a87dd | 1885 | if (!parent_shadow_node) { |
2e313670 | 1886 | ret = -EAGAIN; |
5a9a87dd MD |
1887 | goto unlock_shadow; |
1888 | } | |
1889 | } | |
1890 | ||
48cbe001 | 1891 | if (old_node_flag_ptr && ja_node_ptr(*old_node_flag_ptr)) { |
b306a0fe | 1892 | /* |
c112acaa MD |
1893 | * Target node has been updated between RCU lookup and |
1894 | * lock acquisition. We need to re-try lookup and | |
1895 | * attach. | |
1896 | */ | |
1897 | ret = -EAGAIN; | |
1898 | goto unlock_parent; | |
1899 | } | |
1900 | ||
9be99d4a MD |
1901 | /* |
1902 | * Perform a lookup query to handle the case where | |
1903 | * old_node_flag_ptr is NULL. We cannot use it to check if the | |
1904 | * node has been populated between RCU lookup and mutex | |
1905 | * acquisition. | |
1906 | */ | |
1907 | if (!old_node_flag_ptr) { | |
1908 | uint8_t iter_key; | |
1909 | struct cds_ja_inode_flag *lookup_node_flag; | |
1910 | struct cds_ja_inode_flag **lookup_node_flag_ptr; | |
1911 | ||
1912 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (ja->tree_depth - level))); | |
1913 | lookup_node_flag = ja_node_get_nth(attach_node_flag, | |
1914 | &lookup_node_flag_ptr, | |
1915 | iter_key); | |
1916 | if (lookup_node_flag) { | |
1917 | ret = -EEXIST; | |
1918 | goto unlock_parent; | |
1919 | } | |
1920 | } | |
1921 | ||
c112acaa | 1922 | if (attach_node_flag_ptr && ja_node_ptr(*attach_node_flag_ptr) != |
b62a8d0c | 1923 | ja_node_ptr(attach_node_flag)) { |
c112acaa MD |
1924 | /* |
1925 | * Target node has been updated between RCU lookup and | |
1926 | * lock acquisition. We need to re-try lookup and | |
1927 | * attach. | |
b306a0fe MD |
1928 | */ |
1929 | ret = -EAGAIN; | |
1930 | goto unlock_parent; | |
1931 | } | |
1932 | ||
a2a7ff59 | 1933 | /* Create new branch, starting from bottom */ |
5a9a87dd MD |
1934 | CDS_INIT_HLIST_HEAD(&head); |
1935 | cds_hlist_add_head_rcu(&child_node->list, &head); | |
a2a7ff59 | 1936 | iter_node_flag = (struct cds_ja_inode_flag *) head.next; |
5a9a87dd | 1937 | |
48cbe001 | 1938 | for (i = ja->tree_depth - 1; i >= (int) level; i--) { |
79b41067 MD |
1939 | uint8_t iter_key; |
1940 | ||
48cbe001 | 1941 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (ja->tree_depth - i - 1))); |
79b41067 | 1942 | dbg_printf("branch creation level %d, key %u\n", |
48cbe001 | 1943 | i, (unsigned int) iter_key); |
5a9a87dd MD |
1944 | iter_dest_node_flag = NULL; |
1945 | ret = ja_node_set_nth(ja, &iter_dest_node_flag, | |
79b41067 | 1946 | iter_key, |
5a9a87dd | 1947 | iter_node_flag, |
48cbe001 | 1948 | NULL, i); |
9be99d4a MD |
1949 | if (ret) { |
1950 | dbg_printf("branch creation error %d\n", ret); | |
5a9a87dd | 1951 | goto check_error; |
9be99d4a | 1952 | } |
5a9a87dd MD |
1953 | created_nodes[nr_created_nodes++] = iter_dest_node_flag; |
1954 | iter_node_flag = iter_dest_node_flag; | |
1955 | } | |
48cbe001 | 1956 | assert(level > 0); |
5a9a87dd | 1957 | |
48cbe001 MD |
1958 | /* Publish branch */ |
1959 | if (level == 1) { | |
1960 | /* | |
1961 | * Attaching to root node. | |
1962 | */ | |
1963 | rcu_assign_pointer(ja->root, iter_node_flag); | |
1964 | } else { | |
79b41067 MD |
1965 | uint8_t iter_key; |
1966 | ||
1967 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (ja->tree_depth - level))); | |
48cbe001 MD |
1968 | dbg_printf("publish branch at level %d, key %u\n", |
1969 | level - 1, (unsigned int) iter_key); | |
a2a7ff59 | 1970 | /* We need to use set_nth on the previous level. */ |
48cbe001 | 1971 | iter_dest_node_flag = attach_node_flag; |
a2a7ff59 | 1972 | ret = ja_node_set_nth(ja, &iter_dest_node_flag, |
79b41067 | 1973 | iter_key, |
a2a7ff59 | 1974 | iter_node_flag, |
48cbe001 | 1975 | shadow_node, level - 1); |
9be99d4a MD |
1976 | if (ret) { |
1977 | dbg_printf("branch publish error %d\n", ret); | |
a2a7ff59 | 1978 | goto check_error; |
9be99d4a | 1979 | } |
48cbe001 MD |
1980 | /* |
1981 | * Attach branch | |
1982 | */ | |
1983 | rcu_assign_pointer(*attach_node_flag_ptr, iter_dest_node_flag); | |
a2a7ff59 MD |
1984 | } |
1985 | ||
5a9a87dd MD |
1986 | /* Success */ |
1987 | ret = 0; | |
1988 | ||
1989 | check_error: | |
1990 | if (ret) { | |
1991 | for (i = 0; i < nr_created_nodes; i++) { | |
1992 | int tmpret; | |
a2a7ff59 MD |
1993 | int flags; |
1994 | ||
1995 | flags = RCUJA_SHADOW_CLEAR_FREE_LOCK; | |
1996 | if (i) | |
1997 | flags |= RCUJA_SHADOW_CLEAR_FREE_NODE; | |
5a9a87dd | 1998 | tmpret = rcuja_shadow_clear(ja->ht, |
3d8fe307 | 1999 | created_nodes[i], |
a2a7ff59 MD |
2000 | NULL, |
2001 | flags); | |
5a9a87dd MD |
2002 | assert(!tmpret); |
2003 | } | |
2004 | } | |
b306a0fe | 2005 | unlock_parent: |
5a9a87dd MD |
2006 | if (parent_shadow_node) |
2007 | rcuja_shadow_unlock(parent_shadow_node); | |
2008 | unlock_shadow: | |
2009 | if (shadow_node) | |
2010 | rcuja_shadow_unlock(shadow_node); | |
2011 | end: | |
2012 | return ret; | |
2013 | } | |
2014 | ||
2015 | /* | |
af3cbd45 MD |
2016 | * Lock the parent containing the hlist head pointer, and add node to list of |
2017 | * duplicates. Failure can happen if concurrent update changes the | |
2018 | * parent before we get the lock. We return -EAGAIN in that case. | |
5a9a87dd MD |
2019 | * Return 0 on success, negative error value on failure. |
2020 | */ | |
2021 | static | |
2022 | int ja_chain_node(struct cds_ja *ja, | |
af3cbd45 | 2023 | struct cds_ja_inode_flag *parent_node_flag, |
fa112799 | 2024 | struct cds_ja_inode_flag **node_flag_ptr, |
c112acaa | 2025 | struct cds_ja_inode_flag *node_flag, |
5a9a87dd MD |
2026 | struct cds_ja_node *node) |
2027 | { | |
2028 | struct cds_ja_shadow_node *shadow_node; | |
fa112799 | 2029 | int ret = 0; |
5a9a87dd | 2030 | |
3d8fe307 | 2031 | shadow_node = rcuja_shadow_lookup_lock(ja->ht, parent_node_flag); |
b306a0fe | 2032 | if (!shadow_node) { |
2e313670 | 2033 | return -EAGAIN; |
b306a0fe | 2034 | } |
c112acaa | 2035 | if (ja_node_ptr(*node_flag_ptr) != ja_node_ptr(node_flag)) { |
fa112799 MD |
2036 | ret = -EAGAIN; |
2037 | goto end; | |
2038 | } | |
48cbe001 | 2039 | cds_hlist_add_head_rcu(&node->list, (struct cds_hlist_head *) node_flag_ptr); |
fa112799 | 2040 | end: |
5a9a87dd | 2041 | rcuja_shadow_unlock(shadow_node); |
fa112799 | 2042 | return ret; |
5a9a87dd MD |
2043 | } |
2044 | ||
75d573aa MD |
2045 | static |
2046 | int _cds_ja_add(struct cds_ja *ja, uint64_t key, | |
2047 | struct cds_ja_node *new_node, | |
2048 | struct cds_ja_node **unique_node_ret) | |
5a9a87dd MD |
2049 | { |
2050 | unsigned int tree_depth, i; | |
48cbe001 | 2051 | struct cds_ja_inode_flag *attach_node_flag, |
5a9a87dd | 2052 | *parent_node_flag, |
b62a8d0c | 2053 | *parent2_node_flag, |
48cbe001 MD |
2054 | *node_flag, |
2055 | *parent_attach_node_flag; | |
2056 | struct cds_ja_inode_flag **attach_node_flag_ptr, | |
2057 | **parent_node_flag_ptr, | |
2058 | **node_flag_ptr; | |
5a9a87dd MD |
2059 | int ret; |
2060 | ||
b306a0fe | 2061 | if (caa_unlikely(key > ja->key_max)) { |
5a9a87dd | 2062 | return -EINVAL; |
b306a0fe | 2063 | } |
5a9a87dd MD |
2064 | tree_depth = ja->tree_depth; |
2065 | ||
2066 | retry: | |
a2a7ff59 MD |
2067 | dbg_printf("cds_ja_add attempt: key %" PRIu64 ", node %p\n", |
2068 | key, new_node); | |
5a9a87dd | 2069 | parent2_node_flag = NULL; |
b0f74e47 MD |
2070 | parent_node_flag = |
2071 | (struct cds_ja_inode_flag *) &ja->root; /* Use root ptr address as key for mutex */ | |
48cbe001 | 2072 | parent_node_flag_ptr = NULL; |
35170a44 | 2073 | node_flag = rcu_dereference(ja->root); |
48cbe001 | 2074 | node_flag_ptr = &ja->root; |
5a9a87dd MD |
2075 | |
2076 | /* Iterate on all internal levels */ | |
a2a7ff59 | 2077 | for (i = 1; i < tree_depth; i++) { |
79b41067 MD |
2078 | uint8_t iter_key; |
2079 | ||
48cbe001 MD |
2080 | if (!ja_node_ptr(node_flag)) |
2081 | break; | |
2082 | dbg_printf("cds_ja_add iter parent2_node_flag %p parent_node_flag %p node_flag_ptr %p node_flag %p\n", | |
2083 | parent2_node_flag, parent_node_flag, node_flag_ptr, node_flag); | |
79b41067 | 2084 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1))); |
5a9a87dd MD |
2085 | parent2_node_flag = parent_node_flag; |
2086 | parent_node_flag = node_flag; | |
48cbe001 | 2087 | parent_node_flag_ptr = node_flag_ptr; |
5a9a87dd MD |
2088 | node_flag = ja_node_get_nth(node_flag, |
2089 | &node_flag_ptr, | |
79b41067 | 2090 | iter_key); |
5a9a87dd MD |
2091 | } |
2092 | ||
2093 | /* | |
48cbe001 MD |
2094 | * We reached either bottom of tree or internal NULL node, |
2095 | * simply add node to last internal level, or chain it if key is | |
2096 | * already present. | |
5a9a87dd MD |
2097 | */ |
2098 | if (!ja_node_ptr(node_flag)) { | |
48cbe001 MD |
2099 | dbg_printf("cds_ja_add NULL parent2_node_flag %p parent_node_flag %p node_flag_ptr %p node_flag %p\n", |
2100 | parent2_node_flag, parent_node_flag, node_flag_ptr, node_flag); | |
75d573aa | 2101 | |
48cbe001 MD |
2102 | attach_node_flag = parent_node_flag; |
2103 | attach_node_flag_ptr = parent_node_flag_ptr; | |
2104 | parent_attach_node_flag = parent2_node_flag; | |
2105 | ||
b0ca2d21 | 2106 | ret = ja_attach_node(ja, attach_node_flag_ptr, |
b62a8d0c | 2107 | attach_node_flag, |
48cbe001 MD |
2108 | parent_attach_node_flag, |
2109 | node_flag_ptr, | |
2110 | node_flag, | |
2111 | key, i, new_node); | |
5a9a87dd | 2112 | } else { |
75d573aa MD |
2113 | if (unique_node_ret) { |
2114 | *unique_node_ret = (struct cds_ja_node *) ja_node_ptr(node_flag); | |
2115 | return -EEXIST; | |
2116 | } | |
2117 | ||
48cbe001 MD |
2118 | dbg_printf("cds_ja_add duplicate parent2_node_flag %p parent_node_flag %p node_flag_ptr %p node_flag %p\n", |
2119 | parent2_node_flag, parent_node_flag, node_flag_ptr, node_flag); | |
75d573aa | 2120 | |
48cbe001 MD |
2121 | attach_node_flag = node_flag; |
2122 | attach_node_flag_ptr = node_flag_ptr; | |
2123 | parent_attach_node_flag = parent_node_flag; | |
2124 | ||
5a9a87dd | 2125 | ret = ja_chain_node(ja, |
48cbe001 MD |
2126 | parent_attach_node_flag, |
2127 | attach_node_flag_ptr, | |
2128 | attach_node_flag, | |
5a9a87dd MD |
2129 | new_node); |
2130 | } | |
b306a0fe | 2131 | if (ret == -EAGAIN || ret == -EEXIST) |
5a9a87dd | 2132 | goto retry; |
48cbe001 | 2133 | |
5a9a87dd | 2134 | return ret; |
b4540e8a MD |
2135 | } |
2136 | ||
75d573aa MD |
2137 | int cds_ja_add(struct cds_ja *ja, uint64_t key, |
2138 | struct cds_ja_node *new_node) | |
2139 | { | |
2140 | return _cds_ja_add(ja, key, new_node, NULL); | |
2141 | } | |
2142 | ||
2143 | struct cds_ja_node *cds_ja_add_unique(struct cds_ja *ja, uint64_t key, | |
2144 | struct cds_ja_node *new_node) | |
2145 | { | |
2146 | int ret; | |
2147 | struct cds_ja_node *ret_node; | |
2148 | ||
2149 | ret = _cds_ja_add(ja, key, new_node, &ret_node); | |
2150 | if (ret == -EEXIST) | |
2151 | return ret_node; | |
2152 | else | |
2153 | return new_node; | |
2154 | } | |
2155 | ||
af3cbd45 MD |
2156 | /* |
2157 | * Note: there is no need to lookup the pointer address associated with | |
2158 | * each node's nth item after taking the lock: it's already been done by | |
2159 | * cds_ja_del while holding the rcu read-side lock, and our node rules | |
2160 | * ensure that when a match value -> pointer is found in a node, it is | |
2161 | * _NEVER_ changed for that node without recompaction, and recompaction | |
2162 | * reallocates the node. | |
b306a0fe MD |
2163 | * However, when a child is removed from "linear" nodes, its pointer |
2164 | * is set to NULL. We therefore check, while holding the locks, if this | |
2165 | * pointer is NULL, and return -ENOENT to the caller if it is the case. | |
af3cbd45 | 2166 | */ |
35170a44 MD |
2167 | static |
2168 | int ja_detach_node(struct cds_ja *ja, | |
2169 | struct cds_ja_inode_flag **snapshot, | |
af3cbd45 MD |
2170 | struct cds_ja_inode_flag ***snapshot_ptr, |
2171 | uint8_t *snapshot_n, | |
35170a44 MD |
2172 | int nr_snapshot, |
2173 | uint64_t key, | |
2174 | struct cds_ja_node *node) | |
2175 | { | |
af3cbd45 MD |
2176 | struct cds_ja_shadow_node *shadow_nodes[JA_MAX_DEPTH]; |
2177 | struct cds_ja_inode_flag **node_flag_ptr = NULL, | |
2178 | *parent_node_flag = NULL, | |
2179 | **parent_node_flag_ptr = NULL; | |
b62a8d0c | 2180 | struct cds_ja_inode_flag *iter_node_flag; |
4d6ef45e MD |
2181 | int ret, i, nr_shadow = 0, nr_clear = 0, nr_branch = 0; |
2182 | uint8_t n = 0; | |
35170a44 | 2183 | |
4d6ef45e | 2184 | assert(nr_snapshot == ja->tree_depth + 1); |
35170a44 | 2185 | |
af3cbd45 MD |
2186 | /* |
2187 | * From the last internal level node going up, get the node | |
2188 | * lock, check if the node has only one child left. If it is the | |
2189 | * case, we continue iterating upward. When we reach a node | |
2190 | * which has more that one child left, we lock the parent, and | |
2191 | * proceed to the node deletion (removing its children too). | |
2192 | */ | |
4d6ef45e | 2193 | for (i = nr_snapshot - 2; i >= 1; i--) { |
af3cbd45 MD |
2194 | struct cds_ja_shadow_node *shadow_node; |
2195 | ||
2196 | shadow_node = rcuja_shadow_lookup_lock(ja->ht, | |
3d8fe307 | 2197 | snapshot[i]); |
af3cbd45 MD |
2198 | if (!shadow_node) { |
2199 | ret = -EAGAIN; | |
2200 | goto end; | |
2201 | } | |
af3cbd45 | 2202 | shadow_nodes[nr_shadow++] = shadow_node; |
b62a8d0c MD |
2203 | |
2204 | /* | |
2205 | * Check if node has been removed between RCU | |
2206 | * lookup and lock acquisition. | |
2207 | */ | |
2208 | assert(snapshot_ptr[i + 1]); | |
2209 | if (ja_node_ptr(*snapshot_ptr[i + 1]) | |
2210 | != ja_node_ptr(snapshot[i + 1])) { | |
2211 | ret = -ENOENT; | |
2212 | goto end; | |
2213 | } | |
2214 | ||
2215 | assert(shadow_node->nr_child > 0); | |
d810c97f | 2216 | if (shadow_node->nr_child == 1 && i > 1) |
4d6ef45e MD |
2217 | nr_clear++; |
2218 | nr_branch++; | |
af3cbd45 MD |
2219 | if (shadow_node->nr_child > 1 || i == 1) { |
2220 | /* Lock parent and break */ | |
2221 | shadow_node = rcuja_shadow_lookup_lock(ja->ht, | |
3d8fe307 | 2222 | snapshot[i - 1]); |
af3cbd45 MD |
2223 | if (!shadow_node) { |
2224 | ret = -EAGAIN; | |
2225 | goto end; | |
2226 | } | |
2227 | shadow_nodes[nr_shadow++] = shadow_node; | |
b62a8d0c | 2228 | |
c112acaa MD |
2229 | /* |
2230 | * Check if node has been removed between RCU | |
2231 | * lookup and lock acquisition. | |
2232 | */ | |
b62a8d0c MD |
2233 | assert(snapshot_ptr[i]); |
2234 | if (ja_node_ptr(*snapshot_ptr[i]) | |
2235 | != ja_node_ptr(snapshot[i])) { | |
c112acaa MD |
2236 | ret = -ENOENT; |
2237 | goto end; | |
2238 | } | |
2239 | ||
b62a8d0c | 2240 | node_flag_ptr = snapshot_ptr[i + 1]; |
4d6ef45e MD |
2241 | n = snapshot_n[i + 1]; |
2242 | parent_node_flag_ptr = snapshot_ptr[i]; | |
2243 | parent_node_flag = snapshot[i]; | |
c112acaa | 2244 | |
af3cbd45 MD |
2245 | if (i > 1) { |
2246 | /* | |
2247 | * Lock parent's parent, in case we need | |
2248 | * to recompact parent. | |
2249 | */ | |
2250 | shadow_node = rcuja_shadow_lookup_lock(ja->ht, | |
3d8fe307 | 2251 | snapshot[i - 2]); |
af3cbd45 MD |
2252 | if (!shadow_node) { |
2253 | ret = -EAGAIN; | |
2254 | goto end; | |
2255 | } | |
2256 | shadow_nodes[nr_shadow++] = shadow_node; | |
b62a8d0c MD |
2257 | |
2258 | /* | |
2259 | * Check if node has been removed between RCU | |
2260 | * lookup and lock acquisition. | |
2261 | */ | |
2262 | assert(snapshot_ptr[i - 1]); | |
2263 | if (ja_node_ptr(*snapshot_ptr[i - 1]) | |
2264 | != ja_node_ptr(snapshot[i - 1])) { | |
2265 | ret = -ENOENT; | |
2266 | goto end; | |
2267 | } | |
af3cbd45 | 2268 | } |
b62a8d0c | 2269 | |
af3cbd45 MD |
2270 | break; |
2271 | } | |
2272 | } | |
2273 | ||
2274 | /* | |
4d6ef45e MD |
2275 | * At this point, we want to delete all nodes that are about to |
2276 | * be removed from shadow_nodes (except the last one, which is | |
2277 | * either the root or the parent of the upmost node with 1 | |
b62a8d0c MD |
2278 | * child). OK to free lock here, because RCU read lock is held, |
2279 | * and free only performed in call_rcu. | |
af3cbd45 MD |
2280 | */ |
2281 | ||
2282 | for (i = 0; i < nr_clear; i++) { | |
2283 | ret = rcuja_shadow_clear(ja->ht, | |
3d8fe307 | 2284 | shadow_nodes[i]->node_flag, |
af3cbd45 MD |
2285 | shadow_nodes[i], |
2286 | RCUJA_SHADOW_CLEAR_FREE_NODE | |
2287 | | RCUJA_SHADOW_CLEAR_FREE_LOCK); | |
2288 | assert(!ret); | |
2289 | } | |
2290 | ||
2291 | iter_node_flag = parent_node_flag; | |
2292 | /* Remove from parent */ | |
2293 | ret = ja_node_clear_ptr(ja, | |
2294 | node_flag_ptr, /* Pointer to location to nullify */ | |
2295 | &iter_node_flag, /* Old new parent ptr in its parent */ | |
4d6ef45e | 2296 | shadow_nodes[nr_branch - 1], /* of parent */ |
48cbe001 | 2297 | n, nr_branch - 1); |
b306a0fe MD |
2298 | if (ret) |
2299 | goto end; | |
af3cbd45 | 2300 | |
4d6ef45e MD |
2301 | dbg_printf("ja_detach_node: publish %p instead of %p\n", |
2302 | iter_node_flag, *parent_node_flag_ptr); | |
af3cbd45 MD |
2303 | /* Update address of parent ptr in its parent */ |
2304 | rcu_assign_pointer(*parent_node_flag_ptr, iter_node_flag); | |
2305 | ||
2306 | end: | |
2307 | for (i = 0; i < nr_shadow; i++) | |
2308 | rcuja_shadow_unlock(shadow_nodes[i]); | |
35170a44 MD |
2309 | return ret; |
2310 | } | |
2311 | ||
af3cbd45 MD |
2312 | static |
2313 | int ja_unchain_node(struct cds_ja *ja, | |
2314 | struct cds_ja_inode_flag *parent_node_flag, | |
fa112799 | 2315 | struct cds_ja_inode_flag **node_flag_ptr, |
013a6083 | 2316 | struct cds_ja_inode_flag *node_flag, |
af3cbd45 MD |
2317 | struct cds_ja_node *node) |
2318 | { | |
2319 | struct cds_ja_shadow_node *shadow_node; | |
f2758d14 | 2320 | struct cds_hlist_node *hlist_node; |
013a6083 MD |
2321 | struct cds_hlist_head hlist_head; |
2322 | int ret = 0, count = 0, found = 0; | |
af3cbd45 | 2323 | |
3d8fe307 | 2324 | shadow_node = rcuja_shadow_lookup_lock(ja->ht, parent_node_flag); |
af3cbd45 MD |
2325 | if (!shadow_node) |
2326 | return -EAGAIN; | |
013a6083 | 2327 | if (ja_node_ptr(*node_flag_ptr) != ja_node_ptr(node_flag)) { |
fa112799 MD |
2328 | ret = -EAGAIN; |
2329 | goto end; | |
2330 | } | |
013a6083 | 2331 | hlist_head.next = (struct cds_hlist_node *) ja_node_ptr(node_flag); |
af3cbd45 MD |
2332 | /* |
2333 | * Retry if another thread removed all but one of duplicates | |
fa112799 | 2334 | * since check (this check was performed without lock). |
013a6083 MD |
2335 | * Ensure that the node we are about to remove is still in the |
2336 | * list (while holding lock). | |
af3cbd45 | 2337 | */ |
013a6083 | 2338 | cds_hlist_for_each_rcu(hlist_node, &hlist_head) { |
ade342cb MD |
2339 | if (count == 0) { |
2340 | /* FIXME: currently a work-around */ | |
2341 | hlist_node->prev = (struct cds_hlist_node *) node_flag_ptr; | |
2342 | } | |
f2758d14 | 2343 | count++; |
013a6083 MD |
2344 | if (hlist_node == &node->list) |
2345 | found++; | |
f2758d14 | 2346 | } |
013a6083 MD |
2347 | assert(found <= 1); |
2348 | if (!found || count == 1) { | |
af3cbd45 MD |
2349 | ret = -EAGAIN; |
2350 | goto end; | |
2351 | } | |
2352 | cds_hlist_del_rcu(&node->list); | |
ade342cb MD |
2353 | /* |
2354 | * Validate that we indeed removed the node from linked list. | |
2355 | */ | |
2356 | assert(ja_node_ptr(*node_flag_ptr) != (struct cds_ja_inode *) node); | |
af3cbd45 MD |
2357 | end: |
2358 | rcuja_shadow_unlock(shadow_node); | |
2359 | return ret; | |
2360 | } | |
2361 | ||
2362 | /* | |
2363 | * Called with RCU read lock held. | |
2364 | */ | |
35170a44 MD |
2365 | int cds_ja_del(struct cds_ja *ja, uint64_t key, |
2366 | struct cds_ja_node *node) | |
2367 | { | |
2368 | unsigned int tree_depth, i; | |
2369 | struct cds_ja_inode_flag *snapshot[JA_MAX_DEPTH]; | |
af3cbd45 MD |
2370 | struct cds_ja_inode_flag **snapshot_ptr[JA_MAX_DEPTH]; |
2371 | uint8_t snapshot_n[JA_MAX_DEPTH]; | |
35170a44 | 2372 | struct cds_ja_inode_flag *node_flag; |
fa112799 MD |
2373 | struct cds_ja_inode_flag **prev_node_flag_ptr, |
2374 | **node_flag_ptr; | |
4d6ef45e | 2375 | int nr_snapshot; |
35170a44 MD |
2376 | int ret; |
2377 | ||
2378 | if (caa_unlikely(key > ja->key_max)) | |
2379 | return -EINVAL; | |
2380 | tree_depth = ja->tree_depth; | |
2381 | ||
2382 | retry: | |
4d6ef45e | 2383 | nr_snapshot = 0; |
35170a44 MD |
2384 | dbg_printf("cds_ja_del attempt: key %" PRIu64 ", node %p\n", |
2385 | key, node); | |
2386 | ||
2387 | /* snapshot for level 0 is only for shadow node lookup */ | |
4d6ef45e MD |
2388 | snapshot_n[0] = 0; |
2389 | snapshot_n[1] = 0; | |
af3cbd45 | 2390 | snapshot_ptr[nr_snapshot] = NULL; |
35170a44 MD |
2391 | snapshot[nr_snapshot++] = (struct cds_ja_inode_flag *) &ja->root; |
2392 | node_flag = rcu_dereference(ja->root); | |
af3cbd45 | 2393 | prev_node_flag_ptr = &ja->root; |
fa112799 | 2394 | node_flag_ptr = &ja->root; |
35170a44 MD |
2395 | |
2396 | /* Iterate on all internal levels */ | |
2397 | for (i = 1; i < tree_depth; i++) { | |
2398 | uint8_t iter_key; | |
2399 | ||
2400 | dbg_printf("cds_ja_del iter node_flag %p\n", | |
2401 | node_flag); | |
2402 | if (!ja_node_ptr(node_flag)) { | |
2403 | return -ENOENT; | |
2404 | } | |
35170a44 | 2405 | iter_key = (uint8_t) (key >> (JA_BITS_PER_BYTE * (tree_depth - i - 1))); |
4d6ef45e | 2406 | snapshot_n[nr_snapshot + 1] = iter_key; |
af3cbd45 MD |
2407 | snapshot_ptr[nr_snapshot] = prev_node_flag_ptr; |
2408 | snapshot[nr_snapshot++] = node_flag; | |
35170a44 | 2409 | node_flag = ja_node_get_nth(node_flag, |
fa112799 | 2410 | &node_flag_ptr, |
35170a44 | 2411 | iter_key); |
48cbe001 MD |
2412 | if (node_flag) |
2413 | prev_node_flag_ptr = node_flag_ptr; | |
af3cbd45 MD |
2414 | dbg_printf("cds_ja_del iter key lookup %u finds node_flag %p, prev_node_flag_ptr %p\n", |
2415 | (unsigned int) iter_key, node_flag, | |
2416 | prev_node_flag_ptr); | |
35170a44 | 2417 | } |
35170a44 MD |
2418 | /* |
2419 | * We reached bottom of tree, try to find the node we are trying | |
2420 | * to remove. Fail if we cannot find it. | |
2421 | */ | |
2422 | if (!ja_node_ptr(node_flag)) { | |
4d6ef45e MD |
2423 | dbg_printf("cds_ja_del: no node found for key %" PRIu64 "\n", |
2424 | key); | |
35170a44 MD |
2425 | return -ENOENT; |
2426 | } else { | |
4d6ef45e | 2427 | struct cds_hlist_head hlist_head; |
35170a44 | 2428 | struct cds_hlist_node *hlist_node; |
af3cbd45 MD |
2429 | struct cds_ja_node *entry, *match = NULL; |
2430 | int count = 0; | |
35170a44 | 2431 | |
4d6ef45e MD |
2432 | hlist_head.next = |
2433 | (struct cds_hlist_node *) ja_node_ptr(node_flag); | |
af3cbd45 | 2434 | cds_hlist_for_each_entry_rcu(entry, |
35170a44 | 2435 | hlist_node, |
4d6ef45e | 2436 | &hlist_head, |
35170a44 | 2437 | list) { |
4d6ef45e | 2438 | dbg_printf("cds_ja_del: compare %p with entry %p\n", node, entry); |
af3cbd45 MD |
2439 | if (entry == node) |
2440 | match = entry; | |
2441 | count++; | |
35170a44 | 2442 | } |
4d6ef45e MD |
2443 | if (!match) { |
2444 | dbg_printf("cds_ja_del: no node match for node %p key %" PRIu64 "\n", node, key); | |
35170a44 | 2445 | return -ENOENT; |
4d6ef45e | 2446 | } |
af3cbd45 MD |
2447 | assert(count > 0); |
2448 | if (count == 1) { | |
2449 | /* | |
4d6ef45e MD |
2450 | * Removing last of duplicates. Last snapshot |
2451 | * does not have a shadow node (external leafs). | |
af3cbd45 MD |
2452 | */ |
2453 | snapshot_ptr[nr_snapshot] = prev_node_flag_ptr; | |
2454 | snapshot[nr_snapshot++] = node_flag; | |
2455 | ret = ja_detach_node(ja, snapshot, snapshot_ptr, | |
2456 | snapshot_n, nr_snapshot, key, node); | |
2457 | } else { | |
f2758d14 | 2458 | ret = ja_unchain_node(ja, snapshot[nr_snapshot - 1], |
013a6083 | 2459 | node_flag_ptr, node_flag, match); |
af3cbd45 | 2460 | } |
35170a44 | 2461 | } |
b306a0fe MD |
2462 | /* |
2463 | * Explanation of -ENOENT handling: caused by concurrent delete | |
2464 | * between RCU lookup and actual removal. Need to re-do the | |
2465 | * lookup and removal attempt. | |
2466 | */ | |
2467 | if (ret == -EAGAIN || ret == -ENOENT) | |
35170a44 MD |
2468 | goto retry; |
2469 | return ret; | |
2470 | } | |
2471 | ||
b4540e8a MD |
2472 | struct cds_ja *_cds_ja_new(unsigned int key_bits, |
2473 | const struct rcu_flavor_struct *flavor) | |
be9a7474 MD |
2474 | { |
2475 | struct cds_ja *ja; | |
b0f74e47 | 2476 | int ret; |
f07b240f | 2477 | struct cds_ja_shadow_node *root_shadow_node; |
be9a7474 MD |
2478 | |
2479 | ja = calloc(sizeof(*ja), 1); | |
2480 | if (!ja) | |
2481 | goto ja_error; | |
b4540e8a MD |
2482 | |
2483 | switch (key_bits) { | |
2484 | case 8: | |
b4540e8a | 2485 | case 16: |
1216b3d2 | 2486 | case 24: |
b4540e8a | 2487 | case 32: |
1216b3d2 MD |
2488 | case 40: |
2489 | case 48: | |
2490 | case 56: | |
2491 | ja->key_max = (1ULL << key_bits) - 1; | |
b4540e8a MD |
2492 | break; |
2493 | case 64: | |
2494 | ja->key_max = UINT64_MAX; | |
2495 | break; | |
2496 | default: | |
2497 | goto check_error; | |
2498 | } | |
2499 | ||
be9a7474 | 2500 | /* ja->root is NULL */ |
5a9a87dd | 2501 | /* tree_depth 0 is for pointer to root node */ |
582a6ade | 2502 | ja->tree_depth = (key_bits >> JA_LOG2_BITS_PER_BYTE) + 1; |
a2a7ff59 | 2503 | assert(ja->tree_depth <= JA_MAX_DEPTH); |
be9a7474 MD |
2504 | ja->ht = rcuja_create_ht(flavor); |
2505 | if (!ja->ht) | |
2506 | goto ht_error; | |
b0f74e47 MD |
2507 | |
2508 | /* | |
2509 | * Note: we should not free this node until judy array destroy. | |
2510 | */ | |
f07b240f | 2511 | root_shadow_node = rcuja_shadow_set(ja->ht, |
3d8fe307 | 2512 | (struct cds_ja_inode_flag *) &ja->root, |
48cbe001 | 2513 | NULL, ja, 0); |
f07b240f MD |
2514 | if (!root_shadow_node) { |
2515 | ret = -ENOMEM; | |
b0f74e47 | 2516 | goto ht_node_error; |
f07b240f | 2517 | } |
b0f74e47 | 2518 | |
be9a7474 MD |
2519 | return ja; |
2520 | ||
b0f74e47 MD |
2521 | ht_node_error: |
2522 | ret = rcuja_delete_ht(ja->ht); | |
2523 | assert(!ret); | |
be9a7474 | 2524 | ht_error: |
b4540e8a | 2525 | check_error: |
be9a7474 MD |
2526 | free(ja); |
2527 | ja_error: | |
2528 | return NULL; | |
2529 | } | |
2530 | ||
3d8fe307 MD |
2531 | /* |
2532 | * Called from RCU read-side CS. | |
2533 | */ | |
2534 | __attribute__((visibility("protected"))) | |
2535 | void rcuja_free_all_children(struct cds_ja_shadow_node *shadow_node, | |
2536 | struct cds_ja_inode_flag *node_flag, | |
2537 | void (*free_node_cb)(struct rcu_head *head)) | |
2538 | { | |
2539 | const struct rcu_flavor_struct *flavor; | |
2540 | unsigned int type_index; | |
2541 | struct cds_ja_inode *node; | |
2542 | const struct cds_ja_type *type; | |
2543 | ||
2544 | flavor = cds_lfht_rcu_flavor(shadow_node->ja->ht); | |
2545 | node = ja_node_ptr(node_flag); | |
2546 | assert(node != NULL); | |
2547 | type_index = ja_node_type(node_flag); | |
2548 | type = &ja_types[type_index]; | |
2549 | ||
2550 | switch (type->type_class) { | |
2551 | case RCU_JA_LINEAR: | |
2552 | { | |
2553 | uint8_t nr_child = | |
2554 | ja_linear_node_get_nr_child(type, node); | |
2555 | unsigned int i; | |
2556 | ||
2557 | for (i = 0; i < nr_child; i++) { | |
2558 | struct cds_ja_inode_flag *iter; | |
2559 | struct cds_hlist_head head; | |
2560 | struct cds_ja_node *entry; | |
48cbe001 | 2561 | struct cds_hlist_node *pos, *tmp; |
3d8fe307 MD |
2562 | uint8_t v; |
2563 | ||
2564 | ja_linear_node_get_ith_pos(type, node, i, &v, &iter); | |
2565 | if (!iter) | |
2566 | continue; | |
2567 | head.next = (struct cds_hlist_node *) iter; | |
48cbe001 | 2568 | cds_hlist_for_each_entry_safe(entry, pos, tmp, &head, list) { |
3d8fe307 MD |
2569 | flavor->update_call_rcu(&entry->head, free_node_cb); |
2570 | } | |
2571 | } | |
2572 | break; | |
2573 | } | |
2574 | case RCU_JA_POOL: | |
2575 | { | |
2576 | unsigned int pool_nr; | |
2577 | ||
2578 | for (pool_nr = 0; pool_nr < (1U << type->nr_pool_order); pool_nr++) { | |
2579 | struct cds_ja_inode *pool = | |
2580 | ja_pool_node_get_ith_pool(type, node, pool_nr); | |
2581 | uint8_t nr_child = | |
2582 | ja_linear_node_get_nr_child(type, pool); | |
2583 | unsigned int j; | |
2584 | ||
2585 | for (j = 0; j < nr_child; j++) { | |
2586 | struct cds_ja_inode_flag *iter; | |
2587 | struct cds_hlist_head head; | |
2588 | struct cds_ja_node *entry; | |
48cbe001 | 2589 | struct cds_hlist_node *pos, *tmp; |
3d8fe307 MD |
2590 | uint8_t v; |
2591 | ||
75d573aa | 2592 | ja_linear_node_get_ith_pos(type, pool, j, &v, &iter); |
3d8fe307 MD |
2593 | if (!iter) |
2594 | continue; | |
2595 | head.next = (struct cds_hlist_node *) iter; | |
48cbe001 | 2596 | cds_hlist_for_each_entry_safe(entry, pos, tmp, &head, list) { |
3d8fe307 MD |
2597 | flavor->update_call_rcu(&entry->head, free_node_cb); |
2598 | } | |
2599 | } | |
2600 | } | |
2601 | break; | |
2602 | } | |
2603 | case RCU_JA_NULL: | |
2604 | break; | |
2605 | case RCU_JA_PIGEON: | |
2606 | { | |
3d8fe307 MD |
2607 | unsigned int i; |
2608 | ||
48cbe001 | 2609 | for (i = 0; i < JA_ENTRY_PER_NODE; i++) { |
3d8fe307 MD |
2610 | struct cds_ja_inode_flag *iter; |
2611 | struct cds_hlist_head head; | |
2612 | struct cds_ja_node *entry; | |
48cbe001 | 2613 | struct cds_hlist_node *pos, *tmp; |
3d8fe307 MD |
2614 | |
2615 | iter = ja_pigeon_node_get_ith_pos(type, node, i); | |
2616 | if (!iter) | |
2617 | continue; | |
2618 | head.next = (struct cds_hlist_node *) iter; | |
48cbe001 | 2619 | cds_hlist_for_each_entry_safe(entry, pos, tmp, &head, list) { |
3d8fe307 MD |
2620 | flavor->update_call_rcu(&entry->head, free_node_cb); |
2621 | } | |
2622 | } | |
2623 | break; | |
2624 | } | |
2625 | default: | |
2626 | assert(0); | |
2627 | } | |
2628 | } | |
2629 | ||
19ddcd04 | 2630 | static |
354981c2 | 2631 | void print_debug_fallback_distribution(struct cds_ja *ja) |
19ddcd04 MD |
2632 | { |
2633 | int i; | |
2634 | ||
2635 | fprintf(stderr, "Fallback node distribution:\n"); | |
2636 | for (i = 0; i < JA_ENTRY_PER_NODE; i++) { | |
354981c2 | 2637 | if (!ja->node_fallback_count_distribution[i]) |
19ddcd04 MD |
2638 | continue; |
2639 | fprintf(stderr, " %3u: %4lu\n", | |
354981c2 | 2640 | i, ja->node_fallback_count_distribution[i]); |
19ddcd04 MD |
2641 | } |
2642 | } | |
2643 | ||
021c72c0 | 2644 | static |
19a748d9 | 2645 | int ja_final_checks(struct cds_ja *ja) |
021c72c0 MD |
2646 | { |
2647 | double fallback_ratio; | |
2648 | unsigned long na, nf, nr_fallback; | |
19a748d9 | 2649 | int ret = 0; |
021c72c0 MD |
2650 | |
2651 | fallback_ratio = (double) uatomic_read(&ja->nr_fallback); | |
2652 | fallback_ratio /= (double) uatomic_read(&ja->nr_nodes_allocated); | |
2653 | nr_fallback = uatomic_read(&ja->nr_fallback); | |
2654 | if (nr_fallback) | |
2655 | fprintf(stderr, | |
2656 | "[warning] RCU Judy Array used %lu fallback node(s) (ratio: %g)\n", | |
2657 | uatomic_read(&ja->nr_fallback), | |
2658 | fallback_ratio); | |
2659 | ||
2660 | na = uatomic_read(&ja->nr_nodes_allocated); | |
2661 | nf = uatomic_read(&ja->nr_nodes_freed); | |
19a748d9 MD |
2662 | dbg_printf("Nodes allocated: %lu, Nodes freed: %lu.\n", na, nf); |
2663 | if (nr_fallback) | |
2664 | print_debug_fallback_distribution(ja); | |
2665 | ||
021c72c0 MD |
2666 | if (na != nf) { |
2667 | fprintf(stderr, "[error] Judy array leaked %ld nodes. Allocated: %lu, freed: %lu.\n", | |
2668 | (long) na - nf, na, nf); | |
19a748d9 | 2669 | ret = -1; |
021c72c0 | 2670 | } |
19a748d9 | 2671 | return ret; |
021c72c0 MD |
2672 | } |
2673 | ||
be9a7474 MD |
2674 | /* |
2675 | * There should be no more concurrent add to the judy array while it is | |
2676 | * being destroyed (ensured by the caller). | |
2677 | */ | |
3d8fe307 MD |
2678 | int cds_ja_destroy(struct cds_ja *ja, |
2679 | void (*free_node_cb)(struct rcu_head *head)) | |
be9a7474 | 2680 | { |
48cbe001 | 2681 | const struct rcu_flavor_struct *flavor; |
b4540e8a MD |
2682 | int ret; |
2683 | ||
48cbe001 | 2684 | flavor = cds_lfht_rcu_flavor(ja->ht); |
be9a7474 | 2685 | rcuja_shadow_prune(ja->ht, |
3d8fe307 MD |
2686 | RCUJA_SHADOW_CLEAR_FREE_NODE | RCUJA_SHADOW_CLEAR_FREE_LOCK, |
2687 | free_node_cb); | |
48cbe001 | 2688 | flavor->thread_offline(); |
b4540e8a MD |
2689 | ret = rcuja_delete_ht(ja->ht); |
2690 | if (ret) | |
2691 | return ret; | |
f2ae7af7 MD |
2692 | |
2693 | /* Wait for in-flight call_rcu free to complete. */ | |
2694 | flavor->barrier(); | |
2695 | ||
48cbe001 | 2696 | flavor->thread_online(); |
19a748d9 | 2697 | ret = ja_final_checks(ja); |
b4540e8a | 2698 | free(ja); |
19a748d9 | 2699 | return ret; |
be9a7474 | 2700 | } |