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