2 #include <linux/membarrier.h>
8 #include "../kselftest.h"
10 enum test_membarrier_status
{
11 TEST_MEMBARRIER_PASS
= 0,
16 static int sys_membarrier(int cmd
, int flags
)
18 return syscall(__NR_membarrier
, cmd
, flags
);
21 static enum test_membarrier_status
test_membarrier_cmd_fail(void)
23 int cmd
= -1, flags
= 0;
25 if (sys_membarrier(cmd
, flags
) != -1) {
26 printf("membarrier: Wrong command should fail but passed.\n");
27 return TEST_MEMBARRIER_FAIL
;
29 return TEST_MEMBARRIER_PASS
;
32 static enum test_membarrier_status
test_membarrier_flags_fail(void)
34 int cmd
= MEMBARRIER_CMD_QUERY
, flags
= 1;
36 if (sys_membarrier(cmd
, flags
) != -1) {
37 printf("membarrier: Wrong flags should fail but passed.\n");
38 return TEST_MEMBARRIER_FAIL
;
40 return TEST_MEMBARRIER_PASS
;
43 static enum test_membarrier_status
test_membarrier_success(void)
45 int cmd
= MEMBARRIER_CMD_SHARED
, flags
= 0;
47 if (sys_membarrier(cmd
, flags
) != 0) {
48 printf("membarrier: Executing MEMBARRIER_CMD_SHARED failed. %s.\n",
50 return TEST_MEMBARRIER_FAIL
;
53 printf("membarrier: MEMBARRIER_CMD_SHARED success.\n");
54 return TEST_MEMBARRIER_PASS
;
57 static enum test_membarrier_status
test_membarrier(void)
59 enum test_membarrier_status status
;
61 status
= test_membarrier_cmd_fail();
64 status
= test_membarrier_flags_fail();
67 status
= test_membarrier_success();
70 return TEST_MEMBARRIER_PASS
;
73 static enum test_membarrier_status
test_membarrier_query(void)
77 printf("membarrier MEMBARRIER_CMD_QUERY ");
78 ret
= sys_membarrier(MEMBARRIER_CMD_QUERY
, flags
);
80 printf("failed. %s.\n", strerror(errno
));
84 * It is valid to build a kernel with
85 * CONFIG_MEMBARRIER=n. However, this skips the tests.
87 return TEST_MEMBARRIER_SKIP
;
90 return TEST_MEMBARRIER_FAIL
;
93 if (!(ret
& MEMBARRIER_CMD_SHARED
)) {
94 printf("command MEMBARRIER_CMD_SHARED is not supported.\n");
95 return TEST_MEMBARRIER_FAIL
;
97 printf("syscall available.\n");
98 return TEST_MEMBARRIER_PASS
;
101 int main(int argc
, char **argv
)
103 switch (test_membarrier_query()) {
104 case TEST_MEMBARRIER_FAIL
:
105 return ksft_exit_fail();
106 case TEST_MEMBARRIER_SKIP
:
107 return ksft_exit_skip();
109 switch (test_membarrier()) {
110 case TEST_MEMBARRIER_FAIL
:
111 return ksft_exit_fail();
112 case TEST_MEMBARRIER_SKIP
:
113 return ksft_exit_skip();
116 printf("membarrier: tests done!\n");
117 return ksft_exit_pass();