Commit | Line | Data |
---|---|---|
ad3d6c72 MW |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * test_xarray.c: Test the XArray API | |
4 | * Copyright (c) 2017-2018 Microsoft Corporation | |
5 | * Author: Matthew Wilcox <willy@infradead.org> | |
6 | */ | |
7 | ||
8 | #include <linux/xarray.h> | |
9 | #include <linux/module.h> | |
10 | ||
11 | static unsigned int tests_run; | |
12 | static unsigned int tests_passed; | |
13 | ||
14 | #ifndef XA_DEBUG | |
15 | # ifdef __KERNEL__ | |
16 | void xa_dump(const struct xarray *xa) { } | |
17 | # endif | |
18 | #undef XA_BUG_ON | |
19 | #define XA_BUG_ON(xa, x) do { \ | |
20 | tests_run++; \ | |
21 | if (x) { \ | |
22 | printk("BUG at %s:%d\n", __func__, __LINE__); \ | |
23 | xa_dump(xa); \ | |
24 | dump_stack(); \ | |
25 | } else { \ | |
26 | tests_passed++; \ | |
27 | } \ | |
28 | } while (0) | |
29 | #endif | |
30 | ||
31 | static void *xa_store_index(struct xarray *xa, unsigned long index, gfp_t gfp) | |
32 | { | |
58d6ea30 | 33 | return xa_store(xa, index, xa_mk_value(index & LONG_MAX), gfp); |
ad3d6c72 MW |
34 | } |
35 | ||
371c752d MW |
36 | static void xa_alloc_index(struct xarray *xa, unsigned long index, gfp_t gfp) |
37 | { | |
38 | u32 id = 0; | |
39 | ||
40 | XA_BUG_ON(xa, xa_alloc(xa, &id, UINT_MAX, xa_mk_value(index & LONG_MAX), | |
41 | gfp) != 0); | |
42 | XA_BUG_ON(xa, id != index); | |
43 | } | |
44 | ||
ad3d6c72 MW |
45 | static void xa_erase_index(struct xarray *xa, unsigned long index) |
46 | { | |
58d6ea30 MW |
47 | XA_BUG_ON(xa, xa_erase(xa, index) != xa_mk_value(index & LONG_MAX)); |
48 | XA_BUG_ON(xa, xa_load(xa, index) != NULL); | |
49 | } | |
50 | ||
51 | /* | |
52 | * If anyone needs this, please move it to xarray.c. We have no current | |
53 | * users outside the test suite because all current multislot users want | |
54 | * to use the advanced API. | |
55 | */ | |
56 | static void *xa_store_order(struct xarray *xa, unsigned long index, | |
57 | unsigned order, void *entry, gfp_t gfp) | |
58 | { | |
59 | XA_STATE_ORDER(xas, xa, index, order); | |
60 | void *curr; | |
61 | ||
62 | do { | |
63 | xas_lock(&xas); | |
64 | curr = xas_store(&xas, entry); | |
65 | xas_unlock(&xas); | |
66 | } while (xas_nomem(&xas, gfp)); | |
67 | ||
68 | return curr; | |
69 | } | |
70 | ||
71 | static noinline void check_xa_err(struct xarray *xa) | |
72 | { | |
73 | XA_BUG_ON(xa, xa_err(xa_store_index(xa, 0, GFP_NOWAIT)) != 0); | |
74 | XA_BUG_ON(xa, xa_err(xa_erase(xa, 0)) != 0); | |
75 | #ifndef __KERNEL__ | |
76 | /* The kernel does not fail GFP_NOWAIT allocations */ | |
77 | XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM); | |
78 | XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_NOWAIT)) != -ENOMEM); | |
79 | #endif | |
80 | XA_BUG_ON(xa, xa_err(xa_store_index(xa, 1, GFP_KERNEL)) != 0); | |
81 | XA_BUG_ON(xa, xa_err(xa_store(xa, 1, xa_mk_value(0), GFP_KERNEL)) != 0); | |
82 | XA_BUG_ON(xa, xa_err(xa_erase(xa, 1)) != 0); | |
83 | // kills the test-suite :-( | |
84 | // XA_BUG_ON(xa, xa_err(xa_store(xa, 0, xa_mk_internal(0), 0)) != -EINVAL); | |
ad3d6c72 MW |
85 | } |
86 | ||
b803b428 MW |
87 | static noinline void check_xas_retry(struct xarray *xa) |
88 | { | |
89 | XA_STATE(xas, xa, 0); | |
90 | void *entry; | |
91 | ||
92 | xa_store_index(xa, 0, GFP_KERNEL); | |
93 | xa_store_index(xa, 1, GFP_KERNEL); | |
94 | ||
95 | rcu_read_lock(); | |
96 | XA_BUG_ON(xa, xas_find(&xas, ULONG_MAX) != xa_mk_value(0)); | |
97 | xa_erase_index(xa, 1); | |
98 | XA_BUG_ON(xa, !xa_is_retry(xas_reload(&xas))); | |
99 | XA_BUG_ON(xa, xas_retry(&xas, NULL)); | |
100 | XA_BUG_ON(xa, xas_retry(&xas, xa_mk_value(0))); | |
101 | xas_reset(&xas); | |
102 | XA_BUG_ON(xa, xas.xa_node != XAS_RESTART); | |
103 | XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0)); | |
104 | XA_BUG_ON(xa, xas.xa_node != NULL); | |
105 | ||
106 | XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL); | |
107 | XA_BUG_ON(xa, !xa_is_internal(xas_reload(&xas))); | |
108 | xas.xa_node = XAS_RESTART; | |
109 | XA_BUG_ON(xa, xas_next_entry(&xas, ULONG_MAX) != xa_mk_value(0)); | |
110 | rcu_read_unlock(); | |
111 | ||
112 | /* Make sure we can iterate through retry entries */ | |
113 | xas_lock(&xas); | |
114 | xas_set(&xas, 0); | |
115 | xas_store(&xas, XA_RETRY_ENTRY); | |
116 | xas_set(&xas, 1); | |
117 | xas_store(&xas, XA_RETRY_ENTRY); | |
118 | ||
119 | xas_set(&xas, 0); | |
120 | xas_for_each(&xas, entry, ULONG_MAX) { | |
121 | xas_store(&xas, xa_mk_value(xas.xa_index)); | |
122 | } | |
123 | xas_unlock(&xas); | |
124 | ||
125 | xa_erase_index(xa, 0); | |
126 | xa_erase_index(xa, 1); | |
127 | } | |
128 | ||
ad3d6c72 MW |
129 | static noinline void check_xa_load(struct xarray *xa) |
130 | { | |
131 | unsigned long i, j; | |
132 | ||
133 | for (i = 0; i < 1024; i++) { | |
134 | for (j = 0; j < 1024; j++) { | |
135 | void *entry = xa_load(xa, j); | |
136 | if (j < i) | |
137 | XA_BUG_ON(xa, xa_to_value(entry) != j); | |
138 | else | |
139 | XA_BUG_ON(xa, entry); | |
140 | } | |
141 | XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); | |
142 | } | |
143 | ||
144 | for (i = 0; i < 1024; i++) { | |
145 | for (j = 0; j < 1024; j++) { | |
146 | void *entry = xa_load(xa, j); | |
147 | if (j >= i) | |
148 | XA_BUG_ON(xa, xa_to_value(entry) != j); | |
149 | else | |
150 | XA_BUG_ON(xa, entry); | |
151 | } | |
152 | xa_erase_index(xa, i); | |
153 | } | |
154 | XA_BUG_ON(xa, !xa_empty(xa)); | |
155 | } | |
156 | ||
9b89a035 MW |
157 | static noinline void check_xa_mark_1(struct xarray *xa, unsigned long index) |
158 | { | |
58d6ea30 MW |
159 | unsigned int order; |
160 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 8 : 1; | |
161 | ||
9b89a035 MW |
162 | /* NULL elements have no marks set */ |
163 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); | |
164 | xa_set_mark(xa, index, XA_MARK_0); | |
165 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); | |
166 | ||
167 | /* Storing a pointer will not make a mark appear */ | |
168 | XA_BUG_ON(xa, xa_store_index(xa, index, GFP_KERNEL) != NULL); | |
169 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); | |
170 | xa_set_mark(xa, index, XA_MARK_0); | |
171 | XA_BUG_ON(xa, !xa_get_mark(xa, index, XA_MARK_0)); | |
172 | ||
173 | /* Setting one mark will not set another mark */ | |
174 | XA_BUG_ON(xa, xa_get_mark(xa, index + 1, XA_MARK_0)); | |
175 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_1)); | |
176 | ||
177 | /* Storing NULL clears marks, and they can't be set again */ | |
178 | xa_erase_index(xa, index); | |
179 | XA_BUG_ON(xa, !xa_empty(xa)); | |
180 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); | |
181 | xa_set_mark(xa, index, XA_MARK_0); | |
182 | XA_BUG_ON(xa, xa_get_mark(xa, index, XA_MARK_0)); | |
58d6ea30 MW |
183 | |
184 | /* | |
185 | * Storing a multi-index entry over entries with marks gives the | |
186 | * entire entry the union of the marks | |
187 | */ | |
188 | BUG_ON((index % 4) != 0); | |
189 | for (order = 2; order < max_order; order++) { | |
190 | unsigned long base = round_down(index, 1UL << order); | |
191 | unsigned long next = base + (1UL << order); | |
192 | unsigned long i; | |
193 | ||
194 | XA_BUG_ON(xa, xa_store_index(xa, index + 1, GFP_KERNEL)); | |
195 | xa_set_mark(xa, index + 1, XA_MARK_0); | |
196 | XA_BUG_ON(xa, xa_store_index(xa, index + 2, GFP_KERNEL)); | |
197 | xa_set_mark(xa, index + 2, XA_MARK_1); | |
198 | XA_BUG_ON(xa, xa_store_index(xa, next, GFP_KERNEL)); | |
199 | xa_store_order(xa, index, order, xa_mk_value(index), | |
200 | GFP_KERNEL); | |
201 | for (i = base; i < next; i++) { | |
93eb07f7 MW |
202 | XA_STATE(xas, xa, i); |
203 | unsigned int seen = 0; | |
204 | void *entry; | |
205 | ||
58d6ea30 MW |
206 | XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0)); |
207 | XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_1)); | |
208 | XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_2)); | |
93eb07f7 MW |
209 | |
210 | /* We should see two elements in the array */ | |
211 | xas_for_each(&xas, entry, ULONG_MAX) | |
212 | seen++; | |
213 | XA_BUG_ON(xa, seen != 2); | |
214 | ||
215 | /* One of which is marked */ | |
216 | xas_set(&xas, 0); | |
217 | seen = 0; | |
218 | xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0) | |
219 | seen++; | |
220 | XA_BUG_ON(xa, seen != 1); | |
58d6ea30 MW |
221 | } |
222 | XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_0)); | |
223 | XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_1)); | |
224 | XA_BUG_ON(xa, xa_get_mark(xa, next, XA_MARK_2)); | |
225 | xa_erase_index(xa, index); | |
226 | xa_erase_index(xa, next); | |
227 | XA_BUG_ON(xa, !xa_empty(xa)); | |
228 | } | |
229 | XA_BUG_ON(xa, !xa_empty(xa)); | |
9b89a035 MW |
230 | } |
231 | ||
adb9d9c4 MW |
232 | static noinline void check_xa_mark_2(struct xarray *xa) |
233 | { | |
234 | XA_STATE(xas, xa, 0); | |
235 | unsigned long index; | |
236 | unsigned int count = 0; | |
237 | void *entry; | |
238 | ||
239 | xa_store_index(xa, 0, GFP_KERNEL); | |
240 | xa_set_mark(xa, 0, XA_MARK_0); | |
241 | xas_lock(&xas); | |
242 | xas_load(&xas); | |
243 | xas_init_marks(&xas); | |
244 | xas_unlock(&xas); | |
245 | XA_BUG_ON(xa, !xa_get_mark(xa, 0, XA_MARK_0) == 0); | |
246 | ||
247 | for (index = 3500; index < 4500; index++) { | |
248 | xa_store_index(xa, index, GFP_KERNEL); | |
249 | xa_set_mark(xa, index, XA_MARK_0); | |
250 | } | |
251 | ||
252 | xas_reset(&xas); | |
253 | rcu_read_lock(); | |
254 | xas_for_each_marked(&xas, entry, ULONG_MAX, XA_MARK_0) | |
255 | count++; | |
256 | rcu_read_unlock(); | |
257 | XA_BUG_ON(xa, count != 1000); | |
258 | ||
259 | xas_lock(&xas); | |
260 | xas_for_each(&xas, entry, ULONG_MAX) { | |
261 | xas_init_marks(&xas); | |
262 | XA_BUG_ON(xa, !xa_get_mark(xa, xas.xa_index, XA_MARK_0)); | |
263 | XA_BUG_ON(xa, !xas_get_mark(&xas, XA_MARK_0)); | |
264 | } | |
265 | xas_unlock(&xas); | |
266 | ||
267 | xa_destroy(xa); | |
268 | } | |
269 | ||
9b89a035 MW |
270 | static noinline void check_xa_mark(struct xarray *xa) |
271 | { | |
272 | unsigned long index; | |
273 | ||
274 | for (index = 0; index < 16384; index += 4) | |
275 | check_xa_mark_1(xa, index); | |
adb9d9c4 MW |
276 | |
277 | check_xa_mark_2(xa); | |
9b89a035 MW |
278 | } |
279 | ||
58d6ea30 MW |
280 | static noinline void check_xa_shrink(struct xarray *xa) |
281 | { | |
282 | XA_STATE(xas, xa, 1); | |
283 | struct xa_node *node; | |
93eb07f7 MW |
284 | unsigned int order; |
285 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 15 : 1; | |
58d6ea30 MW |
286 | |
287 | XA_BUG_ON(xa, !xa_empty(xa)); | |
288 | XA_BUG_ON(xa, xa_store_index(xa, 0, GFP_KERNEL) != NULL); | |
289 | XA_BUG_ON(xa, xa_store_index(xa, 1, GFP_KERNEL) != NULL); | |
290 | ||
291 | /* | |
292 | * Check that erasing the entry at 1 shrinks the tree and properly | |
293 | * marks the node as being deleted. | |
294 | */ | |
295 | xas_lock(&xas); | |
296 | XA_BUG_ON(xa, xas_load(&xas) != xa_mk_value(1)); | |
297 | node = xas.xa_node; | |
298 | XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != xa_mk_value(0)); | |
299 | XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1)); | |
300 | XA_BUG_ON(xa, xa_load(xa, 1) != NULL); | |
301 | XA_BUG_ON(xa, xas.xa_node != XAS_BOUNDS); | |
302 | XA_BUG_ON(xa, xa_entry_locked(xa, node, 0) != XA_RETRY_ENTRY); | |
303 | XA_BUG_ON(xa, xas_load(&xas) != NULL); | |
304 | xas_unlock(&xas); | |
305 | XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0)); | |
306 | xa_erase_index(xa, 0); | |
307 | XA_BUG_ON(xa, !xa_empty(xa)); | |
93eb07f7 MW |
308 | |
309 | for (order = 0; order < max_order; order++) { | |
310 | unsigned long max = (1UL << order) - 1; | |
311 | xa_store_order(xa, 0, order, xa_mk_value(0), GFP_KERNEL); | |
312 | XA_BUG_ON(xa, xa_load(xa, max) != xa_mk_value(0)); | |
313 | XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL); | |
314 | rcu_read_lock(); | |
315 | node = xa_head(xa); | |
316 | rcu_read_unlock(); | |
317 | XA_BUG_ON(xa, xa_store_index(xa, ULONG_MAX, GFP_KERNEL) != | |
318 | NULL); | |
319 | rcu_read_lock(); | |
320 | XA_BUG_ON(xa, xa_head(xa) == node); | |
321 | rcu_read_unlock(); | |
322 | XA_BUG_ON(xa, xa_load(xa, max + 1) != NULL); | |
323 | xa_erase_index(xa, ULONG_MAX); | |
324 | XA_BUG_ON(xa, xa->xa_head != node); | |
325 | xa_erase_index(xa, 0); | |
326 | } | |
58d6ea30 MW |
327 | } |
328 | ||
41aec91f MW |
329 | static noinline void check_cmpxchg(struct xarray *xa) |
330 | { | |
331 | void *FIVE = xa_mk_value(5); | |
332 | void *SIX = xa_mk_value(6); | |
333 | void *LOTS = xa_mk_value(12345678); | |
334 | ||
335 | XA_BUG_ON(xa, !xa_empty(xa)); | |
336 | XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_KERNEL) != NULL); | |
337 | XA_BUG_ON(xa, xa_insert(xa, 12345678, xa, GFP_KERNEL) != -EEXIST); | |
338 | XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, SIX, FIVE, GFP_KERNEL) != LOTS); | |
339 | XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, LOTS, FIVE, GFP_KERNEL) != LOTS); | |
340 | XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, FIVE, LOTS, GFP_KERNEL) != FIVE); | |
341 | XA_BUG_ON(xa, xa_cmpxchg(xa, 5, FIVE, NULL, GFP_KERNEL) != NULL); | |
342 | XA_BUG_ON(xa, xa_cmpxchg(xa, 5, NULL, FIVE, GFP_KERNEL) != NULL); | |
343 | xa_erase_index(xa, 12345678); | |
344 | xa_erase_index(xa, 5); | |
345 | XA_BUG_ON(xa, !xa_empty(xa)); | |
346 | } | |
347 | ||
9f14d4f1 MW |
348 | static noinline void check_reserve(struct xarray *xa) |
349 | { | |
350 | void *entry; | |
351 | unsigned long index = 0; | |
352 | ||
353 | /* An array with a reserved entry is not empty */ | |
354 | XA_BUG_ON(xa, !xa_empty(xa)); | |
355 | xa_reserve(xa, 12345678, GFP_KERNEL); | |
356 | XA_BUG_ON(xa, xa_empty(xa)); | |
357 | XA_BUG_ON(xa, xa_load(xa, 12345678)); | |
358 | xa_release(xa, 12345678); | |
359 | XA_BUG_ON(xa, !xa_empty(xa)); | |
360 | ||
361 | /* Releasing a used entry does nothing */ | |
362 | xa_reserve(xa, 12345678, GFP_KERNEL); | |
363 | XA_BUG_ON(xa, xa_store_index(xa, 12345678, GFP_NOWAIT) != NULL); | |
364 | xa_release(xa, 12345678); | |
365 | xa_erase_index(xa, 12345678); | |
366 | XA_BUG_ON(xa, !xa_empty(xa)); | |
367 | ||
368 | /* cmpxchg sees a reserved entry as NULL */ | |
369 | xa_reserve(xa, 12345678, GFP_KERNEL); | |
370 | XA_BUG_ON(xa, xa_cmpxchg(xa, 12345678, NULL, xa_mk_value(12345678), | |
371 | GFP_NOWAIT) != NULL); | |
372 | xa_release(xa, 12345678); | |
373 | xa_erase_index(xa, 12345678); | |
374 | XA_BUG_ON(xa, !xa_empty(xa)); | |
375 | ||
376 | /* Can iterate through a reserved entry */ | |
377 | xa_store_index(xa, 5, GFP_KERNEL); | |
378 | xa_reserve(xa, 6, GFP_KERNEL); | |
379 | xa_store_index(xa, 7, GFP_KERNEL); | |
380 | ||
381 | xa_for_each(xa, entry, index, ULONG_MAX, XA_PRESENT) { | |
382 | XA_BUG_ON(xa, index != 5 && index != 7); | |
383 | } | |
384 | xa_destroy(xa); | |
385 | } | |
386 | ||
b803b428 MW |
387 | static noinline void check_xas_erase(struct xarray *xa) |
388 | { | |
389 | XA_STATE(xas, xa, 0); | |
390 | void *entry; | |
391 | unsigned long i, j; | |
392 | ||
393 | for (i = 0; i < 200; i++) { | |
394 | for (j = i; j < 2 * i + 17; j++) { | |
395 | xas_set(&xas, j); | |
396 | do { | |
397 | xas_lock(&xas); | |
398 | xas_store(&xas, xa_mk_value(j)); | |
399 | xas_unlock(&xas); | |
400 | } while (xas_nomem(&xas, GFP_KERNEL)); | |
401 | } | |
402 | ||
403 | xas_set(&xas, ULONG_MAX); | |
404 | do { | |
405 | xas_lock(&xas); | |
406 | xas_store(&xas, xa_mk_value(0)); | |
407 | xas_unlock(&xas); | |
408 | } while (xas_nomem(&xas, GFP_KERNEL)); | |
409 | ||
410 | xas_lock(&xas); | |
411 | xas_store(&xas, NULL); | |
412 | ||
413 | xas_set(&xas, 0); | |
414 | j = i; | |
415 | xas_for_each(&xas, entry, ULONG_MAX) { | |
416 | XA_BUG_ON(xa, entry != xa_mk_value(j)); | |
417 | xas_store(&xas, NULL); | |
418 | j++; | |
419 | } | |
420 | xas_unlock(&xas); | |
421 | XA_BUG_ON(xa, !xa_empty(xa)); | |
422 | } | |
423 | } | |
424 | ||
4f06d630 MW |
425 | #ifdef CONFIG_XARRAY_MULTI |
426 | static noinline void check_multi_store_1(struct xarray *xa, unsigned long index, | |
427 | unsigned int order) | |
428 | { | |
429 | XA_STATE(xas, xa, index); | |
430 | unsigned long min = index & ~((1UL << order) - 1); | |
431 | unsigned long max = min + (1UL << order); | |
432 | ||
433 | xa_store_order(xa, index, order, xa_mk_value(index), GFP_KERNEL); | |
434 | XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_value(index)); | |
435 | XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_value(index)); | |
436 | XA_BUG_ON(xa, xa_load(xa, max) != NULL); | |
437 | XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL); | |
438 | ||
439 | XA_BUG_ON(xa, xas_store(&xas, xa_mk_value(min)) != xa_mk_value(index)); | |
440 | XA_BUG_ON(xa, xa_load(xa, min) != xa_mk_value(min)); | |
441 | XA_BUG_ON(xa, xa_load(xa, max - 1) != xa_mk_value(min)); | |
442 | XA_BUG_ON(xa, xa_load(xa, max) != NULL); | |
443 | XA_BUG_ON(xa, xa_load(xa, min - 1) != NULL); | |
444 | ||
445 | xa_erase_index(xa, min); | |
446 | XA_BUG_ON(xa, !xa_empty(xa)); | |
447 | } | |
448 | ||
449 | static noinline void check_multi_store_2(struct xarray *xa, unsigned long index, | |
450 | unsigned int order) | |
451 | { | |
452 | XA_STATE(xas, xa, index); | |
453 | xa_store_order(xa, index, order, xa_mk_value(0), GFP_KERNEL); | |
454 | ||
455 | XA_BUG_ON(xa, xas_store(&xas, xa_mk_value(1)) != xa_mk_value(0)); | |
456 | XA_BUG_ON(xa, xas.xa_index != index); | |
457 | XA_BUG_ON(xa, xas_store(&xas, NULL) != xa_mk_value(1)); | |
458 | XA_BUG_ON(xa, !xa_empty(xa)); | |
459 | } | |
460 | #endif | |
461 | ||
58d6ea30 MW |
462 | static noinline void check_multi_store(struct xarray *xa) |
463 | { | |
464 | #ifdef CONFIG_XARRAY_MULTI | |
465 | unsigned long i, j, k; | |
466 | unsigned int max_order = (sizeof(long) == 4) ? 30 : 60; | |
467 | ||
468 | /* Loading from any position returns the same value */ | |
469 | xa_store_order(xa, 0, 1, xa_mk_value(0), GFP_KERNEL); | |
470 | XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0)); | |
471 | XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0)); | |
472 | XA_BUG_ON(xa, xa_load(xa, 2) != NULL); | |
473 | rcu_read_lock(); | |
474 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 2); | |
475 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2); | |
476 | rcu_read_unlock(); | |
477 | ||
478 | /* Storing adjacent to the value does not alter the value */ | |
479 | xa_store(xa, 3, xa, GFP_KERNEL); | |
480 | XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(0)); | |
481 | XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(0)); | |
482 | XA_BUG_ON(xa, xa_load(xa, 2) != NULL); | |
483 | rcu_read_lock(); | |
484 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 3); | |
485 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 2); | |
486 | rcu_read_unlock(); | |
487 | ||
488 | /* Overwriting multiple indexes works */ | |
489 | xa_store_order(xa, 0, 2, xa_mk_value(1), GFP_KERNEL); | |
490 | XA_BUG_ON(xa, xa_load(xa, 0) != xa_mk_value(1)); | |
491 | XA_BUG_ON(xa, xa_load(xa, 1) != xa_mk_value(1)); | |
492 | XA_BUG_ON(xa, xa_load(xa, 2) != xa_mk_value(1)); | |
493 | XA_BUG_ON(xa, xa_load(xa, 3) != xa_mk_value(1)); | |
494 | XA_BUG_ON(xa, xa_load(xa, 4) != NULL); | |
495 | rcu_read_lock(); | |
496 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->count != 4); | |
497 | XA_BUG_ON(xa, xa_to_node(xa_head(xa))->nr_values != 4); | |
498 | rcu_read_unlock(); | |
499 | ||
500 | /* We can erase multiple values with a single store */ | |
501 | xa_store_order(xa, 0, 63, NULL, GFP_KERNEL); | |
502 | XA_BUG_ON(xa, !xa_empty(xa)); | |
503 | ||
504 | /* Even when the first slot is empty but the others aren't */ | |
505 | xa_store_index(xa, 1, GFP_KERNEL); | |
506 | xa_store_index(xa, 2, GFP_KERNEL); | |
507 | xa_store_order(xa, 0, 2, NULL, GFP_KERNEL); | |
508 | XA_BUG_ON(xa, !xa_empty(xa)); | |
509 | ||
510 | for (i = 0; i < max_order; i++) { | |
511 | for (j = 0; j < max_order; j++) { | |
512 | xa_store_order(xa, 0, i, xa_mk_value(i), GFP_KERNEL); | |
513 | xa_store_order(xa, 0, j, xa_mk_value(j), GFP_KERNEL); | |
514 | ||
515 | for (k = 0; k < max_order; k++) { | |
516 | void *entry = xa_load(xa, (1UL << k) - 1); | |
517 | if ((i < k) && (j < k)) | |
518 | XA_BUG_ON(xa, entry != NULL); | |
519 | else | |
520 | XA_BUG_ON(xa, entry != xa_mk_value(j)); | |
521 | } | |
522 | ||
523 | xa_erase(xa, 0); | |
524 | XA_BUG_ON(xa, !xa_empty(xa)); | |
525 | } | |
526 | } | |
4f06d630 MW |
527 | |
528 | for (i = 0; i < 20; i++) { | |
529 | check_multi_store_1(xa, 200, i); | |
530 | check_multi_store_1(xa, 0, i); | |
531 | check_multi_store_1(xa, (1UL << i) + 1, i); | |
532 | } | |
533 | check_multi_store_2(xa, 4095, 9); | |
58d6ea30 MW |
534 | #endif |
535 | } | |
536 | ||
371c752d MW |
537 | static DEFINE_XARRAY_ALLOC(xa0); |
538 | ||
539 | static noinline void check_xa_alloc(void) | |
540 | { | |
541 | int i; | |
542 | u32 id; | |
543 | ||
544 | /* An empty array should assign 0 to the first alloc */ | |
545 | xa_alloc_index(&xa0, 0, GFP_KERNEL); | |
546 | ||
547 | /* Erasing it should make the array empty again */ | |
548 | xa_erase_index(&xa0, 0); | |
549 | XA_BUG_ON(&xa0, !xa_empty(&xa0)); | |
550 | ||
551 | /* And it should assign 0 again */ | |
552 | xa_alloc_index(&xa0, 0, GFP_KERNEL); | |
553 | ||
554 | /* The next assigned ID should be 1 */ | |
555 | xa_alloc_index(&xa0, 1, GFP_KERNEL); | |
556 | xa_erase_index(&xa0, 1); | |
557 | ||
558 | /* Storing a value should mark it used */ | |
559 | xa_store_index(&xa0, 1, GFP_KERNEL); | |
560 | xa_alloc_index(&xa0, 2, GFP_KERNEL); | |
561 | ||
562 | /* If we then erase 0, it should be free */ | |
563 | xa_erase_index(&xa0, 0); | |
564 | xa_alloc_index(&xa0, 0, GFP_KERNEL); | |
565 | ||
566 | xa_erase_index(&xa0, 1); | |
567 | xa_erase_index(&xa0, 2); | |
568 | ||
569 | for (i = 1; i < 5000; i++) { | |
570 | xa_alloc_index(&xa0, i, GFP_KERNEL); | |
571 | } | |
572 | ||
573 | xa_destroy(&xa0); | |
574 | ||
575 | id = 0xfffffffeU; | |
576 | XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_value(0), | |
577 | GFP_KERNEL) != 0); | |
578 | XA_BUG_ON(&xa0, id != 0xfffffffeU); | |
579 | XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_value(0), | |
580 | GFP_KERNEL) != 0); | |
581 | XA_BUG_ON(&xa0, id != 0xffffffffU); | |
582 | XA_BUG_ON(&xa0, xa_alloc(&xa0, &id, UINT_MAX, xa_mk_value(0), | |
583 | GFP_KERNEL) != -ENOSPC); | |
584 | XA_BUG_ON(&xa0, id != 0xffffffffU); | |
585 | xa_destroy(&xa0); | |
586 | } | |
587 | ||
4e99d4e9 MW |
588 | static noinline void __check_store_iter(struct xarray *xa, unsigned long start, |
589 | unsigned int order, unsigned int present) | |
590 | { | |
591 | XA_STATE_ORDER(xas, xa, start, order); | |
592 | void *entry; | |
593 | unsigned int count = 0; | |
594 | ||
595 | retry: | |
596 | xas_lock(&xas); | |
597 | xas_for_each_conflict(&xas, entry) { | |
598 | XA_BUG_ON(xa, !xa_is_value(entry)); | |
599 | XA_BUG_ON(xa, entry < xa_mk_value(start)); | |
600 | XA_BUG_ON(xa, entry > xa_mk_value(start + (1UL << order) - 1)); | |
601 | count++; | |
602 | } | |
603 | xas_store(&xas, xa_mk_value(start)); | |
604 | xas_unlock(&xas); | |
605 | if (xas_nomem(&xas, GFP_KERNEL)) { | |
606 | count = 0; | |
607 | goto retry; | |
608 | } | |
609 | XA_BUG_ON(xa, xas_error(&xas)); | |
610 | XA_BUG_ON(xa, count != present); | |
611 | XA_BUG_ON(xa, xa_load(xa, start) != xa_mk_value(start)); | |
612 | XA_BUG_ON(xa, xa_load(xa, start + (1UL << order) - 1) != | |
613 | xa_mk_value(start)); | |
614 | xa_erase_index(xa, start); | |
615 | } | |
616 | ||
617 | static noinline void check_store_iter(struct xarray *xa) | |
618 | { | |
619 | unsigned int i, j; | |
620 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 20 : 1; | |
621 | ||
622 | for (i = 0; i < max_order; i++) { | |
623 | unsigned int min = 1 << i; | |
624 | unsigned int max = (2 << i) - 1; | |
625 | __check_store_iter(xa, 0, i, 0); | |
626 | XA_BUG_ON(xa, !xa_empty(xa)); | |
627 | __check_store_iter(xa, min, i, 0); | |
628 | XA_BUG_ON(xa, !xa_empty(xa)); | |
629 | ||
630 | xa_store_index(xa, min, GFP_KERNEL); | |
631 | __check_store_iter(xa, min, i, 1); | |
632 | XA_BUG_ON(xa, !xa_empty(xa)); | |
633 | xa_store_index(xa, max, GFP_KERNEL); | |
634 | __check_store_iter(xa, min, i, 1); | |
635 | XA_BUG_ON(xa, !xa_empty(xa)); | |
636 | ||
637 | for (j = 0; j < min; j++) | |
638 | xa_store_index(xa, j, GFP_KERNEL); | |
639 | __check_store_iter(xa, 0, i, min); | |
640 | XA_BUG_ON(xa, !xa_empty(xa)); | |
641 | for (j = 0; j < min; j++) | |
642 | xa_store_index(xa, min + j, GFP_KERNEL); | |
643 | __check_store_iter(xa, min, i, min); | |
644 | XA_BUG_ON(xa, !xa_empty(xa)); | |
645 | } | |
646 | #ifdef CONFIG_XARRAY_MULTI | |
647 | xa_store_index(xa, 63, GFP_KERNEL); | |
648 | xa_store_index(xa, 65, GFP_KERNEL); | |
649 | __check_store_iter(xa, 64, 2, 1); | |
650 | xa_erase_index(xa, 63); | |
651 | #endif | |
652 | XA_BUG_ON(xa, !xa_empty(xa)); | |
653 | } | |
654 | ||
b803b428 MW |
655 | static noinline void check_multi_find(struct xarray *xa) |
656 | { | |
657 | #ifdef CONFIG_XARRAY_MULTI | |
658 | unsigned long index; | |
659 | ||
660 | xa_store_order(xa, 12, 2, xa_mk_value(12), GFP_KERNEL); | |
661 | XA_BUG_ON(xa, xa_store_index(xa, 16, GFP_KERNEL) != NULL); | |
662 | ||
663 | index = 0; | |
664 | XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) != | |
665 | xa_mk_value(12)); | |
666 | XA_BUG_ON(xa, index != 12); | |
667 | index = 13; | |
668 | XA_BUG_ON(xa, xa_find(xa, &index, ULONG_MAX, XA_PRESENT) != | |
669 | xa_mk_value(12)); | |
670 | XA_BUG_ON(xa, (index < 12) || (index >= 16)); | |
671 | XA_BUG_ON(xa, xa_find_after(xa, &index, ULONG_MAX, XA_PRESENT) != | |
672 | xa_mk_value(16)); | |
673 | XA_BUG_ON(xa, index != 16); | |
674 | ||
675 | xa_erase_index(xa, 12); | |
676 | xa_erase_index(xa, 16); | |
677 | XA_BUG_ON(xa, !xa_empty(xa)); | |
678 | #endif | |
679 | } | |
680 | ||
681 | static noinline void check_multi_find_2(struct xarray *xa) | |
682 | { | |
683 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 10 : 1; | |
684 | unsigned int i, j; | |
685 | void *entry; | |
686 | ||
687 | for (i = 0; i < max_order; i++) { | |
688 | unsigned long index = 1UL << i; | |
689 | for (j = 0; j < index; j++) { | |
690 | XA_STATE(xas, xa, j + index); | |
691 | xa_store_index(xa, index - 1, GFP_KERNEL); | |
692 | xa_store_order(xa, index, i, xa_mk_value(index), | |
693 | GFP_KERNEL); | |
694 | rcu_read_lock(); | |
695 | xas_for_each(&xas, entry, ULONG_MAX) { | |
696 | xa_erase_index(xa, index); | |
697 | } | |
698 | rcu_read_unlock(); | |
699 | xa_erase_index(xa, index - 1); | |
700 | XA_BUG_ON(xa, !xa_empty(xa)); | |
701 | } | |
702 | } | |
703 | } | |
704 | ||
8229706e | 705 | static noinline void check_find_1(struct xarray *xa) |
b803b428 MW |
706 | { |
707 | unsigned long i, j, k; | |
708 | ||
709 | XA_BUG_ON(xa, !xa_empty(xa)); | |
710 | ||
711 | /* | |
712 | * Check xa_find with all pairs between 0 and 99 inclusive, | |
713 | * starting at every index between 0 and 99 | |
714 | */ | |
715 | for (i = 0; i < 100; i++) { | |
716 | XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); | |
717 | xa_set_mark(xa, i, XA_MARK_0); | |
718 | for (j = 0; j < i; j++) { | |
719 | XA_BUG_ON(xa, xa_store_index(xa, j, GFP_KERNEL) != | |
720 | NULL); | |
721 | xa_set_mark(xa, j, XA_MARK_0); | |
722 | for (k = 0; k < 100; k++) { | |
723 | unsigned long index = k; | |
724 | void *entry = xa_find(xa, &index, ULONG_MAX, | |
725 | XA_PRESENT); | |
726 | if (k <= j) | |
727 | XA_BUG_ON(xa, index != j); | |
728 | else if (k <= i) | |
729 | XA_BUG_ON(xa, index != i); | |
730 | else | |
731 | XA_BUG_ON(xa, entry != NULL); | |
732 | ||
733 | index = k; | |
734 | entry = xa_find(xa, &index, ULONG_MAX, | |
735 | XA_MARK_0); | |
736 | if (k <= j) | |
737 | XA_BUG_ON(xa, index != j); | |
738 | else if (k <= i) | |
739 | XA_BUG_ON(xa, index != i); | |
740 | else | |
741 | XA_BUG_ON(xa, entry != NULL); | |
742 | } | |
743 | xa_erase_index(xa, j); | |
744 | XA_BUG_ON(xa, xa_get_mark(xa, j, XA_MARK_0)); | |
745 | XA_BUG_ON(xa, !xa_get_mark(xa, i, XA_MARK_0)); | |
746 | } | |
747 | xa_erase_index(xa, i); | |
748 | XA_BUG_ON(xa, xa_get_mark(xa, i, XA_MARK_0)); | |
749 | } | |
750 | XA_BUG_ON(xa, !xa_empty(xa)); | |
8229706e MW |
751 | } |
752 | ||
753 | static noinline void check_find_2(struct xarray *xa) | |
754 | { | |
755 | void *entry; | |
756 | unsigned long i, j, index = 0; | |
757 | ||
758 | xa_for_each(xa, entry, index, ULONG_MAX, XA_PRESENT) { | |
759 | XA_BUG_ON(xa, true); | |
760 | } | |
761 | ||
762 | for (i = 0; i < 1024; i++) { | |
763 | xa_store_index(xa, index, GFP_KERNEL); | |
764 | j = 0; | |
765 | index = 0; | |
766 | xa_for_each(xa, entry, index, ULONG_MAX, XA_PRESENT) { | |
767 | XA_BUG_ON(xa, xa_mk_value(index) != entry); | |
768 | XA_BUG_ON(xa, index != j++); | |
769 | } | |
770 | } | |
771 | ||
772 | xa_destroy(xa); | |
773 | } | |
774 | ||
775 | static noinline void check_find(struct xarray *xa) | |
776 | { | |
777 | check_find_1(xa); | |
778 | check_find_2(xa); | |
b803b428 MW |
779 | check_multi_find(xa); |
780 | check_multi_find_2(xa); | |
781 | } | |
782 | ||
e21a2955 MW |
783 | /* See find_swap_entry() in mm/shmem.c */ |
784 | static noinline unsigned long xa_find_entry(struct xarray *xa, void *item) | |
785 | { | |
786 | XA_STATE(xas, xa, 0); | |
787 | unsigned int checked = 0; | |
788 | void *entry; | |
789 | ||
790 | rcu_read_lock(); | |
791 | xas_for_each(&xas, entry, ULONG_MAX) { | |
792 | if (xas_retry(&xas, entry)) | |
793 | continue; | |
794 | if (entry == item) | |
795 | break; | |
796 | checked++; | |
797 | if ((checked % 4) != 0) | |
798 | continue; | |
799 | xas_pause(&xas); | |
800 | } | |
801 | rcu_read_unlock(); | |
802 | ||
803 | return entry ? xas.xa_index : -1; | |
804 | } | |
805 | ||
806 | static noinline void check_find_entry(struct xarray *xa) | |
807 | { | |
808 | #ifdef CONFIG_XARRAY_MULTI | |
809 | unsigned int order; | |
810 | unsigned long offset, index; | |
811 | ||
812 | for (order = 0; order < 20; order++) { | |
813 | for (offset = 0; offset < (1UL << (order + 3)); | |
814 | offset += (1UL << order)) { | |
815 | for (index = 0; index < (1UL << (order + 5)); | |
816 | index += (1UL << order)) { | |
817 | xa_store_order(xa, index, order, | |
818 | xa_mk_value(index), GFP_KERNEL); | |
819 | XA_BUG_ON(xa, xa_load(xa, index) != | |
820 | xa_mk_value(index)); | |
821 | XA_BUG_ON(xa, xa_find_entry(xa, | |
822 | xa_mk_value(index)) != index); | |
823 | } | |
824 | XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); | |
825 | xa_destroy(xa); | |
826 | } | |
827 | } | |
828 | #endif | |
829 | ||
830 | XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); | |
831 | xa_store_index(xa, ULONG_MAX, GFP_KERNEL); | |
832 | XA_BUG_ON(xa, xa_find_entry(xa, xa) != -1); | |
833 | XA_BUG_ON(xa, xa_find_entry(xa, xa_mk_value(LONG_MAX)) != -1); | |
834 | xa_erase_index(xa, ULONG_MAX); | |
835 | XA_BUG_ON(xa, !xa_empty(xa)); | |
836 | } | |
837 | ||
64d3e9a9 MW |
838 | static noinline void check_move_small(struct xarray *xa, unsigned long idx) |
839 | { | |
840 | XA_STATE(xas, xa, 0); | |
841 | unsigned long i; | |
842 | ||
843 | xa_store_index(xa, 0, GFP_KERNEL); | |
844 | xa_store_index(xa, idx, GFP_KERNEL); | |
845 | ||
846 | rcu_read_lock(); | |
847 | for (i = 0; i < idx * 4; i++) { | |
848 | void *entry = xas_next(&xas); | |
849 | if (i <= idx) | |
850 | XA_BUG_ON(xa, xas.xa_node == XAS_RESTART); | |
851 | XA_BUG_ON(xa, xas.xa_index != i); | |
852 | if (i == 0 || i == idx) | |
853 | XA_BUG_ON(xa, entry != xa_mk_value(i)); | |
854 | else | |
855 | XA_BUG_ON(xa, entry != NULL); | |
856 | } | |
857 | xas_next(&xas); | |
858 | XA_BUG_ON(xa, xas.xa_index != i); | |
859 | ||
860 | do { | |
861 | void *entry = xas_prev(&xas); | |
862 | i--; | |
863 | if (i <= idx) | |
864 | XA_BUG_ON(xa, xas.xa_node == XAS_RESTART); | |
865 | XA_BUG_ON(xa, xas.xa_index != i); | |
866 | if (i == 0 || i == idx) | |
867 | XA_BUG_ON(xa, entry != xa_mk_value(i)); | |
868 | else | |
869 | XA_BUG_ON(xa, entry != NULL); | |
870 | } while (i > 0); | |
871 | ||
872 | xas_set(&xas, ULONG_MAX); | |
873 | XA_BUG_ON(xa, xas_next(&xas) != NULL); | |
874 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); | |
875 | XA_BUG_ON(xa, xas_next(&xas) != xa_mk_value(0)); | |
876 | XA_BUG_ON(xa, xas.xa_index != 0); | |
877 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); | |
878 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); | |
879 | rcu_read_unlock(); | |
880 | ||
881 | xa_erase_index(xa, 0); | |
882 | xa_erase_index(xa, idx); | |
883 | XA_BUG_ON(xa, !xa_empty(xa)); | |
884 | } | |
885 | ||
886 | static noinline void check_move(struct xarray *xa) | |
887 | { | |
888 | XA_STATE(xas, xa, (1 << 16) - 1); | |
889 | unsigned long i; | |
890 | ||
891 | for (i = 0; i < (1 << 16); i++) | |
892 | XA_BUG_ON(xa, xa_store_index(xa, i, GFP_KERNEL) != NULL); | |
893 | ||
894 | rcu_read_lock(); | |
895 | do { | |
896 | void *entry = xas_prev(&xas); | |
897 | i--; | |
898 | XA_BUG_ON(xa, entry != xa_mk_value(i)); | |
899 | XA_BUG_ON(xa, i != xas.xa_index); | |
900 | } while (i != 0); | |
901 | ||
902 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); | |
903 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); | |
904 | ||
905 | do { | |
906 | void *entry = xas_next(&xas); | |
907 | XA_BUG_ON(xa, entry != xa_mk_value(i)); | |
908 | XA_BUG_ON(xa, i != xas.xa_index); | |
909 | i++; | |
910 | } while (i < (1 << 16)); | |
911 | rcu_read_unlock(); | |
912 | ||
913 | for (i = (1 << 8); i < (1 << 15); i++) | |
914 | xa_erase_index(xa, i); | |
915 | ||
916 | i = xas.xa_index; | |
917 | ||
918 | rcu_read_lock(); | |
919 | do { | |
920 | void *entry = xas_prev(&xas); | |
921 | i--; | |
922 | if ((i < (1 << 8)) || (i >= (1 << 15))) | |
923 | XA_BUG_ON(xa, entry != xa_mk_value(i)); | |
924 | else | |
925 | XA_BUG_ON(xa, entry != NULL); | |
926 | XA_BUG_ON(xa, i != xas.xa_index); | |
927 | } while (i != 0); | |
928 | ||
929 | XA_BUG_ON(xa, xas_prev(&xas) != NULL); | |
930 | XA_BUG_ON(xa, xas.xa_index != ULONG_MAX); | |
931 | ||
932 | do { | |
933 | void *entry = xas_next(&xas); | |
934 | if ((i < (1 << 8)) || (i >= (1 << 15))) | |
935 | XA_BUG_ON(xa, entry != xa_mk_value(i)); | |
936 | else | |
937 | XA_BUG_ON(xa, entry != NULL); | |
938 | XA_BUG_ON(xa, i != xas.xa_index); | |
939 | i++; | |
940 | } while (i < (1 << 16)); | |
941 | rcu_read_unlock(); | |
942 | ||
943 | xa_destroy(xa); | |
944 | ||
945 | for (i = 0; i < 16; i++) | |
946 | check_move_small(xa, 1UL << i); | |
947 | ||
948 | for (i = 2; i < 16; i++) | |
949 | check_move_small(xa, (1UL << i) - 1); | |
950 | } | |
951 | ||
2264f513 MW |
952 | static noinline void xa_store_many_order(struct xarray *xa, |
953 | unsigned long index, unsigned order) | |
954 | { | |
955 | XA_STATE_ORDER(xas, xa, index, order); | |
956 | unsigned int i = 0; | |
957 | ||
958 | do { | |
959 | xas_lock(&xas); | |
960 | XA_BUG_ON(xa, xas_find_conflict(&xas)); | |
961 | xas_create_range(&xas); | |
962 | if (xas_error(&xas)) | |
963 | goto unlock; | |
964 | for (i = 0; i < (1U << order); i++) { | |
965 | XA_BUG_ON(xa, xas_store(&xas, xa_mk_value(index + i))); | |
966 | xas_next(&xas); | |
967 | } | |
968 | unlock: | |
969 | xas_unlock(&xas); | |
970 | } while (xas_nomem(&xas, GFP_KERNEL)); | |
971 | ||
972 | XA_BUG_ON(xa, xas_error(&xas)); | |
973 | } | |
974 | ||
975 | static noinline void check_create_range_1(struct xarray *xa, | |
976 | unsigned long index, unsigned order) | |
977 | { | |
978 | unsigned long i; | |
979 | ||
980 | xa_store_many_order(xa, index, order); | |
981 | for (i = index; i < index + (1UL << order); i++) | |
982 | xa_erase_index(xa, i); | |
983 | XA_BUG_ON(xa, !xa_empty(xa)); | |
984 | } | |
985 | ||
986 | static noinline void check_create_range_2(struct xarray *xa, unsigned order) | |
987 | { | |
988 | unsigned long i; | |
989 | unsigned long nr = 1UL << order; | |
990 | ||
991 | for (i = 0; i < nr * nr; i += nr) | |
992 | xa_store_many_order(xa, i, order); | |
993 | for (i = 0; i < nr * nr; i++) | |
994 | xa_erase_index(xa, i); | |
995 | XA_BUG_ON(xa, !xa_empty(xa)); | |
996 | } | |
997 | ||
998 | static noinline void check_create_range_3(void) | |
999 | { | |
1000 | XA_STATE(xas, NULL, 0); | |
1001 | xas_set_err(&xas, -EEXIST); | |
1002 | xas_create_range(&xas); | |
1003 | XA_BUG_ON(NULL, xas_error(&xas) != -EEXIST); | |
1004 | } | |
1005 | ||
1006 | static noinline void check_create_range_4(struct xarray *xa, | |
1007 | unsigned long index, unsigned order) | |
1008 | { | |
1009 | XA_STATE_ORDER(xas, xa, index, order); | |
1010 | unsigned long base = xas.xa_index; | |
1011 | unsigned long i = 0; | |
1012 | ||
1013 | xa_store_index(xa, index, GFP_KERNEL); | |
1014 | do { | |
1015 | xas_lock(&xas); | |
1016 | xas_create_range(&xas); | |
1017 | if (xas_error(&xas)) | |
1018 | goto unlock; | |
1019 | for (i = 0; i < (1UL << order); i++) { | |
1020 | void *old = xas_store(&xas, xa_mk_value(base + i)); | |
1021 | if (xas.xa_index == index) | |
1022 | XA_BUG_ON(xa, old != xa_mk_value(base + i)); | |
1023 | else | |
1024 | XA_BUG_ON(xa, old != NULL); | |
1025 | xas_next(&xas); | |
1026 | } | |
1027 | unlock: | |
1028 | xas_unlock(&xas); | |
1029 | } while (xas_nomem(&xas, GFP_KERNEL)); | |
1030 | ||
1031 | XA_BUG_ON(xa, xas_error(&xas)); | |
1032 | ||
1033 | for (i = base; i < base + (1UL << order); i++) | |
1034 | xa_erase_index(xa, i); | |
1035 | XA_BUG_ON(xa, !xa_empty(xa)); | |
1036 | } | |
1037 | ||
1038 | static noinline void check_create_range(struct xarray *xa) | |
1039 | { | |
1040 | unsigned int order; | |
1041 | unsigned int max_order = IS_ENABLED(CONFIG_XARRAY_MULTI) ? 12 : 1; | |
1042 | ||
1043 | for (order = 0; order < max_order; order++) { | |
1044 | check_create_range_1(xa, 0, order); | |
1045 | check_create_range_1(xa, 1U << order, order); | |
1046 | check_create_range_1(xa, 2U << order, order); | |
1047 | check_create_range_1(xa, 3U << order, order); | |
1048 | check_create_range_1(xa, 1U << 24, order); | |
1049 | if (order < 10) | |
1050 | check_create_range_2(xa, order); | |
1051 | ||
1052 | check_create_range_4(xa, 0, order); | |
1053 | check_create_range_4(xa, 1U << order, order); | |
1054 | check_create_range_4(xa, 2U << order, order); | |
1055 | check_create_range_4(xa, 3U << order, order); | |
1056 | check_create_range_4(xa, 1U << 24, order); | |
1057 | ||
1058 | check_create_range_4(xa, 1, order); | |
1059 | check_create_range_4(xa, (1U << order) + 1, order); | |
1060 | check_create_range_4(xa, (2U << order) + 1, order); | |
1061 | check_create_range_4(xa, (2U << order) - 1, order); | |
1062 | check_create_range_4(xa, (3U << order) + 1, order); | |
1063 | check_create_range_4(xa, (3U << order) - 1, order); | |
1064 | check_create_range_4(xa, (1U << 24) + 1, order); | |
1065 | } | |
1066 | ||
1067 | check_create_range_3(); | |
1068 | } | |
1069 | ||
0e9446c3 MW |
1070 | static noinline void __check_store_range(struct xarray *xa, unsigned long first, |
1071 | unsigned long last) | |
1072 | { | |
1073 | #ifdef CONFIG_XARRAY_MULTI | |
1074 | xa_store_range(xa, first, last, xa_mk_value(first), GFP_KERNEL); | |
1075 | ||
1076 | XA_BUG_ON(xa, xa_load(xa, first) != xa_mk_value(first)); | |
1077 | XA_BUG_ON(xa, xa_load(xa, last) != xa_mk_value(first)); | |
1078 | XA_BUG_ON(xa, xa_load(xa, first - 1) != NULL); | |
1079 | XA_BUG_ON(xa, xa_load(xa, last + 1) != NULL); | |
1080 | ||
1081 | xa_store_range(xa, first, last, NULL, GFP_KERNEL); | |
1082 | #endif | |
1083 | ||
1084 | XA_BUG_ON(xa, !xa_empty(xa)); | |
1085 | } | |
1086 | ||
1087 | static noinline void check_store_range(struct xarray *xa) | |
1088 | { | |
1089 | unsigned long i, j; | |
1090 | ||
1091 | for (i = 0; i < 128; i++) { | |
1092 | for (j = i; j < 128; j++) { | |
1093 | __check_store_range(xa, i, j); | |
1094 | __check_store_range(xa, 128 + i, 128 + j); | |
1095 | __check_store_range(xa, 4095 + i, 4095 + j); | |
1096 | __check_store_range(xa, 4096 + i, 4096 + j); | |
1097 | __check_store_range(xa, 123456 + i, 123456 + j); | |
1098 | __check_store_range(xa, UINT_MAX + i, UINT_MAX + j); | |
1099 | } | |
1100 | } | |
1101 | } | |
1102 | ||
a97e7904 MW |
1103 | static LIST_HEAD(shadow_nodes); |
1104 | ||
1105 | static void test_update_node(struct xa_node *node) | |
1106 | { | |
1107 | if (node->count && node->count == node->nr_values) { | |
1108 | if (list_empty(&node->private_list)) | |
1109 | list_add(&shadow_nodes, &node->private_list); | |
1110 | } else { | |
1111 | if (!list_empty(&node->private_list)) | |
1112 | list_del_init(&node->private_list); | |
1113 | } | |
1114 | } | |
1115 | ||
1116 | static noinline void shadow_remove(struct xarray *xa) | |
1117 | { | |
1118 | struct xa_node *node; | |
1119 | ||
1120 | xa_lock(xa); | |
1121 | while ((node = list_first_entry_or_null(&shadow_nodes, | |
1122 | struct xa_node, private_list))) { | |
1123 | XA_STATE(xas, node->array, 0); | |
1124 | XA_BUG_ON(xa, node->array != xa); | |
1125 | list_del_init(&node->private_list); | |
1126 | xas.xa_node = xa_parent_locked(node->array, node); | |
1127 | xas.xa_offset = node->offset; | |
1128 | xas.xa_shift = node->shift + XA_CHUNK_SHIFT; | |
1129 | xas_set_update(&xas, test_update_node); | |
1130 | xas_store(&xas, NULL); | |
1131 | } | |
1132 | xa_unlock(xa); | |
1133 | } | |
1134 | ||
1135 | static noinline void check_workingset(struct xarray *xa, unsigned long index) | |
1136 | { | |
1137 | XA_STATE(xas, xa, index); | |
1138 | xas_set_update(&xas, test_update_node); | |
1139 | ||
1140 | do { | |
1141 | xas_lock(&xas); | |
1142 | xas_store(&xas, xa_mk_value(0)); | |
1143 | xas_next(&xas); | |
1144 | xas_store(&xas, xa_mk_value(1)); | |
1145 | xas_unlock(&xas); | |
1146 | } while (xas_nomem(&xas, GFP_KERNEL)); | |
1147 | ||
1148 | XA_BUG_ON(xa, list_empty(&shadow_nodes)); | |
1149 | ||
1150 | xas_lock(&xas); | |
1151 | xas_next(&xas); | |
1152 | xas_store(&xas, &xas); | |
1153 | XA_BUG_ON(xa, !list_empty(&shadow_nodes)); | |
1154 | ||
1155 | xas_store(&xas, xa_mk_value(2)); | |
1156 | xas_unlock(&xas); | |
1157 | XA_BUG_ON(xa, list_empty(&shadow_nodes)); | |
1158 | ||
1159 | shadow_remove(xa); | |
1160 | XA_BUG_ON(xa, !list_empty(&shadow_nodes)); | |
1161 | XA_BUG_ON(xa, !xa_empty(xa)); | |
1162 | } | |
1163 | ||
d6427f81 MW |
1164 | /* |
1165 | * Check that the pointer / value / sibling entries are accounted the | |
1166 | * way we expect them to be. | |
1167 | */ | |
1168 | static noinline void check_account(struct xarray *xa) | |
1169 | { | |
1170 | #ifdef CONFIG_XARRAY_MULTI | |
1171 | unsigned int order; | |
1172 | ||
1173 | for (order = 1; order < 12; order++) { | |
1174 | XA_STATE(xas, xa, 1 << order); | |
1175 | ||
1176 | xa_store_order(xa, 0, order, xa, GFP_KERNEL); | |
1177 | xas_load(&xas); | |
1178 | XA_BUG_ON(xa, xas.xa_node->count == 0); | |
1179 | XA_BUG_ON(xa, xas.xa_node->count > (1 << order)); | |
1180 | XA_BUG_ON(xa, xas.xa_node->nr_values != 0); | |
1181 | ||
1182 | xa_store_order(xa, 1 << order, order, xa_mk_value(1 << order), | |
1183 | GFP_KERNEL); | |
1184 | XA_BUG_ON(xa, xas.xa_node->count != xas.xa_node->nr_values * 2); | |
1185 | ||
1186 | xa_erase(xa, 1 << order); | |
1187 | XA_BUG_ON(xa, xas.xa_node->nr_values != 0); | |
1188 | ||
1189 | xa_erase(xa, 0); | |
1190 | XA_BUG_ON(xa, !xa_empty(xa)); | |
1191 | } | |
1192 | #endif | |
1193 | } | |
1194 | ||
687149fc MW |
1195 | static noinline void check_destroy(struct xarray *xa) |
1196 | { | |
1197 | unsigned long index; | |
1198 | ||
1199 | XA_BUG_ON(xa, !xa_empty(xa)); | |
1200 | ||
1201 | /* Destroying an empty array is a no-op */ | |
1202 | xa_destroy(xa); | |
1203 | XA_BUG_ON(xa, !xa_empty(xa)); | |
1204 | ||
1205 | /* Destroying an array with a single entry */ | |
1206 | for (index = 0; index < 1000; index++) { | |
1207 | xa_store_index(xa, index, GFP_KERNEL); | |
1208 | XA_BUG_ON(xa, xa_empty(xa)); | |
1209 | xa_destroy(xa); | |
1210 | XA_BUG_ON(xa, !xa_empty(xa)); | |
1211 | } | |
1212 | ||
1213 | /* Destroying an array with a single entry at ULONG_MAX */ | |
1214 | xa_store(xa, ULONG_MAX, xa, GFP_KERNEL); | |
1215 | XA_BUG_ON(xa, xa_empty(xa)); | |
1216 | xa_destroy(xa); | |
1217 | XA_BUG_ON(xa, !xa_empty(xa)); | |
1218 | ||
1219 | #ifdef CONFIG_XARRAY_MULTI | |
1220 | /* Destroying an array with a multi-index entry */ | |
1221 | xa_store_order(xa, 1 << 11, 11, xa, GFP_KERNEL); | |
1222 | XA_BUG_ON(xa, xa_empty(xa)); | |
1223 | xa_destroy(xa); | |
1224 | XA_BUG_ON(xa, !xa_empty(xa)); | |
1225 | #endif | |
1226 | } | |
1227 | ||
58d6ea30 | 1228 | static DEFINE_XARRAY(array); |
ad3d6c72 MW |
1229 | |
1230 | static int xarray_checks(void) | |
1231 | { | |
58d6ea30 | 1232 | check_xa_err(&array); |
b803b428 | 1233 | check_xas_retry(&array); |
ad3d6c72 | 1234 | check_xa_load(&array); |
9b89a035 | 1235 | check_xa_mark(&array); |
58d6ea30 | 1236 | check_xa_shrink(&array); |
b803b428 | 1237 | check_xas_erase(&array); |
41aec91f | 1238 | check_cmpxchg(&array); |
9f14d4f1 | 1239 | check_reserve(&array); |
58d6ea30 | 1240 | check_multi_store(&array); |
371c752d | 1241 | check_xa_alloc(); |
b803b428 | 1242 | check_find(&array); |
e21a2955 | 1243 | check_find_entry(&array); |
d6427f81 | 1244 | check_account(&array); |
687149fc | 1245 | check_destroy(&array); |
64d3e9a9 | 1246 | check_move(&array); |
2264f513 | 1247 | check_create_range(&array); |
0e9446c3 | 1248 | check_store_range(&array); |
4e99d4e9 | 1249 | check_store_iter(&array); |
ad3d6c72 | 1250 | |
a97e7904 MW |
1251 | check_workingset(&array, 0); |
1252 | check_workingset(&array, 64); | |
1253 | check_workingset(&array, 4096); | |
1254 | ||
ad3d6c72 MW |
1255 | printk("XArray: %u of %u tests passed\n", tests_passed, tests_run); |
1256 | return (tests_run == tests_passed) ? 0 : -EINVAL; | |
1257 | } | |
1258 | ||
1259 | static void xarray_exit(void) | |
1260 | { | |
1261 | } | |
1262 | ||
1263 | module_init(xarray_checks); | |
1264 | module_exit(xarray_exit); | |
1265 | MODULE_AUTHOR("Matthew Wilcox <willy@infradead.org>"); | |
1266 | MODULE_LICENSE("GPL"); |