@@ -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);
}
@@ -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,