Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / testing / radix-tree / idr-test.c
blob6c645eb77d4218fd102968fb2a24b19350e3edd9
1 /*
2 * idr-test.c: Test the IDR API
3 * Copyright (c) 2016 Matthew Wilcox <willy@infradead.org>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 #include <linux/bitmap.h>
15 #include <linux/idr.h>
16 #include <linux/slab.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
20 #include "test.h"
22 #define DUMMY_PTR ((void *)0x12)
24 int item_idr_free(int id, void *p, void *data)
26 struct item *item = p;
27 assert(item->index == id);
28 free(p);
30 return 0;
33 void item_idr_remove(struct idr *idr, int id)
35 struct item *item = idr_find(idr, id);
36 assert(item->index == id);
37 idr_remove(idr, id);
38 free(item);
41 void idr_alloc_test(void)
43 unsigned long i;
44 DEFINE_IDR(idr);
46 assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0, 0x4000, GFP_KERNEL) == 0);
47 assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0x3ffd, 0x4000, GFP_KERNEL) == 0x3ffd);
48 idr_remove(&idr, 0x3ffd);
49 idr_remove(&idr, 0);
51 for (i = 0x3ffe; i < 0x4003; i++) {
52 int id;
53 struct item *item;
55 if (i < 0x4000)
56 item = item_create(i, 0);
57 else
58 item = item_create(i - 0x3fff, 0);
60 id = idr_alloc_cyclic(&idr, item, 1, 0x4000, GFP_KERNEL);
61 assert(id == item->index);
64 idr_for_each(&idr, item_idr_free, &idr);
65 idr_destroy(&idr);
68 void idr_replace_test(void)
70 DEFINE_IDR(idr);
72 idr_alloc(&idr, (void *)-1, 10, 11, GFP_KERNEL);
73 idr_replace(&idr, &idr, 10);
75 idr_destroy(&idr);
79 * Unlike the radix tree, you can put a NULL pointer -- with care -- into
80 * the IDR. Some interfaces, like idr_find() do not distinguish between
81 * "present, value is NULL" and "not present", but that's exactly what some
82 * users want.
84 void idr_null_test(void)
86 int i;
87 DEFINE_IDR(idr);
89 assert(idr_is_empty(&idr));
91 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
92 assert(!idr_is_empty(&idr));
93 idr_remove(&idr, 0);
94 assert(idr_is_empty(&idr));
96 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
97 assert(!idr_is_empty(&idr));
98 idr_destroy(&idr);
99 assert(idr_is_empty(&idr));
101 for (i = 0; i < 10; i++) {
102 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == i);
105 assert(idr_replace(&idr, DUMMY_PTR, 3) == NULL);
106 assert(idr_replace(&idr, DUMMY_PTR, 4) == NULL);
107 assert(idr_replace(&idr, NULL, 4) == DUMMY_PTR);
108 assert(idr_replace(&idr, DUMMY_PTR, 11) == ERR_PTR(-ENOENT));
109 idr_remove(&idr, 5);
110 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 5);
111 idr_remove(&idr, 5);
113 for (i = 0; i < 9; i++) {
114 idr_remove(&idr, i);
115 assert(!idr_is_empty(&idr));
117 idr_remove(&idr, 8);
118 assert(!idr_is_empty(&idr));
119 idr_remove(&idr, 9);
120 assert(idr_is_empty(&idr));
122 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
123 assert(idr_replace(&idr, DUMMY_PTR, 3) == ERR_PTR(-ENOENT));
124 assert(idr_replace(&idr, DUMMY_PTR, 0) == NULL);
125 assert(idr_replace(&idr, NULL, 0) == DUMMY_PTR);
127 idr_destroy(&idr);
128 assert(idr_is_empty(&idr));
130 for (i = 1; i < 10; i++) {
131 assert(idr_alloc(&idr, NULL, 1, 0, GFP_KERNEL) == i);
134 idr_destroy(&idr);
135 assert(idr_is_empty(&idr));
138 void idr_nowait_test(void)
140 unsigned int i;
141 DEFINE_IDR(idr);
143 idr_preload(GFP_KERNEL);
145 for (i = 0; i < 3; i++) {
146 struct item *item = item_create(i, 0);
147 assert(idr_alloc(&idr, item, i, i + 1, GFP_NOWAIT) == i);
150 idr_preload_end();
152 idr_for_each(&idr, item_idr_free, &idr);
153 idr_destroy(&idr);
156 void idr_get_next_test(int base)
158 unsigned long i;
159 int nextid;
160 DEFINE_IDR(idr);
161 idr_init_base(&idr, base);
163 int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0};
165 for(i = 0; indices[i]; i++) {
166 struct item *item = item_create(indices[i], 0);
167 assert(idr_alloc(&idr, item, indices[i], indices[i+1],
168 GFP_KERNEL) == indices[i]);
171 for(i = 0, nextid = 0; indices[i]; i++) {
172 idr_get_next(&idr, &nextid);
173 assert(nextid == indices[i]);
174 nextid++;
177 idr_for_each(&idr, item_idr_free, &idr);
178 idr_destroy(&idr);
181 int idr_u32_cb(int id, void *ptr, void *data)
183 BUG_ON(id < 0);
184 BUG_ON(ptr != DUMMY_PTR);
185 return 0;
188 void idr_u32_test1(struct idr *idr, u32 handle)
190 static bool warned = false;
191 u32 id = handle;
192 int sid = 0;
193 void *ptr;
195 BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL));
196 BUG_ON(id != handle);
197 BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL) != -ENOSPC);
198 BUG_ON(id != handle);
199 if (!warned && id > INT_MAX)
200 printk("vvv Ignore these warnings\n");
201 ptr = idr_get_next(idr, &sid);
202 if (id > INT_MAX) {
203 BUG_ON(ptr != NULL);
204 BUG_ON(sid != 0);
205 } else {
206 BUG_ON(ptr != DUMMY_PTR);
207 BUG_ON(sid != id);
209 idr_for_each(idr, idr_u32_cb, NULL);
210 if (!warned && id > INT_MAX) {
211 printk("^^^ Warnings over\n");
212 warned = true;
214 BUG_ON(idr_remove(idr, id) != DUMMY_PTR);
215 BUG_ON(!idr_is_empty(idr));
218 void idr_u32_test(int base)
220 DEFINE_IDR(idr);
221 idr_init_base(&idr, base);
222 idr_u32_test1(&idr, 10);
223 idr_u32_test1(&idr, 0x7fffffff);
224 idr_u32_test1(&idr, 0x80000000);
225 idr_u32_test1(&idr, 0x80000001);
226 idr_u32_test1(&idr, 0xffe00000);
227 idr_u32_test1(&idr, 0xffffffff);
230 void idr_checks(void)
232 unsigned long i;
233 DEFINE_IDR(idr);
235 for (i = 0; i < 10000; i++) {
236 struct item *item = item_create(i, 0);
237 assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
240 assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
242 for (i = 0; i < 5000; i++)
243 item_idr_remove(&idr, i);
245 idr_remove(&idr, 3);
247 idr_for_each(&idr, item_idr_free, &idr);
248 idr_destroy(&idr);
250 assert(idr_is_empty(&idr));
252 idr_remove(&idr, 3);
253 idr_remove(&idr, 0);
255 for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
256 struct item *item = item_create(i, 0);
257 assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
259 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
260 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC);
262 idr_for_each(&idr, item_idr_free, &idr);
263 idr_destroy(&idr);
264 idr_destroy(&idr);
266 assert(idr_is_empty(&idr));
268 idr_set_cursor(&idr, INT_MAX - 3UL);
269 for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) {
270 struct item *item;
271 unsigned int id;
272 if (i <= INT_MAX)
273 item = item_create(i, 0);
274 else
275 item = item_create(i - INT_MAX - 1, 0);
277 id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL);
278 assert(id == item->index);
281 idr_for_each(&idr, item_idr_free, &idr);
282 idr_destroy(&idr);
283 assert(idr_is_empty(&idr));
285 for (i = 1; i < 10000; i++) {
286 struct item *item = item_create(i, 0);
287 assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
290 idr_for_each(&idr, item_idr_free, &idr);
291 idr_destroy(&idr);
293 idr_replace_test();
294 idr_alloc_test();
295 idr_null_test();
296 idr_nowait_test();
297 idr_get_next_test(0);
298 idr_get_next_test(1);
299 idr_get_next_test(4);
300 idr_u32_test(4);
301 idr_u32_test(1);
302 idr_u32_test(0);
306 * Check that we get the correct error when we run out of memory doing
307 * allocations. To ensure we run out of memory, just "forget" to preload.
308 * The first test is for not having a bitmap available, and the second test
309 * is for not being able to allocate a level of the radix tree.
311 void ida_check_nomem(void)
313 DEFINE_IDA(ida);
314 int id, err;
316 err = ida_get_new_above(&ida, 256, &id);
317 assert(err == -EAGAIN);
318 err = ida_get_new_above(&ida, 1UL << 30, &id);
319 assert(err == -EAGAIN);
323 * Check what happens when we fill a leaf and then delete it. This may
324 * discover mishandling of IDR_FREE.
326 void ida_check_leaf(void)
328 DEFINE_IDA(ida);
329 int id;
330 unsigned long i;
332 for (i = 0; i < IDA_BITMAP_BITS; i++) {
333 assert(ida_pre_get(&ida, GFP_KERNEL));
334 assert(!ida_get_new(&ida, &id));
335 assert(id == i);
338 ida_destroy(&ida);
339 assert(ida_is_empty(&ida));
341 assert(ida_pre_get(&ida, GFP_KERNEL));
342 assert(!ida_get_new(&ida, &id));
343 assert(id == 0);
344 ida_destroy(&ida);
345 assert(ida_is_empty(&ida));
349 * Check handling of conversions between exceptional entries and full bitmaps.
351 void ida_check_conv(void)
353 DEFINE_IDA(ida);
354 int id;
355 unsigned long i;
357 for (i = 0; i < IDA_BITMAP_BITS * 2; i += IDA_BITMAP_BITS) {
358 assert(ida_pre_get(&ida, GFP_KERNEL));
359 assert(!ida_get_new_above(&ida, i + 1, &id));
360 assert(id == i + 1);
361 assert(!ida_get_new_above(&ida, i + BITS_PER_LONG, &id));
362 assert(id == i + BITS_PER_LONG);
363 ida_remove(&ida, i + 1);
364 ida_remove(&ida, i + BITS_PER_LONG);
365 assert(ida_is_empty(&ida));
368 assert(ida_pre_get(&ida, GFP_KERNEL));
370 for (i = 0; i < IDA_BITMAP_BITS * 2; i++) {
371 assert(ida_pre_get(&ida, GFP_KERNEL));
372 assert(!ida_get_new(&ida, &id));
373 assert(id == i);
376 for (i = IDA_BITMAP_BITS * 2; i > 0; i--) {
377 ida_remove(&ida, i - 1);
379 assert(ida_is_empty(&ida));
381 for (i = 0; i < IDA_BITMAP_BITS + BITS_PER_LONG - 4; i++) {
382 assert(ida_pre_get(&ida, GFP_KERNEL));
383 assert(!ida_get_new(&ida, &id));
384 assert(id == i);
387 for (i = IDA_BITMAP_BITS + BITS_PER_LONG - 4; i > 0; i--) {
388 ida_remove(&ida, i - 1);
390 assert(ida_is_empty(&ida));
392 radix_tree_cpu_dead(1);
393 for (i = 0; i < 1000000; i++) {
394 int err = ida_get_new(&ida, &id);
395 if (err == -EAGAIN) {
396 assert((i % IDA_BITMAP_BITS) == (BITS_PER_LONG - 2));
397 assert(ida_pre_get(&ida, GFP_KERNEL));
398 err = ida_get_new(&ida, &id);
399 } else {
400 assert((i % IDA_BITMAP_BITS) != (BITS_PER_LONG - 2));
402 assert(!err);
403 assert(id == i);
405 ida_destroy(&ida);
409 * Check allocations up to and slightly above the maximum allowed (2^31-1) ID.
410 * Allocating up to 2^31-1 should succeed, and then allocating the next one
411 * should fail.
413 void ida_check_max(void)
415 DEFINE_IDA(ida);
416 int id, err;
417 unsigned long i, j;
419 for (j = 1; j < 65537; j *= 2) {
420 unsigned long base = (1UL << 31) - j;
421 for (i = 0; i < j; i++) {
422 assert(ida_pre_get(&ida, GFP_KERNEL));
423 assert(!ida_get_new_above(&ida, base, &id));
424 assert(id == base + i);
426 assert(ida_pre_get(&ida, GFP_KERNEL));
427 err = ida_get_new_above(&ida, base, &id);
428 assert(err == -ENOSPC);
429 ida_destroy(&ida);
430 assert(ida_is_empty(&ida));
431 rcu_barrier();
435 void ida_check_random(void)
437 DEFINE_IDA(ida);
438 DECLARE_BITMAP(bitmap, 2048);
439 int id, err;
440 unsigned int i;
441 time_t s = time(NULL);
443 repeat:
444 memset(bitmap, 0, sizeof(bitmap));
445 for (i = 0; i < 100000; i++) {
446 int i = rand();
447 int bit = i & 2047;
448 if (test_bit(bit, bitmap)) {
449 __clear_bit(bit, bitmap);
450 ida_remove(&ida, bit);
451 } else {
452 __set_bit(bit, bitmap);
453 do {
454 ida_pre_get(&ida, GFP_KERNEL);
455 err = ida_get_new_above(&ida, bit, &id);
456 } while (err == -EAGAIN);
457 assert(!err);
458 assert(id == bit);
461 ida_destroy(&ida);
462 if (time(NULL) < s + 10)
463 goto repeat;
466 void ida_simple_get_remove_test(void)
468 DEFINE_IDA(ida);
469 unsigned long i;
471 for (i = 0; i < 10000; i++) {
472 assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
474 assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
476 for (i = 0; i < 10000; i++) {
477 ida_simple_remove(&ida, i);
479 assert(ida_is_empty(&ida));
481 ida_destroy(&ida);
484 void ida_checks(void)
486 DEFINE_IDA(ida);
487 int id;
488 unsigned long i;
490 radix_tree_cpu_dead(1);
491 ida_check_nomem();
493 for (i = 0; i < 10000; i++) {
494 assert(ida_pre_get(&ida, GFP_KERNEL));
495 assert(!ida_get_new(&ida, &id));
496 assert(id == i);
499 ida_remove(&ida, 20);
500 ida_remove(&ida, 21);
501 for (i = 0; i < 3; i++) {
502 assert(ida_pre_get(&ida, GFP_KERNEL));
503 assert(!ida_get_new(&ida, &id));
504 if (i == 2)
505 assert(id == 10000);
508 for (i = 0; i < 5000; i++)
509 ida_remove(&ida, i);
511 assert(ida_pre_get(&ida, GFP_KERNEL));
512 assert(!ida_get_new_above(&ida, 5000, &id));
513 assert(id == 10001);
515 ida_destroy(&ida);
517 assert(ida_is_empty(&ida));
519 assert(ida_pre_get(&ida, GFP_KERNEL));
520 assert(!ida_get_new_above(&ida, 1, &id));
521 assert(id == 1);
523 ida_remove(&ida, id);
524 assert(ida_is_empty(&ida));
525 ida_destroy(&ida);
526 assert(ida_is_empty(&ida));
528 assert(ida_pre_get(&ida, GFP_KERNEL));
529 assert(!ida_get_new_above(&ida, 1, &id));
530 ida_destroy(&ida);
531 assert(ida_is_empty(&ida));
533 assert(ida_pre_get(&ida, GFP_KERNEL));
534 assert(!ida_get_new_above(&ida, 1, &id));
535 assert(id == 1);
536 assert(ida_pre_get(&ida, GFP_KERNEL));
537 assert(!ida_get_new_above(&ida, 1025, &id));
538 assert(id == 1025);
539 assert(ida_pre_get(&ida, GFP_KERNEL));
540 assert(!ida_get_new_above(&ida, 10000, &id));
541 assert(id == 10000);
542 ida_remove(&ida, 1025);
543 ida_destroy(&ida);
544 assert(ida_is_empty(&ida));
546 ida_check_leaf();
547 ida_check_max();
548 ida_check_conv();
549 ida_check_random();
550 ida_simple_get_remove_test();
552 radix_tree_cpu_dead(1);
555 static void *ida_random_fn(void *arg)
557 rcu_register_thread();
558 ida_check_random();
559 rcu_unregister_thread();
560 return NULL;
563 void ida_thread_tests(void)
565 pthread_t threads[20];
566 int i;
568 for (i = 0; i < ARRAY_SIZE(threads); i++)
569 if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
570 perror("creating ida thread");
571 exit(1);
574 while (i--)
575 pthread_join(threads[i], NULL);
578 int __weak main(void)
580 radix_tree_init();
581 idr_checks();
582 ida_checks();
583 ida_thread_tests();
584 radix_tree_cpu_dead(1);
585 rcu_barrier();
586 if (nr_allocated)
587 printf("nr_allocated = %d\n", nr_allocated);
588 return 0;