Skip to content

Commit

Permalink
selftests/vm: cleanup hugetlb file after mremap test
Browse files Browse the repository at this point in the history
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/[email protected]
Signed-off-by: Mike Kravetz <[email protected]>
Reviewed-by: Shuah Khan <[email protected]>
Acked-by: Yosry Ahmed <[email protected]>
Reviewed-by: Muchun Song <[email protected]>
Reviewed-by: Mina Almasry <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
mjkravetz authored and torvalds committed Mar 5, 2022
1 parent 07ebd38 commit ff712a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
26 changes: 19 additions & 7 deletions tools/testing/selftests/vm/hugepage-mremap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* hugepage-mremap:
*
* Example of remapping huge page memory in a user application using the
* mremap system call. Code assumes a hugetlbfs filesystem is mounted
* at './huge'. The amount of memory used by this test is decided by a command
* line argument in MBs. If missing, the default amount is 10MB.
* mremap system call. The path to a file in a hugetlbfs filesystem must
* be passed as the last argument to this test. The amount of memory used
* by this test in MBs can optionally be passed as an argument. If no memory
* amount is passed, the default amount is 10MB.
*
* To make sure the test triggers pmd sharing and goes through the 'unshare'
* path in the mremap code use 1GB (1024) or more.
Expand All @@ -25,7 +26,6 @@
#define DEFAULT_LENGTH_MB 10UL
#define MB_TO_BYTES(x) (x * 1024 * 1024)

#define FILE_NAME "huge/hugepagefile"
#define PROTECTION (PROT_READ | PROT_WRITE | PROT_EXEC)
#define FLAGS (MAP_SHARED | MAP_ANONYMOUS)

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

int main(int argc, char *argv[])
{
size_t length;

if (argc != 2 && argc != 3) {
printf("Usage: %s [length_in_MB] <hugetlb_file>\n", argv[0]);
exit(1);
}

/* Read memory length as the first arg if valid, otherwise fallback to
* the default length. Any additional args are ignored.
* the default length.
*/
size_t length = argc > 1 ? (size_t)atoi(argv[1]) : 0UL;
if (argc == 3)
length = argc > 2 ? (size_t)atoi(argv[1]) : 0UL;

length = length > 0 ? length : DEFAULT_LENGTH_MB;
length = MB_TO_BYTES(length);

int ret = 0;

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

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

munmap(addr, length);

close(fd);
unlink(argv[argc-1]);

return ret;
}
3 changes: 2 additions & 1 deletion tools/testing/selftests/vm/run_vmtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ fi
echo "-----------------------"
echo "running hugepage-mremap"
echo "-----------------------"
./hugepage-mremap 256
./hugepage-mremap $mnt/huge_mremap
if [ $? -ne 0 ]; then
echo "[FAIL]"
exitcode=1
else
echo "[PASS]"
fi
rm -f $mnt/huge_mremap

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

0 comments on commit ff712a6

Please sign in to comment.