rcuja: set node update, rcu-ize get node/set node
[userspace-rcu.git] / rcuja / rcuja.c
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
23 #include <stdint.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <urcu/rcuja.h>
27 #include <urcu/compiler.h>
28 #include <urcu/arch.h>
29 #include <assert.h>
30 #include <urcu-pointer.h>
31
32 #include "rcuja-internal.h"
33 #include "bitfield.h"
34
35 enum rcu_ja_type_class {
36 RCU_JA_LINEAR = 0, /* Type A */
37 /* 32-bit: 1 to 25 children, 8 to 128 bytes */
38 /* 64-bit: 1 to 28 children, 16 to 256 bytes */
39 RCU_JA_POOL = 1, /* Type B */
40 /* 32-bit: 26 to 100 children, 256 to 512 bytes */
41 /* 64-bit: 29 to 112 children, 512 to 1024 bytes */
42 RCU_JA_PIGEON = 2, /* Type C */
43 /* 32-bit: 101 to 256 children, 1024 bytes */
44 /* 64-bit: 113 to 256 children, 2048 bytes */
45 /* Leaf nodes are implicit from their height in the tree */
46 RCU_JA_NR_TYPES,
47 };
48
49 struct rcu_ja_type {
50 enum rcu_ja_type_class type_class;
51 uint16_t min_child; /* minimum number of children: 1 to 256 */
52 uint16_t max_child; /* maximum number of children: 1 to 256 */
53 uint16_t max_linear_child; /* per-pool max nr. children: 1 to 256 */
54 uint16_t order; /* node size is (1 << order), in bytes */
55 uint16_t nr_pool_order; /* number of pools */
56 uint16_t pool_size_order; /* pool size */
57 };
58
59 /*
60 * Number of least significant pointer bits reserved to represent the
61 * child type.
62 */
63 #define JA_TYPE_BITS 3
64 #define JA_TYPE_MAX_NR (1U << JA_TYPE_BITS)
65 #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1)
66 #define JA_PTR_MASK (~JA_TYPE_MASK)
67
68 #define JA_ENTRY_PER_NODE 256UL
69
70 /*
71 * Iteration on the array to find the right node size for the number of
72 * children stops when it reaches .max_child == 256 (this is the largest
73 * possible node size, which contains 256 children).
74 * The min_child overlaps with the previous max_child to provide an
75 * hysteresis loop to reallocation for patterns of cyclic add/removal
76 * within the same node.
77 * The node the index within the following arrays is represented on 3
78 * bits. It identifies the node type, min/max number of children, and
79 * the size order.
80 * The max_child values for the RCU_JA_POOL below result from
81 * statistical approximation: over million populations, the max_child
82 * covers between 97% and 99% of the populations generated. Therefore, a
83 * fallback should exist to cover the rare extreme population unbalance
84 * cases, but it will not have a major impact on speed nor space
85 * consumption, since those are rare cases.
86 */
87
88 #if (CAA_BITS_PER_LONG < 64)
89 /* 32-bit pointers */
90 enum {
91 ja_type_0_max_child = 1,
92 ja_type_1_max_child = 3,
93 ja_type_2_max_child = 6,
94 ja_type_3_max_child = 12,
95 ja_type_4_max_child = 25,
96 ja_type_5_max_child = 48,
97 ja_type_6_max_child = 92,
98 ja_type_7_max_child = 256,
99 };
100
101 enum {
102 ja_type_0_max_linear_child = 1,
103 ja_type_1_max_linear_child = 3,
104 ja_type_2_max_linear_child = 6,
105 ja_type_3_max_linear_child = 12,
106 ja_type_4_max_linear_child = 25,
107 ja_type_5_max_linear_child = 24,
108 ja_type_6_max_linear_child = 23,
109 };
110
111 enum {
112 ja_type_5_nr_pool_order = 1,
113 ja_type_6_nr_pool_order = 2,
114 };
115
116 const struct rcu_ja_type ja_types[] = {
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, },
122
123 /* Pools may fill sooner than max_child */
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, },
126
127 /*
128 * TODO: Upon node removal below min_child, if child pool is
129 * filled beyond capacity, we need to roll back to pigeon.
130 */
131 { .type_class = RCU_JA_PIGEON, .min_child = 89, .max_child = ja_type_7_max_child, .order = 10, },
132 };
133 #else /* !(CAA_BITS_PER_LONG < 64) */
134 /* 64-bit pointers */
135 enum {
136 ja_type_0_max_child = 1,
137 ja_type_1_max_child = 3,
138 ja_type_2_max_child = 7,
139 ja_type_3_max_child = 14,
140 ja_type_4_max_child = 28,
141 ja_type_5_max_child = 54,
142 ja_type_6_max_child = 104,
143 ja_type_7_max_child = 256,
144 };
145
146 enum {
147 ja_type_0_max_linear_child = 1,
148 ja_type_1_max_linear_child = 3,
149 ja_type_2_max_linear_child = 7,
150 ja_type_3_max_linear_child = 14,
151 ja_type_4_max_linear_child = 28,
152 ja_type_5_max_linear_child = 27,
153 ja_type_6_max_linear_child = 26,
154 };
155
156 enum {
157 ja_type_5_nr_pool_order = 1,
158 ja_type_6_nr_pool_order = 2,
159 };
160
161 const struct rcu_ja_type ja_types[] = {
162 { .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, },
163 { .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, },
164 { .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, },
165 { .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, },
166 { .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, },
167
168 /* Pools may fill sooner than max_child. */
169 { .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, },
170 { .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, },
171
172 /*
173 * TODO: Upon node removal below min_child, if child pool is
174 * filled beyond capacity, we need to roll back to pigeon.
175 */
176 { .type_class = RCU_JA_PIGEON, .min_child = 101, .max_child = ja_type_7_max_child, .order = 11, },
177 };
178 #endif /* !(BITS_PER_LONG < 64) */
179
180 static inline __attribute__((unused))
181 void static_array_size_check(void)
182 {
183 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) > JA_TYPE_MAX_NR);
184 }
185
186 /* Never declared. Opaque type used to store flagged node pointers. */
187 struct rcu_ja_node_flag;
188
189 /*
190 * The rcu_ja_node contains the compressed node data needed for
191 * read-side. For linear and pool node configurations, it starts with a
192 * byte counting the number of children in the node. Then, the
193 * node-specific data is placed.
194 * The node mutex, if any is needed, protecting concurrent updated of
195 * each node is placed in a separate hash table indexed by node address.
196 * For the pigeon configuration, the number of children is also kept in
197 * a separate hash table, indexed by node address, because it is only
198 * required for updates.
199 */
200
201 #define DECLARE_LINEAR_NODE(index) \
202 struct { \
203 uint8_t nr_child; \
204 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
205 struct rcu_ja_node_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
206 }
207
208 #define DECLARE_POOL_NODE(index) \
209 struct { \
210 struct { \
211 uint8_t nr_child; \
212 uint8_t child_value[ja_type_## index ##_max_linear_child]; \
213 struct rcu_ja_node_flag *child_ptr[ja_type_## index ##_max_linear_child]; \
214 } linear[1U << ja_type_## index ##_nr_pool_order]; \
215 }
216
217 struct rcu_ja_node {
218 union {
219 /* Linear configuration */
220 DECLARE_LINEAR_NODE(0) conf_0;
221 DECLARE_LINEAR_NODE(1) conf_1;
222 DECLARE_LINEAR_NODE(2) conf_2;
223 DECLARE_LINEAR_NODE(3) conf_3;
224 DECLARE_LINEAR_NODE(4) conf_4;
225
226 /* Pool configuration */
227 DECLARE_POOL_NODE(5) conf_5;
228 DECLARE_POOL_NODE(6) conf_6;
229
230 /* Pigeon configuration */
231 struct {
232 struct rcu_ja_node_flag *child[ja_type_7_max_child];
233 } conf_7;
234 /* data aliasing nodes for computed accesses */
235 uint8_t data[sizeof(struct rcu_ja_node_flag *) * ja_type_7_max_child];
236 } u;
237 };
238
239 static
240 struct rcu_ja_node_flag *ja_node_flag(struct rcu_ja_node *node,
241 unsigned int type)
242 {
243 assert(type < RCU_JA_NR_TYPES);
244 return (struct rcu_ja_node_flag *) (((unsigned long) node) | type);
245 }
246
247 static
248 unsigned int ja_node_type(struct rcu_ja_node_flag *node)
249 {
250 unsigned int type;
251
252 type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK);
253 assert(type < RCU_JA_NR_TYPES);
254 return type;
255 }
256
257 static
258 struct rcu_ja_node *ja_node_ptr(struct rcu_ja_node_flag *node)
259 {
260 return (struct rcu_ja_node *) (((unsigned long) node) | JA_PTR_MASK);
261 }
262
263 struct rcu_ja_node *alloc_rcu_ja_node(struct rcu_ja_type *ja_type)
264 {
265 return calloc(1U << ja_type->order, sizeof(char));
266 }
267
268 void free_rcu_ja_node(struct rcu_ja_node *node)
269 {
270 free(node);
271 }
272
273 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
274 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
275 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
276 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
277
278 static
279 uint8_t *align_ptr_size(uint8_t *ptr)
280 {
281 return (uint8_t *) JA_ALIGN((unsigned long) ptr, sizeof(void *));
282 }
283
284 /*
285 * The order in which values and pointers are does does not matter: if
286 * a value is missing, we return NULL. If a value is there, but its
287 * associated pointers is still NULL, we return NULL too.
288 */
289 static
290 struct rcu_ja_node_flag *ja_linear_node_get_nth(const struct rcu_ja_type *type,
291 struct rcu_ja_node *node,
292 uint8_t n)
293 {
294 uint8_t nr_child;
295 uint8_t *values;
296 struct rcu_ja_node_flag **pointers;
297 struct rcu_ja_node_flag *ptr;
298 unsigned int i;
299
300 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
301
302 nr_child = CMM_LOAD_SHARED(node->u.data[0]);
303 cmm_smp_rmb(); /* read nr_child before values and pointers */
304 assert(nr_child <= type->max_linear_child);
305 assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child);
306
307 values = &node->u.data[1];
308 for (i = 0; i < nr_child; i++) {
309 if (CMM_LOAD_SHARED(values[i]) == n)
310 break;
311 }
312 if (i >= nr_child)
313 return NULL;
314 pointers = (struct rcu_ja_node_flag **) align_ptr_size(&values[type->max_linear_child]);
315 ptr = rcu_dereference(pointers[i]);
316 assert(ja_node_ptr(ptr) != NULL);
317 return ptr;
318 }
319
320 static
321 struct rcu_ja_node_flag *ja_pool_node_get_nth(const struct rcu_ja_type *type,
322 struct rcu_ja_node *node,
323 uint8_t n)
324 {
325 struct rcu_ja_node *linear;
326
327 assert(type->type_class == RCU_JA_POOL);
328 linear = (struct rcu_ja_node *)
329 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
330 return ja_linear_node_get_nth(type, linear, n);
331 }
332
333 static
334 struct rcu_ja_node_flag *ja_pigeon_node_get_nth(const struct rcu_ja_type *type,
335 struct rcu_ja_node *node,
336 uint8_t n)
337 {
338 assert(type->type_class == RCU_JA_PIGEON);
339 return rcu_dereference(((struct rcu_ja_node_flag **) node->u.data)[n]);
340 }
341
342 /*
343 * ja_node_get_nth: get nth item from a node.
344 * node_flag is already rcu_dereference'd.
345 */
346 static
347 struct rcu_ja_node_flag *ja_node_get_nth(struct rcu_ja_node_flag *node_flag,
348 uint8_t n)
349 {
350 unsigned int type_index;
351 struct rcu_ja_node *node;
352 const struct rcu_ja_type *type;
353
354 node = ja_node_ptr(node_flag);
355 assert(node != NULL);
356 type_index = ja_node_type(node_flag);
357 type = &ja_types[type_index];
358
359 switch (type->type_class) {
360 case RCU_JA_LINEAR:
361 return ja_linear_node_get_nth(type, node, n);
362 case RCU_JA_POOL:
363 return ja_pool_node_get_nth(type, node, n);
364 case RCU_JA_PIGEON:
365 return ja_pigeon_node_get_nth(type, node, n);
366 default:
367 assert(0);
368 return (void *) -1UL;
369 }
370 }
371
372 static
373 int ja_linear_node_set_nth(const struct rcu_ja_type *type,
374 struct rcu_ja_node *node,
375 uint8_t n,
376 struct rcu_ja_node_flag *child_node_flag)
377 {
378 uint8_t nr_child;
379 uint8_t *values, *nr_child_ptr;
380 struct rcu_ja_node_flag **pointers;
381 unsigned int i;
382
383 assert(type->type_class == RCU_JA_LINEAR || type->type_class == RCU_JA_POOL);
384
385 nr_child_ptr = &node->u.data[0];
386 nr_child = *nr_child_ptr;
387 assert(nr_child <= type->max_linear_child);
388 assert(type->type_class != RCU_JA_LINEAR || nr_child >= type->min_child);
389
390 values = &node->u.data[1];
391 for (i = 0; i < nr_child; i++) {
392 if (values[i] == n)
393 return -EEXIST;
394 }
395 if (nr_child >= type->max_linear_child) {
396 /* No space left in this node type */
397 return -ENOSPC;
398 }
399 pointers = (struct rcu_ja_node_flag **) align_ptr_size(&values[type->max_linear_child]);
400 assert(pointers[nr_child] == NULL);
401 rcu_assign_pointer(pointers[nr_child], child_node_flag);
402 CMM_STORE_SHARED(values[nr_child], n);
403 cmm_smp_wmb(); /* write value and pointer before nr_child */
404 CMM_STORE_SHARED(*nr_child_ptr, nr_child + 1);
405 return 0;
406 }
407
408 static
409 int ja_pool_node_set_nth(const struct rcu_ja_type *type,
410 struct rcu_ja_node *node,
411 uint8_t n,
412 struct rcu_ja_node_flag *child_node_flag)
413 {
414 struct rcu_ja_node *linear;
415
416 assert(type->type_class == RCU_JA_POOL);
417 linear = (struct rcu_ja_node *)
418 &node->u.data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order];
419 return ja_linear_node_set_nth(type, linear, n, child_node_flag);
420 }
421
422 static
423 int ja_pigeon_node_set_nth(const struct rcu_ja_type *type,
424 struct rcu_ja_node *node,
425 uint8_t n,
426 struct rcu_ja_node_flag *child_node_flag)
427 {
428 struct rcu_ja_node_flag **ptr;
429
430 assert(type->type_class == RCU_JA_PIGEON);
431 ptr = &((struct rcu_ja_node_flag **) node->u.data)[n];
432 if (*ptr != NULL)
433 return -EEXIST;
434 rcu_assign_pointer(*ptr, child_node_flag);
435 return 0;
436 }
437
438 /*
439 * ja_node_set_nth: set nth item within a node. Return an error
440 * (negative error value) if it is already there.
441 * TODO: exclusive access on node.
442 */
443 static
444 int ja_node_set_nth(struct rcu_ja_node_flag *node_flag, uint8_t n,
445 struct rcu_ja_node_flag *child_node_flag)
446 {
447 unsigned int type_index;
448 struct rcu_ja_node *node;
449 const struct rcu_ja_type *type;
450
451 node = ja_node_ptr(node_flag);
452 assert(node != NULL);
453 type_index = ja_node_type(node_flag);
454 type = &ja_types[type_index];
455
456 switch (type->type_class) {
457 case RCU_JA_LINEAR:
458 return ja_linear_node_set_nth(type, node, n,
459 child_node_flag);
460 case RCU_JA_POOL:
461 return ja_pool_node_set_nth(type, node, n,
462 child_node_flag);
463 case RCU_JA_PIGEON:
464 return ja_pigeon_node_set_nth(type, node, n,
465 child_node_flag);
466 default:
467 assert(0);
468 return -EINVAL;
469 }
470
471 return 0;
472 }
This page took 0.040534 seconds and 5 git commands to generate.