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