Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.

Link: https://lkml.kernel.org/r/20240821123115.2068812-1-david@redhat.com
Fixes: 29750f71a9b4 ("hugetlb_cgroup: add hugetlb_cgroup reservation tests")
Signed-off-by: David Hildenbrand <david@redhat.com>
Reported-by: Mario Casquero <mcasquer@redhat.com>
Reviewed-by: Mina Almasry <almasrymina@google.com>
Tested-by: Mario Casquero <mcasquer@redhat.com>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

David Hildenbrand and committed by
Andrew Morton
c41a701d 7a87225a

+14 -11
+1 -1
tools/testing/selftests/mm/charge_reserved_hugetlb.sh
··· 254 254 local cgroup="$1" 255 255 if [[ "$(pgrep -f write_to_hugetlbfs)" != "" ]]; then 256 256 echo killing write_to_hugetlbfs 257 - killall -2 write_to_hugetlbfs 257 + killall -2 --wait write_to_hugetlbfs 258 258 wait_for_hugetlb_memory_to_get_depleted $cgroup 259 259 fi 260 260 set -e
+13 -10
tools/testing/selftests/mm/write_to_hugetlbfs.c
··· 28 28 29 29 /* Global variables. */ 30 30 static const char *self; 31 - static char *shmaddr; 31 + static int *shmaddr; 32 32 static int shmid; 33 33 34 34 /* ··· 47 47 { 48 48 printf("Received %d.\n", signo); 49 49 if (signo == SIGINT) { 50 - printf("Deleting the memory\n"); 51 - if (shmdt((const void *)shmaddr) != 0) { 52 - perror("Detach failure"); 53 - shmctl(shmid, IPC_RMID, NULL); 54 - exit(4); 55 - } 50 + if (shmaddr) { 51 + printf("Deleting the memory\n"); 52 + if (shmdt((const void *)shmaddr) != 0) { 53 + perror("Detach failure"); 54 + shmctl(shmid, IPC_RMID, NULL); 55 + exit(4); 56 + } 56 57 57 - shmctl(shmid, IPC_RMID, NULL); 58 - printf("Done deleting the memory\n"); 58 + shmctl(shmid, IPC_RMID, NULL); 59 + printf("Done deleting the memory\n"); 60 + } 59 61 } 60 62 exit(2); 61 63 } ··· 213 211 shmctl(shmid, IPC_RMID, NULL); 214 212 exit(2); 215 213 } 216 - printf("shmaddr: %p\n", ptr); 214 + shmaddr = ptr; 215 + printf("shmaddr: %p\n", shmaddr); 217 216 218 217 break; 219 218 default: