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. | |
e5227865 | 75 | */ |
e5227865 | 76 | |
d68c6810 MD |
77 | #if (CAA_BITS_PER_LONG < 64) |
78 | /* 32-bit pointers */ | |
79 | const struct rcu_ja_type ja_types[] = { | |
80 | { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = 1, .order = 3, }, | |
81 | { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = 3, .order = 4, }, | |
82 | { .type_class = RCU_JA_LINEAR, .min_child = 3, .max_child = 6, .order = 5, }, | |
83 | { .type_class = RCU_JA_LINEAR, .min_child = 4, .max_child = 12, .order = 6, }, | |
fd800776 | 84 | { .type_class = RCU_JA_LINEAR, .min_child = 10, .max_child = 25, .order = 7, }, |
e5227865 | 85 | |
fd800776 MD |
86 | /* Pools may fill sooner than max_child */ |
87 | { .type_class = RCU_JA_POOL, .min_child = 20, .max_child = 50, .order = 8, .nr_pool_order = 1, .pool_size_order = 7, }, | |
88 | { .type_class = RCU_JA_POOL, .min_child = 42, .max_child = 100, .order = 9, .nr_pool_order = 2, .pool_size_order = 7, }, | |
e5227865 | 89 | |
fd800776 MD |
90 | /* TODO: Upon downsize, if at least one pool is filled, we need to keep pigeon */ |
91 | { .type_class = RCU_JA_PIGEON, .min_child = 90, .max_child = 256, .order = 10, }, | |
d68c6810 MD |
92 | }; |
93 | CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) > JA_TYPE_MAX_NR); | |
94 | #else /* !(CAA_BITS_PER_LONG < 64) */ | |
95 | /* 64-bit pointers */ | |
96 | const struct rcu_ja_type ja_types[] = { | |
97 | { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = 1, .order = 4, }, | |
98 | { .type_class = RCU_JA_LINEAR, .min_child = 1, .max_child = 3, .order = 5, }, | |
99 | { .type_class = RCU_JA_LINEAR, .min_child = 3, .max_child = 7, .order = 6, }, | |
100 | { .type_class = RCU_JA_LINEAR, .min_child = 5, .max_child = 14, .order = 7, }, | |
fd800776 | 101 | { .type_class = RCU_JA_LINEAR, .min_child = 10, .max_child = 28, .order = 8, }, |
e5227865 | 102 | |
fd800776 MD |
103 | /* Pools may fill sooner than max_child */ |
104 | { .type_class = RCU_JA_POOL, .min_child = 22, .max_child = 56, .order = 9, .nr_pool_order = 1, .pool_size_order = 8, }, | |
105 | { .type_class = RCU_JA_POOL, .min_child = 44, .max_child = 112, .order = 10, .nr_pool_order = 2, .pool_size_order = 8, }, | |
e5227865 | 106 | |
fd800776 MD |
107 | /* TODO: Upon downsize, if at least one pool is filled, we need to keep pigeon */ |
108 | { .type_class = RCU_JA_PIGEON, .min_child = 100, .max_child = 256, .order = 11, }, | |
e5227865 | 109 | }; |
d68c6810 MD |
110 | CAA_BUILD_BUG_ON(CAA_ARRAY_SIZE(ja_types) > JA_TYPE_MAX_NR); |
111 | #endif /* !(BITS_PER_LONG < 64) */ | |
e5227865 MD |
112 | |
113 | /* | |
114 | * The rcu_ja_node starts with a byte counting the number of children in | |
115 | * the node. Then, the node-specific data is placed. | |
116 | * TODO: where should we put the mutex for the node ? | |
117 | * -> mutex could be a 0-value node count. | |
118 | * TODO: where do we keep nr children for pigeon ? | |
119 | */ | |
120 | struct rcu_ja_node { | |
121 | char data[]; | |
122 | }; | |
123 | ||
d68c6810 MD |
124 | /* Never declared. Opaque type used to store flagged node pointers. */ |
125 | struct rcu_ja_node_flag; | |
126 | ||
127 | static | |
128 | struct rcu_ja_node_flag *ja_node_flag(struct rcu_ja_node *node, unsigned int type) | |
129 | { | |
130 | assert(type < JA_TYPE_NR); | |
131 | return (struct rcu_ja_node_flag *) (((unsigned long) node) | type); | |
132 | } | |
133 | ||
134 | static | |
135 | unsigned int ja_node_type(struct rcu_ja_node_flag *node) | |
136 | { | |
137 | unsigned int type; | |
138 | ||
139 | type = (unsigned int) ((unsigned long) node & JA_TYPE_MASK); | |
140 | assert(type < JA_TYPE_NR); | |
141 | return type; | |
142 | } | |
143 | ||
144 | static | |
145 | struct rcu_ja_node *ja_node_ptr(struct rcu_ja_node_flag *node) | |
146 | { | |
147 | return (struct rcu_ja_node *) (((unsigned long) node) | JA_PTR_MASK); | |
148 | } | |
149 | ||
150 | struct rcu_ja_node *alloc_rcu_ja_node(struct rcu_ja_type *ja_type) | |
e5227865 | 151 | { |
d68c6810 | 152 | return zmalloc(1 << ja_type->node_order); |
e5227865 MD |
153 | } |
154 | ||
155 | void free_rcu_ja_node(struct rcu_ja_node *node) | |
156 | { | |
157 | free(node); | |
158 | } | |
159 | ||
d68c6810 MD |
160 | #define __JA_ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask)) |
161 | #define JA_ALIGN(v, align) __JA_ALIGN_MASK(v, (typeof(v)) (align) - 1) | |
162 | #define __JA_FLOOR_MASK(v, mask) ((v) & ~(mask)) | |
163 | #define JA_FLOOR(v, align) __JA_FLOOR_MASK(v, (typeof(v)) (align) - 1) | |
164 | ||
165 | static | |
166 | char *align_ptr_size(char *ptr) | |
167 | { | |
168 | return JA_ALIGN(ptr, sizeof(ptr)); | |
169 | } | |
170 | ||
171 | static | |
172 | struct rcu_ja_node_flag *ja_linear_node_get_nth(const struct rcu_ja_type *type, | |
173 | struct rcu_ja_node *node, | |
174 | uint8_t n) | |
175 | { | |
176 | uint8_t nr_child; | |
177 | uint8_t *values; | |
178 | struct rcu_ja_node_flag *pointers; | |
179 | struct rcu_ja_node_flag *ptr; | |
180 | unsigned int i; | |
181 | ||
fd800776 | 182 | assert(!type || type->type_class == RCU_JA_LINEAR); |
d68c6810 MD |
183 | |
184 | nr_child = node->data[0]; | |
185 | cmm_smp_rmb(); /* read nr_child before values */ | |
fd800776 MD |
186 | assert(!type || nr_child <= type->max_child); |
187 | assert(!type || nr_child >= type->min_child); | |
d68c6810 MD |
188 | |
189 | values = &node[1]; | |
190 | for (i = 0; i < nr_child; i++) { | |
191 | if (values[i] == n) | |
192 | break; | |
193 | } | |
194 | if (i >= nr_child) | |
195 | return NULL; | |
196 | cmm_smp_rmb(); /* read values before pointer */ | |
197 | pointers = align_ptr_size(&values[nr_child]); | |
198 | ptr = pointers[i]; | |
199 | assert(ja_node_ptr(ptr) != NULL); | |
200 | return ptr; | |
201 | } | |
202 | ||
d68c6810 | 203 | static |
fd800776 | 204 | struct rcu_ja_node_flag *ja_pool_node_get_nth(const struct rcu_ja_type *type, |
d68c6810 MD |
205 | struct rcu_ja_node *node, |
206 | uint8_t n) | |
207 | { | |
d68c6810 | 208 | struct rcu_ja_node_flag *ptr; |
fd800776 | 209 | struct rcu_ja_node *linear; |
d68c6810 | 210 | |
fd800776 MD |
211 | assert(type->type_class == RCU_JA_POOL); |
212 | linear = (struct rcu_ja_node *) | |
213 | &node->data[(n >> (CHAR_BIT - type->nr_pool_order)) << type->pool_size_order]; | |
214 | return ja_linear_node_get_nth(NULL, linear, n); | |
d68c6810 MD |
215 | } |
216 | ||
217 | static | |
218 | struct rcu_ja_node_flag *ja_pigeon_node_get_nth(const struct rcu_ja_type *type, | |
219 | struct rcu_ja_node *node, | |
220 | uint8_t n) | |
221 | { | |
222 | assert(type->type_class == RCU_JA_PIGEON); | |
223 | return ((struct rcu_ja_node_flag *) node->data)[n]; | |
224 | } | |
225 | ||
226 | /* ja_node_get_nth: get nth item from a node */ | |
227 | static | |
228 | struct rcu_ja_node_flag *ja_node_get_nth(struct rcu_ja_node_flag *node_flag, | |
229 | uint8_t n) | |
230 | { | |
231 | unsigned int type_index; | |
232 | struct rcu_ja_node *node; | |
233 | const struct rcu_ja_type *type; | |
234 | ||
235 | node_flag = rcu_dereference(node_flag); | |
236 | node = ja_node_ptr(node_flag); | |
237 | assert(node != NULL); | |
238 | type_index = ja_node_type(node_flag); | |
239 | type = &ja_types[type_index]; | |
240 | ||
241 | switch (type->type_class) { | |
242 | case RCU_JA_LINEAR: | |
243 | return ja_linear_node_get_nth(type, node, n); | |
fd800776 MD |
244 | case RCU_JA_POOL: |
245 | return ja_pool_node_get_nth(type, node, n); | |
d68c6810 MD |
246 | case RCU_JA_PIGEON: |
247 | return ja_pigeon_node_get_nth(type, node, n); | |
248 | default: | |
249 | assert(0); | |
250 | return (void *) -1UL; | |
251 | } | |
252 | } | |
253 | ||
254 | /* | |
255 | * ja_node_set_nth: set nth item within a node. asserts that it is not | |
256 | * there yet. | |
257 | */ | |
e5227865 | 258 |