xref: /OK3568_Linux_fs/kernel/Documentation/dev-tools/kselftest.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun======================
2*4882a593SmuzhiyunLinux Kernel Selftests
3*4882a593Smuzhiyun======================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunThe kernel contains a set of "self tests" under the tools/testing/selftests/
6*4882a593Smuzhiyundirectory. These are intended to be small tests to exercise individual code
7*4882a593Smuzhiyunpaths in the kernel. Tests are intended to be run after building, installing
8*4882a593Smuzhiyunand booting a kernel.
9*4882a593Smuzhiyun
10*4882a593SmuzhiyunYou can find additional information on Kselftest framework, how to
11*4882a593Smuzhiyunwrite new tests using the framework on Kselftest wiki:
12*4882a593Smuzhiyun
13*4882a593Smuzhiyunhttps://kselftest.wiki.kernel.org/
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunOn some systems, hot-plug tests could hang forever waiting for cpu and
16*4882a593Smuzhiyunmemory to be ready to be offlined. A special hot-plug target is created
17*4882a593Smuzhiyunto run the full range of hot-plug tests. In default mode, hot-plug tests run
18*4882a593Smuzhiyunin safe mode with a limited scope. In limited mode, cpu-hotplug test is
19*4882a593Smuzhiyunrun on a single cpu as opposed to all hotplug capable cpus, and memory
20*4882a593Smuzhiyunhotplug test is run on 2% of hotplug capable memory instead of 10%.
21*4882a593Smuzhiyun
22*4882a593Smuzhiyunkselftest runs as a userspace process.  Tests that can be written/run in
23*4882a593Smuzhiyunuserspace may wish to use the `Test Harness`_.  Tests that need to be
24*4882a593Smuzhiyunrun in kernel space may wish to use a `Test Module`_.
25*4882a593Smuzhiyun
26*4882a593SmuzhiyunRunning the selftests (hotplug tests are run in limited mode)
27*4882a593Smuzhiyun=============================================================
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunTo build the tests::
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun  $ make -C tools/testing/selftests
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunTo run the tests::
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun  $ make -C tools/testing/selftests run_tests
36*4882a593Smuzhiyun
37*4882a593SmuzhiyunTo build and run the tests with a single command, use::
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun  $ make kselftest
40*4882a593Smuzhiyun
41*4882a593SmuzhiyunNote that some tests will require root privileges.
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunKselftest supports saving output files in a separate directory and then
44*4882a593Smuzhiyunrunning tests. To locate output files in a separate directory two syntaxes
45*4882a593Smuzhiyunare supported. In both cases the working directory must be the root of the
46*4882a593Smuzhiyunkernel src. This is applicable to "Running a subset of selftests" section
47*4882a593Smuzhiyunbelow.
48*4882a593Smuzhiyun
49*4882a593SmuzhiyunTo build, save output files in a separate directory with O= ::
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun  $ make O=/tmp/kselftest kselftest
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunTo build, save output files in a separate directory with KBUILD_OUTPUT ::
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun  $ export KBUILD_OUTPUT=/tmp/kselftest; make kselftest
56*4882a593Smuzhiyun
57*4882a593SmuzhiyunThe O= assignment takes precedence over the KBUILD_OUTPUT environment
58*4882a593Smuzhiyunvariable.
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunThe above commands by default run the tests and print full pass/fail report.
61*4882a593SmuzhiyunKselftest supports "summary" option to make it easier to understand the test
62*4882a593Smuzhiyunresults. Please find the detailed individual test results for each test in
63*4882a593Smuzhiyun/tmp/testname file(s) when summary option is specified. This is applicable
64*4882a593Smuzhiyunto "Running a subset of selftests" section below.
65*4882a593Smuzhiyun
66*4882a593SmuzhiyunTo run kselftest with summary option enabled ::
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun  $ make summary=1 kselftest
69*4882a593Smuzhiyun
70*4882a593SmuzhiyunRunning a subset of selftests
71*4882a593Smuzhiyun=============================
72*4882a593Smuzhiyun
73*4882a593SmuzhiyunYou can use the "TARGETS" variable on the make command line to specify
74*4882a593Smuzhiyunsingle test to run, or a list of tests to run.
75*4882a593Smuzhiyun
76*4882a593SmuzhiyunTo run only tests targeted for a single subsystem::
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun  $ make -C tools/testing/selftests TARGETS=ptrace run_tests
79*4882a593Smuzhiyun
80*4882a593SmuzhiyunYou can specify multiple tests to build and run::
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun  $  make TARGETS="size timers" kselftest
83*4882a593Smuzhiyun
84*4882a593SmuzhiyunTo build, save output files in a separate directory with O= ::
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun  $ make O=/tmp/kselftest TARGETS="size timers" kselftest
87*4882a593Smuzhiyun
88*4882a593SmuzhiyunTo build, save output files in a separate directory with KBUILD_OUTPUT ::
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun  $ export KBUILD_OUTPUT=/tmp/kselftest; make TARGETS="size timers" kselftest
91*4882a593Smuzhiyun
92*4882a593SmuzhiyunAdditionally you can use the "SKIP_TARGETS" variable on the make command
93*4882a593Smuzhiyunline to specify one or more targets to exclude from the TARGETS list.
94*4882a593Smuzhiyun
95*4882a593SmuzhiyunTo run all tests but a single subsystem::
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun  $ make -C tools/testing/selftests SKIP_TARGETS=ptrace run_tests
98*4882a593Smuzhiyun
99*4882a593SmuzhiyunYou can specify multiple tests to skip::
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun  $  make SKIP_TARGETS="size timers" kselftest
102*4882a593Smuzhiyun
103*4882a593SmuzhiyunYou can also specify a restricted list of tests to run together with a
104*4882a593Smuzhiyundedicated skiplist::
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun  $  make TARGETS="bpf breakpoints size timers" SKIP_TARGETS=bpf kselftest
107*4882a593Smuzhiyun
108*4882a593SmuzhiyunSee the top-level tools/testing/selftests/Makefile for the list of all
109*4882a593Smuzhiyunpossible targets.
110*4882a593Smuzhiyun
111*4882a593SmuzhiyunRunning the full range hotplug selftests
112*4882a593Smuzhiyun========================================
113*4882a593Smuzhiyun
114*4882a593SmuzhiyunTo build the hotplug tests::
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun  $ make -C tools/testing/selftests hotplug
117*4882a593Smuzhiyun
118*4882a593SmuzhiyunTo run the hotplug tests::
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun  $ make -C tools/testing/selftests run_hotplug
121*4882a593Smuzhiyun
122*4882a593SmuzhiyunNote that some tests will require root privileges.
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun
125*4882a593SmuzhiyunInstall selftests
126*4882a593Smuzhiyun=================
127*4882a593Smuzhiyun
128*4882a593SmuzhiyunYou can use the "install" target of "make" (which calls the `kselftest_install.sh`
129*4882a593Smuzhiyuntool) to install selftests in the default location (`tools/testing/selftests/kselftest_install`),
130*4882a593Smuzhiyunor in a user specified location via the `INSTALL_PATH` "make" variable.
131*4882a593Smuzhiyun
132*4882a593SmuzhiyunTo install selftests in default location::
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun   $ make -C tools/testing/selftests install
135*4882a593Smuzhiyun
136*4882a593SmuzhiyunTo install selftests in a user specified location::
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun   $ make -C tools/testing/selftests install INSTALL_PATH=/some/other/path
139*4882a593Smuzhiyun
140*4882a593SmuzhiyunRunning installed selftests
141*4882a593Smuzhiyun===========================
142*4882a593Smuzhiyun
143*4882a593SmuzhiyunFound in the install directory, as well as in the Kselftest tarball,
144*4882a593Smuzhiyunis a script named `run_kselftest.sh` to run the tests.
145*4882a593Smuzhiyun
146*4882a593SmuzhiyunYou can simply do the following to run the installed Kselftests. Please
147*4882a593Smuzhiyunnote some tests will require root privileges::
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun   $ cd kselftest_install
150*4882a593Smuzhiyun   $ ./run_kselftest.sh
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunTo see the list of available tests, the `-l` option can be used::
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun   $ ./run_kselftest.sh -l
155*4882a593Smuzhiyun
156*4882a593SmuzhiyunThe `-c` option can be used to run all the tests from a test collection, or
157*4882a593Smuzhiyunthe `-t` option for specific single tests. Either can be used multiple times::
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun   $ ./run_kselftest.sh -c bpf -c seccomp -t timers:posix_timers -t timer:nanosleep
160*4882a593Smuzhiyun
161*4882a593SmuzhiyunFor other features see the script usage output, seen with the `-h` option.
162*4882a593Smuzhiyun
163*4882a593SmuzhiyunPackaging selftests
164*4882a593Smuzhiyun===================
165*4882a593Smuzhiyun
166*4882a593SmuzhiyunIn some cases packaging is desired, such as when tests need to run on a
167*4882a593Smuzhiyundifferent system. To package selftests, run::
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun   $ make -C tools/testing/selftests gen_tar
170*4882a593Smuzhiyun
171*4882a593SmuzhiyunThis generates a tarball in the `INSTALL_PATH/kselftest-packages` directory. By
172*4882a593Smuzhiyundefault, `.gz` format is used. The tar compression format can be overridden by
173*4882a593Smuzhiyunspecifying a `FORMAT` make variable. Any value recognized by `tar's auto-compress`_
174*4882a593Smuzhiyunoption is supported, such as::
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun    $ make -C tools/testing/selftests gen_tar FORMAT=.xz
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun`make gen_tar` invokes `make install` so you can use it to package a subset of
179*4882a593Smuzhiyuntests by using variables specified in `Running a subset of selftests`_
180*4882a593Smuzhiyunsection::
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun    $ make -C tools/testing/selftests gen_tar TARGETS="bpf" FORMAT=.xz
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun.. _tar's auto-compress: https://www.gnu.org/software/tar/manual/html_node/gzip.html#auto_002dcompress
185*4882a593Smuzhiyun
186*4882a593SmuzhiyunContributing new tests
187*4882a593Smuzhiyun======================
188*4882a593Smuzhiyun
189*4882a593SmuzhiyunIn general, the rules for selftests are
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun * Do as much as you can if you're not root;
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun * Don't take too long;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun * Don't break the build on any architecture, and
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun * Don't cause the top-level "make run_tests" to fail if your feature is
198*4882a593Smuzhiyun   unconfigured.
199*4882a593Smuzhiyun
200*4882a593SmuzhiyunContributing new tests (details)
201*4882a593Smuzhiyun================================
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun * Use TEST_GEN_XXX if such binaries or files are generated during
204*4882a593Smuzhiyun   compiling.
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun   TEST_PROGS, TEST_GEN_PROGS mean it is the executable tested by
207*4882a593Smuzhiyun   default.
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun   TEST_CUSTOM_PROGS should be used by tests that require custom build
210*4882a593Smuzhiyun   rules and prevent common build rule use.
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun   TEST_PROGS are for test shell scripts. Please ensure shell script has
213*4882a593Smuzhiyun   its exec bit set. Otherwise, lib.mk run_tests will generate a warning.
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun   TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests.
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun   TEST_PROGS_EXTENDED, TEST_GEN_PROGS_EXTENDED mean it is the
218*4882a593Smuzhiyun   executable which is not tested by default.
219*4882a593Smuzhiyun   TEST_FILES, TEST_GEN_FILES mean it is the file which is used by
220*4882a593Smuzhiyun   test.
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun * First use the headers inside the kernel source and/or git repo, and then the
223*4882a593Smuzhiyun   system headers.  Headers for the kernel release as opposed to headers
224*4882a593Smuzhiyun   installed by the distro on the system should be the primary focus to be able
225*4882a593Smuzhiyun   to find regressions.
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun * If a test needs specific kernel config options enabled, add a config file in
228*4882a593Smuzhiyun   the test directory to enable them.
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun   e.g: tools/testing/selftests/android/config
231*4882a593Smuzhiyun
232*4882a593SmuzhiyunTest Module
233*4882a593Smuzhiyun===========
234*4882a593Smuzhiyun
235*4882a593SmuzhiyunKselftest tests the kernel from userspace.  Sometimes things need
236*4882a593Smuzhiyuntesting from within the kernel, one method of doing this is to create a
237*4882a593Smuzhiyuntest module.  We can tie the module into the kselftest framework by
238*4882a593Smuzhiyunusing a shell script test runner.  ``kselftest/module.sh`` is designed
239*4882a593Smuzhiyunto facilitate this process.  There is also a header file provided to
240*4882a593Smuzhiyunassist writing kernel modules that are for use with kselftest:
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun- ``tools/testing/kselftest/kselftest_module.h``
243*4882a593Smuzhiyun- ``tools/testing/kselftest/kselftest/module.sh``
244*4882a593Smuzhiyun
245*4882a593SmuzhiyunHow to use
246*4882a593Smuzhiyun----------
247*4882a593Smuzhiyun
248*4882a593SmuzhiyunHere we show the typical steps to create a test module and tie it into
249*4882a593Smuzhiyunkselftest.  We use kselftests for lib/ as an example.
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun1. Create the test module
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun2. Create the test script that will run (load/unload) the module
254*4882a593Smuzhiyun   e.g. ``tools/testing/selftests/lib/printf.sh``
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun3. Add line to config file e.g. ``tools/testing/selftests/lib/config``
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun4. Add test script to makefile  e.g. ``tools/testing/selftests/lib/Makefile``
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun5. Verify it works:
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun.. code-block:: sh
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun   # Assumes you have booted a fresh build of this kernel tree
265*4882a593Smuzhiyun   cd /path/to/linux/tree
266*4882a593Smuzhiyun   make kselftest-merge
267*4882a593Smuzhiyun   make modules
268*4882a593Smuzhiyun   sudo make modules_install
269*4882a593Smuzhiyun   make TARGETS=lib kselftest
270*4882a593Smuzhiyun
271*4882a593SmuzhiyunExample Module
272*4882a593Smuzhiyun--------------
273*4882a593Smuzhiyun
274*4882a593SmuzhiyunA bare bones test module might look like this:
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun.. code-block:: c
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun   // SPDX-License-Identifier: GPL-2.0+
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun   #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun   #include "../tools/testing/selftests/kselftest/module.h"
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun   KSTM_MODULE_GLOBALS();
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun   /*
287*4882a593Smuzhiyun    * Kernel module for testing the foobinator
288*4882a593Smuzhiyun    */
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun   static int __init test_function()
291*4882a593Smuzhiyun   {
292*4882a593Smuzhiyun           ...
293*4882a593Smuzhiyun   }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun   static void __init selftest(void)
296*4882a593Smuzhiyun   {
297*4882a593Smuzhiyun           KSTM_CHECK_ZERO(do_test_case("", 0));
298*4882a593Smuzhiyun   }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun   KSTM_MODULE_LOADERS(test_foo);
301*4882a593Smuzhiyun   MODULE_AUTHOR("John Developer <jd@fooman.org>");
302*4882a593Smuzhiyun   MODULE_LICENSE("GPL");
303*4882a593Smuzhiyun
304*4882a593SmuzhiyunExample test script
305*4882a593Smuzhiyun-------------------
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun.. code-block:: sh
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun    #!/bin/bash
310*4882a593Smuzhiyun    # SPDX-License-Identifier: GPL-2.0+
311*4882a593Smuzhiyun    $(dirname $0)/../kselftest/module.sh "foo" test_foo
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun
314*4882a593SmuzhiyunTest Harness
315*4882a593Smuzhiyun============
316*4882a593Smuzhiyun
317*4882a593SmuzhiyunThe kselftest_harness.h file contains useful helpers to build tests.  The
318*4882a593Smuzhiyuntest harness is for userspace testing, for kernel space testing see `Test
319*4882a593SmuzhiyunModule`_ above.
320*4882a593Smuzhiyun
321*4882a593SmuzhiyunThe tests from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as
322*4882a593Smuzhiyunexample.
323*4882a593Smuzhiyun
324*4882a593SmuzhiyunExample
325*4882a593Smuzhiyun-------
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun.. kernel-doc:: tools/testing/selftests/kselftest_harness.h
328*4882a593Smuzhiyun    :doc: example
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun
331*4882a593SmuzhiyunHelpers
332*4882a593Smuzhiyun-------
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun.. kernel-doc:: tools/testing/selftests/kselftest_harness.h
335*4882a593Smuzhiyun    :functions: TH_LOG TEST TEST_SIGNAL FIXTURE FIXTURE_DATA FIXTURE_SETUP
336*4882a593Smuzhiyun                FIXTURE_TEARDOWN TEST_F TEST_HARNESS_MAIN FIXTURE_VARIANT
337*4882a593Smuzhiyun                FIXTURE_VARIANT_ADD
338*4882a593Smuzhiyun
339*4882a593SmuzhiyunOperators
340*4882a593Smuzhiyun---------
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun.. kernel-doc:: tools/testing/selftests/kselftest_harness.h
343*4882a593Smuzhiyun    :doc: operators
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun.. kernel-doc:: tools/testing/selftests/kselftest_harness.h
346*4882a593Smuzhiyun    :functions: ASSERT_EQ ASSERT_NE ASSERT_LT ASSERT_LE ASSERT_GT ASSERT_GE
347*4882a593Smuzhiyun                ASSERT_NULL ASSERT_TRUE ASSERT_NULL ASSERT_TRUE ASSERT_FALSE
348*4882a593Smuzhiyun                ASSERT_STREQ ASSERT_STRNE EXPECT_EQ EXPECT_NE EXPECT_LT
349*4882a593Smuzhiyun                EXPECT_LE EXPECT_GT EXPECT_GE EXPECT_NULL EXPECT_TRUE
350*4882a593Smuzhiyun                EXPECT_FALSE EXPECT_STREQ EXPECT_STRNE
351