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

KVM: selftests: add shared hugetlbfs backing source type

This lets us run the demand paging test on top of a shared
hugetlbfs-backed area. The "shared" is key, as this allows us to
exercise userfaultfd minor faults on hugetlbfs.

Signed-off-by: Axel Rasmussen <axelrasmussen@google.com>
Message-Id: <20210519200339.829146-11-axelrasmussen@google.com>
Reviewed-by: Ben Gardon <bgardon@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Axel Rasmussen and committed by
Paolo Bonzini
33090a88 a4b9722a

+33 -4
+4 -2
tools/testing/selftests/kvm/demand_paging_test.c
··· 484 484 } 485 485 } 486 486 487 - TEST_ASSERT(p.uffd_mode != UFFDIO_REGISTER_MODE_MINOR || p.src_type == VM_MEM_SRC_SHMEM, 488 - "userfaultfd MINOR mode requires shared memory; pick a different -t"); 487 + if (p.uffd_mode == UFFDIO_REGISTER_MODE_MINOR && 488 + !backing_src_is_shared(p.src_type)) { 489 + TEST_FAIL("userfaultfd MINOR mode requires shared memory; pick a different -t"); 490 + } 489 491 490 492 for_each_guest_mode(run_test, &p); 491 493
+11
tools/testing/selftests/kvm/include/test_util.h
··· 17 17 #include <errno.h> 18 18 #include <unistd.h> 19 19 #include <fcntl.h> 20 + #include <sys/mman.h> 20 21 #include "kselftest.h" 21 22 22 23 static inline int _no_printf(const char *format, ...) { return 0; } ··· 86 85 VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB, 87 86 VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB, 88 87 VM_MEM_SRC_SHMEM, 88 + VM_MEM_SRC_SHARED_HUGETLB, 89 89 NUM_SRC_TYPES, 90 90 }; 91 91 ··· 102 100 size_t get_backing_src_pagesz(uint32_t i); 103 101 void backing_src_help(void); 104 102 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name); 103 + 104 + /* 105 + * Whether or not the given source type is shared memory (as opposed to 106 + * anonymous). 107 + */ 108 + static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t) 109 + { 110 + return vm_mem_backing_src_alias(t)->flag & MAP_SHARED; 111 + } 105 112 106 113 #endif /* SELFTEST_KVM_TEST_UTIL_H */
+7 -2
tools/testing/selftests/kvm/lib/kvm_util.c
··· 848 848 region->mmap_size += alignment; 849 849 850 850 region->fd = -1; 851 - if (src_type == VM_MEM_SRC_SHMEM) { 852 - region->fd = memfd_create("kvm_selftest", MFD_CLOEXEC); 851 + if (backing_src_is_shared(src_type)) { 852 + int memfd_flags = MFD_CLOEXEC; 853 + 854 + if (src_type == VM_MEM_SRC_SHARED_HUGETLB) 855 + memfd_flags |= MFD_HUGETLB; 856 + 857 + region->fd = memfd_create("kvm_selftest", memfd_flags); 853 858 TEST_ASSERT(region->fd != -1, 854 859 "memfd_create failed, errno: %i", errno); 855 860
+11
tools/testing/selftests/kvm/lib/test_util.c
··· 240 240 .name = "shmem", 241 241 .flag = MAP_SHARED, 242 242 }, 243 + [VM_MEM_SRC_SHARED_HUGETLB] = { 244 + .name = "shared_hugetlb", 245 + /* 246 + * No MAP_HUGETLB, we use MFD_HUGETLB instead. Since 247 + * we're using "file backed" memory, we need to specify 248 + * this when the FD is created, not when the area is 249 + * mapped. 250 + */ 251 + .flag = MAP_SHARED, 252 + }, 243 253 }; 244 254 _Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES, 245 255 "Missing new backing src types?"); ··· 272 262 case VM_MEM_SRC_ANONYMOUS_THP: 273 263 return get_trans_hugepagesz(); 274 264 case VM_MEM_SRC_ANONYMOUS_HUGETLB: 265 + case VM_MEM_SRC_SHARED_HUGETLB: 275 266 return get_def_hugetlb_pagesz(); 276 267 default: 277 268 return MAP_HUGE_PAGE_SIZE(flag);