Commit | Line | Data |
---|---|---|
61009379 MD |
1 | /* |
2 | * rcuja/rcuja.c | |
3 | * | |
4 | * Userspace RCU library - RCU Judy Array | |
5 | * | |
6 | * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2.1 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
e5227865 | 23 | #include <stdint.h> |
d68c6810 | 24 | #include <limits.h> |
61009379 | 25 | #include <urcu/rcuja.h> |
d68c6810 MD |
26 | #include <urcu/compiler.h> |
27 | #include <urcu/arch.h> | |
28 | #include <assert.h> | |
61009379 | 29 | #include "rcuja-internal.h" |
d68c6810 | 30 | #include "bitfield.h" |
61009379 | 31 | |
d68c6810 | 32 | enum rcu_ja_type_class { |
e5227865 | 33 | RCU_JA_LINEAR = 0, /* Type A */ |
fd800776 MD |
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 */ | |
e5227865 | 39 | RCU_JA_PIGEON = 2, /* Type C */ |
fd800776 MD |
40 | /* 32-bit: 101 to 256 children, 1024 bytes */ |
41 | /* 64-bit: 113 to 256 children, 2048 bytes */ | |
e5227865 MD |
42 | /* Leaf nodes are implicit from their height in the tree */ |
43 | }; | |
44 | ||
d68c6810 MD |
45 | struct rcu_ja_type { |
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 */ | |
fd800776 MD |
50 | uint16_t nr_pool_order; /* number of pools */ |
51 | uint16_t pool_size_order; /* pool size */ | |
e5227865 MD |
52 | }; |
53 | ||
d68c6810 MD |
54 | /* |
55 | * Number of least significant pointer bits reserved to represent the | |
56 | * child type. | |
57 | */ | |
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) | |
62 | ||
63 | #define JA_ENTRY_PER_NODE 256UL | |
64 | ||
e5227865 MD |
65 | /* |
66 | * Iteration on the array to find the right node size for the number of | |
d68c6810 | 67 | * children stops when it reaches .max_child == 256 (this is the largest |
e5227865 | 68 | * possible node size, which contains 256 children). |
d68c6810 MD |
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 | |
74 | * the size order. | |
3d45251f MD |
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. | |
e5227865 | 81 | */ |
e5227865 | 82 | |
d68c6810 MD |
83 | #if (CAA_BITS_PER_LONG < 64) |
84 | /* 32-bit pointers */ | |
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, }, | |
fd800776 | 90 | { .type_class = RCU_JA_LINEAR, .min_child = 10, .max_child = 25, .order = 7, }, |
e5227865 | 91 | |
fd800776 | 92 | /* Pools may fill sooner than max_child */ |
3d45251f MD |
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, }, | |
95 | ||
96 | /* | |
97 | * TODO: Upon node removal below min_child, if child pool is | |
98 | * filled beyond capacity, we need to roll back to pigeon. | |
99 | */ | |
100 | { .type_class = RCU_JA_PIGEON, .min_child = 89, .max_child = 256, .order = 10, }, | |
d68c6810 MD |
101 | }; |
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, }, | |
fd800776 | 110 | { .type_class = RCU_JA_LINEAR, .min_child = 10, .max_child = 28, .order = 8, }, |
e5227865 | 111 | |
3d45251f MD |
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, }, | |
e5227865 | 115 | |
3d45251f MD |
116 | /* |
117 | * TODO: Upon node removal below min_child, if child pool is | |
118 | * filled beyond capacity, we need to roll back to pigeon. | |
119 | */ | |
120 | { .type_class = RCU_JA_PIGEON, .min_child = 101, .max_child = 256, .order = 11, }, | |
e5227865 | 121 | }; |
d68c6810 MD |
122 | CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) > JA_TYPE_MAX_NR); |
123 | #endif /* !(BITS_PER_LONG < 64) */ | |
e5227865 MD |
124 | |
125 | /* | |
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 ? | |
131 | */ | |
132 | struct rcu_ja_node { | |
133 | char data[]; | |
134 | }; | |
135 | ||
d68c6810 MD |
136 | /* Never declared. Opaque type used to store flagged node pointers. */ |
137 | struct rcu_ja_node_flag; | |
138 | ||
139 | static | |
140 | struct rcu_ja_node_flag *ja_node_flag(struct rcu_ja_node *node, unsigned int type) | |
141 | { | |
142 | assert(type < JA_TYPE_NR); | |
143 | return (struct rcu_ja_node_flag *) (((unsigned long) node) | type); | |
144 | } | |
145 | ||
146 | static | |
147 | unsigned int ja_node_type(struct rcu_ja_node_flag *node) | |
148 | { | |
149 | unsigned int type; | |
150 | ||
151 | type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK); | |
152 | assert(type < JA_TYPE_NR); | |
153 | return type; | |
154 | } | |
155 | ||
156 | static | |
157 | struct rcu_ja_node *ja_node_ptr(struct rcu_ja_node_flag *node) | |
158 | { | |
159 | return (struct rcu_ja_node *) (((unsigned long) node) | JA_PTR_MASK); | |
160 | } | |
161 | ||
162 | struct rcu_ja_node *alloc_rcu_ja_node(struct rcu_ja_type *ja_type) | |
e5227865 | 163 | { |
d68c6810 | 164 | return zmalloc(1 << ja_type->node_order); |
e5227865 MD |
165 | } |
166 | ||
167 | void free_rcu_ja_node(struct rcu_ja_node *node) | |
168 | { | |
169 | free(node); | |
170 | } | |
171 | ||
d68c6810 MD |
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) | |
176 | ||
177 | static | |
178 | char *align_ptr_size(char *ptr) | |
179 | { | |
180 | return JA_ALIGN(ptr, sizeof(ptr)); | |
181 | } | |
182 | ||
183 | static | |
184 | struct rcu_ja_node_flag *ja_linear_node_get_nth(const struct rcu_ja_type *type, | |
185 | struct rcu_ja_node *node, | |
186 | uint8_t n) | |
187 | { | |
188 | uint8_t nr_child; | |
189 | uint8_t *values; | |
190 | struct rcu_ja_node_flag *pointers; | |
191 | struct rcu_ja_node_flag *ptr; | |
192 | unsigned int i; | |
193 | ||
fd800776 | 194 | assert(!type || type->type_class == RCU_JA_LINEAR); |
d68c6810 MD |
195 | |
196 | nr_child = node->data[0]; | |
197 | cmm_smp_rmb(); /* read nr_child before values */ | |
fd800776 MD |
198 | assert(!type || nr_child <= type->max_child); |
199 | assert(!type || nr_child >= type->min_child); | |
d68c6810 MD |
200 | |
201 | values = &node[1]; | |
202 | for (i = 0; i < nr_child; i++) { | |
203 | if (values[i] == n) | |
204 | break; | |
205 | } | |
206 | if (i >= nr_child) | |
207 | return NULL; | |
208 | cmm_smp_rmb(); /* read values before pointer */ | |
209 | pointers = align_ptr_size(&values[nr_child]); | |
210 | ptr = pointers[i]; | |
211 | assert(ja_node_ptr(ptr) != NULL); | |
212 | return ptr; | |
213 | } | |
214 | ||
d68c6810 | 215 | static |
fd800776 | 216 | struct rcu_ja_node_flag *ja_pool_node_get_nth(const struct rcu_ja_type *type, |
d68c6810 MD |
217 | struct rcu_ja_node *node, |
218 | uint8_t n) | |
219 | { | |
d68c6810 | 220 | struct rcu_ja_node_flag *ptr; |
fd800776 | 221 | struct rcu_ja_node *linear; |
d68c6810 | 222 | |
fd800776 MD |
223 | assert(type->type_class == RCU_JA_POOL); |
224 | linear = (struct rcu_ja_node *) | |
3d45251f | 225 | &node->data[((unsigned long) n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order]; |
fd800776 | 226 | return ja_linear_node_get_nth(NULL, linear, n); |
d68c6810 MD |
227 | } |
228 | ||
229 | static | |
230 | struct rcu_ja_node_flag *ja_pigeon_node_get_nth(const struct rcu_ja_type *type, | |
231 | struct rcu_ja_node *node, | |
232 | uint8_t n) | |
233 | { | |
234 | assert(type->type_class == RCU_JA_PIGEON); | |
235 | return ((struct rcu_ja_node_flag *) node->data)[n]; | |
236 | } | |
237 | ||
238 | /* ja_node_get_nth: get nth item from a node */ | |
239 | static | |
240 | struct rcu_ja_node_flag *ja_node_get_nth(struct rcu_ja_node_flag *node_flag, | |
241 | uint8_t n) | |
242 | { | |
243 | unsigned int type_index; | |
244 | struct rcu_ja_node *node; | |
245 | const struct rcu_ja_type *type; | |
246 | ||
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]; | |
252 | ||
253 | switch (type->type_class) { | |
254 | case RCU_JA_LINEAR: | |
255 | return ja_linear_node_get_nth(type, node, n); | |
fd800776 MD |
256 | case RCU_JA_POOL: |
257 | return ja_pool_node_get_nth(type, node, n); | |
d68c6810 MD |
258 | case RCU_JA_PIGEON: |
259 | return ja_pigeon_node_get_nth(type, node, n); | |
260 | default: | |
261 | assert(0); | |
262 | return (void *) -1UL; | |
263 | } | |
264 | } | |
265 | ||
266 | /* | |
267 | * ja_node_set_nth: set nth item within a node. asserts that it is not | |
268 | * there yet. | |
269 | */ | |
e5227865 | 270 |