From: Ackerley Tng <[email protected]>

Test that multiple guest_memfd instances can be created backed by the
same tmpfs resource pool descriptor, that each file descriptor reflects
its requested size, and that they have distinct inodes.

Signed-off-by: Ackerley Tng <[email protected]>
---
 tools/testing/selftests/kvm/guest_memfd_test.c | 36 ++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c 
b/tools/testing/selftests/kvm/guest_memfd_test.c
index 94ef64474f2a0..81311be059923 100644
--- a/tools/testing/selftests/kvm/guest_memfd_test.c
+++ b/tools/testing/selftests/kvm/guest_memfd_test.c
@@ -543,6 +543,41 @@ static void test_resource_fd_tmpfs_huge(struct kvm_vm *vm)
        close(pool_fd);
 }
 
+static void test_resource_fd_tmpfs_shared_pool(struct kvm_vm *vm)
+{
+       int pool_fd, fd1, fd2;
+       struct stat st1, st2;
+
+       pool_fd = create_tmpfs_pool_fd("never", true, page_size * 2);
+       if (pool_fd < 0)
+               TEST_REQUIRE(false);
+
+       fd1 = vm_create_guest_memfd_resource(vm, page_size,
+                                            GUEST_MEMFD_FLAG_USE_RESOURCE,
+                                            pool_fd);
+
+       fd2 = vm_create_guest_memfd_resource(vm, page_size * 2,
+                                            GUEST_MEMFD_FLAG_USE_RESOURCE,
+                                            pool_fd);
+
+       TEST_ASSERT(!fstat(fd1, &st1), "fstat on fd1 should succeed");
+       TEST_ASSERT(st1.st_size == page_size,
+                   "fd1 st_size (%lu) should match requested size (%lu)",
+                   st1.st_size, page_size);
+
+       TEST_ASSERT(!fstat(fd2, &st2), "fstat on fd2 should succeed");
+       TEST_ASSERT(st2.st_size == page_size * 2,
+                   "fd2 st_size (%lu) should match requested size (%lu)",
+                   st2.st_size, page_size * 2);
+
+       TEST_ASSERT(st1.st_ino != st2.st_ino,
+                   "different guest_memfd instances should have distinct 
inodes");
+
+       close(fd2);
+       close(fd1);
+       close(pool_fd);
+}
+
 enum gmem_pool_type {
        GMEM_POOL_NONE,
        GMEM_POOL_FSMOUNT,
@@ -673,6 +708,7 @@ static void test_guest_memfd(unsigned long vm_type)
        test_resource_fd_tmpfs_file(vm);
        test_resource_fd_tmpfs_swap(vm);
        test_resource_fd_tmpfs_huge(vm);
+       test_resource_fd_tmpfs_shared_pool(vm);
 
        __test_guest_memfd(vm, 0, GMEM_POOL_NONE);
        __test_guest_memfd(vm, GUEST_MEMFD_FLAG_USE_RESOURCE, 
GMEM_POOL_FSMOUNT);

-- 
2.56.0.rc1.315.gc6ed9934b7-goog



Reply via email to