summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/membarrier/membarrier_test.c
blob: dde3125080074a3588b99ee1beafee82f6a9eded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#define _GNU_SOURCE
#define __EXPORTED_HEADERS__

#include <linux/membarrier.h>
#include <asm-generic/unistd.h>
#include <sys/syscall.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "../kselftest.h"

enum test_membarrier_status {
	TEST_MEMBARRIER_PASS = 0,
	TEST_MEMBARRIER_FAIL,
	TEST_MEMBARRIER_SKIP,
};

static int sys_membarrier(int cmd, int flags)
{
	return syscall(__NR_membarrier, cmd, flags);
}

static enum test_membarrier_status test_membarrier_cmd_fail(void)
{
	int cmd = -1, flags = 0;

	if (sys_membarrier(cmd, flags) != -1) {
		printf("membarrier: Wrong command should fail but passed.\n");
		return TEST_MEMBARRIER_FAIL;
	}
	return TEST_MEMBARRIER_PASS;
}

static enum test_membarrier_status test_membarrier_flags_fail(void)
{
	int cmd = MEMBARRIER_CMD_QUERY, flags = 1;

	if (sys_membarrier(cmd, flags) != -1) {
		printf("membarrier: Wrong flags should fail but passed.\n");
		return TEST_MEMBARRIER_FAIL;
	}
	return TEST_MEMBARRIER_PASS;
}

static enum test_membarrier_status test_membarrier_success(void)
{
	int cmd = MEMBARRIER_CMD_SHARED, flags = 0;

	if (sys_membarrier(cmd, flags) != 0) {
		printf("membarrier: Executing MEMBARRIER_CMD_SHARED failed. %s.\n",
				strerror(errno));
		return TEST_MEMBARRIER_FAIL;
	}

	printf("membarrier: MEMBARRIER_CMD_SHARED success.\n");
	return TEST_MEMBARRIER_PASS;
}

static enum test_membarrier_status test_membarrier(void)
{
	enum test_membarrier_status status;

	status = test_membarrier_cmd_fail();
	if (status)
		return status;
	status = test_membarrier_flags_fail();
	if (status)
		return status;
	status = test_membarrier_success();
	if (status)
		return status;
	return TEST_MEMBARRIER_PASS;
}

static enum test_membarrier_status test_membarrier_query(void)
{
	int flags = 0, ret;

	printf("membarrier MEMBARRIER_CMD_QUERY ");
	ret = sys_membarrier(MEMBARRIER_CMD_QUERY, flags);
	if (ret < 0) {
		printf("failed. %s.\n", strerror(errno));
		switch (errno) {
		case ENOSYS:
			/*
			 * It is valid to build a kernel with
			 * CONFIG_MEMBARRIER=n. However, this skips the tests.
			 */
			return TEST_MEMBARRIER_SKIP;
		case EINVAL:
		default:
			return TEST_MEMBARRIER_FAIL;
		}
	}
	if (!(ret & MEMBARRIER_CMD_SHARED)) {
		printf("command MEMBARRIER_CMD_SHARED is not supported.\n");
		return TEST_MEMBARRIER_FAIL;
	}
	printf("syscall available.\n");
	return TEST_MEMBARRIER_PASS;
}

int main(int argc, char **argv)
{
	switch (test_membarrier_query()) {
	case TEST_MEMBARRIER_FAIL:
		return ksft_exit_fail();
	case TEST_MEMBARRIER_SKIP:
		return ksft_exit_skip();
	}
	switch (test_membarrier()) {
	case TEST_MEMBARRIER_FAIL:
		return ksft_exit_fail();
	case TEST_MEMBARRIER_SKIP:
		return ksft_exit_skip();
	}

	printf("membarrier: tests done!\n");
	return ksft_exit_pass();
}