Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * An API to allow a function, that may fail, to be executed, and recover in a
0004  * controlled manner.
0005  *
0006  * Copyright (C) 2019, Google LLC.
0007  * Author: Brendan Higgins <brendanhiggins@google.com>
0008  */
0009 
0010 #include <kunit/test.h>
0011 #include <linux/completion.h>
0012 #include <linux/kernel.h>
0013 #include <linux/kthread.h>
0014 
0015 #include "try-catch-impl.h"
0016 
0017 void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)
0018 {
0019     try_catch->try_result = -EFAULT;
0020     kthread_complete_and_exit(try_catch->try_completion, -EFAULT);
0021 }
0022 EXPORT_SYMBOL_GPL(kunit_try_catch_throw);
0023 
0024 static int kunit_generic_run_threadfn_adapter(void *data)
0025 {
0026     struct kunit_try_catch *try_catch = data;
0027 
0028     try_catch->try(try_catch->context);
0029 
0030     kthread_complete_and_exit(try_catch->try_completion, 0);
0031 }
0032 
0033 static unsigned long kunit_test_timeout(void)
0034 {
0035     /*
0036      * TODO(brendanhiggins@google.com): We should probably have some type of
0037      * variable timeout here. The only question is what that timeout value
0038      * should be.
0039      *
0040      * The intention has always been, at some point, to be able to label
0041      * tests with some type of size bucket (unit/small, integration/medium,
0042      * large/system/end-to-end, etc), where each size bucket would get a
0043      * default timeout value kind of like what Bazel does:
0044      * https://docs.bazel.build/versions/master/be/common-definitions.html#test.size
0045      * There is still some debate to be had on exactly how we do this. (For
0046      * one, we probably want to have some sort of test runner level
0047      * timeout.)
0048      *
0049      * For more background on this topic, see:
0050      * https://mike-bland.com/2011/11/01/small-medium-large.html
0051      *
0052      * If tests timeout due to exceeding sysctl_hung_task_timeout_secs,
0053      * the task will be killed and an oops generated.
0054      */
0055     return 300 * msecs_to_jiffies(MSEC_PER_SEC); /* 5 min */
0056 }
0057 
0058 void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
0059 {
0060     DECLARE_COMPLETION_ONSTACK(try_completion);
0061     struct kunit *test = try_catch->test;
0062     struct task_struct *task_struct;
0063     int exit_code, time_remaining;
0064 
0065     try_catch->context = context;
0066     try_catch->try_completion = &try_completion;
0067     try_catch->try_result = 0;
0068     task_struct = kthread_run(kunit_generic_run_threadfn_adapter,
0069                   try_catch,
0070                   "kunit_try_catch_thread");
0071     if (IS_ERR(task_struct)) {
0072         try_catch->catch(try_catch->context);
0073         return;
0074     }
0075 
0076     time_remaining = wait_for_completion_timeout(&try_completion,
0077                              kunit_test_timeout());
0078     if (time_remaining == 0) {
0079         kunit_err(test, "try timed out\n");
0080         try_catch->try_result = -ETIMEDOUT;
0081         kthread_stop(task_struct);
0082     }
0083 
0084     exit_code = try_catch->try_result;
0085 
0086     if (!exit_code)
0087         return;
0088 
0089     if (exit_code == -EFAULT)
0090         try_catch->try_result = 0;
0091     else if (exit_code == -EINTR)
0092         kunit_err(test, "wake_up_process() was never called\n");
0093     else if (exit_code)
0094         kunit_err(test, "Unknown error: %d\n", exit_code);
0095 
0096     try_catch->catch(try_catch->context);
0097 }
0098 EXPORT_SYMBOL_GPL(kunit_try_catch_run);