Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp10111886rwb; Fri, 25 Nov 2022 00:53:23 -0800 (PST) X-Google-Smtp-Source: AA0mqf4coueSr966kF/wP3ZTnWp3w6zGFGPmx0HW3cn0j1EbYU9FfwnXT1+xfvyPf7dErf7/F5sv X-Received: by 2002:a17:903:41c8:b0:188:f0d7:ffbe with SMTP id u8-20020a17090341c800b00188f0d7ffbemr17552444ple.27.1669366402772; Fri, 25 Nov 2022 00:53:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669366402; cv=none; d=google.com; s=arc-20160816; b=M38YRX9i9JZm3L3xc0rns+OnBZC+FMBOHb1maplDrfNQGJEnONXMnZ1rHF1NFkl9K1 KUmD+w0O3BqXAGUThz9Fz4dXhwi9iRN2Vwh/m6wmq/SfKcJZePBvoH2PQa0kyxvbV1bo F0qriLkH7Aof6qm55Fd3nhZkxeKCU+al7chLwEO5+oDnUrb49xkPavGJV+TtS89Un6xe jicL6VpO2ZP0pN+uOB6uYecubU6n8sksf6ThwUxUUC4eB2RXic8ytRxrxUWvamI/u1Dk ooZvr7zCf1yNAXMzsVgqrXMNR/ySx1IPFYhkE/Dq2wjVUSsrqEwld2fUnCAhZsyTh4YY +0Eg== 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=dehjIhc0oB2x/7qa8tpHLfPv+8ip2PCtDc/VgLdDFS0=; b=PpMp+ESQcu/Lnty5xh64//olkbuukwA1vfgX9zPGW4PsRVVJFxylakFWPPg3UwUZeN qQqCMo3N0CJiHv/fBgbvEKCFedUdhojLb4F5k8cPPxx3q3sFCcF86pv0ZCZ6ZVv/ZcEu L9TFXwwE5VNOLaq4jcBidMuIOsMkuHCIds9CFYv+THEcjbe05I2yWj6hYYuofQvnKI8C C9VBCPSOGjD63OADd3TNa4F2AufNsIDCYcFt1TN6tKYzH19lPHS/qh5qMB/Fmjm0hs40 NfiU2ccK4vXHppj4JjKLa8HXrBn0EFtYwoL8U5NZBqIF/QgGTnjxLaVlou7pecq9/boO 6KlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=U3tR0OVo; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y2-20020a63fa02000000b004771a05aea3si2731832pgh.554.2022.11.25.00.53.12; Fri, 25 Nov 2022 00:53:22 -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=U3tR0OVo; 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 S229771AbiKYInb (ORCPT + 86 others); Fri, 25 Nov 2022 03:43:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229468AbiKYInQ (ORCPT ); Fri, 25 Nov 2022 03:43:16 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 216E620F6F for ; Fri, 25 Nov 2022 00:43:15 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id a5-20020a25af05000000b006e450a5e507so3361982ybh.22 for ; Fri, 25 Nov 2022 00:43:15 -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=dehjIhc0oB2x/7qa8tpHLfPv+8ip2PCtDc/VgLdDFS0=; b=U3tR0OVoZ0hHgA1k6gQVY26XcT3oNgQIup/uX30yKzUfLfrSKpvRJsmcSBuhiAXpTS rTzQMGzMJNRsRtthJ4BxyoK+TidlhhFCJmjhxBf/o1FEDj74nyLKCXv3UoCsEVkyMOeU 364amPEiG2TPV7cF3TGX+tLLAPX4almhYT7NCNw9CXxbjzI0V/mb8zoOMitrJVRJA7dj so3SRxWDXfqlFI3Uw6FkxsZS8mgE7neClxz6IKN1K/gUkQhbn5yotHy8A0DKQmNZGmod UisNijbujJ0UGmHkgWBKaLdxRuxz2CsxSAWjZikzhuh6GNn9njbaHLDcXdeN0Mt/hTxE 23bg== 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=dehjIhc0oB2x/7qa8tpHLfPv+8ip2PCtDc/VgLdDFS0=; b=P2ujABfCYX3cPygKVsX21DfjB2wFmdqKL7mz1rLyynua2c9CuOQ2/rSw79Q75r+93l dAs8DFoJCfkY9oNRVO+eGtHW1yeppxrvPxk/ptGCAMLe0Ly54cmZtIM2tECSKKYsD1cM 2FFL6dvnm5kJu0fwYYKdKufuH/pntLllaiVbhrggdwDGgzxR2Kj2qrCUGTXUasszr6R9 Uznf9yuREbWk7GfE6tRJCl0fXHSqDe2f+OFxsnViVW1MauW66YI/lda75t+6mYBB1fXQ rSS6I2+htXBwPAx+31TG0d7AQ0bVnn+Tv1gwiGUCngAoB3Ino3KkwTa68+QvmRU+8Wtp IT5g== X-Gm-Message-State: ANoB5pnswS/DCooB8uHzj6W/5mWDTPErVQ4CU+Gj/8uWcxlRykB9tt+W anat4jx818TmbbAnHRbnVRX2O8J4i551bg== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a05:6902:114b:b0:6e1:ce17:6c40 with SMTP id p11-20020a056902114b00b006e1ce176c40mr9ybu.5.1669365793417; Fri, 25 Nov 2022 00:43:13 -0800 (PST) Date: Fri, 25 Nov 2022 16:43:04 +0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221125084306.1063074-1-davidgow@google.com> Subject: [PATCH v4 1/3] kunit: Provide a static key to check if KUnit is actively running tests From: David Gow To: Brendan Higgins , Daniel Latypov , Shuah Khan Cc: David Gow , kunit-dev@googlegroups.com, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, Kees Cook , 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: X-Mailing-List: linux-kernel@vger.kernel.org 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 Reviewed-by: Daniel Latypov --- 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 #include #include +#include #include #include #include @@ -27,6 +28,9 @@ #include +/* 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); + 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