0001 ======================
0002 Linux Kernel Selftests
0003 ======================
0004
0005 The kernel contains a set of "self tests" under the tools/testing/selftests/
0006 directory. These are intended to be small tests to exercise individual code
0007 paths in the kernel. Tests are intended to be run after building, installing
0008 and booting a kernel.
0009
0010 Kselftest from mainline can be run on older stable kernels. Running tests
0011 from mainline offers the best coverage. Several test rings run mainline
0012 kselftest suite on stable releases. The reason is that when a new test
0013 gets added to test existing code to regression test a bug, we should be
0014 able to run that test on an older kernel. Hence, it is important to keep
0015 code that can still test an older kernel and make sure it skips the test
0016 gracefully on newer releases.
0017
0018 You can find additional information on Kselftest framework, how to
0019 write new tests using the framework on Kselftest wiki:
0020
0021 https://kselftest.wiki.kernel.org/
0022
0023 On some systems, hot-plug tests could hang forever waiting for cpu and
0024 memory to be ready to be offlined. A special hot-plug target is created
0025 to run the full range of hot-plug tests. In default mode, hot-plug tests run
0026 in safe mode with a limited scope. In limited mode, cpu-hotplug test is
0027 run on a single cpu as opposed to all hotplug capable cpus, and memory
0028 hotplug test is run on 2% of hotplug capable memory instead of 10%.
0029
0030 kselftest runs as a userspace process. Tests that can be written/run in
0031 userspace may wish to use the `Test Harness`_. Tests that need to be
0032 run in kernel space may wish to use a `Test Module`_.
0033
0034 Running the selftests (hotplug tests are run in limited mode)
0035 =============================================================
0036
0037 To build the tests::
0038
0039 $ make -C tools/testing/selftests
0040
0041 To run the tests::
0042
0043 $ make -C tools/testing/selftests run_tests
0044
0045 To build and run the tests with a single command, use::
0046
0047 $ make kselftest
0048
0049 Note that some tests will require root privileges.
0050
0051 Kselftest supports saving output files in a separate directory and then
0052 running tests. To locate output files in a separate directory two syntaxes
0053 are supported. In both cases the working directory must be the root of the
0054 kernel src. This is applicable to "Running a subset of selftests" section
0055 below.
0056
0057 To build, save output files in a separate directory with O= ::
0058
0059 $ make O=/tmp/kselftest kselftest
0060
0061 To build, save output files in a separate directory with KBUILD_OUTPUT ::
0062
0063 $ export KBUILD_OUTPUT=/tmp/kselftest; make kselftest
0064
0065 The O= assignment takes precedence over the KBUILD_OUTPUT environment
0066 variable.
0067
0068 The above commands by default run the tests and print full pass/fail report.
0069 Kselftest supports "summary" option to make it easier to understand the test
0070 results. Please find the detailed individual test results for each test in
0071 /tmp/testname file(s) when summary option is specified. This is applicable
0072 to "Running a subset of selftests" section below.
0073
0074 To run kselftest with summary option enabled ::
0075
0076 $ make summary=1 kselftest
0077
0078 Running a subset of selftests
0079 =============================
0080
0081 You can use the "TARGETS" variable on the make command line to specify
0082 single test to run, or a list of tests to run.
0083
0084 To run only tests targeted for a single subsystem::
0085
0086 $ make -C tools/testing/selftests TARGETS=ptrace run_tests
0087
0088 You can specify multiple tests to build and run::
0089
0090 $ make TARGETS="size timers" kselftest
0091
0092 To build, save output files in a separate directory with O= ::
0093
0094 $ make O=/tmp/kselftest TARGETS="size timers" kselftest
0095
0096 To build, save output files in a separate directory with KBUILD_OUTPUT ::
0097
0098 $ export KBUILD_OUTPUT=/tmp/kselftest; make TARGETS="size timers" kselftest
0099
0100 Additionally you can use the "SKIP_TARGETS" variable on the make command
0101 line to specify one or more targets to exclude from the TARGETS list.
0102
0103 To run all tests but a single subsystem::
0104
0105 $ make -C tools/testing/selftests SKIP_TARGETS=ptrace run_tests
0106
0107 You can specify multiple tests to skip::
0108
0109 $ make SKIP_TARGETS="size timers" kselftest
0110
0111 You can also specify a restricted list of tests to run together with a
0112 dedicated skiplist::
0113
0114 $ make TARGETS="bpf breakpoints size timers" SKIP_TARGETS=bpf kselftest
0115
0116 See the top-level tools/testing/selftests/Makefile for the list of all
0117 possible targets.
0118
0119 Running the full range hotplug selftests
0120 ========================================
0121
0122 To build the hotplug tests::
0123
0124 $ make -C tools/testing/selftests hotplug
0125
0126 To run the hotplug tests::
0127
0128 $ make -C tools/testing/selftests run_hotplug
0129
0130 Note that some tests will require root privileges.
0131
0132
0133 Install selftests
0134 =================
0135
0136 You can use the "install" target of "make" (which calls the `kselftest_install.sh`
0137 tool) to install selftests in the default location (`tools/testing/selftests/kselftest_install`),
0138 or in a user specified location via the `INSTALL_PATH` "make" variable.
0139
0140 To install selftests in default location::
0141
0142 $ make -C tools/testing/selftests install
0143
0144 To install selftests in a user specified location::
0145
0146 $ make -C tools/testing/selftests install INSTALL_PATH=/some/other/path
0147
0148 Running installed selftests
0149 ===========================
0150
0151 Found in the install directory, as well as in the Kselftest tarball,
0152 is a script named `run_kselftest.sh` to run the tests.
0153
0154 You can simply do the following to run the installed Kselftests. Please
0155 note some tests will require root privileges::
0156
0157 $ cd kselftest_install
0158 $ ./run_kselftest.sh
0159
0160 To see the list of available tests, the `-l` option can be used::
0161
0162 $ ./run_kselftest.sh -l
0163
0164 The `-c` option can be used to run all the tests from a test collection, or
0165 the `-t` option for specific single tests. Either can be used multiple times::
0166
0167 $ ./run_kselftest.sh -c bpf -c seccomp -t timers:posix_timers -t timer:nanosleep
0168
0169 For other features see the script usage output, seen with the `-h` option.
0170
0171 Packaging selftests
0172 ===================
0173
0174 In some cases packaging is desired, such as when tests need to run on a
0175 different system. To package selftests, run::
0176
0177 $ make -C tools/testing/selftests gen_tar
0178
0179 This generates a tarball in the `INSTALL_PATH/kselftest-packages` directory. By
0180 default, `.gz` format is used. The tar compression format can be overridden by
0181 specifying a `FORMAT` make variable. Any value recognized by `tar's auto-compress`_
0182 option is supported, such as::
0183
0184 $ make -C tools/testing/selftests gen_tar FORMAT=.xz
0185
0186 `make gen_tar` invokes `make install` so you can use it to package a subset of
0187 tests by using variables specified in `Running a subset of selftests`_
0188 section::
0189
0190 $ make -C tools/testing/selftests gen_tar TARGETS="bpf" FORMAT=.xz
0191
0192 .. _tar's auto-compress: https://www.gnu.org/software/tar/manual/html_node/gzip.html#auto_002dcompress
0193
0194 Contributing new tests
0195 ======================
0196
0197 In general, the rules for selftests are
0198
0199 * Do as much as you can if you're not root;
0200
0201 * Don't take too long;
0202
0203 * Don't break the build on any architecture, and
0204
0205 * Don't cause the top-level "make run_tests" to fail if your feature is
0206 unconfigured.
0207
0208 Contributing new tests (details)
0209 ================================
0210
0211 * In your Makefile, use facilities from lib.mk by including it instead of
0212 reinventing the wheel. Specify flags and binaries generation flags on
0213 need basis before including lib.mk. ::
0214
0215 CFLAGS = $(KHDR_INCLUDES)
0216 TEST_GEN_PROGS := close_range_test
0217 include ../lib.mk
0218
0219 * Use TEST_GEN_XXX if such binaries or files are generated during
0220 compiling.
0221
0222 TEST_PROGS, TEST_GEN_PROGS mean it is the executable tested by
0223 default.
0224
0225 TEST_CUSTOM_PROGS should be used by tests that require custom build
0226 rules and prevent common build rule use.
0227
0228 TEST_PROGS are for test shell scripts. Please ensure shell script has
0229 its exec bit set. Otherwise, lib.mk run_tests will generate a warning.
0230
0231 TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests.
0232
0233 TEST_PROGS_EXTENDED, TEST_GEN_PROGS_EXTENDED mean it is the
0234 executable which is not tested by default.
0235 TEST_FILES, TEST_GEN_FILES mean it is the file which is used by
0236 test.
0237
0238 * First use the headers inside the kernel source and/or git repo, and then the
0239 system headers. Headers for the kernel release as opposed to headers
0240 installed by the distro on the system should be the primary focus to be able
0241 to find regressions. Use KHDR_INCLUDES in Makefile to include headers from
0242 the kernel source.
0243
0244 * If a test needs specific kernel config options enabled, add a config file in
0245 the test directory to enable them.
0246
0247 e.g: tools/testing/selftests/android/config
0248
0249 * Create a .gitignore file inside test directory and add all generated objects
0250 in it.
0251
0252 * Add new test name in TARGETS in selftests/Makefile::
0253
0254 TARGETS += android
0255
0256 * All changes should pass::
0257
0258 kselftest-{all,install,clean,gen_tar}
0259 kselftest-{all,install,clean,gen_tar} O=abo_path
0260 kselftest-{all,install,clean,gen_tar} O=rel_path
0261 make -C tools/testing/selftests {all,install,clean,gen_tar}
0262 make -C tools/testing/selftests {all,install,clean,gen_tar} O=abs_path
0263 make -C tools/testing/selftests {all,install,clean,gen_tar} O=rel_path
0264
0265 Test Module
0266 ===========
0267
0268 Kselftest tests the kernel from userspace. Sometimes things need
0269 testing from within the kernel, one method of doing this is to create a
0270 test module. We can tie the module into the kselftest framework by
0271 using a shell script test runner. ``kselftest/module.sh`` is designed
0272 to facilitate this process. There is also a header file provided to
0273 assist writing kernel modules that are for use with kselftest:
0274
0275 - ``tools/testing/selftests/kselftest_module.h``
0276 - ``tools/testing/selftests/kselftest/module.sh``
0277
0278 Note that test modules should taint the kernel with TAINT_TEST. This will
0279 happen automatically for modules which are in the ``tools/testing/``
0280 directory, or for modules which use the ``kselftest_module.h`` header above.
0281 Otherwise, you'll need to add ``MODULE_INFO(test, "Y")`` to your module
0282 source. selftests which do not load modules typically should not taint the
0283 kernel, but in cases where a non-test module is loaded, TEST_TAINT can be
0284 applied from userspace by writing to ``/proc/sys/kernel/tainted``.
0285
0286 How to use
0287 ----------
0288
0289 Here we show the typical steps to create a test module and tie it into
0290 kselftest. We use kselftests for lib/ as an example.
0291
0292 1. Create the test module
0293
0294 2. Create the test script that will run (load/unload) the module
0295 e.g. ``tools/testing/selftests/lib/printf.sh``
0296
0297 3. Add line to config file e.g. ``tools/testing/selftests/lib/config``
0298
0299 4. Add test script to makefile e.g. ``tools/testing/selftests/lib/Makefile``
0300
0301 5. Verify it works:
0302
0303 .. code-block:: sh
0304
0305 # Assumes you have booted a fresh build of this kernel tree
0306 cd /path/to/linux/tree
0307 make kselftest-merge
0308 make modules
0309 sudo make modules_install
0310 make TARGETS=lib kselftest
0311
0312 Example Module
0313 --------------
0314
0315 A bare bones test module might look like this:
0316
0317 .. code-block:: c
0318
0319 // SPDX-License-Identifier: GPL-2.0+
0320
0321 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0322
0323 #include "../tools/testing/selftests/kselftest/module.h"
0324
0325 KSTM_MODULE_GLOBALS();
0326
0327 /*
0328 * Kernel module for testing the foobinator
0329 */
0330
0331 static int __init test_function()
0332 {
0333 ...
0334 }
0335
0336 static void __init selftest(void)
0337 {
0338 KSTM_CHECK_ZERO(do_test_case("", 0));
0339 }
0340
0341 KSTM_MODULE_LOADERS(test_foo);
0342 MODULE_AUTHOR("John Developer <jd@fooman.org>");
0343 MODULE_LICENSE("GPL");
0344 MODULE_INFO(test, "Y");
0345
0346 Example test script
0347 -------------------
0348
0349 .. code-block:: sh
0350
0351 #!/bin/bash
0352 # SPDX-License-Identifier: GPL-2.0+
0353 $(dirname $0)/../kselftest/module.sh "foo" test_foo
0354
0355
0356 Test Harness
0357 ============
0358
0359 The kselftest_harness.h file contains useful helpers to build tests. The
0360 test harness is for userspace testing, for kernel space testing see `Test
0361 Module`_ above.
0362
0363 The tests from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as
0364 example.
0365
0366 Example
0367 -------
0368
0369 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
0370 :doc: example
0371
0372
0373 Helpers
0374 -------
0375
0376 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
0377 :functions: TH_LOG TEST TEST_SIGNAL FIXTURE FIXTURE_DATA FIXTURE_SETUP
0378 FIXTURE_TEARDOWN TEST_F TEST_HARNESS_MAIN FIXTURE_VARIANT
0379 FIXTURE_VARIANT_ADD
0380
0381 Operators
0382 ---------
0383
0384 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
0385 :doc: operators
0386
0387 .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
0388 :functions: ASSERT_EQ ASSERT_NE ASSERT_LT ASSERT_LE ASSERT_GT ASSERT_GE
0389 ASSERT_NULL ASSERT_TRUE ASSERT_NULL ASSERT_TRUE ASSERT_FALSE
0390 ASSERT_STREQ ASSERT_STRNE EXPECT_EQ EXPECT_NE EXPECT_LT
0391 EXPECT_LE EXPECT_GT EXPECT_GE EXPECT_NULL EXPECT_TRUE
0392 EXPECT_FALSE EXPECT_STREQ EXPECT_STRNE