Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftest: Taint kernel when test module loaded

Make any kselftest test module (using the kselftest_module framework)
taint the kernel with TAINT_TEST on module load.

Also mark the module as a test module using MODULE_INFO(test, "Y") so
that other tools can tell this is a test module. We can't rely solely
on this, though, as these test modules are also often built-in.

Finally, update the kselftest documentation to mention that the kernel
should be tainted, and how to do so manually (as below).

Note that several selftests use kernel modules which are not based on
the kselftest_module framework, and so will not automatically taint the
kernel.

This can be done in two ways:
- Moving the module to the tools/testing directory. All modules under
this directory will taint the kernel.
- Adding the 'test' module property with:
MODULE_INFO(test, "Y")

Similarly, selftests which do not load modules into the kernel generally
should not taint the kernel (or possibly should only do so on failure),
as it's assumed that testing from user-space should be safe. Regardless,
they can write to /proc/sys/kernel/tainted if required.

Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Acked-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

David Gow and committed by
Shuah Khan
8370b400 74829ddf

+13
+9
Documentation/dev-tools/kselftest.rst
··· 250 250 - ``tools/testing/selftests/kselftest_module.h`` 251 251 - ``tools/testing/selftests/kselftest/module.sh`` 252 252 253 + Note that test modules should taint the kernel with TAINT_TEST. This will 254 + happen automatically for modules which are in the ``tools/testing/`` 255 + directory, or for modules which use the ``kselftest_module.h`` header above. 256 + Otherwise, you'll need to add ``MODULE_INFO(test, "Y")`` to your module 257 + source. selftests which do not load modules typically should not taint the 258 + kernel, but in cases where a non-test module is loaded, TEST_TAINT can be 259 + applied from userspace by writing to ``/proc/sys/kernel/tainted``. 260 + 253 261 How to use 254 262 ---------- 255 263 ··· 316 308 KSTM_MODULE_LOADERS(test_foo); 317 309 MODULE_AUTHOR("John Developer <jd@fooman.org>"); 318 310 MODULE_LICENSE("GPL"); 311 + MODULE_INFO(test, "Y"); 319 312 320 313 Example test script 321 314 -------------------
+4
tools/testing/selftests/kselftest_module.h
··· 3 3 #define __KSELFTEST_MODULE_H 4 4 5 5 #include <linux/module.h> 6 + #include <linux/panic.h> 6 7 7 8 /* 8 9 * Test framework for writing test modules to be loaded by kselftest. ··· 42 41 static int __init __module##_init(void) \ 43 42 { \ 44 43 pr_info("loaded.\n"); \ 44 + add_taint(TAINT_TEST, LOCKDEP_STILL_OK); \ 45 45 selftest(); \ 46 46 return kstm_report(total_tests, failed_tests, skipped_tests); \ 47 47 } \ ··· 52 50 } \ 53 51 module_init(__module##_init); \ 54 52 module_exit(__module##_exit) 53 + 54 + MODULE_INFO(test, "Y"); 55 55 56 56 #endif /* __KSELFTEST_MODULE_H */