xref: /linux-6.15/lib/kunit/try-catch.c (revision 1eb69ded)
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 <[email protected]>
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>
14f8aa1b98SMickaë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;
213a35c130SMickaël Salaün 	kthread_exit(0);
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 
293a35c130SMickaël Salaün 	try_catch->try_result = -EINTR;
305f3e0620SBrendan Higgins 	try_catch->try(try_catch->context);
313a35c130SMickaël Salaün 	if (try_catch->try_result == -EINTR)
323a35c130SMickaël Salaün 		try_catch->try_result = 0;
335f3e0620SBrendan Higgins 
343a35c130SMickaël Salaün 	return 0;
355f3e0620SBrendan Higgins }
365f3e0620SBrendan Higgins 
kunit_test_timeout(void)375f3e0620SBrendan Higgins static unsigned long kunit_test_timeout(void)
385f3e0620SBrendan Higgins {
395f3e0620SBrendan Higgins 	/*
405f3e0620SBrendan Higgins 	 * TODO([email protected]): We should probably have some type of
415f3e0620SBrendan Higgins 	 * variable timeout here. The only question is what that timeout value
425f3e0620SBrendan Higgins 	 * should be.
435f3e0620SBrendan Higgins 	 *
445f3e0620SBrendan Higgins 	 * The intention has always been, at some point, to be able to label
455f3e0620SBrendan Higgins 	 * tests with some type of size bucket (unit/small, integration/medium,
465f3e0620SBrendan Higgins 	 * large/system/end-to-end, etc), where each size bucket would get a
475f3e0620SBrendan Higgins 	 * default timeout value kind of like what Bazel does:
485f3e0620SBrendan Higgins 	 * https://docs.bazel.build/versions/master/be/common-definitions.html#test.size
495f3e0620SBrendan Higgins 	 * There is still some debate to be had on exactly how we do this. (For
505f3e0620SBrendan Higgins 	 * one, we probably want to have some sort of test runner level
515f3e0620SBrendan Higgins 	 * timeout.)
525f3e0620SBrendan Higgins 	 *
535f3e0620SBrendan Higgins 	 * For more background on this topic, see:
545f3e0620SBrendan Higgins 	 * https://mike-bland.com/2011/11/01/small-medium-large.html
555f3e0620SBrendan Higgins 	 *
561c024d45SAlan Maguire 	 * If tests timeout due to exceeding sysctl_hung_task_timeout_secs,
571c024d45SAlan Maguire 	 * the task will be killed and an oops generated.
585f3e0620SBrendan Higgins 	 */
59bdd015f7SPeng Liu 	return 300 * msecs_to_jiffies(MSEC_PER_SEC); /* 5 min */
605f3e0620SBrendan Higgins }
615f3e0620SBrendan Higgins 
kunit_try_catch_run(struct kunit_try_catch * try_catch,void * context)625f3e0620SBrendan Higgins void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
635f3e0620SBrendan Higgins {
645f3e0620SBrendan Higgins 	struct kunit *test = try_catch->test;
655f3e0620SBrendan Higgins 	struct task_struct *task_struct;
66*1eb69dedSDavid Gow 	struct completion *task_done;
675f3e0620SBrendan Higgins 	int exit_code, time_remaining;
685f3e0620SBrendan Higgins 
695f3e0620SBrendan Higgins 	try_catch->context = context;
705f3e0620SBrendan Higgins 	try_catch->try_result = 0;
71f8aa1b98SMickaël Salaün 	task_struct = kthread_create(kunit_generic_run_threadfn_adapter,
72f8aa1b98SMickaël Salaün 				     try_catch, "kunit_try_catch_thread");
735f3e0620SBrendan Higgins 	if (IS_ERR(task_struct)) {
74cde5e1b4SMickaël Salaün 		try_catch->try_result = PTR_ERR(task_struct);
755f3e0620SBrendan Higgins 		try_catch->catch(try_catch->context);
765f3e0620SBrendan Higgins 		return;
775f3e0620SBrendan Higgins 	}
78f8aa1b98SMickaël Salaün 	get_task_struct(task_struct);
793a35c130SMickaël Salaün 	/*
803a35c130SMickaël Salaün 	 * As for a vfork(2), task_struct->vfork_done (pointing to the
813a35c130SMickaël Salaün 	 * underlying kthread->exited) can be used to wait for the end of a
82*1eb69dedSDavid Gow 	 * kernel thread. It is set to NULL when the thread exits, so we
83*1eb69dedSDavid Gow 	 * keep a copy here.
843a35c130SMickaël Salaün 	 */
85*1eb69dedSDavid Gow 	task_done = task_struct->vfork_done;
86*1eb69dedSDavid Gow 	wake_up_process(task_struct);
87*1eb69dedSDavid Gow 
88*1eb69dedSDavid Gow 	time_remaining = wait_for_completion_timeout(task_done,
895f3e0620SBrendan Higgins 						     kunit_test_timeout());
905f3e0620SBrendan Higgins 	if (time_remaining == 0) {
915f3e0620SBrendan Higgins 		try_catch->try_result = -ETIMEDOUT;
92adf50545SPeng Liu 		kthread_stop(task_struct);
935f3e0620SBrendan Higgins 	}
945f3e0620SBrendan Higgins 
95f8aa1b98SMickaël Salaün 	put_task_struct(task_struct);
965f3e0620SBrendan Higgins 	exit_code = try_catch->try_result;
975f3e0620SBrendan Higgins 
985f3e0620SBrendan Higgins 	if (!exit_code)
995f3e0620SBrendan Higgins 		return;
1005f3e0620SBrendan Higgins 
1015f3e0620SBrendan Higgins 	if (exit_code == -EFAULT)
1025f3e0620SBrendan Higgins 		try_catch->try_result = 0;
1038bd5d74bSMickaël Salaün 	else if (exit_code == -EINTR) {
1048bd5d74bSMickaël Salaün 		if (test->last_seen.file)
1058bd5d74bSMickaël Salaün 			kunit_err(test, "try faulted: last line seen %s:%d\n",
1068bd5d74bSMickaël Salaün 				  test->last_seen.file, test->last_seen.line);
1078bd5d74bSMickaël Salaün 		else
1083a35c130SMickaël Salaün 			kunit_err(test, "try faulted\n");
1098bd5d74bSMickaël Salaün 	} else if (exit_code == -ETIMEDOUT)
11053026ff6SMickaël Salaün 		kunit_err(test, "try timed out\n");
1115f3e0620SBrendan Higgins 	else if (exit_code)
1125f3e0620SBrendan Higgins 		kunit_err(test, "Unknown error: %d\n", exit_code);
1135f3e0620SBrendan Higgins 
1145f3e0620SBrendan Higgins 	try_catch->catch(try_catch->context);
1155f3e0620SBrendan Higgins }
116c475c77dSAlan Maguire EXPORT_SYMBOL_GPL(kunit_try_catch_run);
117