The Linux kernel contains a set of developer unit and regression tests (Kselftests) under tools/testing/selftests; these tests exercise individual code paths in the kernel. In this blog post, I’ll explain how to build and run these tests, run Kselftest on a system it’s built on, and how to install and run tests on a target test system.
Even though kselftest’s main purpose is for developer regression test, testers and users can also use it to ensure there are no regressions in a new kernel. Kselftest is run everyday on several Linux kernel trees on the 0-Day and kernelci.org Linux kernel integration test rings.
How to Build Kselftest
The tests are intended to be run after building, installing, and booting a kernel.
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git $ cd linux-kselftest $ make all $ sudo make modules_install install
Boot the new kernel, then execute the following
$ cd linux-kselftest
To build the tests: $ make -C tools/testing/selftests To run the tests: $ make -C tools/testing/selftests run_tests To build and run the tests with a single command, use: $ make kselftest
Please note, some tests require root privileges.
You can run a subset of selftests using “TARGETS” make command variable to specify
single test or a list of tests to run.
To run only tests targeted for a single subsystem: $ make -C tools/testing/selftests TARGETS=ptrace run_tests You can specify multiple tests to build and run: $ make TARGETS="size timers" kselftest
See the top-level tools/testing/selftests/Makefile for the list of all possible targets.
You can use kselftest_install.sh tool installs selftests in default location which is tools/testing/selftests/kselftest or an user specified location.
To install selftests in default location: $ cd tools/testing/selftests $ ./kselftest_install.sh To install selftests in an user specified location: $ cd tools/testing/selftests $ ./kselftest_install.sh install_dir
Generate the Kselftest Install Package
cd tools/testing/selftests ./gen_kselftest_tar.sh
The generated Kselftest tarball can be copied to target test system for running tests.
Run Installed Kselftests
Kselftest install as well as the Kselftest tarball provide a script named run_kselftest.sh to run the tests. You can simply do the following to run the installed Kselftests. Please note some tests will require root privileges.
cd kselftest ./run_kselftest.sh
Interpret Kselftest Results
When Kselftest suite is run each test prints out Pass or Fail and the reason for failure. Example results for a few tests in the suite:
Running tests in membarrier ======================================== membarrier MEMBARRIER_CMD_QUERY syscall available. membarrier: MEMBARRIER_CMD_SHARED success. membarrier: tests done! selftests: membarrier_test [PASS] Running tests in memfd ======================================== memfd: CREATE memfd: BASIC memfd: SEAL-WRITE memfd: SEAL-SHRINK memfd: SEAL-GROW memfd: SEAL-RESIZE memfd: SHARE-DUP memfd: SHARE-MMAP memfd: SHARE-OPEN memfd: SHARE-FORK memfd: SHARE-DUP (shared file-table) memfd: SHARE-MMAP (shared file-table) memfd: SHARE-OPEN (shared file-table) memfd: SHARE-FORK (shared file-table) memfd: DONE selftests: memfd_test [PASS]
To learn more about using Kselftest, check out the following resources
- Documentation/kselftest.txt, and
- the Kselftest source files, located in tools/testing/selftests.