]> asedeno.scripts.mit.edu Git - linux.git/blob - lib/kunit/try-catch.c
kunit: allow kunit tests to be loaded as a module
[linux.git] / lib / kunit / try-catch.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * An API to allow a function, that may fail, to be executed, and recover in a
4  * controlled manner.
5  *
6  * Copyright (C) 2019, Google LLC.
7  * Author: Brendan Higgins <brendanhiggins@google.com>
8  */
9
10 #include <kunit/test.h>
11 #include <linux/completion.h>
12 #include <linux/kernel.h>
13 #include <linux/kthread.h>
14 #include <linux/sched/sysctl.h>
15
16 #include "try-catch-impl.h"
17
18 void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)
19 {
20         try_catch->try_result = -EFAULT;
21         complete_and_exit(try_catch->try_completion, -EFAULT);
22 }
23 EXPORT_SYMBOL_GPL(kunit_try_catch_throw);
24
25 static int kunit_generic_run_threadfn_adapter(void *data)
26 {
27         struct kunit_try_catch *try_catch = data;
28
29         try_catch->try(try_catch->context);
30
31         complete_and_exit(try_catch->try_completion, 0);
32 }
33
34 static unsigned long kunit_test_timeout(void)
35 {
36         unsigned long timeout_msecs;
37
38         /*
39          * TODO(brendanhiggins@google.com): We should probably have some type of
40          * variable timeout here. The only question is what that timeout value
41          * should be.
42          *
43          * The intention has always been, at some point, to be able to label
44          * tests with some type of size bucket (unit/small, integration/medium,
45          * large/system/end-to-end, etc), where each size bucket would get a
46          * default timeout value kind of like what Bazel does:
47          * https://docs.bazel.build/versions/master/be/common-definitions.html#test.size
48          * There is still some debate to be had on exactly how we do this. (For
49          * one, we probably want to have some sort of test runner level
50          * timeout.)
51          *
52          * For more background on this topic, see:
53          * https://mike-bland.com/2011/11/01/small-medium-large.html
54          */
55         if (sysctl_hung_task_timeout_secs) {
56                 /*
57                  * If sysctl_hung_task is active, just set the timeout to some
58                  * value less than that.
59                  *
60                  * In regards to the above TODO, if we decide on variable
61                  * timeouts, this logic will likely need to change.
62                  */
63                 timeout_msecs = (sysctl_hung_task_timeout_secs - 1) *
64                                 MSEC_PER_SEC;
65         } else {
66                 timeout_msecs = 300 * MSEC_PER_SEC; /* 5 min */
67         }
68
69         return timeout_msecs;
70 }
71
72 void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
73 {
74         DECLARE_COMPLETION_ONSTACK(try_completion);
75         struct kunit *test = try_catch->test;
76         struct task_struct *task_struct;
77         int exit_code, time_remaining;
78
79         try_catch->context = context;
80         try_catch->try_completion = &try_completion;
81         try_catch->try_result = 0;
82         task_struct = kthread_run(kunit_generic_run_threadfn_adapter,
83                                   try_catch,
84                                   "kunit_try_catch_thread");
85         if (IS_ERR(task_struct)) {
86                 try_catch->catch(try_catch->context);
87                 return;
88         }
89
90         time_remaining = wait_for_completion_timeout(&try_completion,
91                                                      kunit_test_timeout());
92         if (time_remaining == 0) {
93                 kunit_err(test, "try timed out\n");
94                 try_catch->try_result = -ETIMEDOUT;
95         }
96
97         exit_code = try_catch->try_result;
98
99         if (!exit_code)
100                 return;
101
102         if (exit_code == -EFAULT)
103                 try_catch->try_result = 0;
104         else if (exit_code == -EINTR)
105                 kunit_err(test, "wake_up_process() was never called\n");
106         else if (exit_code)
107                 kunit_err(test, "Unknown error: %d\n", exit_code);
108
109         try_catch->catch(try_catch->context);
110 }
111 EXPORT_SYMBOL_GPL(kunit_try_catch_run);