From patchwork Sat Nov 19 08:12:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 23232 Return-Path: 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 + 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 ); 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 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749911548875576796?= X-GMAIL-MSGID: =?utf-8?q?1749911548875576796?= 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. 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);