18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * An API to allow a function, that may fail, to be executed, and recover in a 48c2ecf20Sopenharmony_ci * controlled manner. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2019, Google LLC. 78c2ecf20Sopenharmony_ci * Author: Brendan Higgins <brendanhiggins@google.com> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <kunit/test.h> 118c2ecf20Sopenharmony_ci#include <linux/completion.h> 128c2ecf20Sopenharmony_ci#include <linux/kernel.h> 138c2ecf20Sopenharmony_ci#include <linux/kthread.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include "try-catch-impl.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_civoid __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch) 188c2ecf20Sopenharmony_ci{ 198c2ecf20Sopenharmony_ci try_catch->try_result = -EFAULT; 208c2ecf20Sopenharmony_ci complete_and_exit(try_catch->try_completion, -EFAULT); 218c2ecf20Sopenharmony_ci} 228c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_try_catch_throw); 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic int kunit_generic_run_threadfn_adapter(void *data) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci struct kunit_try_catch *try_catch = data; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci try_catch->try(try_catch->context); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci complete_and_exit(try_catch->try_completion, 0); 318c2ecf20Sopenharmony_ci} 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic unsigned long kunit_test_timeout(void) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci /* 368c2ecf20Sopenharmony_ci * TODO(brendanhiggins@google.com): We should probably have some type of 378c2ecf20Sopenharmony_ci * variable timeout here. The only question is what that timeout value 388c2ecf20Sopenharmony_ci * should be. 398c2ecf20Sopenharmony_ci * 408c2ecf20Sopenharmony_ci * The intention has always been, at some point, to be able to label 418c2ecf20Sopenharmony_ci * tests with some type of size bucket (unit/small, integration/medium, 428c2ecf20Sopenharmony_ci * large/system/end-to-end, etc), where each size bucket would get a 438c2ecf20Sopenharmony_ci * default timeout value kind of like what Bazel does: 448c2ecf20Sopenharmony_ci * https://docs.bazel.build/versions/master/be/common-definitions.html#test.size 458c2ecf20Sopenharmony_ci * There is still some debate to be had on exactly how we do this. (For 468c2ecf20Sopenharmony_ci * one, we probably want to have some sort of test runner level 478c2ecf20Sopenharmony_ci * timeout.) 488c2ecf20Sopenharmony_ci * 498c2ecf20Sopenharmony_ci * For more background on this topic, see: 508c2ecf20Sopenharmony_ci * https://mike-bland.com/2011/11/01/small-medium-large.html 518c2ecf20Sopenharmony_ci * 528c2ecf20Sopenharmony_ci * If tests timeout due to exceeding sysctl_hung_task_timeout_secs, 538c2ecf20Sopenharmony_ci * the task will be killed and an oops generated. 548c2ecf20Sopenharmony_ci */ 558c2ecf20Sopenharmony_ci return 300 * msecs_to_jiffies(MSEC_PER_SEC); /* 5 min */ 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_civoid kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci DECLARE_COMPLETION_ONSTACK(try_completion); 618c2ecf20Sopenharmony_ci struct kunit *test = try_catch->test; 628c2ecf20Sopenharmony_ci struct task_struct *task_struct; 638c2ecf20Sopenharmony_ci int exit_code, time_remaining; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci try_catch->context = context; 668c2ecf20Sopenharmony_ci try_catch->try_completion = &try_completion; 678c2ecf20Sopenharmony_ci try_catch->try_result = 0; 688c2ecf20Sopenharmony_ci task_struct = kthread_run(kunit_generic_run_threadfn_adapter, 698c2ecf20Sopenharmony_ci try_catch, 708c2ecf20Sopenharmony_ci "kunit_try_catch_thread"); 718c2ecf20Sopenharmony_ci if (IS_ERR(task_struct)) { 728c2ecf20Sopenharmony_ci try_catch->catch(try_catch->context); 738c2ecf20Sopenharmony_ci return; 748c2ecf20Sopenharmony_ci } 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci time_remaining = wait_for_completion_timeout(&try_completion, 778c2ecf20Sopenharmony_ci kunit_test_timeout()); 788c2ecf20Sopenharmony_ci if (time_remaining == 0) { 798c2ecf20Sopenharmony_ci kunit_err(test, "try timed out\n"); 808c2ecf20Sopenharmony_ci try_catch->try_result = -ETIMEDOUT; 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci exit_code = try_catch->try_result; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (!exit_code) 868c2ecf20Sopenharmony_ci return; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci if (exit_code == -EFAULT) 898c2ecf20Sopenharmony_ci try_catch->try_result = 0; 908c2ecf20Sopenharmony_ci else if (exit_code == -EINTR) 918c2ecf20Sopenharmony_ci kunit_err(test, "wake_up_process() was never called\n"); 928c2ecf20Sopenharmony_ci else if (exit_code) 938c2ecf20Sopenharmony_ci kunit_err(test, "Unknown error: %d\n", exit_code); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci try_catch->catch(try_catch->context); 968c2ecf20Sopenharmony_ci} 978c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(kunit_try_catch_run); 98