linux/tools/testing/selftests/kselftest_module.h
David Gow 8370b400f5 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>
2022-07-11 16:58:11 -06:00

59 lines
1.6 KiB
C

/* SPDX-License-Identifier: GPL-2.0+ */
#ifndef __KSELFTEST_MODULE_H
#define __KSELFTEST_MODULE_H
#include <linux/module.h>
#include <linux/panic.h>
/*
* Test framework for writing test modules to be loaded by kselftest.
* See Documentation/dev-tools/kselftest.rst for an example test module.
*/
#define KSTM_MODULE_GLOBALS() \
static unsigned int total_tests __initdata; \
static unsigned int failed_tests __initdata; \
static unsigned int skipped_tests __initdata
#define KSTM_CHECK_ZERO(x) do { \
total_tests++; \
if (x) { \
pr_warn("TC failed at %s:%d\n", __func__, __LINE__); \
failed_tests++; \
} \
} while (0)
static inline int kstm_report(unsigned int total_tests, unsigned int failed_tests,
unsigned int skipped_tests)
{
if (failed_tests == 0) {
if (skipped_tests) {
pr_info("skipped %u tests\n", skipped_tests);
pr_info("remaining %u tests passed\n", total_tests);
} else
pr_info("all %u tests passed\n", total_tests);
} else
pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
return failed_tests ? -EINVAL : 0;
}
#define KSTM_MODULE_LOADERS(__module) \
static int __init __module##_init(void) \
{ \
pr_info("loaded.\n"); \
add_taint(TAINT_TEST, LOCKDEP_STILL_OK); \
selftest(); \
return kstm_report(total_tests, failed_tests, skipped_tests); \
} \
static void __exit __module##_exit(void) \
{ \
pr_info("unloaded.\n"); \
} \
module_init(__module##_init); \
module_exit(__module##_exit)
MODULE_INFO(test, "Y");
#endif /* __KSELFTEST_MODULE_H */