Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0+ */
0002 #ifndef __KSELFTEST_MODULE_H
0003 #define __KSELFTEST_MODULE_H
0004 
0005 #include <linux/module.h>
0006 #include <linux/panic.h>
0007 
0008 /*
0009  * Test framework for writing test modules to be loaded by kselftest.
0010  * See Documentation/dev-tools/kselftest.rst for an example test module.
0011  */
0012 
0013 #define KSTM_MODULE_GLOBALS()           \
0014 static unsigned int total_tests __initdata; \
0015 static unsigned int failed_tests __initdata;    \
0016 static unsigned int skipped_tests __initdata
0017 
0018 #define KSTM_CHECK_ZERO(x) do {                     \
0019     total_tests++;                          \
0020     if (x) {                            \
0021         pr_warn("TC failed at %s:%d\n", __func__, __LINE__);    \
0022         failed_tests++;                     \
0023     }                               \
0024 } while (0)
0025 
0026 static inline int kstm_report(unsigned int total_tests, unsigned int failed_tests,
0027                   unsigned int skipped_tests)
0028 {
0029     if (failed_tests == 0) {
0030         if (skipped_tests) {
0031             pr_info("skipped %u tests\n", skipped_tests);
0032             pr_info("remaining %u tests passed\n", total_tests);
0033         } else
0034             pr_info("all %u tests passed\n", total_tests);
0035     } else
0036         pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
0037 
0038     return failed_tests ? -EINVAL : 0;
0039 }
0040 
0041 #define KSTM_MODULE_LOADERS(__module)           \
0042 static int __init __module##_init(void)         \
0043 {                           \
0044     pr_info("loaded.\n");               \
0045     add_taint(TAINT_TEST, LOCKDEP_STILL_OK);    \
0046     selftest();                 \
0047     return kstm_report(total_tests, failed_tests, skipped_tests);   \
0048 }                           \
0049 static void __exit __module##_exit(void)        \
0050 {                           \
0051     pr_info("unloaded.\n");             \
0052 }                           \
0053 module_init(__module##_init);               \
0054 module_exit(__module##_exit)
0055 
0056 MODULE_INFO(test, "Y");
0057 
0058 #endif  /* __KSELFTEST_MODULE_H */