From patchwork Fri Sep 8 21:31:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rae Moar X-Patchwork-Id: 13377825 X-Patchwork-Delegate: brendanhiggins@google.com 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D13A2EEB56E for ; Fri, 8 Sep 2023 21:32:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241092AbjIHVcD (ORCPT ); Fri, 8 Sep 2023 17:32:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231299AbjIHVcC (ORCPT ); Fri, 8 Sep 2023 17:32:02 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 770CDDC for ; Fri, 8 Sep 2023 14:31:58 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-58fc7afa4beso29402237b3.2 for ; Fri, 08 Sep 2023 14:31:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1694208717; x=1694813517; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=R7b9OwuJv//o+ELCx6dXPif0Xt9fI41k+DjaD06EUhk=; b=g7h0pQqLgkTyJOB05DFW8IDpmoUm2gdMjuWU7ITFm0Z0rkLJnr/7TyJx6BorHGOFqJ +M7Dg2SLb0vbIyUCGoZ+JQbE52JJUufc9ZOfoaNf9d6uzEzBqEUq8xrICsCUWLsINtu+ /v7mEpVR/REz/q2W9+UuOfscHmMXgreT1eD8Ssq0lNk4KKyXN6AzOnb0LQBr9xRw7U3W X6lpl2rpl9UtX807K8KGmvZIh1IzS7Ch18THaplOx2BBGQhMzJAaXqoI+LhLaph7WUuz CgUvVTYh5TAFN4zmWQKIBMPu1G5jntSx1FNoL7BFRdWm3HvnizO+nvyGCPl4lVhel6Dw 6NwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694208717; x=1694813517; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=R7b9OwuJv//o+ELCx6dXPif0Xt9fI41k+DjaD06EUhk=; b=oN+HQ5aFIfyGp2GAy57DC+ot45TEOL7EGoQuFw+DdOh7sFgFKJNfSZZmYqwgLdjHNw eXJ3nwePU4W5wlzragOwNsVmkRMB9yIv2mwXuDoiTpPNtQvPMgC+u/YSpbbE4KQkd7Nv PqM7qvopxnnR0zy7l0P0fbWYIpWps36OR0NCceatwMTGKnn+qoSl3ioyvA0j5XGb3jCg swtA4l3lRXtbZxmfyvRErKM7Qp5YEC43fn/lUlkCkJLMg/VSIDjsN+jQrJk8eWKkB9bo +Mmj9Okn8Uh6dCjglFTCUvmgVhESzqTmzD3YQSlHYhZf7sFtKXryx93E3Bq64H//G1KM O8bg== X-Gm-Message-State: AOJu0YzEaj9Jv3vQWMebRFGBJAn39Ho3xV3WxwRFYUBVjHRofw/7OlgM bECPZBSemKYVfNp7Z/rN1KTAjGZr1A== X-Google-Smtp-Source: AGHT+IEw+uv+zm8RDEBGwRxkANylW/q4pnLNxvXTx2e/IAUJA2oImZKkJ8rFY6JBFveIzVpREN4PUp1ToA== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a25:8203:0:b0:d7e:b82a:ef68 with SMTP id q3-20020a258203000000b00d7eb82aef68mr78275ybk.3.1694208717716; Fri, 08 Sep 2023 14:31:57 -0700 (PDT) Date: Fri, 8 Sep 2023 21:31:47 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230908213148.3610108-1-rmoar@google.com> Subject: [PATCH v2 1/2] kunit: add ability to run tests after boot using debugfs From: Rae Moar To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com, brendan.higgins@linux.dev, sadiyakazi@google.com Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Rae Moar Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Add functionality to run built-in tests after boot by writing to a debugfs file. Add a new debugfs file labeled "run" for each test suite to use for this purpose. As an example, write to the file using the following: echo "any string" > /sys/kernel/debugfs/kunit//run This will trigger the test suite to run and will print results to the kernel log. Note that what you "write" to the debugfs file will not be saved. To guard against running tests concurrently with this feature, add a mutex lock around running kunit. This supports the current practice of not allowing tests to be run concurrently on the same kernel. This functionality may not work for all tests. This new functionality could be used to design a parameter injection feature in the future. Signed-off-by: Rae Moar --- Changes since v1: - Removed second patch as this problem has been fixed - Added Documentation patch - Made changes to work with new dynamically-extending log feature Note that these patches now rely on (and are rebased on) the patch series: https://lore.kernel.org/all/20230828104111.2394344-1-rf@opensource.cirrus.com/ lib/kunit/debugfs.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ lib/kunit/test.c | 13 +++++++++ 2 files changed, 79 insertions(+) base-commit: b754593274e04fc840482a658b29791bc8f8b933 diff --git a/lib/kunit/debugfs.c b/lib/kunit/debugfs.c index 270d185737e6..8c0a970321ce 100644 --- a/lib/kunit/debugfs.c +++ b/lib/kunit/debugfs.c @@ -8,12 +8,14 @@ #include #include +#include #include "string-stream.h" #include "debugfs.h" #define KUNIT_DEBUGFS_ROOT "kunit" #define KUNIT_DEBUGFS_RESULTS "results" +#define KUNIT_DEBUGFS_RUN "run" /* * Create a debugfs representation of test suites: @@ -21,6 +23,8 @@ * Path Semantics * /sys/kernel/debug/kunit//results Show results of last run for * testsuite + * /sys/kernel/debug/kunit//run Write to this file to trigger + * testsuite to run * */ @@ -99,6 +103,51 @@ static int debugfs_results_open(struct inode *inode, struct file *file) return single_open(file, debugfs_print_results, suite); } +/* + * Print a usage message to the debugfs "run" file + * (/sys/kernel/debug/kunit//run) if opened. + */ +static int debugfs_print_run(struct seq_file *seq, void *v) +{ + struct kunit_suite *suite = (struct kunit_suite *)seq->private; + + seq_puts(seq, "Write to this file to trigger the test suite to run.\n"); + seq_printf(seq, "usage: echo \"any string\" > /sys/kernel/debugfs/kunit/%s/run\n", + suite->name); + return 0; +} + +/* + * The debugfs "run" file (/sys/kernel/debug/kunit//run) + * contains no information. Write to the file to trigger the test suite + * to run. + */ +static int debugfs_run_open(struct inode *inode, struct file *file) +{ + struct kunit_suite *suite; + + suite = (struct kunit_suite *)inode->i_private; + + return single_open(file, debugfs_print_run, suite); +} + +/* + * Trigger a test suite to run by writing to the suite's "run" debugfs + * file found at: /sys/kernel/debug/kunit//run + * + * Note: what is written to this file will not be saved. + */ +static ssize_t debugfs_run(struct file *file, + const char __user *buf, size_t count, loff_t *ppos) +{ + struct inode *f_inode = file->f_inode; + struct kunit_suite *suite = (struct kunit_suite *) f_inode->i_private; + + __kunit_test_suites_init(&suite, 1); + + return count; +} + static const struct file_operations debugfs_results_fops = { .open = debugfs_results_open, .read = seq_read, @@ -106,10 +155,23 @@ static const struct file_operations debugfs_results_fops = { .release = debugfs_release, }; +static const struct file_operations debugfs_run_fops = { + .open = debugfs_run_open, + .read = seq_read, + .write = debugfs_run, + .llseek = seq_lseek, + .release = debugfs_release, +}; + void kunit_debugfs_create_suite(struct kunit_suite *suite) { struct kunit_case *test_case; + if (suite->log) { + /* Clear the suite log that's leftover from a previous run. */ + string_stream_clear(suite->log); + return; + } /* Allocate logs before creating debugfs representation. */ suite->log = alloc_string_stream(GFP_KERNEL); string_stream_set_append_newlines(suite->log, true); @@ -124,6 +186,10 @@ void kunit_debugfs_create_suite(struct kunit_suite *suite) debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444, suite->debugfs, suite, &debugfs_results_fops); + + debugfs_create_file(KUNIT_DEBUGFS_RUN, S_IFREG | 0644, + suite->debugfs, + suite, &debugfs_run_fops); } void kunit_debugfs_destroy_suite(struct kunit_suite *suite) diff --git a/lib/kunit/test.c b/lib/kunit/test.c index 651cbda9f250..d376b886d72d 100644 --- a/lib/kunit/test.c +++ b/lib/kunit/test.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,8 @@ #include "string-stream.h" #include "try-catch-impl.h" +static struct mutex kunit_run_lock; + /* * Hook to fail the current test and print an error message to the log. */ @@ -668,6 +671,11 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ return 0; } + /* Use mutex lock to guard against running tests concurrently. */ + if (mutex_lock_interruptible(&kunit_run_lock)) { + pr_err("kunit: test interrupted\n"); + return -EINTR; + } static_branch_inc(&kunit_running); for (i = 0; i < num_suites; i++) { @@ -676,6 +684,7 @@ int __kunit_test_suites_init(struct kunit_suite * const * const suites, int num_ } static_branch_dec(&kunit_running); + mutex_unlock(&kunit_run_lock); return 0; } EXPORT_SYMBOL_GPL(__kunit_test_suites_init); @@ -836,6 +845,10 @@ static int __init kunit_init(void) kunit_install_hooks(); kunit_debugfs_init(); + + /* Initialize lock to guard against running tests concurrently. */ + mutex_init(&kunit_run_lock); + #ifdef CONFIG_MODULES return register_module_notifier(&kunit_mod_nb); #else From patchwork Fri Sep 8 21:31:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rae Moar X-Patchwork-Id: 13377826 X-Patchwork-Delegate: brendanhiggins@google.com 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84A0BEEB573 for ; Fri, 8 Sep 2023 21:32:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343546AbjIHVcL (ORCPT ); Fri, 8 Sep 2023 17:32:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232091AbjIHVcL (ORCPT ); Fri, 8 Sep 2023 17:32:11 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC4EB1FE5 for ; Fri, 8 Sep 2023 14:32:06 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d7e81a07ea3so2536913276.2 for ; Fri, 08 Sep 2023 14:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1694208726; x=1694813526; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=WrSybFetHPfn0OxZQ180UMQcEGO5bVxxCcuiMkmcKr8=; b=S2Jj/pzkU8oCOk/hz+Y71QRYsp6C+ebfO4NY+tZZdl1CJFKorcMp8xonSlPRU1XYRd egZxU58iPzmoA/dAgNglOdxMQLXSXwQFuMvLzCBhaYjT5M+i5Dq9fLgd8KblOj1xvMKy paw7+n3mXP6qbtsjgEkuSDhEBQoRaUhqPAWhPDZmfhkzKIm0neF1RGu2MQH/ZBOuClRJ fTvkhNbWlMoy+QIbHhdKn98CL8UfCveSRDXMy9UAHAut8aXeNMKPapDs2TYCA3Kt4v9S uK/w4VuHBY19ruYNqFFI2iT/QsBcMJ7TBL5/dLF5ZVfoRB0Pf2m8w/CqG+07fWx3xza5 y8Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694208726; x=1694813526; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WrSybFetHPfn0OxZQ180UMQcEGO5bVxxCcuiMkmcKr8=; b=FkDgEhLMexzEpAMduV+OpAOzt9dWAtPXu5YyyorwSDuWgvuhfvxBElCb0Mze3RfpXZ 4pdRM0c4pvwPGHnjZ3yA7b32thSA2M0QjRoASuZadGaYC6/ZBOlGZmmlJ0ZaNW0Z1Nw0 9/AjsUKACgm88X9pSl4veJkLSa4FOzVre2/pZYXoye8dbsCBSDWB+9P6GsFvxDmZCNWv ZH4zvB7vEnHQpAKv6N6u29vY4YbWS0P9cJ+xUtNPOxU7w3Ny3p3b4k3wAE4XszixbUYu NOSBy7neeGz+G1913HMWrFLzCZkW56hKVuDjgG0DQ4SfXhwG/z4jD/Xk7jxG2dvIdLea ra7g== X-Gm-Message-State: AOJu0YxnhemRqpsae2r+8gT1cnOCh32lUQJJ24W9+iTgWzJnBJ3jYj3i D9gHBPQEsE2wb4j6I0KtNxmAIdtbZw== X-Google-Smtp-Source: AGHT+IHG7fQnlP8xDXo7SMz8La6WUcW4q4a4Z3CTA2AhPgnjq1WxXweSG3cbdfplTYuhvZxDVIArjC7iTQ== X-Received: from rmoar-specialist.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:45d3]) (user=rmoar job=sendgmr) by 2002:a25:2749:0:b0:d46:45a1:b775 with SMTP id n70-20020a252749000000b00d4645a1b775mr72079ybn.3.1694208726055; Fri, 08 Sep 2023 14:32:06 -0700 (PDT) Date: Fri, 8 Sep 2023 21:31:48 +0000 In-Reply-To: <20230908213148.3610108-1-rmoar@google.com> Mime-Version: 1.0 References: <20230908213148.3610108-1-rmoar@google.com> X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230908213148.3610108-2-rmoar@google.com> Subject: [PATCH v2 2/2] Documentation: Add debugfs docs with run after boot From: Rae Moar To: shuah@kernel.org, davidgow@google.com, dlatypov@google.com, brendan.higgins@linux.dev, sadiyakazi@google.com Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Rae Moar Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Expand the documentation on the KUnit debugfs filesystem on the run_manual.rst page. Add section describing how to access results using debugfs. Add section describing how to run tests after boot using debugfs. Signed-off-by: Rae Moar Co-developed-by: Sadiya Kazi Signed-off-by: Sadiya Kazi --- Documentation/dev-tools/kunit/run_manual.rst | 45 ++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/Documentation/dev-tools/kunit/run_manual.rst b/Documentation/dev-tools/kunit/run_manual.rst index e7b46421f247..613385c5ba5b 100644 --- a/Documentation/dev-tools/kunit/run_manual.rst +++ b/Documentation/dev-tools/kunit/run_manual.rst @@ -49,9 +49,46 @@ loaded. The results will appear in TAP format in ``dmesg``. +debugfs +======= + +``debugfs`` is a file system that enables user interaction with the files to +make kernel information available to user space. A user can interact with +the debugfs filesystem using a variety of file operations, such as open, +read, and write. + +By default, only the root user has access to the debugfs directory. + +If ``CONFIG_KUNIT_DEBUGFS`` is enabled, you can use KUnit debugfs +filesystem to perform the following actions. + +Retrieve Test Results +===================== + +You can use debugfs to retrieve KUnit test results. The test results are +accessible from the debugfs filesystem in the following read-only file: + +.. code-block :: bash + + /sys/kernel/debug/kunit//results + +The test results are available in KTAP format. + +Run Tests After Kernel Has Booted +================================= + +You can use the debugfs filesystem to trigger built-in tests to run after +boot. To run the test suite, you can use the following command to write to +the ``/sys/kernel/debug/kunit//run`` file: + +.. code-block :: bash + + echo "any string" > /sys/kernel/debugfs/kunit//run + +As a result, the test suite runs and the results are printed to the kernel +log. + .. note :: - If ``CONFIG_KUNIT_DEBUGFS`` is enabled, KUnit test results will - be accessible from the ``debugfs`` filesystem (if mounted). - They will be in ``/sys/kernel/debug/kunit//results``, in - TAP format. + The contents written to the debugfs file + ``/sys/kernel/debug/kunit//run`` are not saved.