1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/membarrier.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) {
23 "%s test: command = %d, flags = %d. Should fail, but passed\n",
24 test_name
, cmd
, flags
);
26 if (errno
!= EINVAL
) {
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
);
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) {
46 "%s test: flags = %d. Should fail, but passed\n",
49 if (errno
!= EINVAL
) {
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
);
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) {
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
);
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) {
85 "%s test: flags = %d. Should fail, but passed\n",
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
);
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) {
108 "%s test: flags = %d, errno = %d\n",
109 test_name
, flags
, errno
);
112 ksft_test_result_pass(
113 "%s test: flags = %d\n",
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) {
125 "%s test: flags = %d, errno = %d\n",
126 test_name
, flags
, errno
);
129 ksft_test_result_pass(
130 "%s test: flags = %d\n",
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) {
142 "%s test: flags = %d. Should fail, but passed\n",
145 if (errno
!= EPERM
) {
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
);
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) {
165 "%s test: flags = %d, errno = %d\n",
166 test_name
, flags
, errno
);
169 ksft_test_result_pass(
170 "%s test: flags = %d\n",
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) {
182 "%s test: flags = %d, errno = %d\n",
183 test_name
, flags
, errno
);
186 ksft_test_result_pass(
187 "%s test: flags = %d\n",
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) {
199 "%s test: flags = %d, errno = %d\n",
200 test_name
, flags
, errno
);
203 ksft_test_result_pass(
204 "%s test: flags = %d\n",
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) {
216 "%s test: flags = %d, errno = %d\n",
217 test_name
, flags
, errno
);
220 ksft_test_result_pass(
221 "%s test: flags = %d\n",
226 static int test_membarrier(void)
230 status
= test_membarrier_cmd_fail();
233 status
= test_membarrier_flags_fail();
236 status
= test_membarrier_global_success();
239 status
= test_membarrier_private_expedited_fail();
242 status
= test_membarrier_register_private_expedited_success();
245 status
= test_membarrier_private_expedited_success();
248 status
= sys_membarrier(MEMBARRIER_CMD_QUERY
, 0);
250 ksft_test_result_fail("sys_membarrier() failed\n");
253 if (status
& MEMBARRIER_CMD_PRIVATE_EXPEDITED_SYNC_CORE
) {
254 status
= test_membarrier_private_expedited_sync_core_fail();
257 status
= test_membarrier_register_private_expedited_sync_core_success();
260 status
= test_membarrier_private_expedited_sync_core_success();
265 * It is valid to send a global membarrier from a non-registered
268 status
= test_membarrier_global_expedited_success();
271 status
= test_membarrier_register_global_expedited_success();
274 status
= test_membarrier_global_expedited_success();
280 static int test_membarrier_query(void)
284 ret
= sys_membarrier(MEMBARRIER_CMD_QUERY
, flags
);
286 if (errno
== ENOSYS
) {
288 * It is valid to build a kernel with
289 * CONFIG_MEMBARRIER=n. However, this skips the tests.
292 "sys membarrier (CONFIG_MEMBARRIER) is disabled.\n");
294 ksft_exit_fail_msg("sys_membarrier() failed\n");
296 if (!(ret
& MEMBARRIER_CMD_GLOBAL
))
298 "sys_membarrier unsupported: CMD_GLOBAL not found.\n");
300 ksft_test_result_pass("sys_membarrier available\n");
304 int main(int argc
, char **argv
)
308 test_membarrier_query();
311 return ksft_exit_pass();