An Introduction to Testing the Linux Kernel with Kselftest

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.

Install Kselftest

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]

Additional Resources

To learn more about using Kselftest, check out the following resources

  • Documentation/kselftest.txt, and
  • the Kselftest source files, located in tools/testing/selftests.
  • https://git.kernel.org/cgit/linux/kernel/git/shuah/linux-kselftest.git/

Author: Shuah Khan

Shuah contributes to multiple aspects of the Linux Kernel, and she maintains the Kernel Selftest framework.

4 thoughts on “An Introduction to Testing the Linux Kernel with Kselftest”

  1. Hi,

    I work for Samsung in Bangalore. I am looking for open source tests for linux kernel validation for various IPs. May I know what are the tests available in Kselftest and how different are they compared to LTP or Autotest?

    Regards,

    1. Kselftest is a kernel regression and unit testing suite for kernel developers. These tests are written by kernel developers to regression test kernel features. Unlike LTP and Autotest, Kselftest consists of black and white box tests. Kselftest is part of kernel sources and please find them under tools/testing/selftests. Please find the details on how to run the tests in Documentation/kselftest.txt

  2. Hi,

    I want to run Kselftest in my arm target board. Can you tell me the steps to cross-compile it. And can we use the cross compiled images in my ARM board?

Comments are closed.