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