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