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