xref: /openbmc/linux/lib/kunit/try-catch.c (revision ee1cd5048959de496cd005c50b137212a5b62062)
15f3e0620SBrendan Higgins // SPDX-License-Identifier: GPL-2.0
25f3e0620SBrendan Higgins /*
35f3e0620SBrendan Higgins  * An API to allow a function, that may fail, to be executed, and recover in a
45f3e0620SBrendan Higgins  * controlled manner.
55f3e0620SBrendan Higgins  *
65f3e0620SBrendan Higgins  * Copyright (C) 2019, Google LLC.
75f3e0620SBrendan Higgins  * Author: Brendan Higgins <brendanhiggins@google.com>
85f3e0620SBrendan Higgins  */
95f3e0620SBrendan Higgins 
105f3e0620SBrendan Higgins #include <kunit/test.h>
115f3e0620SBrendan Higgins #include <linux/completion.h>
125f3e0620SBrendan Higgins #include <linux/kernel.h>
135f3e0620SBrendan Higgins #include <linux/kthread.h>
141ec7ccb4SMickaël Salaün #include <linux/sched/task.h>
155f3e0620SBrendan Higgins 
169bbb11c6SAlan Maguire #include "try-catch-impl.h"
179bbb11c6SAlan Maguire 
kunit_try_catch_throw(struct kunit_try_catch * try_catch)185f3e0620SBrendan Higgins void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)
195f3e0620SBrendan Higgins {
205f3e0620SBrendan Higgins 	try_catch->try_result = -EFAULT;
21cead1855SEric W. Biederman 	kthread_complete_and_exit(try_catch->try_completion, -EFAULT);
225f3e0620SBrendan Higgins }
23c475c77dSAlan Maguire EXPORT_SYMBOL_GPL(kunit_try_catch_throw);
245f3e0620SBrendan Higgins 
kunit_generic_run_threadfn_adapter(void * data)255f3e0620SBrendan Higgins static int kunit_generic_run_threadfn_adapter(void *data)
265f3e0620SBrendan Higgins {
275f3e0620SBrendan Higgins 	struct kunit_try_catch *try_catch = data;
285f3e0620SBrendan Higgins 
295f3e0620SBrendan Higgins 	try_catch->try(try_catch->context);
305f3e0620SBrendan Higgins 
31cead1855SEric W. Biederman 	kthread_complete_and_exit(try_catch->try_completion, 0);
325f3e0620SBrendan Higgins }
335f3e0620SBrendan Higgins 
kunit_test_timeout(void)345f3e0620SBrendan Higgins static unsigned long kunit_test_timeout(void)
355f3e0620SBrendan Higgins {
365f3e0620SBrendan Higgins 	/*
375f3e0620SBrendan Higgins 	 * TODO(brendanhiggins@google.com): We should probably have some type of
385f3e0620SBrendan Higgins 	 * variable timeout here. The only question is what that timeout value
395f3e0620SBrendan Higgins 	 * should be.
405f3e0620SBrendan Higgins 	 *
415f3e0620SBrendan Higgins 	 * The intention has always been, at some point, to be able to label
425f3e0620SBrendan Higgins 	 * tests with some type of size bucket (unit/small, integration/medium,
435f3e0620SBrendan Higgins 	 * large/system/end-to-end, etc), where each size bucket would get a
445f3e0620SBrendan Higgins 	 * default timeout value kind of like what Bazel does:
455f3e0620SBrendan Higgins 	 * https://docs.bazel.build/versions/master/be/common-definitions.html#test.size
465f3e0620SBrendan Higgins 	 * There is still some debate to be had on exactly how we do this. (For
475f3e0620SBrendan Higgins 	 * one, we probably want to have some sort of test runner level
485f3e0620SBrendan Higgins 	 * timeout.)
495f3e0620SBrendan Higgins 	 *
505f3e0620SBrendan Higgins 	 * For more background on this topic, see:
515f3e0620SBrendan Higgins 	 * https://mike-bland.com/2011/11/01/small-medium-large.html
525f3e0620SBrendan Higgins 	 *
531c024d45SAlan Maguire 	 * If tests timeout due to exceeding sysctl_hung_task_timeout_secs,
541c024d45SAlan Maguire 	 * the task will be killed and an oops generated.
555f3e0620SBrendan Higgins 	 */
56bdd015f7SPeng Liu 	return 300 * msecs_to_jiffies(MSEC_PER_SEC); /* 5 min */
575f3e0620SBrendan Higgins }
585f3e0620SBrendan Higgins 
kunit_try_catch_run(struct kunit_try_catch * try_catch,void * context)595f3e0620SBrendan Higgins void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
605f3e0620SBrendan Higgins {
615f3e0620SBrendan Higgins 	DECLARE_COMPLETION_ONSTACK(try_completion);
625f3e0620SBrendan Higgins 	struct kunit *test = try_catch->test;
635f3e0620SBrendan Higgins 	struct task_struct *task_struct;
645f3e0620SBrendan Higgins 	int exit_code, time_remaining;
655f3e0620SBrendan Higgins 
665f3e0620SBrendan Higgins 	try_catch->context = context;
675f3e0620SBrendan Higgins 	try_catch->try_completion = &try_completion;
685f3e0620SBrendan Higgins 	try_catch->try_result = 0;
691ec7ccb4SMickaël Salaün 	task_struct = kthread_create(kunit_generic_run_threadfn_adapter,
701ec7ccb4SMickaël Salaün 				     try_catch, "kunit_try_catch_thread");
715f3e0620SBrendan Higgins 	if (IS_ERR(task_struct)) {
725f3e0620SBrendan Higgins 		try_catch->catch(try_catch->context);
735f3e0620SBrendan Higgins 		return;
745f3e0620SBrendan Higgins 	}
751ec7ccb4SMickaël Salaün 	get_task_struct(task_struct);
761ec7ccb4SMickaël Salaün 	wake_up_process(task_struct);
775f3e0620SBrendan Higgins 
785f3e0620SBrendan Higgins 	time_remaining = wait_for_completion_timeout(&try_completion,
795f3e0620SBrendan Higgins 						     kunit_test_timeout());
805f3e0620SBrendan Higgins 	if (time_remaining == 0) {
815f3e0620SBrendan Higgins 		try_catch->try_result = -ETIMEDOUT;
82adf50545SPeng Liu 		kthread_stop(task_struct);
835f3e0620SBrendan Higgins 	}
845f3e0620SBrendan Higgins 
851ec7ccb4SMickaël Salaün 	put_task_struct(task_struct);
865f3e0620SBrendan Higgins 	exit_code = try_catch->try_result;
875f3e0620SBrendan Higgins 
885f3e0620SBrendan Higgins 	if (!exit_code)
895f3e0620SBrendan Higgins 		return;
905f3e0620SBrendan Higgins 
915f3e0620SBrendan Higgins 	if (exit_code == -EFAULT)
925f3e0620SBrendan Higgins 		try_catch->try_result = 0;
935f3e0620SBrendan Higgins 	else if (exit_code == -EINTR)
945f3e0620SBrendan Higgins 		kunit_err(test, "wake_up_process() was never called\n");
95*4e40bc50SMickaël Salaün 	else if (exit_code == -ETIMEDOUT)
96*4e40bc50SMickaël Salaün 		kunit_err(test, "try timed out\n");
975f3e0620SBrendan Higgins 	else if (exit_code)
985f3e0620SBrendan Higgins 		kunit_err(test, "Unknown error: %d\n", exit_code);
995f3e0620SBrendan Higgins 
1005f3e0620SBrendan Higgins 	try_catch->catch(try_catch->context);
1015f3e0620SBrendan Higgins }
102c475c77dSAlan Maguire EXPORT_SYMBOL_GPL(kunit_try_catch_run);
103