kselftest.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. ======================
  2. Linux Kernel Selftests
  3. ======================
  4. The kernel contains a set of "self tests" under the tools/testing/selftests/
  5. directory. These are intended to be small tests to exercise individual code
  6. paths in the kernel. Tests are intended to be run after building, installing
  7. and booting a kernel.
  8. Kselftest from mainline can be run on older stable kernels. Running tests
  9. from mainline offers the best coverage. Several test rings run mainline
  10. kselftest suite on stable releases. The reason is that when a new test
  11. gets added to test existing code to regression test a bug, we should be
  12. able to run that test on an older kernel. Hence, it is important to keep
  13. code that can still test an older kernel and make sure it skips the test
  14. gracefully on newer releases.
  15. You can find additional information on Kselftest framework, how to
  16. write new tests using the framework on Kselftest wiki:
  17. https://kselftest.wiki.kernel.org/
  18. On some systems, hot-plug tests could hang forever waiting for cpu and
  19. memory to be ready to be offlined. A special hot-plug target is created
  20. to run the full range of hot-plug tests. In default mode, hot-plug tests run
  21. in safe mode with a limited scope. In limited mode, cpu-hotplug test is
  22. run on a single cpu as opposed to all hotplug capable cpus, and memory
  23. hotplug test is run on 2% of hotplug capable memory instead of 10%.
  24. kselftest runs as a userspace process. Tests that can be written/run in
  25. userspace may wish to use the `Test Harness`_. Tests that need to be
  26. run in kernel space may wish to use a `Test Module`_.
  27. Running the selftests (hotplug tests are run in limited mode)
  28. =============================================================
  29. To build the tests::
  30. $ make -C tools/testing/selftests
  31. To run the tests::
  32. $ make -C tools/testing/selftests run_tests
  33. To build and run the tests with a single command, use::
  34. $ make kselftest
  35. Note that some tests will require root privileges.
  36. Kselftest supports saving output files in a separate directory and then
  37. running tests. To locate output files in a separate directory two syntaxes
  38. are supported. In both cases the working directory must be the root of the
  39. kernel src. This is applicable to "Running a subset of selftests" section
  40. below.
  41. To build, save output files in a separate directory with O= ::
  42. $ make O=/tmp/kselftest kselftest
  43. To build, save output files in a separate directory with KBUILD_OUTPUT ::
  44. $ export KBUILD_OUTPUT=/tmp/kselftest; make kselftest
  45. The O= assignment takes precedence over the KBUILD_OUTPUT environment
  46. variable.
  47. The above commands by default run the tests and print full pass/fail report.
  48. Kselftest supports "summary" option to make it easier to understand the test
  49. results. Please find the detailed individual test results for each test in
  50. /tmp/testname file(s) when summary option is specified. This is applicable
  51. to "Running a subset of selftests" section below.
  52. To run kselftest with summary option enabled ::
  53. $ make summary=1 kselftest
  54. Running a subset of selftests
  55. =============================
  56. You can use the "TARGETS" variable on the make command line to specify
  57. single test to run, or a list of tests to run.
  58. To run only tests targeted for a single subsystem::
  59. $ make -C tools/testing/selftests TARGETS=ptrace run_tests
  60. You can specify multiple tests to build and run::
  61. $ make TARGETS="size timers" kselftest
  62. To build, save output files in a separate directory with O= ::
  63. $ make O=/tmp/kselftest TARGETS="size timers" kselftest
  64. To build, save output files in a separate directory with KBUILD_OUTPUT ::
  65. $ export KBUILD_OUTPUT=/tmp/kselftest; make TARGETS="size timers" kselftest
  66. Additionally you can use the "SKIP_TARGETS" variable on the make command
  67. line to specify one or more targets to exclude from the TARGETS list.
  68. To run all tests but a single subsystem::
  69. $ make -C tools/testing/selftests SKIP_TARGETS=ptrace run_tests
  70. You can specify multiple tests to skip::
  71. $ make SKIP_TARGETS="size timers" kselftest
  72. You can also specify a restricted list of tests to run together with a
  73. dedicated skiplist::
  74. $ make TARGETS="bpf breakpoints size timers" SKIP_TARGETS=bpf kselftest
  75. See the top-level tools/testing/selftests/Makefile for the list of all
  76. possible targets.
  77. Running the full range hotplug selftests
  78. ========================================
  79. To build the hotplug tests::
  80. $ make -C tools/testing/selftests hotplug
  81. To run the hotplug tests::
  82. $ make -C tools/testing/selftests run_hotplug
  83. Note that some tests will require root privileges.
  84. Install selftests
  85. =================
  86. You can use the "install" target of "make" (which calls the `kselftest_install.sh`
  87. tool) to install selftests in the default location (`tools/testing/selftests/kselftest_install`),
  88. or in a user specified location via the `INSTALL_PATH` "make" variable.
  89. To install selftests in default location::
  90. $ make -C tools/testing/selftests install
  91. To install selftests in a user specified location::
  92. $ make -C tools/testing/selftests install INSTALL_PATH=/some/other/path
  93. Running installed selftests
  94. ===========================
  95. Found in the install directory, as well as in the Kselftest tarball,
  96. is a script named `run_kselftest.sh` to run the tests.
  97. You can simply do the following to run the installed Kselftests. Please
  98. note some tests will require root privileges::
  99. $ cd kselftest_install
  100. $ ./run_kselftest.sh
  101. To see the list of available tests, the `-l` option can be used::
  102. $ ./run_kselftest.sh -l
  103. The `-c` option can be used to run all the tests from a test collection, or
  104. the `-t` option for specific single tests. Either can be used multiple times::
  105. $ ./run_kselftest.sh -c bpf -c seccomp -t timers:posix_timers -t timer:nanosleep
  106. For other features see the script usage output, seen with the `-h` option.
  107. Packaging selftests
  108. ===================
  109. In some cases packaging is desired, such as when tests need to run on a
  110. different system. To package selftests, run::
  111. $ make -C tools/testing/selftests gen_tar
  112. This generates a tarball in the `INSTALL_PATH/kselftest-packages` directory. By
  113. default, `.gz` format is used. The tar compression format can be overridden by
  114. specifying a `FORMAT` make variable. Any value recognized by `tar's auto-compress`_
  115. option is supported, such as::
  116. $ make -C tools/testing/selftests gen_tar FORMAT=.xz
  117. `make gen_tar` invokes `make install` so you can use it to package a subset of
  118. tests by using variables specified in `Running a subset of selftests`_
  119. section::
  120. $ make -C tools/testing/selftests gen_tar TARGETS="bpf" FORMAT=.xz
  121. .. _tar's auto-compress: https://www.gnu.org/software/tar/manual/html_node/gzip.html#auto_002dcompress
  122. Contributing new tests
  123. ======================
  124. In general, the rules for selftests are
  125. * Do as much as you can if you're not root;
  126. * Don't take too long;
  127. * Don't break the build on any architecture, and
  128. * Don't cause the top-level "make run_tests" to fail if your feature is
  129. unconfigured.
  130. Contributing new tests (details)
  131. ================================
  132. * In your Makefile, use facilities from lib.mk by including it instead of
  133. reinventing the wheel. Specify flags and binaries generation flags on
  134. need basis before including lib.mk. ::
  135. CFLAGS = $(KHDR_INCLUDES)
  136. TEST_GEN_PROGS := close_range_test
  137. include ../lib.mk
  138. * Use TEST_GEN_XXX if such binaries or files are generated during
  139. compiling.
  140. TEST_PROGS, TEST_GEN_PROGS mean it is the executable tested by
  141. default.
  142. TEST_CUSTOM_PROGS should be used by tests that require custom build
  143. rules and prevent common build rule use.
  144. TEST_PROGS are for test shell scripts. Please ensure shell script has
  145. its exec bit set. Otherwise, lib.mk run_tests will generate a warning.
  146. TEST_CUSTOM_PROGS and TEST_PROGS will be run by common run_tests.
  147. TEST_PROGS_EXTENDED, TEST_GEN_PROGS_EXTENDED mean it is the
  148. executable which is not tested by default.
  149. TEST_FILES, TEST_GEN_FILES mean it is the file which is used by
  150. test.
  151. * First use the headers inside the kernel source and/or git repo, and then the
  152. system headers. Headers for the kernel release as opposed to headers
  153. installed by the distro on the system should be the primary focus to be able
  154. to find regressions. Use KHDR_INCLUDES in Makefile to include headers from
  155. the kernel source.
  156. * If a test needs specific kernel config options enabled, add a config file in
  157. the test directory to enable them.
  158. e.g: tools/testing/selftests/android/config
  159. * Create a .gitignore file inside test directory and add all generated objects
  160. in it.
  161. * Add new test name in TARGETS in selftests/Makefile::
  162. TARGETS += android
  163. * All changes should pass::
  164. kselftest-{all,install,clean,gen_tar}
  165. kselftest-{all,install,clean,gen_tar} O=abo_path
  166. kselftest-{all,install,clean,gen_tar} O=rel_path
  167. make -C tools/testing/selftests {all,install,clean,gen_tar}
  168. make -C tools/testing/selftests {all,install,clean,gen_tar} O=abs_path
  169. make -C tools/testing/selftests {all,install,clean,gen_tar} O=rel_path
  170. Test Module
  171. ===========
  172. Kselftest tests the kernel from userspace. Sometimes things need
  173. testing from within the kernel, one method of doing this is to create a
  174. test module. We can tie the module into the kselftest framework by
  175. using a shell script test runner. ``kselftest/module.sh`` is designed
  176. to facilitate this process. There is also a header file provided to
  177. assist writing kernel modules that are for use with kselftest:
  178. - ``tools/testing/selftests/kselftest_module.h``
  179. - ``tools/testing/selftests/kselftest/module.sh``
  180. Note that test modules should taint the kernel with TAINT_TEST. This will
  181. happen automatically for modules which are in the ``tools/testing/``
  182. directory, or for modules which use the ``kselftest_module.h`` header above.
  183. Otherwise, you'll need to add ``MODULE_INFO(test, "Y")`` to your module
  184. source. selftests which do not load modules typically should not taint the
  185. kernel, but in cases where a non-test module is loaded, TEST_TAINT can be
  186. applied from userspace by writing to ``/proc/sys/kernel/tainted``.
  187. How to use
  188. ----------
  189. Here we show the typical steps to create a test module and tie it into
  190. kselftest. We use kselftests for lib/ as an example.
  191. 1. Create the test module
  192. 2. Create the test script that will run (load/unload) the module
  193. e.g. ``tools/testing/selftests/lib/printf.sh``
  194. 3. Add line to config file e.g. ``tools/testing/selftests/lib/config``
  195. 4. Add test script to makefile e.g. ``tools/testing/selftests/lib/Makefile``
  196. 5. Verify it works:
  197. .. code-block:: sh
  198. # Assumes you have booted a fresh build of this kernel tree
  199. cd /path/to/linux/tree
  200. make kselftest-merge
  201. make modules
  202. sudo make modules_install
  203. make TARGETS=lib kselftest
  204. Example Module
  205. --------------
  206. A bare bones test module might look like this:
  207. .. code-block:: c
  208. // SPDX-License-Identifier: GPL-2.0+
  209. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  210. #include "../tools/testing/selftests/kselftest_module.h"
  211. KSTM_MODULE_GLOBALS();
  212. /*
  213. * Kernel module for testing the foobinator
  214. */
  215. static int __init test_function()
  216. {
  217. ...
  218. }
  219. static void __init selftest(void)
  220. {
  221. KSTM_CHECK_ZERO(do_test_case("", 0));
  222. }
  223. KSTM_MODULE_LOADERS(test_foo);
  224. MODULE_AUTHOR("John Developer <[email protected]>");
  225. MODULE_LICENSE("GPL");
  226. MODULE_INFO(test, "Y");
  227. Example test script
  228. -------------------
  229. .. code-block:: sh
  230. #!/bin/bash
  231. # SPDX-License-Identifier: GPL-2.0+
  232. $(dirname $0)/../kselftest/module.sh "foo" test_foo
  233. Test Harness
  234. ============
  235. The kselftest_harness.h file contains useful helpers to build tests. The
  236. test harness is for userspace testing, for kernel space testing see `Test
  237. Module`_ above.
  238. The tests from tools/testing/selftests/seccomp/seccomp_bpf.c can be used as
  239. example.
  240. Example
  241. -------
  242. .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
  243. :doc: example
  244. Helpers
  245. -------
  246. .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
  247. :functions: TH_LOG TEST TEST_SIGNAL FIXTURE FIXTURE_DATA FIXTURE_SETUP
  248. FIXTURE_TEARDOWN TEST_F TEST_HARNESS_MAIN FIXTURE_VARIANT
  249. FIXTURE_VARIANT_ADD
  250. Operators
  251. ---------
  252. .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
  253. :doc: operators
  254. .. kernel-doc:: tools/testing/selftests/kselftest_harness.h
  255. :functions: ASSERT_EQ ASSERT_NE ASSERT_LT ASSERT_LE ASSERT_GT ASSERT_GE
  256. ASSERT_NULL ASSERT_TRUE ASSERT_NULL ASSERT_TRUE ASSERT_FALSE
  257. ASSERT_STREQ ASSERT_STRNE EXPECT_EQ EXPECT_NE EXPECT_LT
  258. EXPECT_LE EXPECT_GT EXPECT_GE EXPECT_NULL EXPECT_TRUE
  259. EXPECT_FALSE EXPECT_STREQ EXPECT_STRNE