xref: /linux/lib/kunit/test.c (revision 6f46e6fb4e267acf8741cb2c29d368b4de7b6c15)
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  */
__kunit_fail_current_test_impl(const char * file,int line,const char * fmt,...)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 
kunit_should_print_stats(struct kunit_result_stats stats)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 
kunit_print_test_stats(struct kunit * test,struct kunit_result_stats stats)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. */
kunit_log_append(struct string_stream * log,const char * fmt,...)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 
kunit_suite_num_test_cases(struct kunit_suite * suite)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 
kunit_print_suite_start(struct kunit_suite * suite)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 
kunit_print_ok_not_ok(struct kunit * test,unsigned int test_level,enum kunit_status status,size_t test_number,const char * description,const char * directive)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 
kunit_suite_has_succeeded(struct kunit_suite * suite)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->suite_init_err)
218 		return KUNIT_FAILURE;
219 
220 	kunit_suite_for_each_test_case(suite, test_case) {
221 		if (test_case->status == KUNIT_FAILURE)
222 			return KUNIT_FAILURE;
223 		else if (test_case->status == KUNIT_SUCCESS)
224 			status = KUNIT_SUCCESS;
225 	}
226 
227 	return status;
228 }
229 EXPORT_SYMBOL_GPL(kunit_suite_has_succeeded);
230 
231 static size_t kunit_suite_counter = 1;
232 
kunit_print_suite_end(struct kunit_suite * suite)233 static void kunit_print_suite_end(struct kunit_suite *suite)
234 {
235 	kunit_print_ok_not_ok(NULL, KUNIT_LEVEL_SUITE,
236 			      kunit_suite_has_succeeded(suite),
237 			      kunit_suite_counter++,
238 			      suite->name,
239 			      suite->status_comment);
240 }
241 
kunit_test_case_num(struct kunit_suite * suite,struct kunit_case * test_case)242 unsigned int kunit_test_case_num(struct kunit_suite *suite,
243 				 struct kunit_case *test_case)
244 {
245 	struct kunit_case *tc;
246 	unsigned int i = 1;
247 
248 	kunit_suite_for_each_test_case(suite, tc) {
249 		if (tc == test_case)
250 			return i;
251 		i++;
252 	}
253 
254 	return 0;
255 }
256 EXPORT_SYMBOL_GPL(kunit_test_case_num);
257 
kunit_print_string_stream(struct kunit * test,struct string_stream * stream)258 static void kunit_print_string_stream(struct kunit *test,
259 				      struct string_stream *stream)
260 {
261 	struct string_stream_fragment *fragment;
262 	char *buf;
263 
264 	if (string_stream_is_empty(stream))
265 		return;
266 
267 	buf = string_stream_get_string(stream);
268 	if (!buf) {
269 		kunit_err(test,
270 			  "Could not allocate buffer, dumping stream:\n");
271 		list_for_each_entry(fragment, &stream->fragments, node) {
272 			kunit_err(test, "%s", fragment->fragment);
273 		}
274 		kunit_err(test, "\n");
275 	} else {
276 		kunit_err(test, "%s", buf);
277 		kfree(buf);
278 	}
279 }
280 
kunit_fail(struct kunit * test,const struct kunit_loc * loc,enum kunit_assert_type type,const struct kunit_assert * assert,assert_format_t assert_format,const struct va_format * message)281 static void kunit_fail(struct kunit *test, const struct kunit_loc *loc,
282 		       enum kunit_assert_type type, const struct kunit_assert *assert,
283 		       assert_format_t assert_format, const struct va_format *message)
284 {
285 	struct string_stream *stream;
286 
287 	kunit_set_failure(test);
288 
289 	stream = kunit_alloc_string_stream(test, GFP_KERNEL);
290 	if (IS_ERR(stream)) {
291 		WARN(true,
292 		     "Could not allocate stream to print failed assertion in %s:%d\n",
293 		     loc->file,
294 		     loc->line);
295 		return;
296 	}
297 
298 	kunit_assert_prologue(loc, type, stream);
299 	assert_format(assert, message, stream);
300 
301 	kunit_print_string_stream(test, stream);
302 
303 	kunit_free_string_stream(test, stream);
304 }
305 
__kunit_abort(struct kunit * test)306 void __noreturn __kunit_abort(struct kunit *test)
307 {
308 	kunit_try_catch_throw(&test->try_catch); /* Does not return. */
309 
310 	/*
311 	 * Throw could not abort from test.
312 	 *
313 	 * XXX: we should never reach this line! As kunit_try_catch_throw is
314 	 * marked __noreturn.
315 	 */
316 	WARN_ONCE(true, "Throw could not abort from test!\n");
317 }
318 EXPORT_SYMBOL_GPL(__kunit_abort);
319 
__kunit_do_failed_assertion(struct kunit * test,const struct kunit_loc * loc,enum kunit_assert_type type,const struct kunit_assert * assert,assert_format_t assert_format,const char * fmt,...)320 void __kunit_do_failed_assertion(struct kunit *test,
321 			       const struct kunit_loc *loc,
322 			       enum kunit_assert_type type,
323 			       const struct kunit_assert *assert,
324 			       assert_format_t assert_format,
325 			       const char *fmt, ...)
326 {
327 	va_list args;
328 	struct va_format message;
329 	va_start(args, fmt);
330 
331 	message.fmt = fmt;
332 	message.va = &args;
333 
334 	kunit_fail(test, loc, type, assert, assert_format, &message);
335 
336 	va_end(args);
337 }
338 EXPORT_SYMBOL_GPL(__kunit_do_failed_assertion);
339 
kunit_init_test(struct kunit * test,const char * name,struct string_stream * log)340 void kunit_init_test(struct kunit *test, const char *name, struct string_stream *log)
341 {
342 	spin_lock_init(&test->lock);
343 	INIT_LIST_HEAD(&test->resources);
344 	test->name = name;
345 	test->log = log;
346 	if (test->log)
347 		string_stream_clear(log);
348 	test->status = KUNIT_SUCCESS;
349 	test->status_comment[0] = '\0';
350 }
351 EXPORT_SYMBOL_GPL(kunit_init_test);
352 
353 /* Only warn when a test takes more than twice the threshold */
354 #define KUNIT_SPEED_WARNING_MULTIPLIER	2
355 
356 /* Slow tests are defined as taking more than 1s */
357 #define KUNIT_SPEED_SLOW_THRESHOLD_S	1
358 
359 #define KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S	\
360 	(KUNIT_SPEED_WARNING_MULTIPLIER * KUNIT_SPEED_SLOW_THRESHOLD_S)
361 
362 #define s_to_timespec64(s) ns_to_timespec64((s) * NSEC_PER_SEC)
363 
kunit_run_case_check_speed(struct kunit * test,struct kunit_case * test_case,struct timespec64 duration)364 static void kunit_run_case_check_speed(struct kunit *test,
365 				       struct kunit_case *test_case,
366 				       struct timespec64 duration)
367 {
368 	struct timespec64 slow_thr =
369 		s_to_timespec64(KUNIT_SPEED_SLOW_WARNING_THRESHOLD_S);
370 	enum kunit_speed speed = test_case->attr.speed;
371 
372 	if (timespec64_compare(&duration, &slow_thr) < 0)
373 		return;
374 
375 	if (speed == KUNIT_SPEED_VERY_SLOW || speed == KUNIT_SPEED_SLOW)
376 		return;
377 
378 	kunit_warn(test,
379 		   "Test should be marked slow (runtime: %lld.%09lds)",
380 		   duration.tv_sec, duration.tv_nsec);
381 }
382 
383 /* Returns timeout multiplier based on speed.
384  * DEFAULT:		    1
385  * KUNIT_SPEED_SLOW:        3
386  * KUNIT_SPEED_VERY_SLOW:   12
387  */
kunit_timeout_mult(enum kunit_speed speed)388 static int kunit_timeout_mult(enum kunit_speed speed)
389 {
390 	switch (speed) {
391 	case KUNIT_SPEED_SLOW:
392 		return 3;
393 	case KUNIT_SPEED_VERY_SLOW:
394 		return 12;
395 	default:
396 		return 1;
397 	}
398 }
399 
kunit_test_timeout(struct kunit_suite * suite,struct kunit_case * test_case)400 static unsigned long kunit_test_timeout(struct kunit_suite *suite, struct kunit_case *test_case)
401 {
402 	int mult = 1;
403 
404 	/*
405 	 * The default test timeout is 300 seconds and will be adjusted by mult
406 	 * based on the test speed. The test speed will be overridden by the
407 	 * innermost test component.
408 	 */
409 	if (suite->attr.speed != KUNIT_SPEED_UNSET)
410 		mult = kunit_timeout_mult(suite->attr.speed);
411 	if (test_case->attr.speed != KUNIT_SPEED_UNSET)
412 		mult = kunit_timeout_mult(test_case->attr.speed);
413 	return mult * kunit_base_timeout * msecs_to_jiffies(MSEC_PER_SEC);
414 }
415 
416 
417 /*
418  * Initializes and runs test case. Does not clean up or do post validations.
419  */
kunit_run_case_internal(struct kunit * test,struct kunit_suite * suite,struct kunit_case * test_case)420 static void kunit_run_case_internal(struct kunit *test,
421 				    struct kunit_suite *suite,
422 				    struct kunit_case *test_case)
423 {
424 	struct timespec64 start, end;
425 
426 	if (suite->init) {
427 		int ret;
428 
429 		ret = suite->init(test);
430 		if (ret) {
431 			kunit_err(test, "failed to initialize: %d\n", ret);
432 			kunit_set_failure(test);
433 			return;
434 		}
435 	}
436 
437 	ktime_get_ts64(&start);
438 
439 	test_case->run_case(test);
440 
441 	ktime_get_ts64(&end);
442 
443 	kunit_run_case_check_speed(test, test_case, timespec64_sub(end, start));
444 }
445 
kunit_case_internal_cleanup(struct kunit * test)446 static void kunit_case_internal_cleanup(struct kunit *test)
447 {
448 	kunit_cleanup(test);
449 }
450 
451 /*
452  * Performs post validations and cleanup after a test case was run.
453  * XXX: Should ONLY BE CALLED AFTER kunit_run_case_internal!
454  */
kunit_run_case_cleanup(struct kunit * test,struct kunit_suite * suite)455 static void kunit_run_case_cleanup(struct kunit *test,
456 				   struct kunit_suite *suite)
457 {
458 	if (suite->exit)
459 		suite->exit(test);
460 
461 	kunit_case_internal_cleanup(test);
462 }
463 
464 struct kunit_try_catch_context {
465 	struct kunit *test;
466 	struct kunit_suite *suite;
467 	struct kunit_case *test_case;
468 };
469 
kunit_try_run_case(void * data)470 static void kunit_try_run_case(void *data)
471 {
472 	struct kunit_try_catch_context *ctx = data;
473 	struct kunit *test = ctx->test;
474 	struct kunit_suite *suite = ctx->suite;
475 	struct kunit_case *test_case = ctx->test_case;
476 
477 	current->kunit_test = test;
478 
479 	/*
480 	 * kunit_run_case_internal may encounter a fatal error; if it does,
481 	 * abort will be called, this thread will exit, and finally the parent
482 	 * thread will resume control and handle any necessary clean up.
483 	 */
484 	kunit_run_case_internal(test, suite, test_case);
485 }
486 
kunit_try_run_case_cleanup(void * data)487 static void kunit_try_run_case_cleanup(void *data)
488 {
489 	struct kunit_try_catch_context *ctx = data;
490 	struct kunit *test = ctx->test;
491 	struct kunit_suite *suite = ctx->suite;
492 
493 	current->kunit_test = test;
494 
495 	kunit_run_case_cleanup(test, suite);
496 }
497 
kunit_catch_run_case_cleanup(void * data)498 static void kunit_catch_run_case_cleanup(void *data)
499 {
500 	struct kunit_try_catch_context *ctx = data;
501 	struct kunit *test = ctx->test;
502 	int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
503 
504 	/* It is always a failure if cleanup aborts. */
505 	kunit_set_failure(test);
506 
507 	if (try_exit_code) {
508 		/*
509 		 * Test case could not finish, we have no idea what state it is
510 		 * in, so don't do clean up.
511 		 */
512 		if (try_exit_code == -ETIMEDOUT) {
513 			kunit_err(test, "test case cleanup timed out\n");
514 		/*
515 		 * Unknown internal error occurred preventing test case from
516 		 * running, so there is nothing to clean up.
517 		 */
518 		} else {
519 			kunit_err(test, "internal error occurred during test case cleanup: %d\n",
520 				  try_exit_code);
521 		}
522 		return;
523 	}
524 
525 	kunit_err(test, "test aborted during cleanup. continuing without cleaning up\n");
526 }
527 
528 
kunit_catch_run_case(void * data)529 static void kunit_catch_run_case(void *data)
530 {
531 	struct kunit_try_catch_context *ctx = data;
532 	struct kunit *test = ctx->test;
533 	int try_exit_code = kunit_try_catch_get_result(&test->try_catch);
534 
535 	if (try_exit_code) {
536 		kunit_set_failure(test);
537 		/*
538 		 * Test case could not finish, we have no idea what state it is
539 		 * in, so don't do clean up.
540 		 */
541 		if (try_exit_code == -ETIMEDOUT) {
542 			kunit_err(test, "test case timed out\n");
543 		/*
544 		 * Unknown internal error occurred preventing test case from
545 		 * running, so there is nothing to clean up.
546 		 */
547 		} else {
548 			kunit_err(test, "internal error occurred preventing test case from running: %d\n",
549 				  try_exit_code);
550 		}
551 		return;
552 	}
553 }
554 
555 /*
556  * Performs all logic to run a test case. It also catches most errors that
557  * occur in a test case and reports them as failures.
558  */
kunit_run_case_catch_errors(struct kunit_suite * suite,struct kunit_case * test_case,struct kunit * test)559 static void kunit_run_case_catch_errors(struct kunit_suite *suite,
560 					struct kunit_case *test_case,
561 					struct kunit *test)
562 {
563 	struct kunit_try_catch_context context;
564 	struct kunit_try_catch *try_catch;
565 
566 	try_catch = &test->try_catch;
567 
568 	kunit_try_catch_init(try_catch,
569 			     test,
570 			     kunit_try_run_case,
571 			     kunit_catch_run_case,
572 			     kunit_test_timeout(suite, test_case));
573 	context.test = test;
574 	context.suite = suite;
575 	context.test_case = test_case;
576 	kunit_try_catch_run(try_catch, &context);
577 
578 	/* Now run the cleanup */
579 	kunit_try_catch_init(try_catch,
580 			     test,
581 			     kunit_try_run_case_cleanup,
582 			     kunit_catch_run_case_cleanup,
583 			     kunit_test_timeout(suite, test_case));
584 	kunit_try_catch_run(try_catch, &context);
585 
586 	/* Propagate the parameter result to the test case. */
587 	if (test->status == KUNIT_FAILURE)
588 		test_case->status = KUNIT_FAILURE;
589 	else if (test_case->status != KUNIT_FAILURE && test->status == KUNIT_SUCCESS)
590 		test_case->status = KUNIT_SUCCESS;
591 }
592 
kunit_print_suite_stats(struct kunit_suite * suite,struct kunit_result_stats suite_stats,struct kunit_result_stats param_stats)593 static void kunit_print_suite_stats(struct kunit_suite *suite,
594 				    struct kunit_result_stats suite_stats,
595 				    struct kunit_result_stats param_stats)
596 {
597 	if (kunit_should_print_stats(suite_stats)) {
598 		kunit_log(KERN_INFO, suite,
599 			  "# %s: pass:%lu fail:%lu skip:%lu total:%lu",
600 			  suite->name,
601 			  suite_stats.passed,
602 			  suite_stats.failed,
603 			  suite_stats.skipped,
604 			  suite_stats.total);
605 	}
606 
607 	if (kunit_should_print_stats(param_stats)) {
608 		kunit_log(KERN_INFO, suite,
609 			  "# Totals: pass:%lu fail:%lu skip:%lu total:%lu",
610 			  param_stats.passed,
611 			  param_stats.failed,
612 			  param_stats.skipped,
613 			  param_stats.total);
614 	}
615 }
616 
kunit_update_stats(struct kunit_result_stats * stats,enum kunit_status status)617 static void kunit_update_stats(struct kunit_result_stats *stats,
618 			       enum kunit_status status)
619 {
620 	switch (status) {
621 	case KUNIT_SUCCESS:
622 		stats->passed++;
623 		break;
624 	case KUNIT_SKIPPED:
625 		stats->skipped++;
626 		break;
627 	case KUNIT_FAILURE:
628 		stats->failed++;
629 		break;
630 	}
631 
632 	stats->total++;
633 }
634 
kunit_accumulate_stats(struct kunit_result_stats * total,struct kunit_result_stats add)635 static void kunit_accumulate_stats(struct kunit_result_stats *total,
636 				   struct kunit_result_stats add)
637 {
638 	total->passed += add.passed;
639 	total->skipped += add.skipped;
640 	total->failed += add.failed;
641 	total->total += add.total;
642 }
643 
kunit_run_tests(struct kunit_suite * suite)644 int kunit_run_tests(struct kunit_suite *suite)
645 {
646 	char param_desc[KUNIT_PARAM_DESC_SIZE];
647 	struct kunit_case *test_case;
648 	struct kunit_result_stats suite_stats = { 0 };
649 	struct kunit_result_stats total_stats = { 0 };
650 
651 	/* Taint the kernel so we know we've run tests. */
652 	add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
653 
654 	if (suite->suite_init) {
655 		suite->suite_init_err = suite->suite_init(suite);
656 		if (suite->suite_init_err) {
657 			kunit_err(suite, KUNIT_SUBTEST_INDENT
658 				  "# failed to initialize (%d)", suite->suite_init_err);
659 			goto suite_end;
660 		}
661 	}
662 
663 	kunit_print_suite_start(suite);
664 
665 	kunit_suite_for_each_test_case(suite, test_case) {
666 		struct kunit test = { .param_value = NULL, .param_index = 0 };
667 		struct kunit_result_stats param_stats = { 0 };
668 
669 		kunit_init_test(&test, test_case->name, test_case->log);
670 		if (test_case->status == KUNIT_SKIPPED) {
671 			/* Test marked as skip */
672 			test.status = KUNIT_SKIPPED;
673 			kunit_update_stats(&param_stats, test.status);
674 		} else if (!test_case->generate_params) {
675 			/* Non-parameterised test. */
676 			test_case->status = KUNIT_SKIPPED;
677 			kunit_run_case_catch_errors(suite, test_case, &test);
678 			kunit_update_stats(&param_stats, test.status);
679 		} else {
680 			/* Get initial param. */
681 			param_desc[0] = '\0';
682 			test.param_value = test_case->generate_params(NULL, param_desc);
683 			test_case->status = KUNIT_SKIPPED;
684 			kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
685 				  "KTAP version 1\n");
686 			kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
687 				  "# Subtest: %s", test_case->name);
688 
689 			while (test.param_value) {
690 				kunit_run_case_catch_errors(suite, test_case, &test);
691 
692 				if (param_desc[0] == '\0') {
693 					snprintf(param_desc, sizeof(param_desc),
694 						 "param-%d", test.param_index);
695 				}
696 
697 				kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE_PARAM,
698 						      test.status,
699 						      test.param_index + 1,
700 						      param_desc,
701 						      test.status_comment);
702 
703 				kunit_update_stats(&param_stats, test.status);
704 
705 				/* Get next param. */
706 				param_desc[0] = '\0';
707 				test.param_value = test_case->generate_params(test.param_value, param_desc);
708 				test.param_index++;
709 				test.status = KUNIT_SUCCESS;
710 				test.status_comment[0] = '\0';
711 				test.priv = NULL;
712 			}
713 		}
714 
715 		kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
716 
717 		kunit_print_test_stats(&test, param_stats);
718 
719 		kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE, test_case->status,
720 				      kunit_test_case_num(suite, test_case),
721 				      test_case->name,
722 				      test.status_comment);
723 
724 		kunit_update_stats(&suite_stats, test_case->status);
725 		kunit_accumulate_stats(&total_stats, param_stats);
726 	}
727 
728 	if (suite->suite_exit)
729 		suite->suite_exit(suite);
730 
731 	kunit_print_suite_stats(suite, suite_stats, total_stats);
732 suite_end:
733 	kunit_print_suite_end(suite);
734 
735 	return 0;
736 }
737 EXPORT_SYMBOL_GPL(kunit_run_tests);
738 
kunit_init_suite(struct kunit_suite * suite)739 static void kunit_init_suite(struct kunit_suite *suite)
740 {
741 	kunit_debugfs_create_suite(suite);
742 	suite->status_comment[0] = '\0';
743 	suite->suite_init_err = 0;
744 
745 	if (suite->log)
746 		string_stream_clear(suite->log);
747 }
748 
kunit_enabled(void)749 bool kunit_enabled(void)
750 {
751 	return enable_param;
752 }
753 
__kunit_test_suites_init(struct kunit_suite * const * const suites,int num_suites,bool run_tests)754 int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_suites,
755 			     bool run_tests)
756 {
757 	unsigned int i;
758 
759 	if (num_suites == 0)
760 		return 0;
761 
762 	if (!kunit_enabled() && num_suites > 0) {
763 		pr_info("kunit: disabled\n");
764 		return 0;
765 	}
766 
767 	kunit_suite_counter = 1;
768 
769 	/* Use mutex lock to guard against running tests concurrently. */
770 	if (mutex_lock_interruptible(&kunit_run_lock)) {
771 		pr_err("kunit: test interrupted\n");
772 		return -EINTR;
773 	}
774 	static_branch_inc(&kunit_running);
775 
776 	for (i = 0; i < num_suites; i++) {
777 		kunit_init_suite(suites[i]);
778 		if (run_tests)
779 			kunit_run_tests(suites[i]);
780 	}
781 
782 	static_branch_dec(&kunit_running);
783 	mutex_unlock(&kunit_run_lock);
784 	return 0;
785 }
786 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);
787 
kunit_exit_suite(struct kunit_suite * suite)788 static void kunit_exit_suite(struct kunit_suite *suite)
789 {
790 	kunit_debugfs_destroy_suite(suite);
791 }
792 
__kunit_test_suites_exit(struct kunit_suite ** suites,int num_suites)793 void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
794 {
795 	unsigned int i;
796 
797 	if (!kunit_enabled())
798 		return;
799 
800 	for (i = 0; i < num_suites; i++)
801 		kunit_exit_suite(suites[i]);
802 }
803 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
804 
805 #ifdef CONFIG_MODULES
kunit_module_init(struct module * mod)806 static void kunit_module_init(struct module *mod)
807 {
808 	struct kunit_suite_set suite_set, filtered_set;
809 	struct kunit_suite_set normal_suite_set = {
810 		mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
811 	};
812 	struct kunit_suite_set init_suite_set = {
813 		mod->kunit_init_suites, mod->kunit_init_suites + mod->num_kunit_init_suites,
814 	};
815 	const char *action = kunit_action();
816 	int err = 0;
817 
818 	if (mod->num_kunit_init_suites > 0)
819 		suite_set = kunit_merge_suite_sets(init_suite_set, normal_suite_set);
820 	else
821 		suite_set = normal_suite_set;
822 
823 	filtered_set = kunit_filter_suites(&suite_set,
824 					kunit_filter_glob() ?: "*.*",
825 					kunit_filter(), kunit_filter_action(),
826 					&err);
827 	if (err)
828 		pr_err("kunit module: error filtering suites: %d\n", err);
829 
830 	mod->kunit_suites = (struct kunit_suite **)filtered_set.start;
831 	mod->num_kunit_suites = filtered_set.end - filtered_set.start;
832 
833 	if (mod->num_kunit_init_suites > 0)
834 		kfree(suite_set.start);
835 
836 	if (!action)
837 		kunit_exec_run_tests(&filtered_set, false);
838 	else if (!strcmp(action, "list"))
839 		kunit_exec_list_tests(&filtered_set, false);
840 	else if (!strcmp(action, "list_attr"))
841 		kunit_exec_list_tests(&filtered_set, true);
842 	else
843 		pr_err("kunit: unknown action '%s'\n", action);
844 }
845 
kunit_module_exit(struct module * mod)846 static void kunit_module_exit(struct module *mod)
847 {
848 	struct kunit_suite_set suite_set = {
849 		mod->kunit_suites, mod->kunit_suites + mod->num_kunit_suites,
850 	};
851 	const char *action = kunit_action();
852 
853 	/*
854 	 * Check if the start address is a valid virtual address to detect
855 	 * if the module load sequence has failed and the suite set has not
856 	 * been initialized and filtered.
857 	 */
858 	if (!suite_set.start || !virt_addr_valid(suite_set.start))
859 		return;
860 
861 	if (!action)
862 		__kunit_test_suites_exit(mod->kunit_suites,
863 					 mod->num_kunit_suites);
864 
865 	kunit_free_suite_set(suite_set);
866 }
867 
kunit_module_notify(struct notifier_block * nb,unsigned long val,void * data)868 static int kunit_module_notify(struct notifier_block *nb, unsigned long val,
869 			       void *data)
870 {
871 	struct module *mod = data;
872 
873 	switch (val) {
874 	case MODULE_STATE_LIVE:
875 		kunit_module_init(mod);
876 		break;
877 	case MODULE_STATE_GOING:
878 		kunit_module_exit(mod);
879 		break;
880 	case MODULE_STATE_COMING:
881 		break;
882 	case MODULE_STATE_UNFORMED:
883 		break;
884 	}
885 
886 	return 0;
887 }
888 
889 static struct notifier_block kunit_mod_nb = {
890 	.notifier_call = kunit_module_notify,
891 	.priority = 0,
892 };
893 #endif
894 
KUNIT_DEFINE_ACTION_WRAPPER(kfree_action_wrapper,kfree,const void *)895 KUNIT_DEFINE_ACTION_WRAPPER(kfree_action_wrapper, kfree, const void *)
896 
897 void *kunit_kmalloc_array(struct kunit *test, size_t n, size_t size, gfp_t gfp)
898 {
899 	void *data;
900 
901 	data = kmalloc_array(n, size, gfp);
902 
903 	if (!data)
904 		return NULL;
905 
906 	if (kunit_add_action_or_reset(test, kfree_action_wrapper, data) != 0)
907 		return NULL;
908 
909 	return data;
910 }
911 EXPORT_SYMBOL_GPL(kunit_kmalloc_array);
912 
kunit_kfree(struct kunit * test,const void * ptr)913 void kunit_kfree(struct kunit *test, const void *ptr)
914 {
915 	if (!ptr)
916 		return;
917 
918 	kunit_release_action(test, kfree_action_wrapper, (void *)ptr);
919 }
920 EXPORT_SYMBOL_GPL(kunit_kfree);
921 
kunit_kfree_const(struct kunit * test,const void * x)922 void kunit_kfree_const(struct kunit *test, const void *x)
923 {
924 #if !IS_MODULE(CONFIG_KUNIT)
925 	if (!is_kernel_rodata((unsigned long)x))
926 #endif
927 		kunit_kfree(test, x);
928 }
929 EXPORT_SYMBOL_GPL(kunit_kfree_const);
930 
kunit_kstrdup_const(struct kunit * test,const char * str,gfp_t gfp)931 const char *kunit_kstrdup_const(struct kunit *test, const char *str, gfp_t gfp)
932 {
933 #if !IS_MODULE(CONFIG_KUNIT)
934 	if (is_kernel_rodata((unsigned long)str))
935 		return str;
936 #endif
937 	return kunit_kstrdup(test, str, gfp);
938 }
939 EXPORT_SYMBOL_GPL(kunit_kstrdup_const);
940 
kunit_cleanup(struct kunit * test)941 void kunit_cleanup(struct kunit *test)
942 {
943 	struct kunit_resource *res;
944 	unsigned long flags;
945 
946 	/*
947 	 * test->resources is a stack - each allocation must be freed in the
948 	 * reverse order from which it was added since one resource may depend
949 	 * on another for its entire lifetime.
950 	 * Also, we cannot use the normal list_for_each constructs, even the
951 	 * safe ones because *arbitrary* nodes may be deleted when
952 	 * kunit_resource_free is called; the list_for_each_safe variants only
953 	 * protect against the current node being deleted, not the next.
954 	 */
955 	while (true) {
956 		spin_lock_irqsave(&test->lock, flags);
957 		if (list_empty(&test->resources)) {
958 			spin_unlock_irqrestore(&test->lock, flags);
959 			break;
960 		}
961 		res = list_last_entry(&test->resources,
962 				      struct kunit_resource,
963 				      node);
964 		/*
965 		 * Need to unlock here as a resource may remove another
966 		 * resource, and this can't happen if the test->lock
967 		 * is held.
968 		 */
969 		spin_unlock_irqrestore(&test->lock, flags);
970 		kunit_remove_resource(test, res);
971 	}
972 	current->kunit_test = NULL;
973 }
974 EXPORT_SYMBOL_GPL(kunit_cleanup);
975 
kunit_init(void)976 static int __init kunit_init(void)
977 {
978 	/* Install the KUnit hook functions. */
979 	kunit_install_hooks();
980 
981 	kunit_debugfs_init();
982 
983 	kunit_bus_init();
984 #ifdef CONFIG_MODULES
985 	return register_module_notifier(&kunit_mod_nb);
986 #else
987 	return 0;
988 #endif
989 }
990 late_initcall(kunit_init);
991 
kunit_exit(void)992 static void __exit kunit_exit(void)
993 {
994 	memset(&kunit_hooks, 0, sizeof(kunit_hooks));
995 #ifdef CONFIG_MODULES
996 	unregister_module_notifier(&kunit_mod_nb);
997 #endif
998 
999 	kunit_bus_shutdown();
1000 
1001 	kunit_debugfs_cleanup();
1002 }
1003 module_exit(kunit_exit);
1004 
1005 MODULE_DESCRIPTION("Base unit test (KUnit) API");
1006 MODULE_LICENSE("GPL v2");
1007