Skip to content

Commit 29f34d1

Browse files
elmarcotorvalds
authored andcommitted
memfd-test: move common code to a shared unit
The memfd & fuse tests will share more common code in the following commits to test hugetlb support. Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Marc-André Lureau <[email protected]> Reviewed-by: Mike Kravetz <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Michal Hocko <[email protected]> Cc: David Herrmann <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3037aeb commit 29f34d1

File tree

5 files changed

+64
-40
lines changed

5 files changed

+64
-40
lines changed

tools/testing/selftests/memfd/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ fuse_mnt.o: CFLAGS += $(shell pkg-config fuse --cflags)
1212
include ../lib.mk
1313

1414
$(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+
19+
EXTRA_CLEAN = common.o
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
14+
int hugetlbfs_test = 0;
15+
16+
/*
17+
* Copied from mlock2-tests.c
18+
*/
19+
unsigned 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+
40+
int 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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef COMMON_H_
2+
#define COMMON_H_
3+
4+
extern int hugetlbfs_test;
5+
6+
unsigned long default_huge_page_size(void);
7+
int sys_memfd_create(const char *name, unsigned int flags);
8+
9+
#endif

tools/testing/selftests/memfd/fuse_test.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,11 @@
3333
#include <sys/wait.h>
3434
#include <unistd.h>
3535

36+
#include "common.h"
37+
3638
#define MFD_DEF_SIZE 8192
3739
#define STACK_SIZE 65536
3840

39-
static int sys_memfd_create(const char *name,
40-
unsigned int flags)
41-
{
42-
return syscall(__NR_memfd_create, name, flags);
43-
}
44-
4541
static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
4642
{
4743
int r, fd;

tools/testing/selftests/memfd/memfd_test.c

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <sys/wait.h>
2020
#include <unistd.h>
2121

22+
#include "common.h"
23+
2224
#define MEMFD_STR "memfd:"
2325
#define MEMFD_HUGE_STR "memfd-hugetlb:"
2426
#define SHARED_FT_STR "(shared file-table)"
@@ -29,43 +31,9 @@
2931
/*
3032
* Default is not to test hugetlbfs
3133
*/
32-
static int hugetlbfs_test;
3334
static size_t mfd_def_size = MFD_DEF_SIZE;
3435
static const char *memfd_str = MEMFD_STR;
3536

36-
/*
37-
* Copied from mlock2-tests.c
38-
*/
39-
static 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-
60-
static 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-
6937
static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
7038
{
7139
int r, fd;

0 commit comments

Comments
 (0)