Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: MIT */
0002 
0003 /*
0004  * Copyright © 2019 Intel Corporation
0005  */
0006 
0007 #include <linux/compiler.h>
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/sched/signal.h>
0011 #include <linux/slab.h>
0012 
0013 #include "selftest.h"
0014 
0015 enum {
0016 #define selftest(n, func) __idx_##n,
0017 #include "selftests.h"
0018 #undef selftest
0019 };
0020 
0021 #define selftest(n, f) [__idx_##n] = { .name = #n, .func = f },
0022 static struct selftest {
0023     bool enabled;
0024     const char *name;
0025     int (*func)(void);
0026 } selftests[] = {
0027 #include "selftests.h"
0028 };
0029 #undef selftest
0030 
0031 /* Embed the line number into the parameter name so that we can order tests */
0032 #define param(n) __PASTE(igt__, __PASTE(__PASTE(__LINE__, __), n))
0033 #define selftest_0(n, func, id) \
0034 module_param_named(id, selftests[__idx_##n].enabled, bool, 0400);
0035 #define selftest(n, func) selftest_0(n, func, param(n))
0036 #include "selftests.h"
0037 #undef selftest
0038 
0039 int __sanitycheck__(void)
0040 {
0041     pr_debug("Hello World!\n");
0042     return 0;
0043 }
0044 
0045 static char *__st_filter;
0046 
0047 static bool apply_subtest_filter(const char *caller, const char *name)
0048 {
0049     char *filter, *sep, *tok;
0050     bool result = true;
0051 
0052     filter = kstrdup(__st_filter, GFP_KERNEL);
0053     for (sep = filter; (tok = strsep(&sep, ","));) {
0054         bool allow = true;
0055         char *sl;
0056 
0057         if (*tok == '!') {
0058             allow = false;
0059             tok++;
0060         }
0061 
0062         if (*tok == '\0')
0063             continue;
0064 
0065         sl = strchr(tok, '/');
0066         if (sl) {
0067             *sl++ = '\0';
0068             if (strcmp(tok, caller)) {
0069                 if (allow)
0070                     result = false;
0071                 continue;
0072             }
0073             tok = sl;
0074         }
0075 
0076         if (strcmp(tok, name)) {
0077             if (allow)
0078                 result = false;
0079             continue;
0080         }
0081 
0082         result = allow;
0083         break;
0084     }
0085     kfree(filter);
0086 
0087     return result;
0088 }
0089 
0090 int
0091 __subtests(const char *caller, const struct subtest *st, int count, void *data)
0092 {
0093     int err;
0094 
0095     for (; count--; st++) {
0096         cond_resched();
0097         if (signal_pending(current))
0098             return -EINTR;
0099 
0100         if (!apply_subtest_filter(caller, st->name))
0101             continue;
0102 
0103         pr_info("dma-buf: Running %s/%s\n", caller, st->name);
0104 
0105         err = st->func(data);
0106         if (err && err != -EINTR) {
0107             pr_err("dma-buf/%s: %s failed with error %d\n",
0108                    caller, st->name, err);
0109             return err;
0110         }
0111     }
0112 
0113     return 0;
0114 }
0115 
0116 static void set_default_test_all(struct selftest *st, unsigned long count)
0117 {
0118     unsigned long i;
0119 
0120     for (i = 0; i < count; i++)
0121         if (st[i].enabled)
0122             return;
0123 
0124     for (i = 0; i < count; i++)
0125         st[i].enabled = true;
0126 }
0127 
0128 static int run_selftests(struct selftest *st, unsigned long count)
0129 {
0130     int err = 0;
0131 
0132     set_default_test_all(st, count);
0133 
0134     /* Tests are listed in natural order in selftests.h */
0135     for (; count--; st++) {
0136         if (!st->enabled)
0137             continue;
0138 
0139         pr_info("dma-buf: Running %s\n", st->name);
0140         err = st->func();
0141         if (err)
0142             break;
0143     }
0144 
0145     if (WARN(err > 0 || err == -ENOTTY,
0146          "%s returned %d, conflicting with selftest's magic values!\n",
0147          st->name, err))
0148         err = -1;
0149 
0150     return err;
0151 }
0152 
0153 static int __init st_init(void)
0154 {
0155     return run_selftests(selftests, ARRAY_SIZE(selftests));
0156 }
0157 
0158 static void __exit st_exit(void)
0159 {
0160 }
0161 
0162 module_param_named(st_filter, __st_filter, charp, 0400);
0163 module_init(st_init);
0164 module_exit(st_exit);
0165 
0166 MODULE_DESCRIPTION("Self-test harness for dma-buf");
0167 MODULE_LICENSE("GPL and additional rights");