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