Skip to contentSkip to conversation

selftests/vm: cleanup hugetlb file after mremap test

PATCH Applied Mike Kravetz 5 participants 5 messages Feb 1, 2022 – Feb 2, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 tools/testing/selftests/vm/hugepage-mremap.c | 26 ++++++++++++++------
 tools/testing/selftests/vm/run_vmtests.sh    |  3 ++-
 2 files changed, 21 insertions(+), 8 deletions(-)
tools/testing/selftests/vm/hugepage-mremap.c +19 -7
index 2a7c33631a29..1d689084a54b 100644
@@ -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.
@@ -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)
 
@@ -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");
@@ -169,5 +178,8 @@ int main(int argc, char *argv[])
 
 	munmap(addr, length);
 
+	close(fd);
+	unlink(argv[argc-1]);
+
 	return ret;
 }
tools/testing/selftests/vm/run_vmtests.sh +2 -1
index e09040a3dc08..e10d50e0b8e8 100755
@@ -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 "------------------------"
 echo "running hugepage-vmemmap"
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 50 lines · Mike Kravetz

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.

---
  tools/testing/selftests/vm/hugepage-mremap.c | 26 ++++++++++++++------
  tools/testing/selftests/vm/run_vmtests.sh    |  3 ++-
  2 files changed, 21 insertions(+), 8 deletions(-)
tools/testing/selftests/vm/hugepage-mremap.c +9 -4
index 2a7c33631a29..1d689084a54b 100644
@@ -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.
@@ -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)
 
@@ -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);

tools/testing/selftests/vm/hugepage-mremap.c static void register_region_with_uffd(char *addr, size_t len) · line 122

Quoted 10 lines
tools/testing/selftests/vm/hugepage-mremap.c +5 -2
+	}
+
  	/* 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;
 

Looks like getopt() might be useful - we have a few arguments now. Makes it easier to maintain the test.

Quoted 31 lines
tools/testing/selftests/vm/hugepage-mremap.c +5 -1
  	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");
@@ -169,5 +178,8 @@ int main(int argc, char *argv[])
 
  	munmap(addr, length);
 
+	close(fd);
+	unlink(argv[argc-1]);
+
  	return ret;
  }
tools/testing/selftests/vm/run_vmtests.sh +1 -1
index e09040a3dc08..e10d50e0b8e8 100755
@@ -111,13 +111,14 @@ fi
  echo "-----------------------"
  echo "running hugepage-mremap"
  echo "-----------------------"
-./hugepage-mremap 256
+./hugepage-mremap $mnt/huge_mremap

tools/testing/selftests/vm/run_vmtests.sh fi · line 124

Quoted 10 lines
tools/testing/selftests/vm/run_vmtests.sh +1 -0
  if [ $? -ne 0 ]; then
  	echo "[FAIL]"
  	exitcode=1
  else
  	echo "[PASS]"
  fi
+rm -f $mnt/huge_mremap
 
  echo "------------------------"
  echo "running hugepage-vmemmap"

thanks,
-- Shuah

Reviewed-by on v1 1/1
#2 lore raw
In reply to Mike Kravetz
Quoted 11 lines · Mike Kravetz

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.

Thanks Mike!

Quoted 93 lines
---
 tools/testing/selftests/vm/hugepage-mremap.c | 26 ++++++++++++++------
 tools/testing/selftests/vm/run_vmtests.sh    |  3 ++-
 2 files changed, 21 insertions(+), 8 deletions(-)
tools/testing/selftests/vm/hugepage-mremap.c +19 -7
index 2a7c33631a29..1d689084a54b 100644
@@ -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.
@@ -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)
 
@@ -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");
@@ -169,5 +178,8 @@ int main(int argc, char *argv[])
 
        munmap(addr, length);
 
+       close(fd);
+       unlink(argv[argc-1]);
+
        return ret;
 }
tools/testing/selftests/vm/run_vmtests.sh +2 -2
index e09040a3dc08..e10d50e0b8e8 100755
@@ -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 "------------------------"
 echo "running hugepage-vmemmap"
--

2.34.1

Reviewed-by on v1 1/1
#3 lore raw
In reply to Mike Kravetz
Quoted 12 lines · Mike Kravetz

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.

---

[...]

Quoted 8 lines
tools/testing/selftests/vm/run_vmtests.sh +0 -0
index e09040a3dc08..e10d50e0b8e8 100755
@@ -111,13 +111,14 @@ fi
 echo "-----------------------"
 echo "running hugepage-mremap"
 echo "-----------------------"

tools/testing/selftests/vm/run_vmtests.sh fi · line 122

Quoted 10 lines
tools/testing/selftests/vm/run_vmtests.sh +2 -1
-./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
 

A little strange (since file creation and deletion are not in the same place). I know $mnt/huge_mremap is created in hugepage-mremap. But I think hugepage-mremap should do the housekeeping instead of deleting the file here. Or we can create the file before doing the test. Like:

+ touch $mnt/huge_mremap
-./hugepage-mremap 256
+./hugepage-mremap $mnt/huge_mremap
+rm -f $mnt/huge_mremap

Otherwise LGTM.

Thanks.

Acked-by on v1 1/1
#4 lore raw
In reply to Mike Kravetz

Thanks Mike for working on this!

Quoted 10 lines · Mike Kravetz

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.

The hugepage-mmap test also uses a hugetlb file in the same fashion. I think the missing close() and unlink() calls were the only difference (thanks for catching that).
I would suggest for consistency purposes to either make both tests take the filename as an argument or not, fine either way.

Quoted 40 lines
---
 tools/testing/selftests/vm/hugepage-mremap.c | 26 ++++++++++++++------
 tools/testing/selftests/vm/run_vmtests.sh    |  3 ++-
 2 files changed, 21 insertions(+), 8 deletions(-)
tools/testing/selftests/vm/hugepage-mremap.c +8 -4
index 2a7c33631a29..1d689084a54b 100644
@@ -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.
@@ -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)
 
@@ -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]);

tools/testing/selftests/vm/hugepage-mremap.c static void register_region_with_uffd(char *addr, size_t len) · line 121

Quoted 10 lines
tools/testing/selftests/vm/hugepage-mremap.c +6 -2
+               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;

I think the ternary operator is not needed here as it will always be true if we enter the if block.

Otherwise, LGTM.

Quoted 44 lines
tools/testing/selftests/vm/hugepage-mremap.c +5 -1
        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");
@@ -169,5 +178,8 @@ int main(int argc, char *argv[])
 
        munmap(addr, length);
 
+       close(fd);
+       unlink(argv[argc-1]);
+
        return ret;
 }
tools/testing/selftests/vm/run_vmtests.sh +2 -2
index e09040a3dc08..e10d50e0b8e8 100755
@@ -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 "------------------------"
 echo "running hugepage-vmemmap"
--

2.34.1