Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1656046rwb; Sat, 19 Nov 2022 00:44:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf6sUOmcz5ICNvfdbMVMLa0UzB4+W3uFWJClgepQQSA1rcTVI+VW1yPXEKCn6lq7iGu+AsAA X-Received: by 2002:a17:902:efcf:b0:186:ba54:f340 with SMTP id ja15-20020a170902efcf00b00186ba54f340mr3126100plb.119.1668847448317; Sat, 19 Nov 2022 00:44:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668847448; cv=none; d=google.com; s=arc-20160816; b=tdGnM36TMIqcTMYS6QVotEN/HDE0P/mlAfWHEGlIi7O323fu3BUYwlY88NZShhQbAv Ip7qdjYUrM9Zwq/YtEQGjCRKLoHzODcfa2B7iC7lfIYcVXlaa4uVhUZ2Hz9K355LoUdb knq7KQeaRp1z3eiptr6j0WGGZNzyOw6ojkczM2NeTZEUSnPSWXnBvSLHMO52QAiYSQXf BmG4ZDpoLRHcIB3P/pewsvcTAZqGuxYl+AHKydGHuHshaXN3GZV6yZmw3MobKnwALBIg Md76VK/vNr+hcfPVvR0FR0EEKlWKbzQVMssoxQ7EivDNxEw6TgI1ZT58Nq9kV2C71IhR xnCg== 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 94-20020a17090a09e700b00212fe4afc73si8837944pjo.24.2022.11.19.00.43.57; Sat, 19 Nov 2022 00:44:08 -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 + 90 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 ); 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 ; Sat, 19 Nov 2022 00:13:00 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-391842a55d6so61368837b3.0 for ; 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 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 --- 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(+) 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 #include #include +#include #include #include #include @@ -27,6 +28,9 @@ #include +/* 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); -- 2.38.1.584.g0f3c55d4c2-goog