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