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(int base) 157 { 158 unsigned long i; 159 int nextid; 160 DEFINE_IDR(idr); 161 idr_init_base(&idr, base); 162 163 int indices[] = {4, 7, 9, 15, 65, 128, 1000, 99999, 0}; 164 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]); 169 } 170 171 for(i = 0, nextid = 0; indices[i]; i++) { 172 idr_get_next(&idr, &nextid); 173 assert(nextid == indices[i]); 174 nextid++; 175 } 176 177 idr_for_each(&idr, item_idr_free, &idr); 178 idr_destroy(&idr); 179 } 180 181 void idr_checks(void) 182 { 183 unsigned long i; 184 DEFINE_IDR(idr); 185 186 for (i = 0; i < 10000; i++) { 187 struct item *item = item_create(i, 0); 188 assert(idr_alloc(&idr, item, 0, 20000, GFP_KERNEL) == i); 189 } 190 191 assert(idr_alloc(&idr, DUMMY_PTR, 5, 30, GFP_KERNEL) < 0); 192 193 for (i = 0; i < 5000; i++) 194 item_idr_remove(&idr, i); 195 196 idr_remove(&idr, 3); 197 198 idr_for_each(&idr, item_idr_free, &idr); 199 idr_destroy(&idr); 200 201 assert(idr_is_empty(&idr)); 202 203 idr_remove(&idr, 3); 204 idr_remove(&idr, 0); 205 206 for (i = INT_MAX - 3UL; i < INT_MAX + 1UL; i++) { 207 struct item *item = item_create(i, 0); 208 assert(idr_alloc(&idr, item, i, i + 10, GFP_KERNEL) == i); 209 } 210 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i, GFP_KERNEL) == -ENOSPC); 211 assert(idr_alloc(&idr, DUMMY_PTR, i - 2, i + 10, GFP_KERNEL) == -ENOSPC); 212 213 idr_for_each(&idr, item_idr_free, &idr); 214 idr_destroy(&idr); 215 idr_destroy(&idr); 216 217 assert(idr_is_empty(&idr)); 218 219 idr_set_cursor(&idr, INT_MAX - 3UL); 220 for (i = INT_MAX - 3UL; i < INT_MAX + 3UL; i++) { 221 struct item *item; 222 unsigned int id; 223 if (i <= INT_MAX) 224 item = item_create(i, 0); 225 else 226 item = item_create(i - INT_MAX - 1, 0); 227 228 id = idr_alloc_cyclic(&idr, item, 0, 0, GFP_KERNEL); 229 assert(id == item->index); 230 } 231 232 idr_for_each(&idr, item_idr_free, &idr); 233 idr_destroy(&idr); 234 assert(idr_is_empty(&idr)); 235 236 for (i = 1; i < 10000; i++) { 237 struct item *item = item_create(i, 0); 238 assert(idr_alloc(&idr, item, 1, 20000, GFP_KERNEL) == i); 239 } 240 241 idr_for_each(&idr, item_idr_free, &idr); 242 idr_destroy(&idr); 243 244 idr_replace_test(); 245 idr_alloc_test(); 246 idr_null_test(); 247 idr_nowait_test(); 248 idr_get_next_test(0); 249 idr_get_next_test(1); 250 idr_get_next_test(4); 251 } 252 253 /* 254 * Check that we get the correct error when we run out of memory doing 255 * allocations. To ensure we run out of memory, just "forget" to preload. 256 * The first test is for not having a bitmap available, and the second test 257 * is for not being able to allocate a level of the radix tree. 258 */ 259 void ida_check_nomem(void) 260 { 261 DEFINE_IDA(ida); 262 int id, err; 263 264 err = ida_get_new_above(&ida, 256, &id); 265 assert(err == -EAGAIN); 266 err = ida_get_new_above(&ida, 1UL << 30, &id); 267 assert(err == -EAGAIN); 268 } 269 270 /* 271 * Check what happens when we fill a leaf and then delete it. This may 272 * discover mishandling of IDR_FREE. 273 */ 274 void ida_check_leaf(void) 275 { 276 DEFINE_IDA(ida); 277 int id; 278 unsigned long i; 279 280 for (i = 0; i < IDA_BITMAP_BITS; i++) { 281 assert(ida_pre_get(&ida, GFP_KERNEL)); 282 assert(!ida_get_new(&ida, &id)); 283 assert(id == i); 284 } 285 286 ida_destroy(&ida); 287 assert(ida_is_empty(&ida)); 288 289 assert(ida_pre_get(&ida, GFP_KERNEL)); 290 assert(!ida_get_new(&ida, &id)); 291 assert(id == 0); 292 ida_destroy(&ida); 293 assert(ida_is_empty(&ida)); 294 } 295 296 /* 297 * Check handling of conversions between exceptional entries and full bitmaps. 298 */ 299 void ida_check_conv(void) 300 { 301 DEFINE_IDA(ida); 302 int id; 303 unsigned long i; 304 305 for (i = 0; i < IDA_BITMAP_BITS * 2; i += IDA_BITMAP_BITS) { 306 assert(ida_pre_get(&ida, GFP_KERNEL)); 307 assert(!ida_get_new_above(&ida, i + 1, &id)); 308 assert(id == i + 1); 309 assert(!ida_get_new_above(&ida, i + BITS_PER_LONG, &id)); 310 assert(id == i + BITS_PER_LONG); 311 ida_remove(&ida, i + 1); 312 ida_remove(&ida, i + BITS_PER_LONG); 313 assert(ida_is_empty(&ida)); 314 } 315 316 assert(ida_pre_get(&ida, GFP_KERNEL)); 317 318 for (i = 0; i < IDA_BITMAP_BITS * 2; i++) { 319 assert(ida_pre_get(&ida, GFP_KERNEL)); 320 assert(!ida_get_new(&ida, &id)); 321 assert(id == i); 322 } 323 324 for (i = IDA_BITMAP_BITS * 2; i > 0; i--) { 325 ida_remove(&ida, i - 1); 326 } 327 assert(ida_is_empty(&ida)); 328 329 for (i = 0; i < IDA_BITMAP_BITS + BITS_PER_LONG - 4; i++) { 330 assert(ida_pre_get(&ida, GFP_KERNEL)); 331 assert(!ida_get_new(&ida, &id)); 332 assert(id == i); 333 } 334 335 for (i = IDA_BITMAP_BITS + BITS_PER_LONG - 4; i > 0; i--) { 336 ida_remove(&ida, i - 1); 337 } 338 assert(ida_is_empty(&ida)); 339 340 radix_tree_cpu_dead(1); 341 for (i = 0; i < 1000000; i++) { 342 int err = ida_get_new(&ida, &id); 343 if (err == -EAGAIN) { 344 assert((i % IDA_BITMAP_BITS) == (BITS_PER_LONG - 2)); 345 assert(ida_pre_get(&ida, GFP_KERNEL)); 346 err = ida_get_new(&ida, &id); 347 } else { 348 assert((i % IDA_BITMAP_BITS) != (BITS_PER_LONG - 2)); 349 } 350 assert(!err); 351 assert(id == i); 352 } 353 ida_destroy(&ida); 354 } 355 356 /* 357 * Check allocations up to and slightly above the maximum allowed (2^31-1) ID. 358 * Allocating up to 2^31-1 should succeed, and then allocating the next one 359 * should fail. 360 */ 361 void ida_check_max(void) 362 { 363 DEFINE_IDA(ida); 364 int id, err; 365 unsigned long i, j; 366 367 for (j = 1; j < 65537; j *= 2) { 368 unsigned long base = (1UL << 31) - j; 369 for (i = 0; i < j; i++) { 370 assert(ida_pre_get(&ida, GFP_KERNEL)); 371 assert(!ida_get_new_above(&ida, base, &id)); 372 assert(id == base + i); 373 } 374 assert(ida_pre_get(&ida, GFP_KERNEL)); 375 err = ida_get_new_above(&ida, base, &id); 376 assert(err == -ENOSPC); 377 ida_destroy(&ida); 378 assert(ida_is_empty(&ida)); 379 rcu_barrier(); 380 } 381 } 382 383 void ida_check_random(void) 384 { 385 DEFINE_IDA(ida); 386 DECLARE_BITMAP(bitmap, 2048); 387 int id, err; 388 unsigned int i; 389 time_t s = time(NULL); 390 391 repeat: 392 memset(bitmap, 0, sizeof(bitmap)); 393 for (i = 0; i < 100000; i++) { 394 int i = rand(); 395 int bit = i & 2047; 396 if (test_bit(bit, bitmap)) { 397 __clear_bit(bit, bitmap); 398 ida_remove(&ida, bit); 399 } else { 400 __set_bit(bit, bitmap); 401 do { 402 ida_pre_get(&ida, GFP_KERNEL); 403 err = ida_get_new_above(&ida, bit, &id); 404 } while (err == -EAGAIN); 405 assert(!err); 406 assert(id == bit); 407 } 408 } 409 ida_destroy(&ida); 410 if (time(NULL) < s + 10) 411 goto repeat; 412 } 413 414 void ida_simple_get_remove_test(void) 415 { 416 DEFINE_IDA(ida); 417 unsigned long i; 418 419 for (i = 0; i < 10000; i++) { 420 assert(ida_simple_get(&ida, 0, 20000, GFP_KERNEL) == i); 421 } 422 assert(ida_simple_get(&ida, 5, 30, GFP_KERNEL) < 0); 423 424 for (i = 0; i < 10000; i++) { 425 ida_simple_remove(&ida, i); 426 } 427 assert(ida_is_empty(&ida)); 428 429 ida_destroy(&ida); 430 } 431 432 void ida_checks(void) 433 { 434 DEFINE_IDA(ida); 435 int id; 436 unsigned long i; 437 438 radix_tree_cpu_dead(1); 439 ida_check_nomem(); 440 441 for (i = 0; i < 10000; i++) { 442 assert(ida_pre_get(&ida, GFP_KERNEL)); 443 assert(!ida_get_new(&ida, &id)); 444 assert(id == i); 445 } 446 447 ida_remove(&ida, 20); 448 ida_remove(&ida, 21); 449 for (i = 0; i < 3; i++) { 450 assert(ida_pre_get(&ida, GFP_KERNEL)); 451 assert(!ida_get_new(&ida, &id)); 452 if (i == 2) 453 assert(id == 10000); 454 } 455 456 for (i = 0; i < 5000; i++) 457 ida_remove(&ida, i); 458 459 assert(ida_pre_get(&ida, GFP_KERNEL)); 460 assert(!ida_get_new_above(&ida, 5000, &id)); 461 assert(id == 10001); 462 463 ida_destroy(&ida); 464 465 assert(ida_is_empty(&ida)); 466 467 assert(ida_pre_get(&ida, GFP_KERNEL)); 468 assert(!ida_get_new_above(&ida, 1, &id)); 469 assert(id == 1); 470 471 ida_remove(&ida, id); 472 assert(ida_is_empty(&ida)); 473 ida_destroy(&ida); 474 assert(ida_is_empty(&ida)); 475 476 assert(ida_pre_get(&ida, GFP_KERNEL)); 477 assert(!ida_get_new_above(&ida, 1, &id)); 478 ida_destroy(&ida); 479 assert(ida_is_empty(&ida)); 480 481 assert(ida_pre_get(&ida, GFP_KERNEL)); 482 assert(!ida_get_new_above(&ida, 1, &id)); 483 assert(id == 1); 484 assert(ida_pre_get(&ida, GFP_KERNEL)); 485 assert(!ida_get_new_above(&ida, 1025, &id)); 486 assert(id == 1025); 487 assert(ida_pre_get(&ida, GFP_KERNEL)); 488 assert(!ida_get_new_above(&ida, 10000, &id)); 489 assert(id == 10000); 490 ida_remove(&ida, 1025); 491 ida_destroy(&ida); 492 assert(ida_is_empty(&ida)); 493 494 ida_check_leaf(); 495 ida_check_max(); 496 ida_check_conv(); 497 ida_check_random(); 498 ida_simple_get_remove_test(); 499 500 radix_tree_cpu_dead(1); 501 } 502 503 static void *ida_random_fn(void *arg) 504 { 505 rcu_register_thread(); 506 ida_check_random(); 507 rcu_unregister_thread(); 508 return NULL; 509 } 510 511 void ida_thread_tests(void) 512 { 513 pthread_t threads[20]; 514 int i; 515 516 for (i = 0; i < ARRAY_SIZE(threads); i++) 517 if (pthread_create(&threads[i], NULL, ida_random_fn, NULL)) { 518 perror("creating ida thread"); 519 exit(1); 520 } 521 522 while (i--) 523 pthread_join(threads[i], NULL); 524 } 525 526 int __weak main(void) 527 { 528 radix_tree_init(); 529 idr_checks(); 530 ida_checks(); 531 ida_thread_tests(); 532 radix_tree_cpu_dead(1); 533 rcu_barrier(); 534 if (nr_allocated) 535 printf("nr_allocated = %d\n", nr_allocated); 536 return 0; 537 } 538