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