Skip to content

Commit

Permalink
selftests: add membarrier syscall test
Browse files Browse the repository at this point in the history
Add a self test for the membarrier system call.

Signed-off-by: Pranith Kumar <[email protected]>
Signed-off-by: Mathieu Desnoyers <[email protected]>
Cc: Michael Ellerman <[email protected]>
Cc: Shuah Khan <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
pranith authored and torvalds committed Sep 11, 2015
1 parent 5b25b13 commit b6d9734
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/testing/selftests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ TARGETS += firmware
TARGETS += ftrace
TARGETS += futex
TARGETS += kcmp
TARGETS += membarrier
TARGETS += memfd
TARGETS += memory-hotplug
TARGETS += mount
Expand Down
1 change: 1 addition & 0 deletions tools/testing/selftests/membarrier/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
membarrier_test
11 changes: 11 additions & 0 deletions tools/testing/selftests/membarrier/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CFLAGS += -g -I../../../../usr/include/

all:
$(CC) $(CFLAGS) membarrier_test.c -o membarrier_test

TEST_PROGS := membarrier_test

include ../lib.mk

clean:
$(RM) membarrier_test
71 changes: 71 additions & 0 deletions tools/testing/selftests/membarrier/membarrier_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#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"

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

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

if (sys_membarrier(cmd, flags) != -1) {
printf("membarrier: Should fail but passed\n");
ksft_exit_fail();
}
}

static void test_membarrier_success(void)
{
int flags = 0;

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

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

static void test_membarrier(void)
{
test_membarrier_fail();
test_membarrier_success();
}

static int test_membarrier_exists(void)
{
int flags = 0;

if (sys_membarrier(MEMBARRIER_CMD_QUERY, flags))
return 0;

return 1;
}

int main(int argc, char **argv)
{
printf("membarrier: MEMBARRIER_CMD_QUERY ");
if (test_membarrier_exists()) {
printf("syscall implemented\n");
test_membarrier();
} else {
printf("syscall not implemented!\n");
return ksft_exit_fail();
}

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

return ksft_exit_pass();
}

0 comments on commit b6d9734

Please sign in to comment.