Skip to content

Commit ff712a6

Browse files
mjkravetztorvalds
authored andcommitted
selftests/vm: cleanup hugetlb file after mremap test
The hugepage-mremap test will create a file in a hugetlb filesystem. In a default 'run_vmtests' run, the file will contain all the hugetlb pages. After the test, the file remains and there are no free hugetlb pages for subsequent tests. This causes those hugetlb tests to fail. Change hugepage-mremap to take the name of the hugetlb file as an argument. Unlink the file within the test, and just to be sure remove the file in the run_vmtests script. Link: https://lkml.kernel.org/r/20220201033459.156944-1-mike.kravetz@oracle.com Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com> Reviewed-by: Shuah Khan <skhan@linuxfoundation.org> Acked-by: Yosry Ahmed <yosryahmed@google.com> Reviewed-by: Muchun Song <songmuchun@bytedance.com> Reviewed-by: Mina Almasry <almasrymina@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 07ebd38 commit ff712a6

2 files changed

Lines changed: 21 additions & 8 deletions

File tree

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
* hugepage-mremap:
44
*
55
* Example of remapping huge page memory in a user application using the
6-
* mremap system call. Code assumes a hugetlbfs filesystem is mounted
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.
6+
* mremap system call. The path to a file in a hugetlbfs filesystem must
7+
* be passed as the last argument to this test. The amount of memory used
8+
* by this test in MBs can optionally be passed as an argument. If no memory
9+
* amount is passed, the default amount is 10MB.
910
*
1011
* To make sure the test triggers pmd sharing and goes through the 'unshare'
1112
* path in the mremap code use 1GB (1024) or more.
@@ -25,7 +26,6 @@
2526
#define DEFAULT_LENGTH_MB 10UL
2627
#define MB_TO_BYTES(x) (x * 1024 * 1024)
2728

28-
#define FILE_NAME "huge/hugepagefile"
2929
#define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC)
3030
#define FLAGS (MAP_SHARED | MAP_ANONYMOUS)
3131

@@ -107,17 +107,26 @@ static void register_region_with_uffd(char *addr, size_t len)
107107

108108
int main(int argc, char *argv[])
109109
{
110+
size_t length;
111+
112+
if (argc != 2 && argc != 3) {
113+
printf("Usage: %s [length_in_MB] <hugetlb_file>\n", argv[0]);
114+
exit(1);
115+
}
116+
110117
/* Read memory length as the first arg if valid, otherwise fallback to
111-
* the default length. Any additional args are ignored.
118+
* the default length.
112119
*/
113-
size_t length = argc > 1 ? (size_t)atoi(argv[1]) : 0UL;
120+
if (argc == 3)
121+
length = argc > 2 ? (size_t)atoi(argv[1]) : 0UL;
114122

115123
length = length > 0 ? length : DEFAULT_LENGTH_MB;
116124
length = MB_TO_BYTES(length);
117125

118126
int ret = 0;
119127

120-
int fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
128+
/* last arg is the hugetlb file name */
129+
int fd = open(argv[argc-1], O_CREAT | O_RDWR, 0755);
121130

122131
if (fd < 0) {
123132
perror("Open failed");
@@ -169,5 +178,8 @@ int main(int argc, char *argv[])
169178

170179
munmap(addr, length);
171180

181+
close(fd);
182+
unlink(argv[argc-1]);
183+
172184
return ret;
173185
}

tools/testing/selftests/vm/run_vmtests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,14 @@ fi
111111
echo "-----------------------"
112112
echo "running hugepage-mremap"
113113
echo "-----------------------"
114-
./hugepage-mremap 256
114+
./hugepage-mremap $mnt/huge_mremap
115115
if [ $? -ne 0 ]; then
116116
echo "[FAIL]"
117117
exitcode=1
118118
else
119119
echo "[PASS]"
120120
fi
121+
rm -f $mnt/huge_mremap
121122

122123
echo "NOTE: The above hugetlb tests provide minimal coverage. Use"
123124
echo " https://github.com/libhugetlbfs/libhugetlbfs.git for"

0 commit comments

Comments
 (0)