blob: 892ef8855b0245bf6f353c0c35e223b6db347610 [file] [log] [blame]
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001/*
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 */
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -050014#include <linux/bitmap.h>
Matthew Wilcox0a835c42016-12-20 10:27:56 -050015#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
24int 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
33void 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
41void 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
68void 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 */
84void 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
138void 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
Rehas Sachdeva2eacc792017-02-18 07:31:00 -0500156void 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
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500180void idr_checks(void)
181{
182 unsigned long i;
183 DEFINE_IDR(idr);
184
185 for (i = 0; i < 10000; i++) {
186 struct item *item = item_create(i, 0);
187 assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i);
188 }
189
190 assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0);
191
192 for (i = 0; i < 5000; i++)
193 item_idr_remove(&idr, i);
194
195 idr_remove(&idr, 3);
196
197 idr_for_each(&idr, item_idr_free, &idr);
198 idr_destroy(&idr);
199
200 assert(idr_is_empty(&idr));
201
202 idr_remove(&idr, 3);
203 idr_remove(&idr, 0);
204
205 for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) {
206 struct item *item = item_create(i, 0);
207 assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i);
208 }
209 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC);
Matthew Wilcox6e6d3012017-11-28 14:27:14 -0500210 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500211
212 idr_for_each(&idr, item_idr_free, &idr);
213 idr_destroy(&idr);
214 idr_destroy(&idr);
215
216 assert(idr_is_empty(&idr));
217
218 for (i = 1; i < 10000; i++) {
219 struct item *item = item_create(i, 0);
220 assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i);
221 }
222
223 idr_for_each(&idr, item_idr_free, &idr);
224 idr_destroy(&idr);
225
226 idr_replace_test();
227 idr_alloc_test();
228 idr_null_test();
229 idr_nowait_test();
Rehas Sachdeva2eacc792017-02-18 07:31:00 -0500230 idr_get_next_test();
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500231}
232
233/*
234 * Check that we get the correct error when we run out of memory doing
235 * allocations. To ensure we run out of memory, just "forget" to preload.
236 * The first test is for not having a bitmap available, and the second test
237 * is for not being able to allocate a level of the radix tree.
238 */
239void ida_check_nomem(void)
240{
241 DEFINE_IDA(ida);
242 int id, err;
243
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500244 err = ida_get_new_above(&ida, 256, &id);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500245 assert(err == -EAGAIN);
246 err = ida_get_new_above(&ida, 1UL << 30, &id);
247 assert(err == -EAGAIN);
248}
249
250/*
251 * Check what happens when we fill a leaf and then delete it. This may
252 * discover mishandling of IDR_FREE.
253 */
254void ida_check_leaf(void)
255{
256 DEFINE_IDA(ida);
257 int id;
258 unsigned long i;
259
260 for (i = 0; i < IDA_BITMAP_BITS; i++) {
261 assert(ida_pre_get(&ida, GFP_KERNEL));
262 assert(!ida_get_new(&ida, &id));
263 assert(id == i);
264 }
265
266 ida_destroy(&ida);
267 assert(ida_is_empty(&ida));
268
269 assert(ida_pre_get(&ida, GFP_KERNEL));
270 assert(!ida_get_new(&ida, &id));
271 assert(id == 0);
272 ida_destroy(&ida);
273 assert(ida_is_empty(&ida));
274}
275
276/*
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500277 * Check handling of conversions between exceptional entries and full bitmaps.
278 */
279void ida_check_conv(void)
280{
281 DEFINE_IDA(ida);
282 int id;
283 unsigned long i;
284
285 for (i = 0; i < IDA_BITMAP_BITS * 2; i += IDA_BITMAP_BITS) {
286 assert(ida_pre_get(&ida, GFP_KERNEL));
287 assert(!ida_get_new_above(&ida, i + 1, &id));
288 assert(id == i + 1);
289 assert(!ida_get_new_above(&ida, i + BITS_PER_LONG, &id));
290 assert(id == i + BITS_PER_LONG);
291 ida_remove(&ida, i + 1);
292 ida_remove(&ida, i + BITS_PER_LONG);
293 assert(ida_is_empty(&ida));
294 }
295
296 assert(ida_pre_get(&ida, GFP_KERNEL));
297
298 for (i = 0; i < IDA_BITMAP_BITS * 2; i++) {
299 assert(ida_pre_get(&ida, GFP_KERNEL));
300 assert(!ida_get_new(&ida, &id));
301 assert(id == i);
302 }
303
304 for (i = IDA_BITMAP_BITS * 2; i > 0; i--) {
305 ida_remove(&ida, i - 1);
306 }
307 assert(ida_is_empty(&ida));
308
309 for (i = 0; i < IDA_BITMAP_BITS + BITS_PER_LONG - 4; i++) {
310 assert(ida_pre_get(&ida, GFP_KERNEL));
311 assert(!ida_get_new(&ida, &id));
312 assert(id == i);
313 }
314
315 for (i = IDA_BITMAP_BITS + BITS_PER_LONG - 4; i > 0; i--) {
316 ida_remove(&ida, i - 1);
317 }
318 assert(ida_is_empty(&ida));
319
320 radix_tree_cpu_dead(1);
321 for (i = 0; i < 1000000; i++) {
322 int err = ida_get_new(&ida, &id);
323 if (err == -EAGAIN) {
324 assert((i % IDA_BITMAP_BITS) == (BITS_PER_LONG - 2));
325 assert(ida_pre_get(&ida, GFP_KERNEL));
326 err = ida_get_new(&ida, &id);
327 } else {
328 assert((i % IDA_BITMAP_BITS) != (BITS_PER_LONG - 2));
329 }
330 assert(!err);
331 assert(id == i);
332 }
333 ida_destroy(&ida);
334}
335
336/*
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500337 * Check allocations up to and slightly above the maximum allowed (2^31-1) ID.
338 * Allocating up to 2^31-1 should succeed, and then allocating the next one
339 * should fail.
340 */
341void ida_check_max(void)
342{
343 DEFINE_IDA(ida);
344 int id, err;
345 unsigned long i, j;
346
347 for (j = 1; j < 65537; j *= 2) {
348 unsigned long base = (1UL << 31) - j;
349 for (i = 0; i < j; i++) {
350 assert(ida_pre_get(&ida, GFP_KERNEL));
351 assert(!ida_get_new_above(&ida, base, &id));
352 assert(id == base + i);
353 }
354 assert(ida_pre_get(&ida, GFP_KERNEL));
355 err = ida_get_new_above(&ida, base, &id);
356 assert(err == -ENOSPC);
357 ida_destroy(&ida);
358 assert(ida_is_empty(&ida));
359 rcu_barrier();
360 }
361}
362
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500363void ida_check_random(void)
364{
365 DEFINE_IDA(ida);
366 DECLARE_BITMAP(bitmap, 2048);
Matthew Wilcox4ecd9542017-03-03 12:16:10 -0500367 int id, err;
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500368 unsigned int i;
369 time_t s = time(NULL);
370
371 repeat:
372 memset(bitmap, 0, sizeof(bitmap));
373 for (i = 0; i < 100000; i++) {
374 int i = rand();
375 int bit = i & 2047;
376 if (test_bit(bit, bitmap)) {
377 __clear_bit(bit, bitmap);
378 ida_remove(&ida, bit);
379 } else {
380 __set_bit(bit, bitmap);
Matthew Wilcox4ecd9542017-03-03 12:16:10 -0500381 do {
382 ida_pre_get(&ida, GFP_KERNEL);
383 err = ida_get_new_above(&ida, bit, &id);
Matthew Wilcox490645d2017-11-09 20:15:14 -0500384 } while (err == -EAGAIN);
Matthew Wilcox4ecd9542017-03-03 12:16:10 -0500385 assert(!err);
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500386 assert(id == bit);
387 }
388 }
389 ida_destroy(&ida);
390 if (time(NULL) < s + 10)
391 goto repeat;
392}
393
Rehas Sachdeva166bb1f2017-02-20 06:40:00 -0500394void ida_simple_get_remove_test(void)
395{
396 DEFINE_IDA(ida);
397 unsigned long i;
398
399 for (i = 0; i < 10000; i++) {
400 assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i);
401 }
402 assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0);
403
404 for (i = 0; i < 10000; i++) {
405 ida_simple_remove(&ida, i);
406 }
407 assert(ida_is_empty(&ida));
408
409 ida_destroy(&ida);
410}
411
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500412void ida_checks(void)
413{
414 DEFINE_IDA(ida);
415 int id;
416 unsigned long i;
417
418 radix_tree_cpu_dead(1);
419 ida_check_nomem();
420
421 for (i = 0; i < 10000; i++) {
422 assert(ida_pre_get(&ida, GFP_KERNEL));
423 assert(!ida_get_new(&ida, &id));
424 assert(id == i);
425 }
426
427 ida_remove(&ida, 20);
428 ida_remove(&ida, 21);
429 for (i = 0; i < 3; i++) {
430 assert(ida_pre_get(&ida, GFP_KERNEL));
431 assert(!ida_get_new(&ida, &id));
432 if (i == 2)
433 assert(id == 10000);
434 }
435
436 for (i = 0; i < 5000; i++)
437 ida_remove(&ida, i);
438
439 assert(ida_pre_get(&ida, GFP_KERNEL));
440 assert(!ida_get_new_above(&ida, 5000, &id));
441 assert(id == 10001);
442
443 ida_destroy(&ida);
444
445 assert(ida_is_empty(&ida));
446
447 assert(ida_pre_get(&ida, GFP_KERNEL));
448 assert(!ida_get_new_above(&ida, 1, &id));
449 assert(id == 1);
450
451 ida_remove(&ida, id);
452 assert(ida_is_empty(&ida));
453 ida_destroy(&ida);
454 assert(ida_is_empty(&ida));
455
456 assert(ida_pre_get(&ida, GFP_KERNEL));
457 assert(!ida_get_new_above(&ida, 1, &id));
458 ida_destroy(&ida);
459 assert(ida_is_empty(&ida));
460
461 assert(ida_pre_get(&ida, GFP_KERNEL));
462 assert(!ida_get_new_above(&ida, 1, &id));
463 assert(id == 1);
464 assert(ida_pre_get(&ida, GFP_KERNEL));
465 assert(!ida_get_new_above(&ida, 1025, &id));
466 assert(id == 1025);
467 assert(ida_pre_get(&ida, GFP_KERNEL));
468 assert(!ida_get_new_above(&ida, 10000, &id));
469 assert(id == 10000);
470 ida_remove(&ida, 1025);
471 ida_destroy(&ida);
472 assert(ida_is_empty(&ida));
473
474 ida_check_leaf();
475 ida_check_max();
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500476 ida_check_conv();
477 ida_check_random();
Rehas Sachdeva166bb1f2017-02-20 06:40:00 -0500478 ida_simple_get_remove_test();
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500479
480 radix_tree_cpu_dead(1);
481}
Matthew Wilcox8ac04862016-12-18 22:56:05 -0500482
Matthew Wilcox4ecd9542017-03-03 12:16:10 -0500483static void *ida_random_fn(void *arg)
484{
485 rcu_register_thread();
486 ida_check_random();
487 rcu_unregister_thread();
488 return NULL;
489}
490
491void ida_thread_tests(void)
492{
Matthew Wilcox490645d2017-11-09 20:15:14 -0500493 pthread_t threads[20];
Matthew Wilcox4ecd9542017-03-03 12:16:10 -0500494 int i;
495
496 for (i = 0; i < ARRAY_SIZE(threads); i++)
497 if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) {
498 perror("creating ida thread");
499 exit(1);
500 }
501
502 while (i--)
503 pthread_join(threads[i], NULL);
504}
505
Matthew Wilcox8ac04862016-12-18 22:56:05 -0500506int __weak main(void)
507{
508 radix_tree_init();
509 idr_checks();
510 ida_checks();
Matthew Wilcox4ecd9542017-03-03 12:16:10 -0500511 ida_thread_tests();
512 radix_tree_cpu_dead(1);
Matthew Wilcox8ac04862016-12-18 22:56:05 -0500513 rcu_barrier();
514 if (nr_allocated)
515 printf("nr_allocated = %d\n", nr_allocated);
516 return 0;
517}