summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/membarrier/membarrier_test_impl.h
blob: 186be69f0a59b1855e6e8d48ba60d96e3602036a (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* SPDX-License-Identifier: GPL-2.0 */
#define _GNU_SOURCE
#include <linux/membarrier.h>
#include <syscall.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>

#include "../kselftest.h"

static int sys_membarrier(int cmd, int flags)
{
	return syscall(__NR_membarrier, cmd, flags);
}

static int test_membarrier_cmd_fail(void)
{
	int cmd = -1, flags = 0;
	const char *test_name = "sys membarrier invalid command";

	if (sys_membarrier(cmd, flags) != -1) {
		ksft_exit_fail_msg(
			"%s test: command = %d, flags = %d. Should fail, but passed\n",
			test_name, cmd, flags);
	}
	if (errno != EINVAL) {
		ksft_exit_fail_msg(
			"%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
			test_name, flags, EINVAL, strerror(EINVAL),
			errno, strerror(errno));
	}

	ksft_test_result_pass(
		"%s test: command = %d, flags = %d, errno = %d. Failed as expected\n",
		test_name, cmd, flags, errno);
	return 0;
}

static int test_membarrier_flags_fail(void)
{
	int cmd = MEMBARRIER_CMD_QUERY, flags = 1;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_QUERY invalid flags";

	if (sys_membarrier(cmd, flags) != -1) {
		ksft_exit_fail_msg(
			"%s test: flags = %d. Should fail, but passed\n",
			test_name, flags);
	}
	if (errno != EINVAL) {
		ksft_exit_fail_msg(
			"%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
			test_name, flags, EINVAL, strerror(EINVAL),
			errno, strerror(errno));
	}

	ksft_test_result_pass(
		"%s test: flags = %d, errno = %d. Failed as expected\n",
		test_name, flags, errno);
	return 0;
}

static int test_membarrier_global_success(void)
{
	int cmd = MEMBARRIER_CMD_GLOBAL, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_GLOBAL";

	if (sys_membarrier(cmd, flags) != 0) {
		ksft_exit_fail_msg(
			"%s test: flags = %d, errno = %d\n",
			test_name, flags, errno);
	}

	ksft_test_result_pass(
		"%s test: flags = %d\n", test_name, flags);
	return 0;
}

static int test_membarrier_private_expedited_fail(void)
{
	int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED not registered failure";

	if (sys_membarrier(cmd, flags) != -1) {
		ksft_exit_fail_msg(
			"%s test: flags = %d. Should fail, but passed\n",
			test_name, flags);
	}
	if (errno != EPERM) {
		ksft_exit_fail_msg(
			"%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
			test_name, flags, EPERM, strerror(EPERM),
			errno, strerror(errno));
	}

	ksft_test_result_pass(
		"%s test: flags = %d, errno = %d\n",
		test_name, flags, errno);
	return 0;
}

static int test_membarrier_register_private_expedited_success(void)
{
	int cmd = MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED";

	if (sys_membarrier(cmd, flags) != 0) {
		ksft_exit_fail_msg(
			"%s test: flags = %d, errno = %d\n",
			test_name, flags, errno);
	}

	ksft_test_result_pass(
		"%s test: flags = %d\n",
		test_name, flags);
	return 0;
}

static int test_membarrier_private_expedited_success(void)
{
	int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED";

	if (sys_membarrier(cmd, flags) != 0) {
		ksft_exit_fail_msg(
			"%s test: flags = %d, errno = %d\n",
			test_name, flags, errno);
	}

	ksft_test_result_pass(
		"%s test: flags = %d\n",
		test_name, flags);
	return 0;
}

static int test_membarrier_private_expedited_sync_core_fail(void)
{
	int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE not registered failure";

	if (sys_membarrier(cmd, flags) != -1) {
		ksft_exit_fail_msg(
			"%s test: flags = %d. Should fail, but passed\n",
			test_name, flags);
	}
	if (errno != EPERM) {
		ksft_exit_fail_msg(
			"%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
			test_name, flags, EPERM, strerror(EPERM),
			errno, strerror(errno));
	}

	ksft_test_result_pass(
		"%s test: flags = %d, errno = %d\n",
		test_name, flags, errno);
	return 0;
}

static int test_membarrier_register_private_expedited_sync_core_success(void)
{
	int cmd = MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE";

	if (sys_membarrier(cmd, flags) != 0) {
		ksft_exit_fail_msg(
			"%s test: flags = %d, errno = %d\n",
			test_name, flags, errno);
	}

	ksft_test_result_pass(
		"%s test: flags = %d\n",
		test_name, flags);
	return 0;
}

static int test_membarrier_private_expedited_sync_core_success(void)
{
	int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE";

	if (sys_membarrier(cmd, flags) != 0) {
		ksft_exit_fail_msg(
			"%s test: flags = %d, errno = %d\n",
			test_name, flags, errno);
	}

	ksft_test_result_pass(
		"%s test: flags = %d\n",
		test_name, flags);
	return 0;
}

static int test_membarrier_register_global_expedited_success(void)
{
	int cmd = MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED";

	if (sys_membarrier(cmd, flags) != 0) {
		ksft_exit_fail_msg(
			"%s test: flags = %d, errno = %d\n",
			test_name, flags, errno);
	}

	ksft_test_result_pass(
		"%s test: flags = %d\n",
		test_name, flags);
	return 0;
}

static int test_membarrier_global_expedited_success(void)
{
	int cmd = MEMBARRIER_CMD_GLOBAL_EXPEDITED, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_GLOBAL_EXPEDITED";

	if (sys_membarrier(cmd, flags) != 0) {
		ksft_exit_fail_msg(
			"%s test: flags = %d, errno = %d\n",
			test_name, flags, errno);
	}

	ksft_test_result_pass(
		"%s test: flags = %d\n",
		test_name, flags);
	return 0;
}

static int test_membarrier_fail(void)
{
	int status;

	status = test_membarrier_cmd_fail();
	if (status)
		return status;
	status = test_membarrier_flags_fail();
	if (status)
		return status;
	status = test_membarrier_private_expedited_fail();
	if (status)
		return status;
	status = sys_membarrier(MEMBARRIER_CMD_QUERY, 0);
	if (status < 0) {
		ksft_test_result_fail("sys_membarrier() failed\n");
		return status;
	}
	if (status & MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE) {
		status = test_membarrier_private_expedited_sync_core_fail();
		if (status)
			return status;
	}
	return 0;
}

static int test_membarrier_success(void)
{
	int status;

	status = test_membarrier_global_success();
	if (status)
		return status;
	status = test_membarrier_register_private_expedited_success();
	if (status)
		return status;
	status = test_membarrier_private_expedited_success();
	if (status)
		return status;
	status = sys_membarrier(MEMBARRIER_CMD_QUERY, 0);
	if (status < 0) {
		ksft_test_result_fail("sys_membarrier() failed\n");
		return status;
	}
	if (status & MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE) {
		status = test_membarrier_register_private_expedited_sync_core_success();
		if (status)
			return status;
		status = test_membarrier_private_expedited_sync_core_success();
		if (status)
			return status;
	}
	/*
	 * It is valid to send a global membarrier from a non-registered
	 * process.
	 */
	status = test_membarrier_global_expedited_success();
	if (status)
		return status;
	status = test_membarrier_register_global_expedited_success();
	if (status)
		return status;
	status = test_membarrier_global_expedited_success();
	if (status)
		return status;
	return 0;
}

static int test_membarrier_query(void)
{
	int flags = 0, ret;

	ret = sys_membarrier(MEMBARRIER_CMD_QUERY, flags);
	if (ret < 0) {
		if (errno == ENOSYS) {
			/*
			 * It is valid to build a kernel with
			 * CONFIG_MEMBARRIER=n. However, this skips the tests.
			 */
			ksft_exit_skip(
				"sys membarrier (CONFIG_MEMBARRIER) is disabled.\n");
		}
		ksft_exit_fail_msg("sys_membarrier() failed\n");
	}
	if (!(ret & MEMBARRIER_CMD_GLOBAL))
		ksft_exit_skip(
			"sys_membarrier unsupported: CMD_GLOBAL not found.\n");

	ksft_test_result_pass("sys_membarrier available\n");
	return 0;
}