summaryrefslogtreecommitdiffstats
path: root/drivers/dma-buf/selftest.c
blob: c60b6944b4bd18e4365ce65efb47642089712b10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* SPDX-License-Identifier: MIT */

/*
 * Copyright © 2019 Intel Corporation
 */

#include <linux/compiler.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched/signal.h>
#include <linux/slab.h>

#include "selftest.h"

enum {
#define selftest(n, func) __idx_##n,
#include "selftests.h"
#undef selftest
};

#define selftest(n, f) [__idx_##n] = { .name = #n, .func = f },
static struct selftest {
	bool enabled;
	const char *name;
	int (*func)(void);
} selftests[] = {
#include "selftests.h"
};
#undef selftest

/* Embed the line number into the parameter name so that we can order tests */
#define param(n) __PASTE(igt__, __PASTE(__PASTE(__LINE__, __), n))
#define selftest_0(n, func, id) \
module_param_named(id, selftests[__idx_##n].enabled, bool, 0400);
#define selftest(n, func) selftest_0(n, func, param(n))
#include "selftests.h"
#undef selftest

int __sanitycheck__(void)
{
	pr_debug("Hello World!\n");
	return 0;
}

static char *__st_filter;

static bool apply_subtest_filter(const char *caller, const char *name)
{
	char *filter, *sep, *tok;
	bool result = true;

	filter = kstrdup(__st_filter, GFP_KERNEL);
	for (sep = filter; (tok = strsep(&sep, ","));) {
		bool allow = true;
		char *sl;

		if (*tok == '!') {
			allow = false;
			tok++;
		}

		if (*tok == '\0')
			continue;

		sl = strchr(tok, '/');
		if (sl) {
			*sl++ = '\0';
			if (strcmp(tok, caller)) {
				if (allow)
					result = false;
				continue;
			}
			tok = sl;
		}

		if (strcmp(tok, name)) {
			if (allow)
				result = false;
			continue;
		}

		result = allow;
		break;
	}
	kfree(filter);

	return result;
}

int
__subtests(const char *caller, const struct subtest *st, int count, void *data)
{
	int err;

	for (; count--; st++) {
		cond_resched();
		if (signal_pending(current))
			return -EINTR;

		if (!apply_subtest_filter(caller, st->name))
			continue;

		pr_info("dma-buf: Running %s/%s\n", caller, st->name);

		err = st->func(data);
		if (err && err != -EINTR) {
			pr_err("dma-buf/%s: %s failed with error %d\n",
			       caller, st->name, err);
			return err;
		}
	}

	return 0;
}

static void set_default_test_all(struct selftest *st, unsigned long count)
{
	unsigned long i;

	for (i = 0; i < count; i++)
		if (st[i].enabled)
			return;

	for (i = 0; i < count; i++)
		st[i].enabled = true;
}

static int run_selftests(struct selftest *st, unsigned long count)
{
	int err = 0;

	set_default_test_all(st, count);

	/* Tests are listed in natural order in selftests.h */
	for (; count--; st++) {
		if (!st->enabled)
			continue;

		pr_info("dma-buf: Running %s\n", st->name);
		err = st->func();
		if (err)
			break;
	}

	if (WARN(err > 0 || err == -ENOTTY,
		 "%s returned %d, conflicting with selftest's magic values!\n",
		 st->name, err))
		err = -1;

	return err;
}

static int __init st_init(void)
{
	return run_selftests(selftests, ARRAY_SIZE(selftests));
}

static void __exit st_exit(void)
{
}

module_param_named(st_filter, __st_filter, charp, 0400);
module_init(st_init);
module_exit(st_exit);

MODULE_DESCRIPTION("Self-test harness for dma-buf");
MODULE_LICENSE("GPL and additional rights");