4 * Userspace RCU library - RCU Judy Array
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
25 #include <urcu/rcuja.h>
26 #include <urcu/compiler.h>
27 #include <urcu/arch.h>
29 #include "rcuja-internal.h"
32 enum rcu_ja_type_class
{
33 RCU_JA_LINEAR
= 0, /* Type A */
34 /* 32-bit: 1 to 25 children, 8 to 128 bytes */
35 /* 64-bit: 1 to 28 children, 16 to 256 bytes */
36 RCU_JA_POOL
= 1, /* Type B */
37 /* 32-bit: 26 to 100 children, 256 to 512 bytes */
38 /* 64-bit: 29 to 112 children, 512 to 1024 bytes */
39 RCU_JA_PIGEON
= 2, /* Type C */
40 /* 32-bit: 101 to 256 children, 1024 bytes */
41 /* 64-bit: 113 to 256 children, 2048 bytes */
42 /* Leaf nodes are implicit from their height in the tree */
46 enum rcu_ja_type_class type_class
;
47 uint16_t min_child
; /* minimum number of children: 1 to 256 */
48 uint16_t max_child
; /* maximum number of children: 1 to 256 */
49 uint16_t order
; /* node size is (1 << order), in bytes */
50 uint16_t nr_pool_order
; /* number of pools */
51 uint16_t pool_size_order
; /* pool size */
55 * Number of least significant pointer bits reserved to represent the
58 #define JA_TYPE_BITS 3
59 #define JA_TYPE_MAX_NR (1U << JA_TYPE_BITS)
60 #define JA_TYPE_MASK (JA_TYPE_MAX_NR - 1)
61 #define JA_PTR_MASK (~JA_TYPE_MASK)
63 #define JA_ENTRY_PER_NODE 256UL
66 * Iteration on the array to find the right node size for the number of
67 * children stops when it reaches .max_child == 256 (this is the largest
68 * possible node size, which contains 256 children).
69 * The min_child overlaps with the previous max_child to provide an
70 * hysteresis loop to reallocation for patterns of cyclic add/removal
71 * within the same node.
72 * The node the index within the following arrays is represented on 3
73 * bits. It identifies the node type, min/max number of children, and
75 * The max_child values for the RCU_JA_POOL below result from
76 * statistical approximation: over million populations, the max_child
77 * covers between 97% and 99% of the populations generated. Therefore, a
78 * fallback should exist to cover the rare extreme population unbalance
79 * cases, but it will not have a major impact on speed nor space
80 * consumption, since those are rare cases.
83 #if (CAA_BITS_PER_LONG < 64)
85 const struct rcu_ja_type ja_types
[] = {
86 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= 1, .order
= 3, },
87 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= 3, .order
= 4, },
88 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= 6, .order
= 5, },
89 { .type_class
= RCU_JA_LINEAR
, .min_child
= 4, .max_child
= 12, .order
= 6, },
90 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= 25, .order
= 7, },
92 /* Pools may fill sooner than max_child */
93 { .type_class
= RCU_JA_POOL
, .min_child
= 20, .max_child
= 48, .order
= 8, .nr_pool_order
= 1, .pool_size_order
= 7, },
94 { .type_class
= RCU_JA_POOL
, .min_child
= 45, .max_child
= 92, .order
= 9, .nr_pool_order
= 2, .pool_size_order
= 7, },
97 * TODO: Upon node removal below min_child, if child pool is
98 * filled beyond capacity, we need to roll back to pigeon.
100 { .type_class
= RCU_JA_PIGEON
, .min_child
= 89, .max_child
= 256, .order
= 10, },
102 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) > JA_TYPE_MAX_NR
);
103 #else /* !(CAA_BITS_PER_LONG < 64) */
104 /* 64-bit pointers */
105 const struct rcu_ja_type ja_types
[] = {
106 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= 1, .order
= 4, },
107 { .type_class
= RCU_JA_LINEAR
, .min_child
= 1, .max_child
= 3, .order
= 5, },
108 { .type_class
= RCU_JA_LINEAR
, .min_child
= 3, .max_child
= 7, .order
= 6, },
109 { .type_class
= RCU_JA_LINEAR
, .min_child
= 5, .max_child
= 14, .order
= 7, },
110 { .type_class
= RCU_JA_LINEAR
, .min_child
= 10, .max_child
= 28, .order
= 8, },
112 /* Pools may fill sooner than max_child. */
113 { .type_class
= RCU_JA_POOL
, .min_child
= 22, .max_child
= 54, .order
= 9, .nr_pool_order
= 1, .pool_size_order
= 8, },
114 { .type_class
= RCU_JA_POOL
, .min_child
= 51, .max_child
= 104, .order
= 10, .nr_pool_order
= 2, .pool_size_order
= 8, },
117 * TODO: Upon node removal below min_child, if child pool is
118 * filled beyond capacity, we need to roll back to pigeon.
120 { .type_class
= RCU_JA_PIGEON
, .min_child
= 101, .max_child
= 256, .order
= 11, },
122 CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types
) > JA_TYPE_MAX_NR
);
123 #endif /* !(BITS_PER_LONG < 64) */
126 * The rcu_ja_node starts with a byte counting the number of children in
127 * the node. Then, the node-specific data is placed.
128 * TODO: where should we put the mutex for the node ?
129 * -> mutex could be a 0-value node count.
130 * TODO: where do we keep nr children for pigeon ?
136 /* Never declared. Opaque type used to store flagged node pointers. */
137 struct rcu_ja_node_flag
;
140 struct rcu_ja_node_flag
*ja_node_flag(struct rcu_ja_node
*node
, unsigned int type
)
142 assert(type
< JA_TYPE_NR
);
143 return (struct rcu_ja_node_flag
*) (((unsigned long) node
) | type
);
147 unsigned int ja_node_type(struct rcu_ja_node_flag
*node
)
151 type
= (unsigned int) ((unsigned long) node
& JA_TYPE_MASK
);
152 assert(type
< JA_TYPE_NR
);
157 struct rcu_ja_node
*ja_node_ptr(struct rcu_ja_node_flag
*node
)
159 return (struct rcu_ja_node
*) (((unsigned long) node
) | JA_PTR_MASK
);
162 struct rcu_ja_node
*alloc_rcu_ja_node(struct rcu_ja_type
*ja_type
)
164 return zmalloc(1 << ja_type
->node_order
);
167 void free_rcu_ja_node(struct rcu_ja_node
*node
)
172 #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
173 #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1)
174 #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask))
175 #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1)
178 char *align_ptr_size(char *ptr
)
180 return JA_ALIGN(ptr
, sizeof(ptr
));
184 struct rcu_ja_node_flag
*ja_linear_node_get_nth(const struct rcu_ja_type
*type
,
185 struct rcu_ja_node
*node
,
190 struct rcu_ja_node_flag
*pointers
;
191 struct rcu_ja_node_flag
*ptr
;
194 assert(!type
|| type
->type_class
== RCU_JA_LINEAR
);
196 nr_child
= node
->data
[0];
197 cmm_smp_rmb(); /* read nr_child before values */
198 assert(!type
|| nr_child
<= type
->max_child
);
199 assert(!type
|| nr_child
>= type
->min_child
);
202 for (i
= 0; i
< nr_child
; i
++) {
208 cmm_smp_rmb(); /* read values before pointer */
209 pointers
= align_ptr_size(&values
[nr_child
]);
211 assert(ja_node_ptr(ptr
) != NULL
);
216 struct rcu_ja_node_flag
*ja_pool_node_get_nth(const struct rcu_ja_type
*type
,
217 struct rcu_ja_node
*node
,
220 struct rcu_ja_node_flag
*ptr
;
221 struct rcu_ja_node
*linear
;
223 assert(type
->type_class
== RCU_JA_POOL
);
224 linear
= (struct rcu_ja_node
*)
225 &node
->data
[((unsigned long) n
>> (CHAR_BIT
- type
->nr_pool_order
)) << type
->pool_size_order
];
226 return ja_linear_node_get_nth(NULL
, linear
, n
);
230 struct rcu_ja_node_flag
*ja_pigeon_node_get_nth(const struct rcu_ja_type
*type
,
231 struct rcu_ja_node
*node
,
234 assert(type
->type_class
== RCU_JA_PIGEON
);
235 return ((struct rcu_ja_node_flag
*) node
->data
)[n
];
238 /* ja_node_get_nth: get nth item from a node */
240 struct rcu_ja_node_flag
*ja_node_get_nth(struct rcu_ja_node_flag
*node_flag
,
243 unsigned int type_index
;
244 struct rcu_ja_node
*node
;
245 const struct rcu_ja_type
*type
;
247 node_flag
= rcu_dereference(node_flag
);
248 node
= ja_node_ptr(node_flag
);
249 assert(node
!= NULL
);
250 type_index
= ja_node_type(node_flag
);
251 type
= &ja_types
[type_index
];
253 switch (type
->type_class
) {
255 return ja_linear_node_get_nth(type
, node
, n
);
257 return ja_pool_node_get_nth(type
, node
, n
);
259 return ja_pigeon_node_get_nth(type
, node
, n
);
262 return (void *) -1UL;
267 * ja_node_set_nth: set nth item within a node. asserts that it is not