1 // SPDX-License-Identifier: GPL-2.0-only
3 * idr-test.c: Test the IDR API
4 * Copyright (c) 2016 Matthew Wilcox <willy@infradead.org>
6 #include <linux/bitmap.h>
8 #include <linux/slab.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
14 #define DUMMY_PTR ((void *)0x10)
16 int item_idr_free(int id, void *p, void *data)
18 struct item *item = p;
19 assert(item->index == id);
25 void item_idr_remove(struct idr *idr, int id)
27 struct item *item = idr_find(idr, id);
28 assert(item->index == id);
33 void idr_alloc_test(void)
38 assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0, 0x4000, GFP_KERNEL) == 0);
39 assert(idr_alloc_cyclic(&idr, DUMMY_PTR, 0x3ffd, 0x4000, GFP_KERNEL) == 0x3ffd);
40 idr_remove(&idr, 0x3ffd);
43 for (i = 0x3ffe; i < 0x4003; i++) {
48 item = item_create(i, 0);
50 item = item_create(i - 0x3fff, 0);
52 id = idr_alloc_cyclic(&idr, item, 1, 0x4000, GFP_KERNEL);
53 assert(id == item->index);
56 idr_for_each(&idr, item_idr_free, &idr);
60 void idr_replace_test(void)
64 idr_alloc(&idr, (void *)-1, 10, 11, GFP_KERNEL);
65 idr_replace(&idr, &idr, 10);
71 * Unlike the radix tree, you can put a NULL pointer -- with care -- into
72 * the IDR. Some interfaces, like idr_find() do not distinguish between
73 * "present, value is NULL" and "not present", but that's exactly what some
76 void idr_null_test(void)
81 assert(idr_is_empty(&idr));
83 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
84 assert(!idr_is_empty(&idr));
86 assert(idr_is_empty(&idr));
88 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
89 assert(!idr_is_empty(&idr));
91 assert(idr_is_empty(&idr));
93 for (i = 0; i < 10; i++) {
94 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == i);
97 assert(idr_replace(&idr, DUMMY_PTR, 3) == NULL);
98 assert(idr_replace(&idr, DUMMY_PTR, 4) == NULL);
99 assert(idr_replace(&idr, NULL, 4) == DUMMY_PTR);
100 assert(idr_replace(&idr, DUMMY_PTR, 11) == ERR_PTR(-ENOENT));
102 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 5);
105 for (i = 0; i < 9; i++) {
107 assert(!idr_is_empty(&idr));
110 assert(!idr_is_empty(&idr));
112 assert(idr_is_empty(&idr));
114 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == 0);
115 assert(idr_replace(&idr, DUMMY_PTR, 3) == ERR_PTR(-ENOENT));
116 assert(idr_replace(&idr, DUMMY_PTR, 0) == NULL);
117 assert(idr_replace(&idr, NULL, 0) == DUMMY_PTR);
120 assert(idr_is_empty(&idr));
122 for (i = 1; i < 10; i++) {
123 assert(idr_alloc(&idr, NULL, 1, 0, GFP_KERNEL) == i);
127 assert(idr_is_empty(&idr));
130 void idr_nowait_test(void)
135 idr_preload(GFP_KERNEL);
137 for (i = 0; i < 3; i++) {
138 struct item *item = item_create(i, 0);
139 assert(idr_alloc(&idr, item, i, i + 1, GFP_NOWAIT) == i);
144 idr_for_each(&idr, item_idr_free, &idr);
148 void idr_get_next_test(int base)
153 idr_init_base(&idr, base);
155 int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0};
157 for(i = 0; indices[i]; i++) {
158 struct item *item = item_create(indices[i], 0);
159 assert(idr_alloc(&idr, item, indices[i], indices[i+1],
160 GFP_KERNEL) == indices[i]);
163 for(i = 0, nextid = 0; indices[i]; i++) {
164 idr_get_next(&idr, &nextid);
165 assert(nextid == indices[i]);
169 idr_for_each(&idr, item_idr_free, &idr);
173 int idr_u32_cb(int id, void *ptr, void *data)
176 BUG_ON(ptr != DUMMY_PTR);
180 void idr_u32_test1(struct idr *idr, u32 handle)
182 static bool warned = false;
187 BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL));
188 BUG_ON(id != handle);
189 BUG_ON(idr_alloc_u32(idr, DUMMY_PTR, &id, id, GFP_KERNEL) != -ENOSPC);
190 BUG_ON(id != handle);
191 if (!warned && id > INT_MAX)
192 printk("vvv Ignore these warnings\n");
193 ptr = idr_get_next(idr, &sid);
198 BUG_ON(ptr != DUMMY_PTR);
201 idr_for_each(idr, idr_u32_cb, NULL);
202 if (!warned && id > INT_MAX) {
203 printk("^^^ Warnings over\n");
206 BUG_ON(idr_remove(idr, id) != DUMMY_PTR);
207 BUG_ON(!idr_is_empty(idr));
210 void idr_u32_test(int base)
213 idr_init_base(&idr, base);
214 idr_u32_test1(&idr, 10);
215 idr_u32_test1(&idr, 0x7fffffff);
216 idr_u32_test1(&idr, 0x80000000);
217 idr_u32_test1(&idr, 0x80000001);
218 idr_u32_test1(&idr, 0xffe00000);
219 idr_u32_test1(&idr, 0xffffffff);
222 static void idr_align_test(struct idr *idr)
224 char name[] = "Motorola 68000";
228 for (i = 0; i < 9; i++) {
229 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i);
230 idr_for_each_entry(idr, entry, id);
234 for (i = 1; i < 10; i++) {
235 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 1);
236 idr_for_each_entry(idr, entry, id);
240 for (i = 2; i < 11; i++) {
241 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 2);
242 idr_for_each_entry(idr, entry, id);
246 for (i = 3; i < 12; i++) {
247 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != i - 3);
248 idr_for_each_entry(idr, entry, id);
252 for (i = 0; i < 8; i++) {
253 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != 0);
254 BUG_ON(idr_alloc(idr, &name[i + 1], 0, 0, GFP_KERNEL) != 1);
255 idr_for_each_entry(idr, entry, id);
257 idr_for_each_entry(idr, entry, id);
259 BUG_ON(!idr_is_empty(idr));
262 for (i = 0; i < 8; i++) {
263 BUG_ON(idr_alloc(idr, NULL, 0, 0, GFP_KERNEL) != 0);
264 idr_for_each_entry(idr, entry, id);
265 idr_replace(idr, &name[i], 0);
266 idr_for_each_entry(idr, entry, id);
267 BUG_ON(idr_find(idr, 0) != &name[i]);
271 for (i = 0; i < 8; i++) {
272 BUG_ON(idr_alloc(idr, &name[i], 0, 0, GFP_KERNEL) != 0);
273 BUG_ON(idr_alloc(idr, NULL, 0, 0, GFP_KERNEL) != 1);
275 idr_for_each_entry(idr, entry, id);
276 idr_replace(idr, &name[i + 1], 0);
277 idr_for_each_entry(idr, entry, id);
282 void idr_checks(void)
287 for (i = 0; i < 10000; i++) {
288 struct item *item = item_create(i, 0);
289 assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
292 assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
294 for (i = 0; i < 5000; i++)
295 item_idr_remove(&idr, i);
299 idr_for_each(&idr, item_idr_free, &idr);
302 assert(idr_is_empty(&idr));
307 assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == 0);
309 for (i = 1; i < RADIX_TREE_MAP_SIZE; i++)
310 assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == i);
311 idr_remove(&idr, 1 << 30);
314 for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
315 struct item *item = item_create(i, 0);
316 assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
318 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
319 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC);
321 idr_for_each(&idr, item_idr_free, &idr);
325 assert(idr_is_empty(&idr));
327 idr_set_cursor(&idr, INT_MAX - 3UL);
328 for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) {
332 item = item_create(i, 0);
334 item = item_create(i - INT_MAX - 1, 0);
336 id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL);
337 assert(id == item->index);
340 idr_for_each(&idr, item_idr_free, &idr);
342 assert(idr_is_empty(&idr));
344 for (i = 1; i < 10000; i++) {
345 struct item *item = item_create(i, 0);
346 assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
349 idr_for_each(&idr, item_idr_free, &idr);
356 idr_get_next_test(0);
357 idr_get_next_test(1);
358 idr_get_next_test(4);
362 idr_align_test(&idr);
365 #define module_init(x)
366 #define module_exit(x)
367 #define MODULE_AUTHOR(x)
368 #define MODULE_LICENSE(x)
369 #define dump_stack() assert(0)
370 void ida_dump(struct ida *);
372 #include "../../../lib/test_ida.c"
375 * Check that we get the correct error when we run out of memory doing
376 * allocations. In userspace, GFP_NOWAIT will always fail an allocation.
377 * The first test is for not having a bitmap available, and the second test
378 * is for not being able to allocate a level of the radix tree.
380 void ida_check_nomem(void)
385 id = ida_alloc_min(&ida, 256, GFP_NOWAIT);
386 IDA_BUG_ON(&ida, id != -ENOMEM);
387 id = ida_alloc_min(&ida, 1UL << 30, GFP_NOWAIT);
388 IDA_BUG_ON(&ida, id != -ENOMEM);
389 IDA_BUG_ON(&ida, !ida_is_empty(&ida));
393 * Check handling of conversions between exceptional entries and full bitmaps.
395 void ida_check_conv_user(void)
400 for (i = 0; i < 1000000; i++) {
401 int id = ida_alloc(&ida, GFP_NOWAIT);
403 IDA_BUG_ON(&ida, ((i % IDA_BITMAP_BITS) !=
404 BITS_PER_XA_VALUE) &&
405 ((i % IDA_BITMAP_BITS) != 0));
406 id = ida_alloc(&ida, GFP_KERNEL);
408 IDA_BUG_ON(&ida, (i % IDA_BITMAP_BITS) ==
411 IDA_BUG_ON(&ida, id != i);
416 void ida_check_random(void)
419 DECLARE_BITMAP(bitmap, 2048);
421 time_t s = time(NULL);
424 memset(bitmap, 0, sizeof(bitmap));
425 for (i = 0; i < 100000; i++) {
428 if (test_bit(bit, bitmap)) {
429 __clear_bit(bit, bitmap);
432 __set_bit(bit, bitmap);
433 IDA_BUG_ON(&ida, ida_alloc_min(&ida, bit, GFP_KERNEL)
438 if (time(NULL) < s + 10)
442 void ida_simple_get_remove_test(void)
447 for (i = 0; i < 10000; i++) {
448 assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
450 assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
452 for (i = 0; i < 10000; i++) {
453 ida_simple_remove(&ida, i);
455 assert(ida_is_empty(&ida));
460 void user_ida_checks(void)
462 radix_tree_cpu_dead(1);
465 ida_check_conv_user();
467 ida_simple_get_remove_test();
469 radix_tree_cpu_dead(1);
472 static void *ida_random_fn(void *arg)
474 rcu_register_thread();
476 rcu_unregister_thread();
480 void ida_thread_tests(void)
482 pthread_t threads[20];
485 for (i = 0; i < ARRAY_SIZE(threads); i++)
486 if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
487 perror("creating ida thread");
492 pthread_join(threads[i], NULL);
503 int __weak main(void)
508 radix_tree_cpu_dead(1);
511 printf("nr_allocated = %d\n", nr_allocated);