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 18 void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch) 19 { 20 try_catch->try_result = -EFAULT; 21 kthread_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 kthread_complete_and_exit(try_catch->try_completion, 0); 32 } 33 34 static unsigned long kunit_test_timeout(void) 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 tests timeout due to exceeding sysctl_hung_task_timeout_secs, 54 * the task will be killed and an oops generated. 55 */ 56 return 300 * msecs_to_jiffies(MSEC_PER_SEC); /* 5 min */ 57 } 58 59 void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) 60 { 61 DECLARE_COMPLETION_ONSTACK(try_completion); 62 struct kunit *test = try_catch->test; 63 struct task_struct *task_struct; 64 int exit_code, time_remaining; 65 66 try_catch->context = context; 67 try_catch->try_completion = &try_completion; 68 try_catch->try_result = 0; 69 task_struct = kthread_create(kunit_generic_run_threadfn_adapter, 70 try_catch, "kunit_try_catch_thread"); 71 if (IS_ERR(task_struct)) { 72 try_catch->catch(try_catch->context); 73 return; 74 } 75 get_task_struct(task_struct); 76 wake_up_process(task_struct); 77 78 time_remaining = wait_for_completion_timeout(&try_completion, 79 kunit_test_timeout()); 80 if (time_remaining == 0) { 81 kunit_err(test, "try timed out\n"); 82 try_catch->try_result = -ETIMEDOUT; 83 kthread_stop(task_struct); 84 } 85 86 put_task_struct(task_struct); 87 exit_code = try_catch->try_result; 88 89 if (!exit_code) 90 return; 91 92 if (exit_code == -EFAULT) 93 try_catch->try_result = 0; 94 else if (exit_code == -EINTR) 95 kunit_err(test, "wake_up_process() was never called\n"); 96 else if (exit_code) 97 kunit_err(test, "Unknown error: %d\n", exit_code); 98 99 try_catch->catch(try_catch->context); 100 } 101 EXPORT_SYMBOL_GPL(kunit_try_catch_run); 102