[PATCH 1/2] kunit: add kunit.enable to enable/disable KUnit test
From: Joe Fradley
Date: Wed Aug 17 2022 - 12:49:27 EST
This patch adds the kunit.enable module parameter that will need to be
set to true in addition to KUNIT being enabled for KUnit tests to run.
The default value is true giving backwards compatibility. However, for
the production+testing use case the new config option
KUNIT_ENABLE_DEFAULT_DISABLED can be enabled to default kunit.enable to
false requiring the tester to opt-in by passing kunit.enable=1 to
the kernel.
Signed-off-by: Joe Fradley <joefradley@xxxxxxxxxx>
---
.../admin-guide/kernel-parameters.txt | 6 ++++++
lib/kunit/Kconfig | 8 ++++++++
lib/kunit/test.c | 20 +++++++++++++++++++
3 files changed, 34 insertions(+)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index d7f30902fda0..ab4c7d133c38 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -2436,6 +2436,12 @@
0: force disabled
1: force enabled
+ kunit.enable= [KUNIT] Enable executing KUnit tests. Requires
+ CONFIG_KUNIT to be set to be fully enabled. The
+ default value can be overridden to disabled via
+ KUNIT_ENABLE_DEFAULT_DISABLED.
+ Default is 1 (enabled)
+
kvm.ignore_msrs=[KVM] Ignore guest accesses to unhandled MSRs.
Default is 0 (don't ignore, but inject #GP)
diff --git a/lib/kunit/Kconfig b/lib/kunit/Kconfig
index 0b5dfb001bac..5d6db58dbe9b 100644
--- a/lib/kunit/Kconfig
+++ b/lib/kunit/Kconfig
@@ -59,4 +59,12 @@ config KUNIT_ALL_TESTS
If unsure, say N.
+config KUNIT_ENABLE_DEFAULT_DISABLED
+ bool "Require boot parameter to enable KUnit test execution"
+ help
+ Sets the default value of the kernel parameter kunit.enable to 0
+ (disabled). This means to fully enable kunit, config KUNIT needs
+ to be enabled along with `kunit.enable=1` passed to the kernel. This
+ allows KUnit to be opt-in at boot time.
+
endif # KUNIT
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index b73d5bb5c473..e6f98e16e8ae 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -54,6 +54,17 @@ void __kunit_fail_current_test(const char *file, int line, const char *fmt, ...)
EXPORT_SYMBOL_GPL(__kunit_fail_current_test);
#endif
+/*
+ * Enable KUnit tests to run.
+ */
+#ifdef CONFIG_KUNIT_ENABLE_DEFAULT_DISABLED
+static bool enable_param;
+#else
+static bool enable_param = true;
+#endif
+module_param_named(enable, enable_param, bool, 0);
+MODULE_PARM_DESC(enable, "Enable KUnit tests to run");
+
/*
* KUnit statistic mode:
* 0 - disabled
@@ -590,6 +601,12 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_
{
unsigned int i;
+ if (!enable_param && num_suites > 0) {
+ pr_info("kunit: deactivated, cannot load %s\n",
+ suites != NULL && suites[0] != NULL ? suites[0]->name : "NULL");
+ return -EPERM;
+ }
+
for (i = 0; i < num_suites; i++) {
kunit_init_suite(suites[i]);
kunit_run_tests(suites[i]);
@@ -607,6 +624,9 @@ void __kunit_test_suites_exit(struct kunit_suite **suites, int num_suites)
{
unsigned int i;
+ if (!enable_param)
+ return;
+
for (i = 0; i < num_suites; i++)
kunit_exit_suite(suites[i]);
--
2.37.1.595.g718a3a8f04-goog