kunit: Provide a static key to check if KUnit is actively running tests
authorDavid Gow <davidgow@google.com>
Fri, 25 Nov 2022 08:43:04 +0000 (16:43 +0800)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 12 Dec 2022 21:13:47 +0000 (14:13 -0700)
KUnit does a few expensive things when enabled. This hasn't been a
problem because KUnit was only enabled on test kernels, but with a few
people enabling (but not _using_) KUnit on production systems, we need a
runtime way of handling this.

Provide a 'kunit_running' static key (defaulting to false), which allows
us to hide any KUnit code behind a static branch. This should reduce the
performance impact (on other code) of having KUnit enabled to a single
NOP when no tests are running.

Note that, while it looks unintuitive, tests always run entirely within
__kunit_test_suites_init(), so it's safe to decrement the static key at
the end of this function, rather than in __kunit_test_suites_exit(),
which is only there to clean up results in debugfs.

Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Daniel Latypov <dlatypov@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
include/kunit/test.h
lib/kunit/test.c

index 4666a4d199ea4225884a3ce00e4f6f5cff8260fd..87ea90576b50749ddac8d7d17e6a2848b6bbad9e 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/container_of.h>
 #include <linux/err.h>
 #include <linux/init.h>
+#include <linux/jump_label.h>
 #include <linux/kconfig.h>
 #include <linux/kref.h>
 #include <linux/list.h>
@@ -27,6 +28,9 @@
 
 #include <asm/rwonce.h>
 
+/* Static key: true if any KUnit tests are currently running */
+DECLARE_STATIC_KEY_FALSE(kunit_running);
+
 struct kunit;
 
 /* Size of log associated with test. */
index 90640a43cf623eeb711af33aa356beecea8ced72..314717b630809e5faba15706e151e888ea05a345 100644 (file)
@@ -20,6 +20,8 @@
 #include "string-stream.h"
 #include "try-catch-impl.h"
 
+DEFINE_STATIC_KEY_FALSE(kunit_running);
+
 #if IS_BUILTIN(CONFIG_KUNIT)
 /*
  * Fail the current test and print an error message to the log.
@@ -612,10 +614,14 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_
                return 0;
        }
 
+       static_branch_inc(&kunit_running);
+
        for (i = 0; i < num_suites; i++) {
                kunit_init_suite(suites[i]);
                kunit_run_tests(suites[i]);
        }
+
+       static_branch_dec(&kunit_running);
        return 0;
 }
 EXPORT_SYMBOL_GPL(__kunit_test_suites_init);