kselftest_module.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #ifndef __KSELFTEST_MODULE_H
  3. #define __KSELFTEST_MODULE_H
  4. #include <linux/module.h>
  5. #include <linux/panic.h>
  6. /*
  7. * Test framework for writing test modules to be loaded by kselftest.
  8. * See Documentation/dev-tools/kselftest.rst for an example test module.
  9. */
  10. #define KSTM_MODULE_GLOBALS() \
  11. static unsigned int total_tests __initdata; \
  12. static unsigned int failed_tests __initdata; \
  13. static unsigned int skipped_tests __initdata
  14. #define KSTM_CHECK_ZERO(x) do { \
  15. total_tests++; \
  16. if (x) { \
  17. pr_warn("TC failed at %s:%d\n", __func__, __LINE__); \
  18. failed_tests++; \
  19. } \
  20. } while (0)
  21. static inline int kstm_report(unsigned int total_tests, unsigned int failed_tests,
  22. unsigned int skipped_tests)
  23. {
  24. if (failed_tests == 0) {
  25. if (skipped_tests) {
  26. pr_info("skipped %u tests\n", skipped_tests);
  27. pr_info("remaining %u tests passed\n", total_tests);
  28. } else
  29. pr_info("all %u tests passed\n", total_tests);
  30. } else
  31. pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
  32. return failed_tests ? -EINVAL : 0;
  33. }
  34. #define KSTM_MODULE_LOADERS(__module) \
  35. static int __init __module##_init(void) \
  36. { \
  37. pr_info("loaded.\n"); \
  38. add_taint(TAINT_TEST, LOCKDEP_STILL_OK); \
  39. selftest(); \
  40. return kstm_report(total_tests, failed_tests, skipped_tests); \
  41. } \
  42. static void __exit __module##_exit(void) \
  43. { \
  44. pr_info("unloaded.\n"); \
  45. } \
  46. module_init(__module##_init); \
  47. module_exit(__module##_exit)
  48. MODULE_INFO(test, "Y");
  49. #endif /* __KSELFTEST_MODULE_H */