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