[RFC PATCH v3 5/5] KVM: selftests: Test transferring private memory to another VM
From: Tao Gong
Date: Mon Oct 26 2026 - 02:46:01 EST
From: Ackerley Tng <ackerleytng@xxxxxxxxxx>
Add private_mem_migrate_tests to verify that private memory backed by
guest_memfd can be linked into another VM of the same type
(KVM_X86_SW_PROTECTED_VM) on the same host via KVM_LINK_GUEST_MEMFD,
and that data written by the source VM is visible to the destination VM.
Also verify that linking a guest_memfd into a VM of a different VM type
is rejected with EINVAL.
Signed-off-by: Ackerley Tng <ackerleytng@xxxxxxxxxx>
Co-developed-by: Ryan Afranji <afranji@xxxxxxxxxx>
Signed-off-by: Ryan Afranji <afranji@xxxxxxxxxx>
Co-developed-by: Tao Gong <gongtao@xxxxxxxxxx>
Signed-off-by: Tao Gong <gongtao@xxxxxxxxxx>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../kvm/x86/private_mem_migrate_tests.c | 118 ++++++++++++++++++
2 files changed, 119 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86/private_mem_migrate_tests.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 239bc61ea384f..cffa0d165a945 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -108,6 +108,7 @@ TEST_GEN_PROGS_x86 += x86/pmu_counters_test
TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test
TEST_GEN_PROGS_x86 += x86/private_mem_conversions_test
TEST_GEN_PROGS_x86 += x86/private_mem_kvm_exits_test
+TEST_GEN_PROGS_x86 += x86/private_mem_migrate_tests
TEST_GEN_PROGS_x86 += x86/save_restore_pf_stress_test
TEST_GEN_PROGS_x86 += x86/set_boot_cpu_id
TEST_GEN_PROGS_x86 += x86/set_sregs_test
diff --git a/tools/testing/selftests/kvm/x86/private_mem_migrate_tests.c b/tools/testing/selftests/kvm/x86/private_mem_migrate_tests.c
new file mode 100644
index 0000000000000..17bebf850b4c4
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/private_mem_migrate_tests.c
@@ -0,0 +1,118 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "kvm_util.h"
+#include "test_util.h"
+#include "ucall_common.h"
+#include <linux/kvm.h>
+#include <linux/sizes.h>
+
+#define TRANSFER_PRIVATE_MEM_TEST_SLOT 10
+#define TRANSFER_PRIVATE_MEM_GPA ((uint64_t)(1ull << 32))
+#define TRANSFER_PRIVATE_MEM_GVA TRANSFER_PRIVATE_MEM_GPA
+#define TRANSFER_PRIVATE_MEM_VALUE 0xdeadbeef
+
+static const struct vm_shape protected_shape = {
+ .mode = VM_MODE_DEFAULT,
+ .type = KVM_X86_SW_PROTECTED_VM,
+};
+
+static void transfer_private_mem_guest_code_src(void)
+{
+ uint64_t *ptr = (uint64_t *)TRANSFER_PRIVATE_MEM_GVA;
+
+ WRITE_ONCE(*ptr, TRANSFER_PRIVATE_MEM_VALUE);
+
+ GUEST_SYNC1(READ_ONCE(*ptr));
+}
+
+static void transfer_private_mem_guest_code_dst(void)
+{
+ uint64_t *ptr = (uint64_t *)TRANSFER_PRIVATE_MEM_GVA;
+
+ GUEST_SYNC1(READ_ONCE(*ptr));
+}
+
+static void add_private_mem(struct kvm_vm *vm, int memfd)
+{
+ vm_mem_add(vm, DEFAULT_VM_MEM_SRC, TRANSFER_PRIVATE_MEM_GPA,
+ TRANSFER_PRIVATE_MEM_TEST_SLOT, 1, KVM_MEM_GUEST_MEMFD,
+ memfd, 0, 0);
+
+ virt_map(vm, TRANSFER_PRIVATE_MEM_GVA, TRANSFER_PRIVATE_MEM_GPA, 1);
+
+ /*
+ * guest_memfd memory is private by default if guest_memfd tracks
+ * memory attributes, otherwise it must be converted via the VM.
+ */
+ if (!kvm_has_gmem_attributes)
+ vm_mem_set_private(vm, TRANSFER_PRIVATE_MEM_GPA, SZ_4K);
+}
+
+static void test_transfer_private_mem(void)
+{
+ struct kvm_vm *src_vm, *dst_vm;
+ struct kvm_vcpu *src_vcpu, *dst_vcpu;
+ int src_memfd, dst_memfd;
+ struct ucall uc;
+
+ /* Build the source VM, use it to write to private memory */
+ src_vm = __vm_create_shape_with_one_vcpu(protected_shape, &src_vcpu, 0,
+ transfer_private_mem_guest_code_src);
+ src_memfd = vm_create_guest_memfd(src_vm, SZ_4K, 0);
+
+ add_private_mem(src_vm, src_memfd);
+
+ vcpu_run(src_vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(src_vcpu, KVM_EXIT_IO);
+ get_ucall(src_vcpu, &uc);
+ TEST_ASSERT(uc.args[0] == TRANSFER_PRIVATE_MEM_VALUE,
+ "Source VM should be able to write to private memory");
+
+ /* Build the destination VM with linked fd */
+ dst_vm = __vm_create_shape_with_one_vcpu(protected_shape, &dst_vcpu, 0,
+ transfer_private_mem_guest_code_dst);
+ dst_memfd = vm_link_guest_memfd(dst_vm, src_memfd, 0);
+
+ add_private_mem(dst_vm, dst_memfd);
+
+ vcpu_run(dst_vcpu);
+ TEST_ASSERT_KVM_EXIT_REASON(dst_vcpu, KVM_EXIT_IO);
+ get_ucall(dst_vcpu, &uc);
+ TEST_ASSERT(uc.args[0] == TRANSFER_PRIVATE_MEM_VALUE,
+ "Destination VM should be able to read value transferred");
+
+ close(dst_memfd);
+ close(src_memfd);
+ kvm_vm_free(dst_vm);
+ kvm_vm_free(src_vm);
+}
+
+static void test_link_different_vm_type(void)
+{
+ struct kvm_vm *src_vm, *dst_vm;
+ int src_memfd, ret;
+
+ src_vm = vm_create_barebones_type(KVM_X86_SW_PROTECTED_VM);
+ src_memfd = vm_create_guest_memfd(src_vm, SZ_4K, 0);
+
+ dst_vm = vm_create_barebones_type(KVM_X86_DEFAULT_VM);
+
+ ret = __vm_link_guest_memfd(dst_vm, src_memfd, 0);
+ TEST_ASSERT(ret == -1 && errno == EINVAL,
+ "Linking into a VM of a different type should fail, ret: %d, errno: %d",
+ ret, errno);
+
+ close(src_memfd);
+ kvm_vm_free(dst_vm);
+ kvm_vm_free(src_vm);
+}
+
+int main(int argc, char *argv[])
+{
+ TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM));
+ TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD_LINK));
+
+ test_transfer_private_mem();
+ test_link_different_vm_type();
+
+ return 0;
+}
--
2.56.0.257.gf7497c7d27-goog