1 // SPDX-License-Identifier: GPL-2.0
3 * Base unit test (KUnit) API.
5 * Copyright (C) 2019, Google LLC.
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/mutex.h>
17 #include <linux/panic.h>
18 #include <linux/sched/debug.h>
19 #include <linux/sched.h>
23 #include "device-impl.h"
24 #include "hooks-impl.h"
25 #include "string-stream.h"
26 #include "try-catch-impl.h"
28 static DEFINE_MUTEX(kunit_run_lock);
31 * Hook to fail the current test and print an error message to the log.
33 void __printf(3, 4) __kunit_fail_current_test_impl(const char *file, int line, const char *fmt, ...)
39 if (!current->kunit_test)
42 kunit_set_failure(current->kunit_test);
44 /* kunit_err() only accepts literals, so evaluate the args first. */
46 len = vsnprintf(NULL, 0, fmt, args) + 1;
49 buffer = kunit_kmalloc(current->kunit_test, len, GFP_KERNEL);
54 vsnprintf(buffer, len, fmt, args);
57 kunit_err(current->kunit_test, "%s:%d: %s", file, line, buffer);
58 kunit_kfree(current->kunit_test, buffer);
62 * Enable KUnit tests to run.
64 #ifdef CONFIG_KUNIT_DEFAULT_ENABLED
65 static bool enable_param = true;
67 static bool enable_param;
69 module_param_named(enable, enable_param, bool, 0);
70 MODULE_PARM_DESC(enable, "Enable KUnit tests");
73 * KUnit statistic mode:
75 * 1 - only when there is more than one subtest
78 static int kunit_stats_enabled = 1;
79 module_param_named(stats_enabled, kunit_stats_enabled, int, 0644);
80 MODULE_PARM_DESC(stats_enabled,
81 "Print test stats: never (0), only for multiple subtests (1), or always (2)");
83 struct kunit_result_stats {
85 unsigned long skipped;
90 static bool kunit_should_print_stats(struct kunit_result_stats stats)
92 if (kunit_stats_enabled == 0)
95 if (kunit_stats_enabled == 2)
98 return (stats.total > 1);
101 static void kunit_print_test_stats(struct kunit *test,
102 struct kunit_result_stats stats)
104 if (!kunit_should_print_stats(stats))
107 kunit_log(KERN_INFO, test,
109 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
117 /* Append formatted message to log. */
118 void kunit_log_append(struct string_stream *log, const char *fmt, ...)
126 string_stream_vadd(log, fmt, args);
129 EXPORT_SYMBOL_GPL(kunit_log_append);
131 size_t kunit_suite_num_test_cases(struct kunit_suite *suite)
133 struct kunit_case *test_case;
136 kunit_suite_for_each_test_case(suite, test_case)
141 EXPORT_SYMBOL_GPL(kunit_suite_num_test_cases);
143 /* Currently supported test levels */
145 KUNIT_LEVEL_SUITE = 0,
147 KUNIT_LEVEL_CASE_PARAM,
150 static void kunit_print_suite_start(struct kunit_suite *suite)
153 * We do not log the test suite header as doing so would
154 * mean debugfs display would consist of the test suite
155 * header prior to individual test results.
156 * Hence directly printk the suite status, and we will
157 * separately seq_printf() the suite header for the debugfs
160 pr_info(KUNIT_SUBTEST_INDENT "KTAP version 1\n");
161 pr_info(KUNIT_SUBTEST_INDENT "# Subtest: %s\n",
163 kunit_print_attr((void *)suite, false, KUNIT_LEVEL_CASE);
164 pr_info(KUNIT_SUBTEST_INDENT "1..%zd\n",
165 kunit_suite_num_test_cases(suite));
168 static void kunit_print_ok_not_ok(struct kunit *test,
169 unsigned int test_level,
170 enum kunit_status status,
172 const char *description,
173 const char *directive)
175 const char *directive_header = (status == KUNIT_SKIPPED) ? " # SKIP " : "";
176 const char *directive_body = (status == KUNIT_SKIPPED) ? directive : "";
179 * When test is NULL assume that results are from the suite
180 * and today suite results are expected at level 0 only.
182 WARN(!test && test_level, "suite test level can't be %u!\n", test_level);
185 * We do not log the test suite results as doing so would
186 * mean debugfs display would consist of an incorrect test
187 * number. Hence directly printk the suite result, and we will
188 * separately seq_printf() the suite results for the debugfs
192 pr_info("%s %zd %s%s%s\n",
193 kunit_status_to_ok_not_ok(status),
194 test_number, description, directive_header,
197 kunit_log(KERN_INFO, test,
199 KUNIT_INDENT_LEN * test_level, "",
200 kunit_status_to_ok_not_ok(status),
201 test_number, description, directive_header,
205 enum kunit_status kunit_suite_has_succeeded(struct kunit_suite *suite)
207 const struct kunit_case *test_case;
208 enum kunit_status status = KUNIT_SKIPPED;
210 if (suite->suite_init_err)
211 return KUNIT_FAILURE;
213 kunit_suite_for_each_test_case(suite, test_case) {
214 if (test_case->status == KUNIT_FAILURE)
215 return KUNIT_FAILURE;
216 else if (test_case->status == KUNIT_SUCCESS)
217 status = KUNIT_SUCCESS;
222 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
224 static size_t kunit_suite_counter = 1;
226 static void kunit_print_suite_end(struct kunit_suite *suite)
228 kunit_print_ok_not_ok(NULL, KUNIT_LEVEL_SUITE,
229 kunit_suite_has_succeeded(suite),
230 kunit_suite_counter++,
232 suite->status_comment);
235 unsigned int kunit_test_case_num(struct kunit_suite *suite,
236 struct kunit_case *test_case)
238 struct kunit_case *tc;
241 kunit_suite_for_each_test_case(suite, tc) {
249 EXPORT_SYMBOL_GPL(kunit_test_case_num);
251 static void kunit_print_string_stream(struct kunit *test,
252 struct string_stream *stream)
254 struct string_stream_fragment *fragment;
257 if (string_stream_is_empty(stream))
260 buf = string_stream_get_string(stream);
263 "Could not allocate buffer, dumping stream:\n");
264 list_for_each_entry(fragment, &stream->fragments, node) {
265 kunit_err(test, "%s", fragment->fragment);
267 kunit_err(test, "\n");
269 kunit_err(test, "%s", buf);
274 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
275 enum kunit_assert_type type, const struct kunit_assert *assert,
276 assert_format_t assert_format, const struct va_format *message)
278 struct string_stream *stream;
280 kunit_set_failure(test);
282 stream = kunit_alloc_string_stream(test, GFP_KERNEL);
283 if (IS_ERR(stream)) {
285 "Could not allocate stream to print failed assertion in %s:%d\n",
291 kunit_assert_prologue(loc, type, stream);
292 assert_format(assert, message, stream);
294 kunit_print_string_stream(test, stream);
296 kunit_free_string_stream(test, stream);
299 void __noreturn __kunit_abort(struct kunit *test)
301 kunit_try_catch_throw(&test->try_catch); /* Does not return. */
304 * Throw could not abort from test.
306 * XXX: we should never reach this line! As kunit_try_catch_throw is
309 WARN_ONCE(true, "Throw could not abort from test!\n");
311 EXPORT_SYMBOL_GPL(__kunit_abort);
313 void __kunit_do_failed_assertion(struct kunit *test,
314 const struct kunit_loc *loc,
315 enum kunit_assert_type type,
316 const struct kunit_assert *assert,
317 assert_format_t assert_format,
318 const char *fmt, ...)
321 struct va_format message;
327 kunit_fail(test, loc, type, assert, assert_format, &message);
331 EXPORT_SYMBOL_GPL(__kunit_do_failed_assertion);
333 void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log)
335 spin_lock_init(&test->lock);
336 INIT_LIST_HEAD(&test->resources);
340 string_stream_clear(log);
341 test->status = KUNIT_SUCCESS;
342 test->status_comment[0] = '\0';
344 EXPORT_SYMBOL_GPL(kunit_init_test);
346 /* Only warn when a test takes more than twice the threshold */
347 #define KUNIT_SPEED_WARNING_MULTIPLIER 2
349 /* Slow tests are defined as taking more than 1s */
350 #define KUNIT_SPEED_SLOW_THRESHOLD_S 1
352 #define KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S \
353 (KUNIT_SPEED_WARNING_MULTIPLIER * KUNIT_SPEED_SLOW_THRESHOLD_S)
355 #define s_to_timespec64(s) ns_to_timespec64((s) * NSEC_PER_SEC)
357 static void kunit_run_case_check_speed(struct kunit *test,
358 struct kunit_case *test_case,
359 struct timespec64 duration)
361 struct timespec64 slow_thr =
362 s_to_timespec64(KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S);
363 enum kunit_speed speed = test_case->attr.speed;
365 if (timespec64_compare(&duration, &slow_thr) < 0)
368 if (speed == KUNIT_SPEED_VERY_SLOW || speed == KUNIT_SPEED_SLOW)
372 "Test should be marked slow (runtime: %lld.%09lds)",
373 duration.tv_sec, duration.tv_nsec);
377 * Initializes and runs test case. Does not clean up or do post validations.
379 static void kunit_run_case_internal(struct kunit *test,
380 struct kunit_suite *suite,
381 struct kunit_case *test_case)
383 struct timespec64 start, end;
388 ret = suite->init(test);
390 kunit_err(test, "failed to initialize: %d\n", ret);
391 kunit_set_failure(test);
396 ktime_get_ts64(&start);
398 test_case->run_case(test);
400 ktime_get_ts64(&end);
402 kunit_run_case_check_speed(test, test_case, timespec64_sub(end, start));
405 static void kunit_case_internal_cleanup(struct kunit *test)
411 * Performs post validations and cleanup after a test case was run.
412 * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
414 static void kunit_run_case_cleanup(struct kunit *test,
415 struct kunit_suite *suite)
420 kunit_case_internal_cleanup(test);
423 struct kunit_try_catch_context {
425 struct kunit_suite *suite;
426 struct kunit_case *test_case;
429 static void kunit_try_run_case(void *data)
431 struct kunit_try_catch_context *ctx = data;
432 struct kunit *test = ctx->test;
433 struct kunit_suite *suite = ctx->suite;
434 struct kunit_case *test_case = ctx->test_case;
436 current->kunit_test = test;
439 * kunit_run_case_internal may encounter a fatal error; if it does,
440 * abort will be called, this thread will exit, and finally the parent
441 * thread will resume control and handle any necessary clean up.
443 kunit_run_case_internal(test, suite, test_case);
446 static void kunit_try_run_case_cleanup(void *data)
448 struct kunit_try_catch_context *ctx = data;
449 struct kunit *test = ctx->test;
450 struct kunit_suite *suite = ctx->suite;
452 current->kunit_test = test;
454 kunit_run_case_cleanup(test, suite);
457 static void kunit_catch_run_case_cleanup(void *data)
459 struct kunit_try_catch_context *ctx = data;
460 struct kunit *test = ctx->test;
461 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
463 /* It is always a failure if cleanup aborts. */
464 kunit_set_failure(test);
468 * Test case could not finish, we have no idea what state it is
469 * in, so don't do clean up.
471 if (try_exit_code == -ETIMEDOUT) {
472 kunit_err(test, "test case cleanup timed out\n");
474 * Unknown internal error occurred preventing test case from
475 * running, so there is nothing to clean up.
478 kunit_err(test, "internal error occurred during test case cleanup: %d\n",
484 kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n");
488 static void kunit_catch_run_case(void *data)
490 struct kunit_try_catch_context *ctx = data;
491 struct kunit *test = ctx->test;
492 int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
495 kunit_set_failure(test);
497 * Test case could not finish, we have no idea what state it is
498 * in, so don't do clean up.
500 if (try_exit_code == -ETIMEDOUT) {
501 kunit_err(test, "test case timed out\n");
503 * Unknown internal error occurred preventing test case from
504 * running, so there is nothing to clean up.
507 kunit_err(test, "internal error occurred preventing test case from running: %d\n",
515 * Performs all logic to run a test case. It also catches most errors that
516 * occur in a test case and reports them as failures.
518 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
519 struct kunit_case *test_case,
522 struct kunit_try_catch_context context;
523 struct kunit_try_catch *try_catch;
525 try_catch = &test->try_catch;
527 kunit_try_catch_init(try_catch,
530 kunit_catch_run_case);
532 context.suite = suite;
533 context.test_case = test_case;
534 kunit_try_catch_run(try_catch, &context);
536 /* Now run the cleanup */
537 kunit_try_catch_init(try_catch,
539 kunit_try_run_case_cleanup,
540 kunit_catch_run_case_cleanup);
541 kunit_try_catch_run(try_catch, &context);
543 /* Propagate the parameter result to the test case. */
544 if (test->status == KUNIT_FAILURE)
545 test_case->status = KUNIT_FAILURE;
546 else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
547 test_case->status = KUNIT_SUCCESS;
550 static void kunit_print_suite_stats(struct kunit_suite *suite,
551 struct kunit_result_stats suite_stats,
552 struct kunit_result_stats param_stats)
554 if (kunit_should_print_stats(suite_stats)) {
555 kunit_log(KERN_INFO, suite,
556 "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
564 if (kunit_should_print_stats(param_stats)) {
565 kunit_log(KERN_INFO, suite,
566 "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
574 static void kunit_update_stats(struct kunit_result_stats *stats,
575 enum kunit_status status)
592 static void kunit_accumulate_stats(struct kunit_result_stats *total,
593 struct kunit_result_stats add)
595 total->passed += add.passed;
596 total->skipped += add.skipped;
597 total->failed += add.failed;
598 total->total += add.total;
601 int kunit_run_tests(struct kunit_suite *suite)
603 char param_desc[KUNIT_PARAM_DESC_SIZE];
604 struct kunit_case *test_case;
605 struct kunit_result_stats suite_stats = { 0 };
606 struct kunit_result_stats total_stats = { 0 };
608 /* Taint the kernel so we know we've run tests. */
609 add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
611 if (suite->suite_init) {
612 suite->suite_init_err = suite->suite_init(suite);
613 if (suite->suite_init_err) {
614 kunit_err(suite, KUNIT_SUBTEST_INDENT
615 "# failed to initialize (%d)", suite->suite_init_err);
620 kunit_print_suite_start(suite);
622 kunit_suite_for_each_test_case(suite, test_case) {
623 struct kunit test = { .param_value = NULL, .param_index = 0 };
624 struct kunit_result_stats param_stats = { 0 };
626 kunit_init_test(&test, test_case->name, test_case->log);
627 if (test_case->status == KUNIT_SKIPPED) {
628 /* Test marked as skip */
629 test.status = KUNIT_SKIPPED;
630 kunit_update_stats(¶m_stats, test.status);
631 } else if (!test_case->generate_params) {
632 /* Non-parameterised test. */
633 test_case->status = KUNIT_SKIPPED;
634 kunit_run_case_catch_errors(suite, test_case, &test);
635 kunit_update_stats(¶m_stats, test.status);
637 /* Get initial param. */
638 param_desc[0] = '\0';
639 test.param_value = test_case->generate_params(NULL, param_desc);
640 test_case->status = KUNIT_SKIPPED;
641 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
643 kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
644 "# Subtest: %s", test_case->name);
646 while (test.param_value) {
647 kunit_run_case_catch_errors(suite, test_case, &test);
649 if (param_desc[0] == '\0') {
650 snprintf(param_desc, sizeof(param_desc),
651 "param-%d", test.param_index);
654 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE_PARAM,
656 test.param_index + 1,
658 test.status_comment);
660 kunit_update_stats(¶m_stats, test.status);
662 /* Get next param. */
663 param_desc[0] = '\0';
664 test.param_value = test_case->generate_params(test.param_value, param_desc);
666 test.status = KUNIT_SUCCESS;
667 test.status_comment[0] = '\0';
672 kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
674 kunit_print_test_stats(&test, param_stats);
676 kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE, test_case->status,
677 kunit_test_case_num(suite, test_case),
679 test.status_comment);
681 kunit_update_stats(&suite_stats, test_case->status);
682 kunit_accumulate_stats(&total_stats, param_stats);
685 if (suite->suite_exit)
686 suite->suite_exit(suite);
688 kunit_print_suite_stats(suite, suite_stats, total_stats);
690 kunit_print_suite_end(suite);
694 EXPORT_SYMBOL_GPL(kunit_run_tests);
696 static void kunit_init_suite(struct kunit_suite *suite)
698 kunit_debugfs_create_suite(suite);
699 suite->status_comment[0] = '\0';
700 suite->suite_init_err = 0;
703 string_stream_clear(suite->log);
706 bool kunit_enabled(void)
711 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites)
718 if (!kunit_enabled() && num_suites > 0) {
719 pr_info("kunit: disabled\n");
723 kunit_suite_counter = 1;
725 /* Use mutex lock to guard against running tests concurrently. */
726 if (mutex_lock_interruptible(&kunit_run_lock)) {
727 pr_err("kunit: test interrupted\n");
730 static_branch_inc(&kunit_running);
732 for (i = 0; i < num_suites; i++) {
733 kunit_init_suite(suites[i]);
734 kunit_run_tests(suites[i]);
737 static_branch_dec(&kunit_running);
738 mutex_unlock(&kunit_run_lock);
741 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
743 static void kunit_exit_suite(struct kunit_suite *suite)
745 kunit_debugfs_destroy_suite(suite);
748 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
752 if (!kunit_enabled())
755 for (i = 0; i < num_suites; i++)
756 kunit_exit_suite(suites[i]);
758 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
760 #ifdef CONFIG_MODULES
761 static void kunit_module_init(struct module *mod)
763 struct kunit_suite_set suite_set, filtered_set;
764 struct kunit_suite_set normal_suite_set = {
765 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
767 struct kunit_suite_set init_suite_set = {
768 mod->kunit_init_suites, mod->kunit_init_suites + mod->num_kunit_init_suites,
770 const char *action = kunit_action();
773 if (mod->num_kunit_init_suites > 0)
774 suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set);
776 suite_set = normal_suite_set;
778 filtered_set = kunit_filter_suites(&suite_set,
779 kunit_filter_glob() ?: "*.*",
780 kunit_filter(), kunit_filter_action(),
783 pr_err("kunit module: error filtering suites: %d\n", err);
785 mod->kunit_suites = (struct kunit_suite **)filtered_set.start;
786 mod->num_kunit_suites = filtered_set.end - filtered_set.start;
788 if (mod->num_kunit_init_suites > 0)
789 kfree(suite_set.start);
792 kunit_exec_run_tests(&filtered_set, false);
793 else if (!strcmp(action, "list"))
794 kunit_exec_list_tests(&filtered_set, false);
795 else if (!strcmp(action, "list_attr"))
796 kunit_exec_list_tests(&filtered_set, true);
798 pr_err("kunit: unknown action '%s'\n", action);
801 static void kunit_module_exit(struct module *mod)
803 struct kunit_suite_set suite_set = {
804 mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
806 const char *action = kunit_action();
809 * Check if the start address is a valid virtual address to detect
810 * if the module load sequence has failed and the suite set has not
811 * been initialized and filtered.
813 if (!suite_set.start || !virt_addr_valid(suite_set.start))
817 __kunit_test_suites_exit(mod->kunit_suites,
818 mod->num_kunit_suites);
820 kunit_free_suite_set(suite_set);
823 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
826 struct module *mod = data;
829 case MODULE_STATE_LIVE:
830 kunit_module_init(mod);
832 case MODULE_STATE_GOING:
833 kunit_module_exit(mod);
835 case MODULE_STATE_COMING:
837 case MODULE_STATE_UNFORMED:
844 static struct notifier_block kunit_mod_nb = {
845 .notifier_call = kunit_module_notify,
850 KUNIT_DEFINE_ACTION_WRAPPER(kfree_action_wrapper, kfree, const void *)
852 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
856 data = kmalloc_array(n, size, gfp);
861 if (kunit_add_action_or_reset(test, kfree_action_wrapper, data) != 0)
866 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
868 void kunit_kfree(struct kunit *test, const void *ptr)
873 kunit_release_action(test, kfree_action_wrapper, (void *)ptr);
875 EXPORT_SYMBOL_GPL(kunit_kfree);
877 void kunit_cleanup(struct kunit *test)
879 struct kunit_resource *res;
883 * test->resources is a stack - each allocation must be freed in the
884 * reverse order from which it was added since one resource may depend
885 * on another for its entire lifetime.
886 * Also, we cannot use the normal list_for_each constructs, even the
887 * safe ones because *arbitrary* nodes may be deleted when
888 * kunit_resource_free is called; the list_for_each_safe variants only
889 * protect against the current node being deleted, not the next.
892 spin_lock_irqsave(&test->lock, flags);
893 if (list_empty(&test->resources)) {
894 spin_unlock_irqrestore(&test->lock, flags);
897 res = list_last_entry(&test->resources,
898 struct kunit_resource,
901 * Need to unlock here as a resource may remove another
902 * resource, and this can't happen if the test->lock
905 spin_unlock_irqrestore(&test->lock, flags);
906 kunit_remove_resource(test, res);
908 current->kunit_test = NULL;
910 EXPORT_SYMBOL_GPL(kunit_cleanup);
912 static int __init kunit_init(void)
914 /* Install the KUnit hook functions. */
915 kunit_install_hooks();
917 kunit_debugfs_init();
920 #ifdef CONFIG_MODULES
921 return register_module_notifier(&kunit_mod_nb);
926 late_initcall(kunit_init);
928 static void __exit kunit_exit(void)
930 memset(&kunit_hooks, 0, sizeof(kunit_hooks));
931 #ifdef CONFIG_MODULES
932 unregister_module_notifier(&kunit_mod_nb);
935 kunit_bus_shutdown();
937 kunit_debugfs_cleanup();
939 module_exit(kunit_exit);
941 MODULE_LICENSE("GPL v2");