Files

92 lines
2.4 KiB
C
Raw Permalink Normal View History

2019-09-23 02:02:39 -07:00
// SPDX-License-Identifier: GPL-2.0
/*
* An API to allow a function, that may fail, to be executed, and recover in a
* controlled manner.
*
* Copyright (C) 2019, Google LLC.
* Author: Brendan Higgins <brendanhiggins@google.com>
*/
#include <kunit/test.h>
#include <linux/completion.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
2024-04-08 09:46:20 +02:00
#include <linux/sched/task.h>
2019-09-23 02:02:39 -07:00
#include "try-catch-impl.h"
2019-09-23 02:02:39 -07:00
void __noreturn kunit_try_catch_throw(struct kunit_try_catch *try_catch)
{
try_catch->try_result = -EFAULT;
2024-04-08 09:46:22 +02:00
kthread_exit(0);
2019-09-23 02:02:39 -07:00
}
EXPORT_SYMBOL_GPL(kunit_try_catch_throw);
2019-09-23 02:02:39 -07:00
static int kunit_generic_run_threadfn_adapter(void *data)
{
struct kunit_try_catch *try_catch = data;
2024-04-08 09:46:22 +02:00
try_catch->try_result = -EINTR;
2019-09-23 02:02:39 -07:00
try_catch->try(try_catch->context);
2024-04-08 09:46:22 +02:00
if (try_catch->try_result == -EINTR)
try_catch->try_result = 0;
2019-09-23 02:02:39 -07:00
2024-04-08 09:46:22 +02:00
return 0;
2019-09-23 02:02:39 -07:00
}
void kunit_try_catch_run(struct kunit_try_catch *try_catch, void *context)
{
struct kunit *test = try_catch->test;
struct task_struct *task_struct;
struct completion *task_done;
2019-09-23 02:02:39 -07:00
int exit_code, time_remaining;
try_catch->context = context;
try_catch->try_result = 0;
2024-04-08 09:46:20 +02:00
task_struct = kthread_create(kunit_generic_run_threadfn_adapter,
try_catch, "kunit_try_catch_thread");
2019-09-23 02:02:39 -07:00
if (IS_ERR(task_struct)) {
2024-04-08 09:46:19 +02:00
try_catch->try_result = PTR_ERR(task_struct);
2019-09-23 02:02:39 -07:00
try_catch->catch(try_catch->context);
return;
}
2024-04-08 09:46:20 +02:00
get_task_struct(task_struct);
2024-04-08 09:46:22 +02:00
/*
* As for a vfork(2), task_struct->vfork_done (pointing to the
* underlying kthread->exited) can be used to wait for the end of a
* kernel thread. It is set to NULL when the thread exits, so we
* keep a copy here.
2024-04-08 09:46:22 +02:00
*/
task_done = task_struct->vfork_done;
wake_up_process(task_struct);
time_remaining = wait_for_completion_timeout(
task_done, try_catch->timeout);
2019-09-23 02:02:39 -07:00
if (time_remaining == 0) {
try_catch->try_result = -ETIMEDOUT;
kthread_stop(task_struct);
2019-09-23 02:02:39 -07:00
}
2024-04-08 09:46:20 +02:00
put_task_struct(task_struct);
2019-09-23 02:02:39 -07:00
exit_code = try_catch->try_result;
if (!exit_code)
return;
if (exit_code == -EFAULT)
try_catch->try_result = 0;
2024-04-08 09:46:24 +02:00
else if (exit_code == -EINTR) {
if (test->last_seen.file)
kunit_err(test, "try faulted: last line seen %s:%d\n",
test->last_seen.file, test->last_seen.line);
else
kunit_err(test, "try faulted\n");
} else if (exit_code == -ETIMEDOUT)
2024-04-08 09:46:21 +02:00
kunit_err(test, "try timed out\n");
2019-09-23 02:02:39 -07:00
else if (exit_code)
kunit_err(test, "Unknown error: %d\n", exit_code);
try_catch->catch(try_catch->context);
}
EXPORT_SYMBOL_GPL(kunit_try_catch_run);