selftest.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. */
  5. #include <linux/compiler.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/slab.h>
  10. #include "selftest.h"
  11. enum {
  12. #define selftest(n, func) __idx_##n,
  13. #include "selftests.h"
  14. #undef selftest
  15. };
  16. #define selftest(n, f) [__idx_##n] = { .name = #n, .func = f },
  17. static struct selftest {
  18. bool enabled;
  19. const char *name;
  20. int (*func)(void);
  21. } selftests[] = {
  22. #include "selftests.h"
  23. };
  24. #undef selftest
  25. /* Embed the line number into the parameter name so that we can order tests */
  26. #define param(n) __PASTE(igt__, __PASTE(__PASTE(__LINE__, __), n))
  27. #define selftest_0(n, func, id) \
  28. module_param_named(id, selftests[__idx_##n].enabled, bool, 0400);
  29. #define selftest(n, func) selftest_0(n, func, param(n))
  30. #include "selftests.h"
  31. #undef selftest
  32. int __sanitycheck__(void)
  33. {
  34. pr_debug("Hello World!\n");
  35. return 0;
  36. }
  37. static char *__st_filter;
  38. static bool apply_subtest_filter(const char *caller, const char *name)
  39. {
  40. char *filter, *sep, *tok;
  41. bool result = true;
  42. filter = kstrdup(__st_filter, GFP_KERNEL);
  43. for (sep = filter; (tok = strsep(&sep, ","));) {
  44. bool allow = true;
  45. char *sl;
  46. if (*tok == '!') {
  47. allow = false;
  48. tok++;
  49. }
  50. if (*tok == '\0')
  51. continue;
  52. sl = strchr(tok, '/');
  53. if (sl) {
  54. *sl++ = '\0';
  55. if (strcmp(tok, caller)) {
  56. if (allow)
  57. result = false;
  58. continue;
  59. }
  60. tok = sl;
  61. }
  62. if (strcmp(tok, name)) {
  63. if (allow)
  64. result = false;
  65. continue;
  66. }
  67. result = allow;
  68. break;
  69. }
  70. kfree(filter);
  71. return result;
  72. }
  73. int
  74. __subtests(const char *caller, const struct subtest *st, int count, void *data)
  75. {
  76. int err;
  77. for (; count--; st++) {
  78. cond_resched();
  79. if (signal_pending(current))
  80. return -EINTR;
  81. if (!apply_subtest_filter(caller, st->name))
  82. continue;
  83. pr_info("dma-buf: Running %s/%s\n", caller, st->name);
  84. err = st->func(data);
  85. if (err && err != -EINTR) {
  86. pr_err("dma-buf/%s: %s failed with error %d\n",
  87. caller, st->name, err);
  88. return err;
  89. }
  90. }
  91. return 0;
  92. }
  93. static void set_default_test_all(struct selftest *st, unsigned long count)
  94. {
  95. unsigned long i;
  96. for (i = 0; i < count; i++)
  97. if (st[i].enabled)
  98. return;
  99. for (i = 0; i < count; i++)
  100. st[i].enabled = true;
  101. }
  102. static int run_selftests(struct selftest *st, unsigned long count)
  103. {
  104. int err = 0;
  105. set_default_test_all(st, count);
  106. /* Tests are listed in natural order in selftests.h */
  107. for (; count--; st++) {
  108. if (!st->enabled)
  109. continue;
  110. pr_info("dma-buf: Running %s\n", st->name);
  111. err = st->func();
  112. if (err)
  113. break;
  114. }
  115. if (WARN(err > 0 || err == -ENOTTY,
  116. "%s returned %d, conflicting with selftest's magic values!\n",
  117. st->name, err))
  118. err = -1;
  119. return err;
  120. }
  121. static int __init st_init(void)
  122. {
  123. return run_selftests(selftests, ARRAY_SIZE(selftests));
  124. }
  125. static void __exit st_exit(void)
  126. {
  127. }
  128. module_param_named(st_filter, __st_filter, charp, 0400);
  129. module_init(st_init);
  130. module_exit(st_exit);
  131. MODULE_DESCRIPTION("Self-test harness for dma-buf");
  132. MODULE_LICENSE("GPL and additional rights");