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/task.h>
15
16 #include "try-catch-impl.h"
17
kunit_try_catch_throw(struct kunit_try_catch * try_catch)18 void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)
19 {
20 try_catch->try_result = -EFAULT;
21 kthread_exit(0);
22 }
23 EXPORT_SYMBOL_GPL(kunit_try_catch_throw);
24
kunit_generic_run_threadfn_adapter(void * data)25 static int kunit_generic_run_threadfn_adapter(void *data)
26 {
27 struct kunit_try_catch *try_catch = data;
28
29 try_catch->try_result = -EINTR;
30 try_catch->try(try_catch->context);
31 if (try_catch->try_result == -EINTR)
32 try_catch->try_result = 0;
33
34 return 0;
35 }
36
kunit_test_timeout(void)37 static unsigned long kunit_test_timeout(void)
38 {
39 /*
40 * TODO(brendanhiggins@google.com): We should probably have some type of
41 * variable timeout here. The only question is what that timeout value
42 * should be.
43 *
44 * The intention has always been, at some point, to be able to label
45 * tests with some type of size bucket (unit/small, integration/medium,
46 * large/system/end-to-end, etc), where each size bucket would get a
47 * default timeout value kind of like what Bazel does:
48 * https://docs.bazel.build/versions/master/be/common-definitions.html#test.size
49 * There is still some debate to be had on exactly how we do this. (For
50 * one, we probably want to have some sort of test runner level
51 * timeout.)
52 *
53 * For more background on this topic, see:
54 * https://mike-bland.com/2011/11/01/small-medium-large.html
55 *
56 * If tests timeout due to exceeding sysctl_hung_task_timeout_secs,
57 * the task will be killed and an oops generated.
58 */
59 return 300 * msecs_to_jiffies(MSEC_PER_SEC); /* 5 min */
60 }
61
kunit_try_catch_run(struct kunit_try_catch * try_catch,void * context)62 void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
63 {
64 struct kunit *test = try_catch->test;
65 struct task_struct *task_struct;
66 struct completion *task_done;
67 int exit_code, time_remaining;
68
69 try_catch->context = context;
70 try_catch->try_result = 0;
71 task_struct = kthread_create(kunit_generic_run_threadfn_adapter,
72 try_catch, "kunit_try_catch_thread");
73 if (IS_ERR(task_struct)) {
74 try_catch->try_result = PTR_ERR(task_struct);
75 try_catch->catch(try_catch->context);
76 return;
77 }
78 get_task_struct(task_struct);
79 /*
80 * As for a vfork(2), task_struct->vfork_done (pointing to the
81 * underlying kthread->exited) can be used to wait for the end of a
82 * kernel thread. It is set to NULL when the thread exits, so we
83 * keep a copy here.
84 */
85 task_done = task_struct->vfork_done;
86 wake_up_process(task_struct);
87
88 time_remaining = wait_for_completion_timeout(task_done,
89 kunit_test_timeout());
90 if (time_remaining == 0) {
91 try_catch->try_result = -ETIMEDOUT;
92 kthread_stop(task_struct);
93 }
94
95 put_task_struct(task_struct);
96 exit_code = try_catch->try_result;
97
98 if (!exit_code)
99 return;
100
101 if (exit_code == -EFAULT)
102 try_catch->try_result = 0;
103 else if (exit_code == -EINTR) {
104 if (test->last_seen.file)
105 kunit_err(test, "try faulted: last line seen %s:%d\n",
106 test->last_seen.file, test->last_seen.line);
107 else
108 kunit_err(test, "try faulted\n");
109 } else if (exit_code == -ETIMEDOUT)
110 kunit_err(test, "try timed out\n");
111 else if (exit_code)
112 kunit_err(test, "Unknown error: %d\n", exit_code);
113
114 try_catch->catch(try_catch->context);
115 }
116 EXPORT_SYMBOL_GPL(kunit_try_catch_run);
117