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