[PATCH 11/12] KVM: selftests: lib: Add src memory type for hwpoison test

From: isaku . yamahata
Date: Tue Oct 10 2023 - 04:36:34 EST


From: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>

For hwpoison test to recover hwpoisoned memory, use madvise(MADV_FREE) that
requires private mapping. Add a new src memory type for it.

Signed-off-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>
---
tools/testing/selftests/kvm/include/test_util.h | 2 ++
tools/testing/selftests/kvm/lib/kvm_util.c | 7 +++++--
tools/testing/selftests/kvm/lib/test_util.c | 8 ++++++++
3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index 7e614adc6cf4..c97af4ff0699 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -111,6 +111,8 @@ enum vm_mem_backing_src_type {
VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB,
VM_MEM_SRC_SHMEM,
VM_MEM_SRC_SHARED_HUGETLB,
+ VM_MEM_SRC_ANONYMOUS_MEMFD,
+ VM_MEM_SRC_ANONYMOUS_MEMFD_HUGETLB,
NUM_SRC_TYPES,
};

diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 310c3a760cb8..95ffb3b97dcf 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1007,9 +1007,12 @@ void __vm_userspace_mem_region_add(struct kvm_vm *vm,
region->mmap_size += alignment;

region->fd = -1;
- if (backing_src_is_shared(src_type))
+ if (backing_src_is_shared(src_type) ||
+ src_type == VM_MEM_SRC_ANONYMOUS_MEMFD ||
+ src_type == VM_MEM_SRC_ANONYMOUS_MEMFD_HUGETLB)
region->fd = kvm_memfd_alloc(region->mmap_size,
- src_type == VM_MEM_SRC_SHARED_HUGETLB);
+ src_type == VM_MEM_SRC_SHARED_HUGETLB ||
+ src_type == VM_MEM_SRC_ANONYMOUS_MEMFD_HUGETLB);

region->mmap_start = mmap(NULL, region->mmap_size,
PROT_READ | PROT_WRITE,
diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index 5d7f28b02d73..a09bba5c074e 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -281,6 +281,14 @@ const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)
*/
.flag = MAP_SHARED,
},
+ [VM_MEM_SRC_ANONYMOUS_MEMFD] = {
+ .name = "anonymous_memfd",
+ .flag = ANON_FLAGS,
+ },
+ [VM_MEM_SRC_ANONYMOUS_MEMFD_HUGETLB] = {
+ .name = "anonymous_memfd_hugetlb",
+ .flag = ANON_HUGE_FLAGS,
+ },
};
_Static_assert(ARRAY_SIZE(aliases) == NUM_SRC_TYPES,
"Missing new backing src types?");
--
2.25.1