From patchwork Fri Nov 25 08:43:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13055606 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40F8AC4332F for ; Fri, 25 Nov 2022 08:43:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A22C56B0073; Fri, 25 Nov 2022 03:43:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D2896B0074; Fri, 25 Nov 2022 03:43:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8C2756B0075; Fri, 25 Nov 2022 03:43:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7D6A06B0073 for ; Fri, 25 Nov 2022 03:43:15 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 48CBE809B0 for ; Fri, 25 Nov 2022 08:43:15 +0000 (UTC) X-FDA: 80171325150.18.0A31DB4 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf17.hostedemail.com (Postfix) with ESMTP id CC9EA40013 for ; Fri, 25 Nov 2022 08:43:14 +0000 (UTC) Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-398d108a04cso33392417b3.16 for ; Fri, 25 Nov 2022 00:43:14 -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=w/1PuXuSARwflnrY6dF6iGfBdOoMHQiq4nVJ/yzJteVwSyg/wp1SE5NMJRLngwplZ6 Tk0OjMTSvkXTJwNBYc8rlAGdiW5rwRWI6+jyZzM7Ox3/SfsAZnBdIst4rgp53DInnnQ5 lOaXxUGCrNa+xirv4H8YgrK+gPPmSdcxuqj6OAAZ0qXY//Fqr+Sl5cwlggM64ChMzBgT 9ar4zHr395dBCn4bEhlYvsa0w3MFT5Zmrb6EEWQKygRoGXRBIVf2LNzJYJASSZ6Vz3MI lvYcYBt918NfE64E1DK4+THuM92fjEbfDbSh8iQ5sWyk6WT+AZ3JQO85rWLGj+ADq8kz GXcg== X-Gm-Message-State: ANoB5plYezxwZEIt8tWPKVVdjA7YvjwsHmUwPnqkMsWK2q65jIXCceo4 Vlmkx1h7xB4dSEmtXm+moEZa1chdz0fpSg== X-Google-Smtp-Source: AA0mqf6SIG2nM06vCqr04rh+5zvembhI1If1NZodMNsjqU25om3zZ8oQIazCspuzdvbnHIuLg9UcLSc+CucUsg== 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669365794; a=rsa-sha256; cv=none; b=yNO6zK8HPRd/m+xvsQ6wrlvWaPH2soz2s1GmLPyItNKQVyitVJyRkuB6EW3s6qtmpqOnUM q1w4LHqBJjprf67IVTvabs364dSgDU2C7rzJeoJeIwwQdHwoqpYQOtRV+I0d910RgBpKT1 qlKv6sfAKo4cgrKzTmTtSivgRm2CO0A= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=U3tR0OVo; spf=pass (imf17.hostedemail.com: domain of 3IYCAYwgKCEYli3qlow4owwotm.kwutqv25-uus3iks.wzo@flex--davidgow.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3IYCAYwgKCEYli3qlow4owwotm.kwutqv25-uus3iks.wzo@flex--davidgow.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669365794; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=dehjIhc0oB2x/7qa8tpHLfPv+8ip2PCtDc/VgLdDFS0=; b=G8pP08Yw5KlCtkX7o/9okoKTKVLzpIJIa4km2CycKhqPqO0QOHutBZZJpzl7sWgcrWQZEo FMB83EOCTZPXse+a3A3TRQyuXf0gzHei3vp2yREKtnRgRMj+gHoQ91hfBfnQDKf0ofeZMN htXPnzHk7wpy061fc9MPLAlNH46OnRc= X-Rspam-User: Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=U3tR0OVo; spf=pass (imf17.hostedemail.com: domain of 3IYCAYwgKCEYli3qlow4owwotm.kwutqv25-uus3iks.wzo@flex--davidgow.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3IYCAYwgKCEYli3qlow4owwotm.kwutqv25-uus3iks.wzo@flex--davidgow.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Stat-Signature: pdpewizrg4ddcfifx6s9xhbym8988wt5 X-Rspamd-Queue-Id: CC9EA40013 X-Rspamd-Server: rspam12 X-HE-Tag: 1669365794-220028 X-Bogosity: Ham, tests=bogofilter, spamicity=0.025890, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Reviewed-by: Kees Cook --- 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);