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: 13049638 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 2D792C4332F for ; Sat, 19 Nov 2022 08:13:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 23CF36B0072; Sat, 19 Nov 2022 03:13:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C7236B0073; Sat, 19 Nov 2022 03:13:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 03FC76B0074; Sat, 19 Nov 2022 03:13:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E3D2B6B0072 for ; Sat, 19 Nov 2022 03:13:02 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B1CEDA0C8C for ; Sat, 19 Nov 2022 08:13:02 +0000 (UTC) X-FDA: 80149476204.21.D8B7B49 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf01.hostedemail.com (Postfix) with ESMTP id C142740002 for ; Sat, 19 Nov 2022 08:13:00 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id c188-20020a25c0c5000000b006d8eba07513so6679452ybf.17 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=gAfb5cFenNjz/rkt4U9YxOX80IIvQSm4LdmbYac8vEIfSFEEOPY/xgeG4pUS/sn8n2 oAgnADcef1uDh/Fb1ocNqiz5wNV3nVVpIqRyzLv7OsWylJkT7vB5Q2YJH+Cmyny0MHOd C9tVPQOKDcN+okryK6FXqPrLiu/EdniesE/c85GJld26W1Wr2P8DRRx4pcU0APUTn7IM Gnj5/pdNInARlFvyFn8aTLk2kLRJXCYBCzIvexlexJ2l5NnXlOXvK1G7bcVtwq/tQITz prPuvftCcjpLCWqd9zpXN5JAzxXDthA7cIraOk1xSgbhdc1WNzGfl0kNoZbzmZ+Lq9kz DoAA== X-Gm-Message-State: ANoB5pm8fo5cP9xjj/lTCq0zYn0tb21BGCS9+UxHJfmgDTSJIkb5XPpg T/z9rCsS4D5xwtkVdOuj+6cibXTGBtFoSw== X-Google-Smtp-Source: AA0mqf67B7vUfx2Z0il/dhl9vKO2jAAok4f+bf6diQKTxBlXzTpb7pEn7mFBK76m9TQGjgkDGqNWmpmh5ibXWQ== 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668845580; a=rsa-sha256; cv=none; b=FnMqU180c2qdSOV2da2jkR+BltzXVbOx5Ncpj9AQZz0N4Sjq7CMSsmnw/Tri9+2jM+VPmE cwDkOqceMzMDLj0xsIOqNxUvbpTV78SEjkD0/ZcrVnDwQXXfntWpwycaOL/2QFQUqtxpr/ eNie6AtWe3nwzynTKjqM3XkU5t21Kag= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=IcwXafex; spf=pass (imf01.hostedemail.com: domain of 3C5B4YwgKCDAPMhUPSaiSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--davidgow.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3C5B4YwgKCDAPMhUPSaiSaaSXQ.OaYXUZgj-YYWhMOW.adS@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=1668845580; 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=mxlxAlM6/dget+xcMGAu2YOt8fNlUAK4C29Oqcd00C8=; b=W1+bVG/7c9SXGmrJWYG6tPmaUe3DkmmiEjru9ETkRWTpXiUYpCBmz36q3S4BVyCIxmnEIV JJ0E6O+ydRh9Z3BzVcb82JxBMd04noJAOcgqj3OPCeX4Qrdc4fLsQESqpvfwl3gQvqGg0z 8uymt+Ls3S4s825TsC9sNk2S9CMegtE= Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=IcwXafex; spf=pass (imf01.hostedemail.com: domain of 3C5B4YwgKCDAPMhUPSaiSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--davidgow.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3C5B4YwgKCDAPMhUPSaiSaaSXQ.OaYXUZgj-YYWhMOW.adS@flex--davidgow.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: C142740002 X-Rspam-User: X-Stat-Signature: hreoz3uzgs7333k7fi97b15dixq54iyw X-HE-Tag: 1668845580-887058 X-Bogosity: Ham, tests=bogofilter, spamicity=0.032607, 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 --- 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);