• David Gow's avatar
    kunit: Always run cleanup from a test kthread · 55e8c1b4
    David Gow authored
    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: default avatarBenjamin Berg <benjamin.berg@intel.com>
    Reviewed-by: default avatarMaxime Ripard <maxime@cerno.tech>
    Tested-by: default avatarMaxime Ripard <maxime@cerno.tech>
    Signed-off-by: default avatarDavid Gow <davidgow@google.com>
    Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
    55e8c1b4
test.c 20.9 KB