GNU Linux-libre 4.14.303-gnu1
[releases.git] / tools / testing / radix-tree / idr-test.c
1 /*
2  * idr-test.c: Test the IDR API
3  * Copyright (c) 2016 Matthew Wilcox <willy@infradead.org>
4  *
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.
8  *
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.
13  */
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>
19
20 #include "test.h"
21
22 #define DUMMY_PTR       ((void *)0x12)
23
24 int item_idr_free(int id, void *p, void *data)
25 {
26         struct item *item = p;
27         assert(item->index == id);
28         free(p);
29
30         return 0;
31 }
32
33 void item_idr_remove(struct idr *idr, int id)
34 {
35         struct item *item = idr_find(idr, id);
36         assert(item->index == id);
37         idr_remove(idr, id);
38         free(item);
39 }
40
41 void idr_alloc_test(void)
42 {
43         unsigned long i;
44         DEFINE_IDR(idr);
45
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);
50
51         for (i = 0x3ffe; i < 0x4003; i++) {
52                 int id;
53                 struct item *item;
54
55                 if (i < 0x4000)
56                         item = item_create(i, 0);
57                 else
58                         item = item_create(i - 0x3fff, 0);
59
60                 id = idr_alloc_cyclic(&idr, item, 1, 0x4000, GFP_KERNEL);
61                 assert(id == item->index);
62         }
63
64         idr_for_each(&idr, item_idr_free, &idr);
65         idr_destroy(&idr);
66 }
67
68 void idr_replace_test(void)
69 {
70         DEFINE_IDR(idr);
71
72         idr_alloc(&idr, (void *)-1, 10, 11, GFP_KERNEL);
73         idr_replace(&idr, &idr, 10);
74
75         idr_destroy(&idr);
76 }
77
78 /*
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.
83  */
84 void idr_null_test(void)
85 {
86         int i;
87         DEFINE_IDR(idr);
88
89         assert(idr_is_empty(&idr));
90
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));
95
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));
100
101         for (i = 0; i < 10; i++) {
102                 assert(idr_alloc(&idr, NULL, 0, 0, GFP_KERNEL) == i);
103         }
104
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);
112
113         for (i = 0; i < 9; i++) {
114                 idr_remove(&idr, i);
115                 assert(!idr_is_empty(&idr));
116         }
117         idr_remove(&idr, 8);
118         assert(!idr_is_empty(&idr));
119         idr_remove(&idr, 9);
120         assert(idr_is_empty(&idr));
121
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);
126
127         idr_destroy(&idr);
128         assert(idr_is_empty(&idr));
129
130         for (i = 1; i < 10; i++) {
131                 assert(idr_alloc(&idr, NULL, 1, 0, GFP_KERNEL) == i);
132         }
133
134         idr_destroy(&idr);
135         assert(idr_is_empty(&idr));
136 }
137
138 void idr_nowait_test(void)
139 {
140         unsigned int i;
141         DEFINE_IDR(idr);
142
143         idr_preload(GFP_KERNEL);
144
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);
148         }
149
150         idr_preload_end();
151
152         idr_for_each(&idr, item_idr_free, &idr);
153         idr_destroy(&idr);
154 }
155
156 void idr_get_next_test(void)
157 {
158         unsigned long i;
159         int nextid;
160         DEFINE_IDR(idr);
161
162         int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0};
163
164         for(i = 0; indices[i]; i++) {
165                 struct item *item = item_create(indices[i], 0);
166                 assert(idr_alloc(&idr, item, indices[i], indices[i+1],
167                                  GFP_KERNEL) == indices[i]);
168         }
169
170         for(i = 0, nextid = 0; indices[i]; i++) {
171                 idr_get_next(&idr, &nextid);
172                 assert(nextid == indices[i]);
173                 nextid++;
174         }
175
176         idr_for_each(&idr, item_idr_free, &idr);
177         idr_destroy(&idr);
178 }
179
180 static inline void *idr_mk_value(unsigned long v)
181 {
182         BUG_ON((long)v < 0);
183         return (void *)((v & 1) | 2 | (v << 1));
184 }
185
186 DEFINE_IDR(find_idr);
187
188 static void *idr_throbber(void *arg)
189 {
190         time_t start = time(NULL);
191         int id = *(int *)arg;
192
193         rcu_register_thread();
194         do {
195                 idr_alloc(&find_idr, idr_mk_value(id), id, id + 1, GFP_KERNEL);
196                 idr_remove(&find_idr, id);
197         } while (time(NULL) < start + 10);
198         rcu_unregister_thread();
199
200         return NULL;
201 }
202
203 void idr_find_test_1(int anchor_id, int throbber_id)
204 {
205         pthread_t throbber;
206         time_t start = time(NULL);
207
208         pthread_create(&throbber, NULL, idr_throbber, &throbber_id);
209
210         BUG_ON(idr_alloc(&find_idr, idr_mk_value(anchor_id), anchor_id,
211                                 anchor_id + 1, GFP_KERNEL) != anchor_id);
212
213         do {
214                 int id = 0;
215                 void *entry = idr_get_next(&find_idr, &id);
216                 BUG_ON(entry != idr_mk_value(id));
217         } while (time(NULL) < start + 11);
218
219         pthread_join(throbber, NULL);
220
221         idr_remove(&find_idr, anchor_id);
222         BUG_ON(!idr_is_empty(&find_idr));
223 }
224
225 void idr_find_test(void)
226 {
227         idr_find_test_1(100000, 0);
228         idr_find_test_1(0, 100000);
229 }
230
231 void idr_checks(void)
232 {
233         unsigned long i;
234         DEFINE_IDR(idr);
235
236         for (i = 0; i < 10000; i++) {
237                 struct item *item = item_create(i, 0);
238                 assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
239         }
240
241         assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
242
243         for (i = 0; i < 5000; i++)
244                 item_idr_remove(&idr, i);
245
246         idr_remove(&idr, 3);
247
248         idr_for_each(&idr, item_idr_free, &idr);
249         idr_destroy(&idr);
250
251         assert(idr_is_empty(&idr));
252
253         idr_remove(&idr, 3);
254         idr_remove(&idr, 0);
255
256         assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == 0);
257         idr_remove(&idr, 1);
258         for (i = 1; i < RADIX_TREE_MAP_SIZE; i++)
259                 assert(idr_alloc(&idr, DUMMY_PTR, 0, 0, GFP_KERNEL) == i);
260         idr_remove(&idr, 1 << 30);
261         idr_destroy(&idr);
262
263         for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
264                 struct item *item = item_create(i, 0);
265                 assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
266         }
267         assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
268
269         idr_for_each(&idr, item_idr_free, &idr);
270         idr_destroy(&idr);
271         idr_destroy(&idr);
272
273         assert(idr_is_empty(&idr));
274
275         for (i = 1; i < 10000; i++) {
276                 struct item *item = item_create(i, 0);
277                 assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
278         }
279
280         idr_for_each(&idr, item_idr_free, &idr);
281         idr_destroy(&idr);
282
283         idr_replace_test();
284         idr_alloc_test();
285         idr_null_test();
286         idr_nowait_test();
287         idr_get_next_test();
288         idr_find_test();
289 }
290
291 /*
292  * Check that we get the correct error when we run out of memory doing
293  * allocations.  To ensure we run out of memory, just "forget" to preload.
294  * The first test is for not having a bitmap available, and the second test
295  * is for not being able to allocate a level of the radix tree.
296  */
297 void ida_check_nomem(void)
298 {
299         DEFINE_IDA(ida);
300         int id, err;
301
302         err = ida_get_new_above(&ida, 256, &id);
303         assert(err == -EAGAIN);
304         err = ida_get_new_above(&ida, 1UL << 30, &id);
305         assert(err == -EAGAIN);
306 }
307
308 /*
309  * Check what happens when we fill a leaf and then delete it.  This may
310  * discover mishandling of IDR_FREE.
311  */
312 void ida_check_leaf(void)
313 {
314         DEFINE_IDA(ida);
315         int id;
316         unsigned long i;
317
318         for (i = 0; i < IDA_BITMAP_BITS; i++) {
319                 assert(ida_pre_get(&ida, GFP_KERNEL));
320                 assert(!ida_get_new(&ida, &id));
321                 assert(id == i);
322         }
323
324         ida_destroy(&ida);
325         assert(ida_is_empty(&ida));
326
327         assert(ida_pre_get(&ida, GFP_KERNEL));
328         assert(!ida_get_new(&ida, &id));
329         assert(id == 0);
330         ida_destroy(&ida);
331         assert(ida_is_empty(&ida));
332 }
333
334 /*
335  * Check handling of conversions between exceptional entries and full bitmaps.
336  */
337 void ida_check_conv(void)
338 {
339         DEFINE_IDA(ida);
340         int id;
341         unsigned long i;
342
343         for (i = 0; i < IDA_BITMAP_BITS * 2; i += IDA_BITMAP_BITS) {
344                 assert(ida_pre_get(&ida, GFP_KERNEL));
345                 assert(!ida_get_new_above(&ida, i + 1, &id));
346                 assert(id == i + 1);
347                 assert(!ida_get_new_above(&ida, i + BITS_PER_LONG, &id));
348                 assert(id == i + BITS_PER_LONG);
349                 ida_remove(&ida, i + 1);
350                 ida_remove(&ida, i + BITS_PER_LONG);
351                 assert(ida_is_empty(&ida));
352         }
353
354         assert(ida_pre_get(&ida, GFP_KERNEL));
355
356         for (i = 0; i < IDA_BITMAP_BITS * 2; i++) {
357                 assert(ida_pre_get(&ida, GFP_KERNEL));
358                 assert(!ida_get_new(&ida, &id));
359                 assert(id == i);
360         }
361
362         for (i = IDA_BITMAP_BITS * 2; i > 0; i--) {
363                 ida_remove(&ida, i - 1);
364         }
365         assert(ida_is_empty(&ida));
366
367         for (i = 0; i < IDA_BITMAP_BITS + BITS_PER_LONG - 4; i++) {
368                 assert(ida_pre_get(&ida, GFP_KERNEL));
369                 assert(!ida_get_new(&ida, &id));
370                 assert(id == i);
371         }
372
373         for (i = IDA_BITMAP_BITS + BITS_PER_LONG - 4; i > 0; i--) {
374                 ida_remove(&ida, i - 1);
375         }
376         assert(ida_is_empty(&ida));
377
378         radix_tree_cpu_dead(1);
379         for (i = 0; i < 1000000; i++) {
380                 int err = ida_get_new(&ida, &id);
381                 if (err == -EAGAIN) {
382                         assert((i % IDA_BITMAP_BITS) == (BITS_PER_LONG - 2));
383                         assert(ida_pre_get(&ida, GFP_KERNEL));
384                         err = ida_get_new(&ida, &id);
385                 } else {
386                         assert((i % IDA_BITMAP_BITS) != (BITS_PER_LONG - 2));
387                 }
388                 assert(!err);
389                 assert(id == i);
390         }
391         ida_destroy(&ida);
392 }
393
394 /*
395  * Check allocations up to and slightly above the maximum allowed (2^31-1) ID.
396  * Allocating up to 2^31-1 should succeed, and then allocating the next one
397  * should fail.
398  */
399 void ida_check_max(void)
400 {
401         DEFINE_IDA(ida);
402         int id, err;
403         unsigned long i, j;
404
405         for (j = 1; j < 65537; j *= 2) {
406                 unsigned long base = (1UL << 31) - j;
407                 for (i = 0; i < j; i++) {
408                         assert(ida_pre_get(&ida, GFP_KERNEL));
409                         assert(!ida_get_new_above(&ida, base, &id));
410                         assert(id == base + i);
411                 }
412                 assert(ida_pre_get(&ida, GFP_KERNEL));
413                 err = ida_get_new_above(&ida, base, &id);
414                 assert(err == -ENOSPC);
415                 ida_destroy(&ida);
416                 assert(ida_is_empty(&ida));
417                 rcu_barrier();
418         }
419 }
420
421 void ida_check_random(void)
422 {
423         DEFINE_IDA(ida);
424         DECLARE_BITMAP(bitmap, 2048);
425         int id, err;
426         unsigned int i;
427         time_t s = time(NULL);
428
429  repeat:
430         memset(bitmap, 0, sizeof(bitmap));
431         for (i = 0; i < 100000; i++) {
432                 int i = rand();
433                 int bit = i & 2047;
434                 if (test_bit(bit, bitmap)) {
435                         __clear_bit(bit, bitmap);
436                         ida_remove(&ida, bit);
437                 } else {
438                         __set_bit(bit, bitmap);
439                         do {
440                                 ida_pre_get(&ida, GFP_KERNEL);
441                                 err = ida_get_new_above(&ida, bit, &id);
442                         } while (err == -ENOMEM);
443                         assert(!err);
444                         assert(id == bit);
445                 }
446         }
447         ida_destroy(&ida);
448         if (time(NULL) < s + 10)
449                 goto repeat;
450 }
451
452 void ida_simple_get_remove_test(void)
453 {
454         DEFINE_IDA(ida);
455         unsigned long i;
456
457         for (i = 0; i < 10000; i++) {
458                 assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
459         }
460         assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
461
462         for (i = 0; i < 10000; i++) {
463                 ida_simple_remove(&ida, i);
464         }
465         assert(ida_is_empty(&ida));
466
467         ida_destroy(&ida);
468 }
469
470 void ida_checks(void)
471 {
472         DEFINE_IDA(ida);
473         int id;
474         unsigned long i;
475
476         radix_tree_cpu_dead(1);
477         ida_check_nomem();
478
479         for (i = 0; i < 10000; i++) {
480                 assert(ida_pre_get(&ida, GFP_KERNEL));
481                 assert(!ida_get_new(&ida, &id));
482                 assert(id == i);
483         }
484
485         ida_remove(&ida, 20);
486         ida_remove(&ida, 21);
487         for (i = 0; i < 3; i++) {
488                 assert(ida_pre_get(&ida, GFP_KERNEL));
489                 assert(!ida_get_new(&ida, &id));
490                 if (i == 2)
491                         assert(id == 10000);
492         }
493
494         for (i = 0; i < 5000; i++)
495                 ida_remove(&ida, i);
496
497         assert(ida_pre_get(&ida, GFP_KERNEL));
498         assert(!ida_get_new_above(&ida, 5000, &id));
499         assert(id == 10001);
500
501         ida_destroy(&ida);
502
503         assert(ida_is_empty(&ida));
504
505         assert(ida_pre_get(&ida, GFP_KERNEL));
506         assert(!ida_get_new_above(&ida, 1, &id));
507         assert(id == 1);
508
509         ida_remove(&ida, id);
510         assert(ida_is_empty(&ida));
511         ida_destroy(&ida);
512         assert(ida_is_empty(&ida));
513
514         assert(ida_pre_get(&ida, GFP_KERNEL));
515         assert(!ida_get_new_above(&ida, 1, &id));
516         ida_destroy(&ida);
517         assert(ida_is_empty(&ida));
518
519         assert(ida_pre_get(&ida, GFP_KERNEL));
520         assert(!ida_get_new_above(&ida, 1, &id));
521         assert(id == 1);
522         assert(ida_pre_get(&ida, GFP_KERNEL));
523         assert(!ida_get_new_above(&ida, 1025, &id));
524         assert(id == 1025);
525         assert(ida_pre_get(&ida, GFP_KERNEL));
526         assert(!ida_get_new_above(&ida, 10000, &id));
527         assert(id == 10000);
528         ida_remove(&ida, 1025);
529         ida_destroy(&ida);
530         assert(ida_is_empty(&ida));
531
532         ida_check_leaf();
533         ida_check_max();
534         ida_check_conv();
535         ida_check_random();
536         ida_simple_get_remove_test();
537
538         radix_tree_cpu_dead(1);
539 }
540
541 static void *ida_random_fn(void *arg)
542 {
543         rcu_register_thread();
544         ida_check_random();
545         rcu_unregister_thread();
546         return NULL;
547 }
548
549 void ida_thread_tests(void)
550 {
551         pthread_t threads[10];
552         int i;
553
554         for (i = 0; i < ARRAY_SIZE(threads); i++)
555                 if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
556                         perror("creating ida thread");
557                         exit(1);
558                 }
559
560         while (i--)
561                 pthread_join(threads[i], NULL);
562 }
563
564 int __weak main(void)
565 {
566         radix_tree_init();
567         idr_checks();
568         ida_checks();
569         ida_thread_tests();
570         radix_tree_cpu_dead(1);
571         rcu_barrier();
572         if (nr_allocated)
573                 printf("nr_allocated = %d\n", nr_allocated);
574         return 0;
575 }