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