1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Base unit test (KUnit) API. 4 * 5 * Copyright (C) 2019, Google LLC. 6 * Author: Brendan Higgins <brendanhiggins@google.com> 7 */ 8 9 #include <kunit/resource.h> 10 #include <kunit/test.h> 11 #include <kunit/test-bug.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/moduleparam.h> 15 #include <linux/panic.h> 16 #include <linux/sched/debug.h> 17 #include <linux/sched.h> 18 19 #include "debugfs.h" 20 #include "hooks-impl.h" 21 #include "string-stream.h" 22 #include "try-catch-impl.h" 23 24 /* 25 * Hook to fail the current test and print an error message to the log. 26 */ 27 void __printf(3, 4) __kunit_fail_current_test_impl(const char *file, int line, const char *fmt, ...) 28 { 29 va_list args; 30 int len; 31 char *buffer; 32 33 if (!current->kunit_test) 34 return; 35 36 kunit_set_failure(current->kunit_test); 37 38 /* kunit_err() only accepts literals, so evaluate the args first. */ 39 va_start(args, fmt); 40 len = vsnprintf(NULL, 0, fmt, args) + 1; 41 va_end(args); 42 43 buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL); 44 if (!buffer) 45 return; 46 47 va_start(args, fmt); 48 vsnprintf(buffer, len, fmt, args); 49 va_end(args); 50 51 kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer); 52 kunit_kfree(current->kunit_test, buffer); 53 } 54 55 /* 56 * Enable KUnit tests to run. 57 */ 58 #ifdef CONFIG_KUNIT_DEFAULT_ENABLED 59 static bool enable_param = true; 60 #else 61 static bool enable_param; 62 #endif 63 module_param_named(enable, enable_param, bool, 0); 64 MODULE_PARM_DESC(enable, "Enable KUnit tests"); 65 66 /* 67 * KUnit statistic mode: 68 * 0 - disabled 69 * 1 - only when there is more than one subtest 70 * 2 - enabled 71 */ 72 static int kunit_stats_enabled = 1; 73 module_param_named(stats_enabled, kunit_stats_enabled, int, 0644); 74 MODULE_PARM_DESC(stats_enabled, 75 "Print test stats: never (0), only for multiple subtests (1), or always (2)"); 76 77 struct kunit_result_stats { 78 unsigned long passed; 79 unsigned long skipped; 80 unsigned long failed; 81 unsigned long total; 82 }; 83 84 static bool kunit_should_print_stats(struct kunit_result_stats stats) 85 { 86 if (kunit_stats_enabled == 0) 87 return false; 88 89 if (kunit_stats_enabled == 2) 90 return true; 91 92 return (stats.total > 1); 93 } 94 95 static void kunit_print_test_stats(struct kunit *test, 96 struct kunit_result_stats stats) 97 { 98 if (!kunit_should_print_stats(stats)) 99 return; 100 101 kunit_log(KERN_INFO, test, 102 KUNIT_SUBTEST_INDENT 103 "# %s: pass:%lu fail:%lu skip:%lu total:%lu", 104 test->name, 105 stats.passed, 106 stats.failed, 107 stats.skipped, 108 stats.total); 109 } 110 111 /** 112 * kunit_log_newline() - Add newline to the end of log if one is not 113 * already present. 114 * @log: The log to add the newline to. 115 */ 116 static void kunit_log_newline(char *log) 117 { 118 int log_len, len_left; 119 120 log_len = strlen(log); 121 len_left = KUNIT_LOG_SIZE - log_len - 1; 122 123 if (log_len > 0 && log[log_len - 1] != '\n') 124 strncat(log, "\n", len_left); 125 } 126 127 /* 128 * Append formatted message to log, size of which is limited to 129 * KUNIT_LOG_SIZE bytes (including null terminating byte). 130 */ 131 void kunit_log_append(char *log, const char *fmt, ...) 132 { 133 va_list args; 134 int len, log_len, len_left; 135 136 if (!log) 137 return; 138 139 log_len = strlen(log); 140 len_left = KUNIT_LOG_SIZE - log_len - 1; 141 if (len_left <= 0) 142 return; 143 144 /* Evaluate length of line to add to log */ 145 va_start(args, fmt); 146 len = vsnprintf(NULL, 0, fmt, args) + 1; 147 va_end(args); 148 149 /* Print formatted line to the log */ 150 va_start(args, fmt); 151 vsnprintf(log + log_len, min(len, len_left), fmt, args); 152 va_end(args); 153 154 /* Add newline to end of log if not already present. */ 155 kunit_log_newline(log); 156 } 157 EXPORT_SYMBOL_GPL(kunit_log_append); 158 159 size_t kunit_suite_num_test_cases(struct kunit_suite *suite) 160 { 161 struct kunit_case *test_case; 162 size_t len = 0; 163 164 kunit_suite_for_each_test_case(suite, test_case) 165 len++; 166 167 return len; 168 } 169 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases); 170 171 static void kunit_print_suite_start(struct kunit_suite *suite) 172 { 173 /* 174 * We do not log the test suite header as doing so would 175 * mean debugfs display would consist of the test suite 176 * header prior to individual test results. 177 * Hence directly printk the suite status, and we will 178 * separately seq_printf() the suite header for the debugfs 179 * representation. 180 */ 181 pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n"); 182 pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n", 183 suite->name); 184 pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n", 185 kunit_suite_num_test_cases(suite)); 186 } 187 188 static void kunit_print_ok_not_ok(void *test_or_suite, 189 bool is_test, 190 enum kunit_status status, 191 size_t test_number, 192 const char *description, 193 const char *directive) 194 { 195 struct kunit_suite *suite = is_test ? NULL : test_or_suite; 196 struct kunit *test = is_test ? test_or_suite : NULL; 197 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : ""; 198 199 /* 200 * We do not log the test suite results as doing so would 201 * mean debugfs display would consist of an incorrect test 202 * number. Hence directly printk the suite result, and we will 203 * separately seq_printf() the suite results for the debugfs 204 * representation. 205 */ 206 if (suite) 207 pr_info("%s %zd %s%s%s\n", 208 kunit_status_to_ok_not_ok(status), 209 test_number, description, directive_header, 210 (status == KUNIT_SKIPPED) ? directive : ""); 211 else 212 kunit_log(KERN_INFO, test, 213 KUNIT_SUBTEST_INDENT "%s %zd %s%s%s", 214 kunit_status_to_ok_not_ok(status), 215 test_number, description, directive_header, 216 (status == KUNIT_SKIPPED) ? directive : ""); 217 } 218 219 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite) 220 { 221 const struct kunit_case *test_case; 222 enum kunit_status status = KUNIT_SKIPPED; 223 224 if (suite->suite_init_err) 225 return KUNIT_FAILURE; 226 227 kunit_suite_for_each_test_case(suite, test_case) { 228 if (test_case->status == KUNIT_FAILURE) 229 return KUNIT_FAILURE; 230 else if (test_case->status == KUNIT_SUCCESS) 231 status = KUNIT_SUCCESS; 232 } 233 234 return status; 235 } 236 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded); 237 238 static size_t kunit_suite_counter = 1; 239 240 static void kunit_print_suite_end(struct kunit_suite *suite) 241 { 242 kunit_print_ok_not_ok((void *)suite, false, 243 kunit_suite_has_succeeded(suite), 244 kunit_suite_counter++, 245 suite->name, 246 suite->status_comment); 247 } 248 249 unsigned int kunit_test_case_num(struct kunit_suite *suite, 250 struct kunit_case *test_case) 251 { 252 struct kunit_case *tc; 253 unsigned int i = 1; 254 255 kunit_suite_for_each_test_case(suite, tc) { 256 if (tc == test_case) 257 return i; 258 i++; 259 } 260 261 return 0; 262 } 263 EXPORT_SYMBOL_GPL(kunit_test_case_num); 264 265 static void kunit_print_string_stream(struct kunit *test, 266 struct string_stream *stream) 267 { 268 struct string_stream_fragment *fragment; 269 char *buf; 270 271 if (string_stream_is_empty(stream)) 272 return; 273 274 buf = string_stream_get_string(stream); 275 if (!buf) { 276 kunit_err(test, 277 "Could not allocate buffer, dumping stream:\n"); 278 list_for_each_entry(fragment, &stream->fragments, node) { 279 kunit_err(test, "%s", fragment->fragment); 280 } 281 kunit_err(test, "\n"); 282 } else { 283 kunit_err(test, "%s", buf); 284 kunit_kfree(test, buf); 285 } 286 } 287 288 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc, 289 enum kunit_assert_type type, const struct kunit_assert *assert, 290 assert_format_t assert_format, const struct va_format *message) 291 { 292 struct string_stream *stream; 293 294 kunit_set_failure(test); 295 296 stream = alloc_string_stream(test, GFP_KERNEL); 297 if (IS_ERR(stream)) { 298 WARN(true, 299 "Could not allocate stream to print failed assertion in %s:%d\n", 300 loc->file, 301 loc->line); 302 return; 303 } 304 305 kunit_assert_prologue(loc, type, stream); 306 assert_format(assert, message, stream); 307 308 kunit_print_string_stream(test, stream); 309 310 string_stream_destroy(stream); 311 } 312 313 static void __noreturn kunit_abort(struct kunit *test) 314 { 315 kunit_try_catch_throw(&test->try_catch); /* Does not return. */ 316 317 /* 318 * Throw could not abort from test. 319 * 320 * XXX: we should never reach this line! As kunit_try_catch_throw is 321 * marked __noreturn. 322 */ 323 WARN_ONCE(true, "Throw could not abort from test!\n"); 324 } 325 326 void kunit_do_failed_assertion(struct kunit *test, 327 const struct kunit_loc *loc, 328 enum kunit_assert_type type, 329 const struct kunit_assert *assert, 330 assert_format_t assert_format, 331 const char *fmt, ...) 332 { 333 va_list args; 334 struct va_format message; 335 va_start(args, fmt); 336 337 message.fmt = fmt; 338 message.va = &args; 339 340 kunit_fail(test, loc, type, assert, assert_format, &message); 341 342 va_end(args); 343 344 if (type == KUNIT_ASSERTION) 345 kunit_abort(test); 346 } 347 EXPORT_SYMBOL_GPL(kunit_do_failed_assertion); 348 349 void kunit_init_test(struct kunit *test, const char *name, char *log) 350 { 351 spin_lock_init(&test->lock); 352 INIT_LIST_HEAD(&test->resources); 353 test->name = name; 354 test->log = log; 355 if (test->log) 356 test->log[0] = '\0'; 357 test->status = KUNIT_SUCCESS; 358 test->status_comment[0] = '\0'; 359 } 360 EXPORT_SYMBOL_GPL(kunit_init_test); 361 362 /* 363 * Initializes and runs test case. Does not clean up or do post validations. 364 */ 365 static void kunit_run_case_internal(struct kunit *test, 366 struct kunit_suite *suite, 367 struct kunit_case *test_case) 368 { 369 if (suite->init) { 370 int ret; 371 372 ret = suite->init(test); 373 if (ret) { 374 kunit_err(test, "failed to initialize: %d\n", ret); 375 kunit_set_failure(test); 376 return; 377 } 378 } 379 380 test_case->run_case(test); 381 } 382 383 static void kunit_case_internal_cleanup(struct kunit *test) 384 { 385 kunit_cleanup(test); 386 } 387 388 /* 389 * Performs post validations and cleanup after a test case was run. 390 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal! 391 */ 392 static void kunit_run_case_cleanup(struct kunit *test, 393 struct kunit_suite *suite) 394 { 395 if (suite->exit) 396 suite->exit(test); 397 398 kunit_case_internal_cleanup(test); 399 } 400 401 struct kunit_try_catch_context { 402 struct kunit *test; 403 struct kunit_suite *suite; 404 struct kunit_case *test_case; 405 }; 406 407 static void kunit_try_run_case(void *data) 408 { 409 struct kunit_try_catch_context *ctx = data; 410 struct kunit *test = ctx->test; 411 struct kunit_suite *suite = ctx->suite; 412 struct kunit_case *test_case = ctx->test_case; 413 414 current->kunit_test = test; 415 416 /* 417 * kunit_run_case_internal may encounter a fatal error; if it does, 418 * abort will be called, this thread will exit, and finally the parent 419 * thread will resume control and handle any necessary clean up. 420 */ 421 kunit_run_case_internal(test, suite, test_case); 422 } 423 424 static void kunit_try_run_case_cleanup(void *data) 425 { 426 struct kunit_try_catch_context *ctx = data; 427 struct kunit *test = ctx->test; 428 struct kunit_suite *suite = ctx->suite; 429 430 current->kunit_test = test; 431 432 kunit_run_case_cleanup(test, suite); 433 } 434 435 static void kunit_catch_run_case_cleanup(void *data) 436 { 437 struct kunit_try_catch_context *ctx = data; 438 struct kunit *test = ctx->test; 439 int try_exit_code = kunit_try_catch_get_result(&test->try_catch); 440 441 /* It is always a failure if cleanup aborts. */ 442 kunit_set_failure(test); 443 444 if (try_exit_code) { 445 /* 446 * Test case could not finish, we have no idea what state it is 447 * in, so don't do clean up. 448 */ 449 if (try_exit_code == -ETIMEDOUT) { 450 kunit_err(test, "test case cleanup timed out\n"); 451 /* 452 * Unknown internal error occurred preventing test case from 453 * running, so there is nothing to clean up. 454 */ 455 } else { 456 kunit_err(test, "internal error occurred during test case cleanup: %d\n", 457 try_exit_code); 458 } 459 return; 460 } 461 462 kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n"); 463 } 464 465 466 static void kunit_catch_run_case(void *data) 467 { 468 struct kunit_try_catch_context *ctx = data; 469 struct kunit *test = ctx->test; 470 int try_exit_code = kunit_try_catch_get_result(&test->try_catch); 471 472 if (try_exit_code) { 473 kunit_set_failure(test); 474 /* 475 * Test case could not finish, we have no idea what state it is 476 * in, so don't do clean up. 477 */ 478 if (try_exit_code == -ETIMEDOUT) { 479 kunit_err(test, "test case timed out\n"); 480 /* 481 * Unknown internal error occurred preventing test case from 482 * running, so there is nothing to clean up. 483 */ 484 } else { 485 kunit_err(test, "internal error occurred preventing test case from running: %d\n", 486 try_exit_code); 487 } 488 return; 489 } 490 } 491 492 /* 493 * Performs all logic to run a test case. It also catches most errors that 494 * occur in a test case and reports them as failures. 495 */ 496 static void kunit_run_case_catch_errors(struct kunit_suite *suite, 497 struct kunit_case *test_case, 498 struct kunit *test) 499 { 500 struct kunit_try_catch_context context; 501 struct kunit_try_catch *try_catch; 502 503 try_catch = &test->try_catch; 504 505 kunit_try_catch_init(try_catch, 506 test, 507 kunit_try_run_case, 508 kunit_catch_run_case); 509 context.test = test; 510 context.suite = suite; 511 context.test_case = test_case; 512 kunit_try_catch_run(try_catch, &context); 513 514 /* Now run the cleanup */ 515 kunit_try_catch_init(try_catch, 516 test, 517 kunit_try_run_case_cleanup, 518 kunit_catch_run_case_cleanup); 519 kunit_try_catch_run(try_catch, &context); 520 521 /* Propagate the parameter result to the test case. */ 522 if (test->status == KUNIT_FAILURE) 523 test_case->status = KUNIT_FAILURE; 524 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS) 525 test_case->status = KUNIT_SUCCESS; 526 } 527 528 static void kunit_print_suite_stats(struct kunit_suite *suite, 529 struct kunit_result_stats suite_stats, 530 struct kunit_result_stats param_stats) 531 { 532 if (kunit_should_print_stats(suite_stats)) { 533 kunit_log(KERN_INFO, suite, 534 "# %s: pass:%lu fail:%lu skip:%lu total:%lu", 535 suite->name, 536 suite_stats.passed, 537 suite_stats.failed, 538 suite_stats.skipped, 539 suite_stats.total); 540 } 541 542 if (kunit_should_print_stats(param_stats)) { 543 kunit_log(KERN_INFO, suite, 544 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu", 545 param_stats.passed, 546 param_stats.failed, 547 param_stats.skipped, 548 param_stats.total); 549 } 550 } 551 552 static void kunit_update_stats(struct kunit_result_stats *stats, 553 enum kunit_status status) 554 { 555 switch (status) { 556 case KUNIT_SUCCESS: 557 stats->passed++; 558 break; 559 case KUNIT_SKIPPED: 560 stats->skipped++; 561 break; 562 case KUNIT_FAILURE: 563 stats->failed++; 564 break; 565 } 566 567 stats->total++; 568 } 569 570 static void kunit_accumulate_stats(struct kunit_result_stats *total, 571 struct kunit_result_stats add) 572 { 573 total->passed += add.passed; 574 total->skipped += add.skipped; 575 total->failed += add.failed; 576 total->total += add.total; 577 } 578 579 int kunit_run_tests(struct kunit_suite *suite) 580 { 581 char param_desc[KUNIT_PARAM_DESC_SIZE]; 582 struct kunit_case *test_case; 583 struct kunit_result_stats suite_stats = { 0 }; 584 struct kunit_result_stats total_stats = { 0 }; 585 586 /* Taint the kernel so we know we've run tests. */ 587 add_taint(TAINT_TEST, LOCKDEP_STILL_OK); 588 589 if (suite->suite_init) { 590 suite->suite_init_err = suite->suite_init(suite); 591 if (suite->suite_init_err) { 592 kunit_err(suite, KUNIT_SUBTEST_INDENT 593 "# failed to initialize (%d)", suite->suite_init_err); 594 goto suite_end; 595 } 596 } 597 598 kunit_print_suite_start(suite); 599 600 kunit_suite_for_each_test_case(suite, test_case) { 601 struct kunit test = { .param_value = NULL, .param_index = 0 }; 602 struct kunit_result_stats param_stats = { 0 }; 603 test_case->status = KUNIT_SKIPPED; 604 605 kunit_init_test(&test, test_case->name, test_case->log); 606 607 if (!test_case->generate_params) { 608 /* Non-parameterised test. */ 609 kunit_run_case_catch_errors(suite, test_case, &test); 610 kunit_update_stats(¶m_stats, test.status); 611 } else { 612 /* Get initial param. */ 613 param_desc[0] = '\0'; 614 test.param_value = test_case->generate_params(NULL, param_desc); 615 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT 616 "KTAP version 1\n"); 617 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT 618 "# Subtest: %s", test_case->name); 619 620 while (test.param_value) { 621 kunit_run_case_catch_errors(suite, test_case, &test); 622 623 if (param_desc[0] == '\0') { 624 snprintf(param_desc, sizeof(param_desc), 625 "param-%d", test.param_index); 626 } 627 628 kunit_log(KERN_INFO, &test, 629 KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT 630 "%s %d %s", 631 kunit_status_to_ok_not_ok(test.status), 632 test.param_index + 1, param_desc); 633 634 /* Get next param. */ 635 param_desc[0] = '\0'; 636 test.param_value = test_case->generate_params(test.param_value, param_desc); 637 test.param_index++; 638 639 kunit_update_stats(¶m_stats, test.status); 640 } 641 } 642 643 644 kunit_print_test_stats(&test, param_stats); 645 646 kunit_print_ok_not_ok(&test, true, test_case->status, 647 kunit_test_case_num(suite, test_case), 648 test_case->name, 649 test.status_comment); 650 651 kunit_update_stats(&suite_stats, test_case->status); 652 kunit_accumulate_stats(&total_stats, param_stats); 653 } 654 655 if (suite->suite_exit) 656 suite->suite_exit(suite); 657 658 kunit_print_suite_stats(suite, suite_stats, total_stats); 659 suite_end: 660 kunit_print_suite_end(suite); 661 662 return 0; 663 } 664 EXPORT_SYMBOL_GPL(kunit_run_tests); 665 666 static void kunit_init_suite(struct kunit_suite *suite) 667 { 668 kunit_debugfs_create_suite(suite); 669 suite->status_comment[0] = '\0'; 670 suite->suite_init_err = 0; 671 } 672 673 bool kunit_enabled(void) 674 { 675 return enable_param; 676 } 677 678 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites) 679 { 680 unsigned int i; 681 682 if (!kunit_enabled() && num_suites > 0) { 683 pr_info("kunit: disabled\n"); 684 return 0; 685 } 686 687 static_branch_inc(&kunit_running); 688 689 for (i = 0; i < num_suites; i++) { 690 kunit_init_suite(suites[i]); 691 kunit_run_tests(suites[i]); 692 } 693 694 static_branch_dec(&kunit_running); 695 return 0; 696 } 697 EXPORT_SYMBOL_GPL(__kunit_test_suites_init); 698 699 static void kunit_exit_suite(struct kunit_suite *suite) 700 { 701 kunit_debugfs_destroy_suite(suite); 702 } 703 704 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites) 705 { 706 unsigned int i; 707 708 if (!kunit_enabled()) 709 return; 710 711 for (i = 0; i < num_suites; i++) 712 kunit_exit_suite(suites[i]); 713 714 kunit_suite_counter = 1; 715 } 716 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit); 717 718 #ifdef CONFIG_MODULES 719 static void kunit_module_init(struct module *mod) 720 { 721 __kunit_test_suites_init(mod->kunit_suites, mod->num_kunit_suites); 722 } 723 724 static void kunit_module_exit(struct module *mod) 725 { 726 __kunit_test_suites_exit(mod->kunit_suites, mod->num_kunit_suites); 727 } 728 729 static int kunit_module_notify(struct notifier_block *nb, unsigned long val, 730 void *data) 731 { 732 struct module *mod = data; 733 734 switch (val) { 735 case MODULE_STATE_LIVE: 736 kunit_module_init(mod); 737 break; 738 case MODULE_STATE_GOING: 739 kunit_module_exit(mod); 740 break; 741 case MODULE_STATE_COMING: 742 case MODULE_STATE_UNFORMED: 743 break; 744 } 745 746 return 0; 747 } 748 749 static struct notifier_block kunit_mod_nb = { 750 .notifier_call = kunit_module_notify, 751 .priority = 0, 752 }; 753 #endif 754 755 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp) 756 { 757 void *data; 758 759 data = kmalloc_array(n, size, gfp); 760 761 if (!data) 762 return NULL; 763 764 if (kunit_add_action_or_reset(test, (kunit_action_t *)kfree, data) != 0) 765 return NULL; 766 767 return data; 768 } 769 EXPORT_SYMBOL_GPL(kunit_kmalloc_array); 770 771 void kunit_kfree(struct kunit *test, const void *ptr) 772 { 773 if (!ptr) 774 return; 775 776 kunit_release_action(test, (kunit_action_t *)kfree, (void *)ptr); 777 } 778 EXPORT_SYMBOL_GPL(kunit_kfree); 779 780 void kunit_cleanup(struct kunit *test) 781 { 782 struct kunit_resource *res; 783 unsigned long flags; 784 785 /* 786 * test->resources is a stack - each allocation must be freed in the 787 * reverse order from which it was added since one resource may depend 788 * on another for its entire lifetime. 789 * Also, we cannot use the normal list_for_each constructs, even the 790 * safe ones because *arbitrary* nodes may be deleted when 791 * kunit_resource_free is called; the list_for_each_safe variants only 792 * protect against the current node being deleted, not the next. 793 */ 794 while (true) { 795 spin_lock_irqsave(&test->lock, flags); 796 if (list_empty(&test->resources)) { 797 spin_unlock_irqrestore(&test->lock, flags); 798 break; 799 } 800 res = list_last_entry(&test->resources, 801 struct kunit_resource, 802 node); 803 /* 804 * Need to unlock here as a resource may remove another 805 * resource, and this can't happen if the test->lock 806 * is held. 807 */ 808 spin_unlock_irqrestore(&test->lock, flags); 809 kunit_remove_resource(test, res); 810 } 811 current->kunit_test = NULL; 812 } 813 EXPORT_SYMBOL_GPL(kunit_cleanup); 814 815 static int __init kunit_init(void) 816 { 817 /* Install the KUnit hook functions. */ 818 kunit_install_hooks(); 819 820 kunit_debugfs_init(); 821 #ifdef CONFIG_MODULES 822 return register_module_notifier(&kunit_mod_nb); 823 #else 824 return 0; 825 #endif 826 } 827 late_initcall(kunit_init); 828 829 static void __exit kunit_exit(void) 830 { 831 memset(&kunit_hooks, 0, sizeof(kunit_hooks)); 832 #ifdef CONFIG_MODULES 833 unregister_module_notifier(&kunit_mod_nb); 834 #endif 835 kunit_debugfs_cleanup(); 836 } 837 module_exit(kunit_exit); 838 839 MODULE_LICENSE("GPL v2"); 840