Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / radix-tree / idr-test.c
blob235eef71f3d13d3e36f3a4a8072a72e5bebf2384
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 static inline void *idr_mk_value(unsigned long v)
232 BUG_ON((long)v < 0);
233 return (void *)((v & 1) | 2 | (v << 1));
236 DEFINE_IDR(find_idr);
238 static void *idr_throbber(void *arg)
240 time_t start = time(NULL);
241 int id = *(int *)arg;
243 rcu_register_thread();
244 do {
245 idr_alloc(&find_idr, idr_mk_value(id), id, id + 1, GFP_KERNEL);
246 idr_remove(&find_idr, id);
247 } while (time(NULL) < start + 10);
248 rcu_unregister_thread();
250 return NULL;
253 void idr_find_test_1(int anchor_id, int throbber_id)
255 pthread_t throbber;
256 time_t start = time(NULL);
258 pthread_create(&throbber, NULL, idr_throbber, &throbber_id);
260 BUG_ON(idr_alloc(&find_idr, idr_mk_value(anchor_id), anchor_id,
261 anchor_id + 1, GFP_KERNEL) != anchor_id);
263 do {
264 int id = 0;
265 void *entry = idr_get_next(&find_idr, &id);
266 BUG_ON(entry != idr_mk_value(id));
267 } while (time(NULL) < start + 11);
269 pthread_join(throbber, NULL);
271 idr_remove(&find_idr, anchor_id);
272 BUG_ON(!idr_is_empty(&find_idr));
275 void idr_find_test(void)
277 idr_find_test_1(100000, 0);
278 idr_find_test_1(0, 100000);
281 void idr_checks(void)
283 unsigned long i;
284 DEFINE_IDR(idr);
286 for (i = 0; i < 10000; i++) {
287 struct item *item = item_create(i, 0);
288 assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
291 assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
293 for (i = 0; i < 5000; i++)
294 item_idr_remove(&idr, i);
296 idr_remove(&idr, 3);
298 idr_for_each(&idr, item_idr_free, &idr);
299 idr_destroy(&idr);
301 assert(idr_is_empty(&idr));
303 idr_remove(&idr, 3);
304 idr_remove(&idr, 0);
306 assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == 0);
307 idr_remove(&idr, 1);
308 for (i = 1; i < RADIX_TREE_MAP_SIZE; i++)
309 assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == i);
310 idr_remove(&idr, 1 << 30);
311 idr_destroy(&idr);
313 for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
314 struct item *item = item_create(i, 0);
315 assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
317 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
318 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC);
320 idr_for_each(&idr, item_idr_free, &idr);
321 idr_destroy(&idr);
322 idr_destroy(&idr);
324 assert(idr_is_empty(&idr));
326 idr_set_cursor(&idr, INT_MAX - 3UL);
327 for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) {
328 struct item *item;
329 unsigned int id;
330 if (i <= INT_MAX)
331 item = item_create(i, 0);
332 else
333 item = item_create(i - INT_MAX - 1, 0);
335 id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL);
336 assert(id == item->index);
339 idr_for_each(&idr, item_idr_free, &idr);
340 idr_destroy(&idr);
341 assert(idr_is_empty(&idr));
343 for (i = 1; i < 10000; i++) {
344 struct item *item = item_create(i, 0);
345 assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
348 idr_for_each(&idr, item_idr_free, &idr);
349 idr_destroy(&idr);
351 idr_replace_test();
352 idr_alloc_test();
353 idr_null_test();
354 idr_nowait_test();
355 idr_get_next_test(0);
356 idr_get_next_test(1);
357 idr_get_next_test(4);
358 idr_u32_test(4);
359 idr_u32_test(1);
360 idr_u32_test(0);
361 idr_find_test();
364 #define module_init(x)
365 #define module_exit(x)
366 #define MODULE_AUTHOR(x)
367 #define MODULE_LICENSE(x)
368 #define dump_stack() assert(0)
369 void ida_dump(struct ida *);
371 #include "../../../lib/test_ida.c"
374 * Check that we get the correct error when we run out of memory doing
375 * allocations. In userspace, GFP_NOWAIT will always fail an allocation.
376 * The first test is for not having a bitmap available, and the second test
377 * is for not being able to allocate a level of the radix tree.
379 void ida_check_nomem(void)
381 DEFINE_IDA(ida);
382 int id;
384 id = ida_alloc_min(&ida, 256, GFP_NOWAIT);
385 IDA_BUG_ON(&ida, id != -ENOMEM);
386 id = ida_alloc_min(&ida, 1UL << 30, GFP_NOWAIT);
387 IDA_BUG_ON(&ida, id != -ENOMEM);
388 IDA_BUG_ON(&ida, !ida_is_empty(&ida));
392 * Check handling of conversions between exceptional entries and full bitmaps.
394 void ida_check_conv_user(void)
396 DEFINE_IDA(ida);
397 unsigned long i;
399 radix_tree_cpu_dead(1);
400 for (i = 0; i < 1000000; i++) {
401 int id = ida_alloc(&ida, GFP_NOWAIT);
402 if (id == -ENOMEM) {
403 IDA_BUG_ON(&ida, (i % IDA_BITMAP_BITS) !=
404 BITS_PER_LONG - 2);
405 id = ida_alloc(&ida, GFP_KERNEL);
406 } else {
407 IDA_BUG_ON(&ida, (i % IDA_BITMAP_BITS) ==
408 BITS_PER_LONG - 2);
410 IDA_BUG_ON(&ida, id != i);
412 ida_destroy(&ida);
415 void ida_check_random(void)
417 DEFINE_IDA(ida);
418 DECLARE_BITMAP(bitmap, 2048);
419 unsigned int i;
420 time_t s = time(NULL);
422 repeat:
423 memset(bitmap, 0, sizeof(bitmap));
424 for (i = 0; i < 100000; i++) {
425 int i = rand();
426 int bit = i & 2047;
427 if (test_bit(bit, bitmap)) {
428 __clear_bit(bit, bitmap);
429 ida_free(&ida, bit);
430 } else {
431 __set_bit(bit, bitmap);
432 IDA_BUG_ON(&ida, ida_alloc_min(&ida, bit, GFP_KERNEL)
433 != bit);
436 ida_destroy(&ida);
437 if (time(NULL) < s + 10)
438 goto repeat;
441 void ida_simple_get_remove_test(void)
443 DEFINE_IDA(ida);
444 unsigned long i;
446 for (i = 0; i < 10000; i++) {
447 assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
449 assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
451 for (i = 0; i < 10000; i++) {
452 ida_simple_remove(&ida, i);
454 assert(ida_is_empty(&ida));
456 ida_destroy(&ida);
459 void user_ida_checks(void)
461 radix_tree_cpu_dead(1);
463 ida_check_nomem();
464 ida_check_conv_user();
465 ida_check_random();
466 ida_simple_get_remove_test();
468 radix_tree_cpu_dead(1);
471 static void *ida_random_fn(void *arg)
473 rcu_register_thread();
474 ida_check_random();
475 rcu_unregister_thread();
476 return NULL;
479 void ida_thread_tests(void)
481 pthread_t threads[20];
482 int i;
484 for (i = 0; i < ARRAY_SIZE(threads); i++)
485 if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
486 perror("creating ida thread");
487 exit(1);
490 while (i--)
491 pthread_join(threads[i], NULL);
494 void ida_tests(void)
496 user_ida_checks();
497 ida_checks();
498 ida_exit();
499 ida_thread_tests();
502 int __weak main(void)
504 radix_tree_init();
505 idr_checks();
506 ida_tests();
507 radix_tree_cpu_dead(1);
508 rcu_barrier();
509 if (nr_allocated)
510 printf("nr_allocated = %d\n", nr_allocated);
511 return 0;