]> git.ipfire.org Git - thirdparty/kernel/linux.git/blob - tools/testing/selftests/membarrier/membarrier_test.c
Merge tag 'sound-4.13-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[thirdparty/kernel/linux.git] / tools / testing / selftests / membarrier / membarrier_test.c
1 #define _GNU_SOURCE
2 #include <linux/membarrier.h>
3 #include <syscall.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7
8 #include "../kselftest.h"
9
10 static int sys_membarrier(int cmd, int flags)
11 {
12 return syscall(__NR_membarrier, cmd, flags);
13 }
14
15 static int test_membarrier_cmd_fail(void)
16 {
17 int cmd = -1, flags = 0;
18
19 if (sys_membarrier(cmd, flags) != -1) {
20 ksft_exit_fail_msg(
21 "sys membarrier invalid command test: command = %d, flags = %d. Should fail, but passed\n",
22 cmd, flags);
23 }
24
25 ksft_test_result_pass(
26 "sys membarrier invalid command test: command = %d, flags = %d. Failed as expected\n",
27 cmd, flags);
28 return 0;
29 }
30
31 static int test_membarrier_flags_fail(void)
32 {
33 int cmd = MEMBARRIER_CMD_QUERY, flags = 1;
34
35 if (sys_membarrier(cmd, flags) != -1) {
36 ksft_exit_fail_msg(
37 "sys membarrier MEMBARRIER_CMD_QUERY invalid flags test: flags = %d. Should fail, but passed\n",
38 flags);
39 }
40
41 ksft_test_result_pass(
42 "sys membarrier MEMBARRIER_CMD_QUERY invalid flags test: flags = %d. Failed as expected\n",
43 flags);
44 return 0;
45 }
46
47 static int test_membarrier_success(void)
48 {
49 int cmd = MEMBARRIER_CMD_SHARED, flags = 0;
50 const char *test_name = "sys membarrier MEMBARRIER_CMD_SHARED\n";
51
52 if (sys_membarrier(cmd, flags) != 0) {
53 ksft_exit_fail_msg(
54 "sys membarrier MEMBARRIER_CMD_SHARED test: flags = %d\n",
55 flags);
56 }
57
58 ksft_test_result_pass(
59 "sys membarrier MEMBARRIER_CMD_SHARED test: flags = %d\n",
60 flags);
61 return 0;
62 }
63
64 static int test_membarrier(void)
65 {
66 int status;
67
68 status = test_membarrier_cmd_fail();
69 if (status)
70 return status;
71 status = test_membarrier_flags_fail();
72 if (status)
73 return status;
74 status = test_membarrier_success();
75 if (status)
76 return status;
77 return 0;
78 }
79
80 static int test_membarrier_query(void)
81 {
82 int flags = 0, ret;
83
84 ret = sys_membarrier(MEMBARRIER_CMD_QUERY, flags);
85 if (ret < 0) {
86 if (errno == ENOSYS) {
87 /*
88 * It is valid to build a kernel with
89 * CONFIG_MEMBARRIER=n. However, this skips the tests.
90 */
91 ksft_exit_skip(
92 "sys membarrier (CONFIG_MEMBARRIER) is disabled.\n");
93 }
94 ksft_exit_fail_msg("sys_membarrier() failed\n");
95 }
96 if (!(ret & MEMBARRIER_CMD_SHARED))
97 ksft_exit_fail_msg("sys_membarrier is not supported.\n");
98
99 ksft_test_result_pass("sys_membarrier available\n");
100 return 0;
101 }
102
103 int main(int argc, char **argv)
104 {
105 ksft_print_header();
106
107 test_membarrier_query();
108 test_membarrier();
109
110 ksft_exit_pass();
111 }