]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: selftests: Test guest_memfd binding overlap without GPA overlap
authorZongyao Chen <ZongYao.Chen@linux.alibaba.com>
Fri, 22 May 2026 17:21:50 +0000 (10:21 -0700)
committerSean Christopherson <seanjc@google.com>
Tue, 26 May 2026 19:23:36 +0000 (12:23 -0700)
The guest_memfd binding overlap test recreates the deleted slot with GPA
ranges that overlap the still-live slot.  KVM rejects those attempts from
the generic memslot overlap check before reaching kvm_gmem_bind(), so the
test can pass even if guest_memfd binding overlap detection is broken.

Recreate the slot at its original, non-overlapping GPA and use guest_memfd
offsets that overlap the front and back halves of the other slot's binding.
Expand the guest_memfd so the back-half case remains within the file size.

Fixes: 2feabb855df8 ("KVM: selftests: Expand set_memory_region_test to validate guest_memfd()")
Signed-off-by: Zongyao Chen <ZongYao.Chen@linux.alibaba.com>
Reviewed-by: Ackerley Tng <ackerleytng@google.com>
Tested-by: Ackerley Tng <ackerleytng@google.com>
[sean: keep the existing GPA overlap testcases]
Link: https://patch.msgid.link/20260522172151.3530267-3-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/set_memory_region_test.c

index 9b919a231c93709695466a1f0534ff0d0303a303..be99c1ff5a5a99217d79d2f2e6eac00c233dad3b 100644 (file)
@@ -510,7 +510,7 @@ static void test_add_overlapping_private_memory_regions(void)
 
        vm = vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM);
 
-       memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE * 4, 0);
+       memfd = vm_create_guest_memfd(vm, MEM_REGION_SIZE * 5, 0);
 
        vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
                                   MEM_REGION_GPA, MEM_REGION_SIZE * 2, 0, memfd, 0);
@@ -526,7 +526,30 @@ static void test_add_overlapping_private_memory_regions(void)
        vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
                                   MEM_REGION_GPA, 0, NULL, -1, 0);
 
-       /* Overlap the front half of the other slot. */
+       /*
+        * Verify that overlap in the guest_memfd bindings (i.e. in guest_memfd
+        * file offsets), but _not_ in the GPA space, fails with -EEXIST.
+        */
+       r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
+                                        MEM_REGION_GPA,
+                                        MEM_REGION_SIZE * 2,
+                                        0, memfd, MEM_REGION_SIZE);
+       TEST_ASSERT(r == -1 && errno == EEXIST,
+                   "Overlapping guest_memfd() bindings should fail with EEXIST");
+
+       /* And now the back half of the other slot's guest_memfd binding. */
+       r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
+                                        MEM_REGION_GPA,
+                                        MEM_REGION_SIZE * 2,
+                                        0, memfd, MEM_REGION_SIZE * 3);
+       TEST_ASSERT(r == -1 && errno == EEXIST,
+                   "Overlapping guest_memfd() bindings should fail with EEXIST");
+
+       /*
+        * Repeat the overlap tests, but this time with overlap in the memslots
+        * GPA space.  Regardless of where there is overlap, KVM should return
+        * -EEXIST.
+        */
        r = __vm_set_user_memory_region2(vm, MEM_REGION_SLOT, KVM_MEM_GUEST_MEMFD,
                                         MEM_REGION_GPA * 2 - MEM_REGION_SIZE,
                                         MEM_REGION_SIZE * 2,