Centos-kernel-stream-9/lib/kunit/debugfs.c

228 lines
5.8 KiB
C
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2020, Oracle and/or its affiliates.
* Author: Alan Maguire <alan.maguire@oracle.com>
*/
#include <linux/debugfs.h>
#include <linux/module.h>
#include <kunit/test.h>
#include <kunit/test-bug.h>
#include "string-stream.h"
kunit: include debugfs header file commit 23108f6aac4c86f822db59b89d64da0e70bad5fe Author: Arnd Bergmann <arnd@arndb.de> Date: Wed May 17 15:10:57 2023 +0200 kunit: include debugfs header file An extra #include statement is needed to ensure the prototypes for debugfs interfaces are visible, avoiding this warning: lib/kunit/debugfs.c:28:6: error: no previous prototype for 'kunit_debugfs_cleanup' [-Werror=missing-prototypes] lib/kunit/debugfs.c:33:6: error: no previous prototype for 'kunit_debugfs_init' [-Werror=missing-prototypes] lib/kunit/debugfs.c:102:6: error: no previous prototype for 'kunit_debugfs_create_suite' [-Werror=missing-prototypes] lib/kunit/debugfs.c:118:6: error: no previous prototype for 'kunit_debugfs_destroy_suite' [-Werror=missing-prototypes] Link: https://lkml.kernel.org/r/20230517131102.934196-10-arnd@kernel.org Signed-off-by: Arnd Bergmann <arnd@arndb.de> Reviewed-by: David Gow <davidgow@google.com> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christoph Lameter <cl@linux.com> Cc: Dennis Zhou <dennis@kernel.org> Cc: Eric Paris <eparis@redhat.com> Cc: Heiko Carstens <hca@linux.ibm.com> Cc: Helge Deller <deller@gmx.de> Cc: Ingo Molnar <mingo@redhat.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Michal Simek <monstr@monstr.eu> Cc: Palmer Dabbelt <palmer@dabbelt.com> Cc: Paul Moore <paul@paul-moore.com> Cc: Pavel Machek <pavel@ucw.cz> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Rafael J. Wysocki <rafael@kernel.org> Cc: Russell King <linux@armlinux.org.uk> Cc: Tejun Heo <tj@kernel.org> Cc: Thomas Bogendoerfer <tsbogend@alpha.franken.de> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Waiman Long <longman@redhat.com> Cc: Will Deacon <will@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> JIRA: https://issues.redhat.com/browse/RHEL-5618 Signed-off-by: Nico Pache <npache@redhat.com>
2023-10-16 22:56:58 +00:00
#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:
*
* Path Semantics
* /sys/kernel/debug/kunit/<testsuite>/results Show results of last run for
* testsuite
* /sys/kernel/debug/kunit/<testsuite>/run Write to this file to trigger
* testsuite to run
*
*/
static struct dentry *debugfs_rootdir;
void kunit_debugfs_cleanup(void)
{
debugfs_remove_recursive(debugfs_rootdir);
}
void kunit_debugfs_init(void)
{
if (!debugfs_rootdir)
debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
}
static void debugfs_print_result(struct seq_file *seq, struct string_stream *log)
{
struct string_stream_fragment *frag_container;
if (!log)
return;
/*
* Walk the fragments so we don't need to allocate a temporary
* buffer to hold the entire string.
*/
spin_lock(&log->lock);
list_for_each_entry(frag_container, &log->fragments, node)
seq_printf(seq, "%s", frag_container->fragment);
spin_unlock(&log->lock);
}
/*
* /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
*/
static int debugfs_print_results(struct seq_file *seq, void *v)
{
struct kunit_suite *suite = (struct kunit_suite *)seq->private;
enum kunit_status success;
struct kunit_case *test_case;
kunit: fix bug in the order of lines in debugfs logs commit f9a301c3317daa921375da0aec82462ddf019928 Author: Rae Moar <rmoar@google.com> Date: Wed Mar 8 20:39:51 2023 +0000 kunit: fix bug in the order of lines in debugfs logs Fix bug in debugfs logs that causes an incorrect order of lines in the debugfs log. Currently, the test counts lines that show the number of tests passed, failed, and skipped, as well as any suite diagnostic lines, appear prior to the individual results, which is a bug. Ensure the order of printing for the debugfs log is correct. Additionally, add a KTAP header to so the debugfs logs can be valid KTAP. This is an example of a log prior to these fixes: KTAP version 1 # Subtest: kunit_status 1..2 # kunit_status: pass:2 fail:0 skip:0 total:2 # Totals: pass:2 fail:0 skip:0 total:2 ok 1 kunit_status_set_failure_test ok 2 kunit_status_mark_skipped_test ok 1 kunit_status Note the two lines with stats are out of order. This is the same debugfs log after the fixes (in combination with the third patch to remove the extra line): KTAP version 1 1..1 KTAP version 1 # Subtest: kunit_status 1..2 ok 1 kunit_status_set_failure_test ok 2 kunit_status_mark_skipped_test # kunit_status: pass:2 fail:0 skip:0 total:2 # Totals: pass:2 fail:0 skip:0 total:2 ok 1 kunit_status Signed-off-by: Rae Moar <rmoar@google.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org> JIRA: https://issues.redhat.com/browse/RHEL-5618 Signed-off-by: Nico Pache <npache@redhat.com>
2023-10-16 22:56:59 +00:00
if (!suite)
return 0;
success = kunit_suite_has_succeeded(suite);
kunit: fix bug in the order of lines in debugfs logs commit f9a301c3317daa921375da0aec82462ddf019928 Author: Rae Moar <rmoar@google.com> Date: Wed Mar 8 20:39:51 2023 +0000 kunit: fix bug in the order of lines in debugfs logs Fix bug in debugfs logs that causes an incorrect order of lines in the debugfs log. Currently, the test counts lines that show the number of tests passed, failed, and skipped, as well as any suite diagnostic lines, appear prior to the individual results, which is a bug. Ensure the order of printing for the debugfs log is correct. Additionally, add a KTAP header to so the debugfs logs can be valid KTAP. This is an example of a log prior to these fixes: KTAP version 1 # Subtest: kunit_status 1..2 # kunit_status: pass:2 fail:0 skip:0 total:2 # Totals: pass:2 fail:0 skip:0 total:2 ok 1 kunit_status_set_failure_test ok 2 kunit_status_mark_skipped_test ok 1 kunit_status Note the two lines with stats are out of order. This is the same debugfs log after the fixes (in combination with the third patch to remove the extra line): KTAP version 1 1..1 KTAP version 1 # Subtest: kunit_status 1..2 ok 1 kunit_status_set_failure_test ok 2 kunit_status_mark_skipped_test # kunit_status: pass:2 fail:0 skip:0 total:2 # Totals: pass:2 fail:0 skip:0 total:2 ok 1 kunit_status Signed-off-by: Rae Moar <rmoar@google.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org> JIRA: https://issues.redhat.com/browse/RHEL-5618 Signed-off-by: Nico Pache <npache@redhat.com>
2023-10-16 22:56:59 +00:00
/* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
seq_puts(seq, "KTAP version 1\n");
seq_puts(seq, "1..1\n");
/* Print suite header because it is not stored in the test logs. */
seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
kunit_suite_for_each_test_case(suite, test_case)
debugfs_print_result(seq, test_case->log);
debugfs_print_result(seq, suite->log);
kunit: fix bug in the order of lines in debugfs logs commit f9a301c3317daa921375da0aec82462ddf019928 Author: Rae Moar <rmoar@google.com> Date: Wed Mar 8 20:39:51 2023 +0000 kunit: fix bug in the order of lines in debugfs logs Fix bug in debugfs logs that causes an incorrect order of lines in the debugfs log. Currently, the test counts lines that show the number of tests passed, failed, and skipped, as well as any suite diagnostic lines, appear prior to the individual results, which is a bug. Ensure the order of printing for the debugfs log is correct. Additionally, add a KTAP header to so the debugfs logs can be valid KTAP. This is an example of a log prior to these fixes: KTAP version 1 # Subtest: kunit_status 1..2 # kunit_status: pass:2 fail:0 skip:0 total:2 # Totals: pass:2 fail:0 skip:0 total:2 ok 1 kunit_status_set_failure_test ok 2 kunit_status_mark_skipped_test ok 1 kunit_status Note the two lines with stats are out of order. This is the same debugfs log after the fixes (in combination with the third patch to remove the extra line): KTAP version 1 1..1 KTAP version 1 # Subtest: kunit_status 1..2 ok 1 kunit_status_set_failure_test ok 2 kunit_status_mark_skipped_test # kunit_status: pass:2 fail:0 skip:0 total:2 # Totals: pass:2 fail:0 skip:0 total:2 ok 1 kunit_status Signed-off-by: Rae Moar <rmoar@google.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org> JIRA: https://issues.redhat.com/browse/RHEL-5618 Signed-off-by: Nico Pache <npache@redhat.com>
2023-10-16 22:56:59 +00:00
kunit: improve KTAP compliance of KUnit test output commit 6c738b52316c58ae8a87abf0907f87a7b5e7a109 Author: Rae Moar <rmoar@google.com> Date: Wed Nov 23 18:25:58 2022 +0000 kunit: improve KTAP compliance of KUnit test output Change KUnit test output to better comply with KTAP v1 specifications found here: https://kernel.org/doc/html/latest/dev-tools/ktap.html. 1) Use "KTAP version 1" instead of "TAP version 14" as test output header 2) Remove '-' between test number and test name on test result lines 2) Add KTAP version lines to each subtest header as well Note that the new KUnit output still includes the “# Subtest” line now located after the KTAP version line. This does not completely match the KTAP v1 spec but since it is classified as a diagnostic line, it is not expected to be disruptive or break any existing parsers. This “# Subtest” line comes from the TAP 14 spec (https://testanything.org/tap-version-14-specification.html) and it is used to define the test name before the results. Original output: TAP version 14 1..1 # Subtest: kunit-test-suite 1..3 ok 1 - kunit_test_1 ok 2 - kunit_test_2 ok 3 - kunit_test_3 # kunit-test-suite: pass:3 fail:0 skip:0 total:3 # Totals: pass:3 fail:0 skip:0 total:3 ok 1 - kunit-test-suite New output: KTAP version 1 1..1 KTAP version 1 # Subtest: kunit-test-suite 1..3 ok 1 kunit_test_1 ok 2 kunit_test_2 ok 3 kunit_test_3 # kunit-test-suite: pass:3 fail:0 skip:0 total:3 # Totals: pass:3 fail:0 skip:0 total:3 ok 1 kunit-test-suite Signed-off-by: Rae Moar <rmoar@google.com> Reviewed-by: Daniel Latypov <dlatypov@google.com> Reviewed-by: David Gow <davidgow@google.com> Tested-by: Anders Roxell <anders.roxell@linaro.org> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org> JIRA: https://issues.redhat.com/browse/RHEL-5618 Signed-off-by: Nico Pache <npache@redhat.com>
2023-10-16 22:57:00 +00:00
seq_printf(seq, "%s %d %s\n",
kunit_status_to_ok_not_ok(success), 1, suite->name);
return 0;
}
static int debugfs_release(struct inode *inode, struct file *file)
{
return single_release(inode, file);
}
static int debugfs_results_open(struct inode *inode, struct file *file)
{
struct kunit_suite *suite;
suite = (struct kunit_suite *)inode->i_private;
return single_open(file, debugfs_print_results, suite);
}
/*
* Print a usage message to the debugfs "run" file
* (/sys/kernel/debug/kunit/<testsuite>/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/<testsuite>/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/<testsuite>/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,
.llseek = seq_lseek,
.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;
struct string_stream *stream;
/* If suite log already allocated, do not create new debugfs files. */
if (suite->log)
return;
/*
* Allocate logs before creating debugfs representation.
* The suite->log and test_case->log pointer are expected to be NULL
* if there isn't a log, so only set it if the log stream was created
* successfully.
*/
stream = alloc_string_stream(GFP_KERNEL);
if (IS_ERR_OR_NULL(stream))
return;
string_stream_set_append_newlines(stream, true);
suite->log = stream;
kunit_suite_for_each_test_case(suite, test_case) {
stream = alloc_string_stream(GFP_KERNEL);
if (IS_ERR_OR_NULL(stream))
goto err;
string_stream_set_append_newlines(stream, true);
test_case->log = stream;
}
suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
suite->debugfs,
suite, &debugfs_results_fops);
/* Do not create file to re-run test if test runs on init */
if (!suite->is_init) {
debugfs_create_file(KUNIT_DEBUGFS_RUN, S_IFREG | 0644,
suite->debugfs,
suite, &debugfs_run_fops);
}
return;
err:
string_stream_destroy(suite->log);
kunit_suite_for_each_test_case(suite, test_case)
string_stream_destroy(test_case->log);
}
void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
{
struct kunit_case *test_case;
debugfs_remove_recursive(suite->debugfs);
string_stream_destroy(suite->log);
kunit_suite_for_each_test_case(suite, test_case)
string_stream_destroy(test_case->log);
}