Linux 4.19.133
[linux/fpc-iii.git] / tools / testing / selftests / membarrier / membarrier_test.c
blob6793f8ecc8e7fc55d2eb1f16bdc04449f7df0a54
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <linux/membarrier.h>
4 #include <syscall.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
9 #include "../kselftest.h"
11 static int sys_membarrier(int cmd, int flags)
13 return syscall(__NR_membarrier, cmd, flags);
16 static int test_membarrier_cmd_fail(void)
18 int cmd = -1, flags = 0;
19 const char *test_name = "sys membarrier invalid command";
21 if (sys_membarrier(cmd, flags) != -1) {
22 ksft_exit_fail_msg(
23 "%s test: command = %d, flags = %d. Should fail, but passed\n",
24 test_name, cmd, flags);
26 if (errno != EINVAL) {
27 ksft_exit_fail_msg(
28 "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
29 test_name, flags, EINVAL, strerror(EINVAL),
30 errno, strerror(errno));
33 ksft_test_result_pass(
34 "%s test: command = %d, flags = %d, errno = %d. Failed as expected\n",
35 test_name, cmd, flags, errno);
36 return 0;
39 static int test_membarrier_flags_fail(void)
41 int cmd = MEMBARRIER_CMD_QUERY, flags = 1;
42 const char *test_name = "sys membarrier MEMBARRIER_CMD_QUERY invalid flags";
44 if (sys_membarrier(cmd, flags) != -1) {
45 ksft_exit_fail_msg(
46 "%s test: flags = %d. Should fail, but passed\n",
47 test_name, flags);
49 if (errno != EINVAL) {
50 ksft_exit_fail_msg(
51 "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
52 test_name, flags, EINVAL, strerror(EINVAL),
53 errno, strerror(errno));
56 ksft_test_result_pass(
57 "%s test: flags = %d, errno = %d. Failed as expected\n",
58 test_name, flags, errno);
59 return 0;
62 static int test_membarrier_global_success(void)
64 int cmd = MEMBARRIER_CMD_GLOBAL, flags = 0;
65 const char *test_name = "sys membarrier MEMBARRIER_CMD_GLOBAL";
67 if (sys_membarrier(cmd, flags) != 0) {
68 ksft_exit_fail_msg(
69 "%s test: flags = %d, errno = %d\n",
70 test_name, flags, errno);
73 ksft_test_result_pass(
74 "%s test: flags = %d\n", test_name, flags);
75 return 0;
78 static int test_membarrier_private_expedited_fail(void)
80 int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED, flags = 0;
81 const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED not registered failure";
83 if (sys_membarrier(cmd, flags) != -1) {
84 ksft_exit_fail_msg(
85 "%s test: flags = %d. Should fail, but passed\n",
86 test_name, flags);
88 if (errno != EPERM) {
89 ksft_exit_fail_msg(
90 "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
91 test_name, flags, EPERM, strerror(EPERM),
92 errno, strerror(errno));
95 ksft_test_result_pass(
96 "%s test: flags = %d, errno = %d\n",
97 test_name, flags, errno);
98 return 0;
101 static int test_membarrier_register_private_expedited_success(void)
103 int cmd = MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED, flags = 0;
104 const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED";
106 if (sys_membarrier(cmd, flags) != 0) {
107 ksft_exit_fail_msg(
108 "%s test: flags = %d, errno = %d\n",
109 test_name, flags, errno);
112 ksft_test_result_pass(
113 "%s test: flags = %d\n",
114 test_name, flags);
115 return 0;
118 static int test_membarrier_private_expedited_success(void)
120 int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED, flags = 0;
121 const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED";
123 if (sys_membarrier(cmd, flags) != 0) {
124 ksft_exit_fail_msg(
125 "%s test: flags = %d, errno = %d\n",
126 test_name, flags, errno);
129 ksft_test_result_pass(
130 "%s test: flags = %d\n",
131 test_name, flags);
132 return 0;
135 static int test_membarrier_private_expedited_sync_core_fail(void)
137 int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE, flags = 0;
138 const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE not registered failure";
140 if (sys_membarrier(cmd, flags) != -1) {
141 ksft_exit_fail_msg(
142 "%s test: flags = %d. Should fail, but passed\n",
143 test_name, flags);
145 if (errno != EPERM) {
146 ksft_exit_fail_msg(
147 "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
148 test_name, flags, EPERM, strerror(EPERM),
149 errno, strerror(errno));
152 ksft_test_result_pass(
153 "%s test: flags = %d, errno = %d\n",
154 test_name, flags, errno);
155 return 0;
158 static int test_membarrier_register_private_expedited_sync_core_success(void)
160 int cmd = MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE, flags = 0;
161 const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE";
163 if (sys_membarrier(cmd, flags) != 0) {
164 ksft_exit_fail_msg(
165 "%s test: flags = %d, errno = %d\n",
166 test_name, flags, errno);
169 ksft_test_result_pass(
170 "%s test: flags = %d\n",
171 test_name, flags);
172 return 0;
175 static int test_membarrier_private_expedited_sync_core_success(void)
177 int cmd = MEMBARRIER_CMD_PRIVATE_EXPEDITED, flags = 0;
178 const char *test_name = "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE";
180 if (sys_membarrier(cmd, flags) != 0) {
181 ksft_exit_fail_msg(
182 "%s test: flags = %d, errno = %d\n",
183 test_name, flags, errno);
186 ksft_test_result_pass(
187 "%s test: flags = %d\n",
188 test_name, flags);
189 return 0;
192 static int test_membarrier_register_global_expedited_success(void)
194 int cmd = MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED, flags = 0;
195 const char *test_name = "sys membarrier MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED";
197 if (sys_membarrier(cmd, flags) != 0) {
198 ksft_exit_fail_msg(
199 "%s test: flags = %d, errno = %d\n",
200 test_name, flags, errno);
203 ksft_test_result_pass(
204 "%s test: flags = %d\n",
205 test_name, flags);
206 return 0;
209 static int test_membarrier_global_expedited_success(void)
211 int cmd = MEMBARRIER_CMD_GLOBAL_EXPEDITED, flags = 0;
212 const char *test_name = "sys membarrier MEMBARRIER_CMD_GLOBAL_EXPEDITED";
214 if (sys_membarrier(cmd, flags) != 0) {
215 ksft_exit_fail_msg(
216 "%s test: flags = %d, errno = %d\n",
217 test_name, flags, errno);
220 ksft_test_result_pass(
221 "%s test: flags = %d\n",
222 test_name, flags);
223 return 0;
226 static int test_membarrier(void)
228 int status;
230 status = test_membarrier_cmd_fail();
231 if (status)
232 return status;
233 status = test_membarrier_flags_fail();
234 if (status)
235 return status;
236 status = test_membarrier_global_success();
237 if (status)
238 return status;
239 status = test_membarrier_private_expedited_fail();
240 if (status)
241 return status;
242 status = test_membarrier_register_private_expedited_success();
243 if (status)
244 return status;
245 status = test_membarrier_private_expedited_success();
246 if (status)
247 return status;
248 status = sys_membarrier(MEMBARRIER_CMD_QUERY, 0);
249 if (status < 0) {
250 ksft_test_result_fail("sys_membarrier() failed\n");
251 return status;
253 if (status & MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE) {
254 status = test_membarrier_private_expedited_sync_core_fail();
255 if (status)
256 return status;
257 status = test_membarrier_register_private_expedited_sync_core_success();
258 if (status)
259 return status;
260 status = test_membarrier_private_expedited_sync_core_success();
261 if (status)
262 return status;
265 * It is valid to send a global membarrier from a non-registered
266 * process.
268 status = test_membarrier_global_expedited_success();
269 if (status)
270 return status;
271 status = test_membarrier_register_global_expedited_success();
272 if (status)
273 return status;
274 status = test_membarrier_global_expedited_success();
275 if (status)
276 return status;
277 return 0;
280 static int test_membarrier_query(void)
282 int flags = 0, ret;
284 ret = sys_membarrier(MEMBARRIER_CMD_QUERY, flags);
285 if (ret < 0) {
286 if (errno == ENOSYS) {
288 * It is valid to build a kernel with
289 * CONFIG_MEMBARRIER=n. However, this skips the tests.
291 ksft_exit_skip(
292 "sys membarrier (CONFIG_MEMBARRIER) is disabled.\n");
294 ksft_exit_fail_msg("sys_membarrier() failed\n");
296 if (!(ret & MEMBARRIER_CMD_GLOBAL))
297 ksft_exit_skip(
298 "sys_membarrier unsupported: CMD_GLOBAL not found.\n");
300 ksft_test_result_pass("sys_membarrier available\n");
301 return 0;
304 int main(int argc, char **argv)
306 ksft_print_header();
308 test_membarrier_query();
309 test_membarrier();
311 return ksft_exit_pass();