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