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