From patchwork Tue Oct 25 07:19:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gow X-Patchwork-Id: 13018611 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 54FA9C38A2D for ; Tue, 25 Oct 2022 07:19:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C089580008; Tue, 25 Oct 2022 03:19:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BB79780007; Tue, 25 Oct 2022 03:19:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7F6F80008; Tue, 25 Oct 2022 03:19:21 -0400 (EDT) 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 8E49780007 for ; Tue, 25 Oct 2022 03:19:21 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 385F4AB334 for ; Tue, 25 Oct 2022 07:19:21 +0000 (UTC) X-FDA: 80058620922.06.2A4CB2F Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf03.hostedemail.com (Postfix) with ESMTP id D07642002C for ; Tue, 25 Oct 2022 07:19:20 +0000 (UTC) Received: by mail-yb1-f202.google.com with SMTP id d8-20020a25bc48000000b00680651cf051so11253823ybk.23 for ; Tue, 25 Oct 2022 00:19:20 -0700 (PDT) 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=Tg+2eIYV8Ht6XVdDxVGWgiFGyJrz8FeDTVg6oxA83to=; b=N20hIBljO9B2x5fTp2LgIWIJ6EaOGsk8PztwvAmRhr/ICL8s2SDtYc8Xej+kchGmum RO8n9j1w+WKDCz7oFgqPc2IdvBYxzjByNjkm4hxwVX+FcssRWGWyfKDGaBXlpNHd/ESd w53SmUZL8Apz9MpYSTDJwmcgtyACqeyETGmVKhgDL1+oWIiKfB8MKRr+LXaBk3NwQMSq S59QO27GUgYRSNVlxuBn8eg+5y6oeZVK8xiN8MecuKy51E1BGP8PDElLUtQgOdAZ3oz6 M198xDTBoLYhZB6crv2JTgJMdFBDEkWmU+VOD3su3teMa0VAyK1zEM0iSqeyadiML47h /9QA== 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=Tg+2eIYV8Ht6XVdDxVGWgiFGyJrz8FeDTVg6oxA83to=; b=lBen+AGQRmz40iYH+9KJeaBqgoM/FIVMAWZNW/Qp1kllYW8D7/oozilEMesh0zno+8 ohP+86ecYv25tHq5zh65+6qMpAbbD4MeNiuWWtWNUmsG11dHSzj+xAU42PxiUqligHGA bM9Xm/B8GJhsWTyQS/UuQ8ZsEu5O2+MQYh2lW9eq5VRy4zdtT9VAL3ohIUOqevUIRYXB wcFhl5zC4xCV2Z7VMlkYZXgT9I9gEg7gn/27+Wk3nKVyc8yeKygS7Cg+UC2e6+NSvN3z OFny4oCR4tVJRt5z13xbS6keiMJI0RKkEbG9T5WaSdy/4w4WLkRyBA/DcNToT9Eo3swD BJOA== X-Gm-Message-State: ACrzQf1SmgV73icnMzjBcl16X8RdhGuRrTWPu8JPXj8ax44PqpIwKsMU AR3dBEMb22Kjz8g+Wi6p0BLwGlfjvvcMFg== X-Google-Smtp-Source: AMsMyM4EBhOMZUTQa08gGVH5ZMe4v1UYMJ1yyiUBna1Fp9X+01+i+YQcjjUKRz2D67kIhfv/nu3VhoLywHoY0A== X-Received: from slicestar.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:20a1]) (user=davidgow job=sendgmr) by 2002:a25:b749:0:b0:68f:171f:96bd with SMTP id e9-20020a25b749000000b0068f171f96bdmr34115629ybm.137.1666682360070; Tue, 25 Oct 2022 00:19:20 -0700 (PDT) Date: Tue, 25 Oct 2022 15:19:05 +0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221025071907.1251820-1-davidgow@google.com> Subject: [PATCH v2 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=1666682360; a=rsa-sha256; cv=none; b=Q7UaeQPjxzVVO1Zlo0t+VYuF/RtiQunYgrzfvCV9tVejJvYvvQEbrHj9J90oR0FDEkV2ik wKhgQHIVzqSHRkZndCKjy2BXtejjtBZ6V4AKwQwu5MlSohMiYgdB6+xzYu56K/5vYhJDlp KZcsoVkc0Ozrt4yQdcqSLk6+M8/QXQc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=N20hIBlj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3-I1XYwgKCJM0xI503BJ3BB381.zB985AHK-997Ixz7.BE3@flex--davidgow.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3-I1XYwgKCJM0xI503BJ3BB381.zB985AHK-997Ixz7.BE3@flex--davidgow.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666682360; 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=Tg+2eIYV8Ht6XVdDxVGWgiFGyJrz8FeDTVg6oxA83to=; b=NGZfyKOp44VrWJTfCbob2KJBq8bsVvVOniysf/X/gRhykCGbtT/K/LAy/jdFFIb1AOg41E V8tEVKe3N58j7JgYYLyTsXMynH926I3JwWNCWObUEl4RLZTbfUbU9qoKpusJADVbyw8L83 8C0saW2TWe87HkyKYRmDkNf2XY/uYXQ= X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D07642002C X-Rspam-User: Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=N20hIBlj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3-I1XYwgKCJM0xI503BJ3BB381.zB985AHK-997Ixz7.BE3@flex--davidgow.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3-I1XYwgKCJM0xI503BJ3BB381.zB985AHK-997Ixz7.BE3@flex--davidgow.bounces.google.com X-Stat-Signature: 83bu37dwtojs65ffj1guug5rd9bq6xpg X-HE-Tag: 1666682360-739158 X-Bogosity: Ham, tests=bogofilter, spamicity=0.003758, 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 --- 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 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 b1ab6b32216d..450a778a039e 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);