1 /* SPDX-License-Identifier: GPL-2.0 */
3 #include <linux/membarrier.h>
10 #include "../kselftest.h"
12 static int sys_membarrier(int cmd
, int flags
)
14 return syscall(__NR_membarrier
, cmd
, flags
);
17 static int test_membarrier_cmd_fail(void)
19 int cmd
= -1, flags
= 0;
20 const char *test_name
= "sys membarrier invalid command";
22 if (sys_membarrier(cmd
, flags
) != -1) {
24 "%s test: command = %d, flags = %d. Should fail, but passed\n",
25 test_name
, cmd
, flags
);
27 if (errno
!= EINVAL
) {
29 "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
30 test_name
, flags
, EINVAL
, strerror(EINVAL
),
31 errno
, strerror(errno
));
34 ksft_test_result_pass(
35 "%s test: command = %d, flags = %d, errno = %d. Failed as expected\n",
36 test_name
, cmd
, flags
, errno
);
40 static int test_membarrier_flags_fail(void)
42 int cmd
= MEMBARRIER_CMD_QUERY
, flags
= 1;
43 const char *test_name
= "sys membarrier MEMBARRIER_CMD_QUERY invalid flags";
45 if (sys_membarrier(cmd
, flags
) != -1) {
47 "%s test: flags = %d. Should fail, but passed\n",
50 if (errno
!= EINVAL
) {
52 "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
53 test_name
, flags
, EINVAL
, strerror(EINVAL
),
54 errno
, strerror(errno
));
57 ksft_test_result_pass(
58 "%s test: flags = %d, errno = %d. Failed as expected\n",
59 test_name
, flags
, errno
);
63 static int test_membarrier_global_success(void)
65 int cmd
= MEMBARRIER_CMD_GLOBAL
, flags
= 0;
66 const char *test_name
= "sys membarrier MEMBARRIER_CMD_GLOBAL";
68 if (sys_membarrier(cmd
, flags
) != 0) {
70 "%s test: flags = %d, errno = %d\n",
71 test_name
, flags
, errno
);
74 ksft_test_result_pass(
75 "%s test: flags = %d\n", test_name
, flags
);
79 static int test_membarrier_private_expedited_fail(void)
81 int cmd
= MEMBARRIER_CMD_PRIVATE_EXPEDITED
, flags
= 0;
82 const char *test_name
= "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED not registered failure";
84 if (sys_membarrier(cmd
, flags
) != -1) {
86 "%s test: flags = %d. Should fail, but passed\n",
91 "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
92 test_name
, flags
, EPERM
, strerror(EPERM
),
93 errno
, strerror(errno
));
96 ksft_test_result_pass(
97 "%s test: flags = %d, errno = %d\n",
98 test_name
, flags
, errno
);
102 static int test_membarrier_register_private_expedited_success(void)
104 int cmd
= MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED
, flags
= 0;
105 const char *test_name
= "sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED";
107 if (sys_membarrier(cmd
, flags
) != 0) {
109 "%s test: flags = %d, errno = %d\n",
110 test_name
, flags
, errno
);
113 ksft_test_result_pass(
114 "%s test: flags = %d\n",
119 static int test_membarrier_private_expedited_success(void)
121 int cmd
= MEMBARRIER_CMD_PRIVATE_EXPEDITED
, flags
= 0;
122 const char *test_name
= "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED";
124 if (sys_membarrier(cmd
, flags
) != 0) {
126 "%s test: flags = %d, errno = %d\n",
127 test_name
, flags
, errno
);
130 ksft_test_result_pass(
131 "%s test: flags = %d\n",
136 static int test_membarrier_private_expedited_sync_core_fail(void)
138 int cmd
= MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE
, flags
= 0;
139 const char *test_name
= "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE not registered failure";
141 if (sys_membarrier(cmd
, flags
) != -1) {
143 "%s test: flags = %d. Should fail, but passed\n",
146 if (errno
!= EPERM
) {
148 "%s test: flags = %d. Should return (%d: \"%s\"), but returned (%d: \"%s\").\n",
149 test_name
, flags
, EPERM
, strerror(EPERM
),
150 errno
, strerror(errno
));
153 ksft_test_result_pass(
154 "%s test: flags = %d, errno = %d\n",
155 test_name
, flags
, errno
);
159 static int test_membarrier_register_private_expedited_sync_core_success(void)
161 int cmd
= MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE
, flags
= 0;
162 const char *test_name
= "sys membarrier MEMBARRIER_CMD_REGISTER_PRIVATE_EXPEDITED_SYNC_CORE";
164 if (sys_membarrier(cmd
, flags
) != 0) {
166 "%s test: flags = %d, errno = %d\n",
167 test_name
, flags
, errno
);
170 ksft_test_result_pass(
171 "%s test: flags = %d\n",
176 static int test_membarrier_private_expedited_sync_core_success(void)
178 int cmd
= MEMBARRIER_CMD_PRIVATE_EXPEDITED
, flags
= 0;
179 const char *test_name
= "sys membarrier MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE";
181 if (sys_membarrier(cmd
, flags
) != 0) {
183 "%s test: flags = %d, errno = %d\n",
184 test_name
, flags
, errno
);
187 ksft_test_result_pass(
188 "%s test: flags = %d\n",
193 static int test_membarrier_register_global_expedited_success(void)
195 int cmd
= MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED
, flags
= 0;
196 const char *test_name
= "sys membarrier MEMBARRIER_CMD_REGISTER_GLOBAL_EXPEDITED";
198 if (sys_membarrier(cmd
, flags
) != 0) {
200 "%s test: flags = %d, errno = %d\n",
201 test_name
, flags
, errno
);
204 ksft_test_result_pass(
205 "%s test: flags = %d\n",
210 static int test_membarrier_global_expedited_success(void)
212 int cmd
= MEMBARRIER_CMD_GLOBAL_EXPEDITED
, flags
= 0;
213 const char *test_name
= "sys membarrier MEMBARRIER_CMD_GLOBAL_EXPEDITED";
215 if (sys_membarrier(cmd
, flags
) != 0) {
217 "%s test: flags = %d, errno = %d\n",
218 test_name
, flags
, errno
);
221 ksft_test_result_pass(
222 "%s test: flags = %d\n",
227 static int test_membarrier_fail(void)
231 status
= test_membarrier_cmd_fail();
234 status
= test_membarrier_flags_fail();
237 status
= test_membarrier_private_expedited_fail();
240 status
= sys_membarrier(MEMBARRIER_CMD_QUERY
, 0);
242 ksft_test_result_fail("sys_membarrier() failed\n");
245 if (status
& MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE
) {
246 status
= test_membarrier_private_expedited_sync_core_fail();
253 static int test_membarrier_success(void)
257 status
= test_membarrier_global_success();
260 status
= test_membarrier_register_private_expedited_success();
263 status
= test_membarrier_private_expedited_success();
266 status
= sys_membarrier(MEMBARRIER_CMD_QUERY
, 0);
268 ksft_test_result_fail("sys_membarrier() failed\n");
271 if (status
& MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE
) {
272 status
= test_membarrier_register_private_expedited_sync_core_success();
275 status
= test_membarrier_private_expedited_sync_core_success();
280 * It is valid to send a global membarrier from a non-registered
283 status
= test_membarrier_global_expedited_success();
286 status
= test_membarrier_register_global_expedited_success();
289 status
= test_membarrier_global_expedited_success();
295 static int test_membarrier_query(void)
299 ret
= sys_membarrier(MEMBARRIER_CMD_QUERY
, flags
);
301 if (errno
== ENOSYS
) {
303 * It is valid to build a kernel with
304 * CONFIG_MEMBARRIER=n. However, this skips the tests.
307 "sys membarrier (CONFIG_MEMBARRIER) is disabled.\n");
309 ksft_exit_fail_msg("sys_membarrier() failed\n");
311 if (!(ret
& MEMBARRIER_CMD_GLOBAL
))
313 "sys_membarrier unsupported: CMD_GLOBAL not found.\n");
315 ksft_test_result_pass("sys_membarrier available\n");