Skip to content

Commit f77a286

Browse files
yosrym93torvalds
authored andcommitted
mm, hugepages: make memory size variable in hugepage-mremap selftest
The hugetlb vma mremap() test currently maps 1GB of memory to trigger pmd sharing and make sure that 'unshare' path in mremap code works. The test originally only mapped 10MB of memory (as specified by the header comment) but was later modified to 1GB to tackle this case. However, not all machines will have 1GB of memory to spare for this test. Adding a mapping size arg will allow run_vmtest.sh to pass an adequate mapping size, while allowing users to run the test independently with arbitrary size mappings. Link: https://lkml.kernel.org/r/20211124203805.3700355-1-yosryahmed@google.com Signed-off-by: Yosry Ahmed <yosryahmed@google.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Mina Almasry <almasrymina@google.com> Cc: Mike Kravetz <mike.kravetz@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent f477619 commit f77a286

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

tools/testing/selftests/vm/hugepage-mremap.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
*
55
* Example of remapping huge page memory in a user application using the
66
* mremap system call. Code assumes a hugetlbfs filesystem is mounted
7-
* at './huge'. The code will use 10MB worth of huge pages.
7+
* at './huge'. The amount of memory used by this test is decided by a command
8+
* line argument in MBs. If missing, the default amount is 10MB.
9+
*
10+
* To make sure the test triggers pmd sharing and goes through the 'unshare'
11+
* path in the mremap code use 1GB (1024) or more.
812
*/
913

1014
#define _GNU_SOURCE
@@ -18,8 +22,10 @@
1822
#include <linux/userfaultfd.h>
1923
#include <sys/ioctl.h>
2024

21-
#define LENGTH (1UL * 1024 * 1024 * 1024)
25+
#define DEFAULT_LENGTH_MB 10UL
26+
#define MB_TO_BYTES(x) (x * 1024 * 1024)
2227

28+
#define FILE_NAME "huge/hugepagefile"
2329
#define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC)
2430
#define FLAGS (MAP_SHARED | MAP_ANONYMOUS)
2531

@@ -28,20 +34,20 @@ static void check_bytes(char *addr)
2834
printf("First hex is %x\n", *((unsigned int *)addr));
2935
}
3036

31-
static void write_bytes(char *addr)
37+
static void write_bytes(char *addr, size_t len)
3238
{
3339
unsigned long i;
3440

35-
for (i = 0; i < LENGTH; i++)
41+
for (i = 0; i < len; i++)
3642
*(addr + i) = (char)i;
3743
}
3844

39-
static int read_bytes(char *addr)
45+
static int read_bytes(char *addr, size_t len)
4046
{
4147
unsigned long i;
4248

4349
check_bytes(addr);
44-
for (i = 0; i < LENGTH; i++)
50+
for (i = 0; i < len; i++)
4551
if (*(addr + i) != (char)i) {
4652
printf("Mismatch at %lu\n", i);
4753
return 1;
@@ -99,11 +105,19 @@ static void register_region_with_uffd(char *addr, size_t len)
99105
}
100106
}
101107

102-
int main(void)
108+
int main(int argc, char *argv[])
103109
{
110+
/* Read memory length as the first arg if valid, otherwise fallback to
111+
* the default length. Any additional args are ignored.
112+
*/
113+
size_t length = argc > 1 ? (size_t)atoi(argv[1]) : 0UL;
114+
115+
length = length > 0 ? length : DEFAULT_LENGTH_MB;
116+
length = MB_TO_BYTES(length);
117+
104118
int ret = 0;
105119

106-
int fd = open("/huge/test", O_CREAT | O_RDWR, 0755);
120+
int fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
107121

108122
if (fd < 0) {
109123
perror("Open failed");
@@ -112,7 +126,7 @@ int main(void)
112126

113127
/* mmap to a PUD aligned address to hopefully trigger pmd sharing. */
114128
unsigned long suggested_addr = 0x7eaa40000000;
115-
void *haddr = mmap((void *)suggested_addr, LENGTH, PROTECTION,
129+
void *haddr = mmap((void *)suggested_addr, length, PROTECTION,
116130
MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
117131
printf("Map haddr: Returned address is %p\n", haddr);
118132
if (haddr == MAP_FAILED) {
@@ -122,7 +136,7 @@ int main(void)
122136

123137
/* mmap again to a dummy address to hopefully trigger pmd sharing. */
124138
suggested_addr = 0x7daa40000000;
125-
void *daddr = mmap((void *)suggested_addr, LENGTH, PROTECTION,
139+
void *daddr = mmap((void *)suggested_addr, length, PROTECTION,
126140
MAP_HUGETLB | MAP_SHARED | MAP_POPULATE, fd, 0);
127141
printf("Map daddr: Returned address is %p\n", daddr);
128142
if (daddr == MAP_FAILED) {
@@ -132,16 +146,16 @@ int main(void)
132146

133147
suggested_addr = 0x7faa40000000;
134148
void *vaddr =
135-
mmap((void *)suggested_addr, LENGTH, PROTECTION, FLAGS, -1, 0);
149+
mmap((void *)suggested_addr, length, PROTECTION, FLAGS, -1, 0);
136150
printf("Map vaddr: Returned address is %p\n", vaddr);
137151
if (vaddr == MAP_FAILED) {
138152
perror("mmap2");
139153
exit(1);
140154
}
141155

142-
register_region_with_uffd(haddr, LENGTH);
156+
register_region_with_uffd(haddr, length);
143157

144-
void *addr = mremap(haddr, LENGTH, LENGTH,
158+
void *addr = mremap(haddr, length, length,
145159
MREMAP_MAYMOVE | MREMAP_FIXED, vaddr);
146160
if (addr == MAP_FAILED) {
147161
perror("mremap");
@@ -150,10 +164,10 @@ int main(void)
150164

151165
printf("Mremap: Returned address is %p\n", addr);
152166
check_bytes(addr);
153-
write_bytes(addr);
154-
ret = read_bytes(addr);
167+
write_bytes(addr, length);
168+
ret = read_bytes(addr, length);
155169

156-
munmap(addr, LENGTH);
170+
munmap(addr, length);
157171

158172
return ret;
159173
}

tools/testing/selftests/vm/run_vmtests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fi
111111
echo "-----------------------"
112112
echo "running hugepage-mremap"
113113
echo "-----------------------"
114-
./hugepage-mremap
114+
./hugepage-mremap 256
115115
if [ $? -ne 0 ]; then
116116
echo "[FAIL]"
117117
exitcode=1

0 commit comments

Comments
 (0)