[v3,1/3] kunit: Provide a static key to check if KUnit is actively running tests

Message ID 20221119081252.3864249-1-davidgow@google.com
State New
Headers
Series [v3,1/3] kunit: Provide a static key to check if KUnit is actively running tests |

Commit Message

David Gow Nov. 19, 2022, 8:12 a.m. UTC
  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>
---

This should be a no-op (other than a possible performance improvement)
functionality-wise, and lays the groundwork for a more optimised static
stub implementation.

The remaining patches in the series add a kunit_get_current_test()
function which is a more friendly and performant wrapper around
current->kunit_test, and use this in the slub test. They also improve
the documentation a bit.

If there are no objections, we'll take the whole series via the KUnit
tree.

No changes since v2:
https://lore.kernel.org/all/20221025071907.1251820-1-davidgow@google.com/

Changes since v1:
https://lore.kernel.org/linux-kselftest/20221021072854.333010-1-davidgow@google.com/
- No changes in this patch.
- Patch 2/3 is reworked, patch 3/3 is new.

---
 include/kunit/test.h | 4 ++++
 lib/kunit/test.c     | 6 ++++++
 2 files changed, 10 insertions(+)
  

Comments

Daniel Latypov Nov. 22, 2022, 1:31 a.m. UTC | #1
On Sat, Nov 19, 2022 at 12:13 AM David Gow <davidgow@google.com> wrote:
>
> 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>

I didn't know anything about the static key support in the kernel
before this patch.
But from what I read and saw of other uses, this looks good to me.

One small question/nit about how we declare the key below.

<snip>

> +/* Static key: true if any KUnit tests are currently running */
> +extern struct static_key_false kunit_running;

Is there any documented preference between this and
  DECLARE_STATIC_KEY_FALSE(kunit_running);
?

I see 89 instances of this macro and 45 of `extern struct static_key_false`.
So I'd vote for the macro since it seems like the newer approach and
more common.

Daniel
  
David Gow Nov. 22, 2022, 2:33 a.m. UTC | #2
On Tue, Nov 22, 2022 at 9:31 AM Daniel Latypov <dlatypov@google.com> wrote:
>
> On Sat, Nov 19, 2022 at 12:13 AM David Gow <davidgow@google.com> wrote:
> >
> > 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>
>
> I didn't know anything about the static key support in the kernel
> before this patch.
> But from what I read and saw of other uses, this looks good to me.
>
> One small question/nit about how we declare the key below.
>
> <snip>
>
> > +/* Static key: true if any KUnit tests are currently running */
> > +extern struct static_key_false kunit_running;
>
> Is there any documented preference between this and
>   DECLARE_STATIC_KEY_FALSE(kunit_running);
> ?
>
> I see 89 instances of this macro and 45 of `extern struct static_key_false`.
> So I'd vote for the macro since it seems like the newer approach and
> more common.
>

Yeah, there was no particular reason I put 'extern struct
static_key_false'. I'll change it to DECLARE_STATIC_KEY_FALSE in v3.

Cheers,
-- David
  

Patch

diff --git a/include/kunit/test.h b/include/kunit/test.h
index d7f60e8aab30..b948c32a7b6b 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -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 */
+extern struct static_key_false kunit_running;
+
 struct kunit;
 
 /* Size of log associated with test. */
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 90640a43cf62..314717b63080 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -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);