Skip to contentSkip to conversation

selftests/mm: fix charge_reserved_hugetlb.sh test

PATCH Applied David Hildenbrand 3 participants 5 messages Aug 21, 2024 – Aug 26, 2024

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

PATCH 1/1 selftests/mm: fix charge_reserved_hugetlb.sh test

Currently, running the charge_reserved_hugetlb.sh selftest we can sometimes observe something like:

  $ ./charge_reserved_hugetlb.sh -cgroup-v2
  ...
  write_result is 0
  After write:
  hugetlb_usage=0
  reserved_usage=10485760
  killing write_to_hugetlbfs
  Received 2.
  Deleting the memory
  Detach failure: Invalid argument
  umount: /mnt/huge: target is busy.

Both cases are issues in the test.

While the unmount error seems to be racy, it will make the test fail:

	$ ./run_vmtests.sh -t hugetlb
	...
	# [FAIL]
	not ok 10 charge_reserved_hugetlb.sh -cgroup-v2 # exit=32

The issue is that we are not waiting for the write_to_hugetlbfs process to quit. So it might still have a hugetlbfs file open, about which umount is not happy. Fix that by making "killall" wait for the process to quit.

The other error ("Detach failure: Invalid argument") does not seem to result in a test error, but is misleading. Turns out write_to_hugetlbfs.c unconditionally tries to cleanup using shmdt(), even when we only mmap()'ed a hugetlb file. Even worse, shmaddr is never even set for the SHM case. Fix that as well.

With this change it seems to work as expected.

Fixes 29750f71a9b4 ("hugetlb_cgroup: add hugetlb_cgroup reservation tests")
Cc Andrew Morton <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Muchun Song <[email protected]>
Cc Mina Almasry <[email protected]>
---
 .../selftests/mm/charge_reserved_hugetlb.sh   |  2 +-
 .../testing/selftests/mm/write_to_hugetlbfs.c | 21 +++++++++++--------
 2 files changed, 13 insertions(+), 10 deletions(-)
tools/testing/selftests/mm/charge_reserved_hugetlb.sh +1 -1
index d680c00d2853a..67df7b47087f0 100755
@@ -254,7 +254,7 @@ function cleanup_hugetlb_memory() {
   local cgroup="$1"
   if [[ "$(pgrep -f write_to_hugetlbfs)" != "" ]]; then
     echo killing write_to_hugetlbfs
-    killall -2 write_to_hugetlbfs
+    killall -2 --wait write_to_hugetlbfs
     wait_for_hugetlb_memory_to_get_depleted $cgroup
   fi
   set -e
tools/testing/selftests/mm/write_to_hugetlbfs.c +12 -9
index 6a2caba19ee1d..1289d311efd70 100644
@@ -28,7 +28,7 @@ enum method {
 
 /* Global variables. */
 static const char *self;
-static char *shmaddr;
+static int *shmaddr;
 static int shmid;
 
 /*
@@ -47,15 +47,17 @@ void sig_handler(int signo)
 {
 	printf("Received %d.\n", signo);
 	if (signo == SIGINT) {
-		printf("Deleting the memory\n");
-		if (shmdt((const void *)shmaddr) != 0) {
-			perror("Detach failure");
+		if (shmaddr) {
+			printf("Deleting the memory\n");
+			if (shmdt((const void *)shmaddr) != 0) {
+				perror("Detach failure");
+				shmctl(shmid, IPC_RMID, NULL);
+				exit(4);
+			}
+
 			shmctl(shmid, IPC_RMID, NULL);
-			exit(4);
+			printf("Done deleting the memory\n");
 		}
-
-		shmctl(shmid, IPC_RMID, NULL);
-		printf("Done deleting the memory\n");
 	}
 	exit(2);
 }
@@ -211,7 +213,8 @@ int main(int argc, char **argv)
 			shmctl(shmid, IPC_RMID, NULL);
 			exit(2);
 		}
-		printf("shmaddr: %p\n", ptr);
+		shmaddr = ptr;
+		printf("shmaddr: %p\n", shmaddr);
 
 		break;
 	default:
Signature · 1 line
2.46.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 37 lines · David Hildenbrand

Currently, running the charge_reserved_hugetlb.sh selftest we can sometimes observe something like:

  $ ./charge_reserved_hugetlb.sh -cgroup-v2
  ...
  write_result is 0
  After write:
  hugetlb_usage=0
  reserved_usage=10485760
  killing write_to_hugetlbfs
  Received 2.
  Deleting the memory
  Detach failure: Invalid argument
  umount: /mnt/huge: target is busy.

Both cases are issues in the test.

While the unmount error seems to be racy, it will make the test fail:

        $ ./run_vmtests.sh -t hugetlb
        ...
        # [FAIL]
        not ok 10 charge_reserved_hugetlb.sh -cgroup-v2 # exit=32

The issue is that we are not waiting for the write_to_hugetlbfs process to quit. So it might still have a hugetlbfs file open, about which umount is not happy. Fix that by making "killall" wait for the process to quit.

The other error ("Detach failure: Invalid argument") does not seem to result in a test error, but is misleading. Turns out write_to_hugetlbfs.c unconditionally tries to cleanup using shmdt(), even when we only mmap()'ed a hugetlb file. Even worse, shmaddr is never even set for the SHM case. Fix that as well.

With this change it seems to work as expected.

Quoted 7 lines
Fixes 29750f71a9b4 ("hugetlb_cgroup: add hugetlb_cgroup reservation tests")
Cc Andrew Morton <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Muchun Song <[email protected]>
Cc Mina Almasry <[email protected]>

Initially I thought it could be nice to split fixes for the 2 issues in separate patches in case one of them ends up needing a revert or something, but probably not worth a respin. Fixes look good to me.

Quoted 10 lines
---
 .../selftests/mm/charge_reserved_hugetlb.sh   |  2 +-
 .../testing/selftests/mm/write_to_hugetlbfs.c | 21 +++++++++++--------
 2 files changed, 13 insertions(+), 10 deletions(-)
tools/testing/selftests/mm/charge_reserved_hugetlb.sh +0 -0
index d680c00d2853a..67df7b47087f0 100755

tools/testing/selftests/mm/charge_reserved_hugetlb.sh function cleanup_hugetlb_memory() { · line 257

Quoted 5 lines
tools/testing/selftests/mm/charge_reserved_hugetlb.sh +1 -1
@@ -254,7 +254,7 @@ function cleanup_hugetlb_memory() {
   local cgroup="$1"
   if [[ "$(pgrep -f write_to_hugetlbfs)" != "" ]]; then
     echo killing write_to_hugetlbfs
-    killall -2 write_to_hugetlbfs
+    killall -2 --wait write_to_hugetlbfs

This looks correct. I don't think I expected killall not to wait.

Quoted 28 lines
tools/testing/selftests/mm/charge_reserved_hugetlb.sh +0 -0
     wait_for_hugetlb_memory_to_get_depleted $cgroup
   fi
   set -e
tools/testing/selftests/mm/write_to_hugetlbfs.c +6 -4
index 6a2caba19ee1d..1289d311efd70 100644
@@ -28,7 +28,7 @@ enum method {
 
 /* Global variables. */
 static const char *self;
-static char *shmaddr;
+static int *shmaddr;
 static int shmid;
 
 /*
@@ -47,15 +47,17 @@ void sig_handler(int signo)
 {
        printf("Received %d.\n", signo);
        if (signo == SIGINT) {
-               printf("Deleting the memory\n");
-               if (shmdt((const void *)shmaddr) != 0) {
-                       perror("Detach failure");
+               if (shmaddr) {
+                       printf("Deleting the memory\n");
+                       if (shmdt((const void *)shmaddr) != 0) {
+                               perror("Detach failure");
+                               shmctl(shmid, IPC_RMID, NULL);

tools/testing/selftests/mm/write_to_hugetlbfs.c void sig_handler(int signo) · line 58

Quoted 10 lines
tools/testing/selftests/mm/write_to_hugetlbfs.c +4 -4
+                               exit(4);
+                       }
+
                        shmctl(shmid, IPC_RMID, NULL);
-                       exit(4);
+                       printf("Done deleting the memory\n");
                }
-
-               shmctl(shmid, IPC_RMID, NULL);
-               printf("Done deleting the memory\n");

This seems like a simple refactor to only delete when shmaddr is set, looks fine to me.

tools/testing/selftests/mm/write_to_hugetlbfs.c int main(int argc, char **argv) · line 218

Quoted 11 lines
tools/testing/selftests/mm/write_to_hugetlbfs.c +2 -1
        }
        exit(2);
 }
@@ -211,7 +213,8 @@ int main(int argc, char **argv)
                        shmctl(shmid, IPC_RMID, NULL);
                        exit(2);
                }
-               printf("shmaddr: %p\n", ptr);
+               shmaddr = ptr;
+               printf("shmaddr: %p\n", shmaddr);
 

Setting shmaddr seems correct and an oversight. I don't see shmaddr set anywhere in the current code.

Quoted 5 lines
tools/testing/selftests/mm/write_to_hugetlbfs.c +0 -1
                break;
        default:
--

2.46.0

Signature · 2 lines
Thanks,
Mina
#2 lore raw
Quoted 46 lines · Mina Almasry
Quoted 37 lines · David Hildenbrand

Currently, running the charge_reserved_hugetlb.sh selftest we can sometimes observe something like:

   $ ./charge_reserved_hugetlb.sh -cgroup-v2
   ...
   write_result is 0
   After write:
   hugetlb_usage=0
   reserved_usage=10485760
   killing write_to_hugetlbfs
   Received 2.
   Deleting the memory
   Detach failure: Invalid argument
   umount: /mnt/huge: target is busy.

Both cases are issues in the test.

While the unmount error seems to be racy, it will make the test fail:

         $ ./run_vmtests.sh -t hugetlb
         ...
         # [FAIL]
         not ok 10 charge_reserved_hugetlb.sh -cgroup-v2 # exit=32

The issue is that we are not waiting for the write_to_hugetlbfs process to quit. So it might still have a hugetlbfs file open, about which umount is not happy. Fix that by making "killall" wait for the process to quit.

The other error ("Detach failure: Invalid argument") does not seem to result in a test error, but is misleading. Turns out write_to_hugetlbfs.c unconditionally tries to cleanup using shmdt(), even when we only mmap()'ed a hugetlb file. Even worse, shmaddr is never even set for the SHM case. Fix that as well.

With this change it seems to work as expected.

Quoted 7 lines
Fixes 29750f71a9b4 ("hugetlb_cgroup: add hugetlb_cgroup reservation tests")
Cc Andrew Morton <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Muchun Song <[email protected]>
Cc Mina Almasry <[email protected]>
Quoted 3 lines

Initially I thought it could be nice to split fixes for the 2 issues in separate patches in case one of them ends up needing a revert or something, but probably not worth a respin. Fixes look good to me.

I was debating with myself as well if it should be separated, but decided to go the simple route of a single patch :)

Quoted 2 lines

Thanks!

Signature · 3 lines
Cheers,

David / dhildenb
#3 lore raw
In reply to David Hildenbrand

This patch has been successfully tested. All hugetlb kernel selftests pass now. # SUMMARY: PASS=12 SKIP=0 FAIL=0

Tested by: Mario Casquero <[email protected]>

Quoted 113 lines · David Hildenbrand

Currently, running the charge_reserved_hugetlb.sh selftest we can sometimes observe something like:

  $ ./charge_reserved_hugetlb.sh -cgroup-v2
  ...
  write_result is 0
  After write:
  hugetlb_usage=0
  reserved_usage=10485760
  killing write_to_hugetlbfs
  Received 2.
  Deleting the memory
  Detach failure: Invalid argument
  umount: /mnt/huge: target is busy.

Both cases are issues in the test.

While the unmount error seems to be racy, it will make the test fail:

        $ ./run_vmtests.sh -t hugetlb
        ...
        # [FAIL]
        not ok 10 charge_reserved_hugetlb.sh -cgroup-v2 # exit=32

The issue is that we are not waiting for the write_to_hugetlbfs process to quit. So it might still have a hugetlbfs file open, about which umount is not happy. Fix that by making "killall" wait for the process to quit.

The other error ("Detach failure: Invalid argument") does not seem to result in a test error, but is misleading. Turns out write_to_hugetlbfs.c unconditionally tries to cleanup using shmdt(), even when we only mmap()'ed a hugetlb file. Even worse, shmaddr is never even set for the SHM case. Fix that as well.

With this change it seems to work as expected.

Fixes 29750f71a9b4 ("hugetlb_cgroup: add hugetlb_cgroup reservation tests")
Cc Andrew Morton <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Muchun Song <[email protected]>
Cc Mina Almasry <[email protected]>
---
 .../selftests/mm/charge_reserved_hugetlb.sh   |  2 +-
 .../testing/selftests/mm/write_to_hugetlbfs.c | 21 +++++++++++--------
 2 files changed, 13 insertions(+), 10 deletions(-)
tools/testing/selftests/mm/charge_reserved_hugetlb.sh +1 -1
index d680c00d2853a..67df7b47087f0 100755
@@ -254,7 +254,7 @@ function cleanup_hugetlb_memory() {
   local cgroup="$1"
   if [[ "$(pgrep -f write_to_hugetlbfs)" != "" ]]; then
     echo killing write_to_hugetlbfs
-    killall -2 write_to_hugetlbfs
+    killall -2 --wait write_to_hugetlbfs
     wait_for_hugetlb_memory_to_get_depleted $cgroup
   fi
   set -e
tools/testing/selftests/mm/write_to_hugetlbfs.c +12 -10
index 6a2caba19ee1d..1289d311efd70 100644
@@ -28,7 +28,7 @@ enum method {
 
 /* Global variables. */
 static const char *self;
-static char *shmaddr;
+static int *shmaddr;
 static int shmid;
 
 /*
@@ -47,15 +47,17 @@ void sig_handler(int signo)
 {
        printf("Received %d.\n", signo);
        if (signo == SIGINT) {
-               printf("Deleting the memory\n");
-               if (shmdt((const void *)shmaddr) != 0) {
-                       perror("Detach failure");
+               if (shmaddr) {
+                       printf("Deleting the memory\n");
+                       if (shmdt((const void *)shmaddr) != 0) {
+                               perror("Detach failure");
+                               shmctl(shmid, IPC_RMID, NULL);
+                               exit(4);
+                       }
+
                        shmctl(shmid, IPC_RMID, NULL);
-                       exit(4);
+                       printf("Done deleting the memory\n");
                }
-
-               shmctl(shmid, IPC_RMID, NULL);
-               printf("Done deleting the memory\n");
        }
        exit(2);
 }
@@ -211,7 +213,8 @@ int main(int argc, char **argv)
                        shmctl(shmid, IPC_RMID, NULL);
                        exit(2);
                }
-               printf("shmaddr: %p\n", ptr);
+               shmaddr = ptr;
+               printf("shmaddr: %p\n", shmaddr);
 
                break;
        default:
--

2.46.0

Tested-by
#4 lore raw

Missing the hyphen :)

Quoted 121 lines · Mario Casquero

This patch has been successfully tested. All hugetlb kernel selftests pass now. # SUMMARY: PASS=12 SKIP=0 FAIL=0

Tested by: Mario Casquero <[email protected]>

Quoted 113 lines · David Hildenbrand

Currently, running the charge_reserved_hugetlb.sh selftest we can sometimes observe something like:

  $ ./charge_reserved_hugetlb.sh -cgroup-v2
  ...
  write_result is 0
  After write:
  hugetlb_usage=0
  reserved_usage=10485760
  killing write_to_hugetlbfs
  Received 2.
  Deleting the memory
  Detach failure: Invalid argument
  umount: /mnt/huge: target is busy.

Both cases are issues in the test.

While the unmount error seems to be racy, it will make the test fail:

        $ ./run_vmtests.sh -t hugetlb
        ...
        # [FAIL]
        not ok 10 charge_reserved_hugetlb.sh -cgroup-v2 # exit=32

The issue is that we are not waiting for the write_to_hugetlbfs process to quit. So it might still have a hugetlbfs file open, about which umount is not happy. Fix that by making "killall" wait for the process to quit.

The other error ("Detach failure: Invalid argument") does not seem to result in a test error, but is misleading. Turns out write_to_hugetlbfs.c unconditionally tries to cleanup using shmdt(), even when we only mmap()'ed a hugetlb file. Even worse, shmaddr is never even set for the SHM case. Fix that as well.

With this change it seems to work as expected.

Fixes 29750f71a9b4 ("hugetlb_cgroup: add hugetlb_cgroup reservation tests")
Cc Andrew Morton <[email protected]>
Cc Shuah Khan <[email protected]>
Cc Muchun Song <[email protected]>
Cc Mina Almasry <[email protected]>
---
 .../selftests/mm/charge_reserved_hugetlb.sh   |  2 +-
 .../testing/selftests/mm/write_to_hugetlbfs.c | 21 +++++++++++--------
 2 files changed, 13 insertions(+), 10 deletions(-)
tools/testing/selftests/mm/charge_reserved_hugetlb.sh +1 -1
index d680c00d2853a..67df7b47087f0 100755
@@ -254,7 +254,7 @@ function cleanup_hugetlb_memory() {
   local cgroup="$1"
   if [[ "$(pgrep -f write_to_hugetlbfs)" != "" ]]; then
     echo killing write_to_hugetlbfs
-    killall -2 write_to_hugetlbfs
+    killall -2 --wait write_to_hugetlbfs
     wait_for_hugetlb_memory_to_get_depleted $cgroup
   fi
   set -e
tools/testing/selftests/mm/write_to_hugetlbfs.c +12 -10
index 6a2caba19ee1d..1289d311efd70 100644
@@ -28,7 +28,7 @@ enum method {
 
 /* Global variables. */
 static const char *self;
-static char *shmaddr;
+static int *shmaddr;
 static int shmid;
 
 /*
@@ -47,15 +47,17 @@ void sig_handler(int signo)
 {
        printf("Received %d.\n", signo);
        if (signo == SIGINT) {
-               printf("Deleting the memory\n");
-               if (shmdt((const void *)shmaddr) != 0) {
-                       perror("Detach failure");
+               if (shmaddr) {
+                       printf("Deleting the memory\n");
+                       if (shmdt((const void *)shmaddr) != 0) {
+                               perror("Detach failure");
+                               shmctl(shmid, IPC_RMID, NULL);
+                               exit(4);
+                       }
+
                        shmctl(shmid, IPC_RMID, NULL);
-                       exit(4);
+                       printf("Done deleting the memory\n");
                }
-
-               shmctl(shmid, IPC_RMID, NULL);
-               printf("Done deleting the memory\n");
        }
        exit(2);
 }
@@ -211,7 +213,8 @@ int main(int argc, char **argv)
                        shmctl(shmid, IPC_RMID, NULL);
                        exit(2);
                }
-               printf("shmaddr: %p\n", ptr);
+               shmaddr = ptr;
+               printf("shmaddr: %p\n", shmaddr);
 
                break;
        default:
--

2.46.0