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

selftests/vm: use memfd for hugepage-mremap test

For dropping the hugetlb mountpoint in run_vmtests.sh. Cleaned it up a
little bit around the changed codes.

Link: https://lkml.kernel.org/r/20221014144013.94027-1-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
Cc: Axel Rasmussen <axelrasmussen@google.com>
Cc: Mike Kravetz <mike.kravetz@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Peter Xu and committed by
Andrew Morton
4705700d 62f33fa2

+9 -12
+9 -12
tools/testing/selftests/vm/hugepage-mremap.c
··· 22 22 #include <sys/syscall.h> /* Definition of SYS_* constants */ 23 23 #include <linux/userfaultfd.h> 24 24 #include <sys/ioctl.h> 25 + #include <string.h> 25 26 26 27 #define DEFAULT_LENGTH_MB 10UL 27 28 #define MB_TO_BYTES(x) (x * 1024 * 1024) ··· 109 108 int main(int argc, char *argv[]) 110 109 { 111 110 size_t length = 0; 111 + int ret = 0, fd; 112 112 113 - if (argc != 2 && argc != 3) { 114 - printf("Usage: %s [length_in_MB] <hugetlb_file>\n", argv[0]); 113 + if (argc >= 2 && !strcmp(argv[1], "-h")) { 114 + printf("Usage: %s [length_in_MB]\n", argv[0]); 115 115 exit(1); 116 116 } 117 117 118 118 /* Read memory length as the first arg if valid, otherwise fallback to 119 119 * the default length. 120 120 */ 121 - if (argc == 3) 122 - length = argc > 2 ? (size_t)atoi(argv[1]) : 0UL; 121 + if (argc >= 2) 122 + length = (size_t)atoi(argv[1]); 123 + else 124 + length = DEFAULT_LENGTH_MB; 123 125 124 - length = length > 0 ? length : DEFAULT_LENGTH_MB; 125 126 length = MB_TO_BYTES(length); 126 - 127 - int ret = 0; 128 - 129 - /* last arg is the hugetlb file name */ 130 - int fd = open(argv[argc-1], O_CREAT | O_RDWR, 0755); 131 - 127 + fd = memfd_create(argv[0], MFD_HUGETLB); 132 128 if (fd < 0) { 133 129 perror("Open failed"); 134 130 exit(1); ··· 183 185 } 184 186 185 187 close(fd); 186 - unlink(argv[argc-1]); 187 188 188 189 return ret; 189 190 }