Skip to content

Commit 8370b40

Browse files
sulixshuahkh
authored andcommitted
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 <[email protected]> Acked-by: Brendan Higgins <[email protected]> Signed-off-by: David Gow <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 74829dd commit 8370b40

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Documentation/dev-tools/kselftest.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ assist writing kernel modules that are for use with kselftest:
250250
- ``tools/testing/selftests/kselftest_module.h``
251251
- ``tools/testing/selftests/kselftest/module.sh``
252252

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+
253261
How to use
254262
----------
255263

@@ -308,6 +316,7 @@ A bare bones test module might look like this:
308316
KSTM_MODULE_LOADERS(test_foo);
309317
MODULE_AUTHOR("John Developer <[email protected]>");
310318
MODULE_LICENSE("GPL");
319+
MODULE_INFO(test, "Y");
311320
312321
Example test script
313322
-------------------

tools/testing/selftests/kselftest_module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define __KSELFTEST_MODULE_H
44

55
#include <linux/module.h>
6+
#include <linux/panic.h>
67

78
/*
89
* Test framework for writing test modules to be loaded by kselftest.
@@ -41,6 +42,7 @@ static inline int kstm_report(unsigned int total_tests, unsigned int failed_test
4142
static int __init __module##_init(void) \
4243
{ \
4344
pr_info("loaded.\n"); \
45+
add_taint(TAINT_TEST, LOCKDEP_STILL_OK); \
4446
selftest(); \
4547
return kstm_report(total_tests, failed_tests, skipped_tests); \
4648
} \
@@ -51,4 +53,6 @@ static void __exit __module##_exit(void) \
5153
module_init(__module##_init); \
5254
module_exit(__module##_exit)
5355

56+
MODULE_INFO(test, "Y");
57+
5458
#endif /* __KSELFTEST_MODULE_H */

0 commit comments

Comments
 (0)