aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/testing/selftests/memfd/Makefile5
-rw-r--r--tools/testing/selftests/memfd/common.c46
-rw-r--r--tools/testing/selftests/memfd/common.h9
-rw-r--r--tools/testing/selftests/memfd/fuse_test.c8
-rw-r--r--tools/testing/selftests/memfd/memfd_test.c36
5 files changed, 64 insertions, 40 deletions
diff --git a/tools/testing/selftests/memfd/Makefile b/tools/testing/selftests/memfd/Makefile
index 3926a0409dda..a5276a91dfbf 100644
--- a/tools/testing/selftests/memfd/Makefile
+++ b/tools/testing/selftests/memfd/Makefile
@@ -12,3 +12,8 @@ fuse_mnt.o: CFLAGS += $(shell pkg-config fuse --cflags)
12include ../lib.mk 12include ../lib.mk
13 13
14$(OUTPUT)/fuse_mnt: LDLIBS += $(shell pkg-config fuse --libs) 14$(OUTPUT)/fuse_mnt: LDLIBS += $(shell pkg-config fuse --libs)
15
16$(OUTPUT)/memfd_test: memfd_test.c common.o
17$(OUTPUT)/fuse_test: fuse_test.c common.o
18
19EXTRA_CLEAN = common.o
diff --git a/tools/testing/selftests/memfd/common.c b/tools/testing/selftests/memfd/common.c
new file mode 100644
index 000000000000..8eb3d75f6e60
--- /dev/null
+++ b/tools/testing/selftests/memfd/common.c
@@ -0,0 +1,46 @@
1// SPDX-License-Identifier: GPL-2.0
2#define _GNU_SOURCE
3#define __EXPORTED_HEADERS__
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <linux/fcntl.h>
8#include <linux/memfd.h>
9#include <unistd.h>
10#include <sys/syscall.h>
11
12#include "common.h"
13
14int hugetlbfs_test = 0;
15
16/*
17 * Copied from mlock2-tests.c
18 */
19unsigned long default_huge_page_size(void)
20{
21 unsigned long hps = 0;
22 char *line = NULL;
23 size_t linelen = 0;
24 FILE *f = fopen("/proc/meminfo", "r");
25
26 if (!f)
27 return 0;
28 while (getline(&line, &linelen, f) > 0) {
29 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
30 hps <<= 10;
31 break;
32 }
33 }
34
35 free(line);
36 fclose(f);
37 return hps;
38}
39
40int sys_memfd_create(const char *name, unsigned int flags)
41{
42 if (hugetlbfs_test)
43 flags |= MFD_HUGETLB;
44
45 return syscall(__NR_memfd_create, name, flags);
46}
diff --git a/tools/testing/selftests/memfd/common.h b/tools/testing/selftests/memfd/common.h
new file mode 100644
index 000000000000..522d2c630bd8
--- /dev/null
+++ b/tools/testing/selftests/memfd/common.h
@@ -0,0 +1,9 @@
1#ifndef COMMON_H_
2#define COMMON_H_
3
4extern int hugetlbfs_test;
5
6unsigned long default_huge_page_size(void);
7int sys_memfd_create(const char *name, unsigned int flags);
8
9#endif
diff --git a/tools/testing/selftests/memfd/fuse_test.c b/tools/testing/selftests/memfd/fuse_test.c
index 1ccb7a3eb14b..795a25ba8521 100644
--- a/tools/testing/selftests/memfd/fuse_test.c
+++ b/tools/testing/selftests/memfd/fuse_test.c
@@ -33,15 +33,11 @@
33#include <sys/wait.h> 33#include <sys/wait.h>
34#include <unistd.h> 34#include <unistd.h>
35 35
36#include "common.h"
37
36#define MFD_DEF_SIZE 8192 38#define MFD_DEF_SIZE 8192
37#define STACK_SIZE 65536 39#define STACK_SIZE 65536
38 40
39static int sys_memfd_create(const char *name,
40 unsigned int flags)
41{
42 return syscall(__NR_memfd_create, name, flags);
43}
44
45static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags) 41static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
46{ 42{
47 int r, fd; 43 int r, fd;
diff --git a/tools/testing/selftests/memfd/memfd_test.c b/tools/testing/selftests/memfd/memfd_test.c
index 910c55f858bb..10baa1652fc2 100644
--- a/tools/testing/selftests/memfd/memfd_test.c
+++ b/tools/testing/selftests/memfd/memfd_test.c
@@ -19,6 +19,8 @@
19#include <sys/wait.h> 19#include <sys/wait.h>
20#include <unistd.h> 20#include <unistd.h>
21 21
22#include "common.h"
23
22#define MEMFD_STR "memfd:" 24#define MEMFD_STR "memfd:"
23#define MEMFD_HUGE_STR "memfd-hugetlb:" 25#define MEMFD_HUGE_STR "memfd-hugetlb:"
24#define SHARED_FT_STR "(shared file-table)" 26#define SHARED_FT_STR "(shared file-table)"
@@ -29,43 +31,9 @@
29/* 31/*
30 * Default is not to test hugetlbfs 32 * Default is not to test hugetlbfs
31 */ 33 */
32static int hugetlbfs_test;
33static size_t mfd_def_size = MFD_DEF_SIZE; 34static size_t mfd_def_size = MFD_DEF_SIZE;
34static const char *memfd_str = MEMFD_STR; 35static const char *memfd_str = MEMFD_STR;
35 36
36/*
37 * Copied from mlock2-tests.c
38 */
39static unsigned long default_huge_page_size(void)
40{
41 unsigned long hps = 0;
42 char *line = NULL;
43 size_t linelen = 0;
44 FILE *f = fopen("/proc/meminfo", "r");
45
46 if (!f)
47 return 0;
48 while (getline(&line, &linelen, f) > 0) {
49 if (sscanf(line, "Hugepagesize: %lu kB", &hps) == 1) {
50 hps <<= 10;
51 break;
52 }
53 }
54
55 free(line);
56 fclose(f);
57 return hps;
58}
59
60static int sys_memfd_create(const char *name,
61 unsigned int flags)
62{
63 if (hugetlbfs_test)
64 flags |= MFD_HUGETLB;
65
66 return syscall(__NR_memfd_create, name, flags);
67}
68
69static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags) 37static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
70{ 38{
71 int r, fd; 39 int r, fd;