[PATCH v2 10/13] KVM: selftests: Isolate the guest_memfd Copy-on-Write negative testcase

Sean Christopherson
2025-10-03T23:26:29Z

Move the guest_memfd Copy-on-Write (CoW) testcase to its own function to
better separate positive testcases from negative testcases.

No functional change intended.

Suggested-by: Ackerley Tng <ackerleytng@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
tools/testing/selftests/kvm/guest_memfd_test.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c
index 319fda4f5d53..640636c76eb9 100644
--- a/tools/testing/selftests/kvm/guest_memfd_test.c
+++ b/tools/testing/selftests/kvm/guest_memfd_test.c
@@ -40,15 +40,20 @@ static void test_file_read_write(int fd, size_t total_size)
"pwrite on a guest_mem fd should fail");
}

-static void test_mmap_supported(int fd, size_t total_size)
+static void test_mmap_cow(int fd, size_t size)
{
- const char val = 0xaa;
- char *mem;
- size_t i;
- int ret;
+ void *mem;

- mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+ mem = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
TEST_ASSERT(mem == MAP_FAILED, "Copy-on-write not allowed by guest_memfd.");
+}
+
+static void test_mmap_supported(int fd, size_t total_size)
+{
+ const char val = 0xaa;
+ char *mem;
+ size_t i;
+ int ret;

mem = kvm_mmap(total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd);

@@ -272,6 +277,7 @@ static void __test_guest_memfd(struct kvm_vm *vm, uint64_t flags)

if (flags & GUEST_MEMFD_FLAG_MMAP) {
gmem_test(mmap_supported, vm, flags);
+ gmem_test(mmap_cow, vm, flags);
gmem_test(fault_overflow, vm, flags);
} else {
gmem_test(mmap_not_supported, vm, flags);
--
2.51.0.618.g983fd99d29-goog