Skip to content

Commit b6d9734

Browse files
pranithtorvalds
authored andcommitted
selftests: add membarrier syscall test
Add a self test for the membarrier system call. Signed-off-by: Pranith Kumar <bobby.prani@gmail.com> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Michael Ellerman <mpe@ellerman.id.au> Cc: Shuah Khan <shuahkh@osg.samsung.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5b25b13 commit b6d9734

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ TARGETS += firmware
66
TARGETS += ftrace
77
TARGETS += futex
88
TARGETS += kcmp
9+
TARGETS += membarrier
910
TARGETS += memfd
1011
TARGETS += memory-hotplug
1112
TARGETS += mount
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
membarrier_test
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CFLAGS += -g -I../../../../usr/include/
2+
3+
all:
4+
$(CC) $(CFLAGS) membarrier_test.c -o membarrier_test
5+
6+
TEST_PROGS := membarrier_test
7+
8+
include ../lib.mk
9+
10+
clean:
11+
$(RM) membarrier_test
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#define _GNU_SOURCE
2+
#define __EXPORTED_HEADERS__
3+
4+
#include <linux/membarrier.h>
5+
#include <asm-generic/unistd.h>
6+
#include <sys/syscall.h>
7+
#include <stdio.h>
8+
#include <errno.h>
9+
#include <string.h>
10+
11+
#include "../kselftest.h"
12+
13+
static int sys_membarrier(int cmd, int flags)
14+
{
15+
return syscall(__NR_membarrier, cmd, flags);
16+
}
17+
18+
static void test_membarrier_fail(void)
19+
{
20+
int cmd = -1, flags = 0;
21+
22+
if (sys_membarrier(cmd, flags) != -1) {
23+
printf("membarrier: Should fail but passed\n");
24+
ksft_exit_fail();
25+
}
26+
}
27+
28+
static void test_membarrier_success(void)
29+
{
30+
int flags = 0;
31+
32+
if (sys_membarrier(MEMBARRIER_CMD_SHARED, flags) != 0) {
33+
printf("membarrier: Executing MEMBARRIER failed, %s\n",
34+
strerror(errno));
35+
ksft_exit_fail();
36+
}
37+
38+
printf("membarrier: MEMBARRIER_CMD_SHARED success\n");
39+
}
40+
41+
static void test_membarrier(void)
42+
{
43+
test_membarrier_fail();
44+
test_membarrier_success();
45+
}
46+
47+
static int test_membarrier_exists(void)
48+
{
49+
int flags = 0;
50+
51+
if (sys_membarrier(MEMBARRIER_CMD_QUERY, flags))
52+
return 0;
53+
54+
return 1;
55+
}
56+
57+
int main(int argc, char **argv)
58+
{
59+
printf("membarrier: MEMBARRIER_CMD_QUERY ");
60+
if (test_membarrier_exists()) {
61+
printf("syscall implemented\n");
62+
test_membarrier();
63+
} else {
64+
printf("syscall not implemented!\n");
65+
return ksft_exit_fail();
66+
}
67+
68+
printf("membarrier: tests done!\n");
69+
70+
return ksft_exit_pass();
71+
}

0 commit comments

Comments
 (0)