kunit: Always run cleanup from a test kthread
authorDavid Gow <davidgow@google.com>
Wed, 10 May 2023 05:38:29 +0000 (13:38 +0800)
committerShuah Khan <skhan@linuxfoundation.org>
Fri, 12 May 2023 00:17:24 +0000 (18:17 -0600)
commit55e8c1b49ac5a7f5567430cc83f2d270d8b7ce46
treefd3fe8e7a59e9fb44a47b08469a06816e9239a1b
parent78227fa03c6854e8ca23911309c763d909653c6e
kunit: Always run cleanup from a test kthread

KUnit tests run in a kthread, with the current->kunit_test pointer set
to the test's context. This allows the kunit_get_current_test() and
kunit_fail_current_test() macros to work. Normally, this pointer is
still valid during test shutdown (i.e., the suite->exit function, and
any resource cleanup). However, if the test has exited early (e.g., due
to a failed assertion), the cleanup is done in the parent KUnit thread,
which does not have an active context.

Instead, in the event test terminates early, run the test exit and
cleanup from a new 'cleanup' kthread, which sets current->kunit_test,
and better isolates the rest of KUnit from issues which arise in test
cleanup.

If a test cleanup function itself aborts (e.g., due to an assertion
failing), there will be no further attempts to clean up: an error will
be logged and the test failed. For example:
 # example_simple_test: test aborted during cleanup. continuing without cleaning up

This should also make it easier to get access to the KUnit context,
particularly from within resource cleanup functions, which may, for
example, need access to data in test->priv.

Reviewed-by: Benjamin Berg <benjamin.berg@intel.com>
Reviewed-by: Maxime Ripard <maxime@cerno.tech>
Tested-by: Maxime Ripard <maxime@cerno.tech>
Signed-off-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
lib/kunit/test.c