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