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

selftests/vm: use memfd for uffd hugetlb tests

Patch series "selftests/vm: Drop hugetlb mntpoint in run_vmtests.sh", v2.

Clean the code up so we can use the same memfd for both hugetlb and shmem
which is cleaner.


This patch (of 4):

We already used memfd for shmem test, move it forward with hugetlb too so
that we don't need user to specify the hugetlb file path explicitly when
running hugetlb shared tests.

Link: https://lkml.kernel.org/r/20221014143921.93887-1-peterx@redhat.com
Link: https://lkml.kernel.org/r/20221014143921.93887-2-peterx@redhat.com
Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Axel Rasmussen <axelrasmussen@google.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
de2baa88 0538a82c

+21 -41
+21 -41
tools/testing/selftests/vm/userfaultfd.c
··· 93 93 static bool test_uffdio_wp = true; 94 94 /* Whether to test uffd minor faults */ 95 95 static bool test_uffdio_minor = false; 96 - 97 96 static bool map_shared; 98 - static int shm_fd; 99 - static int huge_fd; 97 + static int mem_fd; 100 98 static unsigned long long *count_verify; 101 99 static int uffd = -1; 102 100 static int uffd_flags, finished, *pipefd; ··· 141 143 "# Run hugetlb memory test on 256MiB region with 50 bounces:\n" 142 144 "./userfaultfd hugetlb 256 50\n\n" 143 145 "# Run the same hugetlb test but using shared file:\n" 144 - "./userfaultfd hugetlb_shared 256 50 /dev/hugepages/hugefile\n\n" 146 + "./userfaultfd hugetlb_shared 256 50\n\n" 145 147 "# 10MiB-~6GiB 999 bounces anonymous test, " 146 148 "continue forever unless an error triggers\n" 147 149 "while ./userfaultfd anon $[RANDOM % 6000 + 10] 999; do true; done\n\n"; ··· 258 260 259 261 static void hugetlb_allocate_area(void **alloc_area, bool is_src) 260 262 { 263 + off_t size = nr_pages * page_size; 264 + off_t offset = is_src ? 0 : size; 261 265 void *area_alias = NULL; 262 266 char **alloc_area_alias; 263 267 264 - if (!map_shared) 265 - *alloc_area = mmap(NULL, 266 - nr_pages * page_size, 267 - PROT_READ | PROT_WRITE, 268 - MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | 269 - (is_src ? 0 : MAP_NORESERVE), 270 - -1, 271 - 0); 272 - else 273 - *alloc_area = mmap(NULL, 274 - nr_pages * page_size, 275 - PROT_READ | PROT_WRITE, 276 - MAP_SHARED | 277 - (is_src ? 0 : MAP_NORESERVE), 278 - huge_fd, 279 - is_src ? 0 : nr_pages * page_size); 268 + *alloc_area = mmap(NULL, size, PROT_READ | PROT_WRITE, 269 + (map_shared ? MAP_SHARED : MAP_PRIVATE) | 270 + (is_src ? 0 : MAP_NORESERVE), 271 + mem_fd, offset); 280 272 if (*alloc_area == MAP_FAILED) 281 273 err("mmap of hugetlbfs file failed"); 282 274 283 275 if (map_shared) { 284 - area_alias = mmap(NULL, 285 - nr_pages * page_size, 286 - PROT_READ | PROT_WRITE, 287 - MAP_SHARED, 288 - huge_fd, 289 - is_src ? 0 : nr_pages * page_size); 276 + area_alias = mmap(NULL, size, PROT_READ | PROT_WRITE, 277 + MAP_SHARED, mem_fd, offset); 290 278 if (area_alias == MAP_FAILED) 291 279 err("mmap of hugetlb file alias failed"); 292 280 } ··· 318 334 } 319 335 320 336 *alloc_area = mmap(p, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, 321 - shm_fd, offset); 337 + mem_fd, offset); 322 338 if (*alloc_area == MAP_FAILED) 323 339 err("mmap of memfd failed"); 324 340 if (test_collapse && *alloc_area != p) 325 341 err("mmap of memfd failed at %p", p); 326 342 327 343 area_alias = mmap(p_alias, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, 328 - shm_fd, offset); 344 + mem_fd, offset); 329 345 if (area_alias == MAP_FAILED) 330 346 err("mmap of memfd alias failed"); 331 347 if (test_collapse && area_alias != p_alias) ··· 1825 1841 } 1826 1842 nr_pages = nr_pages_per_cpu * nr_cpus; 1827 1843 1828 - if (test_type == TEST_HUGETLB && map_shared) { 1829 - if (argc < 5) 1830 - usage(); 1831 - huge_fd = open(argv[4], O_CREAT | O_RDWR, 0755); 1832 - if (huge_fd < 0) 1833 - err("Open of %s failed", argv[4]); 1834 - if (ftruncate(huge_fd, 0)) 1835 - err("ftruncate %s to size 0 failed", argv[4]); 1836 - } else if (test_type == TEST_SHMEM) { 1837 - shm_fd = memfd_create(argv[0], 0); 1838 - if (shm_fd < 0) 1844 + if (test_type == TEST_SHMEM || test_type == TEST_HUGETLB) { 1845 + unsigned int memfd_flags = 0; 1846 + 1847 + if (test_type == TEST_HUGETLB) 1848 + memfd_flags = MFD_HUGETLB; 1849 + mem_fd = memfd_create(argv[0], memfd_flags); 1850 + if (mem_fd < 0) 1839 1851 err("memfd_create"); 1840 - if (ftruncate(shm_fd, nr_pages * page_size * 2)) 1852 + if (ftruncate(mem_fd, nr_pages * page_size * 2)) 1841 1853 err("ftruncate"); 1842 - if (fallocate(shm_fd, 1854 + if (fallocate(mem_fd, 1843 1855 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1844 1856 nr_pages * page_size * 2)) 1845 1857 err("fallocate");