Message ID | 20221119081252.3864249-1-davidgow@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp608632wrr; Sat, 19 Nov 2022 00:15:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf5gaTE9XcaQQsfapnPyEwS4AbLJ2DUWvwAjmhImCmg4RmPgsIhp831f0u8A4SRcFkkiQUlJ X-Received: by 2002:a17:906:5050:b0:7b2:8f2c:a877 with SMTP id e16-20020a170906505000b007b28f2ca877mr8365644ejk.90.1668845700168; Sat, 19 Nov 2022 00:15:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668845700; cv=none; d=google.com; s=arc-20160816; b=yriUfgacYWa4vmzVVxenNDMzvNlulGtrUzNlpSoU/N2IEixnuUmraz193JTD0oikzj 5uWfGLMinK5f7I8Xz4aiJEIk8yFfLPJgK+JhGSiuv+ArQ25FxaobmSr5w7/0PwH5ZazL VG1M8IfggGrcgZ92VYDaaFEjpoCn3Dlq0fdDkpnbUP85Y4YIT+S5qymY1ZE7ye+HnaAv DpmVA1LhjUvE9LTnZakOOd+1S30zVXw6K47KYdYJbLKCd5+XTEMNY59pWAQrjVS2j+H5 HRL0KuFew9A9cV2mr3KW2vpPt7ksMkjcqKipYSYtRVoo81GdLSRSSCb238M1NSX5VQeA qLQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=mxlxAlM6/dget+xcMGAu2YOt8fNlUAK4C29Oqcd00C8=; b=l/X/2+CypTMhbLs3gLPVvg8/6Z/kzCzqK2cMirgdWAOGyQTjdACPdAYiWqLy47eN5b 7XrRfMqDrJtKvSLj9aLsfNQ3nzV1Dz53ja22GwFP7phIZAzArJgxEzC6kxApRBKEFwl7 CjpJo0U1XSpaHqQsivo1I4jiBaANscOEV/LwxSUKI0fPUfFeFapyskWT8/IVLOiL5QPx MdyljKrM0fQYtgcx0zFoH60eDKGstB1w/FGKeHirxIc256aDQg31m/f5hQmQp7Zf2+GH YEq6ewQ+1jIoHoDVf/iaHiZgHyqzBHrAQWIXvdKuk8iIuKd1s/c7F0eWMWwBiTEaULDe Yh9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=IcwXafex; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n11-20020a170906118b00b007ae4c686a4esi3812330eja.159.2022.11.19.00.14.36; Sat, 19 Nov 2022 00:15:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=IcwXafex; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230424AbiKSINF (ORCPT <rfc822;kkmonlee@gmail.com> + 99 others); Sat, 19 Nov 2022 03:13:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33152 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229836AbiKSIND (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sat, 19 Nov 2022 03:13:03 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BED0F7DED2 for <linux-kernel@vger.kernel.org>; Sat, 19 Nov 2022 00:13:00 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-391842a55d6so61368837b3.0 for <linux-kernel@vger.kernel.org>; Sat, 19 Nov 2022 00:13:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=mxlxAlM6/dget+xcMGAu2YOt8fNlUAK4C29Oqcd00C8=; b=IcwXafexRAl664jaBoJyAa5mMQYLIYMkPXwqAvZb6o4ue+mpDnBlE0xs0UzTUu6YrL J/64TUXc9QJcwIpZb9Yj/DCH6Dwppd6ZMkmAPNMO9NLSQpa+UGz749sgxZn2x0YkNUIf LXPeai2EGngQkB7CfGiliNP7pMyGOorEuwIKH7/iimSqlhTNe9/StKThGkjm1J478yQT FjE1xZ26fMXjtNwEj/vs994FgwWwhpqoI2BaovX8NQreDY9YAYWcG+493mAuAS7O7+Dr a5zzfXgoWCsjb0963b6N3+kOMQOwst8wLhiAngZj5t5raxaGfEX5swv2inf1I1yUiP9c uOiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=mxlxAlM6/dget+xcMGAu2YOt8fNlUAK4C29Oqcd00C8=; b=XR1Pdv0rNX6e03bLDDZYxe5tSrGWLUZUeaH5MDiJ7PHDaMqQBgbUCorM3tzU/ataNK 6rfbzqdl/Eek15LtZzpM83kERTFAkHlns6vxVEmYzg+xKfeVcd2MbU8YQzyjGQM8IMSK YRaNiwzmmKxof5HH0QPkS4YQr3RO7bPYyOyFAD1rnS23leH8KHLLXX3VoFTk4sVBMwZ6 5AUAF8cYB3t8zZ0jp2XL07iTEV6Etm3puZaa/eh9JoMozWppEYY7gTXmAf6GqKu7kKbX T+/RlhB1H5qo6rmpXV7zZitnjcz+DndHt51NBMLaLv+dWcN0iLQN0PfRst98arKGZPez cDEg== X-Gm-Message-State: ANoB5pnZhw7yVZIe2aBYzYOd0viQUSGXfH9eIe/tK8911ozHiwn0gVv2 SeKrorNoStkYHvT8kcmZE8t8iZzK9VOuGw== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:e807:0:b0:6ca:21b3:42ec with SMTP id k7-20020a25e807000000b006ca21b342ecmr9916189ybd.398.1668845579853; Sat, 19 Nov 2022 00:12:59 -0800 (PST) Date: Sat, 19 Nov 2022 16:12:50 +0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221119081252.3864249-1-davidgow@google.com> Subject: [PATCH v3 1/3] kunit: Provide a static key to check if KUnit is actively running tests From: David Gow <davidgow@google.com> To: Brendan Higgins <brendan.higgins@linux.dev>, Daniel Latypov <dlatypov@google.com>, Shuah Khan <skhan@linuxfoundation.org> Cc: David Gow <davidgow@google.com>, kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook <keescook@chromium.org>, linux-doc@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749911548875576796?= X-GMAIL-MSGID: =?utf-8?q?1749911548875576796?= |
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
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
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
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);