linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: David Gow <davidgow@google.com>
Cc: Brendan Higgins <brendan.higgins@linux.dev>,
	Daniel Latypov <dlatypov@google.com>,
	Shuah Khan <skhan@linuxfoundation.org>,
	kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-mm@kvack.org
Subject: Re: [PATCH v4 1/3] kunit: Provide a static key to check if KUnit is actively running tests
Date: Thu, 1 Dec 2022 16:53:06 -0800	[thread overview]
Message-ID: <202212011652.4E8CB40@keescook> (raw)
In-Reply-To: <20221125084306.1063074-1-davidgow@google.com>

On Fri, Nov 25, 2022 at 04:43:04PM +0800, David Gow 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>
> ---
> 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.
> 
> Changes since v3:
> https://lore.kernel.org/linux-kselftest/20221119081252.3864249-1-davidgow@google.com/
> - Use DECLARE_STATIC_KEY_FALSE() -- thanks Daniel!
> 
> 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(+)
> 
> diff --git a/include/kunit/test.h b/include/kunit/test.h
> index 4666a4d199ea..87ea90576b50 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 */
> +DECLARE_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 1c9d8d962d67..87a5d795843b 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.
> @@ -615,10 +617,14 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_
>  		return 0;
>  	}
>  
> +	static_branch_inc(&kunit_running);

Is it expected there will be multiple tests running? (I was expecting
"static_branch_enable").

> +
>  	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);
> -- 
> 2.38.1.584.g0f3c55d4c2-goog
> 

Regardless:

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook


  parent reply	other threads:[~2022-12-02  0:53 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-25  8:43 [PATCH v4 1/3] kunit: Provide a static key to check if KUnit is actively running tests David Gow
2022-11-25  8:43 ` [PATCH v4 2/3] kunit: Use the static key when retrieving the current test David Gow
2022-12-02  0:54   ` Kees Cook
2022-12-05  4:27   ` Sadiya Kazi
2022-11-25  8:43 ` [PATCH v4 3/3] mm: slub: test: Use the kunit_get_current_test() function David Gow
2022-12-02  0:55   ` Kees Cook
2022-12-02  0:53 ` Kees Cook [this message]
2022-12-02  1:19   ` [PATCH v4 1/3] kunit: Provide a static key to check if KUnit is actively running tests Daniel Latypov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202212011652.4E8CB40@keescook \
    --to=keescook@chromium.org \
    --cc=brendan.higgins@linux.dev \
    --cc=davidgow@google.com \
    --cc=dlatypov@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).