[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <a6578d60b14a94b9895272c202cc47025257fc9b.1691446946.git.ackerleytng@google.com>
Date: Mon, 7 Aug 2023 23:01:06 +0000
From: Ackerley Tng <ackerleytng@...gle.com>
To: pbonzini@...hat.com, seanjc@...gle.com, tglx@...utronix.de,
x86@...nel.org, kvm@...r.kernel.org, linux-kernel@...r.kernel.org,
linux-kselftest@...r.kernel.org
Cc: mingo@...hat.com, bp@...en8.de, dave.hansen@...ux.intel.com,
hpa@...or.com, shuah@...nel.org, andrew.jones@...ux.dev,
ricarkol@...gle.com, chao.p.peng@...ux.intel.com, tabba@...gle.com,
jarkko@...nel.org, yu.c.zhang@...ux.intel.com,
vannapurve@...gle.com, ackerleytng@...gle.com,
erdemaktas@...gle.com, mail@...iej.szmigiero.name, vbabka@...e.cz,
david@...hat.com, qperret@...gle.com, michael.roth@....com,
wei.w.wang@...el.com, liam.merwick@...cle.com,
isaku.yamahata@...il.com, kirill.shutemov@...ux.intel.com
Subject: [RFC PATCH 03/11] KVM: selftests: Add tests for KVM_LINK_GUEST_MEMFD ioctl
Test that
+ Invalid inputs should be rejected with EINVAL
+ Successful inputs return a new (destination) fd
+ Destination and source fds have the same inode number
+ No crash on program exit
Signed-off-by: Ackerley Tng <ackerleytng@...gle.com>
---
.../testing/selftests/kvm/guest_memfd_test.c | 42 +++++++++++++++++++
.../selftests/kvm/include/kvm_util_base.h | 18 ++++++++
2 files changed, 60 insertions(+)
diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c
index ad20f11b2d2c..38fe96ea60f9 100644
--- a/tools/testing/selftests/kvm/guest_memfd_test.c
+++ b/tools/testing/selftests/kvm/guest_memfd_test.c
@@ -105,6 +105,47 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm, size_t page_size)
ASSERT_EQ(errno, EINVAL);
}
+static void test_link(struct kvm_vm *src_vm, int src_fd, size_t total_size)
+{
+ int ret;
+ int dst_fd;
+ struct kvm_vm *dst_vm;
+ struct stat src_stat;
+ struct stat dst_stat;
+
+ dst_vm = vm_create_barebones();
+
+ /* Linking with a nonexistent fd */
+ dst_fd = __vm_link_guest_memfd(dst_vm, 99, 0);
+ ASSERT_EQ(dst_fd, -1);
+ ASSERT_EQ(errno, EINVAL);
+
+ /* Linking with a non-gmem fd */
+ dst_fd = __vm_link_guest_memfd(dst_vm, 0, 1);
+ ASSERT_EQ(dst_fd, -1);
+ ASSERT_EQ(errno, EINVAL);
+
+ /* Linking with invalid flags */
+ dst_fd = __vm_link_guest_memfd(dst_vm, src_fd, 1);
+ ASSERT_EQ(dst_fd, -1);
+ ASSERT_EQ(errno, EINVAL);
+
+ /* Linking with an already-associated vm */
+ dst_fd = __vm_link_guest_memfd(src_vm, src_fd, 1);
+ ASSERT_EQ(dst_fd, -1);
+ ASSERT_EQ(errno, EINVAL);
+
+ dst_fd = __vm_link_guest_memfd(dst_vm, src_fd, 0);
+ TEST_ASSERT(dst_vm > 0, "linking should succeed with valid inputs");
+ TEST_ASSERT(src_fd != dst_fd, "linking should return a different fd");
+
+ ret = fstat(src_fd, &src_stat);
+ ASSERT_EQ(ret, 0);
+ ret = fstat(dst_fd, &dst_stat);
+ ASSERT_EQ(ret, 0);
+ TEST_ASSERT(src_stat.st_ino == dst_stat.st_ino,
+ "src and dst files should have the same inode number");
+}
int main(int argc, char *argv[])
{
@@ -126,6 +167,7 @@ int main(int argc, char *argv[])
test_mmap(fd, page_size);
test_file_size(fd, page_size, total_size);
test_fallocate(fd, page_size, total_size);
+ test_link(vm, fd, total_size);
close(fd);
}
diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h
index 8bdfadd72349..868925b26a7b 100644
--- a/tools/testing/selftests/kvm/include/kvm_util_base.h
+++ b/tools/testing/selftests/kvm/include/kvm_util_base.h
@@ -495,6 +495,24 @@ static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
return fd;
}
+static inline int __vm_link_guest_memfd(struct kvm_vm *vm, int fd, uint64_t flags)
+{
+ struct kvm_link_guest_memfd params = {
+ .fd = fd,
+ .flags = flags,
+ };
+
+ return __vm_ioctl(vm, KVM_LINK_GUEST_MEMFD, ¶ms);
+}
+
+static inline int vm_link_guest_memfd(struct kvm_vm *vm, int fd, uint64_t flags)
+{
+ int new_fd = __vm_link_guest_memfd(vm, fd, flags);
+
+ TEST_ASSERT(new_fd >= 0, KVM_IOCTL_ERROR(KVM_LINK_GUEST_MEMFD, new_fd));
+ return new_fd;
+}
+
void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
uint64_t gpa, uint64_t size, void *hva);
int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags,
--
2.41.0.640.ga95def55d0-goog
Powered by blists - more mailing lists