Skip to content

Commit

Permalink
Merge tag 'linux-kselftest-kunit-5.20-rc1' of git://git.kernel.org/pu…
Browse files Browse the repository at this point in the history
…b/scm/linux/kernel/git/shuah/linux-kselftest

Pull KUnit updates from Shuah Khan:
 "This consists of several fixes and an important feature to discourage
  running KUnit tests on production systems. Running tests on a
  production system could leave the system in a bad state.

  Summary:

   - Add a new taint type, TAINT_TEST to signal that a test has been
     run.

     This should discourage people from running these tests on
     production systems, and to make it easier to tell if tests have
     been run accidentally (by loading the wrong configuration, etc)

   - Several documentation and tool enhancements and fixes"

* tag 'linux-kselftest-kunit-5.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: (29 commits)
  Documentation: KUnit: Fix example with compilation error
  Documentation: kunit: Add CLI args for kunit_tool
  kcsan: test: Add a .kunitconfig to run KCSAN tests
  kunit: executor: Fix a memory leak on failure in kunit_filter_tests
  clk: explicitly disable CONFIG_UML_PCI_OVER_VIRTIO in .kunitconfig
  mmc: sdhci-of-aspeed: test: Use kunit_test_suite() macro
  nitro_enclaves: test: Use kunit_test_suite() macro
  thunderbolt: test: Use kunit_test_suite() macro
  kunit: flatten kunit_suite*** to kunit_suite** in .kunit_test_suites
  kunit: unify module and builtin suite definitions
  selftest: Taint kernel when test module loaded
  module: panic: Taint the kernel when selftest modules load
  Documentation: kunit: fix example run_kunit func to allow spaces in args
  Documentation: kunit: Cleanup run_wrapper, fix x-ref
  kunit: test.h: fix a kernel-doc markup
  kunit: tool: Enable virtio/PCI by default on UML
  kunit: tool: make --kunitconfig repeatable, blindly concat
  kunit: add coverage_uml.config to enable GCOV on UML
  kunit: tool: refactor internal kconfig handling, allow overriding
  kunit: tool: introduce --qemu_args
  ...
  • Loading branch information
torvalds committed Aug 3, 2022
2 parents aad26f5 + 4c39251 commit 665fe72
Show file tree
Hide file tree
Showing 36 changed files with 657 additions and 592 deletions.
1 change: 1 addition & 0 deletions Documentation/admin-guide/tainted-kernels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Bit Log Number Reason that got the kernel tainted
15 _/K 32768 kernel has been live patched
16 _/X 65536 auxiliary taint, defined for and used by distros
17 _/T 131072 kernel was built with the struct randomization plugin
18 _/N 262144 an in-kernel test has been run
=== === ====== ========================================================

Note: The character ``_`` is representing a blank in this table to make reading
Expand Down
9 changes: 9 additions & 0 deletions Documentation/dev-tools/kselftest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ assist writing kernel modules that are for use with kselftest:
- ``tools/testing/selftests/kselftest_module.h``
- ``tools/testing/selftests/kselftest/module.sh``

Note that test modules should taint the kernel with TAINT_TEST. This will
happen automatically for modules which are in the ``tools/testing/``
directory, or for modules which use the ``kselftest_module.h`` header above.
Otherwise, you'll need to add ``MODULE_INFO(test, "Y")`` to your module
source. selftests which do not load modules typically should not taint the
kernel, but in cases where a non-test module is loaded, TEST_TAINT can be
applied from userspace by writing to ``/proc/sys/kernel/tainted``.

How to use
----------

Expand Down Expand Up @@ -333,6 +341,7 @@ A bare bones test module might look like this:
KSTM_MODULE_LOADERS(test_foo);
MODULE_AUTHOR("John Developer <[email protected]>");
MODULE_LICENSE("GPL");
MODULE_INFO(test, "Y");
Example test script
-------------------
Expand Down
81 changes: 77 additions & 4 deletions Documentation/dev-tools/kunit/run_wrapper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ via UML. To run tests on qemu, by default it requires two flags:
if we have downloaded the microblaze toolchain from the 0-day
website to a directory in our home directory called toolchains.

This means that for most architectures, running under qemu is as simple as:

.. code-block:: bash
./tools/testing/kunit/kunit.py run --arch=x86_64
When cross-compiling, we'll likely need to specify a different toolchain, for
example:

.. code-block:: bash
./tools/testing/kunit/kunit.py run \
--arch=s390 \
--cross_compile=s390x-linux-gnu-
If we want to run KUnit tests on an architecture not supported by
the ``--arch`` flag, or want to run KUnit tests on qemu using a
non-default configuration; then we can write our own``QemuConfig``.
Expand All @@ -214,14 +229,11 @@ as
--jobs=12 \
--qemu_config=./tools/testing/kunit/qemu_configs/x86_64.py
To run existing KUnit tests on non-UML architectures, see:
Documentation/dev-tools/kunit/non_uml.rst.

Command-Line Arguments
======================

kunit_tool has a number of other command-line arguments which can
be useful for our test environment. Below the most commonly used
be useful for our test environment. Below are the most commonly used
command line arguments:

- ``--help``: Lists all available options. To list common options,
Expand All @@ -245,3 +257,64 @@ command line arguments:
added or modified. Instead, enable all tests
which have satisfied dependencies by adding
``CONFIG_KUNIT_ALL_TESTS=y`` to your ``.kunitconfig``.

- ``--kunitconfig``: Specifies the path or the directory of the ``.kunitconfig``
file. For example:

- ``lib/kunit/.kunitconfig`` can be the path of the file.

- ``lib/kunit`` can be the directory in which the file is located.

This file is used to build and run with a predefined set of tests
and their dependencies. For example, to run tests for a given subsystem.

- ``--kconfig_add``: Specifies additional configuration options to be
appended to the ``.kunitconfig`` file. For example:

.. code-block::
./tools/testing/kunit/kunit.py run --kconfig_add CONFIG_KASAN=y

- ``--arch``: Runs tests on the specified architecture. The architecture
argument is same as the Kbuild ARCH environment variable.
For example, i386, x86_64, arm, um, etc. Non-UML architectures run on qemu.
Default is `um`.

- ``--cross_compile``: Specifies the Kbuild toolchain. It passes the
same argument as passed to the ``CROSS_COMPILE`` variable used by
Kbuild. This will be the prefix for the toolchain
binaries such as GCC. For example:

- ``sparc64-linux-gnu-`` if we have the sparc toolchain installed on
our system.

- ``$HOME/toolchains/microblaze/gcc-9.2.0-nolibc/microblaze-linux/bin/microblaze-linux``
if we have downloaded the microblaze toolchain from the 0-day
website to a specified path in our home directory called toolchains.

- ``--qemu_config``: Specifies the path to a file containing a
custom qemu architecture definition. This should be a python file
containing a `QemuArchParams` object.

- ``--qemu_args``: Specifies additional qemu arguments, for example, ``-smp 8``.

- ``--jobs``: Specifies the number of jobs (commands) to run simultaneously.
By default, this is set to the number of cores on your system.

- ``--timeout``: Specifies the maximum number of seconds allowed for all tests to run.
This does not include the time taken to build the tests.

- ``--kernel_args``: Specifies additional kernel command-line arguments. May be repeated.

- ``--run_isolated``: If set, boots the kernel for each individual suite/test.
This is useful for debugging a non-hermetic test, one that
might pass/fail based on what ran before it.

- ``--raw_output``: If set, generates unformatted output from kernel. Possible options are:

- ``all``: To view the full kernel output, use ``--raw_output=all``.

- ``kunit``: This is the default option and filters to KUnit output. Use ``--raw_output`` or ``--raw_output=kunit``.

- ``--json``: If set, stores the test results in a JSON format and prints to `stdout` or
saves to a file if a filename is specified.
5 changes: 2 additions & 3 deletions Documentation/dev-tools/kunit/running_tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It can be handy to create a bash function like:
.. code-block:: bash
function run_kunit() {
( cd "$(git rev-parse --show-toplevel)" && ./tools/testing/kunit/kunit.py run $@ )
( cd "$(git rev-parse --show-toplevel)" && ./tools/testing/kunit/kunit.py run "$@" )
}
.. note::
Expand Down Expand Up @@ -123,8 +123,7 @@ Putting it together into a copy-pastable sequence of commands:
.. code-block:: bash
# Append coverage options to the current config
$ echo -e "CONFIG_DEBUG_KERNEL=y\nCONFIG_DEBUG_INFO=y\nCONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y\nCONFIG_GCOV=y" >> .kunit/.kunitconfig
$ ./tools/testing/kunit/kunit.py run
$ ./tools/testing/kunit/kunit.py run --kunitconfig=.kunit/ --kunitconfig=tools/testing/kunit/configs/coverage_uml.config
# Extract the coverage information from the build dir (.kunit/)
$ lcov -t "my_kunit_tests" -o coverage.info -c -d .kunit/
Expand Down
2 changes: 1 addition & 1 deletion Documentation/dev-tools/kunit/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ By reusing the same ``cases`` array from above, we can write the test as a
const char *str;
const char *sha1;
};
struct sha1_test_case cases[] = {
const struct sha1_test_case cases[] = {
{
.str = "hello world",
.sha1 = "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
Expand Down
1 change: 1 addition & 0 deletions drivers/clk/.kunitconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ CONFIG_KUNIT=y
CONFIG_COMMON_CLK=y
CONFIG_CLK_KUNIT_TEST=y
CONFIG_CLK_GATE_KUNIT_TEST=y
CONFIG_UML_PCI_OVER_VIRTIO=n
5 changes: 3 additions & 2 deletions drivers/mmc/host/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ config MMC_SDHCI_OF_ASPEED
If unsure, say N.

config MMC_SDHCI_OF_ASPEED_TEST
bool "Tests for the ASPEED SDHCI driver"
depends on MMC_SDHCI_OF_ASPEED && KUNIT=y
bool "Tests for the ASPEED SDHCI driver" if !KUNIT_ALL_TESTS
depends on MMC_SDHCI_OF_ASPEED && KUNIT
default KUNIT_ALL_TESTS
help
Enable KUnit tests for the ASPEED SDHCI driver. Select this
option only if you will boot the kernel for the purpose of running
Expand Down
8 changes: 1 addition & 7 deletions drivers/mmc/host/sdhci-of-aspeed-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,4 @@ static struct kunit_suite aspeed_sdhci_test_suite = {
.test_cases = aspeed_sdhci_test_cases,
};

static struct kunit_suite *aspeed_sdc_test_suite_array[] = {
&aspeed_sdhci_test_suite,
NULL,
};

static struct kunit_suite **aspeed_sdc_test_suites
__used __section(".kunit_test_suites") = aspeed_sdc_test_suite_array;
kunit_test_suite(aspeed_sdhci_test_suite);
34 changes: 1 addition & 33 deletions drivers/mmc/host/sdhci-of-aspeed.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,25 +606,6 @@ static struct platform_driver aspeed_sdc_driver = {

#if defined(CONFIG_MMC_SDHCI_OF_ASPEED_TEST)
#include "sdhci-of-aspeed-test.c"

static inline int aspeed_sdc_tests_init(void)
{
return __kunit_test_suites_init(aspeed_sdc_test_suites);
}

static inline void aspeed_sdc_tests_exit(void)
{
__kunit_test_suites_exit(aspeed_sdc_test_suites);
}
#else
static inline int aspeed_sdc_tests_init(void)
{
return 0;
}

static inline void aspeed_sdc_tests_exit(void)
{
}
#endif

static int __init aspeed_sdc_init(void)
Expand All @@ -637,27 +618,14 @@ static int __init aspeed_sdc_init(void)

rc = platform_driver_register(&aspeed_sdc_driver);
if (rc < 0)
goto cleanup_sdhci;

rc = aspeed_sdc_tests_init();
if (rc < 0) {
platform_driver_unregister(&aspeed_sdc_driver);
goto cleanup_sdhci;
}

return 0;

cleanup_sdhci:
platform_driver_unregister(&aspeed_sdhci_driver);
platform_driver_unregister(&aspeed_sdhci_driver);

return rc;
}
module_init(aspeed_sdc_init);

static void __exit aspeed_sdc_exit(void)
{
aspeed_sdc_tests_exit();

platform_driver_unregister(&aspeed_sdc_driver);
platform_driver_unregister(&aspeed_sdhci_driver);
}
Expand Down
6 changes: 4 additions & 2 deletions drivers/thunderbolt/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ config USB4_DEBUGFS_WRITE
this for production systems or distro kernels.

config USB4_KUNIT_TEST
bool "KUnit tests"
depends on KUNIT=y
bool "KUnit tests" if !KUNIT_ALL_TESTS
depends on (USB4=m || KUNIT=y)
depends on KUNIT
default KUNIT_ALL_TESTS

config USB4_DMA_TEST
tristate "DMA traffic test driver"
Expand Down
3 changes: 0 additions & 3 deletions drivers/thunderbolt/domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,6 @@ int tb_domain_init(void)
{
int ret;

tb_test_init();
tb_debugfs_init();
tb_acpi_init();

Expand All @@ -890,7 +889,6 @@ int tb_domain_init(void)
err_acpi:
tb_acpi_exit();
tb_debugfs_exit();
tb_test_exit();

return ret;
}
Expand All @@ -903,5 +901,4 @@ void tb_domain_exit(void)
tb_xdomain_exit();
tb_acpi_exit();
tb_debugfs_exit();
tb_test_exit();
}
8 changes: 0 additions & 8 deletions drivers/thunderbolt/tb.h
Original file line number Diff line number Diff line change
Expand Up @@ -1271,12 +1271,4 @@ static inline void tb_service_debugfs_init(struct tb_service *svc) { }
static inline void tb_service_debugfs_remove(struct tb_service *svc) { }
#endif

#ifdef CONFIG_USB4_KUNIT_TEST
int tb_test_init(void);
void tb_test_exit(void);
#else
static inline int tb_test_init(void) { return 0; }
static inline void tb_test_exit(void) { }
#endif

#endif
12 changes: 1 addition & 11 deletions drivers/thunderbolt/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2817,14 +2817,4 @@ static struct kunit_suite tb_test_suite = {
.test_cases = tb_test_cases,
};

static struct kunit_suite *tb_test_suites[] = { &tb_test_suite, NULL };

int tb_test_init(void)
{
return __kunit_test_suites_init(tb_test_suites);
}

void tb_test_exit(void)
{
return __kunit_test_suites_exit(tb_test_suites);
}
kunit_test_suite(tb_test_suite);
5 changes: 3 additions & 2 deletions drivers/virt/nitro_enclaves/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ config NITRO_ENCLAVES
The module will be called nitro_enclaves.

config NITRO_ENCLAVES_MISC_DEV_TEST
bool "Tests for the misc device functionality of the Nitro Enclaves"
depends on NITRO_ENCLAVES && KUNIT=y
bool "Tests for the misc device functionality of the Nitro Enclaves" if !KUNIT_ALL_TESTS
depends on NITRO_ENCLAVES && KUNIT
default KUNIT_ALL_TESTS
help
Enable KUnit tests for the misc device functionality of the Nitro
Enclaves. Select this option only if you will boot the kernel for
Expand Down
27 changes: 0 additions & 27 deletions drivers/virt/nitro_enclaves/ne_misc_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -1759,35 +1759,10 @@ static long ne_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

#if defined(CONFIG_NITRO_ENCLAVES_MISC_DEV_TEST)
#include "ne_misc_dev_test.c"

static inline int ne_misc_dev_test_init(void)
{
return __kunit_test_suites_init(ne_misc_dev_test_suites);
}

static inline void ne_misc_dev_test_exit(void)
{
__kunit_test_suites_exit(ne_misc_dev_test_suites);
}
#else
static inline int ne_misc_dev_test_init(void)
{
return 0;
}

static inline void ne_misc_dev_test_exit(void)
{
}
#endif

static int __init ne_init(void)
{
int rc = 0;

rc = ne_misc_dev_test_init();
if (rc < 0)
return rc;

mutex_init(&ne_cpu_pool.mutex);

return pci_register_driver(&ne_pci_driver);
Expand All @@ -1798,8 +1773,6 @@ static void __exit ne_exit(void)
pci_unregister_driver(&ne_pci_driver);

ne_teardown_cpu_pool();

ne_misc_dev_test_exit();
}

module_init(ne_init);
Expand Down
5 changes: 1 addition & 4 deletions drivers/virt/nitro_enclaves/ne_misc_dev_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,4 @@ static struct kunit_suite ne_misc_dev_test_suite = {
.test_cases = ne_misc_dev_test_cases,
};

static struct kunit_suite *ne_misc_dev_test_suites[] = {
&ne_misc_dev_test_suite,
NULL
};
kunit_test_suite(ne_misc_dev_test_suite);
Loading

0 comments on commit 665fe72

Please sign in to comment.