summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/membarrier/membarrier_test.c
blob: 9e674d9514d1d14b43e32c58ef530991f7f3561e (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
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <linux/membarrier.h>
#include <syscall.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

#include "../kselftest.h"

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

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

	if (sys_membarrier(cmd, flags) != -1) {
		ksft_exit_fail_msg(
			"sys membarrier invalid command test: command = %d, flags = %d. Should fail, but passed\n",
			cmd, flags);
	}

	ksft_test_result_pass(
		"sys membarrier invalid command test: command = %d, flags = %d. Failed as expected\n",
		cmd, flags);
	return 0;
}

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

	if (sys_membarrier(cmd, flags) != -1) {
		ksft_exit_fail_msg(
			"sys membarrier MEMBARRIER_CMD_QUERY invalid flags test: flags = %d. Should fail, but passed\n",
			flags);
	}

	ksft_test_result_pass(
		"sys membarrier MEMBARRIER_CMD_QUERY invalid flags test: flags = %d. Failed as expected\n",
		flags);
	return 0;
}

static int test_membarrier_success(void)
{
	int cmd = MEMBARRIER_CMD_SHARED, flags = 0;
	const char *test_name = "sys membarrier MEMBARRIER_CMD_SHARED\n";

	if (sys_membarrier(cmd, flags) != 0) {
		ksft_exit_fail_msg(
			"sys membarrier MEMBARRIER_CMD_SHARED test: flags = %d\n",
			flags);
	}

	ksft_test_result_pass(
		"sys membarrier MEMBARRIER_CMD_SHARED test: flags = %d\n",
		flags);
	return 0;
}

static int test_membarrier(void)
{
	int 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 0;
}

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

	ret = sys_membarrier(MEMBARRIER_CMD_QUERY, flags);
	if (ret < 0) {
		if (errno == ENOSYS) {
			/*
			 * It is valid to build a kernel with
			 * CONFIG_MEMBARRIER=n. However, this skips the tests.
			 */
			ksft_exit_skip(
				"sys membarrier (CONFIG_MEMBARRIER) is disabled.\n");
		}
		ksft_exit_fail_msg("sys_membarrier() failed\n");
	}
	if (!(ret & MEMBARRIER_CMD_SHARED))
		ksft_exit_fail_msg("sys_membarrier is not supported.\n");

	ksft_test_result_pass("sys_membarrier available\n");
	return 0;
}

int main(int argc, char **argv)
{
	ksft_print_header();

	test_membarrier_query();
	test_membarrier();

	ksft_exit_pass();
}