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